ox-ascii.el 68 KB

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