org-e-ascii.el 66 KB

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