ox-ascii.el 68 KB

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