ox-ascii.el 76 KB

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