org-e-ascii.el 63 KB

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