org-e-ascii.el 66 KB

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