org-e-ascii.el 67 KB

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