ox-ascii.el 68 KB

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