org-e-ascii.el 61 KB

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