ox-ascii.el 69 KB

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