ox-ascii.el 67 KB

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