org-e-ascii.el 63 KB

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