ox-ascii.el 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  1. ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Maintainer: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This library implements an ASCII back-end for Org generic exporter.
  20. ;; See Org manual for more information.
  21. ;;; Code:
  22. (require 'org-macs)
  23. (org-assert-version)
  24. (require 'ox)
  25. (require 'ox-publish)
  26. (require 'cl-lib)
  27. ;;; Function Declarations
  28. (declare-function aa2u "ext:ascii-art-to-unicode" ())
  29. (declare-function org-at-heading-p "org" (&optional _))
  30. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  31. (declare-function org-next-visible-heading "org" (arg))
  32. ;;; Define Back-End
  33. ;;
  34. ;; The following setting won't allow modifying preferred charset
  35. ;; through a buffer keyword or an option item, but, since the property
  36. ;; will appear in communication channel nonetheless, it allows
  37. ;; overriding `org-ascii-charset' variable on the fly by the ext-plist
  38. ;; mechanism.
  39. ;;
  40. ;; We also install a filter for headlines and sections, in order to
  41. ;; control blank lines separating them in output string.
  42. (org-export-define-backend 'ascii
  43. '((bold . org-ascii-bold)
  44. (center-block . org-ascii-center-block)
  45. (clock . org-ascii-clock)
  46. (code . org-ascii-code)
  47. (drawer . org-ascii-drawer)
  48. (dynamic-block . org-ascii-dynamic-block)
  49. (entity . org-ascii-entity)
  50. (example-block . org-ascii-example-block)
  51. (export-block . org-ascii-export-block)
  52. (export-snippet . org-ascii-export-snippet)
  53. (fixed-width . org-ascii-fixed-width)
  54. (footnote-reference . org-ascii-footnote-reference)
  55. (headline . org-ascii-headline)
  56. (horizontal-rule . org-ascii-horizontal-rule)
  57. (inline-src-block . org-ascii-inline-src-block)
  58. (inlinetask . org-ascii-inlinetask)
  59. (inner-template . org-ascii-inner-template)
  60. (italic . org-ascii-italic)
  61. (item . org-ascii-item)
  62. (keyword . org-ascii-keyword)
  63. (latex-environment . org-ascii-latex-environment)
  64. (latex-fragment . org-ascii-latex-fragment)
  65. (line-break . org-ascii-line-break)
  66. (link . org-ascii-link)
  67. (node-property . org-ascii-node-property)
  68. (paragraph . org-ascii-paragraph)
  69. (plain-list . org-ascii-plain-list)
  70. (plain-text . org-ascii-plain-text)
  71. (planning . org-ascii-planning)
  72. (property-drawer . org-ascii-property-drawer)
  73. (quote-block . org-ascii-quote-block)
  74. (radio-target . org-ascii-radio-target)
  75. (section . org-ascii-section)
  76. (special-block . org-ascii-special-block)
  77. (src-block . org-ascii-src-block)
  78. (statistics-cookie . org-ascii-statistics-cookie)
  79. (strike-through . org-ascii-strike-through)
  80. (subscript . org-ascii-subscript)
  81. (superscript . org-ascii-superscript)
  82. (table . org-ascii-table)
  83. (table-cell . org-ascii-table-cell)
  84. (table-row . org-ascii-table-row)
  85. (target . org-ascii-target)
  86. (template . org-ascii-template)
  87. (timestamp . org-ascii-timestamp)
  88. (underline . org-ascii-underline)
  89. (verbatim . org-ascii-verbatim)
  90. (verse-block . org-ascii-verse-block))
  91. :menu-entry
  92. '(?t "Export to Plain Text"
  93. ((?A "As ASCII buffer"
  94. (lambda (a s v b)
  95. (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
  96. (?a "As ASCII file"
  97. (lambda (a s v b)
  98. (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
  99. (?L "As Latin1 buffer"
  100. (lambda (a s v b)
  101. (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
  102. (?l "As Latin1 file"
  103. (lambda (a s v b)
  104. (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
  105. (?U "As UTF-8 buffer"
  106. (lambda (a s v b)
  107. (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
  108. (?u "As UTF-8 file"
  109. (lambda (a s v b)
  110. (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
  111. :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
  112. (:filter-parse-tree org-ascii-filter-paragraph-spacing
  113. org-ascii-filter-comment-spacing)
  114. (:filter-section . org-ascii-filter-headline-blank-lines))
  115. :options-alist
  116. '((:subtitle "SUBTITLE" nil nil parse)
  117. (:ascii-bullets nil nil org-ascii-bullets)
  118. (:ascii-caption-above nil nil org-ascii-caption-above)
  119. (:ascii-charset nil nil org-ascii-charset)
  120. (:ascii-global-margin nil nil org-ascii-global-margin)
  121. (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function)
  122. (:ascii-format-inlinetask-function
  123. nil nil org-ascii-format-inlinetask-function)
  124. (:ascii-headline-spacing nil nil org-ascii-headline-spacing)
  125. (:ascii-indented-line-width nil nil org-ascii-indented-line-width)
  126. (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width)
  127. (:ascii-inner-margin nil nil org-ascii-inner-margin)
  128. (:ascii-links-to-notes nil nil org-ascii-links-to-notes)
  129. (:ascii-list-margin nil nil org-ascii-list-margin)
  130. (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing)
  131. (:ascii-quote-margin nil nil org-ascii-quote-margin)
  132. (:ascii-table-keep-all-vertical-lines
  133. nil nil org-ascii-table-keep-all-vertical-lines)
  134. (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art)
  135. (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns)
  136. (:ascii-text-width nil nil org-ascii-text-width)
  137. (:ascii-underline nil nil org-ascii-underline)
  138. (:ascii-verbatim-format nil nil org-ascii-verbatim-format)))
  139. ;;; User Configurable Variables
  140. (defgroup org-export-ascii nil
  141. "Options for exporting Org mode files to ASCII."
  142. :tag "Org Export ASCII"
  143. :group 'org-export)
  144. (defcustom org-ascii-text-width 72
  145. "Maximum width of exported text.
  146. This number includes margin size, as set in
  147. `org-ascii-global-margin'."
  148. :group 'org-export-ascii
  149. :version "24.4"
  150. :package-version '(Org . "8.0")
  151. :type 'integer)
  152. (defcustom org-ascii-global-margin 0
  153. "Width of the left margin, in number of characters."
  154. :group 'org-export-ascii
  155. :version "24.4"
  156. :package-version '(Org . "8.0")
  157. :type 'integer)
  158. (defcustom org-ascii-inner-margin 2
  159. "Width of the inner margin, in number of characters.
  160. Inner margin is applied between each headline."
  161. :group 'org-export-ascii
  162. :version "24.4"
  163. :package-version '(Org . "8.0")
  164. :type 'integer)
  165. (defcustom org-ascii-quote-margin 6
  166. "Width of margin used for quoting text, in characters.
  167. This margin is applied on both sides of the text. It is also
  168. applied on the left side of contents in descriptive lists."
  169. :group 'org-export-ascii
  170. :version "24.4"
  171. :package-version '(Org . "8.0")
  172. :type 'integer)
  173. (defcustom org-ascii-list-margin 0
  174. "Width of margin used for plain lists, in characters.
  175. This margin applies to top level list only, not to its
  176. sub-lists."
  177. :group 'org-export-ascii
  178. :version "26.1"
  179. :package-version '(Org . "8.3")
  180. :type 'integer)
  181. (defcustom org-ascii-inlinetask-width 30
  182. "Width of inline tasks, in number of characters.
  183. This number ignores any margin."
  184. :group 'org-export-ascii
  185. :version "24.4"
  186. :package-version '(Org . "8.0")
  187. :type 'integer)
  188. (defcustom org-ascii-headline-spacing '(1 . 2)
  189. "Number of blank lines inserted around headlines.
  190. This variable can be set to a cons cell. In that case, its car
  191. represents the number of blank lines present before headline
  192. contents whereas its cdr reflects the number of blank lines after
  193. contents.
  194. A nil value replicates the number of blank lines found in the
  195. original Org buffer at the same place."
  196. :group 'org-export-ascii
  197. :version "24.4"
  198. :package-version '(Org . "8.0")
  199. :type '(choice
  200. (const :tag "Replicate original spacing" nil)
  201. (cons :tag "Set a uniform spacing"
  202. (integer :tag "Number of blank lines before contents")
  203. (integer :tag "Number of blank lines after contents"))))
  204. (defcustom org-ascii-indented-line-width 'auto
  205. "Additional indentation width for the first line in a paragraph.
  206. If the value is an integer, indent the first line of each
  207. paragraph by this width, unless it is located at the beginning of
  208. a section, in which case indentation is removed from that line.
  209. If it is the symbol `auto' preserve indentation from original
  210. document."
  211. :group 'org-export-ascii
  212. :version "24.4"
  213. :package-version '(Org . "8.0")
  214. :type '(choice
  215. (integer :tag "Number of white spaces characters")
  216. (const :tag "Preserve original width" auto)))
  217. (defcustom org-ascii-paragraph-spacing 'auto
  218. "Number of white lines between paragraphs.
  219. If the value is an integer, add this number of blank lines
  220. between contiguous paragraphs. If is it the symbol `auto', keep
  221. the same number of blank lines as in the original document."
  222. :group 'org-export-ascii
  223. :version "24.4"
  224. :package-version '(Org . "8.0")
  225. :type '(choice
  226. (integer :tag "Number of blank lines")
  227. (const :tag "Preserve original spacing" auto)))
  228. (defcustom org-ascii-charset 'ascii
  229. "The charset allowed to represent various elements and objects.
  230. Possible values are:
  231. `ascii' Only use plain ASCII characters
  232. `latin1' Include Latin-1 characters
  233. `utf-8' Use all UTF-8 characters"
  234. :group 'org-export-ascii
  235. :version "24.4"
  236. :package-version '(Org . "8.0")
  237. :type '(choice
  238. (const :tag "ASCII" ascii)
  239. (const :tag "Latin-1" latin1)
  240. (const :tag "UTF-8" utf-8)))
  241. (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
  242. (latin1 ?= ?~ ?-)
  243. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  244. "Characters for underlining headings in ASCII export.
  245. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  246. and whose value is a list of characters.
  247. For each supported charset, this variable associates a sequence
  248. of underline characters. In a sequence, the characters will be
  249. used in order for headlines level 1, 2, ... If no character is
  250. available for a given level, the headline won't be underlined."
  251. :group 'org-export-ascii
  252. :version "24.4"
  253. :package-version '(Org . "8.0")
  254. :type '(list
  255. (cons :tag "Underline characters sequence"
  256. (const :tag "ASCII charset" ascii)
  257. (repeat character))
  258. (cons :tag "Underline characters sequence"
  259. (const :tag "Latin-1 charset" latin1)
  260. (repeat character))
  261. (cons :tag "Underline characters sequence"
  262. (const :tag "UTF-8 charset" utf-8)
  263. (repeat character))))
  264. (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
  265. (latin1 ?§ ?¶)
  266. (utf-8 ?◊))
  267. "Bullet characters for headlines converted to lists in ASCII export.
  268. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  269. and whose value is a list of characters.
  270. The first character is used for the first level considered as low
  271. level, and so on. If there are more levels than characters given
  272. here, the list will be repeated.
  273. Note that this variable doesn't affect plain lists
  274. representation."
  275. :group 'org-export-ascii
  276. :version "24.4"
  277. :package-version '(Org . "8.0")
  278. :type '(list
  279. (cons :tag "Bullet characters for low level headlines"
  280. (const :tag "ASCII charset" ascii)
  281. (repeat character))
  282. (cons :tag "Bullet characters for low level headlines"
  283. (const :tag "Latin-1 charset" latin1)
  284. (repeat character))
  285. (cons :tag "Bullet characters for low level headlines"
  286. (const :tag "UTF-8 charset" utf-8)
  287. (repeat character))))
  288. (defcustom org-ascii-links-to-notes t
  289. "Non-nil means convert links to notes before the next headline.
  290. When nil, the link will be exported in place. If the line
  291. becomes long in this way, it will be wrapped."
  292. :group 'org-export-ascii
  293. :version "24.4"
  294. :package-version '(Org . "8.0")
  295. :type 'boolean)
  296. (defcustom org-ascii-table-keep-all-vertical-lines nil
  297. "Non-nil means keep all vertical lines in ASCII tables.
  298. When nil, vertical lines will be removed except for those needed
  299. for column grouping."
  300. :group 'org-export-ascii
  301. :version "24.4"
  302. :package-version '(Org . "8.0")
  303. :type 'boolean)
  304. (defcustom org-ascii-table-widen-columns t
  305. "Non-nil means widen narrowed columns for export.
  306. When nil, narrowed columns will look in ASCII export just like in
  307. Org mode, i.e. with \"=>\" as ellipsis."
  308. :group 'org-export-ascii
  309. :version "24.4"
  310. :package-version '(Org . "8.0")
  311. :type 'boolean)
  312. (defcustom org-ascii-table-use-ascii-art nil
  313. "Non-nil means \"table.el\" tables are turned into ASCII art.
  314. It only makes sense when export charset is `utf-8'. It is nil by
  315. default since it requires \"ascii-art-to-unicode.el\" package,
  316. available through, e.g., GNU ELPA."
  317. :group 'org-export-ascii
  318. :version "24.4"
  319. :package-version '(Org . "8.0")
  320. :type 'boolean)
  321. (defcustom org-ascii-caption-above nil
  322. "When non-nil, place caption string before the element.
  323. Otherwise, place it right after it."
  324. :group 'org-export-ascii
  325. :version "24.4"
  326. :package-version '(Org . "8.0")
  327. :type 'boolean)
  328. (defcustom org-ascii-verbatim-format "`%s'"
  329. "Format string used for verbatim text and inline code."
  330. :group 'org-export-ascii
  331. :version "24.4"
  332. :package-version '(Org . "8.0")
  333. :type 'string)
  334. (defcustom org-ascii-format-drawer-function
  335. (lambda (_name contents _width) contents)
  336. "Function called to format a drawer in ASCII.
  337. The function must accept three parameters:
  338. NAME the drawer name, like \"LOGBOOK\"
  339. CONTENTS the contents of the drawer.
  340. WIDTH the text width within the drawer.
  341. The function should return either the string to be exported or
  342. nil to ignore the drawer.
  343. The default value simply returns the value of CONTENTS."
  344. :group 'org-export-ascii
  345. :version "24.4"
  346. :package-version '(Org . "8.0")
  347. :type 'function)
  348. (defcustom org-ascii-format-inlinetask-function
  349. 'org-ascii-format-inlinetask-default
  350. "Function called to format an inlinetask in ASCII.
  351. The function must accept nine parameters:
  352. TODO the todo keyword, as a string
  353. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  354. PRIORITY the inlinetask priority, as a string
  355. NAME the inlinetask name, as a string.
  356. TAGS the inlinetask tags, as a list of strings.
  357. CONTENTS the contents of the inlinetask, as a string.
  358. WIDTH the width of the inlinetask, as a number.
  359. INLINETASK the inlinetask itself.
  360. INFO the info channel.
  361. The function should return either the string to be exported or
  362. nil to ignore the inline task."
  363. :group 'org-export-ascii
  364. :version "26.1"
  365. :package-version '(Org . "8.3")
  366. :type 'function)
  367. ;;; Internal Functions
  368. ;; Internal functions fall into three categories.
  369. ;; The first one is about text formatting. The core functions are
  370. ;; `org-ascii--current-text-width' and
  371. ;; `org-ascii--current-justification', which determine, respectively,
  372. ;; the current text width allowed to a given element and its expected
  373. ;; justification. Once this information is known,
  374. ;; `org-ascii--fill-string', `org-ascii--justify-lines',
  375. ;; `org-ascii--justify-element' `org-ascii--box-string' and
  376. ;; `org-ascii--indent-string' can operate on a given output string.
  377. ;; In particular, justification happens at the regular (i.e.,
  378. ;; non-greater) element level, which means that when the exporting
  379. ;; process reaches a container (e.g., a center block) content are
  380. ;; already justified.
  381. ;; The second category contains functions handling elements listings,
  382. ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
  383. ;; returns a complete table of contents, `org-ascii--list-listings'
  384. ;; returns a list of referenceable src-block elements, and
  385. ;; `org-ascii--list-tables' does the same for table elements.
  386. ;; The third category includes general helper functions.
  387. ;; `org-ascii--build-title' creates the title for a given headline
  388. ;; or inlinetask element. `org-ascii--build-caption' returns the
  389. ;; caption string associated to a table or a src-block.
  390. ;; `org-ascii--describe-links' creates notes about links for
  391. ;; insertion at the end of a section. It uses
  392. ;; `org-ascii--unique-links' to get the list of links to describe.
  393. ;; Eventually, `org-ascii--translate' translates a string according
  394. ;; to language and charset specification.
  395. (defun org-ascii--fill-string (s text-width info &optional justify)
  396. "Fill a string with specified text-width and return it.
  397. S is the string being filled. TEXT-WIDTH is an integer
  398. specifying maximum length of a line. INFO is the plist used as
  399. a communication channel.
  400. Optional argument JUSTIFY can specify any type of justification
  401. among `left', `center', `right' or `full'. A nil value is
  402. equivalent to `left'. For a justification that doesn't also fill
  403. string, see `org-ascii--justify-lines' and
  404. `org-ascii--justify-block'.
  405. Return nil if S isn't a string."
  406. (when (stringp s)
  407. (let ((double-space-p sentence-end-double-space))
  408. (with-temp-buffer
  409. (let ((fill-column text-width)
  410. (use-hard-newlines t)
  411. (sentence-end-double-space double-space-p))
  412. (insert (if (plist-get info :preserve-breaks)
  413. (replace-regexp-in-string "\n" hard-newline s)
  414. s))
  415. (fill-region (point-min) (point-max) justify))
  416. (buffer-string)))))
  417. (defun org-ascii--justify-lines (s text-width how)
  418. "Justify all lines in string S.
  419. TEXT-WIDTH is an integer specifying maximum length of a line.
  420. HOW determines the type of justification: it can be `left',
  421. `right', `full' or `center'."
  422. (with-temp-buffer
  423. (insert s)
  424. (goto-char (point-min))
  425. (let ((fill-column text-width)
  426. ;; Ensure that `indent-tabs-mode' is nil so that indentation
  427. ;; will always be achieved using spaces rather than tabs.
  428. (indent-tabs-mode nil)
  429. ;; Disable `adaptive-fill-mode' so it doesn't prevent
  430. ;; filling lines matching `adaptive-fill-regexp'.
  431. (adaptive-fill-mode nil))
  432. (while (< (point) (point-max))
  433. (justify-current-line how)
  434. (forward-line)))
  435. (buffer-string)))
  436. (defun org-ascii--justify-element (contents element info)
  437. "Justify CONTENTS of ELEMENT.
  438. INFO is a plist used as a communication channel. Justification
  439. is done according to the type of element. More accurately,
  440. paragraphs are filled and other elements are justified as blocks,
  441. that is according to the widest non blank line in CONTENTS."
  442. (if (not (org-string-nw-p contents)) contents
  443. (let ((text-width (org-ascii--current-text-width element info))
  444. (how (org-ascii--current-justification element)))
  445. (cond
  446. ((eq (org-element-type element) 'paragraph)
  447. ;; Paragraphs are treated specially as they need to be filled.
  448. (org-ascii--fill-string contents text-width info how))
  449. ((eq how 'left) contents)
  450. (t (with-temp-buffer
  451. (insert contents)
  452. (goto-char (point-min))
  453. (catch 'exit
  454. (let ((max-width 0))
  455. ;; Compute maximum width. Bail out if it is greater
  456. ;; than page width, since no justification is
  457. ;; possible.
  458. (save-excursion
  459. (while (not (eobp))
  460. (unless (looking-at-p "[ \t]*$")
  461. (end-of-line)
  462. (let ((column (current-column)))
  463. (cond
  464. ((>= column text-width) (throw 'exit contents))
  465. ((> column max-width) (setq max-width column)))))
  466. (forward-line)))
  467. ;; Justify every line according to TEXT-WIDTH and
  468. ;; MAX-WIDTH.
  469. (let ((offset (/ (- text-width max-width)
  470. (if (eq how 'right) 1 2))))
  471. (if (zerop offset) (throw 'exit contents)
  472. (while (not (eobp))
  473. (unless (looking-at-p "[ \t]*$")
  474. (indent-to-column offset))
  475. (forward-line)))))
  476. (buffer-string))))))))
  477. (defun org-ascii--indent-string (s width)
  478. "Indent string S by WIDTH white spaces.
  479. Empty lines are not indented."
  480. (when (stringp s)
  481. (replace-regexp-in-string
  482. "\\(^\\)[ \t]*\\S-" (make-string width ?\s) s nil nil 1)))
  483. (defun org-ascii--box-string (s info)
  484. "Return string S with a partial box to its left.
  485. INFO is a plist used as a communication channel."
  486. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  487. (format (if utf8p "┌────\n%s\n└────" ",----\n%s\n`----")
  488. (replace-regexp-in-string
  489. "^" (if utf8p "│ " "| ")
  490. ;; Remove last newline character.
  491. (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
  492. (defun org-ascii--current-text-width (element info)
  493. "Return maximum text width for ELEMENT's contents.
  494. INFO is a plist used as a communication channel."
  495. (pcase (org-element-type element)
  496. ;; Elements with an absolute width: `headline' and `inlinetask'.
  497. (`inlinetask (plist-get info :ascii-inlinetask-width))
  498. (`headline
  499. (- (plist-get info :ascii-text-width)
  500. (let ((low-level-rank (org-export-low-level-p element info)))
  501. (if low-level-rank (* low-level-rank 2)
  502. (plist-get info :ascii-global-margin)))))
  503. ;; Elements with a relative width: store maximum text width in
  504. ;; TOTAL-WIDTH.
  505. (_
  506. (let* ((genealogy (org-element-lineage element nil t))
  507. ;; Total width is determined by the presence, or not, of an
  508. ;; inline task among ELEMENT parents.
  509. (total-width
  510. (if (cl-some (lambda (parent)
  511. (eq (org-element-type parent) 'inlinetask))
  512. genealogy)
  513. (plist-get info :ascii-inlinetask-width)
  514. ;; No inlinetask: Remove global margin from text width.
  515. (- (plist-get info :ascii-text-width)
  516. (plist-get info :ascii-global-margin)
  517. (let ((parent (org-export-get-parent-headline element)))
  518. ;; Inner margin doesn't apply to text before first
  519. ;; headline.
  520. (if (not parent) 0
  521. (let ((low-level-rank
  522. (org-export-low-level-p parent info)))
  523. ;; Inner margin doesn't apply to contents of
  524. ;; low level headlines, since they've got their
  525. ;; own indentation mechanism.
  526. (if low-level-rank (* low-level-rank 2)
  527. (plist-get info :ascii-inner-margin)))))))))
  528. (- total-width
  529. ;; Each `quote-block' and `verse-block' above narrows text
  530. ;; width by twice the standard margin size.
  531. (+ (* (cl-count-if (lambda (parent)
  532. (memq (org-element-type parent)
  533. '(quote-block verse-block)))
  534. genealogy)
  535. 2
  536. (plist-get info :ascii-quote-margin))
  537. ;; Apply list margin once per "top-level" plain-list
  538. ;; containing current line
  539. (* (cl-count-if
  540. (lambda (e)
  541. (and (eq (org-element-type e) 'plain-list)
  542. (not (eq (org-element-type (org-export-get-parent e))
  543. 'item))))
  544. genealogy)
  545. (plist-get info :ascii-list-margin))
  546. ;; Compute indentation offset due to current list. It is
  547. ;; `org-ascii-quote-margin' per descriptive item in the
  548. ;; genealogy, bullet's length otherwise.
  549. (let ((indentation 0))
  550. (dolist (e genealogy)
  551. (cond
  552. ((not (eq 'item (org-element-type e))))
  553. ((eq (org-element-property :type (org-export-get-parent e))
  554. 'descriptive)
  555. (cl-incf indentation org-ascii-quote-margin))
  556. (t
  557. (cl-incf indentation
  558. (+ (string-width
  559. (or (org-ascii--checkbox e info) ""))
  560. (string-width
  561. (org-element-property :bullet e)))))))
  562. indentation)))))))
  563. (defun org-ascii--current-justification (element)
  564. "Return expected justification for ELEMENT's contents.
  565. Return value is a symbol among `left', `center', `right' and
  566. `full'."
  567. (let (justification)
  568. (while (and (not justification)
  569. (setq element (org-element-property :parent element)))
  570. (pcase (org-element-type element)
  571. (`center-block (setq justification 'center))
  572. (`special-block
  573. (let ((name (org-element-property :type element)))
  574. (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
  575. ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
  576. (or justification 'left)))
  577. (defun org-ascii--build-title
  578. (element info text-width &optional underline notags toc)
  579. "Format ELEMENT title and return it.
  580. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  581. a plist used as a communication channel. TEXT-WIDTH is an
  582. integer representing the maximum length of a line.
  583. When optional argument UNDERLINE is non-nil, underline title,
  584. without the tags, according to `org-ascii-underline'
  585. specifications.
  586. If optional argument NOTAGS is non-nil, no tags will be added to
  587. the title.
  588. When optional argument TOC is non-nil, use optional title if
  589. possible. It doesn't apply to `inlinetask' elements."
  590. (let* ((headlinep (eq (org-element-type element) 'headline))
  591. (numbers
  592. ;; Numbering is specific to headlines.
  593. (and headlinep
  594. (org-export-numbered-headline-p element info)
  595. (let ((numbering (org-export-get-headline-number element info)))
  596. (if toc (format "%d. " (org-last numbering))
  597. (concat (mapconcat #'number-to-string numbering ".")
  598. " ")))))
  599. (text
  600. (org-trim
  601. (org-export-data
  602. (if (and toc headlinep) (org-export-get-alt-title element info)
  603. (org-element-property :title element))
  604. info)))
  605. (todo
  606. (and (plist-get info :with-todo-keywords)
  607. (let ((todo (org-element-property :todo-keyword element)))
  608. (and todo (concat (org-export-data todo info) " ")))))
  609. (tags (and (not notags)
  610. (plist-get info :with-tags)
  611. (let ((tag-list (org-export-get-tags element info)))
  612. (and tag-list
  613. (org-make-tag-string tag-list)))))
  614. (priority
  615. (and (plist-get info :with-priority)
  616. (let ((char (org-element-property :priority element)))
  617. (and char (format "(#%c) " char)))))
  618. (first-part (concat numbers todo priority text)))
  619. (concat
  620. first-part
  621. ;; Align tags, if any.
  622. (when tags
  623. (format
  624. (format " %%%ds"
  625. (max (- text-width (1+ (string-width first-part)))
  626. (string-width tags)))
  627. tags))
  628. ;; Maybe underline text, if ELEMENT type is `headline' and an
  629. ;; underline character has been defined.
  630. (when (and underline headlinep)
  631. (let ((under-char
  632. (nth (1- (org-export-get-relative-level element info))
  633. (cdr (assq (plist-get info :ascii-charset)
  634. (plist-get info :ascii-underline))))))
  635. (and under-char
  636. (concat "\n"
  637. (make-string (/ (string-width first-part)
  638. (char-width under-char))
  639. under-char))))))))
  640. (defun org-ascii--has-caption-p (element _info)
  641. "Non-nil when ELEMENT has a caption affiliated keyword.
  642. INFO is a plist used as a communication channel. This function
  643. is meant to be used as a predicate for `org-export-get-ordinal'."
  644. (org-element-property :caption element))
  645. (defun org-ascii--build-caption (element info)
  646. "Return caption string for ELEMENT, if applicable.
  647. INFO is a plist used as a communication channel.
  648. The caption string contains the sequence number of ELEMENT along
  649. with its real caption. Return nil when ELEMENT has no affiliated
  650. caption keyword."
  651. (let ((caption (org-export-get-caption element)))
  652. (when caption
  653. ;; Get sequence number of current src-block among every
  654. ;; src-block with a caption.
  655. (let ((reference
  656. (org-export-get-ordinal
  657. element info nil 'org-ascii--has-caption-p))
  658. (title-fmt (org-ascii--translate
  659. (pcase (org-element-type element)
  660. (`table "Table %d:")
  661. (`src-block "Listing %d:"))
  662. info)))
  663. (org-ascii--fill-string
  664. (concat (format title-fmt reference)
  665. " "
  666. (org-export-data caption info))
  667. (org-ascii--current-text-width element info) info)))))
  668. (defun org-ascii--build-toc (info &optional n keyword scope)
  669. "Return a table of contents.
  670. INFO is a plist used as a communication channel.
  671. Optional argument N, when non-nil, is an integer specifying the
  672. depth of the table.
  673. Optional argument KEYWORD specifies the TOC keyword, if any, from
  674. which the table of contents generation has been initiated.
  675. When optional argument SCOPE is non-nil, build a table of
  676. contents according to the specified scope."
  677. (concat
  678. (unless scope
  679. (let ((title (org-ascii--translate "Table of Contents" info)))
  680. (concat title "\n"
  681. (make-string
  682. (string-width title)
  683. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  684. "\n\n")))
  685. (let ((text-width
  686. (if keyword (org-ascii--current-text-width keyword info)
  687. (- (plist-get info :ascii-text-width)
  688. (plist-get info :ascii-global-margin)))))
  689. (mapconcat
  690. (lambda (headline)
  691. (let* ((level (org-export-get-relative-level headline info))
  692. (indent (* (1- level) 3)))
  693. (concat
  694. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  695. (org-ascii--build-title
  696. headline info (- text-width indent) nil
  697. (or (not (plist-get info :with-tags))
  698. (eq (plist-get info :with-tags) 'not-in-toc))
  699. 'toc))))
  700. (org-export-collect-headlines info n scope) "\n"))))
  701. (defun org-ascii--list-listings (keyword info)
  702. "Return a list of listings.
  703. KEYWORD is the keyword that initiated the list of listings
  704. generation. INFO is a plist used as a communication channel."
  705. (let ((title (org-ascii--translate "List of Listings" info)))
  706. (concat
  707. title "\n"
  708. (make-string (string-width title)
  709. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  710. "\n\n"
  711. (let ((text-width
  712. (if keyword (org-ascii--current-text-width keyword info)
  713. (- (plist-get info :ascii-text-width)
  714. (plist-get info :ascii-global-margin))))
  715. ;; Use a counter instead of retrieving ordinal of each
  716. ;; src-block.
  717. (count 0))
  718. (mapconcat
  719. (lambda (src-block)
  720. ;; Store initial text so its length can be computed. This is
  721. ;; used to properly align caption right to it in case of
  722. ;; filling (like contents of a description list item).
  723. (let* ((initial-text
  724. (format (org-ascii--translate "Listing %d:" info)
  725. (cl-incf count)))
  726. (initial-width (string-width initial-text)))
  727. (concat
  728. initial-text " "
  729. (org-trim
  730. (org-ascii--indent-string
  731. (org-ascii--fill-string
  732. ;; Use short name in priority, if available.
  733. (let ((caption (or (org-export-get-caption src-block t)
  734. (org-export-get-caption src-block))))
  735. (org-export-data caption info))
  736. (- text-width initial-width) info)
  737. initial-width)))))
  738. (org-export-collect-listings info) "\n")))))
  739. (defun org-ascii--list-tables (keyword info)
  740. "Return a list of tables.
  741. KEYWORD is the keyword that initiated the list of tables
  742. generation. INFO is a plist used as a communication channel."
  743. (let ((title (org-ascii--translate "List of Tables" info)))
  744. (concat
  745. title "\n"
  746. (make-string (string-width title)
  747. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  748. "\n\n"
  749. (let ((text-width
  750. (if keyword (org-ascii--current-text-width keyword info)
  751. (- (plist-get info :ascii-text-width)
  752. (plist-get info :ascii-global-margin))))
  753. ;; Use a counter instead of retrieving ordinal of each
  754. ;; src-block.
  755. (count 0))
  756. (mapconcat
  757. (lambda (table)
  758. ;; Store initial text so its length can be computed. This is
  759. ;; used to properly align caption right to it in case of
  760. ;; filling (like contents of a description list item).
  761. (let* ((initial-text
  762. (format (org-ascii--translate "Table %d:" info)
  763. (cl-incf count)))
  764. (initial-width (string-width initial-text)))
  765. (concat
  766. initial-text " "
  767. (org-trim
  768. (org-ascii--indent-string
  769. (org-ascii--fill-string
  770. ;; Use short name in priority, if available.
  771. (let ((caption (or (org-export-get-caption table t)
  772. (org-export-get-caption table))))
  773. (org-export-data caption info))
  774. (- text-width initial-width) info)
  775. initial-width)))))
  776. (org-export-collect-tables info) "\n")))))
  777. (defun org-ascii--unique-links (element info)
  778. "Return a list of unique link references in ELEMENT.
  779. ELEMENT is either a headline element or a section element. INFO
  780. is a plist used as a communication channel."
  781. (let* (seen
  782. (unique-link-p
  783. ;; Return LINK if it wasn't referenced so far, or nil.
  784. ;; Update SEEN links along the way.
  785. (lambda (link)
  786. (let ((footprint
  787. ;; Normalize description in footprints.
  788. (cons (org-element-property :raw-link link)
  789. (let ((contents (org-element-contents link)))
  790. (and contents
  791. (replace-regexp-in-string
  792. "[ \r\t\n]+" " "
  793. (org-trim
  794. (org-element-interpret-data contents))))))))
  795. ;; Ignore LINK if it hasn't been translated already. It
  796. ;; can happen if it is located in an affiliated keyword
  797. ;; that was ignored.
  798. (when (and (org-string-nw-p
  799. (gethash link (plist-get info :exported-data)))
  800. (not (member footprint seen)))
  801. (push footprint seen) link)))))
  802. (org-element-map (if (eq (org-element-type element) 'section)
  803. element
  804. ;; In a headline, only retrieve links in title
  805. ;; and relative section, not in children.
  806. (list (org-element-property :title element)
  807. (car (org-element-contents element))))
  808. 'link unique-link-p info nil 'headline t)))
  809. (defun org-ascii--describe-datum (datum info)
  810. "Describe DATUM object or element.
  811. If DATUM is a string, consider it to be a file name, per
  812. `org-export-resolve-id-link'. INFO is the communication channel,
  813. as a plist."
  814. (pcase (org-element-type datum)
  815. (`plain-text (format "See file %s" datum)) ;External file
  816. (`headline
  817. (format (org-ascii--translate "See section %s" info)
  818. (if (org-export-numbered-headline-p datum info)
  819. (mapconcat #'number-to-string
  820. (org-export-get-headline-number datum info)
  821. ".")
  822. (org-export-data (org-element-property :title datum) info))))
  823. (_
  824. (let ((number (org-export-get-ordinal
  825. datum info nil #'org-ascii--has-caption-p))
  826. ;; If destination is a target, make sure we can name the
  827. ;; container it refers to.
  828. (enumerable
  829. (org-element-lineage datum
  830. '(headline paragraph src-block table) t)))
  831. (pcase (org-element-type enumerable)
  832. (`headline
  833. (format (org-ascii--translate "See section %s" info)
  834. (if (org-export-numbered-headline-p enumerable info)
  835. (mapconcat #'number-to-string number ".")
  836. (org-export-data
  837. (org-element-property :title enumerable) info))))
  838. ((guard (not number))
  839. (org-ascii--translate "Unknown reference" info))
  840. (`paragraph
  841. (format (org-ascii--translate "See figure %s" info) number))
  842. (`src-block
  843. (format (org-ascii--translate "See listing %s" info) number))
  844. (`table
  845. (format (org-ascii--translate "See table %s" info) number))
  846. (_ (org-ascii--translate "Unknown reference" info)))))))
  847. (defun org-ascii--describe-links (links width info)
  848. "Return a string describing a list of links.
  849. LINKS is a list of link type objects, as returned by
  850. `org-ascii--unique-links'. WIDTH is the text width allowed for
  851. the output string. INFO is a plist used as a communication
  852. channel."
  853. (mapconcat
  854. (lambda (link)
  855. (let* ((type (org-element-property :type link))
  856. (description (org-element-contents link))
  857. (anchor (org-export-data
  858. (or description (org-element-property :raw-link link))
  859. info)))
  860. (cond
  861. ((member type '("coderef" "radio")) nil)
  862. ((member type '("custom-id" "fuzzy" "id"))
  863. ;; Only links with a description need an entry. Other are
  864. ;; already handled in `org-ascii-link'.
  865. (when description
  866. (let ((dest (if (equal type "fuzzy")
  867. (org-export-resolve-fuzzy-link link info)
  868. ;; Ignore broken links. On broken link,
  869. ;; `org-export-resolve-id-link' will throw an
  870. ;; error and we will return nil.
  871. (condition-case nil
  872. (org-export-resolve-id-link link info)
  873. (org-link-broken nil)))))
  874. (when dest
  875. (concat
  876. (org-ascii--fill-string
  877. (format "[%s] %s" anchor (org-ascii--describe-datum dest info))
  878. width info)
  879. "\n\n")))))
  880. ;; Do not add a link that cannot be resolved and doesn't have
  881. ;; any description: destination is already visible in the
  882. ;; paragraph.
  883. ((not (org-element-contents link)) nil)
  884. ;; Do not add a link already handled by custom export
  885. ;; functions.
  886. ((org-export-custom-protocol-maybe link anchor 'ascii info) nil)
  887. (t
  888. (concat
  889. (org-ascii--fill-string
  890. (format "[%s] <%s>" anchor (org-element-property :raw-link link))
  891. width info)
  892. "\n\n")))))
  893. links ""))
  894. (defun org-ascii--checkbox (item info)
  895. "Return checkbox string for ITEM or nil.
  896. INFO is a plist used as a communication channel."
  897. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  898. (pcase (org-element-property :checkbox item)
  899. (`on (if utf8p "☑ " "[X] "))
  900. (`off (if utf8p "☐ " "[ ] "))
  901. (`trans (if utf8p "☒ " "[-] ")))))
  902. ;;; Template
  903. (defun org-ascii-template--document-title (info)
  904. "Return document title, as a string.
  905. INFO is a plist used as a communication channel."
  906. (let* ((text-width (plist-get info :ascii-text-width))
  907. ;; Links in the title will not be resolved later, so we make
  908. ;; sure their path is located right after them.
  909. (info (org-combine-plists info '(:ascii-links-to-notes nil)))
  910. (with-title (plist-get info :with-title))
  911. (title (org-export-data
  912. (when with-title (plist-get info :title)) info))
  913. (subtitle (org-export-data
  914. (when with-title (plist-get info :subtitle)) info))
  915. (author (and (plist-get info :with-author)
  916. (let ((auth (plist-get info :author)))
  917. (and auth (org-export-data auth info)))))
  918. (email (and (plist-get info :with-email)
  919. (org-export-data (plist-get info :email) info)))
  920. (date (and (plist-get info :with-date)
  921. (org-export-data (org-export-get-date info) info))))
  922. ;; There are two types of title blocks depending on the presence
  923. ;; of a title to display.
  924. (if (string= title "")
  925. ;; Title block without a title. DATE is positioned at the top
  926. ;; right of the document, AUTHOR to the top left and EMAIL
  927. ;; just below.
  928. (cond
  929. ((and (org-string-nw-p date) (org-string-nw-p author))
  930. (concat
  931. author
  932. (make-string (- text-width (string-width date) (string-width author))
  933. ?\s)
  934. date
  935. (when (org-string-nw-p email) (concat "\n" email))
  936. "\n\n\n"))
  937. ((and (org-string-nw-p date) (org-string-nw-p email))
  938. (concat
  939. email
  940. (make-string (- text-width (string-width date) (string-width email))
  941. ?\s)
  942. date "\n\n\n"))
  943. ((org-string-nw-p date)
  944. (concat
  945. (org-ascii--justify-lines date text-width 'right)
  946. "\n\n\n"))
  947. ((and (org-string-nw-p author) (org-string-nw-p email))
  948. (concat author "\n" email "\n\n\n"))
  949. ((org-string-nw-p author) (concat author "\n\n\n"))
  950. ((org-string-nw-p email) (concat email "\n\n\n")))
  951. ;; Title block with a title. Document's TITLE, along with the
  952. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  953. ;; centered. Date is just below, also centered.
  954. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  955. ;; Format TITLE. It may be filled if it is too wide,
  956. ;; that is wider than the two thirds of the total width.
  957. (title-len (min (apply #'max
  958. (mapcar #'string-width
  959. (org-split-string
  960. (concat title "\n" subtitle) "\n")))
  961. (/ (* 2 text-width) 3)))
  962. (formatted-title (org-ascii--fill-string title title-len info))
  963. (formatted-subtitle (when (org-string-nw-p subtitle)
  964. (org-ascii--fill-string subtitle title-len info)))
  965. (line
  966. (make-string
  967. (min (+ (max title-len
  968. (string-width (or author ""))
  969. (string-width (or email "")))
  970. 2)
  971. text-width) (if utf8p ?━ ?_))))
  972. (org-ascii--justify-lines
  973. (concat line "\n"
  974. (unless utf8p "\n")
  975. (upcase formatted-title)
  976. (and formatted-subtitle (concat "\n" formatted-subtitle))
  977. (cond
  978. ((and (org-string-nw-p author) (org-string-nw-p email))
  979. (concat "\n\n" author "\n" email))
  980. ((org-string-nw-p author) (concat "\n\n" author))
  981. ((org-string-nw-p email) (concat "\n\n" email)))
  982. "\n" line
  983. (when (org-string-nw-p date) (concat "\n\n\n" date))
  984. "\n\n\n") text-width 'center)))))
  985. (defun org-ascii-inner-template (contents info)
  986. "Return complete document string after ASCII conversion.
  987. CONTENTS is the transcoded contents string. INFO is a plist
  988. holding export options."
  989. (org-element-normalize-string
  990. (let ((global-margin (plist-get info :ascii-global-margin)))
  991. (org-ascii--indent-string
  992. (concat
  993. ;; 1. Document's body.
  994. contents
  995. ;; 2. Footnote definitions.
  996. (let ((definitions (org-export-collect-footnote-definitions info))
  997. ;; Insert full links right inside the footnote definition
  998. ;; as they have no chance to be inserted later.
  999. (info (org-combine-plists info '(:ascii-links-to-notes nil))))
  1000. (when definitions
  1001. (concat
  1002. "\n\n\n"
  1003. (let ((title (org-ascii--translate "Footnotes" info)))
  1004. (concat
  1005. title "\n"
  1006. (make-string
  1007. (string-width title)
  1008. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  1009. "\n\n"
  1010. (let ((text-width (- (plist-get info :ascii-text-width)
  1011. global-margin)))
  1012. (mapconcat
  1013. (lambda (ref)
  1014. (let ((id (format "[%s] " (car ref))))
  1015. ;; Distinguish between inline definitions and
  1016. ;; full-fledged definitions.
  1017. (org-trim
  1018. (let ((def (nth 2 ref)))
  1019. (if (org-element-map def org-element-all-elements
  1020. #'identity info 'first-match)
  1021. ;; Full-fledged definition: footnote ID is
  1022. ;; inserted inside the first parsed
  1023. ;; paragraph (FIRST), if any, to be sure
  1024. ;; filling will take it into consideration.
  1025. (let ((first (car (org-element-contents def))))
  1026. (if (not (eq (org-element-type first) 'paragraph))
  1027. (concat id "\n" (org-export-data def info))
  1028. (push id (nthcdr 2 first))
  1029. (org-export-data def info)))
  1030. ;; Fill paragraph once footnote ID is inserted
  1031. ;; in order to have a correct length for first
  1032. ;; line.
  1033. (org-ascii--fill-string
  1034. (concat id (org-export-data def info))
  1035. text-width info))))))
  1036. definitions "\n\n"))))))
  1037. global-margin))))
  1038. (defun org-ascii-template (contents info)
  1039. "Return complete document string after ASCII conversion.
  1040. CONTENTS is the transcoded contents string. INFO is a plist
  1041. holding export options."
  1042. (let ((global-margin (plist-get info :ascii-global-margin)))
  1043. (concat
  1044. ;; Build title block.
  1045. (org-ascii--indent-string
  1046. (concat (org-ascii-template--document-title info)
  1047. ;; 2. Table of contents.
  1048. (let ((depth (plist-get info :with-toc)))
  1049. (when depth
  1050. (concat
  1051. (org-ascii--build-toc info (and (wholenump depth) depth))
  1052. "\n\n\n"))))
  1053. global-margin)
  1054. ;; Document's body.
  1055. contents
  1056. ;; Creator. Justify it to the bottom right.
  1057. (and (plist-get info :with-creator)
  1058. (org-ascii--indent-string
  1059. (let ((text-width
  1060. (- (plist-get info :ascii-text-width) global-margin)))
  1061. (concat
  1062. "\n\n\n"
  1063. (org-ascii--fill-string
  1064. (plist-get info :creator) text-width info 'right)))
  1065. global-margin)))))
  1066. (defun org-ascii--translate (s info)
  1067. "Translate string S according to specified language and charset.
  1068. INFO is a plist used as a communication channel."
  1069. (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
  1070. (org-export-translate s charset info)))
  1071. ;;; Transcode Functions
  1072. ;;;; Bold
  1073. (defun org-ascii-bold (_bold contents _info)
  1074. "Transcode BOLD from Org to ASCII.
  1075. CONTENTS is the text with bold markup. INFO is a plist holding
  1076. contextual information."
  1077. (format "*%s*" contents))
  1078. ;;;; Center Block
  1079. (defun org-ascii-center-block (_center-block contents _info)
  1080. "Transcode a CENTER-BLOCK element from Org to ASCII.
  1081. CONTENTS holds the contents of the block. INFO is a plist
  1082. holding contextual information."
  1083. ;; Center has already been taken care of at a lower level, so
  1084. ;; there's nothing left to do.
  1085. contents)
  1086. ;;;; Clock
  1087. (defun org-ascii-clock (clock _contents info)
  1088. "Transcode a CLOCK object from Org to ASCII.
  1089. CONTENTS is nil. INFO is a plist holding contextual
  1090. information."
  1091. (org-ascii--justify-element
  1092. (concat org-clock-string " "
  1093. (org-timestamp-translate (org-element-property :value clock))
  1094. (let ((time (org-element-property :duration clock)))
  1095. (and time
  1096. (concat " => "
  1097. (apply 'format
  1098. "%2s:%02s"
  1099. (org-split-string time ":"))))))
  1100. clock info))
  1101. ;;;; Code
  1102. (defun org-ascii-code (code _contents info)
  1103. "Return a CODE object from Org to ASCII.
  1104. CONTENTS is nil. INFO is a plist holding contextual
  1105. information."
  1106. (format (plist-get info :ascii-verbatim-format)
  1107. (org-element-property :value code)))
  1108. ;;;; Drawer
  1109. (defun org-ascii-drawer (drawer contents info)
  1110. "Transcode a DRAWER element from Org to ASCII.
  1111. CONTENTS holds the contents of the block. INFO is a plist
  1112. holding contextual information."
  1113. (let ((name (org-element-property :drawer-name drawer))
  1114. (width (org-ascii--current-text-width drawer info)))
  1115. (funcall (plist-get info :ascii-format-drawer-function)
  1116. name contents width)))
  1117. ;;;; Dynamic Block
  1118. (defun org-ascii-dynamic-block (_dynamic-block contents _info)
  1119. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  1120. CONTENTS holds the contents of the block. INFO is a plist
  1121. holding contextual information."
  1122. contents)
  1123. ;;;; Entity
  1124. (defun org-ascii-entity (entity _contents info)
  1125. "Transcode an ENTITY object from Org to ASCII.
  1126. CONTENTS are the definition itself. INFO is a plist holding
  1127. contextual information."
  1128. (org-element-property
  1129. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  1130. entity))
  1131. ;;;; Example Block
  1132. (defun org-ascii-example-block (example-block _contents info)
  1133. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  1134. CONTENTS is nil. INFO is a plist holding contextual information."
  1135. (org-ascii--justify-element
  1136. (org-ascii--box-string
  1137. (org-export-format-code-default example-block info) info)
  1138. example-block info))
  1139. ;;;; Export Snippet
  1140. (defun org-ascii-export-snippet (export-snippet _contents _info)
  1141. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  1142. CONTENTS is nil. INFO is a plist holding contextual information."
  1143. (when (eq (org-export-snippet-backend export-snippet) 'ascii)
  1144. (org-element-property :value export-snippet)))
  1145. ;;;; Export Block
  1146. (defun org-ascii-export-block (export-block _contents info)
  1147. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  1148. CONTENTS is nil. INFO is a plist holding contextual information."
  1149. (when (string= (org-element-property :type export-block) "ASCII")
  1150. (org-ascii--justify-element
  1151. (org-element-property :value export-block) export-block info)))
  1152. ;;;; Fixed Width
  1153. (defun org-ascii-fixed-width (fixed-width _contents info)
  1154. "Transcode a FIXED-WIDTH element from Org to ASCII.
  1155. CONTENTS is nil. INFO is a plist holding contextual information."
  1156. (org-ascii--justify-element
  1157. (org-ascii--box-string
  1158. (org-remove-indentation
  1159. (org-element-property :value fixed-width))
  1160. info)
  1161. fixed-width info))
  1162. ;;;; Footnote Definition
  1163. ;; Footnote Definitions are ignored. They are compiled at the end of
  1164. ;; the document, by `org-ascii-inner-template'.
  1165. ;;;; Footnote Reference
  1166. (defun org-ascii-footnote-reference (footnote-reference _contents info)
  1167. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  1168. CONTENTS is nil. INFO is a plist holding contextual information."
  1169. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  1170. ;;;; Headline
  1171. (defun org-ascii-headline (headline contents info)
  1172. "Transcode a HEADLINE element from Org to ASCII.
  1173. CONTENTS holds the contents of the headline. INFO is a plist
  1174. holding contextual information."
  1175. ;; Don't export footnote section, which will be handled at the end
  1176. ;; of the template.
  1177. (unless (org-element-property :footnote-section-p headline)
  1178. (let* ((low-level (org-export-low-level-p headline info))
  1179. (width (org-ascii--current-text-width headline info))
  1180. ;; Export title early so that any link in it can be
  1181. ;; exported and seen in `org-ascii--unique-links'.
  1182. (title (org-ascii--build-title headline info width (not low-level)))
  1183. ;; Blank lines between headline and its contents.
  1184. ;; `org-ascii-headline-spacing', when set, overwrites
  1185. ;; original buffer's spacing.
  1186. (pre-blanks
  1187. (make-string (or (car (plist-get info :ascii-headline-spacing))
  1188. (org-element-property :pre-blank headline)
  1189. 0)
  1190. ?\n))
  1191. (links (and (plist-get info :ascii-links-to-notes)
  1192. (org-ascii--describe-links
  1193. (org-ascii--unique-links headline info) width info)))
  1194. ;; Re-build contents, inserting section links at the right
  1195. ;; place. The cost is low since build results are cached.
  1196. (body
  1197. (if (not (org-string-nw-p links)) contents
  1198. (let* ((contents (org-element-contents headline))
  1199. (section (let ((first (car contents)))
  1200. (and (eq (org-element-type first) 'section)
  1201. first))))
  1202. (concat (and section
  1203. (concat (org-element-normalize-string
  1204. (org-export-data section info))
  1205. "\n\n"))
  1206. links
  1207. (mapconcat (lambda (e) (org-export-data e info))
  1208. (if section (cdr contents) contents)
  1209. ""))))))
  1210. ;; Deep subtree: export it as a list item.
  1211. (if low-level
  1212. (let* ((bullets (cdr (assq (plist-get info :ascii-charset)
  1213. (plist-get info :ascii-bullets))))
  1214. (bullet
  1215. (format "%c "
  1216. (nth (mod (1- low-level) (length bullets)) bullets))))
  1217. (concat bullet title "\n" pre-blanks
  1218. ;; Contents, indented by length of bullet.
  1219. (org-ascii--indent-string body (length bullet))))
  1220. ;; Else: Standard headline.
  1221. (concat title "\n" pre-blanks body)))))
  1222. ;;;; Horizontal Rule
  1223. (defun org-ascii-horizontal-rule (horizontal-rule _contents info)
  1224. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  1225. CONTENTS is nil. INFO is a plist holding contextual
  1226. information."
  1227. (let ((text-width (org-ascii--current-text-width horizontal-rule info))
  1228. (spec-width
  1229. (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  1230. (org-ascii--justify-lines
  1231. (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
  1232. (string-to-number spec-width)
  1233. text-width)
  1234. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
  1235. text-width 'center)))
  1236. ;;;; Inline Src Block
  1237. (defun org-ascii-inline-src-block (inline-src-block _contents info)
  1238. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  1239. CONTENTS holds the contents of the item. INFO is a plist holding
  1240. contextual information."
  1241. (format (plist-get info :ascii-verbatim-format)
  1242. (org-element-property :value inline-src-block)))
  1243. ;;;; Inlinetask
  1244. (defun org-ascii-format-inlinetask-default
  1245. (_todo _type _priority _name _tags contents width inlinetask info)
  1246. "Format an inline task element for ASCII export.
  1247. See `org-ascii-format-inlinetask-function' for a description
  1248. of the parameters."
  1249. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1250. (width (or width (plist-get info :ascii-inlinetask-width))))
  1251. (org-ascii--indent-string
  1252. (concat
  1253. ;; Top line, with an additional blank line if not in UTF-8.
  1254. (make-string width (if utf8p ?━ ?_)) "\n"
  1255. (unless utf8p (concat (make-string width ? ) "\n"))
  1256. ;; Add title. Fill it if wider than inlinetask.
  1257. (let ((title (org-ascii--build-title inlinetask info width)))
  1258. (if (<= (string-width title) width) title
  1259. (org-ascii--fill-string title width info)))
  1260. "\n"
  1261. ;; If CONTENTS is not empty, insert it along with
  1262. ;; a separator.
  1263. (when (org-string-nw-p contents)
  1264. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1265. ;; Bottom line.
  1266. (make-string width (if utf8p ?━ ?_)))
  1267. ;; Flush the inlinetask to the right.
  1268. (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
  1269. (if (not (org-export-get-parent-headline inlinetask)) 0
  1270. (plist-get info :ascii-inner-margin))
  1271. (org-ascii--current-text-width inlinetask info)))))
  1272. (defun org-ascii-inlinetask (inlinetask contents info)
  1273. "Transcode an INLINETASK element from Org to ASCII.
  1274. CONTENTS holds the contents of the block. INFO is a plist
  1275. holding contextual information."
  1276. (let ((width (org-ascii--current-text-width inlinetask info)))
  1277. (funcall (plist-get info :ascii-format-inlinetask-function)
  1278. ;; todo.
  1279. (and (plist-get info :with-todo-keywords)
  1280. (let ((todo (org-element-property
  1281. :todo-keyword inlinetask)))
  1282. (and todo (org-export-data todo info))))
  1283. ;; todo-type
  1284. (org-element-property :todo-type inlinetask)
  1285. ;; priority
  1286. (and (plist-get info :with-priority)
  1287. (org-element-property :priority inlinetask))
  1288. ;; title
  1289. (org-export-data (org-element-property :title inlinetask) info)
  1290. ;; tags
  1291. (and (plist-get info :with-tags)
  1292. (org-element-property :tags inlinetask))
  1293. ;; contents and width
  1294. contents width inlinetask info)))
  1295. ;;;; Italic
  1296. (defun org-ascii-italic (_italic contents _info)
  1297. "Transcode italic from Org to ASCII.
  1298. CONTENTS is the text with italic markup. INFO is a plist holding
  1299. contextual information."
  1300. (format "/%s/" contents))
  1301. ;;;; Item
  1302. (defun org-ascii-item (item contents info)
  1303. "Transcode an ITEM element from Org to ASCII.
  1304. CONTENTS holds the contents of the item. INFO is a plist holding
  1305. contextual information."
  1306. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1307. (checkbox (org-ascii--checkbox item info))
  1308. (list-type (org-element-property :type (org-export-get-parent item)))
  1309. (bullet
  1310. ;; First parent of ITEM is always the plain-list. Get
  1311. ;; `:type' property from it.
  1312. (pcase list-type
  1313. (`descriptive
  1314. (concat checkbox
  1315. (org-export-data (org-element-property :tag item)
  1316. info)))
  1317. (`ordered
  1318. ;; Return correct number for ITEM, paying attention to
  1319. ;; counters.
  1320. (let* ((struct (org-element-property :structure item))
  1321. (bul (org-list-bullet-string
  1322. (org-element-property :bullet item)))
  1323. (num (number-to-string
  1324. (car (last (org-list-get-item-number
  1325. (org-element-property :begin item)
  1326. struct
  1327. (org-list-prevs-alist struct)
  1328. (org-list-parents-alist struct)))))))
  1329. (replace-regexp-in-string "[0-9]+" num bul)))
  1330. (_ (let ((bul (org-list-bullet-string
  1331. (org-element-property :bullet item))))
  1332. ;; Change bullets into more visible form if UTF-8 is active.
  1333. (if (not utf8p) bul
  1334. (replace-regexp-in-string
  1335. "-" "•"
  1336. (replace-regexp-in-string
  1337. "\\+" "⁃"
  1338. (replace-regexp-in-string "\\*" "‣" bul))))))))
  1339. (indentation (if (eq list-type 'descriptive) org-ascii-quote-margin
  1340. (string-width bullet))))
  1341. (concat
  1342. bullet
  1343. checkbox
  1344. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1345. ;; already taken care of at the paragraph level so they don't
  1346. ;; interfere with indentation.
  1347. (let ((contents (org-ascii--indent-string contents indentation)))
  1348. ;; Determine if contents should follow the bullet or start
  1349. ;; a new line. Do the former when the first contributing
  1350. ;; element to contents is a paragraph. In descriptive lists
  1351. ;; however, contents always start a new line.
  1352. (if (and (not (eq list-type 'descriptive))
  1353. (org-string-nw-p contents)
  1354. (eq 'paragraph
  1355. (org-element-type
  1356. (cl-some (lambda (e)
  1357. (and (org-string-nw-p (org-export-data e info))
  1358. e))
  1359. (org-element-contents item)))))
  1360. (org-trim contents)
  1361. (concat "\n" contents))))))
  1362. ;;;; Keyword
  1363. (defun org-ascii-keyword (keyword _contents info)
  1364. "Transcode a KEYWORD element from Org to ASCII.
  1365. CONTENTS is nil. INFO is a plist holding contextual
  1366. information."
  1367. (let ((key (org-element-property :key keyword))
  1368. (value (org-element-property :value keyword)))
  1369. (cond
  1370. ((string= key "ASCII") (org-ascii--justify-element value keyword info))
  1371. ((string= key "TOC")
  1372. (org-ascii--justify-element
  1373. (let ((case-fold-search t))
  1374. (cond
  1375. ((string-match-p "\\<headlines\\>" value)
  1376. (let ((depth (and (string-match "\\<[0-9]+\\>" value)
  1377. (string-to-number (match-string 0 value))))
  1378. (scope
  1379. (cond
  1380. ((string-match ":target +\\(\".+?\"\\|\\S-+\\)" value) ;link
  1381. (org-export-resolve-link
  1382. (org-strip-quotes (match-string 1 value)) info))
  1383. ((string-match-p "\\<local\\>" value) keyword)))) ;local
  1384. (org-ascii--build-toc info depth keyword scope)))
  1385. ((string-match-p "\\<tables\\>" value)
  1386. (org-ascii--list-tables keyword info))
  1387. ((string-match-p "\\<listings\\>" value)
  1388. (org-ascii--list-listings keyword info))))
  1389. keyword info)))))
  1390. ;;;; Latex Environment
  1391. (defun org-ascii-latex-environment (latex-environment _contents info)
  1392. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1393. CONTENTS is nil. INFO is a plist holding contextual
  1394. information."
  1395. (when (plist-get info :with-latex)
  1396. (org-ascii--justify-element
  1397. (org-remove-indentation (org-element-property :value latex-environment))
  1398. latex-environment info)))
  1399. ;;;; Latex Fragment
  1400. (defun org-ascii-latex-fragment (latex-fragment _contents info)
  1401. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1402. CONTENTS is nil. INFO is a plist holding contextual
  1403. information."
  1404. (when (plist-get info :with-latex)
  1405. (org-element-property :value latex-fragment)))
  1406. ;;;; Line Break
  1407. (defun org-ascii-line-break (_line-break _contents _info)
  1408. "Transcode a LINE-BREAK object from Org to ASCII.
  1409. CONTENTS is nil. INFO is a plist holding contextual
  1410. information." hard-newline)
  1411. ;;;; Link
  1412. (defun org-ascii-link (link desc info)
  1413. "Transcode a LINK object from Org to ASCII.
  1414. DESC is the description part of the link, or the empty string.
  1415. INFO is a plist holding contextual information."
  1416. (let ((type (org-element-property :type link)))
  1417. (cond
  1418. ((org-export-custom-protocol-maybe link desc 'ascii info))
  1419. ((string= type "coderef")
  1420. (let ((ref (org-element-property :path link)))
  1421. (format (org-export-get-coderef-format ref desc)
  1422. (org-export-resolve-coderef ref info))))
  1423. ;; Do not apply a special syntax on radio links. Though, use
  1424. ;; transcoded target's contents as output.
  1425. ((string= type "radio") desc)
  1426. ((member type '("custom-id" "fuzzy" "id"))
  1427. (let ((destination (if (string= type "fuzzy")
  1428. (org-export-resolve-fuzzy-link link info)
  1429. (org-export-resolve-id-link link info))))
  1430. (pcase (org-element-type destination)
  1431. ((guard desc)
  1432. (if (plist-get info :ascii-links-to-notes)
  1433. (format "[%s]" desc)
  1434. (concat desc
  1435. (format " (%s)"
  1436. (org-ascii--describe-datum destination info)))))
  1437. ;; External file.
  1438. (`plain-text destination)
  1439. (`headline
  1440. (if (org-export-numbered-headline-p destination info)
  1441. (mapconcat #'number-to-string
  1442. (org-export-get-headline-number destination info)
  1443. ".")
  1444. (org-export-data (org-element-property :title destination) info)))
  1445. ;; Handle enumerable elements and targets within them.
  1446. ((and (let number (org-export-get-ordinal
  1447. destination info nil #'org-ascii--has-caption-p))
  1448. (guard number))
  1449. (if (atom number) (number-to-string number)
  1450. (mapconcat #'number-to-string number ".")))
  1451. ;; Don't know what to do. Signal it.
  1452. (_ "???"))))
  1453. (t
  1454. (let ((path (org-element-property :raw-link link)))
  1455. (if (not (org-string-nw-p desc)) (format "<%s>" path)
  1456. (concat (format "[%s]" desc)
  1457. (and (not (plist-get info :ascii-links-to-notes))
  1458. (format " (<%s>)" path)))))))))
  1459. ;;;; Node Properties
  1460. (defun org-ascii-node-property (node-property _contents _info)
  1461. "Transcode a NODE-PROPERTY element from Org to ASCII.
  1462. CONTENTS is nil. INFO is a plist holding contextual
  1463. information."
  1464. (format "%s:%s"
  1465. (org-element-property :key node-property)
  1466. (let ((value (org-element-property :value node-property)))
  1467. (if value (concat " " value) ""))))
  1468. ;;;; Paragraph
  1469. (defun org-ascii-paragraph (paragraph contents info)
  1470. "Transcode a PARAGRAPH element from Org to ASCII.
  1471. CONTENTS is the contents of the paragraph, as a string. INFO is
  1472. the plist used as a communication channel."
  1473. (org-ascii--justify-element
  1474. (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
  1475. (if (not (wholenump indented-line-width)) contents
  1476. (concat
  1477. ;; Do not indent first paragraph in a section.
  1478. (unless (and (not (org-export-get-previous-element paragraph info))
  1479. (eq (org-element-type (org-export-get-parent paragraph))
  1480. 'section))
  1481. (make-string indented-line-width ?\s))
  1482. (replace-regexp-in-string "\\`[ \t]+" "" contents))))
  1483. paragraph info))
  1484. ;;;; Plain List
  1485. (defun org-ascii-plain-list (plain-list contents info)
  1486. "Transcode a PLAIN-LIST element from Org to ASCII.
  1487. CONTENTS is the contents of the list. INFO is a plist holding
  1488. contextual information."
  1489. (let ((margin (plist-get info :ascii-list-margin)))
  1490. (if (or (< margin 1)
  1491. (eq (org-element-type (org-export-get-parent plain-list)) 'item))
  1492. contents
  1493. (org-ascii--indent-string contents margin))))
  1494. ;;;; Plain Text
  1495. (defun org-ascii-plain-text (text info)
  1496. "Transcode a TEXT string from Org to ASCII.
  1497. INFO is a plist used as a communication channel."
  1498. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1499. (when (and utf8p (plist-get info :with-smart-quotes))
  1500. (setq text (org-export-activate-smart-quotes text :utf-8 info)))
  1501. (if (not (plist-get info :with-special-strings)) text
  1502. (setq text (replace-regexp-in-string "\\\\-" "" text))
  1503. (if (not utf8p) text
  1504. ;; Usual replacements in utf-8 with proper option set.
  1505. (replace-regexp-in-string
  1506. "\\.\\.\\." "…"
  1507. (replace-regexp-in-string
  1508. "--" "–"
  1509. (replace-regexp-in-string "---" "—" text)))))))
  1510. ;;;; Planning
  1511. (defun org-ascii-planning (planning _contents info)
  1512. "Transcode a PLANNING element from Org to ASCII.
  1513. CONTENTS is nil. INFO is a plist used as a communication
  1514. channel."
  1515. (org-ascii--justify-element
  1516. (mapconcat
  1517. #'identity
  1518. (delq nil
  1519. (list (let ((closed (org-element-property :closed planning)))
  1520. (when closed
  1521. (concat org-closed-string " "
  1522. (org-timestamp-translate closed))))
  1523. (let ((deadline (org-element-property :deadline planning)))
  1524. (when deadline
  1525. (concat org-deadline-string " "
  1526. (org-timestamp-translate deadline))))
  1527. (let ((scheduled (org-element-property :scheduled planning)))
  1528. (when scheduled
  1529. (concat org-scheduled-string " "
  1530. (org-timestamp-translate scheduled))))))
  1531. " ")
  1532. planning info))
  1533. ;;;; Property Drawer
  1534. (defun org-ascii-property-drawer (property-drawer contents info)
  1535. "Transcode a PROPERTY-DRAWER element from Org to ASCII.
  1536. CONTENTS holds the contents of the drawer. INFO is a plist
  1537. holding contextual information."
  1538. (and (org-string-nw-p contents)
  1539. (org-ascii--justify-element contents property-drawer info)))
  1540. ;;;; Quote Block
  1541. (defun org-ascii-quote-block (_quote-block contents info)
  1542. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1543. CONTENTS holds the contents of the block. INFO is a plist
  1544. holding contextual information."
  1545. (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
  1546. ;;;; Radio Target
  1547. (defun org-ascii-radio-target (_radio-target contents _info)
  1548. "Transcode a RADIO-TARGET object from Org to ASCII.
  1549. CONTENTS is the contents of the target. INFO is a plist holding
  1550. contextual information."
  1551. contents)
  1552. ;;;; Section
  1553. (defun org-ascii-section (section contents info)
  1554. "Transcode a SECTION element from Org to ASCII.
  1555. CONTENTS is the contents of the section. INFO is a plist holding
  1556. contextual information."
  1557. (let ((links
  1558. (and (plist-get info :ascii-links-to-notes)
  1559. ;; Take care of links in first section of the document.
  1560. (not (org-element-lineage section '(headline)))
  1561. (org-ascii--describe-links
  1562. (org-ascii--unique-links section info)
  1563. (org-ascii--current-text-width section info)
  1564. info))))
  1565. (org-ascii--indent-string
  1566. (if (not (org-string-nw-p links)) contents
  1567. (concat (org-element-normalize-string contents) "\n\n" links))
  1568. ;; Do not apply inner margin if parent headline is low level.
  1569. (let ((headline (org-export-get-parent-headline section)))
  1570. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1571. (plist-get info :ascii-inner-margin))))))
  1572. ;;;; Special Block
  1573. (defun org-ascii-special-block (_special-block contents _info)
  1574. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1575. CONTENTS holds the contents of the block. INFO is a plist
  1576. holding contextual information."
  1577. ;; "JUSTIFYLEFT" and "JUSTIFYRIGHT" have already been taken care of
  1578. ;; at a lower level. There is no other special block type to
  1579. ;; handle.
  1580. contents)
  1581. ;;;; Src Block
  1582. (defun org-ascii-src-block (src-block _contents info)
  1583. "Transcode a SRC-BLOCK element from Org to ASCII.
  1584. CONTENTS holds the contents of the item. INFO is a plist holding
  1585. contextual information."
  1586. (let ((caption (org-ascii--build-caption src-block info))
  1587. (caption-above-p (plist-get info :ascii-caption-above))
  1588. (code (org-export-format-code-default src-block info)))
  1589. (if (equal code "") ""
  1590. (org-ascii--justify-element
  1591. (concat
  1592. (and caption caption-above-p (concat caption "\n"))
  1593. (org-ascii--box-string code info)
  1594. (and caption (not caption-above-p) (concat "\n" caption)))
  1595. src-block info))))
  1596. ;;;; Statistics Cookie
  1597. (defun org-ascii-statistics-cookie (statistics-cookie _contents _info)
  1598. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1599. CONTENTS is nil. INFO is a plist holding contextual information."
  1600. (org-element-property :value statistics-cookie))
  1601. ;;;; Subscript
  1602. (defun org-ascii-subscript (subscript contents _info)
  1603. "Transcode a SUBSCRIPT object from Org to ASCII.
  1604. CONTENTS is the contents of the object. INFO is a plist holding
  1605. contextual information."
  1606. (if (org-element-property :use-brackets-p subscript)
  1607. (format "_{%s}" contents)
  1608. (format "_%s" contents)))
  1609. ;;;; Superscript
  1610. (defun org-ascii-superscript (superscript contents _info)
  1611. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1612. CONTENTS is the contents of the object. INFO is a plist holding
  1613. contextual information."
  1614. (if (org-element-property :use-brackets-p superscript)
  1615. (format "^{%s}" contents)
  1616. (format "^%s" contents)))
  1617. ;;;; Strike-through
  1618. (defun org-ascii-strike-through (_strike-through contents _info)
  1619. "Transcode STRIKE-THROUGH from Org to ASCII.
  1620. CONTENTS is text with strike-through markup. INFO is a plist
  1621. holding contextual information."
  1622. (format "+%s+" contents))
  1623. ;;;; Table
  1624. (defun org-ascii-table (table contents info)
  1625. "Transcode a TABLE element from Org to ASCII.
  1626. CONTENTS is the contents of the table. INFO is a plist holding
  1627. contextual information."
  1628. (let ((caption (org-ascii--build-caption table info))
  1629. (caption-above-p (plist-get info :ascii-caption-above)))
  1630. (org-ascii--justify-element
  1631. (concat
  1632. ;; Possibly add a caption string above.
  1633. (and caption caption-above-p (concat caption "\n"))
  1634. ;; Insert table. Note: "table.el" tables are left unmodified.
  1635. (cond ((eq (org-element-property :type table) 'org) contents)
  1636. ((and (plist-get info :ascii-table-use-ascii-art)
  1637. (eq (plist-get info :ascii-charset) 'utf-8)
  1638. (require 'ascii-art-to-unicode nil t))
  1639. (with-temp-buffer
  1640. (insert (org-remove-indentation
  1641. (org-element-property :value table)))
  1642. (goto-char (point-min))
  1643. (aa2u)
  1644. (goto-char (point-max))
  1645. (skip-chars-backward " \r\t\n")
  1646. (buffer-substring (point-min) (point))))
  1647. (t (org-remove-indentation (org-element-property :value table))))
  1648. ;; Possible add a caption string below.
  1649. (and (not caption-above-p) caption))
  1650. table info)))
  1651. ;;;; Table Cell
  1652. (defun org-ascii--table-cell-width (table-cell info)
  1653. "Return width of TABLE-CELL.
  1654. INFO is a plist used as a communication channel.
  1655. Width of a cell is determined either by a width cookie in the
  1656. same column as the cell, or by the maximum cell's length in that
  1657. column.
  1658. When `org-ascii-table-widen-columns' is non-nil, width cookies
  1659. are ignored."
  1660. (let* ((row (org-export-get-parent table-cell))
  1661. (table (org-export-get-parent row))
  1662. (col (let ((cells (org-element-contents row)))
  1663. (- (length cells) (length (memq table-cell cells)))))
  1664. (cache
  1665. (or (plist-get info :ascii-table-cell-width-cache)
  1666. (plist-get (setq info
  1667. (plist-put info :ascii-table-cell-width-cache
  1668. (make-hash-table :test 'equal)))
  1669. :ascii-table-cell-width-cache)))
  1670. (key (cons table col))
  1671. (widenp (plist-get info :ascii-table-widen-columns)))
  1672. (or (gethash key cache)
  1673. (puthash
  1674. key
  1675. (let ((cookie-width (org-export-table-cell-width table-cell info)))
  1676. (or (and (not widenp) cookie-width)
  1677. (let ((contents-width
  1678. (let ((max-width 0))
  1679. (org-element-map table 'table-row
  1680. (lambda (row)
  1681. (setq max-width
  1682. (max (string-width
  1683. (org-export-data
  1684. (org-element-contents
  1685. (elt (org-element-contents row) col))
  1686. info))
  1687. max-width)))
  1688. info)
  1689. max-width)))
  1690. (cond ((not cookie-width) contents-width)
  1691. (widenp (max cookie-width contents-width))
  1692. (t cookie-width)))))
  1693. cache))))
  1694. (defun org-ascii-table-cell (table-cell contents info)
  1695. "Transcode a TABLE-CELL object from Org to ASCII.
  1696. CONTENTS is the cell contents. INFO is a plist used as
  1697. a communication channel."
  1698. ;; Determine column width. When `org-ascii-table-widen-columns'
  1699. ;; is nil and some width cookie has set it, use that value.
  1700. ;; Otherwise, compute the maximum width among transcoded data of
  1701. ;; each cell in the column.
  1702. (let ((width (org-ascii--table-cell-width table-cell info)))
  1703. ;; When contents are too large, truncate them.
  1704. (unless (or (plist-get info :ascii-table-widen-columns)
  1705. (<= (string-width (or contents "")) width))
  1706. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1707. ;; Align contents correctly within the cell.
  1708. (let* ((indent-tabs-mode nil)
  1709. (data
  1710. (when contents
  1711. (org-ascii--justify-lines
  1712. contents width
  1713. (org-export-table-cell-alignment table-cell info)))))
  1714. (setq contents
  1715. (concat data
  1716. ;; FIXME: If CONTENTS was transformed by filters,
  1717. ;; the whole width calculation can be wrong.
  1718. ;; At least, make sure that we do not throw error
  1719. ;; when CONTENTS is larger than width.
  1720. (make-string (max 0 (- width (string-width (or data "")))) ?\s))))
  1721. ;; Return cell.
  1722. (concat (format " %s " contents)
  1723. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1724. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1725. ;;;; Table Row
  1726. (defun org-ascii-table-row (table-row contents info)
  1727. "Transcode a TABLE-ROW element from Org to ASCII.
  1728. CONTENTS is the row contents. INFO is a plist used as
  1729. a communication channel."
  1730. (when (eq (org-element-property :type table-row) 'standard)
  1731. (let ((build-hline
  1732. (lambda (lcorner horiz vert rcorner)
  1733. (concat
  1734. (apply
  1735. 'concat
  1736. (org-element-map table-row 'table-cell
  1737. (lambda (cell)
  1738. (let ((width (org-ascii--table-cell-width cell info))
  1739. (borders (org-export-table-cell-borders cell info)))
  1740. (concat
  1741. ;; In order to know if CELL starts the row, do
  1742. ;; not compare it with the first cell in the
  1743. ;; row as there might be a special column.
  1744. ;; Instead, compare it with first exportable
  1745. ;; cell, obtained with `org-element-map'.
  1746. (when (and (memq 'left borders)
  1747. (eq (org-element-map table-row 'table-cell
  1748. 'identity info t)
  1749. cell))
  1750. lcorner)
  1751. (make-string (+ 2 width) (string-to-char horiz))
  1752. (cond
  1753. ((not (memq 'right borders)) nil)
  1754. ((eq (car (last (org-element-contents table-row))) cell)
  1755. rcorner)
  1756. (t vert)))))
  1757. info)) "\n")))
  1758. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1759. (borders (org-export-table-cell-borders
  1760. (org-element-map table-row 'table-cell 'identity info t)
  1761. info)))
  1762. (concat (cond
  1763. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1764. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1765. (funcall build-hline "+" "-" "+" "+")))
  1766. ((memq 'above borders)
  1767. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1768. (funcall build-hline "+" "-" "+" "+"))))
  1769. (when (memq 'left borders) (if utf8p "│" "|"))
  1770. contents "\n"
  1771. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1772. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1773. (funcall build-hline "+" "-" "+" "+")))))))
  1774. ;;;; Timestamp
  1775. (defun org-ascii-timestamp (timestamp _contents info)
  1776. "Transcode a TIMESTAMP object from Org to ASCII.
  1777. CONTENTS is nil. INFO is a plist holding contextual information."
  1778. (org-ascii-plain-text (org-timestamp-translate timestamp) info))
  1779. ;;;; Underline
  1780. (defun org-ascii-underline (_underline contents _info)
  1781. "Transcode UNDERLINE from Org to ASCII.
  1782. CONTENTS is the text with underline markup. INFO is a plist
  1783. holding contextual information."
  1784. (format "_%s_" contents))
  1785. ;;;; Verbatim
  1786. (defun org-ascii-verbatim (verbatim _contents info)
  1787. "Return a VERBATIM object from Org to ASCII.
  1788. CONTENTS is nil. INFO is a plist holding contextual information."
  1789. (format (plist-get info :ascii-verbatim-format)
  1790. (org-element-property :value verbatim)))
  1791. ;;;; Verse Block
  1792. (defun org-ascii-verse-block (verse-block contents info)
  1793. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1794. CONTENTS is verse block contents. INFO is a plist holding
  1795. contextual information."
  1796. (org-ascii--indent-string
  1797. (org-ascii--justify-element contents verse-block info)
  1798. (plist-get info :ascii-quote-margin)))
  1799. ;;; Filters
  1800. (defun org-ascii-filter-headline-blank-lines (headline _backend info)
  1801. "Filter controlling number of blank lines after a headline.
  1802. HEADLINE is a string representing a transcoded headline. BACKEND
  1803. is symbol specifying back-end used for export. INFO is plist
  1804. containing the communication channel.
  1805. This function only applies to `ascii' back-end. See
  1806. `org-ascii-headline-spacing' for information."
  1807. (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
  1808. (if (not headline-spacing) headline
  1809. (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
  1810. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
  1811. (defun org-ascii-filter-paragraph-spacing (tree _backend info)
  1812. "Filter controlling number of blank lines between paragraphs.
  1813. TREE is the parse tree. BACKEND is the symbol specifying
  1814. back-end used for export. INFO is a plist used as
  1815. a communication channel.
  1816. See `org-ascii-paragraph-spacing' for information."
  1817. (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
  1818. (when (wholenump paragraph-spacing)
  1819. (org-element-map tree 'paragraph
  1820. (lambda (p)
  1821. (when (eq (org-element-type (org-export-get-next-element p info))
  1822. 'paragraph)
  1823. (org-element-put-property p :post-blank paragraph-spacing))))))
  1824. tree)
  1825. (defun org-ascii-filter-comment-spacing (tree _backend info)
  1826. "Filter removing blank lines between comments.
  1827. TREE is the parse tree. BACKEND is the symbol specifying
  1828. back-end used for export. INFO is a plist used as
  1829. a communication channel."
  1830. (org-element-map tree '(comment comment-block)
  1831. (lambda (c)
  1832. (when (memq (org-element-type (org-export-get-next-element c info))
  1833. '(comment comment-block))
  1834. (org-element-put-property c :post-blank 0))))
  1835. tree)
  1836. ;;; End-user functions
  1837. ;;;###autoload
  1838. (defun org-ascii-convert-region-to-ascii ()
  1839. "Assume region has Org syntax, and convert it to plain ASCII."
  1840. (interactive)
  1841. (let ((org-ascii-charset 'ascii))
  1842. (org-export-replace-region-by 'ascii)))
  1843. ;;;###autoload
  1844. (defun org-ascii-convert-region-to-utf8 ()
  1845. "Assume region has Org syntax, and convert it to UTF-8."
  1846. (interactive)
  1847. (let ((org-ascii-charset 'utf-8))
  1848. (org-export-replace-region-by 'ascii)))
  1849. ;;;###autoload
  1850. (defun org-ascii-export-as-ascii
  1851. (&optional async subtreep visible-only body-only ext-plist)
  1852. "Export current buffer to a text buffer.
  1853. If narrowing is active in the current buffer, only export its
  1854. narrowed part.
  1855. If a region is active, export that region.
  1856. A non-nil optional argument ASYNC means the process should happen
  1857. asynchronously. The resulting buffer should be accessible
  1858. through the `org-export-stack' interface.
  1859. When optional argument SUBTREEP is non-nil, export the sub-tree
  1860. at point, extracting information from the headline properties
  1861. first.
  1862. When optional argument VISIBLE-ONLY is non-nil, don't export
  1863. contents of hidden elements.
  1864. When optional argument BODY-ONLY is non-nil, strip title and
  1865. table of contents from output.
  1866. EXT-PLIST, when provided, is a property list with external
  1867. parameters overriding Org default settings, but still inferior to
  1868. file-local settings.
  1869. Export is done in a buffer named \"*Org ASCII Export*\", which
  1870. will be displayed when `org-export-show-temporary-export-buffer'
  1871. is non-nil."
  1872. (interactive)
  1873. (org-export-to-buffer 'ascii "*Org ASCII Export*"
  1874. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  1875. ;;;###autoload
  1876. (defun org-ascii-export-to-ascii
  1877. (&optional async subtreep visible-only body-only ext-plist)
  1878. "Export current buffer to a text file.
  1879. If narrowing is active in the current buffer, only export its
  1880. narrowed part.
  1881. If a region is active, export that region.
  1882. A non-nil optional argument ASYNC means the process should happen
  1883. asynchronously. The resulting file should be accessible through
  1884. the `org-export-stack' interface.
  1885. When optional argument SUBTREEP is non-nil, export the sub-tree
  1886. at point, extracting information from the headline properties
  1887. first.
  1888. When optional argument VISIBLE-ONLY is non-nil, don't export
  1889. contents of hidden elements.
  1890. When optional argument BODY-ONLY is non-nil, strip title and
  1891. table of contents from output.
  1892. EXT-PLIST, when provided, is a property list with external
  1893. parameters overriding Org default settings, but still inferior to
  1894. file-local settings.
  1895. Return output file's name."
  1896. (interactive)
  1897. (let ((file (org-export-output-file-name ".txt" subtreep)))
  1898. (org-export-to-file 'ascii file
  1899. async subtreep visible-only body-only ext-plist)))
  1900. ;;;###autoload
  1901. (defun org-ascii-publish-to-ascii (plist filename pub-dir)
  1902. "Publish an Org file to ASCII.
  1903. FILENAME is the filename of the Org file to be published. PLIST
  1904. is the property list for the given project. PUB-DIR is the
  1905. publishing directory.
  1906. Return output file name."
  1907. (org-publish-org-to
  1908. 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
  1909. ;;;###autoload
  1910. (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
  1911. "Publish an Org file to Latin-1.
  1912. FILENAME is the filename of the Org file to be published. PLIST
  1913. is the property list for the given project. PUB-DIR is the
  1914. publishing directory.
  1915. Return output file name."
  1916. (org-publish-org-to
  1917. 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
  1918. ;;;###autoload
  1919. (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
  1920. "Publish an org file to UTF-8.
  1921. FILENAME is the filename of the Org file to be published. PLIST
  1922. is the property list for the given project. PUB-DIR is the
  1923. publishing directory.
  1924. Return output file name."
  1925. (org-publish-org-to
  1926. 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
  1927. (provide 'ox-ascii)
  1928. ;; Local variables:
  1929. ;; generated-autoload-file: "org-loaddefs.el"
  1930. ;; coding: utf-8
  1931. ;; End:
  1932. ;;; ox-ascii.el ends here