org-e-ascii.el 69 KB

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