ox-ascii.el 67 KB

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