org-e-ascii.el 67 KB

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