org-e-ascii.el 66 KB

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