1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements an ASCII back-end for Org generic exporter.
26 ;; See Org manual for more information.
30 (eval-when-compile (require 'cl
))
34 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
38 ;; The following setting won't allow to modify preferred charset
39 ;; through a buffer keyword or an option item, but, since the property
40 ;; will appear in communication channel nonetheless, it allows to
41 ;; override `org-ascii-charset' variable on the fly by the ext-plist
44 ;; We also install a filter for headlines and sections, in order to
45 ;; control blank lines separating them in output string.
47 (org-export-define-backend 'ascii
48 '((bold . org-ascii-bold
)
49 (center-block . org-ascii-center-block
)
50 (clock . org-ascii-clock
)
51 (code . org-ascii-code
)
52 (comment .
(lambda (&rest args
) ""))
53 (comment-block .
(lambda (&rest args
) ""))
54 (drawer . org-ascii-drawer
)
55 (dynamic-block . org-ascii-dynamic-block
)
56 (entity . org-ascii-entity
)
57 (example-block . org-ascii-example-block
)
58 (export-block . org-ascii-export-block
)
59 (export-snippet . org-ascii-export-snippet
)
60 (fixed-width . org-ascii-fixed-width
)
61 (footnote-reference . org-ascii-footnote-reference
)
62 (headline . org-ascii-headline
)
63 (horizontal-rule . org-ascii-horizontal-rule
)
64 (inline-src-block . org-ascii-inline-src-block
)
65 (inlinetask . org-ascii-inlinetask
)
66 (inner-template . org-ascii-inner-template
)
67 (italic . org-ascii-italic
)
68 (item . org-ascii-item
)
69 (keyword . org-ascii-keyword
)
70 (latex-environment . org-ascii-latex-environment
)
71 (latex-fragment . org-ascii-latex-fragment
)
72 (line-break . org-ascii-line-break
)
73 (link . org-ascii-link
)
74 (node-property . org-ascii-node-property
)
75 (paragraph . org-ascii-paragraph
)
76 (plain-list . org-ascii-plain-list
)
77 (plain-text . org-ascii-plain-text
)
78 (planning . org-ascii-planning
)
79 (property-drawer . org-ascii-property-drawer
)
80 (quote-block . org-ascii-quote-block
)
81 (quote-section . org-ascii-quote-section
)
82 (radio-target . org-ascii-radio-target
)
83 (section . org-ascii-section
)
84 (special-block . org-ascii-special-block
)
85 (src-block . org-ascii-src-block
)
86 (statistics-cookie . org-ascii-statistics-cookie
)
87 (strike-through . org-ascii-strike-through
)
88 (subscript . org-ascii-subscript
)
89 (superscript . org-ascii-superscript
)
90 (table . org-ascii-table
)
91 (table-cell . org-ascii-table-cell
)
92 (table-row . org-ascii-table-row
)
93 (target . org-ascii-target
)
94 (template . org-ascii-template
)
95 (timestamp . org-ascii-timestamp
)
96 (underline . org-ascii-underline
)
97 (verbatim . org-ascii-verbatim
)
98 (verse-block . org-ascii-verse-block
))
101 '(?t
"Export to Plain Text"
102 ((?A
"As ASCII buffer"
104 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
107 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
108 (?L
"As Latin1 buffer"
110 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
113 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
114 (?U
"As UTF-8 buffer"
116 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
119 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
120 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
121 (:filter-parse-tree org-ascii-filter-paragraph-spacing
122 org-ascii-filter-comment-spacing
)
123 (:filter-section . org-ascii-filter-headline-blank-lines
))
124 :options-alist
'((:ascii-charset nil nil org-ascii-charset
)))
128 ;;; User Configurable Variables
130 (defgroup org-export-ascii nil
131 "Options for exporting Org mode files to ASCII."
132 :tag
"Org Export ASCII"
135 (defcustom org-ascii-text-width
72
136 "Maximum width of exported text.
137 This number includes margin size, as set in
138 `org-ascii-global-margin'."
139 :group
'org-export-ascii
141 :package-version
'(Org .
"8.0")
144 (defcustom org-ascii-global-margin
0
145 "Width of the left margin, in number of characters."
146 :group
'org-export-ascii
148 :package-version
'(Org .
"8.0")
151 (defcustom org-ascii-inner-margin
2
152 "Width of the inner margin, in number of characters.
153 Inner margin is applied between each headline."
154 :group
'org-export-ascii
156 :package-version
'(Org .
"8.0")
159 (defcustom org-ascii-quote-margin
6
160 "Width of margin used for quoting text, in characters.
161 This margin is applied on both sides of the text."
162 :group
'org-export-ascii
164 :package-version
'(Org .
"8.0")
167 (defcustom org-ascii-inlinetask-width
30
168 "Width of inline tasks, in number of characters.
169 This number ignores any margin."
170 :group
'org-export-ascii
172 :package-version
'(Org .
"8.0")
175 (defcustom org-ascii-headline-spacing
'(1 .
2)
176 "Number of blank lines inserted around headlines.
178 This variable can be set to a cons cell. In that case, its car
179 represents the number of blank lines present before headline
180 contents whereas its cdr reflects the number of blank lines after
183 A nil value replicates the number of blank lines found in the
184 original Org buffer at the same place."
185 :group
'org-export-ascii
187 :package-version
'(Org .
"8.0")
189 (const :tag
"Replicate original spacing" nil
)
190 (cons :tag
"Set an uniform spacing"
191 (integer :tag
"Number of blank lines before contents")
192 (integer :tag
"Number of blank lines after contents"))))
194 (defcustom org-ascii-indented-line-width
'auto
195 "Additional indentation width for the first line in a paragraph.
196 If the value is an integer, indent the first line of each
197 paragraph by this number. If it is the symbol `auto' preserve
198 indentation from original document."
199 :group
'org-export-ascii
201 :package-version
'(Org .
"8.0")
203 (integer :tag
"Number of white spaces characters")
204 (const :tag
"Preserve original width" auto
)))
206 (defcustom org-ascii-paragraph-spacing
'auto
207 "Number of white lines between paragraphs.
208 If the value is an integer, add this number of blank lines
209 between contiguous paragraphs. If is it the symbol `auto', keep
210 the same number of blank lines as in the original document."
211 :group
'org-export-ascii
213 :package-version
'(Org .
"8.0")
215 (integer :tag
"Number of blank lines")
216 (const :tag
"Preserve original spacing" auto
)))
218 (defcustom org-ascii-charset
'ascii
219 "The charset allowed to represent various elements and objects.
221 `ascii' Only use plain ASCII characters
222 `latin1' Include Latin-1 characters
223 `utf-8' Use all UTF-8 characters"
224 :group
'org-export-ascii
226 :package-version
'(Org .
"8.0")
228 (const :tag
"ASCII" ascii
)
229 (const :tag
"Latin-1" latin1
)
230 (const :tag
"UTF-8" utf-8
)))
232 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
234 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
235 "Characters for underlining headings in ASCII export.
237 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
238 and whose value is a list of characters.
240 For each supported charset, this variable associates a sequence
241 of underline characters. In a sequence, the characters will be
242 used in order for headlines level 1, 2, ... If no character is
243 available for a given level, the headline won't be underlined."
244 :group
'org-export-ascii
246 :package-version
'(Org .
"8.0")
248 (cons :tag
"Underline characters sequence"
249 (const :tag
"ASCII charset" ascii
)
251 (cons :tag
"Underline characters sequence"
252 (const :tag
"Latin-1 charset" latin1
)
254 (cons :tag
"Underline characters sequence"
255 (const :tag
"UTF-8 charset" utf-8
)
256 (repeat character
))))
258 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
261 "Bullet characters for headlines converted to lists in ASCII export.
263 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
264 and whose value is a list of characters.
266 The first character is used for the first level considered as low
267 level, and so on. If there are more levels than characters given
268 here, the list will be repeated.
270 Note that this variable doesn't affect plain lists
272 :group
'org-export-ascii
274 :package-version
'(Org .
"8.0")
276 (cons :tag
"Bullet characters for low level headlines"
277 (const :tag
"ASCII charset" ascii
)
279 (cons :tag
"Bullet characters for low level headlines"
280 (const :tag
"Latin-1 charset" latin1
)
282 (cons :tag
"Bullet characters for low level headlines"
283 (const :tag
"UTF-8 charset" utf-8
)
284 (repeat character
))))
286 (defcustom org-ascii-links-to-notes t
287 "Non-nil means convert links to notes before the next headline.
288 When nil, the link will be exported in place. If the line
289 becomes long in this way, it will be wrapped."
290 :group
'org-export-ascii
292 :package-version
'(Org .
"8.0")
295 (defcustom org-ascii-table-keep-all-vertical-lines nil
296 "Non-nil means keep all vertical lines in ASCII tables.
297 When nil, vertical lines will be removed except for those needed
298 for column grouping."
299 :group
'org-export-ascii
301 :package-version
'(Org .
"8.0")
304 (defcustom org-ascii-table-widen-columns t
305 "Non-nil means widen narrowed columns for export.
306 When nil, narrowed columns will look in ASCII export just like in
307 Org mode, i.e. with \"=>\" as ellipsis."
308 :group
'org-export-ascii
310 :package-version
'(Org .
"8.0")
313 (defcustom org-ascii-table-use-ascii-art nil
314 "Non-nil means table.el tables are turned into ascii-art.
316 It only makes sense when export charset is `utf-8'. It is nil by
317 default since it requires ascii-art-to-unicode.el package. You
318 can download it here:
320 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
321 :group
'org-export-ascii
323 :package-version
'(Org .
"8.0")
326 (defcustom org-ascii-caption-above nil
327 "When non-nil, place caption string before the element.
328 Otherwise, place it right after it."
329 :group
'org-export-ascii
331 :package-version
'(Org .
"8.0")
334 (defcustom org-ascii-verbatim-format
"`%s'"
335 "Format string used for verbatim text and inline code."
336 :group
'org-export-ascii
338 :package-version
'(Org .
"8.0")
341 (defcustom org-ascii-format-drawer-function
342 (lambda (name contents width
) contents
)
343 "Function called to format a drawer in ASCII.
345 The function must accept three parameters:
346 NAME the drawer name, like \"LOGBOOK\"
347 CONTENTS the contents of the drawer.
348 WIDTH the text width within the drawer.
350 The function should return either the string to be exported or
351 nil to ignore the drawer.
353 The default value simply returns the value of CONTENTS."
354 :group
'org-export-ascii
356 :package-version
'(Org .
"8.0")
359 (defcustom org-ascii-format-inlinetask-function
360 'org-ascii-format-inlinetask-default
361 "Function called to format an inlinetask in ASCII.
363 The function must accept nine parameters:
364 TODO the todo keyword, as a string
365 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
366 PRIORITY the inlinetask priority, as a string
367 NAME the inlinetask name, as a string.
368 TAGS the inlinetask tags, as a list of strings.
369 CONTENTS the contents of the inlinetask, as a string.
370 WIDTH the width of the inlinetask, as a number.
371 INLINETASK the inlinetask itself.
372 INFO the info channel.
374 The function should return either the string to be exported or
375 nil to ignore the inline task."
376 :group
'org-export-ascii
378 :package-version
'(Org .
"8.3")
383 ;;; Internal Functions
385 ;; Internal functions fall into three categories.
387 ;; The first one is about text formatting. The core function is
388 ;; `org-ascii--current-text-width', which determines the current
389 ;; text width allowed to a given element. In other words, it helps
390 ;; keeping each line width within maximum text width defined in
391 ;; `org-ascii-text-width'. Once this information is known,
392 ;; `org-ascii--fill-string', `org-ascii--justify-string',
393 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
394 ;; operate on a given output string.
396 ;; The second category contains functions handling elements listings,
397 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
398 ;; returns a complete table of contents, `org-ascii--list-listings'
399 ;; returns a list of referenceable src-block elements, and
400 ;; `org-ascii--list-tables' does the same for table elements.
402 ;; The third category includes general helper functions.
403 ;; `org-ascii--build-title' creates the title for a given headline
404 ;; or inlinetask element. `org-ascii--build-caption' returns the
405 ;; caption string associated to a table or a src-block.
406 ;; `org-ascii--describe-links' creates notes about links for
407 ;; insertion at the end of a section. It uses
408 ;; `org-ascii--unique-links' to get the list of links to describe.
409 ;; Eventually, `org-ascii--translate' translates a string according
410 ;; to language and charset specification.
413 (defun org-ascii--fill-string (s text-width info
&optional justify
)
414 "Fill a string with specified text-width and return it.
416 S is the string being filled. TEXT-WIDTH is an integer
417 specifying maximum length of a line. INFO is the plist used as
418 a communication channel.
420 Optional argument JUSTIFY can specify any type of justification
421 among `left', `center', `right' or `full'. A nil value is
422 equivalent to `left'. For a justification that doesn't also fill
423 string, see `org-ascii--justify-string'.
425 Return nil if S isn't a string."
426 ;; Don't fill paragraph when break should be preserved.
427 (cond ((not (stringp s
)) nil
)
428 ((plist-get info
:preserve-breaks
) s
)
429 (t (let ((double-space-p sentence-end-double-space
))
431 (let ((fill-column text-width
)
432 (use-hard-newlines t
)
433 (sentence-end-double-space double-space-p
))
435 (fill-region (point-min) (point-max) justify
))
438 (defun org-ascii--justify-string (s text-width how
)
440 TEXT-WIDTH is an integer specifying maximum length of a line.
441 HOW determines the type of justification: it can be `left',
442 `right', `full' or `center'."
445 (goto-char (point-min))
446 (let ((fill-column text-width
)
447 ;; Disable `adaptive-fill-mode' so it doesn't prevent
448 ;; filling lines matching `adaptive-fill-regexp'.
449 (adaptive-fill-mode nil
))
450 (while (< (point) (point-max))
451 (justify-current-line how
)
455 (defun org-ascii--indent-string (s width
)
456 "Indent string S by WIDTH white spaces.
457 Empty lines are not indented."
459 (replace-regexp-in-string
460 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
462 (defun org-ascii--box-string (s info
)
463 "Return string S with a partial box to its left.
464 INFO is a plist used as a communication channel."
465 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
466 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
467 (replace-regexp-in-string
468 "^" (if utf8p
"│ " "| ")
469 ;; Remove last newline character.
470 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
472 (defun org-ascii--current-text-width (element info
)
473 "Return maximum text width for ELEMENT's contents.
474 INFO is a plist used as a communication channel."
475 (case (org-element-type element
)
476 ;; Elements with an absolute width: `headline' and `inlinetask'.
477 (inlinetask org-ascii-inlinetask-width
)
479 (- org-ascii-text-width
480 (let ((low-level-rank (org-export-low-level-p element info
)))
481 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
482 ;; Elements with a relative width: store maximum text width in
485 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
486 ;; Total width is determined by the presence, or not, of an
487 ;; inline task among ELEMENT parents.
489 (if (loop for parent in genealogy
490 thereis
(eq (org-element-type parent
) 'inlinetask
))
491 org-ascii-inlinetask-width
492 ;; No inlinetask: Remove global margin from text width.
493 (- org-ascii-text-width
494 org-ascii-global-margin
495 (let ((parent (org-export-get-parent-headline element
)))
496 ;; Inner margin doesn't apply to text before first
499 (let ((low-level-rank
500 (org-export-low-level-p parent info
)))
501 ;; Inner margin doesn't apply to contents of
502 ;; low level headlines, since they've got their
503 ;; own indentation mechanism.
504 (if low-level-rank
(* low-level-rank
2)
505 org-ascii-inner-margin
))))))))
507 ;; Each `quote-block', `quote-section' and `verse-block' above
508 ;; narrows text width by twice the standard margin size.
509 (+ (* (loop for parent in genealogy
510 when
(memq (org-element-type parent
)
511 '(quote-block quote-section verse-block
))
513 2 org-ascii-quote-margin
)
514 ;; Text width within a plain-list is restricted by
515 ;; indentation of current item. If that's the case,
516 ;; compute it with the help of `:structure' property from
517 ;; parent item, if any.
519 (if (eq (org-element-type element
) 'item
) element
520 (loop for parent in genealogy
521 when
(eq (org-element-type parent
) 'item
)
523 (if (not parent-item
) 0
524 ;; Compute indentation offset of the current item,
525 ;; that is the sum of the difference between its
526 ;; indentation and the indentation of the top item in
527 ;; the list and current item bullet's length. Also
528 ;; remove checkbox length, and tag length (for
529 ;; description lists) or bullet length.
530 (let ((struct (org-element-property :structure parent-item
))
531 (beg-item (org-element-property :begin parent-item
)))
532 (+ (- (org-list-get-ind beg-item struct
)
534 (org-list-get-top-point struct
) struct
))
535 (length (org-ascii--checkbox parent-item info
))
537 (or (org-list-get-tag beg-item struct
)
538 (org-list-get-bullet beg-item struct
)))))))))))))
540 (defun org-ascii--build-title
541 (element info text-width
&optional underline notags toc
)
542 "Format ELEMENT title and return it.
544 ELEMENT is either an `headline' or `inlinetask' element. INFO is
545 a plist used as a communication channel. TEXT-WIDTH is an
546 integer representing the maximum length of a line.
548 When optional argument UNDERLINE is non-nil, underline title,
549 without the tags, according to `org-ascii-underline'
552 If optional argument NOTAGS is non-nil, no tags will be added to
555 When optional argument TOC is non-nil, use optional title if
556 possible. It doesn't apply to `inlinetask' elements."
557 (let* ((headlinep (eq (org-element-type element
) 'headline
))
559 ;; Numbering is specific to headlines.
560 (and headlinep
(org-export-numbered-headline-p element info
)
561 ;; All tests passed: build numbering string.
565 (org-export-get-headline-number element info
) ".")
570 (if (and toc headlinep
) (org-export-get-alt-title element info
)
571 (org-element-property :title element
))
574 (and (plist-get info
:with-todo-keywords
)
575 (let ((todo (org-element-property :todo-keyword element
)))
576 (and todo
(concat (org-export-data todo info
) " ")))))
577 (tags (and (not notags
)
578 (plist-get info
:with-tags
)
579 (let ((tag-list (org-export-get-tags element info
)))
582 (mapconcat 'identity tag-list
":"))))))
584 (and (plist-get info
:with-priority
)
585 (let ((char (org-element-property :priority element
)))
586 (and char
(format "(#%c) " char
)))))
587 (first-part (concat numbers todo priority text
)))
590 ;; Align tags, if any.
594 (max (- text-width
(1+ (length first-part
))) (length tags
)))
596 ;; Maybe underline text, if ELEMENT type is `headline' and an
597 ;; underline character has been defined.
598 (when (and underline headlinep
)
600 (nth (1- (org-export-get-relative-level element info
))
601 (cdr (assq (plist-get info
:ascii-charset
)
602 org-ascii-underline
)))))
605 (make-string (length first-part
) under-char
))))))))
607 (defun org-ascii--has-caption-p (element info
)
608 "Non-nil when ELEMENT has a caption affiliated keyword.
609 INFO is a plist used as a communication channel. This function
610 is meant to be used as a predicate for `org-export-get-ordinal'."
611 (org-element-property :caption element
))
613 (defun org-ascii--build-caption (element info
)
614 "Return caption string for ELEMENT, if applicable.
616 INFO is a plist used as a communication channel.
618 The caption string contains the sequence number of ELEMENT along
619 with its real caption. Return nil when ELEMENT has no affiliated
621 (let ((caption (org-export-get-caption element
)))
623 ;; Get sequence number of current src-block among every
624 ;; src-block with a caption.
626 (org-export-get-ordinal
627 element info nil
'org-ascii--has-caption-p
))
628 (title-fmt (org-ascii--translate
629 (case (org-element-type element
)
631 (src-block "Listing %d:"))
633 (org-ascii--fill-string
634 (concat (format title-fmt reference
)
636 (org-export-data caption info
))
637 (org-ascii--current-text-width element info
) info
)))))
639 (defun org-ascii--build-toc (info &optional n keyword
)
640 "Return a table of contents.
642 INFO is a plist used as a communication channel.
644 Optional argument N, when non-nil, is an integer specifying the
647 Optional argument KEYWORD specifies the TOC keyword, if any, from
648 which the table of contents generation has been initiated."
649 (let ((title (org-ascii--translate "Table of Contents" info
)))
652 (make-string (length title
)
653 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
656 (if keyword
(org-ascii--current-text-width keyword info
)
657 (- org-ascii-text-width org-ascii-global-margin
))))
660 (let* ((level (org-export-get-relative-level headline info
))
661 (indent (* (1- level
) 3)))
663 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
664 (org-ascii--build-title
665 headline info
(- text-width indent
) nil
666 (or (not (plist-get info
:with-tags
))
667 (eq (plist-get info
:with-tags
) 'not-in-toc
))
669 (org-export-collect-headlines info n
) "\n")))))
671 (defun org-ascii--list-listings (keyword info
)
672 "Return a list of listings.
674 KEYWORD is the keyword that initiated the list of listings
675 generation. INFO is a plist used as a communication channel."
676 (let ((title (org-ascii--translate "List of Listings" info
)))
679 (make-string (length title
)
680 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
683 (if keyword
(org-ascii--current-text-width keyword info
)
684 (- org-ascii-text-width org-ascii-global-margin
)))
685 ;; Use a counter instead of retrieving ordinal of each
690 ;; Store initial text so its length can be computed. This is
691 ;; used to properly align caption right to it in case of
692 ;; filling (like contents of a description list item).
694 (format (org-ascii--translate "Listing %d:" info
)
699 (org-ascii--indent-string
700 (org-ascii--fill-string
701 ;; Use short name in priority, if available.
702 (let ((caption (or (org-export-get-caption src-block t
)
703 (org-export-get-caption src-block
))))
704 (org-export-data caption info
))
705 (- text-width
(length initial-text
)) info
)
706 (length initial-text
))))))
707 (org-export-collect-listings info
) "\n")))))
709 (defun org-ascii--list-tables (keyword info
)
710 "Return a list of tables.
712 KEYWORD is the keyword that initiated the list of tables
713 generation. INFO is a plist used as a communication channel."
714 (let ((title (org-ascii--translate "List of Tables" info
)))
717 (make-string (length title
)
718 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
721 (if keyword
(org-ascii--current-text-width keyword info
)
722 (- org-ascii-text-width org-ascii-global-margin
)))
723 ;; Use a counter instead of retrieving ordinal of each
728 ;; Store initial text so its length can be computed. This is
729 ;; used to properly align caption right to it in case of
730 ;; filling (like contents of a description list item).
732 (format (org-ascii--translate "Table %d:" info
)
737 (org-ascii--indent-string
738 (org-ascii--fill-string
739 ;; Use short name in priority, if available.
740 (let ((caption (or (org-export-get-caption table t
)
741 (org-export-get-caption table
))))
742 (org-export-data caption info
))
743 (- text-width
(length initial-text
)) info
)
744 (length initial-text
))))))
745 (org-export-collect-tables info
) "\n")))))
747 (defun org-ascii--unique-links (element info
)
748 "Return a list of unique link references in ELEMENT.
750 ELEMENT is either a headline element or a section element. INFO
751 is a plist used as a communication channel."
755 ;; Return LINK if it wasn't referenced so far, or nil.
756 ;; Update SEEN links along the way.
759 (cons (org-element-property :raw-link link
)
760 (org-element-contents link
))))
761 ;; Ignore LINK if it hasn't been translated already.
762 ;; It can happen if it is located in an affiliated
763 ;; keyword that was ignored.
764 (when (and (org-string-nw-p
765 (gethash link
(plist-get info
:exported-data
)))
766 (not (member footprint seen
)))
767 (push footprint seen
) link
)))))
768 ;; If at a section, find parent headline, if any, in order to
769 ;; count links that might be in the title.
771 (if (eq (org-element-type element
) 'headline
) element
772 (or (org-export-get-parent-headline element
) element
))))
773 ;; Get all links in HEADLINE.
774 (org-element-map headline
'link
775 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
777 (defun org-ascii--describe-links (links width info
)
778 "Return a string describing a list of links.
780 LINKS is a list of link type objects, as returned by
781 `org-ascii--unique-links'. WIDTH is the text width allowed for
782 the output string. INFO is a plist used as a communication
786 (let ((type (org-element-property :type link
))
787 (anchor (let ((desc (org-element-contents link
)))
788 (if desc
(org-export-data desc info
)
789 (org-element-property :raw-link link
)))))
791 ;; Coderefs, radio links and fuzzy links are ignored.
792 ((member type
'("coderef" "radio" "fuzzy")) nil
)
793 ;; Id and custom-id links: Headlines refer to their numbering.
794 ((member type
'("custom-id" "id"))
795 (let ((dest (org-export-resolve-id-link link info
)))
797 (org-ascii--fill-string
801 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
803 (org-ascii--translate "See section %s" info
)
804 (mapconcat 'number-to-string
805 (org-export-get-headline-number dest info
) "."))))
806 width info
) "\n\n")))
807 ;; Do not add a link that cannot be resolved and doesn't have
808 ;; any description: destination is already visible in the
810 ((not (org-element-contents link
)) nil
)
813 (org-ascii--fill-string
814 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
819 (defun org-ascii--checkbox (item info
)
820 "Return checkbox string for ITEM or nil.
821 INFO is a plist used as a communication channel."
822 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
823 (case (org-element-property :checkbox item
)
824 (on (if utf8p
"☑ " "[X] "))
825 (off (if utf8p
"☐ " "[ ] "))
826 (trans (if utf8p
"☒ " "[-] ")))))
832 (defun org-ascii-template--document-title (info)
833 "Return document title, as a string.
834 INFO is a plist used as a communication channel."
835 (let* ((text-width org-ascii-text-width
)
836 ;; Links in the title will not be resolved later, so we make
837 ;; sure their path is located right after them.
838 (org-ascii-links-to-notes nil
)
839 (title (org-export-data (plist-get info
:title
) info
))
840 (author (and (plist-get info
:with-author
)
841 (let ((auth (plist-get info
:author
)))
842 (and auth
(org-export-data auth info
)))))
843 (email (and (plist-get info
:with-email
)
844 (org-export-data (plist-get info
:email
) info
)))
845 (date (and (plist-get info
:with-date
)
846 (org-export-data (org-export-get-date info
) info
))))
847 ;; There are two types of title blocks depending on the presence
848 ;; of a title to display.
849 (if (string= title
"")
850 ;; Title block without a title. DATE is positioned at the top
851 ;; right of the document, AUTHOR to the top left and EMAIL
854 ((and (org-string-nw-p date
) (org-string-nw-p author
))
857 (make-string (- text-width
(length date
) (length author
)) ?
)
859 (when (org-string-nw-p email
) (concat "\n" email
))
861 ((and (org-string-nw-p date
) (org-string-nw-p email
))
864 (make-string (- text-width
(length date
) (length email
)) ?
)
866 ((org-string-nw-p date
)
868 (org-ascii--justify-string date text-width
'right
)
870 ((and (org-string-nw-p author
) (org-string-nw-p email
))
871 (concat author
"\n" email
"\n\n\n"))
872 ((org-string-nw-p author
) (concat author
"\n\n\n"))
873 ((org-string-nw-p email
) (concat email
"\n\n\n")))
874 ;; Title block with a title. Document's TITLE, along with the
875 ;; AUTHOR and its EMAIL are both overlined and an underlined,
876 ;; centered. Date is just below, also centered.
877 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
878 ;; Format TITLE. It may be filled if it is too wide,
879 ;; that is wider than the two thirds of the total width.
880 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
881 (formatted-title (org-ascii--fill-string title title-len info
))
884 (min (+ (max title-len
(length author
) (length email
)) 2)
885 text-width
) (if utf8p ?━ ?_
))))
886 (org-ascii--justify-string
889 (upcase formatted-title
)
891 ((and (org-string-nw-p author
) (org-string-nw-p email
))
892 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
893 ((org-string-nw-p author
)
894 (concat (if utf8p
"\n\n\n" "\n\n") author
))
895 ((org-string-nw-p email
)
896 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
898 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
899 "\n\n\n") text-width
'center
)))))
901 (defun org-ascii-inner-template (contents info
)
902 "Return complete document string after ASCII conversion.
903 CONTENTS is the transcoded contents string. INFO is a plist
904 holding export options."
905 (org-element-normalize-string
906 (org-ascii--indent-string
908 ;; 1. Document's body.
910 ;; 2. Footnote definitions.
911 (let ((definitions (org-export-collect-footnote-definitions
912 (plist-get info
:parse-tree
) info
))
913 ;; Insert full links right inside the footnote definition
914 ;; as they have no chance to be inserted later.
915 (org-ascii-links-to-notes nil
))
919 (let ((title (org-ascii--translate "Footnotes" info
)))
924 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
926 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
929 (let ((id (format "[%s] " (car ref
))))
930 ;; Distinguish between inline definitions and
931 ;; full-fledged definitions.
933 (let ((def (nth 2 ref
)))
934 (if (eq (org-element-type def
) 'org-data
)
935 ;; Full-fledged definition: footnote ID is
936 ;; inserted inside the first parsed paragraph
937 ;; (FIRST), if any, to be sure filling will
938 ;; take it into consideration.
939 (let ((first (car (org-element-contents def
))))
940 (if (not (eq (org-element-type first
) 'paragraph
))
941 (concat id
"\n" (org-export-data def info
))
942 (push id
(nthcdr 2 first
))
943 (org-export-data def info
)))
944 ;; Fill paragraph once footnote ID is inserted
945 ;; in order to have a correct length for first
947 (org-ascii--fill-string
948 (concat id
(org-export-data def info
))
949 text-width info
))))))
950 definitions
"\n\n"))))))
951 org-ascii-global-margin
)))
953 (defun org-ascii-template (contents info
)
954 "Return complete document string after ASCII conversion.
955 CONTENTS is the transcoded contents string. INFO is a plist
956 holding export options."
958 ;; 1. Build title block.
959 (org-ascii--indent-string
960 (concat (org-ascii-template--document-title info
)
961 ;; 2. Table of contents.
962 (let ((depth (plist-get info
:with-toc
)))
965 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
967 org-ascii-global-margin
)
968 ;; 3. Document's body.
970 ;; 4. Creator. Ignore `comment' value as there are no comments in
971 ;; ASCII. Justify it to the bottom right.
972 (org-ascii--indent-string
973 (let ((creator-info (plist-get info
:with-creator
))
974 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
975 (unless (or (not creator-info
) (eq creator-info
'comment
))
978 (org-ascii--fill-string
979 (plist-get info
:creator
) text-width info
'right
))))
980 org-ascii-global-margin
)))
982 (defun org-ascii--translate (s info
)
983 "Translate string S according to specified language and charset.
984 INFO is a plist used as a communication channel."
985 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
986 (org-export-translate s charset info
)))
990 ;;; Transcode Functions
994 (defun org-ascii-bold (bold contents info
)
995 "Transcode BOLD from Org to ASCII.
996 CONTENTS is the text with bold markup. INFO is a plist holding
997 contextual information."
998 (format "*%s*" contents
))
1003 (defun org-ascii-center-block (center-block contents info
)
1004 "Transcode a CENTER-BLOCK element from Org to ASCII.
1005 CONTENTS holds the contents of the block. INFO is a plist
1006 holding contextual information."
1007 (org-ascii--justify-string
1008 contents
(org-ascii--current-text-width center-block info
) 'center
))
1013 (defun org-ascii-clock (clock contents info
)
1014 "Transcode a CLOCK object from Org to ASCII.
1015 CONTENTS is nil. INFO is a plist holding contextual
1017 (concat org-clock-string
" "
1019 (org-element-property :raw-value
1020 (org-element-property :value clock
)))
1021 (let ((time (org-element-property :duration clock
)))
1026 (org-split-string time
":")))))))
1031 (defun org-ascii-code (code contents info
)
1032 "Return a CODE object from Org to ASCII.
1033 CONTENTS is nil. INFO is a plist holding contextual
1035 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1040 (defun org-ascii-drawer (drawer contents info
)
1041 "Transcode a DRAWER element from Org to ASCII.
1042 CONTENTS holds the contents of the block. INFO is a plist
1043 holding contextual information."
1044 (let ((name (org-element-property :drawer-name drawer
))
1045 (width (org-ascii--current-text-width drawer info
)))
1046 (funcall org-ascii-format-drawer-function name contents width
)))
1051 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1052 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1053 CONTENTS holds the contents of the block. INFO is a plist
1054 holding contextual information."
1060 (defun org-ascii-entity (entity contents info
)
1061 "Transcode an ENTITY object from Org to ASCII.
1062 CONTENTS are the definition itself. INFO is a plist holding
1063 contextual information."
1064 (org-element-property
1065 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1071 (defun org-ascii-example-block (example-block contents info
)
1072 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1073 CONTENTS is nil. INFO is a plist holding contextual information."
1074 (org-ascii--box-string
1075 (org-export-format-code-default example-block info
) info
))
1080 (defun org-ascii-export-snippet (export-snippet contents info
)
1081 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1082 CONTENTS is nil. INFO is a plist holding contextual information."
1083 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1084 (org-element-property :value export-snippet
)))
1089 (defun org-ascii-export-block (export-block contents info
)
1090 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1091 CONTENTS is nil. INFO is a plist holding contextual information."
1092 (when (string= (org-element-property :type export-block
) "ASCII")
1093 (org-remove-indentation (org-element-property :value export-block
))))
1098 (defun org-ascii-fixed-width (fixed-width contents info
)
1099 "Transcode a FIXED-WIDTH element from Org to ASCII.
1100 CONTENTS is nil. INFO is a plist holding contextual information."
1101 (org-ascii--box-string
1102 (org-remove-indentation
1103 (org-element-property :value fixed-width
)) info
))
1106 ;;;; Footnote Definition
1108 ;; Footnote Definitions are ignored. They are compiled at the end of
1109 ;; the document, by `org-ascii-inner-template'.
1112 ;;;; Footnote Reference
1114 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1115 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1116 CONTENTS is nil. INFO is a plist holding contextual information."
1117 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1122 (defun org-ascii-headline (headline contents info
)
1123 "Transcode a HEADLINE element from Org to ASCII.
1124 CONTENTS holds the contents of the headline. INFO is a plist
1125 holding contextual information."
1126 ;; Don't export footnote section, which will be handled at the end
1128 (unless (org-element-property :footnote-section-p headline
)
1129 (let* ((low-level-rank (org-export-low-level-p headline info
))
1130 (width (org-ascii--current-text-width headline info
))
1131 ;; Blank lines between headline and its contents.
1132 ;; `org-ascii-headline-spacing', when set, overwrites
1133 ;; original buffer's spacing.
1136 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1137 (org-element-property :pre-blank headline
)) ?
\n))
1138 ;; Even if HEADLINE has no section, there might be some
1139 ;; links in its title that we shouldn't forget to describe.
1141 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1142 (let ((title (org-element-property :title headline
)))
1144 (org-ascii--describe-links
1145 (org-ascii--unique-links title info
) width info
))))))
1146 ;; Deep subtree: export it as a list item.
1150 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1151 org-ascii-bullets
))))
1153 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1156 (org-ascii--build-title headline info width
) "\n"
1157 ;; Contents, indented by length of bullet.
1159 (org-ascii--indent-string
1161 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1163 ;; Else: Standard headline.
1165 (org-ascii--build-title headline info width
'underline
)
1167 (concat (when (org-string-nw-p links
) links
) contents
))))))
1170 ;;;; Horizontal Rule
1172 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1173 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1174 CONTENTS is nil. INFO is a plist holding contextual
1176 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1178 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1179 (org-ascii--justify-string
1180 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1181 (string-to-number spec-width
)
1183 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1184 text-width
'center
)))
1187 ;;;; Inline Src Block
1189 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1190 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1191 CONTENTS holds the contents of the item. INFO is a plist holding
1192 contextual information."
1193 (format org-ascii-verbatim-format
1194 (org-element-property :value inline-src-block
)))
1199 (defun org-ascii-format-inlinetask-default
1200 (todo type priority name tags contents width inlinetask info
)
1201 "Format an inline task element for ASCII export.
1202 See `org-ascii-format-inlinetask-function' for a description
1204 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1205 (width (or width org-ascii-inlinetask-width
)))
1206 (org-ascii--indent-string
1208 ;; Top line, with an additional blank line if not in UTF-8.
1209 (make-string width
(if utf8p ?━ ?_
)) "\n"
1210 (unless utf8p
(concat (make-string width ?
) "\n"))
1211 ;; Add title. Fill it if wider than inlinetask.
1212 (let ((title (org-ascii--build-title inlinetask info width
)))
1213 (if (<= (length title
) width
) title
1214 (org-ascii--fill-string title width info
)))
1216 ;; If CONTENTS is not empty, insert it along with
1218 (when (org-string-nw-p contents
)
1219 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1221 (make-string width
(if utf8p ?━ ?_
)))
1222 ;; Flush the inlinetask to the right.
1223 (- org-ascii-text-width org-ascii-global-margin
1224 (if (not (org-export-get-parent-headline inlinetask
)) 0
1225 org-ascii-inner-margin
)
1226 (org-ascii--current-text-width inlinetask info
)))))
1228 (defun org-ascii-inlinetask (inlinetask contents info
)
1229 "Transcode an INLINETASK element from Org to ASCII.
1230 CONTENTS holds the contents of the block. INFO is a plist
1231 holding contextual information."
1232 (let ((width (org-ascii--current-text-width inlinetask info
)))
1233 (funcall org-ascii-format-inlinetask-function
1235 (and (plist-get info
:with-todo-keywords
)
1236 (let ((todo (org-element-property
1237 :todo-keyword inlinetask
)))
1238 (and todo
(org-export-data todo info
))))
1240 (org-element-property :todo-type inlinetask
)
1242 (and (plist-get info
:with-priority
)
1243 (org-element-property :priority inlinetask
))
1245 (org-export-data (org-element-property :title inlinetask
) info
)
1247 (and (plist-get info
:with-tags
)
1248 (org-element-property :tags inlinetask
))
1249 ;; contents and width
1250 contents width inlinetask info
)))
1255 (defun org-ascii-italic (italic contents info
)
1256 "Transcode italic from Org to ASCII.
1257 CONTENTS is the text with italic markup. INFO is a plist holding
1258 contextual information."
1259 (format "/%s/" contents
))
1264 (defun org-ascii-item (item contents info
)
1265 "Transcode an ITEM element from Org to ASCII.
1266 CONTENTS holds the contents of the item. INFO is a plist holding
1267 contextual information."
1268 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1269 (checkbox (org-ascii--checkbox item info
))
1270 (list-type (org-element-property :type
(org-export-get-parent item
)))
1272 ;; First parent of ITEM is always the plain-list. Get
1273 ;; `:type' property from it.
1274 (org-list-bullet-string
1278 (org-export-data (org-element-property :tag item
) info
)
1281 ;; Return correct number for ITEM, paying attention to
1283 (let* ((struct (org-element-property :structure item
))
1284 (bul (org-element-property :bullet item
))
1285 (num (number-to-string
1286 (car (last (org-list-get-item-number
1287 (org-element-property :begin item
)
1289 (org-list-prevs-alist struct
)
1290 (org-list-parents-alist struct
)))))))
1291 (replace-regexp-in-string "[0-9]+" num bul
)))
1292 (t (let ((bul (org-element-property :bullet item
)))
1293 ;; Change bullets into more visible form if UTF-8 is active.
1295 (replace-regexp-in-string
1297 (replace-regexp-in-string
1299 (replace-regexp-in-string "*" "‣" bul
))))))))))
1302 (unless (eq list-type
'descriptive
) checkbox
)
1303 ;; Contents: Pay attention to indentation. Note: check-boxes are
1304 ;; already taken care of at the paragraph level so they don't
1305 ;; interfere with indentation.
1306 (let ((contents (org-ascii--indent-string contents
(length bullet
))))
1307 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1309 (concat "\n" contents
))))))
1314 (defun org-ascii-keyword (keyword contents info
)
1315 "Transcode a KEYWORD element from Org to ASCII.
1316 CONTENTS is nil. INFO is a plist holding contextual
1318 (let ((key (org-element-property :key keyword
))
1319 (value (org-element-property :value keyword
)))
1321 ((string= key
"ASCII") value
)
1322 ((string= key
"TOC")
1323 (let ((value (downcase value
)))
1325 ((string-match "\\<headlines\\>" value
)
1326 (let ((depth (or (and (string-match "[0-9]+" value
)
1327 (string-to-number (match-string 0 value
)))
1328 (plist-get info
:with-toc
))))
1329 (org-ascii--build-toc
1330 info
(and (wholenump depth
) depth
) keyword
)))
1331 ((string= "tables" value
)
1332 (org-ascii--list-tables keyword info
))
1333 ((string= "listings" value
)
1334 (org-ascii--list-listings keyword info
))))))))
1337 ;;;; Latex Environment
1339 (defun org-ascii-latex-environment (latex-environment contents info
)
1340 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1341 CONTENTS is nil. INFO is a plist holding contextual
1343 (when (plist-get info
:with-latex
)
1344 (org-remove-indentation (org-element-property :value latex-environment
))))
1349 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1350 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1351 CONTENTS is nil. INFO is a plist holding contextual
1353 (when (plist-get info
:with-latex
)
1354 (org-element-property :value latex-fragment
)))
1359 (defun org-ascii-line-break (line-break contents info
)
1360 "Transcode a LINE-BREAK object from Org to ASCII.
1361 CONTENTS is nil. INFO is a plist holding contextual
1362 information." hard-newline
)
1367 (defun org-ascii-link (link desc info
)
1368 "Transcode a LINK object from Org to ASCII.
1370 DESC is the description part of the link, or the empty string.
1371 INFO is a plist holding contextual information."
1372 (let ((raw-link (org-element-property :raw-link link
))
1373 (type (org-element-property :type link
)))
1375 ((string= type
"coderef")
1376 (let ((ref (org-element-property :path link
)))
1377 (format (org-export-get-coderef-format ref desc
)
1378 (org-export-resolve-coderef ref info
))))
1379 ;; Do not apply a special syntax on radio links. Though, use
1380 ;; transcoded target's contents as output.
1381 ((string= type
"radio")
1382 (let ((destination (org-export-resolve-radio-link link info
)))
1384 (org-export-data (org-element-contents destination
) info
))))
1385 ;; Do not apply a special syntax on fuzzy links pointing to
1387 ((string= type
"fuzzy")
1388 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1389 (if (org-string-nw-p desc
) desc
1392 (org-export-get-ordinal
1393 destination info nil
'org-ascii--has-caption-p
)))
1395 (if (atom number
) (number-to-string number
)
1396 (mapconcat 'number-to-string number
"."))))))))
1398 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1400 (format "[%s]" desc
)
1401 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1404 ;;;; Node Properties
1406 (defun org-ascii-node-property (node-property contents info
)
1407 "Transcode a NODE-PROPERTY element from Org to ASCII.
1408 CONTENTS is nil. INFO is a plist holding contextual
1411 (org-element-property :key node-property
)
1412 (let ((value (org-element-property :value node-property
)))
1413 (if value
(concat " " value
) ""))))
1418 (defun org-ascii-paragraph (paragraph contents info
)
1419 "Transcode a PARAGRAPH element from Org to ASCII.
1420 CONTENTS is the contents of the paragraph, as a string. INFO is
1421 the plist used as a communication channel."
1422 (let ((contents (if (not (wholenump org-ascii-indented-line-width
)) contents
1424 (make-string org-ascii-indented-line-width ?
)
1425 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1426 (org-ascii--fill-string
1427 contents
(org-ascii--current-text-width paragraph info
) info
)))
1432 (defun org-ascii-plain-list (plain-list contents info
)
1433 "Transcode a PLAIN-LIST element from Org to ASCII.
1434 CONTENTS is the contents of the list. INFO is a plist holding
1435 contextual information."
1441 (defun org-ascii-plain-text (text info
)
1442 "Transcode a TEXT string from Org to ASCII.
1443 INFO is a plist used as a communication channel."
1444 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1445 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1446 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1447 (if (not (plist-get info
:with-special-strings
)) text
1448 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1449 (if (not utf8p
) text
1450 ;; Usual replacements in utf-8 with proper option set.
1451 (replace-regexp-in-string
1453 (replace-regexp-in-string
1455 (replace-regexp-in-string "---" "—" text
)))))))
1460 (defun org-ascii-planning (planning contents info
)
1461 "Transcode a PLANNING element from Org to ASCII.
1462 CONTENTS is nil. INFO is a plist used as a communication
1467 (list (let ((closed (org-element-property :closed planning
)))
1469 (concat org-closed-string
" "
1471 (org-element-property :raw-value closed
)))))
1472 (let ((deadline (org-element-property :deadline planning
)))
1474 (concat org-deadline-string
" "
1476 (org-element-property :raw-value deadline
)))))
1477 (let ((scheduled (org-element-property :scheduled planning
)))
1479 (concat org-scheduled-string
" "
1481 (org-element-property :raw-value scheduled
)))))))
1485 ;;;; Property Drawer
1487 (defun org-ascii-property-drawer (property-drawer contents info
)
1488 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1489 CONTENTS holds the contents of the drawer. INFO is a plist
1490 holding contextual information."
1491 (org-string-nw-p contents
))
1496 (defun org-ascii-quote-block (quote-block contents info
)
1497 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1498 CONTENTS holds the contents of the block. INFO is a plist
1499 holding contextual information."
1500 (org-ascii--indent-string contents org-ascii-quote-margin
))
1505 (defun org-ascii-quote-section (quote-section contents info
)
1506 "Transcode a QUOTE-SECTION element from Org to ASCII.
1507 CONTENTS is nil. INFO is a plist holding contextual information."
1508 (let ((width (org-ascii--current-text-width quote-section info
))
1511 (org-remove-indentation (org-element-property :value quote-section
))
1513 (org-ascii--indent-string
1515 (+ org-ascii-quote-margin
1516 ;; Don't apply inner margin if parent headline is low level.
1517 (let ((headline (org-export-get-parent-headline quote-section
)))
1518 (if (org-export-low-level-p headline info
) 0
1519 org-ascii-inner-margin
))))))
1524 (defun org-ascii-radio-target (radio-target contents info
)
1525 "Transcode a RADIO-TARGET object from Org to ASCII.
1526 CONTENTS is the contents of the target. INFO is a plist holding
1527 contextual information."
1533 (defun org-ascii-section (section contents info
)
1534 "Transcode a SECTION element from Org to ASCII.
1535 CONTENTS is the contents of the section. INFO is a plist holding
1536 contextual information."
1537 (org-ascii--indent-string
1540 (when org-ascii-links-to-notes
1541 ;; Add list of links at the end of SECTION.
1542 (let ((links (org-ascii--describe-links
1543 (org-ascii--unique-links section info
)
1544 (org-ascii--current-text-width section info
) info
)))
1545 ;; Separate list of links and section contents.
1546 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1547 ;; Do not apply inner margin if parent headline is low level.
1548 (let ((headline (org-export-get-parent-headline section
)))
1549 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1550 org-ascii-inner-margin
))))
1555 (defun org-ascii-special-block (special-block contents info
)
1556 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1557 CONTENTS holds the contents of the block. INFO is a plist
1558 holding contextual information."
1564 (defun org-ascii-src-block (src-block contents info
)
1565 "Transcode a SRC-BLOCK element from Org to ASCII.
1566 CONTENTS holds the contents of the item. INFO is a plist holding
1567 contextual information."
1568 (let ((caption (org-ascii--build-caption src-block info
))
1569 (code (org-export-format-code-default src-block info
)))
1570 (if (equal code
"") ""
1572 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1573 (org-ascii--box-string code info
)
1574 (when (and caption
(not org-ascii-caption-above
))
1575 (concat "\n" caption
))))))
1578 ;;;; Statistics Cookie
1580 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1581 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1582 CONTENTS is nil. INFO is a plist holding contextual information."
1583 (org-element-property :value statistics-cookie
))
1588 (defun org-ascii-subscript (subscript contents info
)
1589 "Transcode a SUBSCRIPT object from Org to ASCII.
1590 CONTENTS is the contents of the object. INFO is a plist holding
1591 contextual information."
1592 (if (org-element-property :use-brackets-p subscript
)
1593 (format "_{%s}" contents
)
1594 (format "_%s" contents
)))
1599 (defun org-ascii-superscript (superscript contents info
)
1600 "Transcode a SUPERSCRIPT object from Org to ASCII.
1601 CONTENTS is the contents of the object. INFO is a plist holding
1602 contextual information."
1603 (if (org-element-property :use-brackets-p superscript
)
1604 (format "_{%s}" contents
)
1605 (format "_%s" contents
)))
1610 (defun org-ascii-strike-through (strike-through contents info
)
1611 "Transcode STRIKE-THROUGH from Org to ASCII.
1612 CONTENTS is text with strike-through markup. INFO is a plist
1613 holding contextual information."
1614 (format "+%s+" contents
))
1619 (defun org-ascii-table (table contents info
)
1620 "Transcode a TABLE element from Org to ASCII.
1621 CONTENTS is the contents of the table. INFO is a plist holding
1622 contextual information."
1623 (let ((caption (org-ascii--build-caption table info
)))
1625 ;; Possibly add a caption string above.
1626 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1627 ;; Insert table. Note: "table.el" tables are left unmodified.
1628 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1629 ((and org-ascii-table-use-ascii-art
1630 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1631 (require 'ascii-art-to-unicode nil t
))
1633 (insert (org-remove-indentation
1634 (org-element-property :value table
)))
1635 (goto-char (point-min))
1637 (goto-char (point-max))
1638 (skip-chars-backward " \r\t\n")
1639 (buffer-substring (point-min) (point))))
1640 (t (org-remove-indentation (org-element-property :value table
))))
1641 ;; Possible add a caption string below.
1642 (and (not org-ascii-caption-above
) caption
))))
1647 (defun org-ascii--table-cell-width (table-cell info
)
1648 "Return width of TABLE-CELL.
1650 INFO is a plist used as a communication channel.
1652 Width of a cell is determined either by a width cookie in the
1653 same column as the cell, or by the maximum cell's length in that
1656 When `org-ascii-table-widen-columns' is non-nil, width cookies
1658 (let* ((row (org-export-get-parent table-cell
))
1659 (table (org-export-get-parent row
))
1660 (col (let ((cells (org-element-contents row
)))
1661 (- (length cells
) (length (memq table-cell cells
)))))
1663 (or (plist-get info
:ascii-table-cell-width-cache
)
1664 (plist-get (setq info
1665 (plist-put info
:ascii-table-cell-width-cache
1666 (make-hash-table :test
'equal
)))
1667 :ascii-table-cell-width-cache
)))
1668 (key (cons table col
)))
1669 (or (gethash key cache
)
1672 (or (and (not org-ascii-table-widen-columns
)
1673 (org-export-table-cell-width table-cell info
))
1674 (let* ((max-width 0))
1675 (org-element-map table
'table-row
1680 (org-element-contents
1681 (elt (org-element-contents row
) col
))
1688 (defun org-ascii-table-cell (table-cell contents info
)
1689 "Transcode a TABLE-CELL object from Org to ASCII.
1690 CONTENTS is the cell contents. INFO is a plist used as
1691 a communication channel."
1692 ;; Determine column width. When `org-ascii-table-widen-columns'
1693 ;; is nil and some width cookie has set it, use that value.
1694 ;; Otherwise, compute the maximum width among transcoded data of
1695 ;; each cell in the column.
1696 (let ((width (org-ascii--table-cell-width table-cell info
)))
1697 ;; When contents are too large, truncate them.
1698 (unless (or org-ascii-table-widen-columns
(<= (length contents
) width
))
1699 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1700 ;; Align contents correctly within the cell.
1701 (let* ((indent-tabs-mode nil
)
1704 (org-ascii--justify-string
1706 (org-export-table-cell-alignment table-cell info
)))))
1707 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1709 (concat (format " %s " contents
)
1710 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1711 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1716 (defun org-ascii-table-row (table-row contents info
)
1717 "Transcode a TABLE-ROW element from Org to ASCII.
1718 CONTENTS is the row contents. INFO is a plist used as
1719 a communication channel."
1720 (when (eq (org-element-property :type table-row
) 'standard
)
1723 (lambda (lcorner horiz vert rcorner
)
1727 (org-element-map table-row
'table-cell
1729 (let ((width (org-ascii--table-cell-width cell info
))
1730 (borders (org-export-table-cell-borders cell info
)))
1732 ;; In order to know if CELL starts the row, do
1733 ;; not compare it with the first cell in the
1734 ;; row as there might be a special column.
1735 ;; Instead, compare it with first exportable
1736 ;; cell, obtained with `org-element-map'.
1737 (when (and (memq 'left borders
)
1738 (eq (org-element-map table-row
'table-cell
1742 (make-string (+ 2 width
) (string-to-char horiz
))
1744 ((not (memq 'right borders
)) nil
)
1745 ((eq (car (last (org-element-contents table-row
))) cell
)
1749 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1750 (borders (org-export-table-cell-borders
1751 (org-element-map table-row
'table-cell
'identity info t
)
1754 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1755 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1756 (funcall build-hline
"+" "-" "+" "+")))
1757 ((memq 'above borders
)
1758 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1759 (funcall build-hline
"+" "-" "+" "+"))))
1760 (when (memq 'left borders
) (if utf8p
"│" "|"))
1762 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1763 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1764 (funcall build-hline
"+" "-" "+" "+")))))))
1769 (defun org-ascii-timestamp (timestamp contents info
)
1770 "Transcode a TIMESTAMP object from Org to ASCII.
1771 CONTENTS is nil. INFO is a plist holding contextual information."
1772 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1777 (defun org-ascii-underline (underline contents info
)
1778 "Transcode UNDERLINE from Org to ASCII.
1779 CONTENTS is the text with underline markup. INFO is a plist
1780 holding contextual information."
1781 (format "_%s_" contents
))
1786 (defun org-ascii-verbatim (verbatim contents info
)
1787 "Return a VERBATIM object from Org to ASCII.
1788 CONTENTS is nil. INFO is a plist holding contextual information."
1789 (format org-ascii-verbatim-format
1790 (org-element-property :value verbatim
)))
1795 (defun org-ascii-verse-block (verse-block contents info
)
1796 "Transcode a VERSE-BLOCK element from Org to ASCII.
1797 CONTENTS is verse block contents. INFO is a plist holding
1798 contextual information."
1799 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1800 (org-ascii--indent-string
1801 (org-ascii--justify-string contents verse-width
'left
)
1802 org-ascii-quote-margin
)))
1808 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1809 "Filter controlling number of blank lines after a headline.
1811 HEADLINE is a string representing a transcoded headline.
1812 BACK-END is symbol specifying back-end used for export. INFO is
1813 plist containing the communication channel.
1815 This function only applies to `ascii' back-end. See
1816 `org-ascii-headline-spacing' for information."
1817 (if (not org-ascii-headline-spacing
) headline
1818 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1819 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1821 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1822 "Filter controlling number of blank lines between paragraphs.
1824 TREE is the parse tree. BACK-END is the symbol specifying
1825 back-end used for export. INFO is a plist used as
1826 a communication channel.
1828 See `org-ascii-paragraph-spacing' for information."
1829 (when (wholenump org-ascii-paragraph-spacing
)
1830 (org-element-map tree
'paragraph
1832 (when (eq (org-element-type (org-export-get-next-element p info
))
1834 (org-element-put-property
1835 p
:post-blank org-ascii-paragraph-spacing
)))))
1838 (defun org-ascii-filter-comment-spacing (tree backend info
)
1839 "Filter removing blank lines between comments.
1840 TREE is the parse tree. BACK-END is the symbol specifying
1841 back-end used for export. INFO is a plist used as
1842 a communication channel."
1843 (org-element-map tree
'(comment comment-block
)
1845 (when (memq (org-element-type (org-export-get-next-element c info
))
1846 '(comment comment-block
))
1847 (org-element-put-property c
:post-blank
0))))
1852 ;;; End-user functions
1855 (defun org-ascii-export-as-ascii
1856 (&optional async subtreep visible-only body-only ext-plist
)
1857 "Export current buffer to a text buffer.
1859 If narrowing is active in the current buffer, only export its
1862 If a region is active, export that region.
1864 A non-nil optional argument ASYNC means the process should happen
1865 asynchronously. The resulting buffer should be accessible
1866 through the `org-export-stack' interface.
1868 When optional argument SUBTREEP is non-nil, export the sub-tree
1869 at point, extracting information from the headline properties
1872 When optional argument VISIBLE-ONLY is non-nil, don't export
1873 contents of hidden elements.
1875 When optional argument BODY-ONLY is non-nil, strip title and
1876 table of contents from output.
1878 EXT-PLIST, when provided, is a property list with external
1879 parameters overriding Org default settings, but still inferior to
1880 file-local settings.
1882 Export is done in a buffer named \"*Org ASCII Export*\", which
1883 will be displayed when `org-export-show-temporary-export-buffer'
1886 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
1887 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
1890 (defun org-ascii-export-to-ascii
1891 (&optional async subtreep visible-only body-only ext-plist
)
1892 "Export current buffer to a text file.
1894 If narrowing is active in the current buffer, only export its
1897 If a region is active, export that region.
1899 A non-nil optional argument ASYNC means the process should happen
1900 asynchronously. The resulting file should be accessible through
1901 the `org-export-stack' interface.
1903 When optional argument SUBTREEP is non-nil, export the sub-tree
1904 at point, extracting information from the headline properties
1907 When optional argument VISIBLE-ONLY is non-nil, don't export
1908 contents of hidden elements.
1910 When optional argument BODY-ONLY is non-nil, strip title and
1911 table of contents from output.
1913 EXT-PLIST, when provided, is a property list with external
1914 parameters overriding Org default settings, but still inferior to
1915 file-local settings.
1917 Return output file's name."
1919 (let ((file (org-export-output-file-name ".txt" subtreep
)))
1920 (org-export-to-file 'ascii file
1921 async subtreep visible-only body-only ext-plist
)))
1924 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1925 "Publish an Org file to ASCII.
1927 FILENAME is the filename of the Org file to be published. PLIST
1928 is the property list for the given project. PUB-DIR is the
1929 publishing directory.
1931 Return output file name."
1933 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1936 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1937 "Publish an Org file to Latin-1.
1939 FILENAME is the filename of the Org file to be published. PLIST
1940 is the property list for the given project. PUB-DIR is the
1941 publishing directory.
1943 Return output file name."
1945 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1948 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1949 "Publish an org file to UTF-8.
1951 FILENAME is the filename of the Org file to be published. PLIST
1952 is the property list for the given project. PUB-DIR is the
1953 publishing directory.
1955 Return output file name."
1957 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1963 ;; generated-autoload-file: "org-loaddefs.el"
1964 ;; coding: utf-8-emacs
1967 ;;; ox-ascii.el ends here