org-e-ascii.el 65 KB

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