org-e-ascii.el 63 KB

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