ox-ascii.el 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2015 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 <http://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. (eval-when-compile (require 'cl))
  22. (require 'ox)
  23. (require 'ox-publish)
  24. (declare-function aa2u "ext:ascii-art-to-unicode" ())
  25. ;;; Define Back-End
  26. ;;
  27. ;; The following setting won't allow to modify preferred charset
  28. ;; through a buffer keyword or an option item, but, since the property
  29. ;; will appear in communication channel nonetheless, it allows to
  30. ;; override `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."
  161. :group 'org-export-ascii
  162. :version "24.4"
  163. :package-version '(Org . "8.0")
  164. :type 'integer)
  165. (defcustom org-ascii-list-margin 0
  166. "Width of margin used for plain lists, in characters.
  167. This margin applies to top level list only, not to its
  168. sub-lists."
  169. :group 'org-export-ascii
  170. :version "25.1"
  171. :package-version '(Org . "8.3")
  172. :type 'integer)
  173. (defcustom org-ascii-inlinetask-width 30
  174. "Width of inline tasks, in number of characters.
  175. This number ignores any margin."
  176. :group 'org-export-ascii
  177. :version "24.4"
  178. :package-version '(Org . "8.0")
  179. :type 'integer)
  180. (defcustom org-ascii-headline-spacing '(1 . 2)
  181. "Number of blank lines inserted around headlines.
  182. This variable can be set to a cons cell. In that case, its car
  183. represents the number of blank lines present before headline
  184. contents whereas its cdr reflects the number of blank lines after
  185. contents.
  186. A nil value replicates the number of blank lines found in the
  187. original Org buffer at the same place."
  188. :group 'org-export-ascii
  189. :version "24.4"
  190. :package-version '(Org . "8.0")
  191. :type '(choice
  192. (const :tag "Replicate original spacing" nil)
  193. (cons :tag "Set a uniform spacing"
  194. (integer :tag "Number of blank lines before contents")
  195. (integer :tag "Number of blank lines after contents"))))
  196. (defcustom org-ascii-indented-line-width 'auto
  197. "Additional indentation width for the first line in a paragraph.
  198. If the value is an integer, indent the first line of each
  199. paragraph by this width, unless it is located at the beginning of
  200. a section, in which case indentation is removed from that line.
  201. If it is the symbol `auto' preserve indentation from original
  202. document."
  203. :group 'org-export-ascii
  204. :version "24.4"
  205. :package-version '(Org . "8.0")
  206. :type '(choice
  207. (integer :tag "Number of white spaces characters")
  208. (const :tag "Preserve original width" auto)))
  209. (defcustom org-ascii-paragraph-spacing 'auto
  210. "Number of white lines between paragraphs.
  211. If the value is an integer, add this number of blank lines
  212. between contiguous paragraphs. If is it the symbol `auto', keep
  213. the same number of blank lines as in the original document."
  214. :group 'org-export-ascii
  215. :version "24.4"
  216. :package-version '(Org . "8.0")
  217. :type '(choice
  218. (integer :tag "Number of blank lines")
  219. (const :tag "Preserve original spacing" auto)))
  220. (defcustom org-ascii-charset 'ascii
  221. "The charset allowed to represent various elements and objects.
  222. Possible values are:
  223. `ascii' Only use plain ASCII characters
  224. `latin1' Include Latin-1 characters
  225. `utf-8' Use all UTF-8 characters"
  226. :group 'org-export-ascii
  227. :version "24.4"
  228. :package-version '(Org . "8.0")
  229. :type '(choice
  230. (const :tag "ASCII" ascii)
  231. (const :tag "Latin-1" latin1)
  232. (const :tag "UTF-8" utf-8)))
  233. (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
  234. (latin1 ?= ?~ ?-)
  235. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  236. "Characters for underlining headings in ASCII export.
  237. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  238. and whose value is a list of characters.
  239. For each supported charset, this variable associates a sequence
  240. of underline characters. In a sequence, the characters will be
  241. used in order for headlines level 1, 2, ... If no character is
  242. available for a given level, the headline won't be underlined."
  243. :group 'org-export-ascii
  244. :version "24.4"
  245. :package-version '(Org . "8.0")
  246. :type '(list
  247. (cons :tag "Underline characters sequence"
  248. (const :tag "ASCII charset" ascii)
  249. (repeat character))
  250. (cons :tag "Underline characters sequence"
  251. (const :tag "Latin-1 charset" latin1)
  252. (repeat character))
  253. (cons :tag "Underline characters sequence"
  254. (const :tag "UTF-8 charset" utf-8)
  255. (repeat character))))
  256. (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
  257. (latin1 ?§ ?¶)
  258. (utf-8 ?◊))
  259. "Bullet characters for headlines converted to lists in ASCII export.
  260. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  261. and whose value is a list of characters.
  262. The first character is used for the first level considered as low
  263. level, and so on. If there are more levels than characters given
  264. here, the list will be repeated.
  265. Note that this variable doesn't affect plain lists
  266. representation."
  267. :group 'org-export-ascii
  268. :version "24.4"
  269. :package-version '(Org . "8.0")
  270. :type '(list
  271. (cons :tag "Bullet characters for low level headlines"
  272. (const :tag "ASCII charset" ascii)
  273. (repeat character))
  274. (cons :tag "Bullet characters for low level headlines"
  275. (const :tag "Latin-1 charset" latin1)
  276. (repeat character))
  277. (cons :tag "Bullet characters for low level headlines"
  278. (const :tag "UTF-8 charset" utf-8)
  279. (repeat character))))
  280. (defcustom org-ascii-links-to-notes t
  281. "Non-nil means convert links to notes before the next headline.
  282. When nil, the link will be exported in place. If the line
  283. becomes long in this way, it will be wrapped."
  284. :group 'org-export-ascii
  285. :version "24.4"
  286. :package-version '(Org . "8.0")
  287. :type 'boolean)
  288. (defcustom org-ascii-table-keep-all-vertical-lines nil
  289. "Non-nil means keep all vertical lines in ASCII tables.
  290. When nil, vertical lines will be removed except for those needed
  291. for column grouping."
  292. :group 'org-export-ascii
  293. :version "24.4"
  294. :package-version '(Org . "8.0")
  295. :type 'boolean)
  296. (defcustom org-ascii-table-widen-columns t
  297. "Non-nil means widen narrowed columns for export.
  298. When nil, narrowed columns will look in ASCII export just like in
  299. Org mode, i.e. with \"=>\" as ellipsis."
  300. :group 'org-export-ascii
  301. :version "24.4"
  302. :package-version '(Org . "8.0")
  303. :type 'boolean)
  304. (defcustom org-ascii-table-use-ascii-art nil
  305. "Non-nil means table.el tables are turned into ascii-art.
  306. It only makes sense when export charset is `utf-8'. It is nil by
  307. default since it requires ascii-art-to-unicode.el package. You
  308. can download it here:
  309. http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
  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 "24.4"
  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 (org-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 (org-looking-at-p "[ \t]*$")
  464. (org-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. (case (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. (otherwise
  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 (loop for parent in genealogy
  501. thereis (eq (org-element-type parent) 'inlinetask))
  502. (plist-get info :ascii-inlinetask-width)
  503. ;; No inlinetask: Remove global margin from text width.
  504. (- (plist-get info :ascii-text-width)
  505. (plist-get info :ascii-global-margin)
  506. (let ((parent (org-export-get-parent-headline element)))
  507. ;; Inner margin doesn't apply to text before first
  508. ;; headline.
  509. (if (not parent) 0
  510. (let ((low-level-rank
  511. (org-export-low-level-p parent info)))
  512. ;; Inner margin doesn't apply to contents of
  513. ;; low level headlines, since they've got their
  514. ;; own indentation mechanism.
  515. (if low-level-rank (* low-level-rank 2)
  516. (plist-get info :ascii-inner-margin)))))))))
  517. (- total-width
  518. ;; Each `quote-block' and `verse-block' above narrows text
  519. ;; width by twice the standard margin size.
  520. (+ (* (loop for parent in genealogy
  521. when (memq (org-element-type parent)
  522. '(quote-block verse-block))
  523. count parent)
  524. 2 (plist-get info :ascii-quote-margin))
  525. ;; Apply list margin once per "top-level" plain-list
  526. ;; containing current line
  527. (* (let ((count 0))
  528. (dolist (e genealogy count)
  529. (and (eq (org-element-type e) 'plain-list)
  530. (not (eq (org-element-type (org-export-get-parent e))
  531. 'item))
  532. (incf count))))
  533. (plist-get info :ascii-list-margin))
  534. ;; Text width within a plain-list is restricted by
  535. ;; indentation of current item. If that's the case,
  536. ;; compute it with the help of `:structure' property from
  537. ;; parent item, if any.
  538. (let ((item
  539. (if (eq (org-element-type element) 'item) element
  540. (loop for parent in genealogy
  541. when (eq (org-element-type parent) 'item)
  542. return parent))))
  543. (if (not item) 0
  544. ;; Compute indentation offset of the current item,
  545. ;; that is the sum of the difference between its
  546. ;; indentation and the indentation of the top item in
  547. ;; the list and current item bullet's length. Also
  548. ;; remove checkbox length, and tag length (for
  549. ;; description lists) or bullet length.
  550. (let ((struct (org-element-property :structure item))
  551. (beg-item (org-element-property :begin item)))
  552. (+ (- (org-list-get-ind beg-item struct)
  553. (org-list-get-ind
  554. (org-list-get-top-point struct) struct))
  555. (string-width (or (org-ascii--checkbox item info)
  556. ""))
  557. (string-width
  558. (let ((tag (org-element-property :tag item)))
  559. (if tag (org-export-data tag info)
  560. (org-element-property :bullet item))))))))))))))
  561. (defun org-ascii--current-justification (element)
  562. "Return expected justification for ELEMENT's contents.
  563. Return value is a symbol among `left', `center', `right' and
  564. `full'."
  565. (let (justification)
  566. (while (and (not justification)
  567. (setq element (org-element-property :parent element)))
  568. (case (org-element-type element)
  569. (center-block (setq justification 'center))
  570. (special-block
  571. (let ((name (org-element-property :type element)))
  572. (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
  573. ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
  574. (or justification 'left)))
  575. (defun org-ascii--build-title
  576. (element info text-width &optional underline notags toc)
  577. "Format ELEMENT title and return it.
  578. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  579. a plist used as a communication channel. TEXT-WIDTH is an
  580. integer representing the maximum length of a line.
  581. When optional argument UNDERLINE is non-nil, underline title,
  582. without the tags, according to `org-ascii-underline'
  583. specifications.
  584. If optional argument NOTAGS is non-nil, no tags will be added to
  585. the title.
  586. When optional argument TOC is non-nil, use optional title if
  587. possible. It doesn't apply to `inlinetask' elements."
  588. (let* ((headlinep (eq (org-element-type element) 'headline))
  589. (numbers
  590. ;; Numbering is specific to headlines.
  591. (and headlinep (org-export-numbered-headline-p element info)
  592. ;; All tests passed: build numbering string.
  593. (concat
  594. (mapconcat
  595. 'number-to-string
  596. (org-export-get-headline-number element info) ".")
  597. " ")))
  598. (text
  599. (org-trim
  600. (org-export-data
  601. (if (and toc headlinep) (org-export-get-alt-title element info)
  602. (org-element-property :title element))
  603. info)))
  604. (todo
  605. (and (plist-get info :with-todo-keywords)
  606. (let ((todo (org-element-property :todo-keyword element)))
  607. (and todo (concat (org-export-data todo info) " ")))))
  608. (tags (and (not notags)
  609. (plist-get info :with-tags)
  610. (let ((tag-list (org-export-get-tags element info)))
  611. (and tag-list
  612. (format ":%s:"
  613. (mapconcat 'identity tag-list ":"))))))
  614. (priority
  615. (and (plist-get info :with-priority)
  616. (let ((char (org-element-property :priority element)))
  617. (and char (format "(#%c) " char)))))
  618. (first-part (concat numbers todo priority text)))
  619. (concat
  620. first-part
  621. ;; Align tags, if any.
  622. (when tags
  623. (format
  624. (format " %%%ds"
  625. (max (- text-width (1+ (string-width first-part)))
  626. (string-width tags)))
  627. tags))
  628. ;; Maybe underline text, if ELEMENT type is `headline' and an
  629. ;; underline character has been defined.
  630. (when (and underline headlinep)
  631. (let ((under-char
  632. (nth (1- (org-export-get-relative-level element info))
  633. (cdr (assq (plist-get info :ascii-charset)
  634. (plist-get info :ascii-underline))))))
  635. (and under-char
  636. (concat "\n"
  637. (make-string (/ (string-width first-part)
  638. (char-width under-char))
  639. under-char))))))))
  640. (defun org-ascii--has-caption-p (element _info)
  641. "Non-nil when ELEMENT has a caption affiliated keyword.
  642. INFO is a plist used as a communication channel. This function
  643. is meant to be used as a predicate for `org-export-get-ordinal'."
  644. (org-element-property :caption element))
  645. (defun org-ascii--build-caption (element info)
  646. "Return caption string for ELEMENT, if applicable.
  647. INFO is a plist used as a communication channel.
  648. The caption string contains the sequence number of ELEMENT along
  649. with its real caption. Return nil when ELEMENT has no affiliated
  650. caption keyword."
  651. (let ((caption (org-export-get-caption element)))
  652. (when caption
  653. ;; Get sequence number of current src-block among every
  654. ;; src-block with a caption.
  655. (let ((reference
  656. (org-export-get-ordinal
  657. element info nil 'org-ascii--has-caption-p))
  658. (title-fmt (org-ascii--translate
  659. (case (org-element-type element)
  660. (table "Table %d:")
  661. (src-block "Listing %d:"))
  662. info)))
  663. (org-ascii--fill-string
  664. (concat (format title-fmt reference)
  665. " "
  666. (org-export-data caption info))
  667. (org-ascii--current-text-width element info) info)))))
  668. (defun org-ascii--build-toc (info &optional n keyword local)
  669. "Return a table of contents.
  670. INFO is a plist used as a communication channel.
  671. Optional argument N, when non-nil, is an integer specifying the
  672. depth of the table.
  673. Optional argument KEYWORD specifies the TOC keyword, if any, from
  674. which the table of contents generation has been initiated.
  675. When optional argument LOCAL is non-nil, build a table of
  676. contents according to the current headline."
  677. (concat
  678. (unless local
  679. (let ((title (org-ascii--translate "Table of Contents" info)))
  680. (concat title "\n"
  681. (make-string
  682. (string-width title)
  683. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  684. "\n\n")))
  685. (let ((text-width
  686. (if keyword (org-ascii--current-text-width keyword info)
  687. (- (plist-get info :ascii-text-width)
  688. (plist-get info :ascii-global-margin)))))
  689. (mapconcat
  690. (lambda (headline)
  691. (let* ((level (org-export-get-relative-level headline info))
  692. (indent (* (1- level) 3)))
  693. (concat
  694. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  695. (org-ascii--build-title
  696. headline info (- text-width indent) nil
  697. (or (not (plist-get info :with-tags))
  698. (eq (plist-get info :with-tags) 'not-in-toc))
  699. 'toc))))
  700. (org-export-collect-headlines info n (and local keyword)) "\n"))))
  701. (defun org-ascii--list-listings (keyword info)
  702. "Return a list of listings.
  703. KEYWORD is the keyword that initiated the list of listings
  704. generation. INFO is a plist used as a communication channel."
  705. (let ((title (org-ascii--translate "List of Listings" info)))
  706. (concat
  707. title "\n"
  708. (make-string (string-width title)
  709. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  710. "\n\n"
  711. (let ((text-width
  712. (if keyword (org-ascii--current-text-width keyword info)
  713. (- (plist-get info :ascii-text-width)
  714. (plist-get info :ascii-global-margin))))
  715. ;; Use a counter instead of retrieving ordinal of each
  716. ;; src-block.
  717. (count 0))
  718. (mapconcat
  719. (lambda (src-block)
  720. ;; Store initial text so its length can be computed. This is
  721. ;; used to properly align caption right to it in case of
  722. ;; filling (like contents of a description list item).
  723. (let* ((initial-text
  724. (format (org-ascii--translate "Listing %d:" info)
  725. (incf count)))
  726. (initial-width (string-width initial-text)))
  727. (concat
  728. initial-text " "
  729. (org-trim
  730. (org-ascii--indent-string
  731. (org-ascii--fill-string
  732. ;; Use short name in priority, if available.
  733. (let ((caption (or (org-export-get-caption src-block t)
  734. (org-export-get-caption src-block))))
  735. (org-export-data caption info))
  736. (- text-width initial-width) info)
  737. initial-width)))))
  738. (org-export-collect-listings info) "\n")))))
  739. (defun org-ascii--list-tables (keyword info)
  740. "Return a list of tables.
  741. KEYWORD is the keyword that initiated the list of tables
  742. generation. INFO is a plist used as a communication channel."
  743. (let ((title (org-ascii--translate "List of Tables" info)))
  744. (concat
  745. title "\n"
  746. (make-string (string-width title)
  747. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  748. "\n\n"
  749. (let ((text-width
  750. (if keyword (org-ascii--current-text-width keyword info)
  751. (- (plist-get info :ascii-text-width)
  752. (plist-get info :ascii-global-margin))))
  753. ;; Use a counter instead of retrieving ordinal of each
  754. ;; src-block.
  755. (count 0))
  756. (mapconcat
  757. (lambda (table)
  758. ;; Store initial text so its length can be computed. This is
  759. ;; used to properly align caption right to it in case of
  760. ;; filling (like contents of a description list item).
  761. (let* ((initial-text
  762. (format (org-ascii--translate "Table %d:" info)
  763. (incf count)))
  764. (initial-width (string-width initial-text)))
  765. (concat
  766. initial-text " "
  767. (org-trim
  768. (org-ascii--indent-string
  769. (org-ascii--fill-string
  770. ;; Use short name in priority, if available.
  771. (let ((caption (or (org-export-get-caption table t)
  772. (org-export-get-caption table))))
  773. (org-export-data caption info))
  774. (- text-width initial-width) info)
  775. initial-width)))))
  776. (org-export-collect-tables info) "\n")))))
  777. (defun org-ascii--unique-links (element info)
  778. "Return a list of unique link references in ELEMENT.
  779. ELEMENT is either a headline element or a section element. INFO
  780. is a plist used as a communication channel."
  781. (let* (seen
  782. (unique-link-p
  783. (function
  784. ;; Return LINK if it wasn't referenced so far, or nil.
  785. ;; Update SEEN links along the way.
  786. (lambda (link)
  787. (let ((footprint
  788. ;; Normalize description in footprints.
  789. (cons (org-element-property :raw-link link)
  790. (let ((contents (org-element-contents link)))
  791. (and contents
  792. (replace-regexp-in-string
  793. "[ \r\t\n]+" " "
  794. (org-trim
  795. (org-element-interpret-data contents))))))))
  796. ;; Ignore LINK if it hasn't been translated already.
  797. ;; It can happen if it is located in an affiliated
  798. ;; keyword that was ignored.
  799. (when (and (org-string-nw-p
  800. (gethash link (plist-get info :exported-data)))
  801. (not (member footprint seen)))
  802. (push footprint seen) link)))))
  803. ;; If at a section, find parent headline, if any, in order to
  804. ;; count links that might be in the title.
  805. (headline
  806. (if (eq (org-element-type element) 'headline) element
  807. (or (org-export-get-parent-headline element) element))))
  808. ;; Get all links in HEADLINE.
  809. (org-element-map headline 'link
  810. (lambda (l) (funcall unique-link-p l)) info nil nil t)))
  811. (defun org-ascii--describe-links (links width info)
  812. "Return a string describing a list of links.
  813. LINKS is a list of link type objects, as returned by
  814. `org-ascii--unique-links'. WIDTH is the text width allowed for
  815. the output string. INFO is a plist used as a communication
  816. channel."
  817. (mapconcat
  818. (lambda (link)
  819. (let ((type (org-element-property :type link))
  820. (anchor (let ((desc (org-element-contents link)))
  821. (if desc (org-export-data desc info)
  822. (org-element-property :raw-link link)))))
  823. (cond
  824. ;; Coderefs, radio links and fuzzy links are ignored.
  825. ((member type '("coderef" "radio" "fuzzy")) nil)
  826. ;; Id and custom-id links: Headlines refer to their numbering.
  827. ((member type '("custom-id" "id"))
  828. (let ((dest (org-export-resolve-id-link link info)))
  829. (concat
  830. (org-ascii--fill-string
  831. (format
  832. "[%s] %s"
  833. anchor
  834. (if (stringp dest) ; External file.
  835. dest
  836. (format
  837. (org-ascii--translate "See section %s" info)
  838. (if (org-export-numbered-headline-p dest info)
  839. (mapconcat #'number-to-string
  840. (org-export-get-headline-number dest info)
  841. ".")
  842. (org-export-data (org-element-property :title dest) info)))))
  843. width info)
  844. "\n\n")))
  845. ;; Do not add a link that cannot be resolved and doesn't have
  846. ;; any description: destination is already visible in the
  847. ;; paragraph.
  848. ((not (org-element-contents link)) nil)
  849. ;; Do not add a link already handled by custom export
  850. ;; functions.
  851. ((let ((protocol (nth 2 (assoc type org-link-protocols)))
  852. (path (org-element-property :path link)))
  853. (and (functionp protocol)
  854. (funcall protocol (org-link-unescape path) anchor 'ascii)))
  855. nil)
  856. (t
  857. (concat
  858. (org-ascii--fill-string
  859. (format "[%s] %s" anchor (org-element-property :raw-link link))
  860. width info)
  861. "\n\n")))))
  862. links ""))
  863. (defun org-ascii--checkbox (item info)
  864. "Return checkbox string for ITEM or nil.
  865. INFO is a plist used as a communication channel."
  866. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  867. (case (org-element-property :checkbox item)
  868. (on (if utf8p "☑ " "[X] "))
  869. (off (if utf8p "☐ " "[ ] "))
  870. (trans (if utf8p "☒ " "[-] ")))))
  871. ;;; Template
  872. (defun org-ascii-template--document-title (info)
  873. "Return document title, as a string.
  874. INFO is a plist used as a communication channel."
  875. (let* ((text-width (plist-get info :ascii-text-width))
  876. ;; Links in the title will not be resolved later, so we make
  877. ;; sure their path is located right after them.
  878. (info (org-combine-plists info '(:ascii-links-to-notes nil)))
  879. (with-title (plist-get info :with-title))
  880. (title (org-export-data
  881. (when with-title (plist-get info :title)) info))
  882. (subtitle (org-export-data
  883. (when with-title (plist-get info :subtitle)) info))
  884. (author (and (plist-get info :with-author)
  885. (let ((auth (plist-get info :author)))
  886. (and auth (org-export-data auth info)))))
  887. (email (and (plist-get info :with-email)
  888. (org-export-data (plist-get info :email) info)))
  889. (date (and (plist-get info :with-date)
  890. (org-export-data (org-export-get-date info) info))))
  891. ;; There are two types of title blocks depending on the presence
  892. ;; of a title to display.
  893. (if (string= title "")
  894. ;; Title block without a title. DATE is positioned at the top
  895. ;; right of the document, AUTHOR to the top left and EMAIL
  896. ;; just below.
  897. (cond
  898. ((and (org-string-nw-p date) (org-string-nw-p author))
  899. (concat
  900. author
  901. (make-string (- text-width (string-width date) (string-width author))
  902. ?\s)
  903. date
  904. (when (org-string-nw-p email) (concat "\n" email))
  905. "\n\n\n"))
  906. ((and (org-string-nw-p date) (org-string-nw-p email))
  907. (concat
  908. email
  909. (make-string (- text-width (string-width date) (string-width email))
  910. ?\s)
  911. date "\n\n\n"))
  912. ((org-string-nw-p date)
  913. (concat
  914. (org-ascii--justify-lines date text-width 'right)
  915. "\n\n\n"))
  916. ((and (org-string-nw-p author) (org-string-nw-p email))
  917. (concat author "\n" email "\n\n\n"))
  918. ((org-string-nw-p author) (concat author "\n\n\n"))
  919. ((org-string-nw-p email) (concat email "\n\n\n")))
  920. ;; Title block with a title. Document's TITLE, along with the
  921. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  922. ;; centered. Date is just below, also centered.
  923. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  924. ;; Format TITLE. It may be filled if it is too wide,
  925. ;; that is wider than the two thirds of the total width.
  926. (title-len (min (apply #'max
  927. (mapcar #'length
  928. (org-split-string
  929. (concat title "\n" subtitle) "\n")))
  930. (/ (* 2 text-width) 3)))
  931. (formatted-title (org-ascii--fill-string title title-len info))
  932. (formatted-subtitle (when (org-string-nw-p subtitle)
  933. (org-ascii--fill-string subtitle title-len info)))
  934. (line
  935. (make-string
  936. (min (+ (max title-len
  937. (string-width (or author ""))
  938. (string-width (or email "")))
  939. 2)
  940. text-width) (if utf8p ?━ ?_))))
  941. (org-ascii--justify-lines
  942. (concat line "\n"
  943. (unless utf8p "\n")
  944. (upcase formatted-title)
  945. (and formatted-subtitle (concat "\n" formatted-subtitle))
  946. (cond
  947. ((and (org-string-nw-p author) (org-string-nw-p email))
  948. (concat "\n\n" author "\n" email))
  949. ((org-string-nw-p author) (concat "\n\n" author))
  950. ((org-string-nw-p email) (concat "\n\n" email)))
  951. "\n" line
  952. (when (org-string-nw-p date) (concat "\n\n\n" date))
  953. "\n\n\n") text-width 'center)))))
  954. (defun org-ascii-inner-template (contents info)
  955. "Return complete document string after ASCII conversion.
  956. CONTENTS is the transcoded contents string. INFO is a plist
  957. holding export options."
  958. (org-element-normalize-string
  959. (let ((global-margin (plist-get info :ascii-global-margin)))
  960. (org-ascii--indent-string
  961. (concat
  962. ;; 1. Document's body.
  963. contents
  964. ;; 2. Footnote definitions.
  965. (let ((definitions (org-export-collect-footnote-definitions info))
  966. ;; Insert full links right inside the footnote definition
  967. ;; as they have no chance to be inserted later.
  968. (info (org-combine-plists info '(:ascii-links-to-notes nil))))
  969. (when definitions
  970. (concat
  971. "\n\n\n"
  972. (let ((title (org-ascii--translate "Footnotes" info)))
  973. (concat
  974. title "\n"
  975. (make-string
  976. (string-width title)
  977. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  978. "\n\n"
  979. (let ((text-width (- (plist-get info :ascii-text-width)
  980. global-margin)))
  981. (mapconcat
  982. (lambda (ref)
  983. (let ((id (format "[%s] " (car ref))))
  984. ;; Distinguish between inline definitions and
  985. ;; full-fledged definitions.
  986. (org-trim
  987. (let ((def (nth 2 ref)))
  988. (if (org-element-map def org-element-all-elements
  989. #'identity info 'first-match)
  990. ;; Full-fledged definition: footnote ID is
  991. ;; inserted inside the first parsed
  992. ;; paragraph (FIRST), if any, to be sure
  993. ;; filling will take it into consideration.
  994. (let ((first (car (org-element-contents def))))
  995. (if (not (eq (org-element-type first) 'paragraph))
  996. (concat id "\n" (org-export-data def info))
  997. (push id (nthcdr 2 first))
  998. (org-export-data def info)))
  999. ;; Fill paragraph once footnote ID is inserted
  1000. ;; in order to have a correct length for first
  1001. ;; line.
  1002. (org-ascii--fill-string
  1003. (concat id (org-export-data def info))
  1004. text-width info))))))
  1005. definitions "\n\n"))))))
  1006. global-margin))))
  1007. (defun org-ascii-template (contents info)
  1008. "Return complete document string after ASCII conversion.
  1009. CONTENTS is the transcoded contents string. INFO is a plist
  1010. holding export options."
  1011. (let ((global-margin (plist-get info :ascii-global-margin)))
  1012. (concat
  1013. ;; Build title block.
  1014. (org-ascii--indent-string
  1015. (concat (org-ascii-template--document-title info)
  1016. ;; 2. Table of contents.
  1017. (let ((depth (plist-get info :with-toc)))
  1018. (when depth
  1019. (concat
  1020. (org-ascii--build-toc info (and (wholenump depth) depth))
  1021. "\n\n\n"))))
  1022. global-margin)
  1023. ;; Document's body.
  1024. contents
  1025. ;; Creator. Justify it to the bottom right.
  1026. (and (plist-get info :with-creator)
  1027. (org-ascii--indent-string
  1028. (let ((text-width
  1029. (- (plist-get info :ascii-text-width) global-margin)))
  1030. (concat
  1031. "\n\n\n"
  1032. (org-ascii--fill-string
  1033. (plist-get info :creator) text-width info 'right)))
  1034. global-margin)))))
  1035. (defun org-ascii--translate (s info)
  1036. "Translate string S according to specified language and charset.
  1037. INFO is a plist used as a communication channel."
  1038. (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
  1039. (org-export-translate s charset info)))
  1040. ;;; Transcode Functions
  1041. ;;;; Bold
  1042. (defun org-ascii-bold (_bold contents _info)
  1043. "Transcode BOLD from Org to ASCII.
  1044. CONTENTS is the text with bold markup. INFO is a plist holding
  1045. contextual information."
  1046. (format "*%s*" contents))
  1047. ;;;; Center Block
  1048. (defun org-ascii-center-block (_center-block contents _info)
  1049. "Transcode a CENTER-BLOCK element from Org to ASCII.
  1050. CONTENTS holds the contents of the block. INFO is a plist
  1051. holding contextual information."
  1052. ;; Center has already been taken care of at a lower level, so
  1053. ;; there's nothing left to do.
  1054. contents)
  1055. ;;;; Clock
  1056. (defun org-ascii-clock (clock _contents info)
  1057. "Transcode a CLOCK object from Org to ASCII.
  1058. CONTENTS is nil. INFO is a plist holding contextual
  1059. information."
  1060. (org-ascii--justify-element
  1061. (concat org-clock-string " "
  1062. (org-timestamp-translate (org-element-property :value clock))
  1063. (let ((time (org-element-property :duration clock)))
  1064. (and time
  1065. (concat " => "
  1066. (apply 'format
  1067. "%2s:%02s"
  1068. (org-split-string time ":"))))))
  1069. clock info))
  1070. ;;;; Code
  1071. (defun org-ascii-code (code _contents info)
  1072. "Return a CODE object from Org to ASCII.
  1073. CONTENTS is nil. INFO is a plist holding contextual
  1074. information."
  1075. (format (plist-get info :ascii-verbatim-format)
  1076. (org-element-property :value code)))
  1077. ;;;; Drawer
  1078. (defun org-ascii-drawer (drawer contents info)
  1079. "Transcode a DRAWER element from Org to ASCII.
  1080. CONTENTS holds the contents of the block. INFO is a plist
  1081. holding contextual information."
  1082. (let ((name (org-element-property :drawer-name drawer))
  1083. (width (org-ascii--current-text-width drawer info)))
  1084. (funcall (plist-get info :ascii-format-drawer-function)
  1085. name contents width)))
  1086. ;;;; Dynamic Block
  1087. (defun org-ascii-dynamic-block (_dynamic-block contents _info)
  1088. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  1089. CONTENTS holds the contents of the block. INFO is a plist
  1090. holding contextual information."
  1091. contents)
  1092. ;;;; Entity
  1093. (defun org-ascii-entity (entity _contents info)
  1094. "Transcode an ENTITY object from Org to ASCII.
  1095. CONTENTS are the definition itself. INFO is a plist holding
  1096. contextual information."
  1097. (org-element-property
  1098. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  1099. entity))
  1100. ;;;; Example Block
  1101. (defun org-ascii-example-block (example-block _contents info)
  1102. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  1103. CONTENTS is nil. INFO is a plist holding contextual information."
  1104. (org-ascii--justify-element
  1105. (org-ascii--box-string
  1106. (org-export-format-code-default example-block info) info)
  1107. example-block info))
  1108. ;;;; Export Snippet
  1109. (defun org-ascii-export-snippet (export-snippet _contents _info)
  1110. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  1111. CONTENTS is nil. INFO is a plist holding contextual information."
  1112. (when (eq (org-export-snippet-backend export-snippet) 'ascii)
  1113. (org-element-property :value export-snippet)))
  1114. ;;;; Export Block
  1115. (defun org-ascii-export-block (export-block _contents info)
  1116. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  1117. CONTENTS is nil. INFO is a plist holding contextual information."
  1118. (when (string= (org-element-property :type export-block) "ASCII")
  1119. (org-ascii--justify-element
  1120. (org-element-property :value export-block) export-block info)))
  1121. ;;;; Fixed Width
  1122. (defun org-ascii-fixed-width (fixed-width _contents info)
  1123. "Transcode a FIXED-WIDTH element from Org to ASCII.
  1124. CONTENTS is nil. INFO is a plist holding contextual information."
  1125. (org-ascii--justify-element
  1126. (org-ascii--box-string
  1127. (org-remove-indentation
  1128. (org-element-property :value fixed-width)) info)
  1129. fixed-width info))
  1130. ;;;; Footnote Definition
  1131. ;; Footnote Definitions are ignored. They are compiled at the end of
  1132. ;; the document, by `org-ascii-inner-template'.
  1133. ;;;; Footnote Reference
  1134. (defun org-ascii-footnote-reference (footnote-reference _contents info)
  1135. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  1136. CONTENTS is nil. INFO is a plist holding contextual information."
  1137. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  1138. ;;;; Headline
  1139. (defun org-ascii-headline (headline contents info)
  1140. "Transcode a HEADLINE element from Org to ASCII.
  1141. CONTENTS holds the contents of the headline. INFO is a plist
  1142. holding contextual information."
  1143. ;; Don't export footnote section, which will be handled at the end
  1144. ;; of the template.
  1145. (unless (org-element-property :footnote-section-p headline)
  1146. (let* ((low-level-rank (org-export-low-level-p headline info))
  1147. (width (org-ascii--current-text-width headline info))
  1148. ;; Blank lines between headline and its contents.
  1149. ;; `org-ascii-headline-spacing', when set, overwrites
  1150. ;; original buffer's spacing.
  1151. (pre-blanks
  1152. (make-string
  1153. (or (car (plist-get info :ascii-headline-spacing))
  1154. (org-element-property :pre-blank headline))
  1155. ?\n))
  1156. ;; Even if HEADLINE has no section, there might be some
  1157. ;; links in its title that we shouldn't forget to describe.
  1158. (links
  1159. (unless (or (eq (caar (org-element-contents headline)) 'section))
  1160. (let ((title (org-element-property :title headline)))
  1161. (when (consp title)
  1162. (org-ascii--describe-links
  1163. (org-ascii--unique-links title info) width info))))))
  1164. ;; Deep subtree: export it as a list item.
  1165. (if low-level-rank
  1166. (concat
  1167. ;; Bullet.
  1168. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  1169. (plist-get info :ascii-bullets)))))
  1170. (char-to-string
  1171. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  1172. " "
  1173. ;; Title.
  1174. (org-ascii--build-title headline info width) "\n"
  1175. ;; Contents, indented by length of bullet.
  1176. pre-blanks
  1177. (org-ascii--indent-string
  1178. (concat contents
  1179. (when (org-string-nw-p links) (concat "\n\n" links)))
  1180. 2))
  1181. ;; Else: Standard headline.
  1182. (concat
  1183. (org-ascii--build-title headline info width 'underline)
  1184. "\n" pre-blanks
  1185. (concat (when (org-string-nw-p links) links) contents))))))
  1186. ;;;; Horizontal Rule
  1187. (defun org-ascii-horizontal-rule (horizontal-rule _contents info)
  1188. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  1189. CONTENTS is nil. INFO is a plist holding contextual
  1190. information."
  1191. (let ((text-width (org-ascii--current-text-width horizontal-rule info))
  1192. (spec-width
  1193. (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  1194. (org-ascii--justify-lines
  1195. (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
  1196. (string-to-number spec-width)
  1197. text-width)
  1198. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
  1199. text-width 'center)))
  1200. ;;;; Inline Src Block
  1201. (defun org-ascii-inline-src-block (inline-src-block _contents info)
  1202. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  1203. CONTENTS holds the contents of the item. INFO is a plist holding
  1204. contextual information."
  1205. (format (plist-get info :ascii-verbatim-format)
  1206. (org-element-property :value inline-src-block)))
  1207. ;;;; Inlinetask
  1208. (defun org-ascii-format-inlinetask-default
  1209. (_todo _type _priority _name _tags contents width inlinetask info)
  1210. "Format an inline task element for ASCII export.
  1211. See `org-ascii-format-inlinetask-function' for a description
  1212. of the parameters."
  1213. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1214. (width (or width (plist-get info :ascii-inlinetask-width))))
  1215. (org-ascii--indent-string
  1216. (concat
  1217. ;; Top line, with an additional blank line if not in UTF-8.
  1218. (make-string width (if utf8p ?━ ?_)) "\n"
  1219. (unless utf8p (concat (make-string width ? ) "\n"))
  1220. ;; Add title. Fill it if wider than inlinetask.
  1221. (let ((title (org-ascii--build-title inlinetask info width)))
  1222. (if (<= (string-width title) width) title
  1223. (org-ascii--fill-string title width info)))
  1224. "\n"
  1225. ;; If CONTENTS is not empty, insert it along with
  1226. ;; a separator.
  1227. (when (org-string-nw-p contents)
  1228. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1229. ;; Bottom line.
  1230. (make-string width (if utf8p ?━ ?_)))
  1231. ;; Flush the inlinetask to the right.
  1232. (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
  1233. (if (not (org-export-get-parent-headline inlinetask)) 0
  1234. (plist-get info :ascii-inner-margin))
  1235. (org-ascii--current-text-width inlinetask info)))))
  1236. (defun org-ascii-inlinetask (inlinetask contents info)
  1237. "Transcode an INLINETASK element from Org to ASCII.
  1238. CONTENTS holds the contents of the block. INFO is a plist
  1239. holding contextual information."
  1240. (let ((width (org-ascii--current-text-width inlinetask info)))
  1241. (funcall (plist-get info :ascii-format-inlinetask-function)
  1242. ;; todo.
  1243. (and (plist-get info :with-todo-keywords)
  1244. (let ((todo (org-element-property
  1245. :todo-keyword inlinetask)))
  1246. (and todo (org-export-data todo info))))
  1247. ;; todo-type
  1248. (org-element-property :todo-type inlinetask)
  1249. ;; priority
  1250. (and (plist-get info :with-priority)
  1251. (org-element-property :priority inlinetask))
  1252. ;; title
  1253. (org-export-data (org-element-property :title inlinetask) info)
  1254. ;; tags
  1255. (and (plist-get info :with-tags)
  1256. (org-element-property :tags inlinetask))
  1257. ;; contents and width
  1258. contents width inlinetask info)))
  1259. ;;;; Italic
  1260. (defun org-ascii-italic (_italic contents _info)
  1261. "Transcode italic from Org to ASCII.
  1262. CONTENTS is the text with italic markup. INFO is a plist holding
  1263. contextual information."
  1264. (format "/%s/" contents))
  1265. ;;;; Item
  1266. (defun org-ascii-item (item contents info)
  1267. "Transcode an ITEM element from Org to ASCII.
  1268. CONTENTS holds the contents of the item. INFO is a plist holding
  1269. contextual information."
  1270. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1271. (checkbox (org-ascii--checkbox item info))
  1272. (list-type (org-element-property :type (org-export-get-parent item)))
  1273. (bullet
  1274. ;; First parent of ITEM is always the plain-list. Get
  1275. ;; `:type' property from it.
  1276. (org-list-bullet-string
  1277. (case list-type
  1278. (descriptive
  1279. (concat checkbox
  1280. (org-export-data (org-element-property :tag item) info)
  1281. ": "))
  1282. (ordered
  1283. ;; Return correct number for ITEM, paying attention to
  1284. ;; counters.
  1285. (let* ((struct (org-element-property :structure item))
  1286. (bul (org-element-property :bullet item))
  1287. (num (number-to-string
  1288. (car (last (org-list-get-item-number
  1289. (org-element-property :begin item)
  1290. struct
  1291. (org-list-prevs-alist struct)
  1292. (org-list-parents-alist struct)))))))
  1293. (replace-regexp-in-string "[0-9]+" num bul)))
  1294. (t (let ((bul (org-element-property :bullet item)))
  1295. ;; Change bullets into more visible form if UTF-8 is active.
  1296. (if (not utf8p) bul
  1297. (replace-regexp-in-string
  1298. "-" "•"
  1299. (replace-regexp-in-string
  1300. "+" "⁃"
  1301. (replace-regexp-in-string "*" "‣" bul))))))))))
  1302. (concat
  1303. bullet
  1304. (unless (eq list-type 'descriptive) checkbox)
  1305. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1306. ;; already taken care of at the paragraph level so they don't
  1307. ;; interfere with indentation.
  1308. (let ((contents (org-ascii--indent-string contents (string-width bullet))))
  1309. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1310. (org-trim contents)
  1311. (concat "\n" contents))))))
  1312. ;;;; Keyword
  1313. (defun org-ascii-keyword (keyword _contents info)
  1314. "Transcode a KEYWORD element from Org to ASCII.
  1315. CONTENTS is nil. INFO is a plist holding contextual
  1316. information."
  1317. (let ((key (org-element-property :key keyword))
  1318. (value (org-element-property :value keyword)))
  1319. (cond
  1320. ((string= key "ASCII") (org-ascii--justify-element value keyword info))
  1321. ((string= key "TOC")
  1322. (org-ascii--justify-element
  1323. (let ((case-fold-search t))
  1324. (cond
  1325. ((org-string-match-p "\\<headlines\\>" value)
  1326. (let ((depth (and (string-match "\\<[0-9]+\\>" value)
  1327. (string-to-number (match-string 0 value))))
  1328. (localp (org-string-match-p "\\<local\\>" value)))
  1329. (org-ascii--build-toc info depth keyword localp)))
  1330. ((org-string-match-p "\\<tables\\>" value)
  1331. (org-ascii--list-tables keyword info))
  1332. ((org-string-match-p "\\<listings\\>" value)
  1333. (org-ascii--list-listings keyword info))))
  1334. keyword info)))))
  1335. ;;;; Latex Environment
  1336. (defun org-ascii-latex-environment (latex-environment _contents info)
  1337. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1338. CONTENTS is nil. INFO is a plist holding contextual
  1339. information."
  1340. (when (plist-get info :with-latex)
  1341. (org-ascii--justify-element
  1342. (org-remove-indentation (org-element-property :value latex-environment))
  1343. latex-environment info)))
  1344. ;;;; Latex Fragment
  1345. (defun org-ascii-latex-fragment (latex-fragment _contents info)
  1346. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1347. CONTENTS is nil. INFO is a plist holding contextual
  1348. information."
  1349. (when (plist-get info :with-latex)
  1350. (org-element-property :value latex-fragment)))
  1351. ;;;; Line Break
  1352. (defun org-ascii-line-break (_line-break _contents _info)
  1353. "Transcode a LINE-BREAK object from Org to ASCII.
  1354. CONTENTS is nil. INFO is a plist holding contextual
  1355. information." hard-newline)
  1356. ;;;; Link
  1357. (defun org-ascii-link (link desc info)
  1358. "Transcode a LINK object from Org to ASCII.
  1359. DESC is the description part of the link, or the empty string.
  1360. INFO is a plist holding contextual information."
  1361. (let ((type (org-element-property :type link)))
  1362. (cond
  1363. ((org-export-custom-protocol-maybe link desc 'ascii))
  1364. ((string= type "coderef")
  1365. (let ((ref (org-element-property :path link)))
  1366. (format (org-export-get-coderef-format ref desc)
  1367. (org-export-resolve-coderef ref info))))
  1368. ;; Do not apply a special syntax on radio links. Though, use
  1369. ;; transcoded target's contents as output.
  1370. ((string= type "radio") desc)
  1371. ;; Do not apply a special syntax on fuzzy links pointing to
  1372. ;; targets.
  1373. ((string= type "fuzzy")
  1374. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1375. (if (org-string-nw-p desc) desc
  1376. (when destination
  1377. (let ((number
  1378. (org-export-get-ordinal
  1379. destination info nil 'org-ascii--has-caption-p)))
  1380. (if number
  1381. (if (atom number) (number-to-string number)
  1382. (mapconcat #'number-to-string number "."))
  1383. ;; Unnumbered headline.
  1384. (when (eq 'headline (org-element-type destination))
  1385. (format "[%s]"
  1386. (org-export-data
  1387. (org-element-property :title destination)
  1388. info)))))))))
  1389. (t
  1390. (let ((raw-link (org-element-property :raw-link link)))
  1391. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1392. (concat (format "[%s]" desc)
  1393. (and (not (plist-get info :ascii-links-to-notes))
  1394. (format " (%s)" raw-link)))))))))
  1395. ;;;; Node Properties
  1396. (defun org-ascii-node-property (node-property _contents _info)
  1397. "Transcode a NODE-PROPERTY element from Org to ASCII.
  1398. CONTENTS is nil. INFO is a plist holding contextual
  1399. information."
  1400. (format "%s:%s"
  1401. (org-element-property :key node-property)
  1402. (let ((value (org-element-property :value node-property)))
  1403. (if value (concat " " value) ""))))
  1404. ;;;; Paragraph
  1405. (defun org-ascii-paragraph (paragraph contents info)
  1406. "Transcode a PARAGRAPH element from Org to ASCII.
  1407. CONTENTS is the contents of the paragraph, as a string. INFO is
  1408. the plist used as a communication channel."
  1409. (org-ascii--justify-element
  1410. (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
  1411. (if (not (wholenump indented-line-width)) contents
  1412. (concat
  1413. ;; Do not indent first paragraph in a section.
  1414. (unless (and (not (org-export-get-previous-element paragraph info))
  1415. (eq (org-element-type (org-export-get-parent paragraph))
  1416. 'section))
  1417. (make-string indented-line-width ?\s))
  1418. (replace-regexp-in-string "\\`[ \t]+" "" contents))))
  1419. paragraph info))
  1420. ;;;; Plain List
  1421. (defun org-ascii-plain-list (plain-list contents info)
  1422. "Transcode a PLAIN-LIST element from Org to ASCII.
  1423. CONTENTS is the contents of the list. INFO is a plist holding
  1424. contextual information."
  1425. (let ((margin (plist-get info :ascii-list-margin)))
  1426. (if (or (< margin 1)
  1427. (eq (org-element-type (org-export-get-parent plain-list)) 'item))
  1428. contents
  1429. (org-ascii--indent-string contents margin))))
  1430. ;;;; Plain Text
  1431. (defun org-ascii-plain-text (text info)
  1432. "Transcode a TEXT string from Org to ASCII.
  1433. INFO is a plist used as a communication channel."
  1434. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1435. (when (and utf8p (plist-get info :with-smart-quotes))
  1436. (setq text (org-export-activate-smart-quotes text :utf-8 info)))
  1437. (if (not (plist-get info :with-special-strings)) text
  1438. (setq text (replace-regexp-in-string "\\\\-" "" text))
  1439. (if (not utf8p) text
  1440. ;; Usual replacements in utf-8 with proper option set.
  1441. (replace-regexp-in-string
  1442. "\\.\\.\\." "…"
  1443. (replace-regexp-in-string
  1444. "--" "–"
  1445. (replace-regexp-in-string "---" "—" text)))))))
  1446. ;;;; Planning
  1447. (defun org-ascii-planning (planning _contents info)
  1448. "Transcode a PLANNING element from Org to ASCII.
  1449. CONTENTS is nil. INFO is a plist used as a communication
  1450. channel."
  1451. (org-ascii--justify-element
  1452. (mapconcat
  1453. #'identity
  1454. (delq nil
  1455. (list (let ((closed (org-element-property :closed planning)))
  1456. (when closed
  1457. (concat org-closed-string " "
  1458. (org-timestamp-translate closed))))
  1459. (let ((deadline (org-element-property :deadline planning)))
  1460. (when deadline
  1461. (concat org-deadline-string " "
  1462. (org-timestamp-translate deadline))))
  1463. (let ((scheduled (org-element-property :scheduled planning)))
  1464. (when scheduled
  1465. (concat org-scheduled-string " "
  1466. (org-timestamp-translate scheduled))))))
  1467. " ")
  1468. planning info))
  1469. ;;;; Property Drawer
  1470. (defun org-ascii-property-drawer (property-drawer contents info)
  1471. "Transcode a PROPERTY-DRAWER element from Org to ASCII.
  1472. CONTENTS holds the contents of the drawer. INFO is a plist
  1473. holding contextual information."
  1474. (and (org-string-nw-p contents)
  1475. (org-ascii--justify-element contents property-drawer info)))
  1476. ;;;; Quote Block
  1477. (defun org-ascii-quote-block (_quote-block contents info)
  1478. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1479. CONTENTS holds the contents of the block. INFO is a plist
  1480. holding contextual information."
  1481. (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
  1482. ;;;; Radio Target
  1483. (defun org-ascii-radio-target (_radio-target contents _info)
  1484. "Transcode a RADIO-TARGET object from Org to ASCII.
  1485. CONTENTS is the contents of the target. INFO is a plist holding
  1486. contextual information."
  1487. contents)
  1488. ;;;; Section
  1489. (defun org-ascii-section (section contents info)
  1490. "Transcode a SECTION element from Org to ASCII.
  1491. CONTENTS is the contents of the section. INFO is a plist holding
  1492. contextual information."
  1493. (org-ascii--indent-string
  1494. (concat
  1495. contents
  1496. (when (plist-get info :ascii-links-to-notes)
  1497. ;; Add list of links at the end of SECTION.
  1498. (let ((links (org-ascii--describe-links
  1499. (org-ascii--unique-links section info)
  1500. (org-ascii--current-text-width section info) info)))
  1501. ;; Separate list of links and section contents.
  1502. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1503. ;; Do not apply inner margin if parent headline is low level.
  1504. (let ((headline (org-export-get-parent-headline section)))
  1505. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1506. (plist-get info :ascii-inner-margin)))))
  1507. ;;;; Special Block
  1508. (defun org-ascii-special-block (_special-block contents _info)
  1509. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1510. CONTENTS holds the contents of the block. INFO is a plist
  1511. holding contextual information."
  1512. ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
  1513. ;; at a lower level. There is no other special block type to
  1514. ;; handle.
  1515. contents)
  1516. ;;;; Src Block
  1517. (defun org-ascii-src-block (src-block _contents info)
  1518. "Transcode a SRC-BLOCK element from Org to ASCII.
  1519. CONTENTS holds the contents of the item. INFO is a plist holding
  1520. contextual information."
  1521. (let ((caption (org-ascii--build-caption src-block info))
  1522. (caption-above-p (plist-get info :ascii-caption-above))
  1523. (code (org-export-format-code-default src-block info)))
  1524. (if (equal code "") ""
  1525. (org-ascii--justify-element
  1526. (concat
  1527. (and caption caption-above-p (concat caption "\n"))
  1528. (org-ascii--box-string code info)
  1529. (and caption (not caption-above-p) (concat "\n" caption)))
  1530. src-block info))))
  1531. ;;;; Statistics Cookie
  1532. (defun org-ascii-statistics-cookie (statistics-cookie _contents _info)
  1533. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1534. CONTENTS is nil. INFO is a plist holding contextual information."
  1535. (org-element-property :value statistics-cookie))
  1536. ;;;; Subscript
  1537. (defun org-ascii-subscript (subscript contents _info)
  1538. "Transcode a SUBSCRIPT object from Org to ASCII.
  1539. CONTENTS is the contents of the object. INFO is a plist holding
  1540. contextual information."
  1541. (if (org-element-property :use-brackets-p subscript)
  1542. (format "_{%s}" contents)
  1543. (format "_%s" contents)))
  1544. ;;;; Superscript
  1545. (defun org-ascii-superscript (superscript contents _info)
  1546. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1547. CONTENTS is the contents of the object. INFO is a plist holding
  1548. contextual information."
  1549. (if (org-element-property :use-brackets-p superscript)
  1550. (format "^{%s}" contents)
  1551. (format "^%s" contents)))
  1552. ;;;; Strike-through
  1553. (defun org-ascii-strike-through (_strike-through contents _info)
  1554. "Transcode STRIKE-THROUGH from Org to ASCII.
  1555. CONTENTS is text with strike-through markup. INFO is a plist
  1556. holding contextual information."
  1557. (format "+%s+" contents))
  1558. ;;;; Table
  1559. (defun org-ascii-table (table contents info)
  1560. "Transcode a TABLE element from Org to ASCII.
  1561. CONTENTS is the contents of the table. INFO is a plist holding
  1562. contextual information."
  1563. (let ((caption (org-ascii--build-caption table info))
  1564. (caption-above-p (plist-get info :ascii-caption-above)))
  1565. (org-ascii--justify-element
  1566. (concat
  1567. ;; Possibly add a caption string above.
  1568. (and caption caption-above-p (concat caption "\n"))
  1569. ;; Insert table. Note: "table.el" tables are left unmodified.
  1570. (cond ((eq (org-element-property :type table) 'org) contents)
  1571. ((and (plist-get info :ascii-table-use-ascii-art)
  1572. (eq (plist-get info :ascii-charset) 'utf-8)
  1573. (require 'ascii-art-to-unicode nil t))
  1574. (with-temp-buffer
  1575. (insert (org-remove-indentation
  1576. (org-element-property :value table)))
  1577. (goto-char (point-min))
  1578. (aa2u)
  1579. (goto-char (point-max))
  1580. (skip-chars-backward " \r\t\n")
  1581. (buffer-substring (point-min) (point))))
  1582. (t (org-remove-indentation (org-element-property :value table))))
  1583. ;; Possible add a caption string below.
  1584. (and (not caption-above-p) caption))
  1585. table info)))
  1586. ;;;; Table Cell
  1587. (defun org-ascii--table-cell-width (table-cell info)
  1588. "Return width of TABLE-CELL.
  1589. INFO is a plist used as a communication channel.
  1590. Width of a cell is determined either by a width cookie in the
  1591. same column as the cell, or by the maximum cell's length in that
  1592. column.
  1593. When `org-ascii-table-widen-columns' is non-nil, width cookies
  1594. are ignored."
  1595. (let* ((row (org-export-get-parent table-cell))
  1596. (table (org-export-get-parent row))
  1597. (col (let ((cells (org-element-contents row)))
  1598. (- (length cells) (length (memq table-cell cells)))))
  1599. (cache
  1600. (or (plist-get info :ascii-table-cell-width-cache)
  1601. (plist-get (setq info
  1602. (plist-put info :ascii-table-cell-width-cache
  1603. (make-hash-table :test 'equal)))
  1604. :ascii-table-cell-width-cache)))
  1605. (key (cons table col))
  1606. (widenp (plist-get info :ascii-table-widen-columns)))
  1607. (or (gethash key cache)
  1608. (puthash
  1609. key
  1610. (let ((cookie-width (org-export-table-cell-width table-cell info)))
  1611. (or (and (not widenp) cookie-width)
  1612. (let ((contents-width
  1613. (let ((max-width 0))
  1614. (org-element-map table 'table-row
  1615. (lambda (row)
  1616. (setq max-width
  1617. (max (string-width
  1618. (org-export-data
  1619. (org-element-contents
  1620. (elt (org-element-contents row) col))
  1621. info))
  1622. max-width)))
  1623. info)
  1624. max-width)))
  1625. (cond ((not cookie-width) contents-width)
  1626. (widenp (max cookie-width contents-width))
  1627. (t cookie-width)))))
  1628. cache))))
  1629. (defun org-ascii-table-cell (table-cell contents info)
  1630. "Transcode a TABLE-CELL object from Org to ASCII.
  1631. CONTENTS is the cell contents. INFO is a plist used as
  1632. a communication channel."
  1633. ;; Determine column width. When `org-ascii-table-widen-columns'
  1634. ;; is nil and some width cookie has set it, use that value.
  1635. ;; Otherwise, compute the maximum width among transcoded data of
  1636. ;; each cell in the column.
  1637. (let ((width (org-ascii--table-cell-width table-cell info)))
  1638. ;; When contents are too large, truncate them.
  1639. (unless (or (plist-get info :ascii-table-widen-columns)
  1640. (<= (string-width (or contents "")) width))
  1641. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1642. ;; Align contents correctly within the cell.
  1643. (let* ((indent-tabs-mode nil)
  1644. (data
  1645. (when contents
  1646. (org-ascii--justify-lines
  1647. contents width
  1648. (org-export-table-cell-alignment table-cell info)))))
  1649. (setq contents
  1650. (concat data
  1651. (make-string (- width (string-width (or data ""))) ?\s))))
  1652. ;; Return cell.
  1653. (concat (format " %s " contents)
  1654. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1655. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1656. ;;;; Table Row
  1657. (defun org-ascii-table-row (table-row contents info)
  1658. "Transcode a TABLE-ROW element from Org to ASCII.
  1659. CONTENTS is the row contents. INFO is a plist used as
  1660. a communication channel."
  1661. (when (eq (org-element-property :type table-row) 'standard)
  1662. (let ((build-hline
  1663. (function
  1664. (lambda (lcorner horiz vert rcorner)
  1665. (concat
  1666. (apply
  1667. 'concat
  1668. (org-element-map table-row 'table-cell
  1669. (lambda (cell)
  1670. (let ((width (org-ascii--table-cell-width cell info))
  1671. (borders (org-export-table-cell-borders cell info)))
  1672. (concat
  1673. ;; In order to know if CELL starts the row, do
  1674. ;; not compare it with the first cell in the
  1675. ;; row as there might be a special column.
  1676. ;; Instead, compare it with first exportable
  1677. ;; cell, obtained with `org-element-map'.
  1678. (when (and (memq 'left borders)
  1679. (eq (org-element-map table-row 'table-cell
  1680. 'identity info t)
  1681. cell))
  1682. lcorner)
  1683. (make-string (+ 2 width) (string-to-char horiz))
  1684. (cond
  1685. ((not (memq 'right borders)) nil)
  1686. ((eq (car (last (org-element-contents table-row))) cell)
  1687. rcorner)
  1688. (t vert)))))
  1689. info)) "\n"))))
  1690. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1691. (borders (org-export-table-cell-borders
  1692. (org-element-map table-row 'table-cell 'identity info t)
  1693. info)))
  1694. (concat (cond
  1695. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1696. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1697. (funcall build-hline "+" "-" "+" "+")))
  1698. ((memq 'above borders)
  1699. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1700. (funcall build-hline "+" "-" "+" "+"))))
  1701. (when (memq 'left borders) (if utf8p "│" "|"))
  1702. contents "\n"
  1703. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1704. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1705. (funcall build-hline "+" "-" "+" "+")))))))
  1706. ;;;; Timestamp
  1707. (defun org-ascii-timestamp (timestamp _contents info)
  1708. "Transcode a TIMESTAMP object from Org to ASCII.
  1709. CONTENTS is nil. INFO is a plist holding contextual information."
  1710. (org-ascii-plain-text (org-timestamp-translate timestamp) info))
  1711. ;;;; Underline
  1712. (defun org-ascii-underline (_underline contents _info)
  1713. "Transcode UNDERLINE from Org to ASCII.
  1714. CONTENTS is the text with underline markup. INFO is a plist
  1715. holding contextual information."
  1716. (format "_%s_" contents))
  1717. ;;;; Verbatim
  1718. (defun org-ascii-verbatim (verbatim _contents info)
  1719. "Return a VERBATIM object from Org to ASCII.
  1720. CONTENTS is nil. INFO is a plist holding contextual information."
  1721. (format (plist-get info :ascii-verbatim-format)
  1722. (org-element-property :value verbatim)))
  1723. ;;;; Verse Block
  1724. (defun org-ascii-verse-block (verse-block contents info)
  1725. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1726. CONTENTS is verse block contents. INFO is a plist holding
  1727. contextual information."
  1728. (org-ascii--indent-string
  1729. (org-ascii--justify-element contents verse-block info)
  1730. (plist-get info :ascii-quote-margin)))
  1731. ;;; Filters
  1732. (defun org-ascii-filter-headline-blank-lines (headline _backend info)
  1733. "Filter controlling number of blank lines after a headline.
  1734. HEADLINE is a string representing a transcoded headline. BACKEND
  1735. is symbol specifying back-end used for export. INFO is plist
  1736. containing the communication channel.
  1737. This function only applies to `ascii' back-end. See
  1738. `org-ascii-headline-spacing' for information."
  1739. (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
  1740. (if (not headline-spacing) headline
  1741. (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
  1742. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
  1743. (defun org-ascii-filter-paragraph-spacing (tree _backend info)
  1744. "Filter controlling number of blank lines between paragraphs.
  1745. TREE is the parse tree. BACKEND is the symbol specifying
  1746. back-end used for export. INFO is a plist used as
  1747. a communication channel.
  1748. See `org-ascii-paragraph-spacing' for information."
  1749. (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
  1750. (when (wholenump paragraph-spacing)
  1751. (org-element-map tree 'paragraph
  1752. (lambda (p)
  1753. (when (eq (org-element-type (org-export-get-next-element p info))
  1754. 'paragraph)
  1755. (org-element-put-property p :post-blank paragraph-spacing))))))
  1756. tree)
  1757. (defun org-ascii-filter-comment-spacing (tree _backend info)
  1758. "Filter removing blank lines between comments.
  1759. TREE is the parse tree. BACKEND is the symbol specifying
  1760. back-end used for export. INFO is a plist used as
  1761. a communication channel."
  1762. (org-element-map tree '(comment comment-block)
  1763. (lambda (c)
  1764. (when (memq (org-element-type (org-export-get-next-element c info))
  1765. '(comment comment-block))
  1766. (org-element-put-property c :post-blank 0))))
  1767. tree)
  1768. ;;; End-user functions
  1769. ;;;###autoload
  1770. (defun org-ascii-export-as-ascii
  1771. (&optional async subtreep visible-only body-only ext-plist)
  1772. "Export current buffer to a text buffer.
  1773. If narrowing is active in the current buffer, only export its
  1774. narrowed part.
  1775. If a region is active, export that region.
  1776. A non-nil optional argument ASYNC means the process should happen
  1777. asynchronously. The resulting buffer should be accessible
  1778. through the `org-export-stack' interface.
  1779. When optional argument SUBTREEP is non-nil, export the sub-tree
  1780. at point, extracting information from the headline properties
  1781. first.
  1782. When optional argument VISIBLE-ONLY is non-nil, don't export
  1783. contents of hidden elements.
  1784. When optional argument BODY-ONLY is non-nil, strip title and
  1785. table of contents from output.
  1786. EXT-PLIST, when provided, is a property list with external
  1787. parameters overriding Org default settings, but still inferior to
  1788. file-local settings.
  1789. Export is done in a buffer named \"*Org ASCII Export*\", which
  1790. will be displayed when `org-export-show-temporary-export-buffer'
  1791. is non-nil."
  1792. (interactive)
  1793. (org-export-to-buffer 'ascii "*Org ASCII Export*"
  1794. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  1795. ;;;###autoload
  1796. (defun org-ascii-export-to-ascii
  1797. (&optional async subtreep visible-only body-only ext-plist)
  1798. "Export current buffer to a text file.
  1799. If narrowing is active in the current buffer, only export its
  1800. narrowed part.
  1801. If a region is active, export that region.
  1802. A non-nil optional argument ASYNC means the process should happen
  1803. asynchronously. The resulting file should be accessible through
  1804. the `org-export-stack' interface.
  1805. When optional argument SUBTREEP is non-nil, export the sub-tree
  1806. at point, extracting information from the headline properties
  1807. first.
  1808. When optional argument VISIBLE-ONLY is non-nil, don't export
  1809. contents of hidden elements.
  1810. When optional argument BODY-ONLY is non-nil, strip title and
  1811. table of contents from output.
  1812. EXT-PLIST, when provided, is a property list with external
  1813. parameters overriding Org default settings, but still inferior to
  1814. file-local settings.
  1815. Return output file's name."
  1816. (interactive)
  1817. (let ((file (org-export-output-file-name ".txt" subtreep)))
  1818. (org-export-to-file 'ascii file
  1819. async subtreep visible-only body-only ext-plist)))
  1820. ;;;###autoload
  1821. (defun org-ascii-publish-to-ascii (plist filename pub-dir)
  1822. "Publish an Org file to ASCII.
  1823. FILENAME is the filename of the Org file to be published. PLIST
  1824. is the property list for the given project. PUB-DIR is the
  1825. publishing directory.
  1826. Return output file name."
  1827. (org-publish-org-to
  1828. 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
  1829. ;;;###autoload
  1830. (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
  1831. "Publish an Org file to Latin-1.
  1832. FILENAME is the filename of the Org file to be published. PLIST
  1833. is the property list for the given project. PUB-DIR is the
  1834. publishing directory.
  1835. Return output file name."
  1836. (org-publish-org-to
  1837. 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
  1838. ;;;###autoload
  1839. (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
  1840. "Publish an org file to UTF-8.
  1841. FILENAME is the filename of the Org file to be published. PLIST
  1842. is the property list for the given project. PUB-DIR is the
  1843. publishing directory.
  1844. Return output file name."
  1845. (org-publish-org-to
  1846. 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
  1847. (provide 'ox-ascii)
  1848. ;; Local variables:
  1849. ;; generated-autoload-file: "org-loaddefs.el"
  1850. ;; coding: utf-8
  1851. ;; End:
  1852. ;;; ox-ascii.el ends here