org-e-ascii.el 62 KB

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