org-e-ascii.el 67 KB

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