ox-ascii.el 68 KB

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