ox-ascii.el 68 KB

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