ox-ascii.el 67 KB

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