ox-ascii.el 67 KB

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