ox-ascii.el 74 KB

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