org-e-ascii.el 64 KB

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