ox-ascii.el 68 KB

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