org-e-ascii.el 64 KB

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