ox-ascii.el 68 KB

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