org-e-ascii.el 66 KB

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