org-e-ascii.el 64 KB

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