org-e-ascii.el 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. ;;
  17. ;; This library implements an ASCII back-end for Org generic exporter.
  18. ;;
  19. ;; It provides two commands for export, depending on the desired
  20. ;; output: `org-e-ascii-export-as-ascii' (temporary buffer) and
  21. ;; `org-e-ascii-export-to-ascii' ("txt" file).
  22. ;;
  23. ;; Output encoding is specified through `org-e-ascii-charset'
  24. ;; variable, among `ascii', `latin1' and `utf-8' symbols.
  25. ;;
  26. ;; By default, horizontal rules span over the full text with, but with
  27. ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
  28. ;; they can be shortened and centered.
  29. ;;; Code:
  30. (eval-when-compile (require 'cl))
  31. (require 'org-export)
  32. ;;; Define Back-End
  33. ;;
  34. ;; The following setting won't allow to modify preferred charset
  35. ;; through a buffer keyword or an option item, but, since the property
  36. ;; will appear in communication channel nonetheless, it allows to
  37. ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
  38. ;; mechanism.
  39. ;;
  40. ;; We also install a filter for headlines and sections, in order to
  41. ;; control blank lines separating them in output string.
  42. (org-export-define-backend e-ascii
  43. ((bold . org-e-ascii-bold)
  44. (center-block . org-e-ascii-center-block)
  45. (clock . org-e-ascii-clock)
  46. (code . org-e-ascii-code)
  47. (drawer . org-e-ascii-drawer)
  48. (dynamic-block . org-e-ascii-dynamic-block)
  49. (entity . org-e-ascii-entity)
  50. (example-block . org-e-ascii-example-block)
  51. (export-block . org-e-ascii-export-block)
  52. (export-snippet . org-e-ascii-export-snippet)
  53. (fixed-width . org-e-ascii-fixed-width)
  54. (footnote-definition . org-e-ascii-footnote-definition)
  55. (footnote-reference . org-e-ascii-footnote-reference)
  56. (headline . org-e-ascii-headline)
  57. (horizontal-rule . org-e-ascii-horizontal-rule)
  58. (inline-src-block . org-e-ascii-inline-src-block)
  59. (inlinetask . org-e-ascii-inlinetask)
  60. (italic . org-e-ascii-italic)
  61. (item . org-e-ascii-item)
  62. (keyword . org-e-ascii-keyword)
  63. (latex-environment . org-e-ascii-latex-environment)
  64. (latex-fragment . org-e-ascii-latex-fragment)
  65. (line-break . org-e-ascii-line-break)
  66. (link . org-e-ascii-link)
  67. (macro . org-e-ascii-macro)
  68. (paragraph . org-e-ascii-paragraph)
  69. (plain-list . org-e-ascii-plain-list)
  70. (plain-text . org-e-ascii-plain-text)
  71. (planning . org-e-ascii-planning)
  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. :export-block "ASCII"
  92. :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
  93. (:filter-section . org-e-ascii-filter-headline-blank-lines))
  94. :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))
  95. ;;; User Configurable Variables
  96. (defgroup org-export-e-ascii nil
  97. "Options for exporting Org mode files to ASCII."
  98. :tag "Org Export ASCII"
  99. :group 'org-export)
  100. (defcustom org-e-ascii-text-width 72
  101. "Maximum width of exported text.
  102. This number includes margin size, as set in
  103. `org-e-ascii-global-margin'."
  104. :group 'org-export-e-ascii
  105. :type 'integer)
  106. (defcustom org-e-ascii-global-margin 0
  107. "Width of the left margin, in number of characters."
  108. :group 'org-export-e-ascii
  109. :type 'integer)
  110. (defcustom org-e-ascii-inner-margin 2
  111. "Width of the inner margin, in number of characters.
  112. Inner margin is applied between each headline."
  113. :group 'org-export-e-ascii
  114. :type 'integer)
  115. (defcustom org-e-ascii-quote-margin 6
  116. "Width of margin used for quoting text, in characters.
  117. This margin is applied on both sides of the text."
  118. :group 'org-export-e-ascii
  119. :type 'integer)
  120. (defcustom org-e-ascii-inlinetask-width 30
  121. "Width of inline tasks, in number of characters.
  122. This number ignores any margin."
  123. :group 'org-export-e-ascii
  124. :type 'integer)
  125. (defcustom org-e-ascii-headline-spacing '(1 . 2)
  126. "Number of blank lines inserted around headlines.
  127. This variable can be set to a cons cell. In that case, its car
  128. represents the number of blank lines present before headline
  129. contents whereas its cdr reflects the number of blank lines after
  130. contents.
  131. A nil value replicates the number of blank lines found in the
  132. original Org buffer at the same place."
  133. :group 'org-export-e-ascii
  134. :type '(choice
  135. (const :tag "Replicate original spacing" nil)
  136. (cons :tag "Set an uniform spacing"
  137. (integer :tag "Number of blank lines before contents")
  138. (integer :tag "Number of blank lines after contents"))))
  139. (defcustom org-e-ascii-charset 'ascii
  140. "The charset allowed to represent various elements and objects.
  141. Possible values are:
  142. `ascii' Only use plain ASCII characters
  143. `latin1' Include Latin-1 characters
  144. `utf-8' Use all UTF-8 characters"
  145. :group 'org-export-e-ascii
  146. :type '(choice
  147. (const :tag "ASCII" ascii)
  148. (const :tag "Latin-1" latin1)
  149. (const :tag "UTF-8" utf-8)))
  150. (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
  151. (latin1 ?= ?~ ?-)
  152. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  153. "Characters for underlining headings in ASCII export.
  154. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  155. and whose value is a list of characters.
  156. For each supported charset, this variable associates a sequence
  157. of underline characters. In a sequence, the characters will be
  158. used in order for headlines level 1, 2, ... If no character is
  159. available for a given level, the headline won't be underlined."
  160. :group 'org-export-e-ascii
  161. :type '(list
  162. (cons :tag "Underline characters sequence"
  163. (const :tag "ASCII charset" ascii)
  164. (repeat character))
  165. (cons :tag "Underline characters sequence"
  166. (const :tag "Latin-1 charset" latin1)
  167. (repeat character))
  168. (cons :tag "Underline characters sequence"
  169. (const :tag "UTF-8 charset" utf-8)
  170. (repeat character))))
  171. (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
  172. (latin1 ?§ ?¶)
  173. (utf-8 ?◊))
  174. "Bullet characters for headlines converted to lists in ASCII export.
  175. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  176. and whose value is a list of characters.
  177. The first character is used for the first level considered as low
  178. level, and so on. If there are more levels than characters given
  179. here, the list will be repeated.
  180. Note that this variable doesn't affect plain lists
  181. representation."
  182. :group 'org-export-e-ascii
  183. :type '(list
  184. (cons :tag "Bullet characters for low level headlines"
  185. (const :tag "ASCII charset" ascii)
  186. (repeat character))
  187. (cons :tag "Bullet characters for low level headlines"
  188. (const :tag "Latin-1 charset" latin1)
  189. (repeat character))
  190. (cons :tag "Bullet characters for low level headlines"
  191. (const :tag "UTF-8 charset" utf-8)
  192. (repeat character))))
  193. (defcustom org-e-ascii-links-to-notes t
  194. "Non-nil means convert links to notes before the next headline.
  195. When nil, the link will be exported in place. If the line
  196. becomes long in this way, it will be wrapped."
  197. :group 'org-export-e-ascii
  198. :type 'boolean)
  199. (defcustom org-e-ascii-table-keep-all-vertical-lines nil
  200. "Non-nil means keep all vertical lines in ASCII tables.
  201. When nil, vertical lines will be removed except for those needed
  202. for column grouping."
  203. :group 'org-export-e-ascii
  204. :type 'boolean)
  205. (defcustom org-e-ascii-table-widen-columns t
  206. "Non-nil means widen narrowed columns for export.
  207. When nil, narrowed columns will look in ASCII export just like in
  208. Org mode, i.e. with \"=>\" as ellipsis."
  209. :group 'org-export-e-ascii
  210. :type 'boolean)
  211. (defcustom org-e-ascii-table-use-ascii-art nil
  212. "Non-nil means table.el tables are turned into ascii-art.
  213. It only makes sense when export charset is `utf-8'. It is nil by
  214. default since it requires ascii-art-to-unicode.el package. You
  215. can download it here:
  216. http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
  217. (defcustom org-e-ascii-caption-above nil
  218. "When non-nil, place caption string before the element.
  219. Otherwise, place it right after it."
  220. :group 'org-export-e-ascii
  221. :type 'boolean)
  222. (defcustom org-e-ascii-verbatim-format "`%s'"
  223. "Format string used for verbatim text and inline code."
  224. :group 'org-export-e-ascii
  225. :type 'string)
  226. (defcustom org-e-ascii-format-drawer-function nil
  227. "Function called to format a drawer in ASCII.
  228. The function must accept two parameters:
  229. NAME the drawer name, like \"LOGBOOK\"
  230. CONTENTS the contents of the drawer.
  231. WIDTH the text width within the drawer.
  232. The function should return either the string to be exported or
  233. nil to ignore the drawer.
  234. For example, the variable could be set to the following function
  235. in order to mimic default behaviour:
  236. \(defun org-e-ascii-format-drawer-default \(name contents width\)
  237. \"Format a drawer element for ASCII export.\"
  238. contents\)"
  239. :group 'org-export-e-ascii
  240. :type 'function)
  241. (defcustom org-e-ascii-format-inlinetask-function nil
  242. "Function called to format an inlinetask in ASCII.
  243. The function must accept six parameters:
  244. TODO the todo keyword, as a string
  245. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  246. PRIORITY the inlinetask priority, as a string
  247. NAME the inlinetask name, as a string.
  248. TAGS the inlinetask tags, as a list of strings.
  249. CONTENTS the contents of the inlinetask, as a string.
  250. The function should return either the string to be exported or
  251. nil to ignore the inline task.
  252. For example, the variable could be set to the following function
  253. in order to mimic default behaviour:
  254. \(defun org-e-ascii-format-inlinetask-default
  255. \(todo type priority name tags contents\)
  256. \"Format an inline task element for ASCII export.\"
  257. \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
  258. \(width org-e-ascii-inlinetask-width\)
  259. \(org-e-ascii--indent-string
  260. \(concat
  261. ;; Top line, with an additional blank line if not in UTF-8.
  262. \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
  263. \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
  264. ;; Add title. Fill it if wider than inlinetask.
  265. \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
  266. \(if \(<= \(length title\) width\) title
  267. \(org-e-ascii--fill-string title width info\)\)\)
  268. \"\\n\"
  269. ;; If CONTENTS is not empty, insert it along with
  270. ;; a separator.
  271. \(when \(org-string-nw-p contents\)
  272. \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
  273. ;; Bottom line.
  274. \(make-string width \(if utf8p ?━ ?_\)\)\)
  275. ;; Flush the inlinetask to the right.
  276. \(- \(plist-get info :ascii-width\)
  277. \(plist-get info :ascii-margin\)
  278. \(plist-get info :ascii-inner-margin\)
  279. \(org-e-ascii--current-text-width inlinetask info\)\)"
  280. :group 'org-export-e-ascii
  281. :type 'function)
  282. ;;; Internal Functions
  283. ;; Internal functions fall into three categories.
  284. ;; The first one is about text formatting. The core function is
  285. ;; `org-e-ascii--current-text-width', which determines the current
  286. ;; text width allowed to a given element. In other words, it helps
  287. ;; keeping each line width within maximum text width defined in
  288. ;; `org-e-ascii-text-width'. Once this information is known,
  289. ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
  290. ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
  291. ;; operate on a given output string.
  292. ;; The second category contains functions handling elements listings,
  293. ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
  294. ;; returns a complete table of contents, `org-e-ascii--list-listings'
  295. ;; returns a list of referenceable src-block elements, and
  296. ;; `org-e-ascii--list-tables' does the same for table elements.
  297. ;; The third category includes general helper functions.
  298. ;; `org-e-ascii--build-title' creates the title for a given headline
  299. ;; or inlinetask element. `org-e-ascii--build-caption' returns the
  300. ;; caption string associated to a table or a src-block.
  301. ;; `org-e-ascii--describe-links' creates notes about links for
  302. ;; insertion at the end of a section. It uses
  303. ;; `org-e-ascii--unique-links' to get the list of links to describe.
  304. ;; Eventually, `org-e-ascii--translate' translates a string according
  305. ;; to language and charset specification.
  306. (defun org-e-ascii--fill-string (s text-width info &optional justify)
  307. "Fill a string with specified text-width and return it.
  308. S is the string being filled. TEXT-WIDTH is an integer
  309. specifying maximum length of a line. INFO is the plist used as
  310. a communication channel.
  311. Optional argument JUSTIFY can specify any type of justification
  312. among `left', `center', `right' or `full'. A nil value is
  313. equivalent to `left'. For a justification that doesn't also fill
  314. string, see `org-e-ascii--justify-string'.
  315. Return nil if S isn't a string."
  316. ;; Don't fill paragraph when break should be preserved.
  317. (cond ((not (stringp s)) nil)
  318. ((plist-get info :preserve-breaks) s)
  319. (t (with-temp-buffer
  320. (let ((fill-column text-width)
  321. (use-hard-newlines t))
  322. (insert s)
  323. (fill-region (point-min) (point-max) justify))
  324. (buffer-string)))))
  325. (defun org-e-ascii--justify-string (s text-width how)
  326. "Justify string S.
  327. TEXT-WIDTH is an integer specifying maximum length of a line.
  328. HOW determines the type of justification: it can be `left',
  329. `right', `full' or `center'."
  330. (with-temp-buffer
  331. (insert s)
  332. (goto-char (point-min))
  333. (let ((fill-column text-width))
  334. (while (< (point) (point-max))
  335. (justify-current-line how)
  336. (forward-line)))
  337. (buffer-string)))
  338. (defun org-e-ascii--indent-string (s width)
  339. "Indent string S by WIDTH white spaces.
  340. Empty lines are not indented."
  341. (when (stringp s)
  342. (replace-regexp-in-string
  343. "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
  344. (defun org-e-ascii--box-string (s info)
  345. "Return string S with a partial box to its left.
  346. INFO is a plist used as a communicaton channel."
  347. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  348. (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
  349. (replace-regexp-in-string
  350. "^" (if utf8p "│ " "| ")
  351. ;; Remove last newline character.
  352. (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
  353. (defun org-e-ascii--current-text-width (element info)
  354. "Return maximum text width for ELEMENT's contents.
  355. INFO is a plist used as a communication channel."
  356. (case (org-element-type element)
  357. ;; Elements with an absolute width: `headline' and `inlinetask'.
  358. (inlinetask org-e-ascii-inlinetask-width)
  359. ('headline
  360. (- org-e-ascii-text-width
  361. (let ((low-level-rank (org-export-low-level-p element info)))
  362. (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
  363. ;; Elements with a relative width: store maximum text width in
  364. ;; TOTAL-WIDTH.
  365. (otherwise
  366. (let* ((genealogy (cons element (org-export-get-genealogy element)))
  367. ;; Total width is determined by the presence, or not, of an
  368. ;; inline task among ELEMENT parents.
  369. (total-width
  370. (if (loop for parent in genealogy
  371. thereis (eq (org-element-type parent) 'inlinetask))
  372. org-e-ascii-inlinetask-width
  373. ;; No inlinetask: Remove global margin from text width.
  374. (- org-e-ascii-text-width
  375. org-e-ascii-global-margin
  376. (let ((parent (org-export-get-parent-headline element)))
  377. ;; Inner margin doesn't apply to text before first
  378. ;; headline.
  379. (if (not parent) 0
  380. (let ((low-level-rank
  381. (org-export-low-level-p parent info)))
  382. ;; Inner margin doesn't apply to contents of
  383. ;; low level headlines, since they've got their
  384. ;; own indentation mechanism.
  385. (if low-level-rank (* low-level-rank 2)
  386. org-e-ascii-inner-margin))))))))
  387. (- total-width
  388. ;; Each `quote-block', `quote-section' and `verse-block' above
  389. ;; narrows text width by twice the standard margin size.
  390. (+ (* (loop for parent in genealogy
  391. when (memq (org-element-type parent)
  392. '(quote-block quote-section verse-block))
  393. count parent)
  394. 2 org-e-ascii-quote-margin)
  395. ;; Text width within a plain-list is restricted by
  396. ;; indentation of current item. If that's the case,
  397. ;; compute it with the help of `:structure' property from
  398. ;; parent item, if any.
  399. (let ((parent-item
  400. (if (eq (org-element-type element) 'item) element
  401. (loop for parent in genealogy
  402. when (eq (org-element-type parent) 'item)
  403. return parent))))
  404. (if (not parent-item) 0
  405. ;; Compute indentation offset of the current item,
  406. ;; that is the sum of the difference between its
  407. ;; indentation and the indentation of the top item in
  408. ;; the list and current item bullet's length. Also
  409. ;; remove checkbox length, and tag length (for
  410. ;; description lists) or bullet length.
  411. (let ((struct (org-element-property :structure parent-item))
  412. (beg-item (org-element-property :begin parent-item)))
  413. (+ (- (org-list-get-ind beg-item struct)
  414. (org-list-get-ind
  415. (org-list-get-top-point struct) struct))
  416. (length (org-e-ascii--checkbox parent-item info))
  417. (length
  418. (or (org-list-get-tag beg-item struct)
  419. (org-list-get-bullet beg-item struct)))))))))))))
  420. (defun org-e-ascii--build-title
  421. (element info text-width &optional underline notags)
  422. "Format ELEMENT title and return it.
  423. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  424. a plist used as a communication channel. TEXT-WIDTH is an
  425. integer representing the maximum length of a line.
  426. When optional argument UNDERLINE is non-nil, underline title,
  427. without the tags, according to `org-e-ascii-underline'
  428. specifications.
  429. if optional argument NOTAGS is nil, no tags will be added to the
  430. title."
  431. (let* ((headlinep (eq (org-element-type element) 'headline))
  432. (numbers
  433. ;; Numbering is specific to headlines.
  434. (and headlinep (org-export-numbered-headline-p element info)
  435. ;; All tests passed: build numbering string.
  436. (concat
  437. (mapconcat
  438. 'number-to-string
  439. (org-export-get-headline-number element info) ".")
  440. " ")))
  441. (text (org-export-data (org-element-property :title element) info))
  442. (todo
  443. (and (plist-get info :with-todo-keywords)
  444. (let ((todo (org-element-property :todo-keyword element)))
  445. (and todo (concat (org-export-data todo info) " ")))))
  446. (tags (and (not notags)
  447. (plist-get info :with-tags)
  448. (let ((tag-list (org-export-get-tags element info)))
  449. (and tag-list
  450. (format ":%s:"
  451. (mapconcat 'identity tag-list ":"))))))
  452. (priority
  453. (and (plist-get info :with-priority)
  454. (let ((char (org-element-property :priority element)))
  455. (and char (format "(#%c) " char)))))
  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 (or (eq (caar (org-element-contents headline)) 'section))
  923. (let ((title (org-element-property :title headline)))
  924. (when (consp title)
  925. (org-e-ascii--describe-links
  926. (org-e-ascii--unique-links title info) width info))))))
  927. ;; Deep subtree: export it as a list item.
  928. (if low-level-rank
  929. (concat
  930. ;; Bullet.
  931. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  932. org-e-ascii-bullets))))
  933. (char-to-string
  934. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  935. " "
  936. ;; Title.
  937. (org-e-ascii--build-title headline info width) "\n"
  938. ;; Contents, indented by length of bullet.
  939. pre-blanks
  940. (org-e-ascii--indent-string
  941. (concat contents
  942. (when (org-string-nw-p links) (concat "\n\n" links)))
  943. 2))
  944. ;; Else: Standard headline.
  945. (concat
  946. (org-e-ascii--build-title headline info width 'underline)
  947. "\n" pre-blanks
  948. (concat (when (org-string-nw-p links) links) contents))))))
  949. ;;;; Horizontal Rule
  950. (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  951. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  952. CONTENTS is nil. INFO is a plist holding contextual
  953. information."
  954. (let ((text-width (org-e-ascii--current-text-width horizontal-rule info))
  955. (spec-width
  956. (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  957. (org-e-ascii--justify-string
  958. (make-string (if (wholenump spec-width) spec-width text-width)
  959. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
  960. text-width 'center)))
  961. ;;;; Inline Babel Call
  962. ;; Inline Babel Calls are ignored.
  963. ;;;; Inline Src Block
  964. (defun org-e-ascii-inline-src-block (inline-src-block contents info)
  965. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  966. CONTENTS holds the contents of the item. INFO is a plist holding
  967. contextual information."
  968. (format org-e-ascii-verbatim-format
  969. (org-element-property :value inline-src-block)))
  970. ;;;; Inlinetask
  971. (defun org-e-ascii-inlinetask (inlinetask contents info)
  972. "Transcode an INLINETASK element from Org to ASCII.
  973. CONTENTS holds the contents of the block. INFO is a plist
  974. holding contextual information."
  975. (let ((width (org-e-ascii--current-text-width inlinetask info)))
  976. ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
  977. ;; with appropriate arguments.
  978. (if (functionp org-e-ascii-format-inlinetask-function)
  979. (funcall org-e-ascii-format-inlinetask-function
  980. ;; todo.
  981. (and (plist-get info :with-todo-keywords)
  982. (let ((todo (org-element-property
  983. :todo-keyword inlinetask)))
  984. (and todo (org-export-data todo info))))
  985. ;; todo-type
  986. (org-element-property :todo-type inlinetask)
  987. ;; priority
  988. (and (plist-get info :with-priority)
  989. (org-element-property :priority inlinetask))
  990. ;; title
  991. (org-export-data (org-element-property :title inlinetask) info)
  992. ;; tags
  993. (and (plist-get info :with-tags)
  994. (org-element-property :tags inlinetask))
  995. ;; contents and width
  996. contents width)
  997. ;; Otherwise, use a default template.
  998. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  999. (org-e-ascii--indent-string
  1000. (concat
  1001. ;; Top line, with an additional blank line if not in UTF-8.
  1002. (make-string width (if utf8p ?━ ?_)) "\n"
  1003. (unless utf8p (concat (make-string width ? ) "\n"))
  1004. ;; Add title. Fill it if wider than inlinetask.
  1005. (let ((title (org-e-ascii--build-title inlinetask info width)))
  1006. (if (<= (length title) width) title
  1007. (org-e-ascii--fill-string title width info)))
  1008. "\n"
  1009. ;; If CONTENTS is not empty, insert it along with
  1010. ;; a separator.
  1011. (when (org-string-nw-p contents)
  1012. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1013. ;; Bottom line.
  1014. (make-string width (if utf8p ?━ ?_)))
  1015. ;; Flush the inlinetask to the right.
  1016. (- org-e-ascii-text-width org-e-ascii-global-margin
  1017. (if (not (org-export-get-parent-headline inlinetask)) 0
  1018. org-e-ascii-inner-margin)
  1019. (org-e-ascii--current-text-width inlinetask info)))))))
  1020. ;;;; Italic
  1021. (defun org-e-ascii-italic (italic contents info)
  1022. "Transcode italic from Org to ASCII.
  1023. CONTENTS is the text with italic markup. INFO is a plist holding
  1024. contextual information."
  1025. (format "/%s/" contents))
  1026. ;;;; Item
  1027. (defun org-e-ascii-item (item contents info)
  1028. "Transcode an ITEM element from Org to ASCII.
  1029. CONTENTS holds the contents of the item. INFO is a plist holding
  1030. contextual information."
  1031. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1032. (checkbox (org-e-ascii--checkbox item info))
  1033. (list-type (org-element-property :type (org-export-get-parent item)))
  1034. (bullet
  1035. ;; First parent of ITEM is always the plain-list. Get
  1036. ;; `:type' property from it.
  1037. (org-list-bullet-string
  1038. (case list-type
  1039. (descriptive
  1040. (concat checkbox
  1041. (org-export-data (org-element-property :tag item) info)
  1042. ": "))
  1043. (ordered
  1044. ;; Return correct number for ITEM, paying attention to
  1045. ;; counters.
  1046. (let* ((struct (org-element-property :structure item))
  1047. (bul (org-element-property :bullet item))
  1048. (num (number-to-string
  1049. (car (last (org-list-get-item-number
  1050. (org-element-property :begin item)
  1051. struct
  1052. (org-list-prevs-alist struct)
  1053. (org-list-parents-alist struct)))))))
  1054. (replace-regexp-in-string "[0-9]+" num bul)))
  1055. (t (let ((bul (org-element-property :bullet item)))
  1056. ;; Change bullets into more visible form if UTF-8 is active.
  1057. (if (not utf8p) bul
  1058. (replace-regexp-in-string
  1059. "-" "•"
  1060. (replace-regexp-in-string
  1061. "+" "⁃"
  1062. (replace-regexp-in-string "*" "‣" bul))))))))))
  1063. (concat
  1064. bullet
  1065. (unless (eq list-type 'descriptive) checkbox)
  1066. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1067. ;; already taken care of at the paragraph level so they don't
  1068. ;; interfere with indentation.
  1069. (let ((contents (org-e-ascii--indent-string contents (length bullet))))
  1070. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1071. (org-trim contents)
  1072. (concat "\n" contents))))))
  1073. ;;;; Keyword
  1074. (defun org-e-ascii-keyword (keyword contents info)
  1075. "Transcode a KEYWORD element from Org to ASCII.
  1076. CONTENTS is nil. INFO is a plist holding contextual
  1077. information."
  1078. (let ((key (org-element-property :key keyword))
  1079. (value (org-element-property :value keyword)))
  1080. (cond
  1081. ((string= key "ASCII") value)
  1082. ((string= key "TOC")
  1083. (let ((value (downcase value)))
  1084. (cond
  1085. ((string-match "\\<headlines\\>" value)
  1086. (let ((depth (or (and (string-match "[0-9]+" value)
  1087. (string-to-number (match-string 0 value)))
  1088. (plist-get info :with-toc))))
  1089. (org-e-ascii--build-toc
  1090. info (and (wholenump depth) depth) keyword)))
  1091. ((string= "tables" value)
  1092. (org-e-ascii--list-tables keyword info))
  1093. ((string= "listings" value)
  1094. (org-e-ascii--list-listings keyword info))))))))
  1095. ;;;; Latex Environment
  1096. (defun org-e-ascii-latex-environment (latex-environment contents info)
  1097. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1098. CONTENTS is nil. INFO is a plist holding contextual
  1099. information."
  1100. (org-remove-indentation (org-element-property :value latex-environment)))
  1101. ;;;; Latex Fragment
  1102. (defun org-e-ascii-latex-fragment (latex-fragment contents info)
  1103. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1104. CONTENTS is nil. INFO is a plist holding contextual
  1105. information."
  1106. (org-element-property :value latex-fragment))
  1107. ;;;; Line Break
  1108. (defun org-e-ascii-line-break (line-break contents info)
  1109. "Transcode a LINE-BREAK object from Org to ASCII.
  1110. CONTENTS is nil. INFO is a plist holding contextual
  1111. information." hard-newline)
  1112. ;;;; Link
  1113. (defun org-e-ascii-link (link desc info)
  1114. "Transcode a LINK object from Org to ASCII.
  1115. DESC is the description part of the link, or the empty string.
  1116. INFO is a plist holding contextual information."
  1117. (let ((raw-link (org-element-property :raw-link link))
  1118. (type (org-element-property :type link)))
  1119. (cond
  1120. ((string= type "coderef")
  1121. (let ((ref (org-element-property :path link)))
  1122. (format (org-export-get-coderef-format ref desc)
  1123. (org-export-resolve-coderef ref info))))
  1124. ;; Do not apply a special syntax on radio links. Though, use
  1125. ;; transcoded target's contents as output.
  1126. ((string= type "radio")
  1127. (let ((destination (org-export-resolve-radio-link link info)))
  1128. (when destination
  1129. (org-export-data (org-element-contents destination) info))))
  1130. ;; Do not apply a special syntax on fuzzy links pointing to
  1131. ;; targets.
  1132. ((string= type "fuzzy")
  1133. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1134. ;; Ignore invisible "#+TARGET: path".
  1135. (unless (eq (org-element-type destination) 'keyword)
  1136. (if (org-string-nw-p desc) desc
  1137. (when destination
  1138. (let ((number
  1139. (org-export-get-ordinal
  1140. destination info nil 'org-e-ascii--has-caption-p)))
  1141. (when number
  1142. (if (atom number) (number-to-string number)
  1143. (mapconcat 'number-to-string number ".")))))))))
  1144. (t
  1145. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1146. (concat
  1147. (format "[%s]" desc)
  1148. (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1149. ;;;; Macro
  1150. (defun org-e-ascii-macro (macro contents info)
  1151. "Transcode a MACRO element from Org to ASCII.
  1152. CONTENTS is nil. INFO is a plist holding contextual
  1153. information."
  1154. (org-export-expand-macro macro info))
  1155. ;;;; Paragraph
  1156. (defun org-e-ascii-paragraph (paragraph contents info)
  1157. "Transcode a PARAGRAPH element from Org to ASCII.
  1158. CONTENTS is the contents of the paragraph, as a string. INFO is
  1159. the plist used as a communication channel."
  1160. (org-e-ascii--fill-string
  1161. contents
  1162. (org-e-ascii--current-text-width paragraph info) info))
  1163. ;;;; Plain List
  1164. (defun org-e-ascii-plain-list (plain-list contents info)
  1165. "Transcode a PLAIN-LIST element from Org to ASCII.
  1166. CONTENTS is the contents of the list. INFO is a plist holding
  1167. contextual information."
  1168. contents)
  1169. ;;;; Plain Text
  1170. (defun org-e-ascii-plain-text (text info)
  1171. "Transcode a TEXT string from Org to ASCII.
  1172. INFO is a plist used as a communication channel."
  1173. (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
  1174. (plist-get info :with-special-strings)))
  1175. text
  1176. ;; Usual replacements in utf-8 with proper option set.
  1177. (replace-regexp-in-string
  1178. "\\.\\.\\." "…"
  1179. (replace-regexp-in-string
  1180. "--" "–"
  1181. (replace-regexp-in-string "---" "—" text)))))
  1182. ;;;; Planning
  1183. (defun org-e-ascii-planning (planning contents info)
  1184. "Transcode a PLANNING element from Org to ASCII.
  1185. CONTENTS is nil. INFO is a plist used as a communication
  1186. channel."
  1187. (mapconcat
  1188. 'identity
  1189. (delq nil
  1190. (list (let ((closed (org-element-property :closed planning)))
  1191. (when closed (concat org-closed-string " "
  1192. (org-translate-time closed))))
  1193. (let ((deadline (org-element-property :deadline planning)))
  1194. (when deadline (concat org-deadline-string " "
  1195. (org-translate-time deadline))))
  1196. (let ((scheduled (org-element-property :scheduled planning)))
  1197. (when scheduled (concat org-scheduled-string " "
  1198. (org-translate-time scheduled))))))
  1199. " "))
  1200. ;;;; Property Drawer
  1201. ;;
  1202. ;; Property drawers are ignored.
  1203. ;;;; Quote Block
  1204. (defun org-e-ascii-quote-block (quote-block contents info)
  1205. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1206. CONTENTS holds the contents of the block. INFO is a plist
  1207. holding contextual information."
  1208. (let ((width (org-e-ascii--current-text-width quote-block info)))
  1209. (org-e-ascii--indent-string
  1210. (org-remove-indentation
  1211. (org-e-ascii--fill-string contents width info))
  1212. org-e-ascii-quote-margin)))
  1213. ;;;; Quote Section
  1214. (defun org-e-ascii-quote-section (quote-section contents info)
  1215. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1216. CONTENTS is nil. INFO is a plist holding contextual information."
  1217. (let ((width (org-e-ascii--current-text-width quote-section info))
  1218. (value
  1219. (org-export-data
  1220. (org-remove-indentation (org-element-property :value quote-section))
  1221. info)))
  1222. (org-e-ascii--indent-string
  1223. value
  1224. (+ org-e-ascii-quote-margin
  1225. ;; Don't apply inner margin if parent headline is low level.
  1226. (let ((headline (org-export-get-parent-headline quote-section)))
  1227. (if (org-export-low-level-p headline info) 0
  1228. org-e-ascii-inner-margin))))))
  1229. ;;;; Radio Target
  1230. (defun org-e-ascii-radio-target (radio-target contents info)
  1231. "Transcode a RADIO-TARGET object from Org to ASCII.
  1232. CONTENTS is the contents of the target. INFO is a plist holding
  1233. contextual information."
  1234. contents)
  1235. ;;;; Section
  1236. (defun org-e-ascii-section (section contents info)
  1237. "Transcode a SECTION element from Org to ASCII.
  1238. CONTENTS is the contents of the section. INFO is a plist holding
  1239. contextual information."
  1240. (org-e-ascii--indent-string
  1241. (concat
  1242. contents
  1243. (when org-e-ascii-links-to-notes
  1244. ;; Add list of links at the end of SECTION.
  1245. (let ((links (org-e-ascii--describe-links
  1246. (org-e-ascii--unique-links section info)
  1247. (org-e-ascii--current-text-width section info) info)))
  1248. ;; Separate list of links and section contents.
  1249. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1250. ;; Do not apply inner margin if parent headline is low level.
  1251. (let ((headline (org-export-get-parent-headline section)))
  1252. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1253. org-e-ascii-inner-margin))))
  1254. ;;;; Special Block
  1255. (defun org-e-ascii-special-block (special-block contents info)
  1256. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1257. CONTENTS holds the contents of the block. INFO is a plist
  1258. holding contextual information."
  1259. contents)
  1260. ;;;; Src Block
  1261. (defun org-e-ascii-src-block (src-block contents info)
  1262. "Transcode a SRC-BLOCK element from Org to ASCII.
  1263. CONTENTS holds the contents of the item. INFO is a plist holding
  1264. contextual information."
  1265. (let ((caption (org-e-ascii--build-caption src-block info)))
  1266. (concat
  1267. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1268. (org-e-ascii--box-string
  1269. (org-export-format-code-default src-block info) info)
  1270. (when (and caption (not org-e-ascii-caption-above))
  1271. (concat "\n" caption)))))
  1272. ;;;; Statistics Cookie
  1273. (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  1274. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1275. CONTENTS is nil. INFO is a plist holding contextual information."
  1276. (org-element-property :value statistics-cookie))
  1277. ;;;; Subscript
  1278. (defun org-e-ascii-subscript (subscript contents info)
  1279. "Transcode a SUBSCRIPT object from Org to ASCII.
  1280. CONTENTS is the contents of the object. INFO is a plist holding
  1281. contextual information."
  1282. (if (org-element-property :use-brackets-p subscript)
  1283. (format "_{%s}" contents)
  1284. (format "_%s" contents)))
  1285. ;;;; Superscript
  1286. (defun org-e-ascii-superscript (superscript contents info)
  1287. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1288. CONTENTS is the contents of the object. INFO is a plist holding
  1289. contextual information."
  1290. (if (org-element-property :use-brackets-p superscript)
  1291. (format "_{%s}" contents)
  1292. (format "_%s" contents)))
  1293. ;;;; Strike-through
  1294. (defun org-e-ascii-strike-through (strike-through contents info)
  1295. "Transcode STRIKE-THROUGH from Org to ASCII.
  1296. CONTENTS is text with strike-through markup. INFO is a plist
  1297. holding contextual information."
  1298. (format "+%s+" contents))
  1299. ;;;; Table
  1300. (defun org-e-ascii-table (table contents info)
  1301. "Transcode a TABLE element from Org to ASCII.
  1302. CONTENTS is the contents of the table. INFO is a plist holding
  1303. contextual information."
  1304. (let ((caption (org-e-ascii--build-caption table info)))
  1305. (concat
  1306. ;; Possibly add a caption string above.
  1307. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1308. ;; Insert table. Note: "table.el" tables are left unmodified.
  1309. (cond ((eq (org-element-property :type table) 'org) contents)
  1310. ((and org-e-ascii-table-use-ascii-art
  1311. (eq (plist-get info :ascii-charset) 'utf-8)
  1312. (require 'ascii-art-to-unicode nil t))
  1313. (with-temp-buffer
  1314. (insert (org-remove-indentation
  1315. (org-element-property :value table)))
  1316. (goto-char (point-min))
  1317. (aa2u)
  1318. (goto-char (point-max))
  1319. (skip-chars-backward " \r\t\n")
  1320. (buffer-string)))
  1321. (t (org-remove-indentation (org-element-property :value table))))
  1322. ;; Possible add a caption string below.
  1323. (when (and caption (not org-e-ascii-caption-above))
  1324. (concat "\n" caption)))))
  1325. ;;;; Table Cell
  1326. (defun org-e-ascii--table-cell-width (table-cell info)
  1327. "Return width of TABLE-CELL.
  1328. INFO is a plist used as a communication channel.
  1329. Width of a cell is determined either by a width cookie in the
  1330. same column as the cell, or by the maximum cell's length in that
  1331. column.
  1332. When `org-e-ascii-table-widen-columns' is non-nil, width cookies
  1333. are ignored."
  1334. (or (and (not org-e-ascii-table-widen-columns)
  1335. (org-export-table-cell-width table-cell info))
  1336. (let* ((max-width 0)
  1337. (table (org-export-get-parent-table table-cell))
  1338. (specialp (org-export-table-has-special-column-p table))
  1339. (col (cdr (org-export-table-cell-address table-cell info))))
  1340. (org-element-map
  1341. table 'table-row
  1342. (lambda (row)
  1343. (setq max-width
  1344. (max (length
  1345. (org-export-data
  1346. (org-element-contents
  1347. (elt (if specialp (cdr (org-element-contents row))
  1348. (org-element-contents row))
  1349. col))
  1350. info))
  1351. max-width)))
  1352. info)
  1353. max-width)))
  1354. (defun org-e-ascii-table-cell (table-cell contents info)
  1355. "Transcode a TABLE-CELL object from Org to ASCII.
  1356. CONTENTS is the cell contents. INFO is a plist used as
  1357. a communication channel."
  1358. ;; Determine column width. When `org-e-ascii-table-widen-columns'
  1359. ;; is nil and some width cookie has set it, use that value.
  1360. ;; Otherwise, compute the maximum width among transcoded data of
  1361. ;; each cell in the column.
  1362. (let ((width (org-e-ascii--table-cell-width table-cell info)))
  1363. ;; When contents are too large, truncate them.
  1364. (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
  1365. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1366. ;; Align contents correctly within the cell.
  1367. (let* ((indent-tabs-mode nil)
  1368. (data
  1369. (when contents
  1370. (org-e-ascii--justify-string
  1371. contents width
  1372. (org-export-table-cell-alignment table-cell info)))))
  1373. (setq contents (concat data (make-string (- width (length data)) ? ))))
  1374. ;; Return cell.
  1375. (concat (format " %s " contents)
  1376. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1377. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1378. ;;;; Table Row
  1379. (defun org-e-ascii-table-row (table-row contents info)
  1380. "Transcode a TABLE-ROW element from Org to ASCII.
  1381. CONTENTS is the row contents. INFO is a plist used as
  1382. a communication channel."
  1383. (when (eq (org-element-property :type table-row) 'standard)
  1384. (let ((build-hline
  1385. (function
  1386. (lambda (lcorner horiz vert rcorner)
  1387. (concat
  1388. (apply
  1389. 'concat
  1390. (org-element-map
  1391. table-row 'table-cell
  1392. (lambda (cell)
  1393. (let ((width (org-e-ascii--table-cell-width cell info))
  1394. (borders (org-export-table-cell-borders cell info)))
  1395. (concat
  1396. ;; In order to know if CELL starts the row, do
  1397. ;; not compare it with the first cell in the row
  1398. ;; as there might be a special column. Instead,
  1399. ;; compare it with the first exportable cell,
  1400. ;; obtained with `org-element-map'.
  1401. (when (and (memq 'left borders)
  1402. (eq (org-element-map
  1403. table-row 'table-cell 'identity info t)
  1404. cell))
  1405. lcorner)
  1406. (make-string (+ 2 width) (string-to-char horiz))
  1407. (cond
  1408. ((not (memq 'right borders)) nil)
  1409. ((eq (car (last (org-element-contents table-row))) cell)
  1410. rcorner)
  1411. (t vert)))))
  1412. info)) "\n"))))
  1413. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1414. (borders (org-export-table-cell-borders
  1415. (org-element-map table-row 'table-cell 'identity info t)
  1416. info)))
  1417. (concat (cond
  1418. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1419. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1420. (funcall build-hline "+" "-" "+" "+")))
  1421. ((memq 'above borders)
  1422. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1423. (funcall build-hline "+" "-" "+" "+"))))
  1424. (when (memq 'left borders) (if utf8p "│" "|"))
  1425. contents "\n"
  1426. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1427. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1428. (funcall build-hline "+" "-" "+" "+")))))))
  1429. ;;;; Target
  1430. ;; Targets are invisible.
  1431. ;;;; Timestamp
  1432. (defun org-e-ascii-timestamp (timestamp contents info)
  1433. "Transcode a TIMESTAMP object from Org to ASCII.
  1434. CONTENTS is nil. INFO is a plist holding contextual information."
  1435. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1436. (range-end
  1437. (org-translate-time (org-element-property :range-end timestamp)))
  1438. (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1439. (concat value
  1440. (when range-end (concat (if utf8p "–" "--") range-end)))))
  1441. ;;;; Underline
  1442. (defun org-e-ascii-underline (underline contents info)
  1443. "Transcode UNDERLINE from Org to ASCII.
  1444. CONTENTS is the text with underline markup. INFO is a plist
  1445. holding contextual information."
  1446. (format "_%s_" contents))
  1447. ;;;; Verbatim
  1448. (defun org-e-ascii-verbatim (verbatim contents info)
  1449. "Return a VERBATIM object from Org to ASCII.
  1450. CONTENTS is nil. INFO is a plist holding contextual information."
  1451. (format org-e-ascii-verbatim-format
  1452. (org-element-property :value verbatim)))
  1453. ;;;; Verse Block
  1454. (defun org-e-ascii-verse-block (verse-block contents info)
  1455. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1456. CONTENTS is verse block contents. INFO is a plist holding
  1457. contextual information."
  1458. (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
  1459. (org-e-ascii--indent-string
  1460. (org-e-ascii--justify-string contents verse-width 'left)
  1461. org-e-ascii-quote-margin)))
  1462. ;;; Filter
  1463. (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  1464. "Filter controlling number of blank lines after an headline.
  1465. HEADLINE is a string representing a transcoded headline.
  1466. BACK-END is symbol specifying back-end used for export. INFO is
  1467. plist containing the communication channel.
  1468. This function only applies to `e-ascii' back-end. See
  1469. `org-e-ascii-headline-spacing' for information.
  1470. For any other back-end, HEADLINE is returned as-is."
  1471. (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
  1472. (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
  1473. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1474. ;;; Interactive function
  1475. ;;;###autoload
  1476. (defun org-e-ascii-export-as-ascii
  1477. (&optional subtreep visible-only body-only ext-plist)
  1478. "Export current buffer to a text buffer.
  1479. If narrowing is active in the current buffer, only export its
  1480. narrowed part.
  1481. If a region is active, export that region.
  1482. When optional argument SUBTREEP is non-nil, export the sub-tree
  1483. at point, extracting information from the headline properties
  1484. first.
  1485. When optional argument VISIBLE-ONLY is non-nil, don't export
  1486. contents of hidden elements.
  1487. When optional argument BODY-ONLY is non-nil, strip title, table
  1488. of contents and footnote definitions from output.
  1489. EXT-PLIST, when provided, is a property list with external
  1490. parameters overriding Org default settings, but still inferior to
  1491. file-local settings.
  1492. Export is done in a buffer named \"*Org E-ASCII Export*\", which
  1493. will be displayed when `org-export-show-temporary-export-buffer'
  1494. is non-nil."
  1495. (interactive)
  1496. (let ((outbuf (org-export-to-buffer
  1497. 'e-ascii "*Org E-ASCII Export*"
  1498. subtreep visible-only body-only ext-plist)))
  1499. (with-current-buffer outbuf (text-mode))
  1500. (when org-export-show-temporary-export-buffer
  1501. (switch-to-buffer-other-window outbuf))))
  1502. ;;;###autoload
  1503. (defun org-e-ascii-export-to-ascii
  1504. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1505. "Export current buffer to a text file.
  1506. If narrowing is active in the current buffer, only export its
  1507. narrowed part.
  1508. If a region is active, export that region.
  1509. When optional argument SUBTREEP is non-nil, export the sub-tree
  1510. at point, extracting information from the headline properties
  1511. first.
  1512. When optional argument VISIBLE-ONLY is non-nil, don't export
  1513. contents of hidden elements.
  1514. When optional argument BODY-ONLY is non-nil, strip title, table
  1515. of contents and footnote definitions from output.
  1516. EXT-PLIST, when provided, is a property list with external
  1517. parameters overriding Org default settings, but still inferior to
  1518. file-local settings.
  1519. When optional argument PUB-DIR is set, use it as the publishing
  1520. directory.
  1521. Return output file's name."
  1522. (interactive)
  1523. (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
  1524. (org-export-to-file
  1525. 'e-ascii outfile subtreep visible-only body-only ext-plist)))
  1526. (provide 'org-e-ascii)
  1527. ;;; org-e-ascii.el ends here