org-e-ascii.el 68 KB

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