org-e-ascii.el 62 KB

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