org-e-ascii.el 65 KB

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