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