org-e-ascii.el 66 KB

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