ox-ascii.el 68 KB

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