ox-ascii.el 78 KB

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