org-e-ascii.el 67 KB

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