ox-ascii.el 72 KB

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