org-e-ascii.el 67 KB

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