ox-ascii.el 75 KB

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