org-e-ascii.el 68 KB

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