ox-ascii.el 69 KB

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