ox-ascii.el 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
  2. ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This library implements an ASCII back-end for Org generic exporter.
  19. ;; See Org manual for more information.
  20. ;;; Code:
  21. (eval-when-compile (require 'cl))
  22. (require 'ox)
  23. (require 'ox-publish)
  24. (declare-function aa2u "ext:ascii-art-to-unicode" ())
  25. ;;; Define Back-End
  26. ;;
  27. ;; The following setting won't allow to modify preferred charset
  28. ;; through a buffer keyword or an option item, but, since the property
  29. ;; will appear in communication channel nonetheless, it allows to
  30. ;; override `org-ascii-charset' variable on the fly by the ext-plist
  31. ;; mechanism.
  32. ;;
  33. ;; We also install a filter for headlines and sections, in order to
  34. ;; control blank lines separating them in output string.
  35. (org-export-define-backend 'ascii
  36. '((bold . org-ascii-bold)
  37. (center-block . org-ascii-center-block)
  38. (clock . org-ascii-clock)
  39. (code . org-ascii-code)
  40. (comment . (lambda (&rest args) ""))
  41. (comment-block . (lambda (&rest args) ""))
  42. (drawer . org-ascii-drawer)
  43. (dynamic-block . org-ascii-dynamic-block)
  44. (entity . org-ascii-entity)
  45. (example-block . org-ascii-example-block)
  46. (export-block . org-ascii-export-block)
  47. (export-snippet . org-ascii-export-snippet)
  48. (fixed-width . org-ascii-fixed-width)
  49. (footnote-reference . org-ascii-footnote-reference)
  50. (headline . org-ascii-headline)
  51. (horizontal-rule . org-ascii-horizontal-rule)
  52. (inline-src-block . org-ascii-inline-src-block)
  53. (inlinetask . org-ascii-inlinetask)
  54. (inner-template . org-ascii-inner-template)
  55. (italic . org-ascii-italic)
  56. (item . org-ascii-item)
  57. (keyword . org-ascii-keyword)
  58. (latex-environment . org-ascii-latex-environment)
  59. (latex-fragment . org-ascii-latex-fragment)
  60. (line-break . org-ascii-line-break)
  61. (link . org-ascii-link)
  62. (node-property . org-ascii-node-property)
  63. (paragraph . org-ascii-paragraph)
  64. (plain-list . org-ascii-plain-list)
  65. (plain-text . org-ascii-plain-text)
  66. (planning . org-ascii-planning)
  67. (property-drawer . org-ascii-property-drawer)
  68. (quote-block . org-ascii-quote-block)
  69. (quote-section . org-ascii-quote-section)
  70. (radio-target . org-ascii-radio-target)
  71. (section . org-ascii-section)
  72. (special-block . org-ascii-special-block)
  73. (src-block . org-ascii-src-block)
  74. (statistics-cookie . org-ascii-statistics-cookie)
  75. (strike-through . org-ascii-strike-through)
  76. (subscript . org-ascii-subscript)
  77. (superscript . org-ascii-superscript)
  78. (table . org-ascii-table)
  79. (table-cell . org-ascii-table-cell)
  80. (table-row . org-ascii-table-row)
  81. (target . org-ascii-target)
  82. (template . org-ascii-template)
  83. (timestamp . org-ascii-timestamp)
  84. (underline . org-ascii-underline)
  85. (verbatim . org-ascii-verbatim)
  86. (verse-block . org-ascii-verse-block))
  87. :export-block "ASCII"
  88. :menu-entry
  89. '(?t "Export to Plain Text"
  90. ((?A "As ASCII buffer"
  91. (lambda (a s v b)
  92. (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
  93. (?a "As ASCII file"
  94. (lambda (a s v b)
  95. (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
  96. (?L "As Latin1 buffer"
  97. (lambda (a s v b)
  98. (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
  99. (?l "As Latin1 file"
  100. (lambda (a s v b)
  101. (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
  102. (?U "As UTF-8 buffer"
  103. (lambda (a s v b)
  104. (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
  105. (?u "As UTF-8 file"
  106. (lambda (a s v b)
  107. (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
  108. :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
  109. (:filter-parse-tree org-ascii-filter-paragraph-spacing
  110. org-ascii-filter-comment-spacing)
  111. (:filter-section . org-ascii-filter-headline-blank-lines))
  112. :options-alist '((:ascii-charset nil nil org-ascii-charset)))
  113. ;;; User Configurable Variables
  114. (defgroup org-export-ascii nil
  115. "Options for exporting Org mode files to ASCII."
  116. :tag "Org Export ASCII"
  117. :group 'org-export)
  118. (defcustom org-ascii-text-width 72
  119. "Maximum width of exported text.
  120. This number includes margin size, as set in
  121. `org-ascii-global-margin'."
  122. :group 'org-export-ascii
  123. :version "24.4"
  124. :package-version '(Org . "8.0")
  125. :type 'integer)
  126. (defcustom org-ascii-global-margin 0
  127. "Width of the left margin, in number of characters."
  128. :group 'org-export-ascii
  129. :version "24.4"
  130. :package-version '(Org . "8.0")
  131. :type 'integer)
  132. (defcustom org-ascii-inner-margin 2
  133. "Width of the inner margin, in number of characters.
  134. Inner margin is applied between each headline."
  135. :group 'org-export-ascii
  136. :version "24.4"
  137. :package-version '(Org . "8.0")
  138. :type 'integer)
  139. (defcustom org-ascii-quote-margin 6
  140. "Width of margin used for quoting text, in characters.
  141. This margin is applied on both sides of the text."
  142. :group 'org-export-ascii
  143. :version "24.4"
  144. :package-version '(Org . "8.0")
  145. :type 'integer)
  146. (defcustom org-ascii-inlinetask-width 30
  147. "Width of inline tasks, in number of characters.
  148. This number ignores any margin."
  149. :group 'org-export-ascii
  150. :version "24.4"
  151. :package-version '(Org . "8.0")
  152. :type 'integer)
  153. (defcustom org-ascii-headline-spacing '(1 . 2)
  154. "Number of blank lines inserted around headlines.
  155. This variable can be set to a cons cell. In that case, its car
  156. represents the number of blank lines present before headline
  157. contents whereas its cdr reflects the number of blank lines after
  158. contents.
  159. A nil value replicates the number of blank lines found in the
  160. original Org buffer at the same place."
  161. :group 'org-export-ascii
  162. :version "24.4"
  163. :package-version '(Org . "8.0")
  164. :type '(choice
  165. (const :tag "Replicate original spacing" nil)
  166. (cons :tag "Set an uniform spacing"
  167. (integer :tag "Number of blank lines before contents")
  168. (integer :tag "Number of blank lines after contents"))))
  169. (defcustom org-ascii-indented-line-width 'auto
  170. "Additional indentation width for the first line in a paragraph.
  171. If the value is an integer, indent the first line of each
  172. paragraph by this number. If it is the symbol `auto' preserve
  173. indentation from original document."
  174. :group 'org-export-ascii
  175. :version "24.4"
  176. :package-version '(Org . "8.0")
  177. :type '(choice
  178. (integer :tag "Number of white spaces characters")
  179. (const :tag "Preserve original width" auto)))
  180. (defcustom org-ascii-paragraph-spacing 'auto
  181. "Number of white lines between paragraphs.
  182. If the value is an integer, add this number of blank lines
  183. between contiguous paragraphs. If is it the symbol `auto', keep
  184. the same number of blank lines as in the original document."
  185. :group 'org-export-ascii
  186. :version "24.4"
  187. :package-version '(Org . "8.0")
  188. :type '(choice
  189. (integer :tag "Number of blank lines")
  190. (const :tag "Preserve original spacing" auto)))
  191. (defcustom org-ascii-charset 'ascii
  192. "The charset allowed to represent various elements and objects.
  193. Possible values are:
  194. `ascii' Only use plain ASCII characters
  195. `latin1' Include Latin-1 characters
  196. `utf-8' Use all UTF-8 characters"
  197. :group 'org-export-ascii
  198. :version "24.4"
  199. :package-version '(Org . "8.0")
  200. :type '(choice
  201. (const :tag "ASCII" ascii)
  202. (const :tag "Latin-1" latin1)
  203. (const :tag "UTF-8" utf-8)))
  204. (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
  205. (latin1 ?= ?~ ?-)
  206. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  207. "Characters for underlining headings in ASCII export.
  208. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  209. and whose value is a list of characters.
  210. For each supported charset, this variable associates a sequence
  211. of underline characters. In a sequence, the characters will be
  212. used in order for headlines level 1, 2, ... If no character is
  213. available for a given level, the headline won't be underlined."
  214. :group 'org-export-ascii
  215. :version "24.4"
  216. :package-version '(Org . "8.0")
  217. :type '(list
  218. (cons :tag "Underline characters sequence"
  219. (const :tag "ASCII charset" ascii)
  220. (repeat character))
  221. (cons :tag "Underline characters sequence"
  222. (const :tag "Latin-1 charset" latin1)
  223. (repeat character))
  224. (cons :tag "Underline characters sequence"
  225. (const :tag "UTF-8 charset" utf-8)
  226. (repeat character))))
  227. (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
  228. (latin1 ?§ ?¶)
  229. (utf-8 ?◊))
  230. "Bullet characters for headlines converted to lists in ASCII export.
  231. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  232. and whose value is a list of characters.
  233. The first character is used for the first level considered as low
  234. level, and so on. If there are more levels than characters given
  235. here, the list will be repeated.
  236. Note that this variable doesn't affect plain lists
  237. representation."
  238. :group 'org-export-ascii
  239. :version "24.4"
  240. :package-version '(Org . "8.0")
  241. :type '(list
  242. (cons :tag "Bullet characters for low level headlines"
  243. (const :tag "ASCII charset" ascii)
  244. (repeat character))
  245. (cons :tag "Bullet characters for low level headlines"
  246. (const :tag "Latin-1 charset" latin1)
  247. (repeat character))
  248. (cons :tag "Bullet characters for low level headlines"
  249. (const :tag "UTF-8 charset" utf-8)
  250. (repeat character))))
  251. (defcustom org-ascii-links-to-notes t
  252. "Non-nil means convert links to notes before the next headline.
  253. When nil, the link will be exported in place. If the line
  254. becomes long in this way, it will be wrapped."
  255. :group 'org-export-ascii
  256. :version "24.4"
  257. :package-version '(Org . "8.0")
  258. :type 'boolean)
  259. (defcustom org-ascii-table-keep-all-vertical-lines nil
  260. "Non-nil means keep all vertical lines in ASCII tables.
  261. When nil, vertical lines will be removed except for those needed
  262. for column grouping."
  263. :group 'org-export-ascii
  264. :version "24.4"
  265. :package-version '(Org . "8.0")
  266. :type 'boolean)
  267. (defcustom org-ascii-table-widen-columns t
  268. "Non-nil means widen narrowed columns for export.
  269. When nil, narrowed columns will look in ASCII export just like in
  270. Org mode, i.e. with \"=>\" as ellipsis."
  271. :group 'org-export-ascii
  272. :version "24.4"
  273. :package-version '(Org . "8.0")
  274. :type 'boolean)
  275. (defcustom org-ascii-table-use-ascii-art nil
  276. "Non-nil means table.el tables are turned into ascii-art.
  277. It only makes sense when export charset is `utf-8'. It is nil by
  278. default since it requires ascii-art-to-unicode.el package. You
  279. can download it here:
  280. http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
  281. :group 'org-export-ascii
  282. :version "24.4"
  283. :package-version '(Org . "8.0")
  284. :type 'boolean)
  285. (defcustom org-ascii-caption-above nil
  286. "When non-nil, place caption string before the element.
  287. Otherwise, place it right after it."
  288. :group 'org-export-ascii
  289. :version "24.4"
  290. :package-version '(Org . "8.0")
  291. :type 'boolean)
  292. (defcustom org-ascii-verbatim-format "`%s'"
  293. "Format string used for verbatim text and inline code."
  294. :group 'org-export-ascii
  295. :version "24.4"
  296. :package-version '(Org . "8.0")
  297. :type 'string)
  298. (defcustom org-ascii-format-drawer-function
  299. (lambda (name contents width) contents)
  300. "Function called to format a drawer in ASCII.
  301. The function must accept three parameters:
  302. NAME the drawer name, like \"LOGBOOK\"
  303. CONTENTS the contents of the drawer.
  304. WIDTH the text width within the drawer.
  305. The function should return either the string to be exported or
  306. nil to ignore the drawer.
  307. The default value simply returns the value of CONTENTS."
  308. :group 'org-export-ascii
  309. :version "24.4"
  310. :package-version '(Org . "8.0")
  311. :type 'function)
  312. (defcustom org-ascii-format-inlinetask-function
  313. 'org-ascii-format-inlinetask-default
  314. "Function called to format an inlinetask in ASCII.
  315. The function must accept nine parameters:
  316. TODO the todo keyword, as a string
  317. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  318. PRIORITY the inlinetask priority, as a string
  319. NAME the inlinetask name, as a string.
  320. TAGS the inlinetask tags, as a list of strings.
  321. CONTENTS the contents of the inlinetask, as a string.
  322. WIDTH the width of the inlinetask, as a number.
  323. INLINETASK the inlinetask itself.
  324. INFO the info channel.
  325. The function should return either the string to be exported or
  326. nil to ignore the inline task."
  327. :group 'org-export-ascii
  328. :version "24.4"
  329. :package-version '(Org . "8.3")
  330. :type 'function)
  331. ;;; Internal Functions
  332. ;; Internal functions fall into three categories.
  333. ;; The first one is about text formatting. The core function is
  334. ;; `org-ascii--current-text-width', which determines the current
  335. ;; text width allowed to a given element. In other words, it helps
  336. ;; keeping each line width within maximum text width defined in
  337. ;; `org-ascii-text-width'. Once this information is known,
  338. ;; `org-ascii--fill-string', `org-ascii--justify-string',
  339. ;; `org-ascii--box-string' and `org-ascii--indent-string' can
  340. ;; operate on a given output string.
  341. ;; The second category contains functions handling elements listings,
  342. ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
  343. ;; returns a complete table of contents, `org-ascii--list-listings'
  344. ;; returns a list of referenceable src-block elements, and
  345. ;; `org-ascii--list-tables' does the same for table elements.
  346. ;; The third category includes general helper functions.
  347. ;; `org-ascii--build-title' creates the title for a given headline
  348. ;; or inlinetask element. `org-ascii--build-caption' returns the
  349. ;; caption string associated to a table or a src-block.
  350. ;; `org-ascii--describe-links' creates notes about links for
  351. ;; insertion at the end of a section. It uses
  352. ;; `org-ascii--unique-links' to get the list of links to describe.
  353. ;; Eventually, `org-ascii--translate' translates a string according
  354. ;; to language and charset specification.
  355. (defun org-ascii--fill-string (s text-width info &optional justify)
  356. "Fill a string with specified text-width and return it.
  357. S is the string being filled. TEXT-WIDTH is an integer
  358. specifying maximum length of a line. INFO is the plist used as
  359. a communication channel.
  360. Optional argument JUSTIFY can specify any type of justification
  361. among `left', `center', `right' or `full'. A nil value is
  362. equivalent to `left'. For a justification that doesn't also fill
  363. string, see `org-ascii--justify-string'.
  364. Return nil if S isn't a string."
  365. ;; Don't fill paragraph when break should be preserved.
  366. (cond ((not (stringp s)) nil)
  367. ((plist-get info :preserve-breaks) s)
  368. (t (let ((double-space-p sentence-end-double-space))
  369. (with-temp-buffer
  370. (let ((fill-column text-width)
  371. (use-hard-newlines t)
  372. (sentence-end-double-space double-space-p))
  373. (insert s)
  374. (fill-region (point-min) (point-max) justify))
  375. (buffer-string))))))
  376. (defun org-ascii--justify-string (s text-width how)
  377. "Justify string S.
  378. TEXT-WIDTH is an integer specifying maximum length of a line.
  379. HOW determines the type of justification: it can be `left',
  380. `right', `full' or `center'."
  381. (with-temp-buffer
  382. (insert s)
  383. (goto-char (point-min))
  384. (let ((fill-column text-width)
  385. ;; Disable `adaptive-fill-mode' so it doesn't prevent
  386. ;; filling lines matching `adaptive-fill-regexp'.
  387. (adaptive-fill-mode nil))
  388. (while (< (point) (point-max))
  389. (justify-current-line how)
  390. (forward-line)))
  391. (buffer-string)))
  392. (defun org-ascii--indent-string (s width)
  393. "Indent string S by WIDTH white spaces.
  394. Empty lines are not indented."
  395. (when (stringp s)
  396. (replace-regexp-in-string
  397. "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
  398. (defun org-ascii--box-string (s info)
  399. "Return string S with a partial box to its left.
  400. INFO is a plist used as a communication channel."
  401. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  402. (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
  403. (replace-regexp-in-string
  404. "^" (if utf8p "│ " "| ")
  405. ;; Remove last newline character.
  406. (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
  407. (defun org-ascii--current-text-width (element info)
  408. "Return maximum text width for ELEMENT's contents.
  409. INFO is a plist used as a communication channel."
  410. (case (org-element-type element)
  411. ;; Elements with an absolute width: `headline' and `inlinetask'.
  412. (inlinetask org-ascii-inlinetask-width)
  413. ('headline
  414. (- org-ascii-text-width
  415. (let ((low-level-rank (org-export-low-level-p element info)))
  416. (if low-level-rank (* low-level-rank 2) org-ascii-global-margin))))
  417. ;; Elements with a relative width: store maximum text width in
  418. ;; TOTAL-WIDTH.
  419. (otherwise
  420. (let* ((genealogy (cons element (org-export-get-genealogy element)))
  421. ;; Total width is determined by the presence, or not, of an
  422. ;; inline task among ELEMENT parents.
  423. (total-width
  424. (if (loop for parent in genealogy
  425. thereis (eq (org-element-type parent) 'inlinetask))
  426. org-ascii-inlinetask-width
  427. ;; No inlinetask: Remove global margin from text width.
  428. (- org-ascii-text-width
  429. org-ascii-global-margin
  430. (let ((parent (org-export-get-parent-headline element)))
  431. ;; Inner margin doesn't apply to text before first
  432. ;; headline.
  433. (if (not parent) 0
  434. (let ((low-level-rank
  435. (org-export-low-level-p parent info)))
  436. ;; Inner margin doesn't apply to contents of
  437. ;; low level headlines, since they've got their
  438. ;; own indentation mechanism.
  439. (if low-level-rank (* low-level-rank 2)
  440. org-ascii-inner-margin))))))))
  441. (- total-width
  442. ;; Each `quote-block', `quote-section' and `verse-block' above
  443. ;; narrows text width by twice the standard margin size.
  444. (+ (* (loop for parent in genealogy
  445. when (memq (org-element-type parent)
  446. '(quote-block quote-section verse-block))
  447. count parent)
  448. 2 org-ascii-quote-margin)
  449. ;; Text width within a plain-list is restricted by
  450. ;; indentation of current item. If that's the case,
  451. ;; compute it with the help of `:structure' property from
  452. ;; parent item, if any.
  453. (let ((parent-item
  454. (if (eq (org-element-type element) 'item) element
  455. (loop for parent in genealogy
  456. when (eq (org-element-type parent) 'item)
  457. return parent))))
  458. (if (not parent-item) 0
  459. ;; Compute indentation offset of the current item,
  460. ;; that is the sum of the difference between its
  461. ;; indentation and the indentation of the top item in
  462. ;; the list and current item bullet's length. Also
  463. ;; remove checkbox length, and tag length (for
  464. ;; description lists) or bullet length.
  465. (let ((struct (org-element-property :structure parent-item))
  466. (beg-item (org-element-property :begin parent-item)))
  467. (+ (- (org-list-get-ind beg-item struct)
  468. (org-list-get-ind
  469. (org-list-get-top-point struct) struct))
  470. (string-width (or (org-ascii--checkbox parent-item info)
  471. ""))
  472. (string-width
  473. (or (org-list-get-tag beg-item struct)
  474. (org-list-get-bullet beg-item struct)))))))))))))
  475. (defun org-ascii--build-title
  476. (element info text-width &optional underline notags toc)
  477. "Format ELEMENT title and return it.
  478. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  479. a plist used as a communication channel. TEXT-WIDTH is an
  480. integer representing the maximum length of a line.
  481. When optional argument UNDERLINE is non-nil, underline title,
  482. without the tags, according to `org-ascii-underline'
  483. specifications.
  484. If optional argument NOTAGS is non-nil, no tags will be added to
  485. the title.
  486. When optional argument TOC is non-nil, use optional title if
  487. possible. It doesn't apply to `inlinetask' elements."
  488. (let* ((headlinep (eq (org-element-type element) 'headline))
  489. (numbers
  490. ;; Numbering is specific to headlines.
  491. (and headlinep (org-export-numbered-headline-p element info)
  492. ;; All tests passed: build numbering string.
  493. (concat
  494. (mapconcat
  495. 'number-to-string
  496. (org-export-get-headline-number element info) ".")
  497. " ")))
  498. (text
  499. (org-trim
  500. (org-export-data
  501. (if (and toc headlinep) (org-export-get-alt-title element info)
  502. (org-element-property :title element))
  503. info)))
  504. (todo
  505. (and (plist-get info :with-todo-keywords)
  506. (let ((todo (org-element-property :todo-keyword element)))
  507. (and todo (concat (org-export-data todo info) " ")))))
  508. (tags (and (not notags)
  509. (plist-get info :with-tags)
  510. (let ((tag-list (org-export-get-tags element info)))
  511. (and tag-list
  512. (format ":%s:"
  513. (mapconcat 'identity tag-list ":"))))))
  514. (priority
  515. (and (plist-get info :with-priority)
  516. (let ((char (org-element-property :priority element)))
  517. (and char (format "(#%c) " char)))))
  518. (first-part (concat numbers todo priority text)))
  519. (concat
  520. first-part
  521. ;; Align tags, if any.
  522. (when tags
  523. (format
  524. (format " %%%ds"
  525. (max (- text-width (1+ (string-width first-part)))
  526. (string-width tags)))
  527. tags))
  528. ;; Maybe underline text, if ELEMENT type is `headline' and an
  529. ;; underline character has been defined.
  530. (when (and underline headlinep)
  531. (let ((under-char
  532. (nth (1- (org-export-get-relative-level element info))
  533. (cdr (assq (plist-get info :ascii-charset)
  534. org-ascii-underline)))))
  535. (and under-char
  536. (concat "\n"
  537. (make-string (/ (string-width first-part)
  538. (char-width under-char))
  539. under-char))))))))
  540. (defun org-ascii--has-caption-p (element info)
  541. "Non-nil when ELEMENT has a caption affiliated keyword.
  542. INFO is a plist used as a communication channel. This function
  543. is meant to be used as a predicate for `org-export-get-ordinal'."
  544. (org-element-property :caption element))
  545. (defun org-ascii--build-caption (element info)
  546. "Return caption string for ELEMENT, if applicable.
  547. INFO is a plist used as a communication channel.
  548. The caption string contains the sequence number of ELEMENT along
  549. with its real caption. Return nil when ELEMENT has no affiliated
  550. caption keyword."
  551. (let ((caption (org-export-get-caption element)))
  552. (when caption
  553. ;; Get sequence number of current src-block among every
  554. ;; src-block with a caption.
  555. (let ((reference
  556. (org-export-get-ordinal
  557. element info nil 'org-ascii--has-caption-p))
  558. (title-fmt (org-ascii--translate
  559. (case (org-element-type element)
  560. (table "Table %d:")
  561. (src-block "Listing %d:"))
  562. info)))
  563. (org-ascii--fill-string
  564. (concat (format title-fmt reference)
  565. " "
  566. (org-export-data caption info))
  567. (org-ascii--current-text-width element info) info)))))
  568. (defun org-ascii--build-toc (info &optional n keyword)
  569. "Return a table of contents.
  570. INFO is a plist used as a communication channel.
  571. Optional argument N, when non-nil, is an integer specifying the
  572. depth of the table.
  573. Optional argument KEYWORD specifies the TOC keyword, if any, from
  574. which the table of contents generation has been initiated."
  575. (let ((title (org-ascii--translate "Table of Contents" info)))
  576. (concat
  577. title "\n"
  578. (make-string (string-width title)
  579. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  580. "\n\n"
  581. (let ((text-width
  582. (if keyword (org-ascii--current-text-width keyword info)
  583. (- org-ascii-text-width org-ascii-global-margin))))
  584. (mapconcat
  585. (lambda (headline)
  586. (let* ((level (org-export-get-relative-level headline info))
  587. (indent (* (1- level) 3)))
  588. (concat
  589. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  590. (org-ascii--build-title
  591. headline info (- text-width indent) nil
  592. (or (not (plist-get info :with-tags))
  593. (eq (plist-get info :with-tags) 'not-in-toc))
  594. 'toc))))
  595. (org-export-collect-headlines info n) "\n")))))
  596. (defun org-ascii--list-listings (keyword info)
  597. "Return a list of listings.
  598. KEYWORD is the keyword that initiated the list of listings
  599. generation. INFO is a plist used as a communication channel."
  600. (let ((title (org-ascii--translate "List of Listings" info)))
  601. (concat
  602. title "\n"
  603. (make-string (string-width title)
  604. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  605. "\n\n"
  606. (let ((text-width
  607. (if keyword (org-ascii--current-text-width keyword info)
  608. (- org-ascii-text-width org-ascii-global-margin)))
  609. ;; Use a counter instead of retrieving ordinal of each
  610. ;; src-block.
  611. (count 0))
  612. (mapconcat
  613. (lambda (src-block)
  614. ;; Store initial text so its length can be computed. This is
  615. ;; used to properly align caption right to it in case of
  616. ;; filling (like contents of a description list item).
  617. (let* ((initial-text
  618. (format (org-ascii--translate "Listing %d:" info)
  619. (incf count)))
  620. (initial-width (string-width initial-text)))
  621. (concat
  622. initial-text " "
  623. (org-trim
  624. (org-ascii--indent-string
  625. (org-ascii--fill-string
  626. ;; Use short name in priority, if available.
  627. (let ((caption (or (org-export-get-caption src-block t)
  628. (org-export-get-caption src-block))))
  629. (org-export-data caption info))
  630. (- text-width initial-width) info)
  631. initial-width)))))
  632. (org-export-collect-listings info) "\n")))))
  633. (defun org-ascii--list-tables (keyword info)
  634. "Return a list of tables.
  635. KEYWORD is the keyword that initiated the list of tables
  636. generation. INFO is a plist used as a communication channel."
  637. (let ((title (org-ascii--translate "List of Tables" info)))
  638. (concat
  639. title "\n"
  640. (make-string (string-width title)
  641. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  642. "\n\n"
  643. (let ((text-width
  644. (if keyword (org-ascii--current-text-width keyword info)
  645. (- org-ascii-text-width org-ascii-global-margin)))
  646. ;; Use a counter instead of retrieving ordinal of each
  647. ;; src-block.
  648. (count 0))
  649. (mapconcat
  650. (lambda (table)
  651. ;; Store initial text so its length can be computed. This is
  652. ;; used to properly align caption right to it in case of
  653. ;; filling (like contents of a description list item).
  654. (let* ((initial-text
  655. (format (org-ascii--translate "Table %d:" info)
  656. (incf count)))
  657. (initial-width (string-width initial-text)))
  658. (concat
  659. initial-text " "
  660. (org-trim
  661. (org-ascii--indent-string
  662. (org-ascii--fill-string
  663. ;; Use short name in priority, if available.
  664. (let ((caption (or (org-export-get-caption table t)
  665. (org-export-get-caption table))))
  666. (org-export-data caption info))
  667. (- text-width initial-width) info)
  668. initial-width)))))
  669. (org-export-collect-tables info) "\n")))))
  670. (defun org-ascii--unique-links (element info)
  671. "Return a list of unique link references in ELEMENT.
  672. ELEMENT is either a headline element or a section element. INFO
  673. is a plist used as a communication channel."
  674. (let* (seen
  675. (unique-link-p
  676. (function
  677. ;; Return LINK if it wasn't referenced so far, or nil.
  678. ;; Update SEEN links along the way.
  679. (lambda (link)
  680. (let ((footprint
  681. (cons (org-element-property :raw-link link)
  682. (org-element-contents link))))
  683. ;; Ignore LINK if it hasn't been translated already.
  684. ;; It can happen if it is located in an affiliated
  685. ;; keyword that was ignored.
  686. (when (and (org-string-nw-p
  687. (gethash link (plist-get info :exported-data)))
  688. (not (member footprint seen)))
  689. (push footprint seen) link)))))
  690. ;; If at a section, find parent headline, if any, in order to
  691. ;; count links that might be in the title.
  692. (headline
  693. (if (eq (org-element-type element) 'headline) element
  694. (or (org-export-get-parent-headline element) element))))
  695. ;; Get all links in HEADLINE.
  696. (org-element-map headline 'link
  697. (lambda (l) (funcall unique-link-p l)) info nil nil t)))
  698. (defun org-ascii--describe-links (links width info)
  699. "Return a string describing a list of links.
  700. LINKS is a list of link type objects, as returned by
  701. `org-ascii--unique-links'. WIDTH is the text width allowed for
  702. the output string. INFO is a plist used as a communication
  703. channel."
  704. (mapconcat
  705. (lambda (link)
  706. (let ((type (org-element-property :type link))
  707. (anchor (let ((desc (org-element-contents link)))
  708. (if desc (org-export-data desc info)
  709. (org-element-property :raw-link link)))))
  710. (cond
  711. ;; Coderefs, radio links and fuzzy links are ignored.
  712. ((member type '("coderef" "radio" "fuzzy")) nil)
  713. ;; Id and custom-id links: Headlines refer to their numbering.
  714. ((member type '("custom-id" "id"))
  715. (let ((dest (org-export-resolve-id-link link info)))
  716. (concat
  717. (org-ascii--fill-string
  718. (format
  719. "[%s] %s"
  720. anchor
  721. (if (not dest) (org-ascii--translate "Unknown reference" info)
  722. (format
  723. (org-ascii--translate "See section %s" info)
  724. (mapconcat 'number-to-string
  725. (org-export-get-headline-number dest info) "."))))
  726. width info) "\n\n")))
  727. ;; Do not add a link that cannot be resolved and doesn't have
  728. ;; any description: destination is already visible in the
  729. ;; paragraph.
  730. ((not (org-element-contents link)) nil)
  731. (t
  732. (concat
  733. (org-ascii--fill-string
  734. (format "[%s] %s" anchor (org-element-property :raw-link link))
  735. width info)
  736. "\n\n")))))
  737. links ""))
  738. (defun org-ascii--checkbox (item info)
  739. "Return checkbox string for ITEM or nil.
  740. INFO is a plist used as a communication channel."
  741. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  742. (case (org-element-property :checkbox item)
  743. (on (if utf8p "☑ " "[X] "))
  744. (off (if utf8p "☐ " "[ ] "))
  745. (trans (if utf8p "☒ " "[-] ")))))
  746. ;;; Template
  747. (defun org-ascii-template--document-title (info)
  748. "Return document title, as a string.
  749. INFO is a plist used as a communication channel."
  750. (let* ((text-width org-ascii-text-width)
  751. ;; Links in the title will not be resolved later, so we make
  752. ;; sure their path is located right after them.
  753. (org-ascii-links-to-notes nil)
  754. (title (org-export-data (plist-get info :title) info))
  755. (author (and (plist-get info :with-author)
  756. (let ((auth (plist-get info :author)))
  757. (and auth (org-export-data auth info)))))
  758. (email (and (plist-get info :with-email)
  759. (org-export-data (plist-get info :email) info)))
  760. (date (and (plist-get info :with-date)
  761. (org-export-data (org-export-get-date info) info))))
  762. ;; There are two types of title blocks depending on the presence
  763. ;; of a title to display.
  764. (if (string= title "")
  765. ;; Title block without a title. DATE is positioned at the top
  766. ;; right of the document, AUTHOR to the top left and EMAIL
  767. ;; just below.
  768. (cond
  769. ((and (org-string-nw-p date) (org-string-nw-p author))
  770. (concat
  771. author
  772. (make-string (- text-width (string-width date) (string-width author))
  773. ?\s)
  774. date
  775. (when (org-string-nw-p email) (concat "\n" email))
  776. "\n\n\n"))
  777. ((and (org-string-nw-p date) (org-string-nw-p email))
  778. (concat
  779. email
  780. (make-string (- text-width (string-width date) (string-width email))
  781. ?\s)
  782. date "\n\n\n"))
  783. ((org-string-nw-p date)
  784. (concat
  785. (org-ascii--justify-string date text-width 'right)
  786. "\n\n\n"))
  787. ((and (org-string-nw-p author) (org-string-nw-p email))
  788. (concat author "\n" email "\n\n\n"))
  789. ((org-string-nw-p author) (concat author "\n\n\n"))
  790. ((org-string-nw-p email) (concat email "\n\n\n")))
  791. ;; Title block with a title. Document's TITLE, along with the
  792. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  793. ;; centered. Date is just below, also centered.
  794. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  795. ;; Format TITLE. It may be filled if it is too wide,
  796. ;; that is wider than the two thirds of the total width.
  797. (title-len (min (length title) (/ (* 2 text-width) 3)))
  798. (formatted-title (org-ascii--fill-string title title-len info))
  799. (line
  800. (make-string
  801. (min (+ (max title-len
  802. (string-width (or author ""))
  803. (string-width (or email "")))
  804. 2)
  805. text-width) (if utf8p ?━ ?_))))
  806. (org-ascii--justify-string
  807. (concat line "\n"
  808. (unless utf8p "\n")
  809. (upcase formatted-title)
  810. (cond
  811. ((and (org-string-nw-p author) (org-string-nw-p email))
  812. (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
  813. ((org-string-nw-p author)
  814. (concat (if utf8p "\n\n\n" "\n\n") author))
  815. ((org-string-nw-p email)
  816. (concat (if utf8p "\n\n\n" "\n\n") email)))
  817. "\n" line
  818. (when (org-string-nw-p date) (concat "\n\n\n" date))
  819. "\n\n\n") text-width 'center)))))
  820. (defun org-ascii-inner-template (contents info)
  821. "Return complete document string after ASCII conversion.
  822. CONTENTS is the transcoded contents string. INFO is a plist
  823. holding export options."
  824. (org-element-normalize-string
  825. (org-ascii--indent-string
  826. (concat
  827. ;; 1. Document's body.
  828. contents
  829. ;; 2. Footnote definitions.
  830. (let ((definitions (org-export-collect-footnote-definitions
  831. (plist-get info :parse-tree) info))
  832. ;; Insert full links right inside the footnote definition
  833. ;; as they have no chance to be inserted later.
  834. (org-ascii-links-to-notes nil))
  835. (when definitions
  836. (concat
  837. "\n\n\n"
  838. (let ((title (org-ascii--translate "Footnotes" info)))
  839. (concat
  840. title "\n"
  841. (make-string
  842. (string-width title)
  843. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  844. "\n\n"
  845. (let ((text-width (- org-ascii-text-width org-ascii-global-margin)))
  846. (mapconcat
  847. (lambda (ref)
  848. (let ((id (format "[%s] " (car ref))))
  849. ;; Distinguish between inline definitions and
  850. ;; full-fledged definitions.
  851. (org-trim
  852. (let ((def (nth 2 ref)))
  853. (if (eq (org-element-type def) 'org-data)
  854. ;; Full-fledged definition: footnote ID is
  855. ;; inserted inside the first parsed paragraph
  856. ;; (FIRST), if any, to be sure filling will
  857. ;; take it into consideration.
  858. (let ((first (car (org-element-contents def))))
  859. (if (not (eq (org-element-type first) 'paragraph))
  860. (concat id "\n" (org-export-data def info))
  861. (push id (nthcdr 2 first))
  862. (org-export-data def info)))
  863. ;; Fill paragraph once footnote ID is inserted
  864. ;; in order to have a correct length for first
  865. ;; line.
  866. (org-ascii--fill-string
  867. (concat id (org-export-data def info))
  868. text-width info))))))
  869. definitions "\n\n"))))))
  870. org-ascii-global-margin)))
  871. (defun org-ascii-template (contents info)
  872. "Return complete document string after ASCII conversion.
  873. CONTENTS is the transcoded contents string. INFO is a plist
  874. holding export options."
  875. (concat
  876. ;; 1. Build title block.
  877. (org-ascii--indent-string
  878. (concat (org-ascii-template--document-title info)
  879. ;; 2. Table of contents.
  880. (let ((depth (plist-get info :with-toc)))
  881. (when depth
  882. (concat
  883. (org-ascii--build-toc info (and (wholenump depth) depth))
  884. "\n\n\n"))))
  885. org-ascii-global-margin)
  886. ;; 3. Document's body.
  887. contents
  888. ;; 4. Creator. Ignore `comment' value as there are no comments in
  889. ;; ASCII. Justify it to the bottom right.
  890. (org-ascii--indent-string
  891. (let ((creator-info (plist-get info :with-creator))
  892. (text-width (- org-ascii-text-width org-ascii-global-margin)))
  893. (unless (or (not creator-info) (eq creator-info 'comment))
  894. (concat
  895. "\n\n\n"
  896. (org-ascii--fill-string
  897. (plist-get info :creator) text-width info 'right))))
  898. org-ascii-global-margin)))
  899. (defun org-ascii--translate (s info)
  900. "Translate string S according to specified language and charset.
  901. INFO is a plist used as a communication channel."
  902. (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
  903. (org-export-translate s charset info)))
  904. ;;; Transcode Functions
  905. ;;;; Bold
  906. (defun org-ascii-bold (bold contents info)
  907. "Transcode BOLD from Org to ASCII.
  908. CONTENTS is the text with bold markup. INFO is a plist holding
  909. contextual information."
  910. (format "*%s*" contents))
  911. ;;;; Center Block
  912. (defun org-ascii-center-block (center-block contents info)
  913. "Transcode a CENTER-BLOCK element from Org to ASCII.
  914. CONTENTS holds the contents of the block. INFO is a plist
  915. holding contextual information."
  916. (org-ascii--justify-string
  917. contents (org-ascii--current-text-width center-block info) 'center))
  918. ;;;; Clock
  919. (defun org-ascii-clock (clock contents info)
  920. "Transcode a CLOCK object from Org to ASCII.
  921. CONTENTS is nil. INFO is a plist holding contextual
  922. information."
  923. (concat org-clock-string " "
  924. (org-translate-time
  925. (org-element-property :raw-value
  926. (org-element-property :value clock)))
  927. (let ((time (org-element-property :duration clock)))
  928. (and time
  929. (concat " => "
  930. (apply 'format
  931. "%2s:%02s"
  932. (org-split-string time ":")))))))
  933. ;;;; Code
  934. (defun org-ascii-code (code contents info)
  935. "Return a CODE object from Org to ASCII.
  936. CONTENTS is nil. INFO is a plist holding contextual
  937. information."
  938. (format org-ascii-verbatim-format (org-element-property :value code)))
  939. ;;;; Drawer
  940. (defun org-ascii-drawer (drawer contents info)
  941. "Transcode a DRAWER element from Org to ASCII.
  942. CONTENTS holds the contents of the block. INFO is a plist
  943. holding contextual information."
  944. (let ((name (org-element-property :drawer-name drawer))
  945. (width (org-ascii--current-text-width drawer info)))
  946. (funcall org-ascii-format-drawer-function name contents width)))
  947. ;;;; Dynamic Block
  948. (defun org-ascii-dynamic-block (dynamic-block contents info)
  949. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  950. CONTENTS holds the contents of the block. INFO is a plist
  951. holding contextual information."
  952. contents)
  953. ;;;; Entity
  954. (defun org-ascii-entity (entity contents info)
  955. "Transcode an ENTITY object from Org to ASCII.
  956. CONTENTS are the definition itself. INFO is a plist holding
  957. contextual information."
  958. (org-element-property
  959. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  960. entity))
  961. ;;;; Example Block
  962. (defun org-ascii-example-block (example-block contents info)
  963. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  964. CONTENTS is nil. INFO is a plist holding contextual information."
  965. (org-ascii--box-string
  966. (org-export-format-code-default example-block info) info))
  967. ;;;; Export Snippet
  968. (defun org-ascii-export-snippet (export-snippet contents info)
  969. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  970. CONTENTS is nil. INFO is a plist holding contextual information."
  971. (when (eq (org-export-snippet-backend export-snippet) 'ascii)
  972. (org-element-property :value export-snippet)))
  973. ;;;; Export Block
  974. (defun org-ascii-export-block (export-block contents info)
  975. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  976. CONTENTS is nil. INFO is a plist holding contextual information."
  977. (when (string= (org-element-property :type export-block) "ASCII")
  978. (org-remove-indentation (org-element-property :value export-block))))
  979. ;;;; Fixed Width
  980. (defun org-ascii-fixed-width (fixed-width contents info)
  981. "Transcode a FIXED-WIDTH element from Org to ASCII.
  982. CONTENTS is nil. INFO is a plist holding contextual information."
  983. (org-ascii--box-string
  984. (org-remove-indentation
  985. (org-element-property :value fixed-width)) info))
  986. ;;;; Footnote Definition
  987. ;; Footnote Definitions are ignored. They are compiled at the end of
  988. ;; the document, by `org-ascii-inner-template'.
  989. ;;;; Footnote Reference
  990. (defun org-ascii-footnote-reference (footnote-reference contents info)
  991. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  992. CONTENTS is nil. INFO is a plist holding contextual information."
  993. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  994. ;;;; Headline
  995. (defun org-ascii-headline (headline contents info)
  996. "Transcode a HEADLINE element from Org to ASCII.
  997. CONTENTS holds the contents of the headline. INFO is a plist
  998. holding contextual information."
  999. ;; Don't export footnote section, which will be handled at the end
  1000. ;; of the template.
  1001. (unless (org-element-property :footnote-section-p headline)
  1002. (let* ((low-level-rank (org-export-low-level-p headline info))
  1003. (width (org-ascii--current-text-width headline info))
  1004. ;; Blank lines between headline and its contents.
  1005. ;; `org-ascii-headline-spacing', when set, overwrites
  1006. ;; original buffer's spacing.
  1007. (pre-blanks
  1008. (make-string
  1009. (if org-ascii-headline-spacing (car org-ascii-headline-spacing)
  1010. (org-element-property :pre-blank headline)) ?\n))
  1011. ;; Even if HEADLINE has no section, there might be some
  1012. ;; links in its title that we shouldn't forget to describe.
  1013. (links
  1014. (unless (or (eq (caar (org-element-contents headline)) 'section))
  1015. (let ((title (org-element-property :title headline)))
  1016. (when (consp title)
  1017. (org-ascii--describe-links
  1018. (org-ascii--unique-links title info) width info))))))
  1019. ;; Deep subtree: export it as a list item.
  1020. (if low-level-rank
  1021. (concat
  1022. ;; Bullet.
  1023. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  1024. org-ascii-bullets))))
  1025. (char-to-string
  1026. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  1027. " "
  1028. ;; Title.
  1029. (org-ascii--build-title headline info width) "\n"
  1030. ;; Contents, indented by length of bullet.
  1031. pre-blanks
  1032. (org-ascii--indent-string
  1033. (concat contents
  1034. (when (org-string-nw-p links) (concat "\n\n" links)))
  1035. 2))
  1036. ;; Else: Standard headline.
  1037. (concat
  1038. (org-ascii--build-title headline info width 'underline)
  1039. "\n" pre-blanks
  1040. (concat (when (org-string-nw-p links) links) contents))))))
  1041. ;;;; Horizontal Rule
  1042. (defun org-ascii-horizontal-rule (horizontal-rule contents info)
  1043. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  1044. CONTENTS is nil. INFO is a plist holding contextual
  1045. information."
  1046. (let ((text-width (org-ascii--current-text-width horizontal-rule info))
  1047. (spec-width
  1048. (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  1049. (org-ascii--justify-string
  1050. (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
  1051. (string-to-number spec-width)
  1052. text-width)
  1053. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
  1054. text-width 'center)))
  1055. ;;;; Inline Src Block
  1056. (defun org-ascii-inline-src-block (inline-src-block contents info)
  1057. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  1058. CONTENTS holds the contents of the item. INFO is a plist holding
  1059. contextual information."
  1060. (format org-ascii-verbatim-format
  1061. (org-element-property :value inline-src-block)))
  1062. ;;;; Inlinetask
  1063. (defun org-ascii-format-inlinetask-default
  1064. (todo type priority name tags contents width inlinetask info)
  1065. "Format an inline task element for ASCII export.
  1066. See `org-ascii-format-inlinetask-function' for a description
  1067. of the parameters."
  1068. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1069. (width (or width org-ascii-inlinetask-width)))
  1070. (org-ascii--indent-string
  1071. (concat
  1072. ;; Top line, with an additional blank line if not in UTF-8.
  1073. (make-string width (if utf8p ?━ ?_)) "\n"
  1074. (unless utf8p (concat (make-string width ? ) "\n"))
  1075. ;; Add title. Fill it if wider than inlinetask.
  1076. (let ((title (org-ascii--build-title inlinetask info width)))
  1077. (if (<= (string-width title) width) title
  1078. (org-ascii--fill-string title width info)))
  1079. "\n"
  1080. ;; If CONTENTS is not empty, insert it along with
  1081. ;; a separator.
  1082. (when (org-string-nw-p contents)
  1083. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1084. ;; Bottom line.
  1085. (make-string width (if utf8p ?━ ?_)))
  1086. ;; Flush the inlinetask to the right.
  1087. (- org-ascii-text-width org-ascii-global-margin
  1088. (if (not (org-export-get-parent-headline inlinetask)) 0
  1089. org-ascii-inner-margin)
  1090. (org-ascii--current-text-width inlinetask info)))))
  1091. (defun org-ascii-inlinetask (inlinetask contents info)
  1092. "Transcode an INLINETASK element from Org to ASCII.
  1093. CONTENTS holds the contents of the block. INFO is a plist
  1094. holding contextual information."
  1095. (let ((width (org-ascii--current-text-width inlinetask info)))
  1096. (funcall org-ascii-format-inlinetask-function
  1097. ;; todo.
  1098. (and (plist-get info :with-todo-keywords)
  1099. (let ((todo (org-element-property
  1100. :todo-keyword inlinetask)))
  1101. (and todo (org-export-data todo info))))
  1102. ;; todo-type
  1103. (org-element-property :todo-type inlinetask)
  1104. ;; priority
  1105. (and (plist-get info :with-priority)
  1106. (org-element-property :priority inlinetask))
  1107. ;; title
  1108. (org-export-data (org-element-property :title inlinetask) info)
  1109. ;; tags
  1110. (and (plist-get info :with-tags)
  1111. (org-element-property :tags inlinetask))
  1112. ;; contents and width
  1113. contents width inlinetask info)))
  1114. ;;;; Italic
  1115. (defun org-ascii-italic (italic contents info)
  1116. "Transcode italic from Org to ASCII.
  1117. CONTENTS is the text with italic markup. INFO is a plist holding
  1118. contextual information."
  1119. (format "/%s/" contents))
  1120. ;;;; Item
  1121. (defun org-ascii-item (item contents info)
  1122. "Transcode an ITEM element from Org to ASCII.
  1123. CONTENTS holds the contents of the item. INFO is a plist holding
  1124. contextual information."
  1125. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1126. (checkbox (org-ascii--checkbox item info))
  1127. (list-type (org-element-property :type (org-export-get-parent item)))
  1128. (bullet
  1129. ;; First parent of ITEM is always the plain-list. Get
  1130. ;; `:type' property from it.
  1131. (org-list-bullet-string
  1132. (case list-type
  1133. (descriptive
  1134. (concat checkbox
  1135. (org-export-data (org-element-property :tag item) info)
  1136. ": "))
  1137. (ordered
  1138. ;; Return correct number for ITEM, paying attention to
  1139. ;; counters.
  1140. (let* ((struct (org-element-property :structure item))
  1141. (bul (org-element-property :bullet item))
  1142. (num (number-to-string
  1143. (car (last (org-list-get-item-number
  1144. (org-element-property :begin item)
  1145. struct
  1146. (org-list-prevs-alist struct)
  1147. (org-list-parents-alist struct)))))))
  1148. (replace-regexp-in-string "[0-9]+" num bul)))
  1149. (t (let ((bul (org-element-property :bullet item)))
  1150. ;; Change bullets into more visible form if UTF-8 is active.
  1151. (if (not utf8p) bul
  1152. (replace-regexp-in-string
  1153. "-" "•"
  1154. (replace-regexp-in-string
  1155. "+" "⁃"
  1156. (replace-regexp-in-string "*" "‣" bul))))))))))
  1157. (concat
  1158. bullet
  1159. (unless (eq list-type 'descriptive) checkbox)
  1160. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1161. ;; already taken care of at the paragraph level so they don't
  1162. ;; interfere with indentation.
  1163. (let ((contents (org-ascii--indent-string contents (string-width bullet))))
  1164. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1165. (org-trim contents)
  1166. (concat "\n" contents))))))
  1167. ;;;; Keyword
  1168. (defun org-ascii-keyword (keyword contents info)
  1169. "Transcode a KEYWORD element from Org to ASCII.
  1170. CONTENTS is nil. INFO is a plist holding contextual
  1171. information."
  1172. (let ((key (org-element-property :key keyword))
  1173. (value (org-element-property :value keyword)))
  1174. (cond
  1175. ((string= key "ASCII") value)
  1176. ((string= key "TOC")
  1177. (let ((value (downcase value)))
  1178. (cond
  1179. ((string-match "\\<headlines\\>" value)
  1180. (let ((depth (or (and (string-match "[0-9]+" value)
  1181. (string-to-number (match-string 0 value)))
  1182. (plist-get info :with-toc))))
  1183. (org-ascii--build-toc
  1184. info (and (wholenump depth) depth) keyword)))
  1185. ((string= "tables" value)
  1186. (org-ascii--list-tables keyword info))
  1187. ((string= "listings" value)
  1188. (org-ascii--list-listings keyword info))))))))
  1189. ;;;; Latex Environment
  1190. (defun org-ascii-latex-environment (latex-environment contents info)
  1191. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1192. CONTENTS is nil. INFO is a plist holding contextual
  1193. information."
  1194. (when (plist-get info :with-latex)
  1195. (org-remove-indentation (org-element-property :value latex-environment))))
  1196. ;;;; Latex Fragment
  1197. (defun org-ascii-latex-fragment (latex-fragment contents info)
  1198. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1199. CONTENTS is nil. INFO is a plist holding contextual
  1200. information."
  1201. (when (plist-get info :with-latex)
  1202. (org-element-property :value latex-fragment)))
  1203. ;;;; Line Break
  1204. (defun org-ascii-line-break (line-break contents info)
  1205. "Transcode a LINE-BREAK object from Org to ASCII.
  1206. CONTENTS is nil. INFO is a plist holding contextual
  1207. information." hard-newline)
  1208. ;;;; Link
  1209. (defun org-ascii-link (link desc info)
  1210. "Transcode a LINK object from Org to ASCII.
  1211. DESC is the description part of the link, or the empty string.
  1212. INFO is a plist holding contextual information."
  1213. (let ((raw-link (org-element-property :raw-link link))
  1214. (type (org-element-property :type link)))
  1215. (cond
  1216. ((string= type "coderef")
  1217. (let ((ref (org-element-property :path link)))
  1218. (format (org-export-get-coderef-format ref desc)
  1219. (org-export-resolve-coderef ref info))))
  1220. ;; Do not apply a special syntax on radio links. Though, use
  1221. ;; transcoded target's contents as output.
  1222. ((string= type "radio")
  1223. (let ((destination (org-export-resolve-radio-link link info)))
  1224. (when destination
  1225. (org-export-data (org-element-contents destination) info))))
  1226. ;; Do not apply a special syntax on fuzzy links pointing to
  1227. ;; targets.
  1228. ((string= type "fuzzy")
  1229. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1230. (if (org-string-nw-p desc) desc
  1231. (when destination
  1232. (let ((number
  1233. (org-export-get-ordinal
  1234. destination info nil 'org-ascii--has-caption-p)))
  1235. (when number
  1236. (if (atom number) (number-to-string number)
  1237. (mapconcat 'number-to-string number "."))))))))
  1238. (t
  1239. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1240. (concat
  1241. (format "[%s]" desc)
  1242. (unless org-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1243. ;;;; Node Properties
  1244. (defun org-ascii-node-property (node-property contents info)
  1245. "Transcode a NODE-PROPERTY element from Org to ASCII.
  1246. CONTENTS is nil. INFO is a plist holding contextual
  1247. information."
  1248. (format "%s:%s"
  1249. (org-element-property :key node-property)
  1250. (let ((value (org-element-property :value node-property)))
  1251. (if value (concat " " value) ""))))
  1252. ;;;; Paragraph
  1253. (defun org-ascii-paragraph (paragraph contents info)
  1254. "Transcode a PARAGRAPH element from Org to ASCII.
  1255. CONTENTS is the contents of the paragraph, as a string. INFO is
  1256. the plist used as a communication channel."
  1257. (let ((contents (if (not (wholenump org-ascii-indented-line-width)) contents
  1258. (concat
  1259. (make-string org-ascii-indented-line-width ? )
  1260. (replace-regexp-in-string "\\`[ \t]+" "" contents)))))
  1261. (org-ascii--fill-string
  1262. contents (org-ascii--current-text-width paragraph info) info)))
  1263. ;;;; Plain List
  1264. (defun org-ascii-plain-list (plain-list contents info)
  1265. "Transcode a PLAIN-LIST element from Org to ASCII.
  1266. CONTENTS is the contents of the list. INFO is a plist holding
  1267. contextual information."
  1268. contents)
  1269. ;;;; Plain Text
  1270. (defun org-ascii-plain-text (text info)
  1271. "Transcode a TEXT string from Org to ASCII.
  1272. INFO is a plist used as a communication channel."
  1273. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1274. (when (and utf8p (plist-get info :with-smart-quotes))
  1275. (setq text (org-export-activate-smart-quotes text :utf-8 info)))
  1276. (if (not (plist-get info :with-special-strings)) text
  1277. (setq text (replace-regexp-in-string "\\\\-" "" text))
  1278. (if (not utf8p) text
  1279. ;; Usual replacements in utf-8 with proper option set.
  1280. (replace-regexp-in-string
  1281. "\\.\\.\\." "…"
  1282. (replace-regexp-in-string
  1283. "--" "–"
  1284. (replace-regexp-in-string "---" "—" text)))))))
  1285. ;;;; Planning
  1286. (defun org-ascii-planning (planning contents info)
  1287. "Transcode a PLANNING element from Org to ASCII.
  1288. CONTENTS is nil. INFO is a plist used as a communication
  1289. channel."
  1290. (mapconcat
  1291. 'identity
  1292. (delq nil
  1293. (list (let ((closed (org-element-property :closed planning)))
  1294. (when closed
  1295. (concat org-closed-string " "
  1296. (org-translate-time
  1297. (org-element-property :raw-value closed)))))
  1298. (let ((deadline (org-element-property :deadline planning)))
  1299. (when deadline
  1300. (concat org-deadline-string " "
  1301. (org-translate-time
  1302. (org-element-property :raw-value deadline)))))
  1303. (let ((scheduled (org-element-property :scheduled planning)))
  1304. (when scheduled
  1305. (concat org-scheduled-string " "
  1306. (org-translate-time
  1307. (org-element-property :raw-value scheduled)))))))
  1308. " "))
  1309. ;;;; Property Drawer
  1310. (defun org-ascii-property-drawer (property-drawer contents info)
  1311. "Transcode a PROPERTY-DRAWER element from Org to ASCII.
  1312. CONTENTS holds the contents of the drawer. INFO is a plist
  1313. holding contextual information."
  1314. (org-string-nw-p contents))
  1315. ;;;; Quote Block
  1316. (defun org-ascii-quote-block (quote-block contents info)
  1317. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1318. CONTENTS holds the contents of the block. INFO is a plist
  1319. holding contextual information."
  1320. (org-ascii--indent-string contents org-ascii-quote-margin))
  1321. ;;;; Quote Section
  1322. (defun org-ascii-quote-section (quote-section contents info)
  1323. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1324. CONTENTS is nil. INFO is a plist holding contextual information."
  1325. (let ((width (org-ascii--current-text-width quote-section info))
  1326. (value
  1327. (org-export-data
  1328. (org-remove-indentation (org-element-property :value quote-section))
  1329. info)))
  1330. (org-ascii--indent-string
  1331. value
  1332. (+ org-ascii-quote-margin
  1333. ;; Don't apply inner margin if parent headline is low level.
  1334. (let ((headline (org-export-get-parent-headline quote-section)))
  1335. (if (org-export-low-level-p headline info) 0
  1336. org-ascii-inner-margin))))))
  1337. ;;;; Radio Target
  1338. (defun org-ascii-radio-target (radio-target contents info)
  1339. "Transcode a RADIO-TARGET object from Org to ASCII.
  1340. CONTENTS is the contents of the target. INFO is a plist holding
  1341. contextual information."
  1342. contents)
  1343. ;;;; Section
  1344. (defun org-ascii-section (section contents info)
  1345. "Transcode a SECTION element from Org to ASCII.
  1346. CONTENTS is the contents of the section. INFO is a plist holding
  1347. contextual information."
  1348. (org-ascii--indent-string
  1349. (concat
  1350. contents
  1351. (when org-ascii-links-to-notes
  1352. ;; Add list of links at the end of SECTION.
  1353. (let ((links (org-ascii--describe-links
  1354. (org-ascii--unique-links section info)
  1355. (org-ascii--current-text-width section info) info)))
  1356. ;; Separate list of links and section contents.
  1357. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1358. ;; Do not apply inner margin if parent headline is low level.
  1359. (let ((headline (org-export-get-parent-headline section)))
  1360. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1361. org-ascii-inner-margin))))
  1362. ;;;; Special Block
  1363. (defun org-ascii-special-block (special-block contents info)
  1364. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1365. CONTENTS holds the contents of the block. INFO is a plist
  1366. holding contextual information."
  1367. contents)
  1368. ;;;; Src Block
  1369. (defun org-ascii-src-block (src-block contents info)
  1370. "Transcode a SRC-BLOCK element from Org to ASCII.
  1371. CONTENTS holds the contents of the item. INFO is a plist holding
  1372. contextual information."
  1373. (let ((caption (org-ascii--build-caption src-block info))
  1374. (code (org-export-format-code-default src-block info)))
  1375. (if (equal code "") ""
  1376. (concat
  1377. (when (and caption org-ascii-caption-above) (concat caption "\n"))
  1378. (org-ascii--box-string code info)
  1379. (when (and caption (not org-ascii-caption-above))
  1380. (concat "\n" caption))))))
  1381. ;;;; Statistics Cookie
  1382. (defun org-ascii-statistics-cookie (statistics-cookie contents info)
  1383. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1384. CONTENTS is nil. INFO is a plist holding contextual information."
  1385. (org-element-property :value statistics-cookie))
  1386. ;;;; Subscript
  1387. (defun org-ascii-subscript (subscript contents info)
  1388. "Transcode a SUBSCRIPT object from Org to ASCII.
  1389. CONTENTS is the contents of the object. INFO is a plist holding
  1390. contextual information."
  1391. (if (org-element-property :use-brackets-p subscript)
  1392. (format "_{%s}" contents)
  1393. (format "_%s" contents)))
  1394. ;;;; Superscript
  1395. (defun org-ascii-superscript (superscript contents info)
  1396. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1397. CONTENTS is the contents of the object. INFO is a plist holding
  1398. contextual information."
  1399. (if (org-element-property :use-brackets-p superscript)
  1400. (format "_{%s}" contents)
  1401. (format "_%s" contents)))
  1402. ;;;; Strike-through
  1403. (defun org-ascii-strike-through (strike-through contents info)
  1404. "Transcode STRIKE-THROUGH from Org to ASCII.
  1405. CONTENTS is text with strike-through markup. INFO is a plist
  1406. holding contextual information."
  1407. (format "+%s+" contents))
  1408. ;;;; Table
  1409. (defun org-ascii-table (table contents info)
  1410. "Transcode a TABLE element from Org to ASCII.
  1411. CONTENTS is the contents of the table. INFO is a plist holding
  1412. contextual information."
  1413. (let ((caption (org-ascii--build-caption table info)))
  1414. (concat
  1415. ;; Possibly add a caption string above.
  1416. (when (and caption org-ascii-caption-above) (concat caption "\n"))
  1417. ;; Insert table. Note: "table.el" tables are left unmodified.
  1418. (cond ((eq (org-element-property :type table) 'org) contents)
  1419. ((and org-ascii-table-use-ascii-art
  1420. (eq (plist-get info :ascii-charset) 'utf-8)
  1421. (require 'ascii-art-to-unicode nil t))
  1422. (with-temp-buffer
  1423. (insert (org-remove-indentation
  1424. (org-element-property :value table)))
  1425. (goto-char (point-min))
  1426. (aa2u)
  1427. (goto-char (point-max))
  1428. (skip-chars-backward " \r\t\n")
  1429. (buffer-substring (point-min) (point))))
  1430. (t (org-remove-indentation (org-element-property :value table))))
  1431. ;; Possible add a caption string below.
  1432. (and (not org-ascii-caption-above) caption))))
  1433. ;;;; Table Cell
  1434. (defun org-ascii--table-cell-width (table-cell info)
  1435. "Return width of TABLE-CELL.
  1436. INFO is a plist used as a communication channel.
  1437. Width of a cell is determined either by a width cookie in the
  1438. same column as the cell, or by the maximum cell's length in that
  1439. column.
  1440. When `org-ascii-table-widen-columns' is non-nil, width cookies
  1441. are ignored."
  1442. (let* ((row (org-export-get-parent table-cell))
  1443. (table (org-export-get-parent row))
  1444. (col (let ((cells (org-element-contents row)))
  1445. (- (length cells) (length (memq table-cell cells)))))
  1446. (cache
  1447. (or (plist-get info :ascii-table-cell-width-cache)
  1448. (plist-get (setq info
  1449. (plist-put info :ascii-table-cell-width-cache
  1450. (make-hash-table :test 'equal)))
  1451. :ascii-table-cell-width-cache)))
  1452. (key (cons table col)))
  1453. (or (gethash key cache)
  1454. (puthash
  1455. key
  1456. (or (and (not org-ascii-table-widen-columns)
  1457. (org-export-table-cell-width table-cell info))
  1458. (let* ((max-width 0))
  1459. (org-element-map table 'table-row
  1460. (lambda (row)
  1461. (setq max-width
  1462. (max (string-width
  1463. (org-export-data
  1464. (org-element-contents
  1465. (elt (org-element-contents row) col))
  1466. info))
  1467. max-width)))
  1468. info)
  1469. max-width))
  1470. cache))))
  1471. (defun org-ascii-table-cell (table-cell contents info)
  1472. "Transcode a TABLE-CELL object from Org to ASCII.
  1473. CONTENTS is the cell contents. INFO is a plist used as
  1474. a communication channel."
  1475. ;; Determine column width. When `org-ascii-table-widen-columns'
  1476. ;; is nil and some width cookie has set it, use that value.
  1477. ;; Otherwise, compute the maximum width among transcoded data of
  1478. ;; each cell in the column.
  1479. (let ((width (org-ascii--table-cell-width table-cell info)))
  1480. ;; When contents are too large, truncate them.
  1481. (unless (or org-ascii-table-widen-columns
  1482. (<= (string-width (or contents "")) width))
  1483. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1484. ;; Align contents correctly within the cell.
  1485. (let* ((indent-tabs-mode nil)
  1486. (data
  1487. (when contents
  1488. (org-ascii--justify-string
  1489. contents width
  1490. (org-export-table-cell-alignment table-cell info)))))
  1491. (setq contents
  1492. (concat data
  1493. (make-string (- width (string-width (or data ""))) ?\s))))
  1494. ;; Return cell.
  1495. (concat (format " %s " contents)
  1496. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1497. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1498. ;;;; Table Row
  1499. (defun org-ascii-table-row (table-row contents info)
  1500. "Transcode a TABLE-ROW element from Org to ASCII.
  1501. CONTENTS is the row contents. INFO is a plist used as
  1502. a communication channel."
  1503. (when (eq (org-element-property :type table-row) 'standard)
  1504. (let ((build-hline
  1505. (function
  1506. (lambda (lcorner horiz vert rcorner)
  1507. (concat
  1508. (apply
  1509. 'concat
  1510. (org-element-map table-row 'table-cell
  1511. (lambda (cell)
  1512. (let ((width (org-ascii--table-cell-width cell info))
  1513. (borders (org-export-table-cell-borders cell info)))
  1514. (concat
  1515. ;; In order to know if CELL starts the row, do
  1516. ;; not compare it with the first cell in the
  1517. ;; row as there might be a special column.
  1518. ;; Instead, compare it with first exportable
  1519. ;; cell, obtained with `org-element-map'.
  1520. (when (and (memq 'left borders)
  1521. (eq (org-element-map table-row 'table-cell
  1522. 'identity info t)
  1523. cell))
  1524. lcorner)
  1525. (make-string (+ 2 width) (string-to-char horiz))
  1526. (cond
  1527. ((not (memq 'right borders)) nil)
  1528. ((eq (car (last (org-element-contents table-row))) cell)
  1529. rcorner)
  1530. (t vert)))))
  1531. info)) "\n"))))
  1532. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1533. (borders (org-export-table-cell-borders
  1534. (org-element-map table-row 'table-cell 'identity info t)
  1535. info)))
  1536. (concat (cond
  1537. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1538. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1539. (funcall build-hline "+" "-" "+" "+")))
  1540. ((memq 'above borders)
  1541. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1542. (funcall build-hline "+" "-" "+" "+"))))
  1543. (when (memq 'left borders) (if utf8p "│" "|"))
  1544. contents "\n"
  1545. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1546. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1547. (funcall build-hline "+" "-" "+" "+")))))))
  1548. ;;;; Timestamp
  1549. (defun org-ascii-timestamp (timestamp contents info)
  1550. "Transcode a TIMESTAMP object from Org to ASCII.
  1551. CONTENTS is nil. INFO is a plist holding contextual information."
  1552. (org-ascii-plain-text (org-timestamp-translate timestamp) info))
  1553. ;;;; Underline
  1554. (defun org-ascii-underline (underline contents info)
  1555. "Transcode UNDERLINE from Org to ASCII.
  1556. CONTENTS is the text with underline markup. INFO is a plist
  1557. holding contextual information."
  1558. (format "_%s_" contents))
  1559. ;;;; Verbatim
  1560. (defun org-ascii-verbatim (verbatim contents info)
  1561. "Return a VERBATIM object from Org to ASCII.
  1562. CONTENTS is nil. INFO is a plist holding contextual information."
  1563. (format org-ascii-verbatim-format
  1564. (org-element-property :value verbatim)))
  1565. ;;;; Verse Block
  1566. (defun org-ascii-verse-block (verse-block contents info)
  1567. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1568. CONTENTS is verse block contents. INFO is a plist holding
  1569. contextual information."
  1570. (let ((verse-width (org-ascii--current-text-width verse-block info)))
  1571. (org-ascii--indent-string
  1572. (org-ascii--justify-string contents verse-width 'left)
  1573. org-ascii-quote-margin)))
  1574. ;;; Filters
  1575. (defun org-ascii-filter-headline-blank-lines (headline back-end info)
  1576. "Filter controlling number of blank lines after a headline.
  1577. HEADLINE is a string representing a transcoded headline.
  1578. BACK-END is symbol specifying back-end used for export. INFO is
  1579. plist containing the communication channel.
  1580. This function only applies to `ascii' back-end. See
  1581. `org-ascii-headline-spacing' for information."
  1582. (if (not org-ascii-headline-spacing) headline
  1583. (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing)) ?\n)))
  1584. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1585. (defun org-ascii-filter-paragraph-spacing (tree back-end info)
  1586. "Filter controlling number of blank lines between paragraphs.
  1587. TREE is the parse tree. BACK-END is the symbol specifying
  1588. back-end used for export. INFO is a plist used as
  1589. a communication channel.
  1590. See `org-ascii-paragraph-spacing' for information."
  1591. (when (wholenump org-ascii-paragraph-spacing)
  1592. (org-element-map tree 'paragraph
  1593. (lambda (p)
  1594. (when (eq (org-element-type (org-export-get-next-element p info))
  1595. 'paragraph)
  1596. (org-element-put-property
  1597. p :post-blank org-ascii-paragraph-spacing)))))
  1598. tree)
  1599. (defun org-ascii-filter-comment-spacing (tree backend info)
  1600. "Filter removing blank lines between comments.
  1601. TREE is the parse tree. BACK-END is the symbol specifying
  1602. back-end used for export. INFO is a plist used as
  1603. a communication channel."
  1604. (org-element-map tree '(comment comment-block)
  1605. (lambda (c)
  1606. (when (memq (org-element-type (org-export-get-next-element c info))
  1607. '(comment comment-block))
  1608. (org-element-put-property c :post-blank 0))))
  1609. tree)
  1610. ;;; End-user functions
  1611. ;;;###autoload
  1612. (defun org-ascii-export-as-ascii
  1613. (&optional async subtreep visible-only body-only ext-plist)
  1614. "Export current buffer to a text buffer.
  1615. If narrowing is active in the current buffer, only export its
  1616. narrowed part.
  1617. If a region is active, export that region.
  1618. A non-nil optional argument ASYNC means the process should happen
  1619. asynchronously. The resulting buffer should be accessible
  1620. through the `org-export-stack' interface.
  1621. When optional argument SUBTREEP is non-nil, export the sub-tree
  1622. at point, extracting information from the headline properties
  1623. first.
  1624. When optional argument VISIBLE-ONLY is non-nil, don't export
  1625. contents of hidden elements.
  1626. When optional argument BODY-ONLY is non-nil, strip title and
  1627. table of contents from output.
  1628. EXT-PLIST, when provided, is a property list with external
  1629. parameters overriding Org default settings, but still inferior to
  1630. file-local settings.
  1631. Export is done in a buffer named \"*Org ASCII Export*\", which
  1632. will be displayed when `org-export-show-temporary-export-buffer'
  1633. is non-nil."
  1634. (interactive)
  1635. (org-export-to-buffer 'ascii "*Org ASCII Export*"
  1636. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  1637. ;;;###autoload
  1638. (defun org-ascii-export-to-ascii
  1639. (&optional async subtreep visible-only body-only ext-plist)
  1640. "Export current buffer to a text file.
  1641. If narrowing is active in the current buffer, only export its
  1642. narrowed part.
  1643. If a region is active, export that region.
  1644. A non-nil optional argument ASYNC means the process should happen
  1645. asynchronously. The resulting file should be accessible through
  1646. the `org-export-stack' interface.
  1647. When optional argument SUBTREEP is non-nil, export the sub-tree
  1648. at point, extracting information from the headline properties
  1649. first.
  1650. When optional argument VISIBLE-ONLY is non-nil, don't export
  1651. contents of hidden elements.
  1652. When optional argument BODY-ONLY is non-nil, strip title and
  1653. table of contents from output.
  1654. EXT-PLIST, when provided, is a property list with external
  1655. parameters overriding Org default settings, but still inferior to
  1656. file-local settings.
  1657. Return output file's name."
  1658. (interactive)
  1659. (let ((file (org-export-output-file-name ".txt" subtreep)))
  1660. (org-export-to-file 'ascii file
  1661. async subtreep visible-only body-only ext-plist)))
  1662. ;;;###autoload
  1663. (defun org-ascii-publish-to-ascii (plist filename pub-dir)
  1664. "Publish an Org file to ASCII.
  1665. FILENAME is the filename of the Org file to be published. PLIST
  1666. is the property list for the given project. PUB-DIR is the
  1667. publishing directory.
  1668. Return output file name."
  1669. (org-publish-org-to
  1670. 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
  1671. ;;;###autoload
  1672. (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
  1673. "Publish an Org file to Latin-1.
  1674. FILENAME is the filename of the Org file to be published. PLIST
  1675. is the property list for the given project. PUB-DIR is the
  1676. publishing directory.
  1677. Return output file name."
  1678. (org-publish-org-to
  1679. 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
  1680. ;;;###autoload
  1681. (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
  1682. "Publish an org file to UTF-8.
  1683. FILENAME is the filename of the Org file to be published. PLIST
  1684. is the property list for the given project. PUB-DIR is the
  1685. publishing directory.
  1686. Return output file name."
  1687. (org-publish-org-to
  1688. 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
  1689. (provide 'ox-ascii)
  1690. ;; Local variables:
  1691. ;; generated-autoload-file: "org-loaddefs.el"
  1692. ;; coding: utf-8-emacs
  1693. ;; End:
  1694. ;;; ox-ascii.el ends here