ox-ascii.el 69 KB

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