ox-ascii.el 74 KB

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