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