org-e-ascii.el 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  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 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 (org-export-numbered-headline-p element info)
  512. ;; All tests passed: build numbering string.
  513. (concat
  514. (mapconcat
  515. #'number-to-string
  516. (org-export-get-headline-number element info) ".")
  517. " ")))
  518. (text (org-export-secondary-string
  519. (org-element-property :title element) 'e-ascii info))
  520. (todo
  521. (and (plist-get info :with-todo-keywords)
  522. (let ((todo (org-element-property :todo-keyword element)))
  523. (and todo
  524. (concat (org-export-secondary-string todo 'e-ascii info)
  525. " ")))))
  526. (tags (and (not notags)
  527. (plist-get info :with-tags)
  528. (org-element-property :tags element)))
  529. (priority
  530. (and (plist-get info :with-priority)
  531. (concat (org-element-property :priority element) " ")))
  532. (first-part (concat numbers todo priority text)))
  533. (concat
  534. first-part
  535. ;; Align tags, if any.
  536. (when tags
  537. (format
  538. (format " %%%ds"
  539. (max (- text-width (1+ (length first-part))) (length tags)))
  540. tags))
  541. ;; Maybe underline text, if ELEMENT type is `headline' and an
  542. ;; underline character has been defined.
  543. (when (and underline headlinep)
  544. (let ((under-char
  545. (nth (1- (org-export-get-relative-level element info))
  546. (cdr (assq (plist-get info :ascii-charset)
  547. org-e-ascii-underline)))))
  548. (and under-char
  549. (concat "\n"
  550. (make-string (length first-part) under-char))))))))
  551. (defun org-e-ascii--build-caption (element info)
  552. "Return caption string for ELEMENT, if applicable.
  553. INFO is a plist used as a communication channel.
  554. The caption string contains the sequence number of ELEMENT if it
  555. has a name affiliated keyword, along with the real caption, if
  556. any. Return nil when ELEMENT has no affiliated caption or name
  557. keyword."
  558. (let ((caption (org-element-property :caption element))
  559. (name (org-element-property :name element)))
  560. (when (or caption name)
  561. ;; Get sequence number of current src-block among every
  562. ;; src-block with either a caption or a name.
  563. (let ((reference
  564. (org-export-get-ordinal
  565. element info nil
  566. (lambda (el) (or (org-element-property :caption el)
  567. (org-element-property :name el)))))
  568. (title-fmt (org-e-ascii--translate
  569. (case (org-element-type element)
  570. (table "Table %d: %s")
  571. (src-block "Listing %d: %s")) info)))
  572. (org-e-ascii--fill-string
  573. (format
  574. title-fmt reference
  575. (if (not caption) name
  576. (org-export-secondary-string (car caption) 'e-ascii info)))
  577. (org-e-ascii--current-text-width element info) info)))))
  578. (defun org-e-ascii--build-toc (info &optional n keyword)
  579. "Return a table of contents.
  580. INFO is a plist used as a communication channel.
  581. Optional argument N, when non-nil, is an integer specifying the
  582. depth of the table.
  583. Optional argument KEYWORD specifies the TOC keyword, if any, from
  584. which the table of contents generation has been initiated."
  585. (let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
  586. (concat
  587. title
  588. (make-string (1- (length title))
  589. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  590. "\n\n"
  591. (let ((text-width
  592. (if keyword (org-e-ascii--current-text-width keyword info)
  593. (- org-e-ascii-text-width org-e-ascii-global-margin))))
  594. (mapconcat
  595. (lambda (headline)
  596. (let* ((level (org-export-get-relative-level headline info))
  597. (indent (* (1- level) 3)))
  598. (concat
  599. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  600. (org-e-ascii--build-title
  601. headline info (- text-width indent) nil
  602. (eq (plist-get info :with-tags) 'not-in-toc)))))
  603. (org-export-collect-headlines info n) "\n")))))
  604. (defun org-e-ascii--list-listings (keyword info)
  605. "Return a list of listings.
  606. KEYWORD is the keyword that initiated the list of listings
  607. generation. INFO is a plist used as a communication channel."
  608. (let ((title (org-e-ascii--translate "List Of Listings\n" info)))
  609. (concat
  610. title
  611. (make-string (1- (length title))
  612. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  613. "\n\n"
  614. (let ((text-width
  615. (if keyword (org-e-ascii--current-text-width keyword info)
  616. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  617. ;; Use a counter instead of retreiving ordinal of each
  618. ;; src-block.
  619. (count 0))
  620. (mapconcat
  621. (lambda (src-block)
  622. ;; Store initial text so its length can be computed. This is
  623. ;; used to properly align caption right to it in case of
  624. ;; filling (like contents of a description list item).
  625. (let ((initial-text
  626. (format (org-e-ascii--translate "Listing %d: " info)
  627. (incf count))))
  628. (concat
  629. initial-text
  630. (org-trim
  631. (org-e-ascii--indent-string
  632. (org-e-ascii--fill-string
  633. (let ((caption (org-element-property :caption src-block)))
  634. (if (not caption) (org-element-property :name src-block)
  635. (org-export-secondary-string
  636. ;; Use short name in priority, if available.
  637. (or (cdr caption) (car caption)) 'e-ascii info)))
  638. (- text-width (length initial-text)) info)
  639. (length initial-text))))))
  640. (org-export-collect-listings info) "\n")))))
  641. (defun org-e-ascii--list-tables (keyword info)
  642. "Return a list of listings.
  643. KEYWORD is the keyword that initiated the list of listings
  644. generation. INFO is a plist used as a communication channel."
  645. (let ((title (org-e-ascii--translate "List Of Tables\n" info)))
  646. (concat
  647. title
  648. (make-string (1- (length title))
  649. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  650. "\n\n"
  651. (let ((text-width
  652. (if keyword (org-e-ascii--current-text-width keyword info)
  653. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  654. ;; Use a counter instead of retreiving ordinal of each
  655. ;; src-block.
  656. (count 0))
  657. (mapconcat
  658. (lambda (table)
  659. ;; Store initial text so its length can be computed. This is
  660. ;; used to properly align caption right to it in case of
  661. ;; filling (like contents of a description list item).
  662. (let ((initial-text
  663. (format (org-e-ascii--translate "Table %d: " info)
  664. (incf count))))
  665. (concat
  666. initial-text
  667. (org-trim
  668. (org-e-ascii--indent-string
  669. (org-e-ascii--fill-string
  670. (let ((caption (org-element-property :caption table)))
  671. (if (not caption) (org-element-property :name table)
  672. ;; Use short name in priority, if available.
  673. (org-export-secondary-string
  674. (or (cdr caption) (car caption)) 'e-ascii info)))
  675. (- text-width (length initial-text)) info)
  676. (length initial-text))))))
  677. (org-export-collect-tables info) "\n")))))
  678. (defun org-e-ascii--unique-links (element info)
  679. "Return a list of unique link references in ELEMENT.
  680. ELEMENT is either an headline element or a section element. INFO
  681. is a plist used as a communication channel.
  682. It covers links that may be found current headline's title, in
  683. the following section and in any inlinetask's title there."
  684. (let* (seen
  685. (unique-link-p
  686. (function
  687. ;; Return LINK if it wasn't referenced so far, or nil.
  688. ;; Update SEEN links along the way.
  689. (lambda (link)
  690. (let ((footprint
  691. (cons (org-element-property :raw-link link)
  692. (org-element-contents link))))
  693. (unless (member footprint seen)
  694. (push footprint seen) link)))))
  695. (harvest-links-in-title
  696. (function
  697. ;; Return a list of all unique links in ELEMENT. ELEMENT
  698. ;; may be an headline or an inlinetask element.
  699. (lambda (element)
  700. (let (acc)
  701. (dolist (obj (org-element-property :title element) acc)
  702. (when (eq (org-element-type obj) 'link)
  703. (let ((link (funcall unique-link-p obj)))
  704. (and link (push link acc)))))))))
  705. ;; Retrieve HEADLINE's section, if it exists.
  706. (section (if (eq (org-element-type element) 'section) element
  707. (let ((sec (car (org-element-contents element))))
  708. (and (eq (org-element-type sec) 'section) sec))))
  709. (headline (if (eq (org-element-type element) 'headline) element
  710. (org-export-get-parent-headline element info))))
  711. (append
  712. ;; Links that may be in HEADLINE's title.
  713. (funcall harvest-links-in-title headline)
  714. ;; Get all links in SECTION.
  715. (org-element-map
  716. section 'link (lambda (link) (funcall unique-link-p link)) info))))
  717. (defun org-e-ascii--describe-links (links width info)
  718. "Return a string describing a list of links.
  719. LINKS is a list of link type objects, as returned by
  720. `org-e-ascii--unique-links'. WIDTH is the text width allowed for
  721. the output string. INFO is a plist used as a communication
  722. channel."
  723. (mapconcat
  724. (lambda (link)
  725. (let ((type (org-element-property :type link))
  726. (anchor (let ((desc (org-element-contents link)))
  727. (if (not desc) (org-element-property :raw-link link)
  728. (org-export-secondary-string desc 'e-ascii info)))))
  729. (cond
  730. ;; Coderefs, radio links and fuzzy links are ignored.
  731. ((member type '("coderef" "radio" "fuzzy")) nil)
  732. ;; Id and custom-id links: Headlines refer to their numbering.
  733. ((member type '("custom-id" "id"))
  734. (let ((dest (org-export-resolve-id-link link info)))
  735. (concat
  736. (org-e-ascii--fill-string
  737. (format
  738. "[%s] %s"
  739. anchor
  740. (if (not dest) (org-e-ascii--translate "Unknown reference" info)
  741. (format
  742. (org-e-ascii--translate "See section %s" info)
  743. (mapconcat 'number-to-string
  744. (org-export-get-headline-number dest info) "."))))
  745. width info) "\n\n")))
  746. ;; Do not add a link that cannot be resolved and doesn't have
  747. ;; any description: destination is already visible in the
  748. ;; paragraph.
  749. ((not (org-element-contents link)) nil)
  750. (t
  751. (concat
  752. (org-e-ascii--fill-string
  753. (format "[%s] %s" anchor (org-element-property :raw-link link))
  754. width info)
  755. "\n\n")))))
  756. links ""))
  757. ;;; Template
  758. (defun org-e-ascii-template--document-title (info)
  759. "Return document title, as a string.
  760. INFO is a plist used as a communication channel."
  761. (let ((text-width org-e-ascii-text-width)
  762. (title (org-export-secondary-string
  763. (plist-get info :title) 'e-ascii info))
  764. (author
  765. (and (plist-get info :with-author)
  766. (let ((auth (plist-get info :author)))
  767. (and auth (org-export-secondary-string auth 'e-ascii info)))))
  768. (email
  769. (and (plist-get info :with-email)
  770. (org-export-secondary-string
  771. (plist-get info :email) 'e-ascii info)))
  772. (date (plist-get info :date)))
  773. ;; There are two types of title blocks depending on the presence
  774. ;; of a title to display.
  775. (if (string= title "")
  776. ;; Title block without a title. DATE is positioned at the top
  777. ;; right of the document, AUTHOR to the top left and EMAIL
  778. ;; just below.
  779. (cond
  780. ((and (org-string-nw-p date) (org-string-nw-p author))
  781. (concat
  782. author
  783. (make-string (- text-width (length date) (length author)) ? )
  784. date
  785. (when (org-string-nw-p email) (concat "\n" email))
  786. "\n\n\n"))
  787. ((and (org-string-nw-p date) (org-string-nw-p email))
  788. (concat
  789. email
  790. (make-string (- text-width (length date) (length email)) ? )
  791. date "\n\n\n"))
  792. ((org-string-nw-p date)
  793. (concat
  794. (org-e-ascii--justify-string date text-width 'right)
  795. "\n\n\n"))
  796. ((and (org-string-nw-p author) (org-string-nw-p email))
  797. (concat author "\n" email "\n\n\n"))
  798. ((org-string-nw-p author) (concat author "\n\n\n"))
  799. ((org-string-nw-p email) (concat email "\n\n\n")))
  800. ;; Title block with a title. Document's TITLE, along with the
  801. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  802. ;; centered. Date is just below, also centered.
  803. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  804. ;; Format TITLE. It may be filled if it is too wide,
  805. ;; that is wider than the two thirds of the total width.
  806. (title-len (min (length title) (/ (* 2 text-width) 3)))
  807. (formatted-title (org-e-ascii--fill-string title title-len info))
  808. (line
  809. (make-string
  810. (min (+ (max title-len (length author) (length email)) 2)
  811. text-width) (if utf8p ?━ ?_))))
  812. (org-e-ascii--justify-string
  813. (concat line "\n"
  814. (unless utf8p "\n")
  815. (upcase formatted-title)
  816. (cond
  817. ((and (org-string-nw-p author) (org-string-nw-p email))
  818. (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
  819. ((org-string-nw-p author)
  820. (concat (if utf8p "\n\n\n" "\n\n") author))
  821. ((org-string-nw-p email)
  822. (concat (if utf8p "\n\n\n" "\n\n") email)))
  823. "\n" line
  824. (when (org-string-nw-p date) (concat "\n\n\n" date))
  825. "\n\n\n") text-width 'center)))))
  826. (defun org-e-ascii-template (contents info)
  827. "Return complete document string after ASCII conversion.
  828. CONTENTS is the transcoded contents string. INFO is a plist
  829. holding export options."
  830. (org-element-normalize-string
  831. (org-e-ascii--indent-string
  832. (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
  833. ;; 1. Build title block.
  834. (concat
  835. (org-e-ascii-template--document-title info)
  836. ;; 2. Table of contents.
  837. (let ((depth (plist-get info :with-toc)))
  838. (when depth
  839. (concat
  840. (org-e-ascii--build-toc info (and (wholenump depth) depth))
  841. "\n\n\n")))
  842. ;; 3. Document's body.
  843. contents
  844. ;; 4. Footnote definitions.
  845. (let ((definitions (org-export-collect-footnote-definitions
  846. (plist-get info :parse-tree) info))
  847. ;; Insert full links right inside the footnote definition
  848. ;; as they have no chance to be inserted later.
  849. (org-e-ascii-links-to-notes nil))
  850. (when definitions
  851. (concat
  852. "\n\n\n"
  853. (let ((title (org-e-ascii--translate "Footnotes\n" info)))
  854. (concat
  855. title
  856. (make-string
  857. (1- (length title))
  858. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  859. "\n\n"
  860. (mapconcat
  861. (lambda (ref)
  862. (let ((id (format "[%s] " (car ref))))
  863. ;; Distinguish between inline definitions and
  864. ;; full-fledged definitions.
  865. (org-trim
  866. (let ((def (nth 2 ref)))
  867. (if (eq (org-element-type def) 'org-data)
  868. ;; Full-fledged definition: footnote ID is
  869. ;; inserted inside the first parsed paragraph
  870. ;; (FIRST), if any, to be sure filling will
  871. ;; take it into consideration.
  872. (let ((first (car (org-element-contents def))))
  873. (if (not (eq (org-element-type first) 'paragraph))
  874. (concat id "\n" (org-export-data def 'e-ascii info))
  875. (push id (nthcdr 2 first))
  876. (org-export-data def 'e-ascii info)))
  877. ;; Fill paragraph once footnote ID is inserted in
  878. ;; order to have a correct length for first line.
  879. (org-e-ascii--fill-string
  880. (concat id (org-export-secondary-string def 'e-ascii info))
  881. text-width info))))))
  882. definitions "\n\n"))))
  883. ;; 5. Creator. Ignore `comment' value as there are no comments in
  884. ;; ASCII. Justify it to the bottom right.
  885. (let ((creator-info (plist-get info :with-creator)))
  886. (unless (or (not creator-info) (eq creator-info 'comment))
  887. (concat
  888. "\n\n\n"
  889. (org-e-ascii--fill-string
  890. (plist-get info :creator) text-width info 'right))))))
  891. org-e-ascii-global-margin)))
  892. (defun org-e-ascii--translate (s info)
  893. "Translate string S.
  894. INFO is a plist used as a communication channel.
  895. Translation depends on `:language' property and allowed charset.
  896. If no translation in found for a given language and a given
  897. charset, fall-back to S."
  898. (let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
  899. (lang (plist-get info :language))
  900. (translations (cdr (assoc s org-e-ascii-dictionary))))
  901. (or (plist-get (cdr (assoc lang translations)) charset) s)))
  902. ;;; Transcode Functions
  903. ;;;; Babel Call
  904. ;; Babel Calls are ignored.
  905. ;;;; Center Block
  906. (defun org-e-ascii-center-block (center-block contents info)
  907. "Transcode a CENTER-BLOCK element from Org to ASCII.
  908. CONTENTS holds the contents of the block. INFO is a plist
  909. holding contextual information."
  910. (org-e-ascii--justify-string
  911. contents (org-e-ascii--current-text-width center-block info) 'center))
  912. ;;;; Comment
  913. ;; Comments are ignored.
  914. ;;;; Comment Block
  915. ;; Comment Blocks are ignored.
  916. ;;;; Drawer
  917. (defun org-e-ascii-drawer (drawer contents info)
  918. "Transcode a DRAWER element from Org to ASCII.
  919. CONTENTS holds the contents of the block. INFO is a plist
  920. holding contextual information."
  921. (let ((name (org-element-property :drawer-name drawer))
  922. (width (org-e-ascii--current-text-width drawer info)))
  923. (if (functionp org-e-ascii-format-drawer-function)
  924. (funcall org-e-ascii-format-drawer-function name contents width)
  925. ;; If there's no user defined function: simply
  926. ;; display contents of the drawer.
  927. contents)))
  928. ;;;; Dynamic Block
  929. (defun org-e-ascii-dynamic-block (dynamic-block contents info)
  930. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  931. CONTENTS holds the contents of the block. INFO is a plist
  932. holding contextual information. See
  933. `org-export-data'."
  934. contents)
  935. ;;;; Emphasis
  936. (defun org-e-ascii-emphasis (emphasis contents info)
  937. "Transcode EMPHASIS from Org to ASCII.
  938. CONTENTS is the contents of the emphasized text. INFO is a plist
  939. holding contextual information.."
  940. (let ((marker (org-element-property :marker emphasis)))
  941. ;; Leave emphasis markers as-is.
  942. (concat marker contents marker)))
  943. ;;;; Entity
  944. (defun org-e-ascii-entity (entity contents info)
  945. "Transcode an ENTITY object from Org to ASCII.
  946. CONTENTS are the definition itself. INFO is a plist holding
  947. contextual information."
  948. (org-element-property
  949. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  950. entity))
  951. ;;;; Example Block
  952. (defun org-e-ascii-example-block (example-block contents info)
  953. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  954. CONTENTS is nil. INFO is a plist holding contextual information."
  955. (org-e-ascii--box-string (org-export-handle-code example-block info) info))
  956. ;;;; Export Snippet
  957. (defun org-e-ascii-export-snippet (export-snippet contents info)
  958. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  959. CONTENTS is nil. INFO is a plist holding contextual information."
  960. (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
  961. (org-element-property :value export-snippet)))
  962. ;;;; Export Block
  963. (defun org-e-ascii-export-block (export-block contents info)
  964. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  965. CONTENTS is nil. INFO is a plist holding contextual information."
  966. (when (string= (org-element-property :type export-block) "ascii")
  967. (org-remove-indentation (org-element-property :value export-block))))
  968. ;;;; Fixed Width
  969. (defun org-e-ascii-fixed-width (fixed-width contents info)
  970. "Transcode a FIXED-WIDTH element from Org to ASCII.
  971. CONTENTS is nil. INFO is a plist holding contextual information."
  972. (org-e-ascii--box-string
  973. (replace-regexp-in-string
  974. "^[ \t]*: ?" "" (org-element-property :value fixed-width)) info))
  975. ;;;; Footnote Definition
  976. ;; Footnote Definitions are ignored. They are compiled at the end of
  977. ;; the document, by `org-e-ascii-template'.
  978. ;;;; Footnote Reference
  979. (defun org-e-ascii-footnote-reference (footnote-reference contents info)
  980. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  981. CONTENTS is nil. INFO is a plist holding contextual information."
  982. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  983. ;;;; Headline
  984. (defun org-e-ascii-headline (headline contents info)
  985. "Transcode an HEADLINE element from Org to ASCII.
  986. CONTENTS holds the contents of the headline. INFO is a plist
  987. holding contextual information."
  988. ;; Don't export footnote section, which will be handled at the end
  989. ;; of the template.
  990. (unless (org-element-property :footnote-section-p headline)
  991. (let* ((low-level-rank (org-export-low-level-p headline info))
  992. (width (org-e-ascii--current-text-width headline info))
  993. ;; Blank lines between headline and its contents.
  994. ;; `org-e-ascii-headline-spacing', when set, overwrites
  995. ;; original buffer's spacing.
  996. (pre-blanks
  997. (make-string
  998. (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
  999. (org-element-property :pre-blank headline)) ?\n))
  1000. ;; Even if HEADLINE has no section, there might be some
  1001. ;; links in its title that we shouldn't forget to describe.
  1002. (links
  1003. (unless (eq (caar (org-element-contents headline)) 'section)
  1004. (org-e-ascii--describe-links
  1005. (org-e-ascii--unique-links headline info) width info))))
  1006. ;; Deep subtree: export it as a list item.
  1007. (if low-level-rank
  1008. (concat
  1009. ;; Bullet.
  1010. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  1011. org-e-ascii-bullets))))
  1012. (char-to-string
  1013. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  1014. " "
  1015. ;; Title.
  1016. (org-e-ascii--build-title headline info width) "\n"
  1017. ;; Contents, indented by length of bullet.
  1018. pre-blanks
  1019. (org-e-ascii--indent-string
  1020. (concat contents
  1021. (when (org-string-nw-p links) (concat "\n\n" links)))
  1022. 2))
  1023. ;; Else: Standard headline.
  1024. (concat
  1025. (org-e-ascii--build-title headline info width 'underline)
  1026. "\n" pre-blanks
  1027. (concat (when (org-string-nw-p links) links) contents))))))
  1028. ;;;; Horizontal Rule
  1029. (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  1030. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  1031. CONTENTS is nil. INFO is a plist holding contextual
  1032. information."
  1033. (let ((attr
  1034. (read
  1035. (format
  1036. "(%s)"
  1037. (mapconcat
  1038. #'identity
  1039. (org-element-property :attr_ascii horizontal-rule)
  1040. " ")))))
  1041. (make-string (or (and (wholenump (plist-get attr :width))
  1042. (plist-get attr :width))
  1043. (org-e-ascii--current-text-width horizontal-rule info))
  1044. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))
  1045. ;;;; Inline Babel Call
  1046. ;; Inline Babel Calls are ignored.
  1047. ;;;; Inline Src Block
  1048. (defun org-e-ascii-inline-src-block (inline-src-block contents info)
  1049. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  1050. CONTENTS holds the contents of the item. INFO is a plist holding
  1051. contextual information."
  1052. (format org-e-ascii-verbatim-format
  1053. (org-element-property :value inline-src-block)))
  1054. ;;;; Inlinetask
  1055. (defun org-e-ascii-inlinetask (inlinetask contents info)
  1056. "Transcode an INLINETASK element from Org to ASCII.
  1057. CONTENTS holds the contents of the block. INFO is a plist
  1058. holding contextual information."
  1059. (let ((width (org-e-ascii--current-text-width inlinetask info))
  1060. (title (org-export-secondary-string
  1061. (org-element-property :title inlinetask) 'e-ascii info))
  1062. (todo (and (plist-get info :with-todo-keywords)
  1063. (let ((todo (org-element-property
  1064. :todo-keyword inlinetask)))
  1065. (and todo
  1066. (org-export-secondary-string todo 'e-ascii info)))))
  1067. (todo-type (org-element-property :todo-type inlinetask))
  1068. (tags (and (plist-get info :with-tags)
  1069. (org-element-property :tags inlinetask)))
  1070. (priority (and (plist-get info :with-priority)
  1071. (org-element-property :priority inlinetask))))
  1072. ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
  1073. ;; with appropriate arguments.
  1074. (if (functionp org-e-ascii-format-inlinetask-function)
  1075. (funcall org-e-ascii-format-inlinetask-function
  1076. todo todo-type priority title tags contents width)
  1077. ;; Otherwise, use a default template.
  1078. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1079. (org-e-ascii--indent-string
  1080. (concat
  1081. ;; Top line, with an additional blank line if not in UTF-8.
  1082. (make-string width (if utf8p ?━ ?_)) "\n"
  1083. (unless utf8p (concat (make-string width ? ) "\n"))
  1084. ;; Add title. Fill it if wider than inlinetask.
  1085. (let ((title (org-e-ascii--build-title inlinetask info width)))
  1086. (if (<= (length title) width) title
  1087. (org-e-ascii--fill-string title width info)))
  1088. "\n"
  1089. ;; If CONTENTS is not empty, insert it along with
  1090. ;; a separator.
  1091. (when (org-string-nw-p contents)
  1092. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1093. ;; Bottom line.
  1094. (make-string width (if utf8p ?━ ?_)))
  1095. ;; Flush the inlinetask to the right.
  1096. (- org-e-ascii-text-width org-e-ascii-global-margin
  1097. (if (not (org-export-get-parent-headline inlinetask info)) 0
  1098. org-e-ascii-inner-margin)
  1099. (org-e-ascii--current-text-width inlinetask info)))))))
  1100. ;;;; Item
  1101. (defun org-e-ascii-item (item contents info)
  1102. "Transcode an ITEM element from Org to ASCII.
  1103. CONTENTS holds the contents of the item. INFO is a plist holding
  1104. contextual information."
  1105. (let ((bullet
  1106. ;; First parent of ITEM is always the plain-list. Get
  1107. ;; `:type' property from it.
  1108. (org-list-bullet-string
  1109. (case (org-element-property :type (org-export-get-parent item info))
  1110. (descriptive
  1111. (concat
  1112. (org-export-secondary-string
  1113. (org-element-property :tag item) 'e-ascii info) ": "))
  1114. (ordered
  1115. ;; Return correct number for ITEM, paying attention to
  1116. ;; counters.
  1117. (let* ((struct (org-element-property :structure item))
  1118. (bul (org-element-property :bullet item))
  1119. (num
  1120. (number-to-string
  1121. (car (last (org-list-get-item-number
  1122. (org-element-property :begin item)
  1123. struct
  1124. (org-list-prevs-alist struct)
  1125. (org-list-parents-alist struct)))))))
  1126. (replace-regexp-in-string "[0-9]+" num bul)))
  1127. (t (let ((bul (org-element-property :bullet item)))
  1128. ;; Change bullets into more visible form if UTF-8 is active.
  1129. (if (not (eq (plist-get info :ascii-charset) 'utf-8)) bul
  1130. (replace-regexp-in-string
  1131. "-" "•"
  1132. (replace-regexp-in-string
  1133. "+" "⁃"
  1134. (replace-regexp-in-string "*" "‣" bul))))))))))
  1135. (concat
  1136. bullet
  1137. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1138. ;; already taken care of at the paragraph level so they don't
  1139. ;; interfere with indentation.
  1140. (let ((contents (org-e-ascii--indent-string contents (length bullet))))
  1141. (if (eq (caar (org-element-contents item)) 'paragraph)
  1142. (org-trim contents)
  1143. (concat "\n" contents))))))
  1144. ;;;; Keyword
  1145. (defun org-e-ascii-keyword (keyword contents info)
  1146. "Transcode a KEYWORD element from Org to ASCII.
  1147. CONTENTS is nil. INFO is a plist holding contextual
  1148. information."
  1149. (let ((key (downcase (org-element-property :key keyword)))
  1150. (value (org-element-property :value keyword)))
  1151. (cond
  1152. ((string= key "ascii") value)
  1153. ((string= key "toc")
  1154. (let ((value (downcase value)))
  1155. (cond
  1156. ((string-match "\\<headlines\\>" value)
  1157. (let ((depth (or (and (string-match "[0-9]+" value)
  1158. (string-to-number (match-string 0 value)))
  1159. (plist-get info :with-toc))))
  1160. (org-e-ascii--build-toc
  1161. info (and (wholenump depth) depth) keyword)))
  1162. ((string= "tables" value)
  1163. (org-e-ascii--list-tables keyword info))
  1164. ((string= "listings" value)
  1165. (org-e-ascii--list-listings keyword info))))))))
  1166. ;;;; Latex Environment
  1167. (defun org-e-ascii-latex-environment (latex-environment contents info)
  1168. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1169. CONTENTS is nil. INFO is a plist holding contextual
  1170. information."
  1171. (org-remove-indentation (org-element-property :value latex-environment)))
  1172. ;;;; Latex Fragment
  1173. (defun org-e-ascii-latex-fragment (latex-fragment contents info)
  1174. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1175. CONTENTS is nil. INFO is a plist holding contextual
  1176. information."
  1177. (org-element-property :value latex-fragment))
  1178. ;;;; Line Break
  1179. (defun org-e-ascii-line-break (line-break contents info)
  1180. "Transcode a LINE-BREAK object from Org to ASCII.
  1181. CONTENTS is nil. INFO is a plist holding contextual
  1182. information." hard-newline)
  1183. ;;;; Link
  1184. (defun org-e-ascii-link (link desc info)
  1185. "Transcode a LINK object from Org to ASCII.
  1186. DESC is the description part of the link, or the empty string.
  1187. INFO is a plist holding contextual information."
  1188. (let ((raw-link (org-element-property :raw-link link))
  1189. (type (org-element-property :type link)))
  1190. (cond
  1191. ((string= type "coderef")
  1192. (let ((ref (org-element-property :path link)))
  1193. (format (org-export-get-coderef-format ref desc)
  1194. (org-export-resolve-coderef ref info))))
  1195. ;; Do not apply a special syntax on radio links. Though, parse
  1196. ;; and transcode path to have a proper display of contents.
  1197. ((string= type "radio")
  1198. (org-export-secondary-string
  1199. (org-element-parse-secondary-string
  1200. (org-element-property :path link)
  1201. (cdr (assq 'radio-target org-element-object-restrictions)))
  1202. 'e-ascii info))
  1203. ;; Do not apply a special syntax on fuzzy links pointing to
  1204. ;; targets.
  1205. ((string= type "fuzzy")
  1206. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1207. ;; Ignore invisible "#+target: path".
  1208. (unless (eq (org-element-type destination) 'keyword)
  1209. (if (org-string-nw-p desc) desc
  1210. (when destination
  1211. (let ((number (org-export-get-ordinal destination info)))
  1212. (when number
  1213. (if (atom number) (number-to-string number)
  1214. (mapconcat 'number-to-string number ".")))))))))
  1215. (t
  1216. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1217. (concat
  1218. (format "[%s]" desc)
  1219. (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1220. ;;;; Macro
  1221. (defun org-e-ascii-macro (macro contents info)
  1222. "Transcode a MACRO element from Org to ASCII.
  1223. CONTENTS is nil. INFO is a plist holding contextual
  1224. information."
  1225. (org-export-expand-macro macro info))
  1226. ;;;; Paragraph
  1227. (defun org-e-ascii-paragraph (paragraph contents info)
  1228. "Transcode a PARAGRAPH element from Org to ASCII.
  1229. CONTENTS is the contents of the paragraph, as a string. INFO is
  1230. the plist used as a communication channel."
  1231. (org-e-ascii--fill-string
  1232. (let ((parent (org-export-get-parent paragraph info)))
  1233. ;; If PARAGRAPH is the first one in a list element, be sure to
  1234. ;; add the check-box in front of it, before any filling. Later,
  1235. ;; it would interfere with line width.
  1236. (if (and (eq (org-element-type parent) 'item)
  1237. (equal (car (org-element-contents parent)) paragraph))
  1238. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1239. (concat (case (org-element-property :checkbox parent)
  1240. (on (if utf8p "☑ " "[X] "))
  1241. (off (if utf8p "☐ " "[ ] "))
  1242. (trans (if utf8p "☒ " "[-] ")))
  1243. contents))
  1244. contents))
  1245. (org-e-ascii--current-text-width paragraph info) info))
  1246. ;;;; Plain List
  1247. (defun org-e-ascii-plain-list (plain-list contents info)
  1248. "Transcode a PLAIN-LIST element from Org to ASCII.
  1249. CONTENTS is the contents of the list. INFO is a plist holding
  1250. contextual information."
  1251. contents)
  1252. ;;;; Plain Text
  1253. (defun org-e-ascii-plain-text (text info)
  1254. "Transcode a TEXT string from Org to ASCII.
  1255. INFO is a plist used as a communication channel."
  1256. (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
  1257. (plist-get info :with-special-strings)))
  1258. text
  1259. ;; Usual replacements in utf-8 with proper option set.
  1260. (replace-regexp-in-string
  1261. "\\.\\.\\." "…"
  1262. (replace-regexp-in-string
  1263. "--" "–"
  1264. (replace-regexp-in-string "---" "—" text)))))
  1265. ;;;; Property Drawer
  1266. (defun org-e-ascii-property-drawer (property-drawer contents info)
  1267. "Transcode a PROPERTY-DRAWER element from Org to ASCII.
  1268. CONTENTS is nil. INFO is a plist used as a communication
  1269. channel."
  1270. ;; The property drawer isn't exported but we want separating blank
  1271. ;; lines nonetheless.
  1272. "")
  1273. ;;;; Quote Block
  1274. (defun org-e-ascii-quote-block (quote-block contents info)
  1275. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1276. CONTENTS holds the contents of the block. INFO is a plist
  1277. holding contextual information."
  1278. (let ((width (org-e-ascii--current-text-width quote-block info)))
  1279. (org-e-ascii--indent-string
  1280. (org-remove-indentation
  1281. (org-e-ascii--fill-string contents width info))
  1282. org-e-ascii-quote-margin)))
  1283. ;;;; Quote Section
  1284. (defun org-e-ascii-quote-section (quote-section contents info)
  1285. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1286. CONTENTS is nil. INFO is a plist holding contextual information."
  1287. (let ((width (org-e-ascii--current-text-width quote-section info))
  1288. (value
  1289. (org-export-secondary-string
  1290. (org-remove-indentation
  1291. (org-element-property :value quote-section)) 'e-ascii info)))
  1292. (org-e-ascii--indent-string
  1293. value
  1294. (+ org-e-ascii-quote-margin
  1295. ;; Don't apply inner margin if parent headline is low level.
  1296. (let ((headline (org-export-get-parent-headline quote-section info)))
  1297. (if (org-export-low-level-p headline info) 0
  1298. org-e-ascii-inner-margin))))))
  1299. ;;;; Radio Target
  1300. (defun org-e-ascii-radio-target (radio-target contents info)
  1301. "Transcode a RADIO-TARGET object from Org to ASCII.
  1302. CONTENTS is the contents of the target. INFO is a plist holding
  1303. contextual information."
  1304. contents)
  1305. ;;;; Section
  1306. (defun org-e-ascii-section (section contents info)
  1307. "Transcode a SECTION element from Org to ASCII.
  1308. CONTENTS is the contents of the section. INFO is a plist holding
  1309. contextual information."
  1310. (org-e-ascii--indent-string
  1311. (concat
  1312. contents
  1313. (when org-e-ascii-links-to-notes
  1314. ;; Add list of links at the end of SECTION.
  1315. (let ((links (org-e-ascii--describe-links
  1316. (org-e-ascii--unique-links section info)
  1317. (org-e-ascii--current-text-width section info) info)))
  1318. ;; Separate list of links and section contents.
  1319. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1320. ;; Do not apply inner margin if parent headline is low level.
  1321. (let ((headline (org-export-get-parent-headline section info)))
  1322. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1323. org-e-ascii-inner-margin))))
  1324. ;;;; Special Block
  1325. (defun org-e-ascii-special-block (special-block contents info)
  1326. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1327. CONTENTS holds the contents of the block. INFO is a plist
  1328. holding contextual information."
  1329. contents)
  1330. ;;;; Src Block
  1331. (defun org-e-ascii-src-block (src-block contents info)
  1332. "Transcode a SRC-BLOCK element from Org to ASCII.
  1333. CONTENTS holds the contents of the item. INFO is a plist holding
  1334. contextual information."
  1335. (let ((caption (org-e-ascii--build-caption src-block info)))
  1336. (concat
  1337. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1338. (org-e-ascii--box-string (org-export-handle-code src-block info) info)
  1339. (when (and caption (not org-e-ascii-caption-above))
  1340. (concat "\n" caption)))))
  1341. ;;;; Statistics Cookie
  1342. (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  1343. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1344. CONTENTS is nil. INFO is a plist holding contextual information."
  1345. (org-element-property :value statistics-cookie))
  1346. ;;;; Subscript
  1347. (defun org-e-ascii-subscript (subscript contents info)
  1348. "Transcode a SUBSCRIPT object from Org to ASCII.
  1349. CONTENTS is the contents of the object. INFO is a plist holding
  1350. contextual information."
  1351. (if (org-element-property :use-brackets-p subscript)
  1352. (format "_{%s}" contents)
  1353. (format "_%s" contents)))
  1354. ;;;; Superscript
  1355. (defun org-e-ascii-superscript (superscript contents info)
  1356. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1357. CONTENTS is the contents of the object. INFO is a plist holding
  1358. contextual information."
  1359. (if (org-element-property :use-brackets-p superscript)
  1360. (format "_{%s}" contents)
  1361. (format "_%s" contents)))
  1362. ;;;; Table
  1363. ;; While `org-e-ascii-table' is the callback function expected by
  1364. ;; org-export mechanism, it requires four subroutines to display
  1365. ;; tables accordingly to chosen charset, alignment and width
  1366. ;; specifications.
  1367. ;; Thus, `org-e-ascii-table--column-width' computes the display width
  1368. ;; for each column in the table,
  1369. ;; `org-e-ascii-table--vertical-separators' returns a vector
  1370. ;; containing separators (or lack thereof),
  1371. ;; `org-e-ascii-table--build-hline' creates various hline strings,
  1372. ;; depending on charset, separators and position within the tabl and
  1373. ;; `org-e-ascii-table--format-cell' properly aligns contents within
  1374. ;; a given cell and width.
  1375. (defun org-e-ascii-table (table contents info)
  1376. "Transcode a TABLE element from Org to ASCII.
  1377. CONTENTS is nil. INFO is a plist holding contextual information."
  1378. (let ((raw-table (org-element-property :raw-table table))
  1379. (caption (org-e-ascii--build-caption table info)))
  1380. (concat
  1381. ;; Possibly add a caption string above.
  1382. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1383. ;; Insert table. Note: "table.el" tables are left unmodified.
  1384. (if (eq (org-element-property :type table) 'table.el) raw-table
  1385. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1386. ;; Extract information out of the raw table (TABLE-INFO)
  1387. ;; and clean it (CLEAN-TABLE).
  1388. (table-info (org-export-table-format-info raw-table))
  1389. (special-col-p (plist-get table-info :special-column-p))
  1390. (alignment (plist-get table-info :alignment))
  1391. (clean-table (org-export-clean-table raw-table special-col-p))
  1392. ;; Change table into lisp, much like
  1393. ;; `org-table-to-lisp', though cells are parsed and
  1394. ;; transcoded along the way.
  1395. (lisp-table
  1396. (mapcar
  1397. (lambda (line)
  1398. (if (string-match org-table-hline-regexp line) 'hline
  1399. (mapcar
  1400. (lambda (cell)
  1401. (org-trim
  1402. (org-export-secondary-string
  1403. (org-element-parse-secondary-string
  1404. cell
  1405. (cdr (assq 'item org-element-string-restrictions)))
  1406. 'e-ascii info)))
  1407. (org-split-string (org-trim line) "\\s-?|\\s-?"))))
  1408. (org-split-string clean-table "[ \t]*\n[ \t]*")))
  1409. ;; Compute real column widths.
  1410. (column-widths
  1411. (org-e-ascii-table--column-width lisp-table table-info))
  1412. ;; Construct separators according to column groups.
  1413. (separators (org-e-ascii-table--vertical-separators table-info))
  1414. ;; Build different `hline' strings, depending on
  1415. ;; separators, column widths and position.
  1416. (hline-standard
  1417. (org-e-ascii-table--build-hline
  1418. nil separators column-widths info))
  1419. (hline-top
  1420. (and utf8p (org-e-ascii-table--build-hline
  1421. 'top separators column-widths info)))
  1422. (hline-bottom
  1423. (and utf8p (org-e-ascii-table--build-hline
  1424. 'bottom separators column-widths info))))
  1425. ;; Now build table back, with correct alignment, considering
  1426. ;; columns widths and separators.
  1427. (mapconcat
  1428. (lambda (line)
  1429. (cond
  1430. ((eq line 'hline) hline-standard)
  1431. ((eq line 'hline-bottom) hline-bottom)
  1432. ((eq line 'hline-top) hline-top)
  1433. (t (loop for cell in line
  1434. for col from 0 to (length line)
  1435. concat
  1436. (concat
  1437. (let ((sep (aref separators col)))
  1438. (if (and utf8p (not (string= sep ""))) "│" sep))
  1439. (org-e-ascii-table--format-cell
  1440. cell col column-widths alignment info)) into l
  1441. finally return
  1442. (concat l
  1443. (let ((sep (aref separators col)))
  1444. (if (and utf8p (not (string= sep ""))) "│"
  1445. sep)))))))
  1446. ;; If charset is `utf-8', make sure lisp-table always starts
  1447. ;; with `hline-top' and ends with `hline-bottom'.
  1448. (if (not utf8p) lisp-table
  1449. (setq lisp-table
  1450. (cons 'hline-top
  1451. (if (eq (car lisp-table) 'hline) (cdr lisp-table)
  1452. lisp-table)))
  1453. (setq lisp-table
  1454. (nconc
  1455. (if (eq (car (last lisp-table)) 'hline) (butlast lisp-table)
  1456. lisp-table)
  1457. '(hline-bottom)))) "\n")))
  1458. ;; Possible add a caption string below.
  1459. (when (and caption (not org-e-ascii-caption-above))
  1460. (concat "\n" caption)))))
  1461. (defun org-e-ascii-table--column-width (table table-info)
  1462. "Return vector of TABLE columns width.
  1463. TABLE is the Lisp representation of the Org table considered.
  1464. TABLE-INFO holds information about the table. See
  1465. `org-export-table-format-info'.
  1466. Unlike to `:width' property from `org-export-table-format-info',
  1467. the return value is a vector containing width of every column,
  1468. not only those with an explicit width cookie. Special column, if
  1469. any, is ignored."
  1470. ;; All rows have the same length, but be sure to ignore hlines.
  1471. (let ((width (make-vector
  1472. (loop for row in table
  1473. unless (eq row 'hline)
  1474. return (length row))
  1475. 0)))
  1476. ;; Set column width to the maximum width of the cells in that
  1477. ;; column.
  1478. (mapc
  1479. (lambda (line)
  1480. (let ((idx 0))
  1481. (unless (eq line 'hline)
  1482. (mapc (lambda (cell)
  1483. (let ((len (length cell)))
  1484. (when (> len (aref width idx)) (aset width idx len)))
  1485. (incf idx))
  1486. line))))
  1487. table)
  1488. (unless org-e-ascii-table-widen-columns
  1489. ;; When colums are not widened, width cookies have precedence
  1490. ;; over string lengths. Thus, overwrite the latter with the
  1491. ;; former.
  1492. (let ((cookies (plist-get table-info :width))
  1493. (specialp (plist-get table-info :special-column-p)))
  1494. ;; Remove special column from COOKIES vector, if any.
  1495. (loop for w across (if specialp (substring cookies 1) cookies)
  1496. for idx from 0 to width
  1497. when w do (aset width idx w))))
  1498. ;; Return value.
  1499. width))
  1500. (defun org-e-ascii-table--vertical-separators (table-info)
  1501. "Return a vector of strings for vertical separators.
  1502. TABLE-INFO holds information about considered table. See
  1503. `org-export-table-format-info'.
  1504. Return value is a vector whose length is one more than the number
  1505. of columns in the table. Special column, if any, is ignored."
  1506. (let* ((colgroups (plist-get table-info :column-groups))
  1507. (separators (make-vector (1+ (length colgroups)) "")))
  1508. (if org-e-ascii-table-keep-all-vertical-lines
  1509. (make-vector (length separators) "|")
  1510. (let ((column 0))
  1511. (mapc (lambda (group)
  1512. (when (memq group '(start start-end))
  1513. (aset separators column "|"))
  1514. (when (memq group '(end start-end))
  1515. (aset separators (1+ column) "|"))
  1516. (incf column))
  1517. colgroups)
  1518. ;; Remove unneeded special column.
  1519. (if (not (plist-get table-info :special-column-p)) separators
  1520. (substring separators 1))))))
  1521. (defun org-e-ascii-table--format-cell (cell col width alignment info)
  1522. "Format CELL with column width and alignment constraints.
  1523. CELL is the contents of the cell, as a string.
  1524. COL is the column containing the cell considered.
  1525. WIDTH is a vector holding every column width, as returned by
  1526. `org-e-ascii-table--column-width'.
  1527. ALIGNMENT is a vector containing alignment strings for every
  1528. column.
  1529. INFO is a plist used as a communication channel."
  1530. (let ((col-width (if org-e-ascii-table-widen-columns (aref width col)
  1531. (or (aref width col) (length cell)))))
  1532. ;; When CELL is too large, it has to be truncated.
  1533. (unless (or org-e-ascii-table-widen-columns (<= (length cell) col-width))
  1534. (setq cell (concat (substring cell 0 (- col-width 2)) "=>")))
  1535. (let* ((indent-tabs-mode nil)
  1536. (align (aref alignment col))
  1537. (aligned-cell
  1538. (org-e-ascii--justify-string
  1539. (org-trim cell) col-width
  1540. (cond ((string= align "c") 'center)
  1541. ((string= align "r") 'right)))))
  1542. ;; Return aligned cell, with missing white spaces added and
  1543. ;; space separators between columns.
  1544. (format
  1545. " %s "
  1546. (concat aligned-cell
  1547. (make-string (- col-width (length aligned-cell)) ? ))))))
  1548. (defun org-e-ascii-table--build-hline (position separators column-widths info)
  1549. "Return string used as an horizontal line in tables.
  1550. POSITION is a symbol among `top', `bottom' and nil, which
  1551. specifies position of the horizontal line within the table.
  1552. SEPARATORS is a vector strings specifying separators used in the
  1553. table, as returned by `org-e-ascii-table--vertical-separators'.
  1554. COLUMN-WIDTHS is a vector of numbers specifying widths of all
  1555. columns in the table, as returned by
  1556. `org-e-ascii-table--column-width'.
  1557. INFO is a plist used as a communication channel."
  1558. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1559. (loop for idx from 0 to (length separators)
  1560. for width across column-widths
  1561. concat
  1562. (concat
  1563. (cond ((string= (aref separators idx) "") nil)
  1564. ((and utf8p (zerop idx))
  1565. (cond ((eq position 'top) "┍")
  1566. ((eq position 'bottom) "┕")
  1567. (t "├")))
  1568. (utf8p
  1569. (cond ((eq position 'top) "┯")
  1570. ((eq position 'bottom) "┷")
  1571. (t "┼")))
  1572. (t "+"))
  1573. ;; Hline has to cover all the cell and both white spaces
  1574. ;; between columns.
  1575. (make-string (+ width 2)
  1576. (cond ((not utf8p) ?-)
  1577. ((not position) ?─)
  1578. (t ?━))))
  1579. into hline
  1580. finally return
  1581. ;; There is one separator more than columns, so handle it
  1582. ;; here.
  1583. (concat
  1584. hline
  1585. (cond
  1586. ((string= (aref separators idx) "") nil)
  1587. (utf8p (cond ((eq position 'top) "┑")
  1588. ((eq position 'bottom) "┙")
  1589. (t "┤")))
  1590. (t "+"))))))
  1591. ;;;; Target
  1592. ;; Targets are invisible.
  1593. ;;;; Time-stamp
  1594. (defun org-e-ascii-time-stamp (time-stamp contents info)
  1595. "Transcode a TIME-STAMP object from Org to ASCII.
  1596. CONTENTS is nil. INFO is a plist holding contextual information."
  1597. ;; Return time-stamps as-is.
  1598. (org-element-time-stamp-interpreter time-stamp contents))
  1599. ;;;; Verbatim
  1600. (defun org-e-ascii-verbatim (verbatim contents info)
  1601. "Return a VERBATIM object from Org to ASCII.
  1602. CONTENTS is nil. INFO is a plist holding contextual information."
  1603. (format org-e-ascii-verbatim-format
  1604. (org-element-property :value verbatim)))
  1605. ;;;; Verse Block
  1606. (defun org-e-ascii-verse-block (verse-block contents info)
  1607. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1608. CONTENTS is nil. INFO is a plist holding contextual information."
  1609. (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
  1610. (org-e-ascii--indent-string
  1611. (org-e-ascii--justify-string
  1612. (org-export-secondary-string
  1613. (org-element-property :value verse-block) 'e-ascii info)
  1614. verse-width 'left)
  1615. org-e-ascii-quote-margin)))
  1616. ;;; Filter
  1617. (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  1618. "Filter controlling number of blank lines after an headline.
  1619. HEADLINE is a string representing a transcoded headline.
  1620. BACK-END is symbol specifying back-end used for export. INFO is
  1621. plist containing the communication channel.
  1622. This function only applies to `e-ascii' back-end. See
  1623. `org-e-ascii-headline-spacing' for information.
  1624. For any other back-end, HEADLINE is returned as-is."
  1625. (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
  1626. (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
  1627. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1628. ;;; Interactive function
  1629. (defun org-e-ascii-export-to-ascii
  1630. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1631. "Export current buffer to a text file.
  1632. If narrowing is active in the current buffer, only export its
  1633. narrowed part.
  1634. If a region is active, export that region.
  1635. When optional argument SUBTREEP is non-nil, export the sub-tree
  1636. at point, extracting information from the headline properties
  1637. first.
  1638. When optional argument VISIBLE-ONLY is non-nil, don't export
  1639. contents of hidden elements.
  1640. When optional argument BODY-ONLY is non-nil, strip title, table
  1641. of contents and footnote definitions from output.
  1642. EXT-PLIST, when provided, is a property list with external
  1643. parameters overriding Org default settings, but still inferior to
  1644. file-local settings.
  1645. When optional argument PUB-DIR is set, use it as the publishing
  1646. directory.
  1647. Return output file's name."
  1648. (interactive)
  1649. (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
  1650. (org-export-to-file
  1651. 'e-ascii outfile subtreep visible-only body-only ext-plist)))
  1652. (provide 'org-e-ascii)
  1653. ;;; org-e-ascii.el ends here