org-e-ascii.el 65 KB

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