org-e-ascii.el 64 KB

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