ox-ascii.el 69 KB

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