org-e-ascii.el 63 KB

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