org-e-ascii.el 65 KB

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