ox-ascii.el 79 KB

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