ox-ascii.el 69 KB

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