ox-ascii.el 78 KB

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