org-e-ascii.el 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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. (declare-function aa2u "ext:ascii-art-to-unicode" ())
  33. ;;; Define Back-End
  34. ;;
  35. ;; The following setting won't allow to modify preferred charset
  36. ;; through a buffer keyword or an option item, but, since the property
  37. ;; will appear in communication channel nonetheless, it allows to
  38. ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
  39. ;; mechanism.
  40. ;;
  41. ;; We also install a filter for headlines and sections, in order to
  42. ;; control blank lines separating them in output string.
  43. (org-export-define-backend e-ascii
  44. ((bold . org-e-ascii-bold)
  45. (center-block . org-e-ascii-center-block)
  46. (clock . org-e-ascii-clock)
  47. (code . org-e-ascii-code)
  48. (drawer . org-e-ascii-drawer)
  49. (dynamic-block . org-e-ascii-dynamic-block)
  50. (entity . org-e-ascii-entity)
  51. (example-block . org-e-ascii-example-block)
  52. (export-block . org-e-ascii-export-block)
  53. (export-snippet . org-e-ascii-export-snippet)
  54. (fixed-width . org-e-ascii-fixed-width)
  55. (footnote-definition . org-e-ascii-footnote-definition)
  56. (footnote-reference . org-e-ascii-footnote-reference)
  57. (headline . org-e-ascii-headline)
  58. (horizontal-rule . org-e-ascii-horizontal-rule)
  59. (inline-src-block . org-e-ascii-inline-src-block)
  60. (inlinetask . org-e-ascii-inlinetask)
  61. (italic . org-e-ascii-italic)
  62. (item . org-e-ascii-item)
  63. (keyword . org-e-ascii-keyword)
  64. (latex-environment . org-e-ascii-latex-environment)
  65. (latex-fragment . org-e-ascii-latex-fragment)
  66. (line-break . org-e-ascii-line-break)
  67. (link . org-e-ascii-link)
  68. (macro . org-e-ascii-macro)
  69. (paragraph . org-e-ascii-paragraph)
  70. (plain-list . org-e-ascii-plain-list)
  71. (plain-text . org-e-ascii-plain-text)
  72. (planning . org-e-ascii-planning)
  73. (quote-block . org-e-ascii-quote-block)
  74. (quote-section . org-e-ascii-quote-section)
  75. (radio-target . org-e-ascii-radio-target)
  76. (section . org-e-ascii-section)
  77. (special-block . org-e-ascii-special-block)
  78. (src-block . org-e-ascii-src-block)
  79. (statistics-cookie . org-e-ascii-statistics-cookie)
  80. (strike-through . org-e-ascii-strike-through)
  81. (subscript . org-e-ascii-subscript)
  82. (superscript . org-e-ascii-superscript)
  83. (table . org-e-ascii-table)
  84. (table-cell . org-e-ascii-table-cell)
  85. (table-row . org-e-ascii-table-row)
  86. (target . org-e-ascii-target)
  87. (template . org-e-ascii-template)
  88. (timestamp . org-e-ascii-timestamp)
  89. (underline . org-e-ascii-underline)
  90. (verbatim . org-e-ascii-verbatim)
  91. (verse-block . org-e-ascii-verse-block))
  92. :export-block "ASCII"
  93. :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
  94. (:filter-section . org-e-ascii-filter-headline-blank-lines))
  95. :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))
  96. ;;; User Configurable Variables
  97. (defgroup org-export-e-ascii nil
  98. "Options for exporting Org mode files to ASCII."
  99. :tag "Org Export ASCII"
  100. :group 'org-export)
  101. (defcustom org-e-ascii-text-width 72
  102. "Maximum width of exported text.
  103. This number includes margin size, as set in
  104. `org-e-ascii-global-margin'."
  105. :group 'org-export-e-ascii
  106. :type 'integer)
  107. (defcustom org-e-ascii-global-margin 0
  108. "Width of the left margin, in number of characters."
  109. :group 'org-export-e-ascii
  110. :type 'integer)
  111. (defcustom org-e-ascii-inner-margin 2
  112. "Width of the inner margin, in number of characters.
  113. Inner margin is applied between each headline."
  114. :group 'org-export-e-ascii
  115. :type 'integer)
  116. (defcustom org-e-ascii-quote-margin 6
  117. "Width of margin used for quoting text, in characters.
  118. This margin is applied on both sides of the text."
  119. :group 'org-export-e-ascii
  120. :type 'integer)
  121. (defcustom org-e-ascii-inlinetask-width 30
  122. "Width of inline tasks, in number of characters.
  123. This number ignores any margin."
  124. :group 'org-export-e-ascii
  125. :type 'integer)
  126. (defcustom org-e-ascii-headline-spacing '(1 . 2)
  127. "Number of blank lines inserted around headlines.
  128. This variable can be set to a cons cell. In that case, its car
  129. represents the number of blank lines present before headline
  130. contents whereas its cdr reflects the number of blank lines after
  131. contents.
  132. A nil value replicates the number of blank lines found in the
  133. original Org buffer at the same place."
  134. :group 'org-export-e-ascii
  135. :type '(choice
  136. (const :tag "Replicate original spacing" nil)
  137. (cons :tag "Set an uniform spacing"
  138. (integer :tag "Number of blank lines before contents")
  139. (integer :tag "Number of blank lines after contents"))))
  140. (defcustom org-e-ascii-charset 'ascii
  141. "The charset allowed to represent various elements and objects.
  142. Possible values are:
  143. `ascii' Only use plain ASCII characters
  144. `latin1' Include Latin-1 characters
  145. `utf-8' Use all UTF-8 characters"
  146. :group 'org-export-e-ascii
  147. :type '(choice
  148. (const :tag "ASCII" ascii)
  149. (const :tag "Latin-1" latin1)
  150. (const :tag "UTF-8" utf-8)))
  151. (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
  152. (latin1 ?= ?~ ?-)
  153. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  154. "Characters for underlining headings in ASCII export.
  155. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  156. and whose value is a list of characters.
  157. For each supported charset, this variable associates a sequence
  158. of underline characters. In a sequence, the characters will be
  159. used in order for headlines level 1, 2, ... If no character is
  160. available for a given level, the headline won't be underlined."
  161. :group 'org-export-e-ascii
  162. :type '(list
  163. (cons :tag "Underline characters sequence"
  164. (const :tag "ASCII charset" ascii)
  165. (repeat character))
  166. (cons :tag "Underline characters sequence"
  167. (const :tag "Latin-1 charset" latin1)
  168. (repeat character))
  169. (cons :tag "Underline characters sequence"
  170. (const :tag "UTF-8 charset" utf-8)
  171. (repeat character))))
  172. (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
  173. (latin1 ?§ ?¶)
  174. (utf-8 ?◊))
  175. "Bullet characters for headlines converted to lists in ASCII export.
  176. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  177. and whose value is a list of characters.
  178. The first character is used for the first level considered as low
  179. level, and so on. If there are more levels than characters given
  180. here, the list will be repeated.
  181. Note that this variable doesn't affect plain lists
  182. representation."
  183. :group 'org-export-e-ascii
  184. :type '(list
  185. (cons :tag "Bullet characters for low level headlines"
  186. (const :tag "ASCII charset" ascii)
  187. (repeat character))
  188. (cons :tag "Bullet characters for low level headlines"
  189. (const :tag "Latin-1 charset" latin1)
  190. (repeat character))
  191. (cons :tag "Bullet characters for low level headlines"
  192. (const :tag "UTF-8 charset" utf-8)
  193. (repeat character))))
  194. (defcustom org-e-ascii-links-to-notes t
  195. "Non-nil means convert links to notes before the next headline.
  196. When nil, the link will be exported in place. If the line
  197. becomes long in this way, it will be wrapped."
  198. :group 'org-export-e-ascii
  199. :type 'boolean)
  200. (defcustom org-e-ascii-table-keep-all-vertical-lines nil
  201. "Non-nil means keep all vertical lines in ASCII tables.
  202. When nil, vertical lines will be removed except for those needed
  203. for column grouping."
  204. :group 'org-export-e-ascii
  205. :type 'boolean)
  206. (defcustom org-e-ascii-table-widen-columns t
  207. "Non-nil means widen narrowed columns for export.
  208. When nil, narrowed columns will look in ASCII export just like in
  209. Org mode, i.e. with \"=>\" as ellipsis."
  210. :group 'org-export-e-ascii
  211. :type 'boolean)
  212. (defcustom org-e-ascii-table-use-ascii-art nil
  213. "Non-nil means table.el tables are turned into ascii-art.
  214. It only makes sense when export charset is `utf-8'. It is nil by
  215. default since it requires ascii-art-to-unicode.el package. You
  216. can download it here:
  217. http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
  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' translates a string according
  306. ;; to language and charset specification.
  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. (let ((char (org-element-property :priority element)))
  456. (and char (format "(#%c) " char)))))
  457. (first-part (concat numbers todo priority text)))
  458. (concat
  459. first-part
  460. ;; Align tags, if any.
  461. (when tags
  462. (format
  463. (format " %%%ds"
  464. (max (- text-width (1+ (length first-part))) (length tags)))
  465. tags))
  466. ;; Maybe underline text, if ELEMENT type is `headline' and an
  467. ;; underline character has been defined.
  468. (when (and underline headlinep)
  469. (let ((under-char
  470. (nth (1- (org-export-get-relative-level element info))
  471. (cdr (assq (plist-get info :ascii-charset)
  472. org-e-ascii-underline)))))
  473. (and under-char
  474. (concat "\n"
  475. (make-string (length first-part) under-char))))))))
  476. (defun org-e-ascii--has-caption-p (element info)
  477. "Non-nil when ELEMENT has a caption affiliated keyword.
  478. INFO is a plist used as a communication channel. This function
  479. is meant to be used as a predicate for `org-export-get-ordinal'."
  480. (org-element-property :caption element))
  481. (defun org-e-ascii--build-caption (element info)
  482. "Return caption string for ELEMENT, if applicable.
  483. INFO is a plist used as a communication channel.
  484. The caption string contains the sequence number of ELEMENT along
  485. with its real caption. Return nil when ELEMENT has no affiliated
  486. caption keyword."
  487. (let ((caption (org-element-property :caption element)))
  488. (when caption
  489. ;; Get sequence number of current src-block among every
  490. ;; src-block with a caption.
  491. (let ((reference
  492. (org-export-get-ordinal
  493. element info nil 'org-e-ascii--has-caption-p))
  494. (title-fmt (org-e-ascii--translate
  495. (case (org-element-type element)
  496. (table "Table %d: %s")
  497. (src-block "Listing %d: %s"))
  498. info)))
  499. (org-e-ascii--fill-string
  500. (format title-fmt reference (org-export-data (car caption) info))
  501. (org-e-ascii--current-text-width element info) info)))))
  502. (defun org-e-ascii--build-toc (info &optional n keyword)
  503. "Return a table of contents.
  504. INFO is a plist used as a communication channel.
  505. Optional argument N, when non-nil, is an integer specifying the
  506. depth of the table.
  507. Optional argument KEYWORD specifies the TOC keyword, if any, from
  508. which the table of contents generation has been initiated."
  509. (let ((title (org-e-ascii--translate "Table of Contents" info)))
  510. (concat
  511. title "\n"
  512. (make-string (length title)
  513. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  514. "\n\n"
  515. (let ((text-width
  516. (if keyword (org-e-ascii--current-text-width keyword info)
  517. (- org-e-ascii-text-width org-e-ascii-global-margin))))
  518. (mapconcat
  519. (lambda (headline)
  520. (let* ((level (org-export-get-relative-level headline info))
  521. (indent (* (1- level) 3)))
  522. (concat
  523. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  524. (org-e-ascii--build-title
  525. headline info (- text-width indent) nil
  526. (eq (plist-get info :with-tags) 'not-in-toc)))))
  527. (org-export-collect-headlines info n) "\n")))))
  528. (defun org-e-ascii--list-listings (keyword info)
  529. "Return a list of listings.
  530. KEYWORD is the keyword that initiated the list of listings
  531. generation. INFO is a plist used as a communication channel."
  532. (let ((title (org-e-ascii--translate "List of Listings" info)))
  533. (concat
  534. title "\n"
  535. (make-string (length title)
  536. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  537. "\n\n"
  538. (let ((text-width
  539. (if keyword (org-e-ascii--current-text-width keyword info)
  540. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  541. ;; Use a counter instead of retreiving ordinal of each
  542. ;; src-block.
  543. (count 0))
  544. (mapconcat
  545. (lambda (src-block)
  546. ;; Store initial text so its length can be computed. This is
  547. ;; used to properly align caption right to it in case of
  548. ;; filling (like contents of a description list item).
  549. (let ((initial-text
  550. (format (org-e-ascii--translate "Listing %d:" info)
  551. (incf count))))
  552. (concat
  553. initial-text " "
  554. (org-trim
  555. (org-e-ascii--indent-string
  556. (org-e-ascii--fill-string
  557. (let ((caption (org-element-property :caption src-block)))
  558. ;; Use short name in priority, if available.
  559. (org-export-data (or (cdr caption) (car caption)) info))
  560. (- text-width (length initial-text)) info)
  561. (length initial-text))))))
  562. (org-export-collect-listings info) "\n")))))
  563. (defun org-e-ascii--list-tables (keyword info)
  564. "Return a list of listings.
  565. KEYWORD is the keyword that initiated the list of listings
  566. generation. INFO is a plist used as a communication channel."
  567. (let ((title (org-e-ascii--translate "List of Tables" info)))
  568. (concat
  569. title "\n"
  570. (make-string (length title)
  571. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  572. "\n\n"
  573. (let ((text-width
  574. (if keyword (org-e-ascii--current-text-width keyword info)
  575. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  576. ;; Use a counter instead of retreiving ordinal of each
  577. ;; src-block.
  578. (count 0))
  579. (mapconcat
  580. (lambda (table)
  581. ;; Store initial text so its length can be computed. This is
  582. ;; used to properly align caption right to it in case of
  583. ;; filling (like contents of a description list item).
  584. (let ((initial-text
  585. (format (org-e-ascii--translate "Table %d:" info)
  586. (incf count))))
  587. (concat
  588. initial-text " "
  589. (org-trim
  590. (org-e-ascii--indent-string
  591. (org-e-ascii--fill-string
  592. (let ((caption (org-element-property :caption table)))
  593. ;; Use short name in priority, if available.
  594. (org-export-data (or (cdr caption) (car caption)) info))
  595. (- text-width (length initial-text)) info)
  596. (length initial-text))))))
  597. (org-export-collect-tables info) "\n")))))
  598. (defun org-e-ascii--unique-links (element info)
  599. "Return a list of unique link references in ELEMENT.
  600. ELEMENT is either an headline element or a section element. INFO
  601. is a plist used as a communication channel."
  602. (let* (seen
  603. (unique-link-p
  604. (function
  605. ;; Return LINK if it wasn't referenced so far, or nil.
  606. ;; Update SEEN links along the way.
  607. (lambda (link)
  608. (let ((footprint
  609. (cons (org-element-property :raw-link link)
  610. (org-element-contents link))))
  611. (unless (member footprint seen)
  612. (push footprint seen) link)))))
  613. ;; If at a section, find parent headline, if any, in order to
  614. ;; count links that might be in the title.
  615. (headline
  616. (if (eq (org-element-type element) 'headline) element
  617. (or (org-export-get-parent-headline element) element))))
  618. ;; Get all links in HEADLINE.
  619. (org-element-map
  620. headline 'link (lambda (link) (funcall unique-link-p link)) info)))
  621. (defun org-e-ascii--describe-links (links width info)
  622. "Return a string describing a list of links.
  623. LINKS is a list of link type objects, as returned by
  624. `org-e-ascii--unique-links'. WIDTH is the text width allowed for
  625. the output string. INFO is a plist used as a communication
  626. channel."
  627. (mapconcat
  628. (lambda (link)
  629. (let ((type (org-element-property :type link))
  630. (anchor (let ((desc (org-element-contents link)))
  631. (if (not desc) (org-element-property :raw-link link)
  632. (org-export-data desc info)))))
  633. (cond
  634. ;; Coderefs, radio links and fuzzy links are ignored.
  635. ((member type '("coderef" "radio" "fuzzy")) nil)
  636. ;; Id and custom-id links: Headlines refer to their numbering.
  637. ((member type '("custom-id" "id"))
  638. (let ((dest (org-export-resolve-id-link link info)))
  639. (concat
  640. (org-e-ascii--fill-string
  641. (format
  642. "[%s] %s"
  643. anchor
  644. (if (not dest) (org-e-ascii--translate "Unknown reference" info)
  645. (format
  646. (org-e-ascii--translate "See section %s" info)
  647. (mapconcat 'number-to-string
  648. (org-export-get-headline-number dest info) "."))))
  649. width info) "\n\n")))
  650. ;; Do not add a link that cannot be resolved and doesn't have
  651. ;; any description: destination is already visible in the
  652. ;; paragraph.
  653. ((not (org-element-contents link)) nil)
  654. (t
  655. (concat
  656. (org-e-ascii--fill-string
  657. (format "[%s] %s" anchor (org-element-property :raw-link link))
  658. width info)
  659. "\n\n")))))
  660. links ""))
  661. (defun org-e-ascii--checkbox (item info)
  662. "Return checkbox string for ITEM or nil.
  663. INFO is a plist used as a communication channel."
  664. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  665. (case (org-element-property :checkbox item)
  666. (on (if utf8p "☑ " "[X] "))
  667. (off (if utf8p "☐ " "[ ] "))
  668. (trans (if utf8p "☒ " "[-] ")))))
  669. ;;; Template
  670. (defun org-e-ascii-template--document-title (info)
  671. "Return document title, as a string.
  672. INFO is a plist used as a communication channel."
  673. (let ((text-width org-e-ascii-text-width)
  674. (title (org-export-data (plist-get info :title) info))
  675. (author (and (plist-get info :with-author)
  676. (let ((auth (plist-get info :author)))
  677. (and auth (org-export-data auth info)))))
  678. (email (and (plist-get info :with-email)
  679. (org-export-data (plist-get info :email) info)))
  680. (date (org-export-data (plist-get info :date) info)))
  681. ;; There are two types of title blocks depending on the presence
  682. ;; of a title to display.
  683. (if (string= title "")
  684. ;; Title block without a title. DATE is positioned at the top
  685. ;; right of the document, AUTHOR to the top left and EMAIL
  686. ;; just below.
  687. (cond
  688. ((and (org-string-nw-p date) (org-string-nw-p author))
  689. (concat
  690. author
  691. (make-string (- text-width (length date) (length author)) ? )
  692. date
  693. (when (org-string-nw-p email) (concat "\n" email))
  694. "\n\n\n"))
  695. ((and (org-string-nw-p date) (org-string-nw-p email))
  696. (concat
  697. email
  698. (make-string (- text-width (length date) (length email)) ? )
  699. date "\n\n\n"))
  700. ((org-string-nw-p date)
  701. (concat
  702. (org-e-ascii--justify-string date text-width 'right)
  703. "\n\n\n"))
  704. ((and (org-string-nw-p author) (org-string-nw-p email))
  705. (concat author "\n" email "\n\n\n"))
  706. ((org-string-nw-p author) (concat author "\n\n\n"))
  707. ((org-string-nw-p email) (concat email "\n\n\n")))
  708. ;; Title block with a title. Document's TITLE, along with the
  709. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  710. ;; centered. Date is just below, also centered.
  711. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  712. ;; Format TITLE. It may be filled if it is too wide,
  713. ;; that is wider than the two thirds of the total width.
  714. (title-len (min (length title) (/ (* 2 text-width) 3)))
  715. (formatted-title (org-e-ascii--fill-string title title-len info))
  716. (line
  717. (make-string
  718. (min (+ (max title-len (length author) (length email)) 2)
  719. text-width) (if utf8p ?━ ?_))))
  720. (org-e-ascii--justify-string
  721. (concat line "\n"
  722. (unless utf8p "\n")
  723. (upcase formatted-title)
  724. (cond
  725. ((and (org-string-nw-p author) (org-string-nw-p email))
  726. (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
  727. ((org-string-nw-p author)
  728. (concat (if utf8p "\n\n\n" "\n\n") author))
  729. ((org-string-nw-p email)
  730. (concat (if utf8p "\n\n\n" "\n\n") email)))
  731. "\n" line
  732. (when (org-string-nw-p date) (concat "\n\n\n" date))
  733. "\n\n\n") text-width 'center)))))
  734. (defun org-e-ascii-template (contents info)
  735. "Return complete document string after ASCII conversion.
  736. CONTENTS is the transcoded contents string. INFO is a plist
  737. holding export options."
  738. (org-element-normalize-string
  739. (org-e-ascii--indent-string
  740. (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
  741. ;; 1. Build title block.
  742. (concat
  743. (org-e-ascii-template--document-title info)
  744. ;; 2. Table of contents.
  745. (let ((depth (plist-get info :with-toc)))
  746. (when depth
  747. (concat
  748. (org-e-ascii--build-toc info (and (wholenump depth) depth))
  749. "\n\n\n")))
  750. ;; 3. Document's body.
  751. contents
  752. ;; 4. Footnote definitions.
  753. (let ((definitions (org-export-collect-footnote-definitions
  754. (plist-get info :parse-tree) info))
  755. ;; Insert full links right inside the footnote definition
  756. ;; as they have no chance to be inserted later.
  757. (org-e-ascii-links-to-notes nil))
  758. (when definitions
  759. (concat
  760. "\n\n\n"
  761. (let ((title (org-e-ascii--translate "Footnotes" info)))
  762. (concat
  763. title "\n"
  764. (make-string
  765. (length title)
  766. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  767. "\n\n"
  768. (mapconcat
  769. (lambda (ref)
  770. (let ((id (format "[%s] " (car ref))))
  771. ;; Distinguish between inline definitions and
  772. ;; full-fledged definitions.
  773. (org-trim
  774. (let ((def (nth 2 ref)))
  775. (if (eq (org-element-type def) 'org-data)
  776. ;; Full-fledged definition: footnote ID is
  777. ;; inserted inside the first parsed paragraph
  778. ;; (FIRST), if any, to be sure filling will
  779. ;; take it into consideration.
  780. (let ((first (car (org-element-contents def))))
  781. (if (not (eq (org-element-type first) 'paragraph))
  782. (concat id "\n" (org-export-data def info))
  783. (push id (nthcdr 2 first))
  784. (org-export-data def info)))
  785. ;; Fill paragraph once footnote ID is inserted in
  786. ;; order to have a correct length for first line.
  787. (org-e-ascii--fill-string
  788. (concat id (org-export-data def info))
  789. text-width info))))))
  790. definitions "\n\n"))))
  791. ;; 5. Creator. Ignore `comment' value as there are no comments in
  792. ;; ASCII. Justify it to the bottom right.
  793. (let ((creator-info (plist-get info :with-creator)))
  794. (unless (or (not creator-info) (eq creator-info 'comment))
  795. (concat
  796. "\n\n\n"
  797. (org-e-ascii--fill-string
  798. (plist-get info :creator) text-width info 'right))))))
  799. org-e-ascii-global-margin)))
  800. (defun org-e-ascii--translate (s info)
  801. "Translate string S according to specified language and charset.
  802. INFO is a plist used as a communication channel."
  803. (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
  804. (org-export-translate s charset info)))
  805. ;;; Transcode Functions
  806. ;;;; Babel Call
  807. ;; Babel Calls are ignored.
  808. ;;;; Bold
  809. (defun org-e-ascii-bold (bold contents info)
  810. "Transcode BOLD from Org to ASCII.
  811. CONTENTS is the text with bold markup. INFO is a plist holding
  812. contextual information."
  813. (format "*%s*" contents))
  814. ;;;; Center Block
  815. (defun org-e-ascii-center-block (center-block contents info)
  816. "Transcode a CENTER-BLOCK element from Org to ASCII.
  817. CONTENTS holds the contents of the block. INFO is a plist
  818. holding contextual information."
  819. (org-e-ascii--justify-string
  820. contents (org-e-ascii--current-text-width center-block info) 'center))
  821. ;;;; Clock
  822. (defun org-e-ascii-clock (clock contents info)
  823. "Transcode a CLOCK object from Org to ASCII.
  824. CONTENTS is nil. INFO is a plist holding contextual
  825. information."
  826. (concat org-clock-string " "
  827. (org-translate-time (org-element-property :value clock))
  828. (let ((time (org-element-property :time clock)))
  829. (and time
  830. (concat " => "
  831. (apply 'format
  832. "%2s:%02s"
  833. (org-split-string time ":")))))))
  834. ;;;; Code
  835. (defun org-e-ascii-code (code contents info)
  836. "Return a CODE object from Org to ASCII.
  837. CONTENTS is nil. INFO is a plist holding contextual
  838. information."
  839. (format org-e-ascii-verbatim-format (org-element-property :value code)))
  840. ;;;; Comment
  841. ;; Comments are ignored.
  842. ;;;; Comment Block
  843. ;; Comment Blocks are ignored.
  844. ;;;; Drawer
  845. (defun org-e-ascii-drawer (drawer contents info)
  846. "Transcode a DRAWER element from Org to ASCII.
  847. CONTENTS holds the contents of the block. INFO is a plist
  848. holding contextual information."
  849. (let ((name (org-element-property :drawer-name drawer))
  850. (width (org-e-ascii--current-text-width drawer info)))
  851. (if (functionp org-e-ascii-format-drawer-function)
  852. (funcall org-e-ascii-format-drawer-function name contents width)
  853. ;; If there's no user defined function: simply
  854. ;; display contents of the drawer.
  855. contents)))
  856. ;;;; Dynamic Block
  857. (defun org-e-ascii-dynamic-block (dynamic-block contents info)
  858. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  859. CONTENTS holds the contents of the block. INFO is a plist
  860. holding contextual information."
  861. contents)
  862. ;;;; Entity
  863. (defun org-e-ascii-entity (entity contents info)
  864. "Transcode an ENTITY object from Org to ASCII.
  865. CONTENTS are the definition itself. INFO is a plist holding
  866. contextual information."
  867. (org-element-property
  868. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  869. entity))
  870. ;;;; Example Block
  871. (defun org-e-ascii-example-block (example-block contents info)
  872. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  873. CONTENTS is nil. INFO is a plist holding contextual information."
  874. (org-e-ascii--box-string
  875. (org-export-format-code-default example-block info) info))
  876. ;;;; Export Snippet
  877. (defun org-e-ascii-export-snippet (export-snippet contents info)
  878. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  879. CONTENTS is nil. INFO is a plist holding contextual information."
  880. (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
  881. (org-element-property :value export-snippet)))
  882. ;;;; Export Block
  883. (defun org-e-ascii-export-block (export-block contents info)
  884. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  885. CONTENTS is nil. INFO is a plist holding contextual information."
  886. (when (string= (org-element-property :type export-block) "ASCII")
  887. (org-remove-indentation (org-element-property :value export-block))))
  888. ;;;; Fixed Width
  889. (defun org-e-ascii-fixed-width (fixed-width contents info)
  890. "Transcode a FIXED-WIDTH element from Org to ASCII.
  891. CONTENTS is nil. INFO is a plist holding contextual information."
  892. (org-e-ascii--box-string
  893. (org-remove-indentation
  894. (org-element-property :value fixed-width)) info))
  895. ;;;; Footnote Definition
  896. ;; Footnote Definitions are ignored. They are compiled at the end of
  897. ;; the document, by `org-e-ascii-template'.
  898. ;;;; Footnote Reference
  899. (defun org-e-ascii-footnote-reference (footnote-reference contents info)
  900. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  901. CONTENTS is nil. INFO is a plist holding contextual information."
  902. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  903. ;;;; Headline
  904. (defun org-e-ascii-headline (headline contents info)
  905. "Transcode an HEADLINE element from Org to ASCII.
  906. CONTENTS holds the contents of the headline. INFO is a plist
  907. holding contextual information."
  908. ;; Don't export footnote section, which will be handled at the end
  909. ;; of the template.
  910. (unless (org-element-property :footnote-section-p headline)
  911. (let* ((low-level-rank (org-export-low-level-p headline info))
  912. (width (org-e-ascii--current-text-width headline info))
  913. ;; Blank lines between headline and its contents.
  914. ;; `org-e-ascii-headline-spacing', when set, overwrites
  915. ;; original buffer's spacing.
  916. (pre-blanks
  917. (make-string
  918. (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
  919. (org-element-property :pre-blank headline)) ?\n))
  920. ;; Even if HEADLINE has no section, there might be some
  921. ;; links in its title that we shouldn't forget to describe.
  922. (links
  923. (unless (or (eq (caar (org-element-contents headline)) 'section))
  924. (let ((title (org-element-property :title headline)))
  925. (when (consp title)
  926. (org-e-ascii--describe-links
  927. (org-e-ascii--unique-links title info) width info))))))
  928. ;; Deep subtree: export it as a list item.
  929. (if low-level-rank
  930. (concat
  931. ;; Bullet.
  932. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  933. org-e-ascii-bullets))))
  934. (char-to-string
  935. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  936. " "
  937. ;; Title.
  938. (org-e-ascii--build-title headline info width) "\n"
  939. ;; Contents, indented by length of bullet.
  940. pre-blanks
  941. (org-e-ascii--indent-string
  942. (concat contents
  943. (when (org-string-nw-p links) (concat "\n\n" links)))
  944. 2))
  945. ;; Else: Standard headline.
  946. (concat
  947. (org-e-ascii--build-title headline info width 'underline)
  948. "\n" pre-blanks
  949. (concat (when (org-string-nw-p links) links) contents))))))
  950. ;;;; Horizontal Rule
  951. (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  952. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  953. CONTENTS is nil. INFO is a plist holding contextual
  954. information."
  955. (let ((text-width (org-e-ascii--current-text-width horizontal-rule info))
  956. (spec-width
  957. (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  958. (org-e-ascii--justify-string
  959. (make-string (if (wholenump spec-width) spec-width text-width)
  960. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
  961. text-width 'center)))
  962. ;;;; Inline Babel Call
  963. ;; Inline Babel Calls are ignored.
  964. ;;;; Inline Src Block
  965. (defun org-e-ascii-inline-src-block (inline-src-block contents info)
  966. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  967. CONTENTS holds the contents of the item. INFO is a plist holding
  968. contextual information."
  969. (format org-e-ascii-verbatim-format
  970. (org-element-property :value inline-src-block)))
  971. ;;;; Inlinetask
  972. (defun org-e-ascii-inlinetask (inlinetask contents info)
  973. "Transcode an INLINETASK element from Org to ASCII.
  974. CONTENTS holds the contents of the block. INFO is a plist
  975. holding contextual information."
  976. (let ((width (org-e-ascii--current-text-width inlinetask info)))
  977. ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
  978. ;; with appropriate arguments.
  979. (if (functionp org-e-ascii-format-inlinetask-function)
  980. (funcall org-e-ascii-format-inlinetask-function
  981. ;; todo.
  982. (and (plist-get info :with-todo-keywords)
  983. (let ((todo (org-element-property
  984. :todo-keyword inlinetask)))
  985. (and todo (org-export-data todo info))))
  986. ;; todo-type
  987. (org-element-property :todo-type inlinetask)
  988. ;; priority
  989. (and (plist-get info :with-priority)
  990. (org-element-property :priority inlinetask))
  991. ;; title
  992. (org-export-data (org-element-property :title inlinetask) info)
  993. ;; tags
  994. (and (plist-get info :with-tags)
  995. (org-element-property :tags inlinetask))
  996. ;; contents and width
  997. contents width)
  998. ;; Otherwise, use a default template.
  999. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1000. (org-e-ascii--indent-string
  1001. (concat
  1002. ;; Top line, with an additional blank line if not in UTF-8.
  1003. (make-string width (if utf8p ?━ ?_)) "\n"
  1004. (unless utf8p (concat (make-string width ? ) "\n"))
  1005. ;; Add title. Fill it if wider than inlinetask.
  1006. (let ((title (org-e-ascii--build-title inlinetask info width)))
  1007. (if (<= (length title) width) title
  1008. (org-e-ascii--fill-string title width info)))
  1009. "\n"
  1010. ;; If CONTENTS is not empty, insert it along with
  1011. ;; a separator.
  1012. (when (org-string-nw-p contents)
  1013. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  1014. ;; Bottom line.
  1015. (make-string width (if utf8p ?━ ?_)))
  1016. ;; Flush the inlinetask to the right.
  1017. (- org-e-ascii-text-width org-e-ascii-global-margin
  1018. (if (not (org-export-get-parent-headline inlinetask)) 0
  1019. org-e-ascii-inner-margin)
  1020. (org-e-ascii--current-text-width inlinetask info)))))))
  1021. ;;;; Italic
  1022. (defun org-e-ascii-italic (italic contents info)
  1023. "Transcode italic from Org to ASCII.
  1024. CONTENTS is the text with italic markup. INFO is a plist holding
  1025. contextual information."
  1026. (format "/%s/" contents))
  1027. ;;;; Item
  1028. (defun org-e-ascii-item (item contents info)
  1029. "Transcode an ITEM element from Org to ASCII.
  1030. CONTENTS holds the contents of the item. INFO is a plist holding
  1031. contextual information."
  1032. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1033. (checkbox (org-e-ascii--checkbox item info))
  1034. (list-type (org-element-property :type (org-export-get-parent item)))
  1035. (bullet
  1036. ;; First parent of ITEM is always the plain-list. Get
  1037. ;; `:type' property from it.
  1038. (org-list-bullet-string
  1039. (case list-type
  1040. (descriptive
  1041. (concat checkbox
  1042. (org-export-data (org-element-property :tag item) info)
  1043. ": "))
  1044. (ordered
  1045. ;; Return correct number for ITEM, paying attention to
  1046. ;; counters.
  1047. (let* ((struct (org-element-property :structure item))
  1048. (bul (org-element-property :bullet item))
  1049. (num (number-to-string
  1050. (car (last (org-list-get-item-number
  1051. (org-element-property :begin item)
  1052. struct
  1053. (org-list-prevs-alist struct)
  1054. (org-list-parents-alist struct)))))))
  1055. (replace-regexp-in-string "[0-9]+" num bul)))
  1056. (t (let ((bul (org-element-property :bullet item)))
  1057. ;; Change bullets into more visible form if UTF-8 is active.
  1058. (if (not utf8p) bul
  1059. (replace-regexp-in-string
  1060. "-" "•"
  1061. (replace-regexp-in-string
  1062. "+" "⁃"
  1063. (replace-regexp-in-string "*" "‣" bul))))))))))
  1064. (concat
  1065. bullet
  1066. (unless (eq list-type 'descriptive) checkbox)
  1067. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1068. ;; already taken care of at the paragraph level so they don't
  1069. ;; interfere with indentation.
  1070. (let ((contents (org-e-ascii--indent-string contents (length bullet))))
  1071. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1072. (org-trim contents)
  1073. (concat "\n" contents))))))
  1074. ;;;; Keyword
  1075. (defun org-e-ascii-keyword (keyword contents info)
  1076. "Transcode a KEYWORD element from Org to ASCII.
  1077. CONTENTS is nil. INFO is a plist holding contextual
  1078. information."
  1079. (let ((key (org-element-property :key keyword))
  1080. (value (org-element-property :value keyword)))
  1081. (cond
  1082. ((string= key "ASCII") value)
  1083. ((string= key "TOC")
  1084. (let ((value (downcase value)))
  1085. (cond
  1086. ((string-match "\\<headlines\\>" value)
  1087. (let ((depth (or (and (string-match "[0-9]+" value)
  1088. (string-to-number (match-string 0 value)))
  1089. (plist-get info :with-toc))))
  1090. (org-e-ascii--build-toc
  1091. info (and (wholenump depth) depth) keyword)))
  1092. ((string= "tables" value)
  1093. (org-e-ascii--list-tables keyword info))
  1094. ((string= "listings" value)
  1095. (org-e-ascii--list-listings keyword info))))))))
  1096. ;;;; Latex Environment
  1097. (defun org-e-ascii-latex-environment (latex-environment contents info)
  1098. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1099. CONTENTS is nil. INFO is a plist holding contextual
  1100. information."
  1101. (org-remove-indentation (org-element-property :value latex-environment)))
  1102. ;;;; Latex Fragment
  1103. (defun org-e-ascii-latex-fragment (latex-fragment contents info)
  1104. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1105. CONTENTS is nil. INFO is a plist holding contextual
  1106. information."
  1107. (org-element-property :value latex-fragment))
  1108. ;;;; Line Break
  1109. (defun org-e-ascii-line-break (line-break contents info)
  1110. "Transcode a LINE-BREAK object from Org to ASCII.
  1111. CONTENTS is nil. INFO is a plist holding contextual
  1112. information." hard-newline)
  1113. ;;;; Link
  1114. (defun org-e-ascii-link (link desc info)
  1115. "Transcode a LINK object from Org to ASCII.
  1116. DESC is the description part of the link, or the empty string.
  1117. INFO is a plist holding contextual information."
  1118. (let ((raw-link (org-element-property :raw-link link))
  1119. (type (org-element-property :type link)))
  1120. (cond
  1121. ((string= type "coderef")
  1122. (let ((ref (org-element-property :path link)))
  1123. (format (org-export-get-coderef-format ref desc)
  1124. (org-export-resolve-coderef ref info))))
  1125. ;; Do not apply a special syntax on radio links. Though, use
  1126. ;; transcoded target's contents as output.
  1127. ((string= type "radio")
  1128. (let ((destination (org-export-resolve-radio-link link info)))
  1129. (when destination
  1130. (org-export-data (org-element-contents destination) info))))
  1131. ;; Do not apply a special syntax on fuzzy links pointing to
  1132. ;; targets.
  1133. ((string= type "fuzzy")
  1134. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1135. ;; Ignore invisible "#+TARGET: path".
  1136. (unless (eq (org-element-type destination) 'keyword)
  1137. (if (org-string-nw-p desc) desc
  1138. (when destination
  1139. (let ((number
  1140. (org-export-get-ordinal
  1141. destination info nil 'org-e-ascii--has-caption-p)))
  1142. (when number
  1143. (if (atom number) (number-to-string number)
  1144. (mapconcat 'number-to-string number ".")))))))))
  1145. (t
  1146. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1147. (concat
  1148. (format "[%s]" desc)
  1149. (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1150. ;;;; Macro
  1151. (defun org-e-ascii-macro (macro contents info)
  1152. "Transcode a MACRO element from Org to ASCII.
  1153. CONTENTS is nil. INFO is a plist holding contextual
  1154. information."
  1155. (org-export-expand-macro macro info))
  1156. ;;;; Paragraph
  1157. (defun org-e-ascii-paragraph (paragraph contents info)
  1158. "Transcode a PARAGRAPH element from Org to ASCII.
  1159. CONTENTS is the contents of the paragraph, as a string. INFO is
  1160. the plist used as a communication channel."
  1161. (org-e-ascii--fill-string
  1162. contents
  1163. (org-e-ascii--current-text-width paragraph info) info))
  1164. ;;;; Plain List
  1165. (defun org-e-ascii-plain-list (plain-list contents info)
  1166. "Transcode a PLAIN-LIST element from Org to ASCII.
  1167. CONTENTS is the contents of the list. INFO is a plist holding
  1168. contextual information."
  1169. contents)
  1170. ;;;; Plain Text
  1171. (defun org-e-ascii-plain-text (text info)
  1172. "Transcode a TEXT string from Org to ASCII.
  1173. INFO is a plist used as a communication channel."
  1174. (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
  1175. (plist-get info :with-special-strings)))
  1176. text
  1177. ;; Usual replacements in utf-8 with proper option set.
  1178. (replace-regexp-in-string
  1179. "\\.\\.\\." "…"
  1180. (replace-regexp-in-string
  1181. "--" "–"
  1182. (replace-regexp-in-string "---" "—" text)))))
  1183. ;;;; Planning
  1184. (defun org-e-ascii-planning (planning contents info)
  1185. "Transcode a PLANNING element from Org to ASCII.
  1186. CONTENTS is nil. INFO is a plist used as a communication
  1187. channel."
  1188. (mapconcat
  1189. 'identity
  1190. (delq nil
  1191. (list (let ((closed (org-element-property :closed planning)))
  1192. (when closed (concat org-closed-string " "
  1193. (org-translate-time closed))))
  1194. (let ((deadline (org-element-property :deadline planning)))
  1195. (when deadline (concat org-deadline-string " "
  1196. (org-translate-time deadline))))
  1197. (let ((scheduled (org-element-property :scheduled planning)))
  1198. (when scheduled (concat org-scheduled-string " "
  1199. (org-translate-time scheduled))))))
  1200. " "))
  1201. ;;;; Property Drawer
  1202. ;;
  1203. ;; Property drawers are ignored.
  1204. ;;;; Quote Block
  1205. (defun org-e-ascii-quote-block (quote-block contents info)
  1206. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1207. CONTENTS holds the contents of the block. INFO is a plist
  1208. holding contextual information."
  1209. (let ((width (org-e-ascii--current-text-width quote-block info)))
  1210. (org-e-ascii--indent-string
  1211. (org-remove-indentation
  1212. (org-e-ascii--fill-string contents width info))
  1213. org-e-ascii-quote-margin)))
  1214. ;;;; Quote Section
  1215. (defun org-e-ascii-quote-section (quote-section contents info)
  1216. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1217. CONTENTS is nil. INFO is a plist holding contextual information."
  1218. (let ((width (org-e-ascii--current-text-width quote-section info))
  1219. (value
  1220. (org-export-data
  1221. (org-remove-indentation (org-element-property :value quote-section))
  1222. info)))
  1223. (org-e-ascii--indent-string
  1224. value
  1225. (+ org-e-ascii-quote-margin
  1226. ;; Don't apply inner margin if parent headline is low level.
  1227. (let ((headline (org-export-get-parent-headline quote-section)))
  1228. (if (org-export-low-level-p headline info) 0
  1229. org-e-ascii-inner-margin))))))
  1230. ;;;; Radio Target
  1231. (defun org-e-ascii-radio-target (radio-target contents info)
  1232. "Transcode a RADIO-TARGET object from Org to ASCII.
  1233. CONTENTS is the contents of the target. INFO is a plist holding
  1234. contextual information."
  1235. contents)
  1236. ;;;; Section
  1237. (defun org-e-ascii-section (section contents info)
  1238. "Transcode a SECTION element from Org to ASCII.
  1239. CONTENTS is the contents of the section. INFO is a plist holding
  1240. contextual information."
  1241. (org-e-ascii--indent-string
  1242. (concat
  1243. contents
  1244. (when org-e-ascii-links-to-notes
  1245. ;; Add list of links at the end of SECTION.
  1246. (let ((links (org-e-ascii--describe-links
  1247. (org-e-ascii--unique-links section info)
  1248. (org-e-ascii--current-text-width section info) info)))
  1249. ;; Separate list of links and section contents.
  1250. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1251. ;; Do not apply inner margin if parent headline is low level.
  1252. (let ((headline (org-export-get-parent-headline section)))
  1253. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1254. org-e-ascii-inner-margin))))
  1255. ;;;; Special Block
  1256. (defun org-e-ascii-special-block (special-block contents info)
  1257. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1258. CONTENTS holds the contents of the block. INFO is a plist
  1259. holding contextual information."
  1260. contents)
  1261. ;;;; Src Block
  1262. (defun org-e-ascii-src-block (src-block contents info)
  1263. "Transcode a SRC-BLOCK element from Org to ASCII.
  1264. CONTENTS holds the contents of the item. INFO is a plist holding
  1265. contextual information."
  1266. (let ((caption (org-e-ascii--build-caption src-block info)))
  1267. (concat
  1268. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1269. (org-e-ascii--box-string
  1270. (org-export-format-code-default src-block info) info)
  1271. (when (and caption (not org-e-ascii-caption-above))
  1272. (concat "\n" caption)))))
  1273. ;;;; Statistics Cookie
  1274. (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  1275. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1276. CONTENTS is nil. INFO is a plist holding contextual information."
  1277. (org-element-property :value statistics-cookie))
  1278. ;;;; Subscript
  1279. (defun org-e-ascii-subscript (subscript contents info)
  1280. "Transcode a SUBSCRIPT object from Org to ASCII.
  1281. CONTENTS is the contents of the object. INFO is a plist holding
  1282. contextual information."
  1283. (if (org-element-property :use-brackets-p subscript)
  1284. (format "_{%s}" contents)
  1285. (format "_%s" contents)))
  1286. ;;;; Superscript
  1287. (defun org-e-ascii-superscript (superscript contents info)
  1288. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1289. CONTENTS is the contents of the object. INFO is a plist holding
  1290. contextual information."
  1291. (if (org-element-property :use-brackets-p superscript)
  1292. (format "_{%s}" contents)
  1293. (format "_%s" contents)))
  1294. ;;;; Strike-through
  1295. (defun org-e-ascii-strike-through (strike-through contents info)
  1296. "Transcode STRIKE-THROUGH from Org to ASCII.
  1297. CONTENTS is text with strike-through markup. INFO is a plist
  1298. holding contextual information."
  1299. (format "+%s+" contents))
  1300. ;;;; Table
  1301. (defun org-e-ascii-table (table contents info)
  1302. "Transcode a TABLE element from Org to ASCII.
  1303. CONTENTS is the contents of the table. INFO is a plist holding
  1304. contextual information."
  1305. (let ((caption (org-e-ascii--build-caption table info)))
  1306. (concat
  1307. ;; Possibly add a caption string above.
  1308. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1309. ;; Insert table. Note: "table.el" tables are left unmodified.
  1310. (cond ((eq (org-element-property :type table) 'org) contents)
  1311. ((and org-e-ascii-table-use-ascii-art
  1312. (eq (plist-get info :ascii-charset) 'utf-8)
  1313. (require 'ascii-art-to-unicode nil t))
  1314. (with-temp-buffer
  1315. (insert (org-remove-indentation
  1316. (org-element-property :value table)))
  1317. (goto-char (point-min))
  1318. (aa2u)
  1319. (goto-char (point-max))
  1320. (skip-chars-backward " \r\t\n")
  1321. (buffer-substring (point-min) (point))))
  1322. (t (org-remove-indentation (org-element-property :value table))))
  1323. ;; Possible add a caption string below.
  1324. (when (and caption (not org-e-ascii-caption-above))
  1325. (concat "\n" caption)))))
  1326. ;;;; Table Cell
  1327. (defun org-e-ascii--table-cell-width (table-cell info)
  1328. "Return width of TABLE-CELL.
  1329. INFO is a plist used as a communication channel.
  1330. Width of a cell is determined either by a width cookie in the
  1331. same column as the cell, or by the maximum cell's length in that
  1332. column.
  1333. When `org-e-ascii-table-widen-columns' is non-nil, width cookies
  1334. are ignored."
  1335. (or (and (not org-e-ascii-table-widen-columns)
  1336. (org-export-table-cell-width table-cell info))
  1337. (let* ((max-width 0)
  1338. (table (org-export-get-parent-table table-cell))
  1339. (specialp (org-export-table-has-special-column-p table))
  1340. (col (cdr (org-export-table-cell-address table-cell info))))
  1341. (org-element-map
  1342. table 'table-row
  1343. (lambda (row)
  1344. (setq max-width
  1345. (max (length
  1346. (org-export-data
  1347. (org-element-contents
  1348. (elt (if specialp (cdr (org-element-contents row))
  1349. (org-element-contents row))
  1350. col))
  1351. info))
  1352. max-width)))
  1353. info)
  1354. max-width)))
  1355. (defun org-e-ascii-table-cell (table-cell contents info)
  1356. "Transcode a TABLE-CELL object from Org to ASCII.
  1357. CONTENTS is the cell contents. INFO is a plist used as
  1358. a communication channel."
  1359. ;; Determine column width. When `org-e-ascii-table-widen-columns'
  1360. ;; is nil and some width cookie has set it, use that value.
  1361. ;; Otherwise, compute the maximum width among transcoded data of
  1362. ;; each cell in the column.
  1363. (let ((width (org-e-ascii--table-cell-width table-cell info)))
  1364. ;; When contents are too large, truncate them.
  1365. (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
  1366. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1367. ;; Align contents correctly within the cell.
  1368. (let* ((indent-tabs-mode nil)
  1369. (data
  1370. (when contents
  1371. (org-e-ascii--justify-string
  1372. contents width
  1373. (org-export-table-cell-alignment table-cell info)))))
  1374. (setq contents (concat data (make-string (- width (length data)) ? ))))
  1375. ;; Return cell.
  1376. (concat (format " %s " contents)
  1377. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1378. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1379. ;;;; Table Row
  1380. (defun org-e-ascii-table-row (table-row contents info)
  1381. "Transcode a TABLE-ROW element from Org to ASCII.
  1382. CONTENTS is the row contents. INFO is a plist used as
  1383. a communication channel."
  1384. (when (eq (org-element-property :type table-row) 'standard)
  1385. (let ((build-hline
  1386. (function
  1387. (lambda (lcorner horiz vert rcorner)
  1388. (concat
  1389. (apply
  1390. 'concat
  1391. (org-element-map
  1392. table-row 'table-cell
  1393. (lambda (cell)
  1394. (let ((width (org-e-ascii--table-cell-width cell info))
  1395. (borders (org-export-table-cell-borders cell info)))
  1396. (concat
  1397. ;; In order to know if CELL starts the row, do
  1398. ;; not compare it with the first cell in the row
  1399. ;; as there might be a special column. Instead,
  1400. ;; compare it with the first exportable cell,
  1401. ;; obtained with `org-element-map'.
  1402. (when (and (memq 'left borders)
  1403. (eq (org-element-map
  1404. table-row 'table-cell 'identity info t)
  1405. cell))
  1406. lcorner)
  1407. (make-string (+ 2 width) (string-to-char horiz))
  1408. (cond
  1409. ((not (memq 'right borders)) nil)
  1410. ((eq (car (last (org-element-contents table-row))) cell)
  1411. rcorner)
  1412. (t vert)))))
  1413. info)) "\n"))))
  1414. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1415. (borders (org-export-table-cell-borders
  1416. (org-element-map table-row 'table-cell 'identity info t)
  1417. info)))
  1418. (concat (cond
  1419. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1420. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1421. (funcall build-hline "+" "-" "+" "+")))
  1422. ((memq 'above borders)
  1423. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1424. (funcall build-hline "+" "-" "+" "+"))))
  1425. (when (memq 'left borders) (if utf8p "│" "|"))
  1426. contents "\n"
  1427. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1428. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1429. (funcall build-hline "+" "-" "+" "+")))))))
  1430. ;;;; Target
  1431. ;; Targets are invisible.
  1432. ;;;; Timestamp
  1433. (defun org-e-ascii-timestamp (timestamp contents info)
  1434. "Transcode a TIMESTAMP object from Org to ASCII.
  1435. CONTENTS is nil. INFO is a plist holding contextual information."
  1436. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1437. (range-end
  1438. (org-translate-time (org-element-property :range-end timestamp)))
  1439. (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1440. (concat value
  1441. (when range-end (concat (if utf8p "–" "--") range-end)))))
  1442. ;;;; Underline
  1443. (defun org-e-ascii-underline (underline contents info)
  1444. "Transcode UNDERLINE from Org to ASCII.
  1445. CONTENTS is the text with underline markup. INFO is a plist
  1446. holding contextual information."
  1447. (format "_%s_" contents))
  1448. ;;;; Verbatim
  1449. (defun org-e-ascii-verbatim (verbatim contents info)
  1450. "Return a VERBATIM object from Org to ASCII.
  1451. CONTENTS is nil. INFO is a plist holding contextual information."
  1452. (format org-e-ascii-verbatim-format
  1453. (org-element-property :value verbatim)))
  1454. ;;;; Verse Block
  1455. (defun org-e-ascii-verse-block (verse-block contents info)
  1456. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1457. CONTENTS is verse block contents. INFO is a plist holding
  1458. contextual information."
  1459. (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
  1460. (org-e-ascii--indent-string
  1461. (org-e-ascii--justify-string contents verse-width 'left)
  1462. org-e-ascii-quote-margin)))
  1463. ;;; Filter
  1464. (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  1465. "Filter controlling number of blank lines after an headline.
  1466. HEADLINE is a string representing a transcoded headline.
  1467. BACK-END is symbol specifying back-end used for export. INFO is
  1468. plist containing the communication channel.
  1469. This function only applies to `e-ascii' back-end. See
  1470. `org-e-ascii-headline-spacing' for information.
  1471. For any other back-end, HEADLINE is returned as-is."
  1472. (if (not org-e-ascii-headline-spacing) headline
  1473. (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
  1474. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1475. ;;; Interactive function
  1476. ;;;###autoload
  1477. (defun org-e-ascii-export-as-ascii
  1478. (&optional subtreep visible-only body-only ext-plist)
  1479. "Export current buffer to a text buffer.
  1480. If narrowing is active in the current buffer, only export its
  1481. narrowed part.
  1482. If a region is active, export that region.
  1483. When optional argument SUBTREEP is non-nil, export the sub-tree
  1484. at point, extracting information from the headline properties
  1485. first.
  1486. When optional argument VISIBLE-ONLY is non-nil, don't export
  1487. contents of hidden elements.
  1488. When optional argument BODY-ONLY is non-nil, strip title, table
  1489. of contents and footnote definitions from output.
  1490. EXT-PLIST, when provided, is a property list with external
  1491. parameters overriding Org default settings, but still inferior to
  1492. file-local settings.
  1493. Export is done in a buffer named \"*Org E-ASCII Export*\", which
  1494. will be displayed when `org-export-show-temporary-export-buffer'
  1495. is non-nil."
  1496. (interactive)
  1497. (let ((outbuf (org-export-to-buffer
  1498. 'e-ascii "*Org E-ASCII Export*"
  1499. subtreep visible-only body-only ext-plist)))
  1500. (with-current-buffer outbuf (text-mode))
  1501. (when org-export-show-temporary-export-buffer
  1502. (switch-to-buffer-other-window outbuf))))
  1503. ;;;###autoload
  1504. (defun org-e-ascii-export-to-ascii
  1505. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1506. "Export current buffer to a text file.
  1507. If narrowing is active in the current buffer, only export its
  1508. narrowed part.
  1509. If a region is active, export that region.
  1510. When optional argument SUBTREEP is non-nil, export the sub-tree
  1511. at point, extracting information from the headline properties
  1512. first.
  1513. When optional argument VISIBLE-ONLY is non-nil, don't export
  1514. contents of hidden elements.
  1515. When optional argument BODY-ONLY is non-nil, strip title, table
  1516. of contents and footnote definitions from output.
  1517. EXT-PLIST, when provided, is a property list with external
  1518. parameters overriding Org default settings, but still inferior to
  1519. file-local settings.
  1520. When optional argument PUB-DIR is set, use it as the publishing
  1521. directory.
  1522. Return output file's name."
  1523. (interactive)
  1524. (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
  1525. (org-export-to-file
  1526. 'e-ascii outfile subtreep visible-only body-only ext-plist)))
  1527. (provide 'org-e-ascii)
  1528. ;;; org-e-ascii.el ends here