ox-ascii.el 67 KB

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