org-e-ascii.el 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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))
  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)))
  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)))
  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 checkbox length, and tag length (for
  537. ;; description lists) or bullet 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 (org-e-ascii--checkbox parent-item info))
  544. (length
  545. (or (org-list-get-tag beg-item struct)
  546. (org-list-get-bullet beg-item struct)))))))))))))
  547. (defun org-e-ascii--build-title
  548. (element info text-width &optional underline notags)
  549. "Format ELEMENT title and return it.
  550. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  551. a plist used as a communication channel. TEXT-WIDTH is an
  552. integer representing the maximum length of a line.
  553. When optional argument UNDERLINE is non-nil, underline title,
  554. without the tags, according to `org-e-ascii-underline'
  555. specifications.
  556. if optional argument NOTAGS is nil, no tags will be added to the
  557. title."
  558. (let* ((headlinep (eq (org-element-type element) 'headline))
  559. (numbers
  560. ;; Numbering is specific to headlines.
  561. (and headlinep (org-export-numbered-headline-p element info)
  562. ;; All tests passed: build numbering string.
  563. (concat
  564. (mapconcat
  565. 'number-to-string
  566. (org-export-get-headline-number element info) ".")
  567. " ")))
  568. (text (org-export-data (org-element-property :title element) info))
  569. (todo
  570. (and (plist-get info :with-todo-keywords)
  571. (let ((todo (org-element-property :todo-keyword element)))
  572. (and todo (concat (org-export-data todo info) " ")))))
  573. (tags (and (not notags)
  574. (plist-get info :with-tags)
  575. (let ((tag-list (org-export-get-tags element info)))
  576. (and tag-list
  577. (format ":%s:"
  578. (mapconcat 'identity tag-list ":"))))))
  579. (priority
  580. (and (plist-get info :with-priority)
  581. (concat (org-element-property :priority element) " ")))
  582. (first-part (concat numbers todo priority text)))
  583. (concat
  584. first-part
  585. ;; Align tags, if any.
  586. (when tags
  587. (format
  588. (format " %%%ds"
  589. (max (- text-width (1+ (length first-part))) (length tags)))
  590. tags))
  591. ;; Maybe underline text, if ELEMENT type is `headline' and an
  592. ;; underline character has been defined.
  593. (when (and underline headlinep)
  594. (let ((under-char
  595. (nth (1- (org-export-get-relative-level element info))
  596. (cdr (assq (plist-get info :ascii-charset)
  597. org-e-ascii-underline)))))
  598. (and under-char
  599. (concat "\n"
  600. (make-string (length first-part) under-char))))))))
  601. (defun org-e-ascii--has-caption-p (element info)
  602. "Non-nil when ELEMENT has a caption affiliated keyword.
  603. INFO is a plist used as a communication channel. This function
  604. is meant to be used as a predicate for `org-export-get-ordinal'."
  605. (org-element-property :caption element))
  606. (defun org-e-ascii--build-caption (element info)
  607. "Return caption string for ELEMENT, if applicable.
  608. INFO is a plist used as a communication channel.
  609. The caption string contains the sequence number of ELEMENT along
  610. with its real caption. Return nil when ELEMENT has no affiliated
  611. caption keyword."
  612. (let ((caption (org-element-property :caption element)))
  613. (when caption
  614. ;; Get sequence number of current src-block among every
  615. ;; src-block with a caption.
  616. (let ((reference
  617. (org-export-get-ordinal
  618. element info nil 'org-e-ascii--has-caption-p))
  619. (title-fmt (org-e-ascii--translate
  620. (case (org-element-type element)
  621. (table "Table %d: %s")
  622. (src-block "Listing %d: %s"))
  623. info)))
  624. (org-e-ascii--fill-string
  625. (format title-fmt reference (org-export-data (car caption) info))
  626. (org-e-ascii--current-text-width element info) info)))))
  627. (defun org-e-ascii--build-toc (info &optional n keyword)
  628. "Return a table of contents.
  629. INFO is a plist used as a communication channel.
  630. Optional argument N, when non-nil, is an integer specifying the
  631. depth of the table.
  632. Optional argument KEYWORD specifies the TOC keyword, if any, from
  633. which the table of contents generation has been initiated."
  634. (let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
  635. (concat
  636. title
  637. (make-string (1- (length title))
  638. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  639. "\n\n"
  640. (let ((text-width
  641. (if keyword (org-e-ascii--current-text-width keyword info)
  642. (- org-e-ascii-text-width org-e-ascii-global-margin))))
  643. (mapconcat
  644. (lambda (headline)
  645. (let* ((level (org-export-get-relative-level headline info))
  646. (indent (* (1- level) 3)))
  647. (concat
  648. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  649. (org-e-ascii--build-title
  650. headline info (- text-width indent) nil
  651. (eq (plist-get info :with-tags) 'not-in-toc)))))
  652. (org-export-collect-headlines info n) "\n")))))
  653. (defun org-e-ascii--list-listings (keyword info)
  654. "Return a list of listings.
  655. KEYWORD is the keyword that initiated the list of listings
  656. generation. INFO is a plist used as a communication channel."
  657. (let ((title (org-e-ascii--translate "List Of Listings\n" info)))
  658. (concat
  659. title
  660. (make-string (1- (length title))
  661. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  662. "\n\n"
  663. (let ((text-width
  664. (if keyword (org-e-ascii--current-text-width keyword info)
  665. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  666. ;; Use a counter instead of retreiving ordinal of each
  667. ;; src-block.
  668. (count 0))
  669. (mapconcat
  670. (lambda (src-block)
  671. ;; Store initial text so its length can be computed. This is
  672. ;; used to properly align caption right to it in case of
  673. ;; filling (like contents of a description list item).
  674. (let ((initial-text
  675. (format (org-e-ascii--translate "Listing %d: " info)
  676. (incf count))))
  677. (concat
  678. initial-text
  679. (org-trim
  680. (org-e-ascii--indent-string
  681. (org-e-ascii--fill-string
  682. (let ((caption (org-element-property :caption src-block)))
  683. ;; Use short name in priority, if available.
  684. (org-export-data (or (cdr caption) (car caption)) info))
  685. (- text-width (length initial-text)) info)
  686. (length initial-text))))))
  687. (org-export-collect-listings info) "\n")))))
  688. (defun org-e-ascii--list-tables (keyword info)
  689. "Return a list of listings.
  690. KEYWORD is the keyword that initiated the list of listings
  691. generation. INFO is a plist used as a communication channel."
  692. (let ((title (org-e-ascii--translate "List Of Tables\n" info)))
  693. (concat
  694. title
  695. (make-string (1- (length title))
  696. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  697. "\n\n"
  698. (let ((text-width
  699. (if keyword (org-e-ascii--current-text-width keyword info)
  700. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  701. ;; Use a counter instead of retreiving ordinal of each
  702. ;; src-block.
  703. (count 0))
  704. (mapconcat
  705. (lambda (table)
  706. ;; Store initial text so its length can be computed. This is
  707. ;; used to properly align caption right to it in case of
  708. ;; filling (like contents of a description list item).
  709. (let ((initial-text
  710. (format (org-e-ascii--translate "Table %d: " info)
  711. (incf count))))
  712. (concat
  713. initial-text
  714. (org-trim
  715. (org-e-ascii--indent-string
  716. (org-e-ascii--fill-string
  717. (let ((caption (org-element-property :caption table)))
  718. ;; Use short name in priority, if available.
  719. (org-export-data (or (cdr caption) (car caption)) info))
  720. (- text-width (length initial-text)) info)
  721. (length initial-text))))))
  722. (org-export-collect-tables info) "\n")))))
  723. (defun org-e-ascii--unique-links (element info)
  724. "Return a list of unique link references in ELEMENT.
  725. ELEMENT is either an headline element or a section element. INFO
  726. is a plist used as a communication channel."
  727. (let* (seen
  728. (unique-link-p
  729. (function
  730. ;; Return LINK if it wasn't referenced so far, or nil.
  731. ;; Update SEEN links along the way.
  732. (lambda (link)
  733. (let ((footprint
  734. (cons (org-element-property :raw-link link)
  735. (org-element-contents link))))
  736. (unless (member footprint seen)
  737. (push footprint seen) link)))))
  738. ;; If at a section, find parent headline, if any, in order to
  739. ;; count links that might be in the title.
  740. (headline
  741. (if (eq (org-element-type element) 'headline) element
  742. (or (org-export-get-parent-headline element) element))))
  743. ;; Get all links in HEADLINE.
  744. (org-element-map
  745. headline 'link (lambda (link) (funcall unique-link-p link)) info)))
  746. (defun org-e-ascii--describe-links (links width info)
  747. "Return a string describing a list of links.
  748. LINKS is a list of link type objects, as returned by
  749. `org-e-ascii--unique-links'. WIDTH is the text width allowed for
  750. the output string. INFO is a plist used as a communication
  751. channel."
  752. (mapconcat
  753. (lambda (link)
  754. (let ((type (org-element-property :type link))
  755. (anchor (let ((desc (org-element-contents link)))
  756. (if (not desc) (org-element-property :raw-link link)
  757. (org-export-data desc info)))))
  758. (cond
  759. ;; Coderefs, radio links and fuzzy links are ignored.
  760. ((member type '("coderef" "radio" "fuzzy")) nil)
  761. ;; Id and custom-id links: Headlines refer to their numbering.
  762. ((member type '("custom-id" "id"))
  763. (let ((dest (org-export-resolve-id-link link info)))
  764. (concat
  765. (org-e-ascii--fill-string
  766. (format
  767. "[%s] %s"
  768. anchor
  769. (if (not dest) (org-e-ascii--translate "Unknown reference" info)
  770. (format
  771. (org-e-ascii--translate "See section %s" info)
  772. (mapconcat 'number-to-string
  773. (org-export-get-headline-number dest info) "."))))
  774. width info) "\n\n")))
  775. ;; Do not add a link that cannot be resolved and doesn't have
  776. ;; any description: destination is already visible in the
  777. ;; paragraph.
  778. ((not (org-element-contents link)) nil)
  779. (t
  780. (concat
  781. (org-e-ascii--fill-string
  782. (format "[%s] %s" anchor (org-element-property :raw-link link))
  783. width info)
  784. "\n\n")))))
  785. links ""))
  786. (defun org-e-ascii--checkbox (item info)
  787. "Return checkbox string for ITEM or nil.
  788. INFO is a plist used as a communication channel."
  789. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  790. (case (org-element-property :checkbox item)
  791. (on (if utf8p "☑ " "[X] "))
  792. (off (if utf8p "☐ " "[ ] "))
  793. (trans (if utf8p "☒ " "[-] ")))))
  794. ;;; Template
  795. (defun org-e-ascii-template--document-title (info)
  796. "Return document title, as a string.
  797. INFO is a plist used as a communication channel."
  798. (let ((text-width org-e-ascii-text-width)
  799. (title (org-export-data (plist-get info :title) info))
  800. (author (and (plist-get info :with-author)
  801. (let ((auth (plist-get info :author)))
  802. (and auth (org-export-data auth info)))))
  803. (email (and (plist-get info :with-email)
  804. (org-export-data (plist-get info :email) info)))
  805. (date (plist-get info :date)))
  806. ;; There are two types of title blocks depending on the presence
  807. ;; of a title to display.
  808. (if (string= title "")
  809. ;; Title block without a title. DATE is positioned at the top
  810. ;; right of the document, AUTHOR to the top left and EMAIL
  811. ;; just below.
  812. (cond
  813. ((and (org-string-nw-p date) (org-string-nw-p author))
  814. (concat
  815. author
  816. (make-string (- text-width (length date) (length author)) ? )
  817. date
  818. (when (org-string-nw-p email) (concat "\n" email))
  819. "\n\n\n"))
  820. ((and (org-string-nw-p date) (org-string-nw-p email))
  821. (concat
  822. email
  823. (make-string (- text-width (length date) (length email)) ? )
  824. date "\n\n\n"))
  825. ((org-string-nw-p date)
  826. (concat
  827. (org-e-ascii--justify-string date text-width 'right)
  828. "\n\n\n"))
  829. ((and (org-string-nw-p author) (org-string-nw-p email))
  830. (concat author "\n" email "\n\n\n"))
  831. ((org-string-nw-p author) (concat author "\n\n\n"))
  832. ((org-string-nw-p email) (concat email "\n\n\n")))
  833. ;; Title block with a title. Document's TITLE, along with the
  834. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  835. ;; centered. Date is just below, also centered.
  836. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  837. ;; Format TITLE. It may be filled if it is too wide,
  838. ;; that is wider than the two thirds of the total width.
  839. (title-len (min (length title) (/ (* 2 text-width) 3)))
  840. (formatted-title (org-e-ascii--fill-string title title-len info))
  841. (line
  842. (make-string
  843. (min (+ (max title-len (length author) (length email)) 2)
  844. text-width) (if utf8p ?━ ?_))))
  845. (org-e-ascii--justify-string
  846. (concat line "\n"
  847. (unless utf8p "\n")
  848. (upcase formatted-title)
  849. (cond
  850. ((and (org-string-nw-p author) (org-string-nw-p email))
  851. (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
  852. ((org-string-nw-p author)
  853. (concat (if utf8p "\n\n\n" "\n\n") author))
  854. ((org-string-nw-p email)
  855. (concat (if utf8p "\n\n\n" "\n\n") email)))
  856. "\n" line
  857. (when (org-string-nw-p date) (concat "\n\n\n" date))
  858. "\n\n\n") text-width 'center)))))
  859. (defun org-e-ascii-template (contents info)
  860. "Return complete document string after ASCII conversion.
  861. CONTENTS is the transcoded contents string. INFO is a plist
  862. holding export options."
  863. (org-element-normalize-string
  864. (org-e-ascii--indent-string
  865. (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
  866. ;; 1. Build title block.
  867. (concat
  868. (org-e-ascii-template--document-title info)
  869. ;; 2. Table of contents.
  870. (let ((depth (plist-get info :with-toc)))
  871. (when depth
  872. (concat
  873. (org-e-ascii--build-toc info (and (wholenump depth) depth))
  874. "\n\n\n")))
  875. ;; 3. Document's body.
  876. contents
  877. ;; 4. Footnote definitions.
  878. (let ((definitions (org-export-collect-footnote-definitions
  879. (plist-get info :parse-tree) info))
  880. ;; Insert full links right inside the footnote definition
  881. ;; as they have no chance to be inserted later.
  882. (org-e-ascii-links-to-notes nil))
  883. (when definitions
  884. (concat
  885. "\n\n\n"
  886. (let ((title (org-e-ascii--translate "Footnotes\n" info)))
  887. (concat
  888. title
  889. (make-string
  890. (1- (length title))
  891. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  892. "\n\n"
  893. (mapconcat
  894. (lambda (ref)
  895. (let ((id (format "[%s] " (car ref))))
  896. ;; Distinguish between inline definitions and
  897. ;; full-fledged definitions.
  898. (org-trim
  899. (let ((def (nth 2 ref)))
  900. (if (eq (org-element-type def) 'org-data)
  901. ;; Full-fledged definition: footnote ID is
  902. ;; inserted inside the first parsed paragraph
  903. ;; (FIRST), if any, to be sure filling will
  904. ;; take it into consideration.
  905. (let ((first (car (org-element-contents def))))
  906. (if (not (eq (org-element-type first) 'paragraph))
  907. (concat id "\n" (org-export-data def info))
  908. (push id (nthcdr 2 first))
  909. (org-export-data def info)))
  910. ;; Fill paragraph once footnote ID is inserted in
  911. ;; order to have a correct length for first line.
  912. (org-e-ascii--fill-string
  913. (concat id (org-export-data def info))
  914. text-width info))))))
  915. definitions "\n\n"))))
  916. ;; 5. Creator. Ignore `comment' value as there are no comments in
  917. ;; ASCII. Justify it to the bottom right.
  918. (let ((creator-info (plist-get info :with-creator)))
  919. (unless (or (not creator-info) (eq creator-info 'comment))
  920. (concat
  921. "\n\n\n"
  922. (org-e-ascii--fill-string
  923. (plist-get info :creator) text-width info 'right))))))
  924. org-e-ascii-global-margin)))
  925. (defun org-e-ascii--translate (s info)
  926. "Translate string S.
  927. INFO is a plist used as a communication channel.
  928. Translation depends on `:language' property and allowed charset.
  929. If no translation in found for a given language and a given
  930. charset, fall-back to S."
  931. (let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
  932. (lang (plist-get info :language))
  933. (translations (cdr (assoc s org-e-ascii-dictionary))))
  934. (or (plist-get (cdr (assoc lang translations)) charset) s)))
  935. ;;; Transcode Functions
  936. ;;;; Babel Call
  937. ;; Babel Calls are ignored.
  938. ;;;; Bold
  939. (defun org-e-ascii-bold (bold contents info)
  940. "Transcode BOLD from Org to ASCII.
  941. CONTENTS is the text with bold markup. INFO is a plist holding
  942. contextual information."
  943. (format "*%s*" contents))
  944. ;;;; Center Block
  945. (defun org-e-ascii-center-block (center-block contents info)
  946. "Transcode a CENTER-BLOCK element from Org to ASCII.
  947. CONTENTS holds the contents of the block. INFO is a plist
  948. holding contextual information."
  949. (org-e-ascii--justify-string
  950. contents (org-e-ascii--current-text-width center-block info) 'center))
  951. ;;;; Clock
  952. (defun org-e-ascii-clock (clock contents info)
  953. "Transcode a CLOCK object from Org to ASCII.
  954. CONTENTS is nil. INFO is a plist holding contextual
  955. information."
  956. (concat org-clock-string " "
  957. (org-translate-time (org-element-property :value clock))
  958. (let ((time (org-element-property :time clock)))
  959. (and time
  960. (concat " => "
  961. (apply 'format
  962. "%2s:%02s"
  963. (org-split-string time ":")))))))
  964. ;;;; Code
  965. (defun org-e-ascii-code (code contents info)
  966. "Return a CODE object from Org to ASCII.
  967. CONTENTS is nil. INFO is a plist holding contextual
  968. information."
  969. (format org-e-ascii-verbatim-format (org-element-property :value code)))
  970. ;;;; Comment
  971. ;; Comments are ignored.
  972. ;;;; Comment Block
  973. ;; Comment Blocks are ignored.
  974. ;;;; Drawer
  975. (defun org-e-ascii-drawer (drawer contents info)
  976. "Transcode a DRAWER element from Org to ASCII.
  977. CONTENTS holds the contents of the block. INFO is a plist
  978. holding contextual information."
  979. (let ((name (org-element-property :drawer-name drawer))
  980. (width (org-e-ascii--current-text-width drawer info)))
  981. (if (functionp org-e-ascii-format-drawer-function)
  982. (funcall org-e-ascii-format-drawer-function name contents width)
  983. ;; If there's no user defined function: simply
  984. ;; display contents of the drawer.
  985. contents)))
  986. ;;;; Dynamic Block
  987. (defun org-e-ascii-dynamic-block (dynamic-block contents info)
  988. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  989. CONTENTS holds the contents of the block. INFO is a plist
  990. holding contextual information."
  991. contents)
  992. ;;;; Entity
  993. (defun org-e-ascii-entity (entity contents info)
  994. "Transcode an ENTITY object from Org to ASCII.
  995. CONTENTS are the definition itself. INFO is a plist holding
  996. contextual information."
  997. (org-element-property
  998. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  999. entity))
  1000. ;;;; Example Block
  1001. (defun org-e-ascii-example-block (example-block contents info)
  1002. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  1003. CONTENTS is nil. INFO is a plist holding contextual information."
  1004. (org-e-ascii--box-string
  1005. (org-export-format-code-default example-block info) info))
  1006. ;;;; Export Snippet
  1007. (defun org-e-ascii-export-snippet (export-snippet contents info)
  1008. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  1009. CONTENTS is nil. INFO is a plist holding contextual information."
  1010. (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
  1011. (org-element-property :value export-snippet)))
  1012. ;;;; Export Block
  1013. (defun org-e-ascii-export-block (export-block contents info)
  1014. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  1015. CONTENTS is nil. INFO is a plist holding contextual information."
  1016. (when (string= (org-element-property :type export-block) "ASCII")
  1017. (org-remove-indentation (org-element-property :value export-block))))
  1018. ;;;; Fixed Width
  1019. (defun org-e-ascii-fixed-width (fixed-width contents info)
  1020. "Transcode a FIXED-WIDTH element from Org to ASCII.
  1021. CONTENTS is nil. INFO is a plist holding contextual information."
  1022. (org-e-ascii--box-string
  1023. (org-remove-indentation
  1024. (org-element-property :value fixed-width)) info))
  1025. ;;;; Footnote Definition
  1026. ;; Footnote Definitions are ignored. They are compiled at the end of
  1027. ;; the document, by `org-e-ascii-template'.
  1028. ;;;; Footnote Reference
  1029. (defun org-e-ascii-footnote-reference (footnote-reference contents info)
  1030. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  1031. CONTENTS is nil. INFO is a plist holding contextual information."
  1032. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  1033. ;;;; Headline
  1034. (defun org-e-ascii-headline (headline contents info)
  1035. "Transcode an HEADLINE element from Org to ASCII.
  1036. CONTENTS holds the contents of the headline. INFO is a plist
  1037. holding contextual information."
  1038. ;; Don't export footnote section, which will be handled at the end
  1039. ;; of the template.
  1040. (unless (org-element-property :footnote-section-p headline)
  1041. (let* ((low-level-rank (org-export-low-level-p headline info))
  1042. (width (org-e-ascii--current-text-width headline info))
  1043. ;; Blank lines between headline and its contents.
  1044. ;; `org-e-ascii-headline-spacing', when set, overwrites
  1045. ;; original buffer's spacing.
  1046. (pre-blanks
  1047. (make-string
  1048. (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
  1049. (org-element-property :pre-blank headline)) ?\n))
  1050. ;; Even if HEADLINE has no section, there might be some
  1051. ;; links in its title that we shouldn't forget to describe.
  1052. (links
  1053. (unless (eq (caar (org-element-contents headline)) 'section)
  1054. (org-e-ascii--describe-links
  1055. (org-e-ascii--unique-links headline info) width info))))
  1056. ;; Deep subtree: export it as a list item.
  1057. (if low-level-rank
  1058. (concat
  1059. ;; Bullet.
  1060. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  1061. org-e-ascii-bullets))))
  1062. (char-to-string
  1063. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  1064. " "
  1065. ;; Title.
  1066. (org-e-ascii--build-title headline info width) "\n"
  1067. ;; Contents, indented by length of bullet.
  1068. pre-blanks
  1069. (org-e-ascii--indent-string
  1070. (concat contents
  1071. (when (org-string-nw-p links) (concat "\n\n" links)))
  1072. 2))
  1073. ;; Else: Standard headline.
  1074. (concat
  1075. (org-e-ascii--build-title headline info width 'underline)
  1076. "\n" pre-blanks
  1077. (concat (when (org-string-nw-p links) links) contents))))))
  1078. ;;;; Horizontal Rule
  1079. (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  1080. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  1081. CONTENTS is nil. INFO is a plist holding contextual
  1082. information."
  1083. (let ((attr
  1084. (read
  1085. (format
  1086. "(%s)"
  1087. (mapconcat
  1088. #'identity
  1089. (org-element-property :attr_ascii horizontal-rule)
  1090. " ")))))
  1091. (make-string (or (and (wholenump (plist-get attr :width))
  1092. (plist-get attr :width))
  1093. (org-e-ascii--current-text-width horizontal-rule info))
  1094. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))
  1095. ;;;; Inline Babel Call
  1096. ;; Inline Babel Calls are ignored.
  1097. ;;;; Inline Src Block
  1098. (defun org-e-ascii-inline-src-block (inline-src-block contents info)
  1099. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  1100. CONTENTS holds the contents of the item. INFO is a plist holding
  1101. contextual information."
  1102. (format org-e-ascii-verbatim-format
  1103. (org-element-property :value inline-src-block)))
  1104. ;;;; Inlinetask
  1105. (defun org-e-ascii-inlinetask (inlinetask contents info)
  1106. "Transcode an INLINETASK element from Org to ASCII.
  1107. CONTENTS holds the contents of the block. INFO is a plist
  1108. holding contextual information."
  1109. (let ((width (org-e-ascii--current-text-width inlinetask info)))
  1110. ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
  1111. ;; with appropriate arguments.
  1112. (if (functionp org-e-ascii-format-inlinetask-function)
  1113. (funcall org-e-ascii-format-inlinetask-function
  1114. ;; todo.
  1115. (and (plist-get info :with-todo-keywords)
  1116. (let ((todo (org-element-property
  1117. :todo-keyword inlinetask)))
  1118. (and todo (org-export-data todo info))))
  1119. ;; todo-type
  1120. (org-element-property :todo-type inlinetask)
  1121. ;; priority
  1122. (and (plist-get info :with-priority)
  1123. (org-element-property :priority inlinetask))
  1124. ;; title
  1125. (org-export-data (org-element-property :title inlinetask) info)
  1126. ;; tags
  1127. (and (plist-get info :with-tags)
  1128. (org-element-property :tags inlinetask))
  1129. ;; contents and width
  1130. contents width)
  1131. ;; Otherwise, use a default template.
  1132. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1133. (org-e-ascii--indent-string
  1134. (concat
  1135. ;; Top line, with an additional blank line if not in UTF-8.
  1136. (make-string width (if utf8p ?━ ?_)) "\n"
  1137. (unless utf8p (concat (make-string width ? ) "\n"))
  1138. ;; Add title. Fill it if wider than inlinetask.
  1139. (let ((title (org-e-ascii--build-title inlinetask info width)))
  1140. (if (<= (length title) width) title
  1141. (org-e-ascii--fill-string title width info)))
  1142. "\n"
  1143. ;; If CONTENTS is not empty, insert it along with
  1144. ;; a separator.
  1145. (when (org-string-nw-p contents)
  1146. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1147. ;; Bottom line.
  1148. (make-string width (if utf8p ?━ ?_)))
  1149. ;; Flush the inlinetask to the right.
  1150. (- org-e-ascii-text-width org-e-ascii-global-margin
  1151. (if (not (org-export-get-parent-headline inlinetask)) 0
  1152. org-e-ascii-inner-margin)
  1153. (org-e-ascii--current-text-width inlinetask info)))))))
  1154. ;;;; Italic
  1155. (defun org-e-ascii-italic (italic contents info)
  1156. "Transcode italic from Org to ASCII.
  1157. CONTENTS is the text with italic markup. INFO is a plist holding
  1158. contextual information."
  1159. (format "/%s/" contents))
  1160. ;;;; Item
  1161. (defun org-e-ascii-item (item contents info)
  1162. "Transcode an ITEM element from Org to ASCII.
  1163. CONTENTS holds the contents of the item. INFO is a plist holding
  1164. contextual information."
  1165. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1166. (checkbox (org-e-ascii--checkbox item info))
  1167. (list-type (org-element-property :type (org-export-get-parent item)))
  1168. (bullet
  1169. ;; First parent of ITEM is always the plain-list. Get
  1170. ;; `:type' property from it.
  1171. (org-list-bullet-string
  1172. (case list-type
  1173. (descriptive
  1174. (concat checkbox
  1175. (org-export-data (org-element-property :tag item) info)
  1176. ": "))
  1177. (ordered
  1178. ;; Return correct number for ITEM, paying attention to
  1179. ;; counters.
  1180. (let* ((struct (org-element-property :structure item))
  1181. (bul (org-element-property :bullet item))
  1182. (num (number-to-string
  1183. (car (last (org-list-get-item-number
  1184. (org-element-property :begin item)
  1185. struct
  1186. (org-list-prevs-alist struct)
  1187. (org-list-parents-alist struct)))))))
  1188. (replace-regexp-in-string "[0-9]+" num bul)))
  1189. (t (let ((bul (org-element-property :bullet item)))
  1190. ;; Change bullets into more visible form if UTF-8 is active.
  1191. (if (not utf8p) bul
  1192. (replace-regexp-in-string
  1193. "-" "•"
  1194. (replace-regexp-in-string
  1195. "+" "⁃"
  1196. (replace-regexp-in-string "*" "‣" bul))))))))))
  1197. (concat
  1198. bullet
  1199. (unless (eq list-type 'descriptive) checkbox)
  1200. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1201. ;; already taken care of at the paragraph level so they don't
  1202. ;; interfere with indentation.
  1203. (let ((contents (org-e-ascii--indent-string contents (length bullet))))
  1204. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1205. (org-trim contents)
  1206. (concat "\n" contents))))))
  1207. ;;;; Keyword
  1208. (defun org-e-ascii-keyword (keyword contents info)
  1209. "Transcode a KEYWORD element from Org to ASCII.
  1210. CONTENTS is nil. INFO is a plist holding contextual
  1211. information."
  1212. (let ((key (org-element-property :key keyword))
  1213. (value (org-element-property :value keyword)))
  1214. (cond
  1215. ((string= key "ASCII") value)
  1216. ((string= key "TOC")
  1217. (let ((value (downcase value)))
  1218. (cond
  1219. ((string-match "\\<headlines\\>" value)
  1220. (let ((depth (or (and (string-match "[0-9]+" value)
  1221. (string-to-number (match-string 0 value)))
  1222. (plist-get info :with-toc))))
  1223. (org-e-ascii--build-toc
  1224. info (and (wholenump depth) depth) keyword)))
  1225. ((string= "tables" value)
  1226. (org-e-ascii--list-tables keyword info))
  1227. ((string= "listings" value)
  1228. (org-e-ascii--list-listings keyword info))))))))
  1229. ;;;; Latex Environment
  1230. (defun org-e-ascii-latex-environment (latex-environment contents info)
  1231. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1232. CONTENTS is nil. INFO is a plist holding contextual
  1233. information."
  1234. (org-remove-indentation (org-element-property :value latex-environment)))
  1235. ;;;; Latex Fragment
  1236. (defun org-e-ascii-latex-fragment (latex-fragment contents info)
  1237. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1238. CONTENTS is nil. INFO is a plist holding contextual
  1239. information."
  1240. (org-element-property :value latex-fragment))
  1241. ;;;; Line Break
  1242. (defun org-e-ascii-line-break (line-break contents info)
  1243. "Transcode a LINE-BREAK object from Org to ASCII.
  1244. CONTENTS is nil. INFO is a plist holding contextual
  1245. information." hard-newline)
  1246. ;;;; Link
  1247. (defun org-e-ascii-link (link desc info)
  1248. "Transcode a LINK object from Org to ASCII.
  1249. DESC is the description part of the link, or the empty string.
  1250. INFO is a plist holding contextual information."
  1251. (let ((raw-link (org-element-property :raw-link link))
  1252. (type (org-element-property :type link)))
  1253. (cond
  1254. ((string= type "coderef")
  1255. (let ((ref (org-element-property :path link)))
  1256. (format (org-export-get-coderef-format ref desc)
  1257. (org-export-resolve-coderef ref info))))
  1258. ;; Do not apply a special syntax on radio links. Though, use
  1259. ;; transcoded target's contents as output.
  1260. ((string= type "radio")
  1261. (let ((destination (org-export-resolve-radio-link link info)))
  1262. (when destination
  1263. (org-export-data (org-element-contents destination) info))))
  1264. ;; Do not apply a special syntax on fuzzy links pointing to
  1265. ;; targets.
  1266. ((string= type "fuzzy")
  1267. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1268. ;; Ignore invisible "#+TARGET: path".
  1269. (unless (eq (org-element-type destination) 'keyword)
  1270. (if (org-string-nw-p desc) desc
  1271. (when destination
  1272. (let ((number
  1273. (org-export-get-ordinal
  1274. destination info nil 'org-e-ascii--has-caption-p)))
  1275. (when number
  1276. (if (atom number) (number-to-string number)
  1277. (mapconcat 'number-to-string number ".")))))))))
  1278. (t
  1279. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1280. (concat
  1281. (format "[%s]" desc)
  1282. (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1283. ;;;; Macro
  1284. (defun org-e-ascii-macro (macro contents info)
  1285. "Transcode a MACRO element from Org to ASCII.
  1286. CONTENTS is nil. INFO is a plist holding contextual
  1287. information."
  1288. (org-export-expand-macro macro info))
  1289. ;;;; Paragraph
  1290. (defun org-e-ascii-paragraph (paragraph contents info)
  1291. "Transcode a PARAGRAPH element from Org to ASCII.
  1292. CONTENTS is the contents of the paragraph, as a string. INFO is
  1293. the plist used as a communication channel."
  1294. (org-e-ascii--fill-string
  1295. contents
  1296. (org-e-ascii--current-text-width paragraph info) info))
  1297. ;;;; Plain List
  1298. (defun org-e-ascii-plain-list (plain-list contents info)
  1299. "Transcode a PLAIN-LIST element from Org to ASCII.
  1300. CONTENTS is the contents of the list. INFO is a plist holding
  1301. contextual information."
  1302. contents)
  1303. ;;;; Plain Text
  1304. (defun org-e-ascii-plain-text (text info)
  1305. "Transcode a TEXT string from Org to ASCII.
  1306. INFO is a plist used as a communication channel."
  1307. (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
  1308. (plist-get info :with-special-strings)))
  1309. text
  1310. ;; Usual replacements in utf-8 with proper option set.
  1311. (replace-regexp-in-string
  1312. "\\.\\.\\." "…"
  1313. (replace-regexp-in-string
  1314. "--" "–"
  1315. (replace-regexp-in-string "---" "—" text)))))
  1316. ;;;; Planning
  1317. (defun org-e-ascii-planning (planning contents info)
  1318. "Transcode a PLANNING element from Org to ASCII.
  1319. CONTENTS is nil. INFO is a plist used as a communication
  1320. channel."
  1321. (mapconcat
  1322. 'identity
  1323. (delq nil
  1324. (list (let ((closed (org-element-property :closed planning)))
  1325. (when closed (concat org-closed-string " "
  1326. (org-translate-time closed))))
  1327. (let ((deadline (org-element-property :deadline planning)))
  1328. (when deadline (concat org-deadline-string " "
  1329. (org-translate-time deadline))))
  1330. (let ((scheduled (org-element-property :scheduled planning)))
  1331. (when scheduled (concat org-scheduled-string " "
  1332. (org-translate-time scheduled))))))
  1333. " "))
  1334. ;;;; Property Drawer
  1335. ;;
  1336. ;; Property drawers are ignored.
  1337. ;;;; Quote Block
  1338. (defun org-e-ascii-quote-block (quote-block contents info)
  1339. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1340. CONTENTS holds the contents of the block. INFO is a plist
  1341. holding contextual information."
  1342. (let ((width (org-e-ascii--current-text-width quote-block info)))
  1343. (org-e-ascii--indent-string
  1344. (org-remove-indentation
  1345. (org-e-ascii--fill-string contents width info))
  1346. org-e-ascii-quote-margin)))
  1347. ;;;; Quote Section
  1348. (defun org-e-ascii-quote-section (quote-section contents info)
  1349. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1350. CONTENTS is nil. INFO is a plist holding contextual information."
  1351. (let ((width (org-e-ascii--current-text-width quote-section info))
  1352. (value
  1353. (org-export-data
  1354. (org-remove-indentation (org-element-property :value quote-section))
  1355. info)))
  1356. (org-e-ascii--indent-string
  1357. value
  1358. (+ org-e-ascii-quote-margin
  1359. ;; Don't apply inner margin if parent headline is low level.
  1360. (let ((headline (org-export-get-parent-headline quote-section)))
  1361. (if (org-export-low-level-p headline info) 0
  1362. org-e-ascii-inner-margin))))))
  1363. ;;;; Radio Target
  1364. (defun org-e-ascii-radio-target (radio-target contents info)
  1365. "Transcode a RADIO-TARGET object from Org to ASCII.
  1366. CONTENTS is the contents of the target. INFO is a plist holding
  1367. contextual information."
  1368. contents)
  1369. ;;;; Section
  1370. (defun org-e-ascii-section (section contents info)
  1371. "Transcode a SECTION element from Org to ASCII.
  1372. CONTENTS is the contents of the section. INFO is a plist holding
  1373. contextual information."
  1374. (org-e-ascii--indent-string
  1375. (concat
  1376. contents
  1377. (when org-e-ascii-links-to-notes
  1378. ;; Add list of links at the end of SECTION.
  1379. (let ((links (org-e-ascii--describe-links
  1380. (org-e-ascii--unique-links section info)
  1381. (org-e-ascii--current-text-width section info) info)))
  1382. ;; Separate list of links and section contents.
  1383. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1384. ;; Do not apply inner margin if parent headline is low level.
  1385. (let ((headline (org-export-get-parent-headline section)))
  1386. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1387. org-e-ascii-inner-margin))))
  1388. ;;;; Special Block
  1389. (defun org-e-ascii-special-block (special-block contents info)
  1390. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1391. CONTENTS holds the contents of the block. INFO is a plist
  1392. holding contextual information."
  1393. contents)
  1394. ;;;; Src Block
  1395. (defun org-e-ascii-src-block (src-block contents info)
  1396. "Transcode a SRC-BLOCK element from Org to ASCII.
  1397. CONTENTS holds the contents of the item. INFO is a plist holding
  1398. contextual information."
  1399. (let ((caption (org-e-ascii--build-caption src-block info)))
  1400. (concat
  1401. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1402. (org-e-ascii--box-string
  1403. (org-export-format-code-default src-block info) info)
  1404. (when (and caption (not org-e-ascii-caption-above))
  1405. (concat "\n" caption)))))
  1406. ;;;; Statistics Cookie
  1407. (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  1408. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1409. CONTENTS is nil. INFO is a plist holding contextual information."
  1410. (org-element-property :value statistics-cookie))
  1411. ;;;; Subscript
  1412. (defun org-e-ascii-subscript (subscript contents info)
  1413. "Transcode a SUBSCRIPT object from Org to ASCII.
  1414. CONTENTS is the contents of the object. INFO is a plist holding
  1415. contextual information."
  1416. (if (org-element-property :use-brackets-p subscript)
  1417. (format "_{%s}" contents)
  1418. (format "_%s" contents)))
  1419. ;;;; Superscript
  1420. (defun org-e-ascii-superscript (superscript contents info)
  1421. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1422. CONTENTS is the contents of the object. INFO is a plist holding
  1423. contextual information."
  1424. (if (org-element-property :use-brackets-p superscript)
  1425. (format "_{%s}" contents)
  1426. (format "_%s" contents)))
  1427. ;;;; Strike-through
  1428. (defun org-e-ascii-strike-through (strike-through contents info)
  1429. "Transcode STRIKE-THROUGH from Org to ASCII.
  1430. CONTENTS is text with strike-through markup. INFO is a plist
  1431. holding contextual information."
  1432. (format "+%s+" contents))
  1433. ;;;; Table
  1434. (defun org-e-ascii-table (table contents info)
  1435. "Transcode a TABLE element from Org to ASCII.
  1436. CONTENTS is the contents of the table. INFO is a plist holding
  1437. contextual information."
  1438. (let ((caption (org-e-ascii--build-caption table info)))
  1439. (concat
  1440. ;; Possibly add a caption string above.
  1441. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1442. ;; Insert table. Note: "table.el" tables are left unmodified.
  1443. (if (eq (org-element-property :type table) 'org) contents
  1444. (org-remove-indentation (org-element-property :value table)))
  1445. ;; Possible add a caption string below.
  1446. (when (and caption (not org-e-ascii-caption-above))
  1447. (concat "\n" caption)))))
  1448. ;;;; Table Cell
  1449. (defun org-e-ascii--table-cell-width (table-cell info)
  1450. "Return width of TABLE-CELL.
  1451. INFO is a plist used as a communication channel.
  1452. Width of a cell is determined either by a width cookie in the
  1453. same column as the cell, or by the maximum cell's length in that
  1454. column.
  1455. When `org-e-ascii-table-widen-columns' is non-nil, width cookies
  1456. are ignored."
  1457. (or (and (not org-e-ascii-table-widen-columns)
  1458. (org-export-table-cell-width table-cell info))
  1459. (let* ((max-width 0)
  1460. (table (org-export-get-parent-table table-cell))
  1461. (specialp (org-export-table-has-special-column-p table))
  1462. (col (cdr (org-export-table-cell-address table-cell info))))
  1463. (org-element-map
  1464. table 'table-row
  1465. (lambda (row)
  1466. (setq max-width
  1467. (max (length
  1468. (org-export-data
  1469. (org-element-contents
  1470. (elt (if specialp (car (org-element-contents row))
  1471. (org-element-contents row))
  1472. col))
  1473. info))
  1474. max-width)))
  1475. info)
  1476. max-width)))
  1477. (defun org-e-ascii-table-cell (table-cell contents info)
  1478. "Transcode a TABLE-CELL object from Org to ASCII.
  1479. CONTENTS is the cell contents. INFO is a plist used as
  1480. a communication channel."
  1481. ;; Determine column width. When `org-e-ascii-table-widen-columns'
  1482. ;; is nil and some width cookie has set it, use that value.
  1483. ;; Otherwise, compute the maximum width among transcoded data of
  1484. ;; each cell in the column.
  1485. (let ((width (org-e-ascii--table-cell-width table-cell info)))
  1486. ;; When contents are too large, truncate them.
  1487. (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
  1488. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1489. ;; Align contents correctly within the cell.
  1490. (let* ((indent-tabs-mode nil)
  1491. (data
  1492. (when contents
  1493. (org-e-ascii--justify-string
  1494. contents width
  1495. (org-export-table-cell-alignment table-cell info)))))
  1496. (setq contents (concat data (make-string (- width (length data)) ? ))))
  1497. ;; Return cell.
  1498. (concat (format " %s " contents)
  1499. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1500. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1501. ;;;; Table Row
  1502. (defun org-e-ascii-table-row (table-row contents info)
  1503. "Transcode a TABLE-ROW element from Org to ASCII.
  1504. CONTENTS is the row contents. INFO is a plist used as
  1505. a communication channel."
  1506. (when (eq (org-element-property :type table-row) 'standard)
  1507. (let ((build-hline
  1508. (function
  1509. (lambda (lcorner horiz vert rcorner)
  1510. (concat
  1511. (apply
  1512. 'concat
  1513. (org-element-map
  1514. table-row 'table-cell
  1515. (lambda (cell)
  1516. (let ((width (org-e-ascii--table-cell-width cell info))
  1517. (borders (org-export-table-cell-borders cell info)))
  1518. (concat
  1519. (when (and (memq 'left borders)
  1520. (equal (org-element-map
  1521. table-row 'table-cell 'identity info t)
  1522. cell)))
  1523. (make-string (+ 2 width) (string-to-char horiz))
  1524. (cond
  1525. ((not (memq 'right borders)) nil)
  1526. ((equal (car (last (org-element-contents table-row)))
  1527. cell)
  1528. rcorner)
  1529. (t vert)))))
  1530. info)) "\n"))))
  1531. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1532. (borders (org-export-table-cell-borders
  1533. (org-element-map table-row 'table-cell 'identity info t)
  1534. info)))
  1535. (concat (cond
  1536. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1537. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1538. (funcall build-hline "+" "-" "+" "+")))
  1539. ((memq 'above borders)
  1540. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1541. (funcall build-hline "+" "-" "+" "+"))))
  1542. (when (memq 'left borders) (if utf8p "│" "|"))
  1543. contents "\n"
  1544. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1545. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1546. (funcall build-hline "+" "-" "+" "+")))))))
  1547. ;;;; Target
  1548. ;; Targets are invisible.
  1549. ;;;; Timestamp
  1550. (defun org-e-ascii-timestamp (timestamp contents info)
  1551. "Transcode a TIMESTAMP object from Org to ASCII.
  1552. CONTENTS is nil. INFO is a plist holding contextual information."
  1553. (org-translate-time (org-element-property :value timestamp)))
  1554. ;;;; Underline
  1555. (defun org-e-ascii-underline (underline contents info)
  1556. "Transcode UNDERLINE from Org to ASCII.
  1557. CONTENTS is the text with underline markup. INFO is a plist
  1558. holding contextual information."
  1559. (format "_%s_" contents))
  1560. ;;;; Verbatim
  1561. (defun org-e-ascii-verbatim (verbatim contents info)
  1562. "Return a VERBATIM object from Org to ASCII.
  1563. CONTENTS is nil. INFO is a plist holding contextual information."
  1564. (format org-e-ascii-verbatim-format
  1565. (org-element-property :value verbatim)))
  1566. ;;;; Verse Block
  1567. (defun org-e-ascii-verse-block (verse-block contents info)
  1568. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1569. CONTENTS is verse block contents. INFO is a plist holding
  1570. contextual information."
  1571. (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
  1572. (org-e-ascii--indent-string
  1573. (org-e-ascii--justify-string contents verse-width 'left)
  1574. org-e-ascii-quote-margin)))
  1575. ;;; Filter
  1576. (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  1577. "Filter controlling number of blank lines after an headline.
  1578. HEADLINE is a string representing a transcoded headline.
  1579. BACK-END is symbol specifying back-end used for export. INFO is
  1580. plist containing the communication channel.
  1581. This function only applies to `e-ascii' back-end. See
  1582. `org-e-ascii-headline-spacing' for information.
  1583. For any other back-end, HEADLINE is returned as-is."
  1584. (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
  1585. (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
  1586. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1587. ;;; Interactive function
  1588. (defun org-e-ascii-export-to-ascii
  1589. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1590. "Export current buffer to a text file.
  1591. If narrowing is active in the current buffer, only export its
  1592. narrowed part.
  1593. If a region is active, export that region.
  1594. When optional argument SUBTREEP is non-nil, export the sub-tree
  1595. at point, extracting information from the headline properties
  1596. first.
  1597. When optional argument VISIBLE-ONLY is non-nil, don't export
  1598. contents of hidden elements.
  1599. When optional argument BODY-ONLY is non-nil, strip title, table
  1600. of contents and footnote definitions from output.
  1601. EXT-PLIST, when provided, is a property list with external
  1602. parameters overriding Org default settings, but still inferior to
  1603. file-local settings.
  1604. When optional argument PUB-DIR is set, use it as the publishing
  1605. directory.
  1606. Return output file's name."
  1607. (interactive)
  1608. (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
  1609. (org-export-to-file
  1610. 'e-ascii outfile subtreep visible-only body-only ext-plist)))
  1611. (provide 'org-e-ascii)
  1612. ;;; org-e-ascii.el ends here