ox-ascii.el 77 KB

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