ox-ascii.el 73 KB

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