ox-ascii.el 79 KB

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