1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2015 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 (paragraph . org-ascii-paragraph
)
75 (plain-list . org-ascii-plain-list
)
76 (plain-text . org-ascii-plain-text
)
77 (planning . org-ascii-planning
)
78 (quote-block . org-ascii-quote-block
)
79 (quote-section . org-ascii-quote-section
)
80 (radio-target . org-ascii-radio-target
)
81 (section . org-ascii-section
)
82 (special-block . org-ascii-special-block
)
83 (src-block . org-ascii-src-block
)
84 (statistics-cookie . org-ascii-statistics-cookie
)
85 (strike-through . org-ascii-strike-through
)
86 (subscript . org-ascii-subscript
)
87 (superscript . org-ascii-superscript
)
88 (table . org-ascii-table
)
89 (table-cell . org-ascii-table-cell
)
90 (table-row . org-ascii-table-row
)
91 (target . org-ascii-target
)
92 (template . org-ascii-template
)
93 (timestamp . org-ascii-timestamp
)
94 (underline . org-ascii-underline
)
95 (verbatim . org-ascii-verbatim
)
96 (verse-block . org-ascii-verse-block
))
99 '(?t
"Export to Plain Text"
100 ((?A
"As ASCII buffer"
102 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
105 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
106 (?L
"As Latin1 buffer"
108 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
111 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
112 (?U
"As UTF-8 buffer"
114 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
117 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
118 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
119 (:filter-parse-tree org-ascii-filter-paragraph-spacing
120 org-ascii-filter-comment-spacing
)
121 (:filter-section . org-ascii-filter-headline-blank-lines
))
122 :options-alist
'((:ascii-charset nil nil org-ascii-charset
)))
126 ;;; User Configurable Variables
128 (defgroup org-export-ascii nil
129 "Options for exporting Org mode files to ASCII."
130 :tag
"Org Export ASCII"
133 (defcustom org-ascii-text-width
72
134 "Maximum width of exported text.
135 This number includes margin size, as set in
136 `org-ascii-global-margin'."
137 :group
'org-export-ascii
139 :package-version
'(Org .
"8.0")
142 (defcustom org-ascii-global-margin
0
143 "Width of the left margin, in number of characters."
144 :group
'org-export-ascii
146 :package-version
'(Org .
"8.0")
149 (defcustom org-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-ascii
154 :package-version
'(Org .
"8.0")
157 (defcustom org-ascii-quote-margin
6
158 "Width of margin used for quoting text, in characters.
159 This margin is applied on both sides of the text."
160 :group
'org-export-ascii
162 :package-version
'(Org .
"8.0")
165 (defcustom org-ascii-inlinetask-width
30
166 "Width of inline tasks, in number of characters.
167 This number ignores any margin."
168 :group
'org-export-ascii
170 :package-version
'(Org .
"8.0")
173 (defcustom org-ascii-headline-spacing
'(1 .
2)
174 "Number of blank lines inserted around headlines.
176 This variable can be set to a cons cell. In that case, its car
177 represents the number of blank lines present before headline
178 contents whereas its cdr reflects the number of blank lines after
181 A nil value replicates the number of blank lines found in the
182 original Org buffer at the same place."
183 :group
'org-export-ascii
185 :package-version
'(Org .
"8.0")
187 (const :tag
"Replicate original spacing" nil
)
188 (cons :tag
"Set a uniform spacing"
189 (integer :tag
"Number of blank lines before contents")
190 (integer :tag
"Number of blank lines after contents"))))
192 (defcustom org-ascii-indented-line-width
'auto
193 "Additional indentation width for the first line in a paragraph.
194 If the value is an integer, indent the first line of each
195 paragraph by this width, unless it is located at the beginning of
196 a section, in which case indentation is removed from that line.
197 If it is the symbol `auto' preserve indentation from original
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."
427 (let ((double-space-p sentence-end-double-space
))
429 (let ((fill-column text-width
)
430 (use-hard-newlines t
)
431 (sentence-end-double-space double-space-p
))
432 (insert (if (plist-get info
:preserve-breaks
)
433 (replace-regexp-in-string "\n" hard-newline s
)
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 "\\(^\\)[ \t]*\\S-" (make-string width ?\s
) 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 (string-width (or (org-ascii--checkbox parent-item info
)
538 (or (org-list-get-tag beg-item struct
)
539 (org-list-get-bullet beg-item struct
)))))))))))))
541 (defun org-ascii--build-title
542 (element info text-width
&optional underline notags toc
)
543 "Format ELEMENT title and return it.
545 ELEMENT is either an `headline' or `inlinetask' element. INFO is
546 a plist used as a communication channel. TEXT-WIDTH is an
547 integer representing the maximum length of a line.
549 When optional argument UNDERLINE is non-nil, underline title,
550 without the tags, according to `org-ascii-underline'
553 If optional argument NOTAGS is non-nil, no tags will be added to
556 When optional argument TOC is non-nil, use optional title if
557 possible. It doesn't apply to `inlinetask' elements."
558 (let* ((headlinep (eq (org-element-type element
) 'headline
))
560 ;; Numbering is specific to headlines.
561 (and headlinep
(org-export-numbered-headline-p element info
)
562 ;; All tests passed: build numbering string.
566 (org-export-get-headline-number element info
) ".")
571 (if (and toc headlinep
) (org-export-get-alt-title element info
)
572 (org-element-property :title element
))
575 (and (plist-get info
:with-todo-keywords
)
576 (let ((todo (org-element-property :todo-keyword element
)))
577 (and todo
(concat (org-export-data todo info
) " ")))))
578 (tags (and (not notags
)
579 (plist-get info
:with-tags
)
580 (let ((tag-list (org-export-get-tags element info
)))
583 (mapconcat 'identity tag-list
":"))))))
585 (and (plist-get info
:with-priority
)
586 (let ((char (org-element-property :priority element
)))
587 (and char
(format "(#%c) " char
)))))
588 (first-part (concat numbers todo priority text
)))
591 ;; Align tags, if any.
595 (max (- text-width
(1+ (string-width first-part
)))
596 (string-width tags
)))
598 ;; Maybe underline text, if ELEMENT type is `headline' and an
599 ;; underline character has been defined.
600 (when (and underline headlinep
)
602 (nth (1- (org-export-get-relative-level element info
))
603 (cdr (assq (plist-get info
:ascii-charset
)
604 org-ascii-underline
)))))
607 (make-string (/ (string-width first-part
)
608 (char-width under-char
))
611 (defun org-ascii--has-caption-p (element info
)
612 "Non-nil when ELEMENT has a caption affiliated keyword.
613 INFO is a plist used as a communication channel. This function
614 is meant to be used as a predicate for `org-export-get-ordinal'."
615 (org-element-property :caption element
))
617 (defun org-ascii--build-caption (element info
)
618 "Return caption string for ELEMENT, if applicable.
620 INFO is a plist used as a communication channel.
622 The caption string contains the sequence number of ELEMENT along
623 with its real caption. Return nil when ELEMENT has no affiliated
625 (let ((caption (org-export-get-caption element
)))
627 ;; Get sequence number of current src-block among every
628 ;; src-block with a caption.
630 (org-export-get-ordinal
631 element info nil
'org-ascii--has-caption-p
))
632 (title-fmt (org-ascii--translate
633 (case (org-element-type element
)
635 (src-block "Listing %d:"))
637 (org-ascii--fill-string
638 (concat (format title-fmt reference
)
640 (org-export-data caption info
))
641 (org-ascii--current-text-width element info
) info
)))))
643 (defun org-ascii--build-toc (info &optional n keyword
)
644 "Return a table of contents.
646 INFO is a plist used as a communication channel.
648 Optional argument N, when non-nil, is an integer specifying the
651 Optional argument KEYWORD specifies the TOC keyword, if any, from
652 which the table of contents generation has been initiated."
653 (let ((title (org-ascii--translate "Table of Contents" info
)))
656 (make-string (string-width title
)
657 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
660 (if keyword
(org-ascii--current-text-width keyword info
)
661 (- org-ascii-text-width org-ascii-global-margin
))))
664 (let* ((level (org-export-get-relative-level headline info
))
665 (indent (* (1- level
) 3)))
667 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
668 (org-ascii--build-title
669 headline info
(- text-width indent
) nil
670 (or (not (plist-get info
:with-tags
))
671 (eq (plist-get info
:with-tags
) 'not-in-toc
))
673 (org-export-collect-headlines info n
) "\n")))))
675 (defun org-ascii--list-listings (keyword info
)
676 "Return a list of listings.
678 KEYWORD is the keyword that initiated the list of listings
679 generation. INFO is a plist used as a communication channel."
680 (let ((title (org-ascii--translate "List of Listings" info
)))
683 (make-string (string-width title
)
684 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
687 (if keyword
(org-ascii--current-text-width keyword info
)
688 (- org-ascii-text-width org-ascii-global-margin
)))
689 ;; Use a counter instead of retrieving ordinal of each
694 ;; Store initial text so its length can be computed. This is
695 ;; used to properly align caption right to it in case of
696 ;; filling (like contents of a description list item).
698 (format (org-ascii--translate "Listing %d:" info
)
700 (initial-width (string-width initial-text
)))
704 (org-ascii--indent-string
705 (org-ascii--fill-string
706 ;; Use short name in priority, if available.
707 (let ((caption (or (org-export-get-caption src-block t
)
708 (org-export-get-caption src-block
))))
709 (org-export-data caption info
))
710 (- text-width initial-width
) info
)
712 (org-export-collect-listings info
) "\n")))))
714 (defun org-ascii--list-tables (keyword info
)
715 "Return a list of tables.
717 KEYWORD is the keyword that initiated the list of tables
718 generation. INFO is a plist used as a communication channel."
719 (let ((title (org-ascii--translate "List of Tables" info
)))
722 (make-string (string-width title
)
723 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
726 (if keyword
(org-ascii--current-text-width keyword info
)
727 (- org-ascii-text-width org-ascii-global-margin
)))
728 ;; Use a counter instead of retrieving ordinal of each
733 ;; Store initial text so its length can be computed. This is
734 ;; used to properly align caption right to it in case of
735 ;; filling (like contents of a description list item).
737 (format (org-ascii--translate "Table %d:" info
)
739 (initial-width (string-width initial-text
)))
743 (org-ascii--indent-string
744 (org-ascii--fill-string
745 ;; Use short name in priority, if available.
746 (let ((caption (or (org-export-get-caption table t
)
747 (org-export-get-caption table
))))
748 (org-export-data caption info
))
749 (- text-width initial-width
) info
)
751 (org-export-collect-tables info
) "\n")))))
753 (defun org-ascii--unique-links (element info
)
754 "Return a list of unique link references in ELEMENT.
755 ELEMENT is either a headline element or a section element. INFO
756 is a plist used as a communication channel."
760 ;; Return LINK if it wasn't referenced so far, or nil.
761 ;; Update SEEN links along the way.
764 ;; Normalize description in footprints.
765 (cons (org-element-property :raw-link link
)
766 (let ((contents (org-element-contents link
)))
768 (replace-regexp-in-string
771 (org-element-interpret-data contents
))))))))
772 ;; Ignore LINK if it hasn't been translated already.
773 ;; It can happen if it is located in an affiliated
774 ;; keyword that was ignored.
775 (when (and (org-string-nw-p
776 (gethash link
(plist-get info
:exported-data
)))
777 (not (member footprint seen
)))
778 (push footprint seen
) link
)))))
779 ;; If at a section, find parent headline, if any, in order to
780 ;; count links that might be in the title.
782 (if (eq (org-element-type element
) 'headline
) element
783 (or (org-export-get-parent-headline element
) element
))))
784 ;; Get all links in HEADLINE.
785 (org-element-map headline
'link
786 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
788 (defun org-ascii--describe-links (links width info
)
789 "Return a string describing a list of links.
791 LINKS is a list of link type objects, as returned by
792 `org-ascii--unique-links'. WIDTH is the text width allowed for
793 the output string. INFO is a plist used as a communication
797 (let ((type (org-element-property :type link
))
798 (anchor (let ((desc (org-element-contents link
)))
799 (if desc
(org-export-data desc info
)
800 (org-element-property :raw-link link
)))))
802 ;; Coderefs, radio links and fuzzy links are ignored.
803 ((member type
'("coderef" "radio" "fuzzy")) nil
)
804 ;; Id and custom-id links: Headlines refer to their numbering.
805 ((member type
'("custom-id" "id"))
806 (let ((dest (org-export-resolve-id-link link info
)))
808 (org-ascii--fill-string
812 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
814 (org-ascii--translate "See section %s" info
)
815 (mapconcat 'number-to-string
816 (org-export-get-headline-number dest info
) "."))))
817 width info
) "\n\n")))
818 ;; Do not add a link that cannot be resolved and doesn't have
819 ;; any description: destination is already visible in the
821 ((not (org-element-contents link
)) nil
)
824 (org-ascii--fill-string
825 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
830 (defun org-ascii--checkbox (item info
)
831 "Return checkbox string for ITEM or nil.
832 INFO is a plist used as a communication channel."
833 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
834 (case (org-element-property :checkbox item
)
835 (on (if utf8p
"☑ " "[X] "))
836 (off (if utf8p
"☐ " "[ ] "))
837 (trans (if utf8p
"☒ " "[-] ")))))
843 (defun org-ascii-template--document-title (info)
844 "Return document title, as a string.
845 INFO is a plist used as a communication channel."
846 (let* ((text-width org-ascii-text-width
)
847 ;; Links in the title will not be resolved later, so we make
848 ;; sure their path is located right after them.
849 (org-ascii-links-to-notes nil
)
850 (title (org-export-data (plist-get info
:title
) info
))
851 (author (and (plist-get info
:with-author
)
852 (let ((auth (plist-get info
:author
)))
853 (and auth
(org-export-data auth info
)))))
854 (email (and (plist-get info
:with-email
)
855 (org-export-data (plist-get info
:email
) info
)))
856 (date (and (plist-get info
:with-date
)
857 (org-export-data (org-export-get-date info
) info
))))
858 ;; There are two types of title blocks depending on the presence
859 ;; of a title to display.
860 (if (string= title
"")
861 ;; Title block without a title. DATE is positioned at the top
862 ;; right of the document, AUTHOR to the top left and EMAIL
865 ((and (org-string-nw-p date
) (org-string-nw-p author
))
868 (make-string (- text-width
(string-width date
) (string-width author
))
871 (when (org-string-nw-p email
) (concat "\n" email
))
873 ((and (org-string-nw-p date
) (org-string-nw-p email
))
876 (make-string (- text-width
(string-width date
) (string-width email
))
879 ((org-string-nw-p date
)
881 (org-ascii--justify-string date text-width
'right
)
883 ((and (org-string-nw-p author
) (org-string-nw-p email
))
884 (concat author
"\n" email
"\n\n\n"))
885 ((org-string-nw-p author
) (concat author
"\n\n\n"))
886 ((org-string-nw-p email
) (concat email
"\n\n\n")))
887 ;; Title block with a title. Document's TITLE, along with the
888 ;; AUTHOR and its EMAIL are both overlined and an underlined,
889 ;; centered. Date is just below, also centered.
890 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
891 ;; Format TITLE. It may be filled if it is too wide,
892 ;; that is wider than the two thirds of the total width.
893 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
894 (formatted-title (org-ascii--fill-string title title-len info
))
897 (min (+ (max title-len
898 (string-width (or author
""))
899 (string-width (or email
"")))
901 text-width
) (if utf8p ?━ ?_
))))
902 (org-ascii--justify-string
905 (upcase formatted-title
)
907 ((and (org-string-nw-p author
) (org-string-nw-p email
))
908 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
909 ((org-string-nw-p author
)
910 (concat (if utf8p
"\n\n\n" "\n\n") author
))
911 ((org-string-nw-p email
)
912 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
914 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
915 "\n\n\n") text-width
'center
)))))
917 (defun org-ascii-inner-template (contents info
)
918 "Return complete document string after ASCII conversion.
919 CONTENTS is the transcoded contents string. INFO is a plist
920 holding export options."
921 (org-element-normalize-string
922 (org-ascii--indent-string
924 ;; 1. Document's body.
926 ;; 2. Footnote definitions.
927 (let ((definitions (org-export-collect-footnote-definitions
928 (plist-get info
:parse-tree
) info
))
929 ;; Insert full links right inside the footnote definition
930 ;; as they have no chance to be inserted later.
931 (org-ascii-links-to-notes nil
))
935 (let ((title (org-ascii--translate "Footnotes" info
)))
940 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
942 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
945 (let ((id (format "[%s] " (car ref
))))
946 ;; Distinguish between inline definitions and
947 ;; full-fledged definitions.
949 (let ((def (nth 2 ref
)))
950 (if (eq (org-element-type def
) 'org-data
)
951 ;; Full-fledged definition: footnote ID is
952 ;; inserted inside the first parsed paragraph
953 ;; (FIRST), if any, to be sure filling will
954 ;; take it into consideration.
955 (let ((first (car (org-element-contents def
))))
956 (if (not (eq (org-element-type first
) 'paragraph
))
957 (concat id
"\n" (org-export-data def info
))
958 (push id
(nthcdr 2 first
))
959 (org-export-data def info
)))
960 ;; Fill paragraph once footnote ID is inserted
961 ;; in order to have a correct length for first
963 (org-ascii--fill-string
964 (concat id
(org-export-data def info
))
965 text-width info
))))))
966 definitions
"\n\n"))))))
967 org-ascii-global-margin
)))
969 (defun org-ascii-template (contents info
)
970 "Return complete document string after ASCII conversion.
971 CONTENTS is the transcoded contents string. INFO is a plist
972 holding export options."
974 ;; 1. Build title block.
975 (org-ascii--indent-string
976 (concat (org-ascii-template--document-title info
)
977 ;; 2. Table of contents.
978 (let ((depth (plist-get info
:with-toc
)))
981 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
983 org-ascii-global-margin
)
984 ;; 3. Document's body.
986 ;; 4. Creator. Ignore `comment' value as there are no comments in
987 ;; ASCII. Justify it to the bottom right.
988 (org-ascii--indent-string
989 (let ((creator-info (plist-get info
:with-creator
))
990 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
991 (unless (or (not creator-info
) (eq creator-info
'comment
))
994 (org-ascii--fill-string
995 (plist-get info
:creator
) text-width info
'right
))))
996 org-ascii-global-margin
)))
998 (defun org-ascii--translate (s info
)
999 "Translate string S according to specified language and charset.
1000 INFO is a plist used as a communication channel."
1001 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1002 (org-export-translate s charset info
)))
1006 ;;; Transcode Functions
1010 (defun org-ascii-bold (bold contents info
)
1011 "Transcode BOLD from Org to ASCII.
1012 CONTENTS is the text with bold markup. INFO is a plist holding
1013 contextual information."
1014 (format "*%s*" contents
))
1019 (defun org-ascii-center-block (center-block contents info
)
1020 "Transcode a CENTER-BLOCK element from Org to ASCII.
1021 CONTENTS holds the contents of the block. INFO is a plist
1022 holding contextual information."
1023 (org-ascii--justify-string
1024 contents
(org-ascii--current-text-width center-block info
) 'center
))
1029 (defun org-ascii-clock (clock contents info
)
1030 "Transcode a CLOCK object from Org to ASCII.
1031 CONTENTS is nil. INFO is a plist holding contextual
1033 (concat org-clock-string
" "
1035 (org-element-property :raw-value
1036 (org-element-property :value clock
)))
1037 (let ((time (org-element-property :duration clock
)))
1042 (org-split-string time
":")))))))
1047 (defun org-ascii-code (code contents info
)
1048 "Return a CODE object from Org to ASCII.
1049 CONTENTS is nil. INFO is a plist holding contextual
1051 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1056 (defun org-ascii-drawer (drawer contents info
)
1057 "Transcode a DRAWER element from Org to ASCII.
1058 CONTENTS holds the contents of the block. INFO is a plist
1059 holding contextual information."
1060 (let ((name (org-element-property :drawer-name drawer
))
1061 (width (org-ascii--current-text-width drawer info
)))
1062 (funcall org-ascii-format-drawer-function name contents width
)))
1067 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1068 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1069 CONTENTS holds the contents of the block. INFO is a plist
1070 holding contextual information."
1076 (defun org-ascii-entity (entity contents info
)
1077 "Transcode an ENTITY object from Org to ASCII.
1078 CONTENTS are the definition itself. INFO is a plist holding
1079 contextual information."
1080 (org-element-property
1081 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1087 (defun org-ascii-example-block (example-block contents info
)
1088 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1089 CONTENTS is nil. INFO is a plist holding contextual information."
1090 (org-ascii--box-string
1091 (org-export-format-code-default example-block info
) info
))
1096 (defun org-ascii-export-snippet (export-snippet contents info
)
1097 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1098 CONTENTS is nil. INFO is a plist holding contextual information."
1099 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1100 (org-element-property :value export-snippet
)))
1105 (defun org-ascii-export-block (export-block contents info
)
1106 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1107 CONTENTS is nil. INFO is a plist holding contextual information."
1108 (when (string= (org-element-property :type export-block
) "ASCII")
1109 (org-remove-indentation (org-element-property :value export-block
))))
1114 (defun org-ascii-fixed-width (fixed-width contents info
)
1115 "Transcode a FIXED-WIDTH element from Org to ASCII.
1116 CONTENTS is nil. INFO is a plist holding contextual information."
1117 (org-ascii--box-string
1118 (org-remove-indentation
1119 (org-element-property :value fixed-width
)) info
))
1122 ;;;; Footnote Definition
1124 ;; Footnote Definitions are ignored. They are compiled at the end of
1125 ;; the document, by `org-ascii-inner-template'.
1128 ;;;; Footnote Reference
1130 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1131 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1132 CONTENTS is nil. INFO is a plist holding contextual information."
1133 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1138 (defun org-ascii-headline (headline contents info
)
1139 "Transcode a HEADLINE element from Org to ASCII.
1140 CONTENTS holds the contents of the headline. INFO is a plist
1141 holding contextual information."
1142 ;; Don't export footnote section, which will be handled at the end
1144 (unless (org-element-property :footnote-section-p headline
)
1145 (let* ((low-level-rank (org-export-low-level-p headline info
))
1146 (width (org-ascii--current-text-width headline info
))
1147 ;; Blank lines between headline and its contents.
1148 ;; `org-ascii-headline-spacing', when set, overwrites
1149 ;; original buffer's spacing.
1152 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1153 (org-element-property :pre-blank headline
)) ?
\n))
1154 ;; Even if HEADLINE has no section, there might be some
1155 ;; links in its title that we shouldn't forget to describe.
1157 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1158 (let ((title (org-element-property :title headline
)))
1160 (org-ascii--describe-links
1161 (org-ascii--unique-links title info
) width info
))))))
1162 ;; Deep subtree: export it as a list item.
1166 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1167 org-ascii-bullets
))))
1169 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1172 (org-ascii--build-title headline info width
) "\n"
1173 ;; Contents, indented by length of bullet.
1175 (org-ascii--indent-string
1177 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1179 ;; Else: Standard headline.
1181 (org-ascii--build-title headline info width
'underline
)
1183 (concat (when (org-string-nw-p links
) links
) contents
))))))
1186 ;;;; Horizontal Rule
1188 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1189 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1190 CONTENTS is nil. INFO is a plist holding contextual
1192 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1194 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1195 (org-ascii--justify-string
1196 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1197 (string-to-number spec-width
)
1199 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1200 text-width
'center
)))
1203 ;;;; Inline Src Block
1205 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1206 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1207 CONTENTS holds the contents of the item. INFO is a plist holding
1208 contextual information."
1209 (format org-ascii-verbatim-format
1210 (org-element-property :value inline-src-block
)))
1215 (defun org-ascii-format-inlinetask-default
1216 (todo type priority name tags contents width inlinetask info
)
1217 "Format an inline task element for ASCII export.
1218 See `org-ascii-format-inlinetask-function' for a description
1220 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1221 (width (or width org-ascii-inlinetask-width
)))
1222 (org-ascii--indent-string
1224 ;; Top line, with an additional blank line if not in UTF-8.
1225 (make-string width
(if utf8p ?━ ?_
)) "\n"
1226 (unless utf8p
(concat (make-string width ?
) "\n"))
1227 ;; Add title. Fill it if wider than inlinetask.
1228 (let ((title (org-ascii--build-title inlinetask info width
)))
1229 (if (<= (string-width title
) width
) title
1230 (org-ascii--fill-string title width info
)))
1232 ;; If CONTENTS is not empty, insert it along with
1234 (when (org-string-nw-p contents
)
1235 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1237 (make-string width
(if utf8p ?━ ?_
)))
1238 ;; Flush the inlinetask to the right.
1239 (- org-ascii-text-width org-ascii-global-margin
1240 (if (not (org-export-get-parent-headline inlinetask
)) 0
1241 org-ascii-inner-margin
)
1242 (org-ascii--current-text-width inlinetask info
)))))
1244 (defun org-ascii-inlinetask (inlinetask contents info
)
1245 "Transcode an INLINETASK element from Org to ASCII.
1246 CONTENTS holds the contents of the block. INFO is a plist
1247 holding contextual information."
1248 (let ((width (org-ascii--current-text-width inlinetask info
)))
1249 (funcall org-ascii-format-inlinetask-function
1251 (and (plist-get info
:with-todo-keywords
)
1252 (let ((todo (org-element-property
1253 :todo-keyword inlinetask
)))
1254 (and todo
(org-export-data todo info
))))
1256 (org-element-property :todo-type inlinetask
)
1258 (and (plist-get info
:with-priority
)
1259 (org-element-property :priority inlinetask
))
1261 (org-export-data (org-element-property :title inlinetask
) info
)
1263 (and (plist-get info
:with-tags
)
1264 (org-element-property :tags inlinetask
))
1265 ;; contents and width
1266 contents width inlinetask info
)))
1271 (defun org-ascii-italic (italic contents info
)
1272 "Transcode italic from Org to ASCII.
1273 CONTENTS is the text with italic markup. INFO is a plist holding
1274 contextual information."
1275 (format "/%s/" contents
))
1280 (defun org-ascii-item (item contents info
)
1281 "Transcode an ITEM element from Org to ASCII.
1282 CONTENTS holds the contents of the item. INFO is a plist holding
1283 contextual information."
1284 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1285 (checkbox (org-ascii--checkbox item info
))
1286 (list-type (org-element-property :type
(org-export-get-parent item
)))
1288 ;; First parent of ITEM is always the plain-list. Get
1289 ;; `:type' property from it.
1290 (org-list-bullet-string
1294 (org-export-data (org-element-property :tag item
) info
)
1297 ;; Return correct number for ITEM, paying attention to
1299 (let* ((struct (org-element-property :structure item
))
1300 (bul (org-element-property :bullet item
))
1301 (num (number-to-string
1302 (car (last (org-list-get-item-number
1303 (org-element-property :begin item
)
1305 (org-list-prevs-alist struct
)
1306 (org-list-parents-alist struct
)))))))
1307 (replace-regexp-in-string "[0-9]+" num bul
)))
1308 (t (let ((bul (org-element-property :bullet item
)))
1309 ;; Change bullets into more visible form if UTF-8 is active.
1311 (replace-regexp-in-string
1313 (replace-regexp-in-string
1315 (replace-regexp-in-string "*" "‣" bul
))))))))))
1318 (unless (eq list-type
'descriptive
) checkbox
)
1319 ;; Contents: Pay attention to indentation. Note: check-boxes are
1320 ;; already taken care of at the paragraph level so they don't
1321 ;; interfere with indentation.
1322 (let ((contents (org-ascii--indent-string contents
(string-width bullet
))))
1323 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1325 (concat "\n" contents
))))))
1330 (defun org-ascii-keyword (keyword contents info
)
1331 "Transcode a KEYWORD element from Org to ASCII.
1332 CONTENTS is nil. INFO is a plist holding contextual
1334 (let ((key (org-element-property :key keyword
))
1335 (value (org-element-property :value keyword
)))
1337 ((string= key
"ASCII") value
)
1338 ((string= key
"TOC")
1339 (let ((value (downcase value
)))
1341 ((string-match "\\<headlines\\>" value
)
1342 (let ((depth (or (and (string-match "[0-9]+" value
)
1343 (string-to-number (match-string 0 value
)))
1344 (plist-get info
:with-toc
))))
1345 (org-ascii--build-toc
1346 info
(and (wholenump depth
) depth
) keyword
)))
1347 ((string= "tables" value
)
1348 (org-ascii--list-tables keyword info
))
1349 ((string= "listings" value
)
1350 (org-ascii--list-listings keyword info
))))))))
1353 ;;;; Latex Environment
1355 (defun org-ascii-latex-environment (latex-environment contents info
)
1356 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1357 CONTENTS is nil. INFO is a plist holding contextual
1359 (when (plist-get info
:with-latex
)
1360 (org-remove-indentation (org-element-property :value latex-environment
))))
1365 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1366 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1367 CONTENTS is nil. INFO is a plist holding contextual
1369 (when (plist-get info
:with-latex
)
1370 (org-element-property :value latex-fragment
)))
1375 (defun org-ascii-line-break (line-break contents info
)
1376 "Transcode a LINE-BREAK object from Org to ASCII.
1377 CONTENTS is nil. INFO is a plist holding contextual
1378 information." hard-newline
)
1383 (defun org-ascii-link (link desc info
)
1384 "Transcode a LINK object from Org to ASCII.
1386 DESC is the description part of the link, or the empty string.
1387 INFO is a plist holding contextual information."
1388 (let ((raw-link (org-element-property :raw-link link
))
1389 (type (org-element-property :type link
)))
1391 ((string= type
"coderef")
1392 (let ((ref (org-element-property :path link
)))
1393 (format (org-export-get-coderef-format ref desc
)
1394 (org-export-resolve-coderef ref info
))))
1395 ;; Do not apply a special syntax on radio links. Though, use
1396 ;; transcoded target's contents as output.
1397 ((string= type
"radio") desc
)
1398 ;; Do not apply a special syntax on fuzzy links pointing to
1400 ((string= type
"fuzzy")
1401 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1402 (if (org-string-nw-p desc
) desc
1405 (org-export-get-ordinal
1406 destination info nil
'org-ascii--has-caption-p
)))
1408 (if (atom number
) (number-to-string number
)
1409 (mapconcat 'number-to-string number
"."))))))))
1411 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1413 (format "[%s]" desc
)
1414 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1419 (defun org-ascii-paragraph (paragraph contents info
)
1420 "Transcode a PARAGRAPH element from Org to ASCII.
1421 CONTENTS is the contents of the paragraph, as a string. INFO is
1422 the plist used as a communication channel."
1423 (org-ascii--fill-string
1424 (if (not (wholenump org-ascii-indented-line-width
)) contents
1426 ;; Do not indent first paragraph in a section.
1427 (unless (and (not (org-export-get-previous-element paragraph info
))
1428 (eq (org-element-type (org-export-get-parent paragraph
))
1430 (make-string org-ascii-indented-line-width ?\s
))
1431 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))
1432 (org-ascii--current-text-width paragraph info
) info
))
1437 (defun org-ascii-plain-list (plain-list contents info
)
1438 "Transcode a PLAIN-LIST element from Org to ASCII.
1439 CONTENTS is the contents of the list. INFO is a plist holding
1440 contextual information."
1446 (defun org-ascii-plain-text (text info
)
1447 "Transcode a TEXT string from Org to ASCII.
1448 INFO is a plist used as a communication channel."
1449 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1450 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1451 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1452 (if (not (plist-get info
:with-special-strings
)) text
1453 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1454 (if (not utf8p
) text
1455 ;; Usual replacements in utf-8 with proper option set.
1456 (replace-regexp-in-string
1458 (replace-regexp-in-string
1460 (replace-regexp-in-string "---" "—" text
)))))))
1465 (defun org-ascii-planning (planning contents info
)
1466 "Transcode a PLANNING element from Org to ASCII.
1467 CONTENTS is nil. INFO is a plist used as a communication
1472 (list (let ((closed (org-element-property :closed planning
)))
1474 (concat org-closed-string
" "
1476 (org-element-property :raw-value closed
)))))
1477 (let ((deadline (org-element-property :deadline planning
)))
1479 (concat org-deadline-string
" "
1481 (org-element-property :raw-value deadline
)))))
1482 (let ((scheduled (org-element-property :scheduled planning
)))
1484 (concat org-scheduled-string
" "
1486 (org-element-property :raw-value scheduled
)))))))
1492 (defun org-ascii-quote-block (quote-block contents info
)
1493 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1494 CONTENTS holds the contents of the block. INFO is a plist
1495 holding contextual information."
1496 (org-ascii--indent-string contents org-ascii-quote-margin
))
1501 (defun org-ascii-quote-section (quote-section contents info
)
1502 "Transcode a QUOTE-SECTION element from Org to ASCII.
1503 CONTENTS is nil. INFO is a plist holding contextual information."
1504 (let ((width (org-ascii--current-text-width quote-section info
))
1507 (org-remove-indentation (org-element-property :value quote-section
))
1509 (org-ascii--indent-string
1511 (+ org-ascii-quote-margin
1512 ;; Don't apply inner margin if parent headline is low level.
1513 (let ((headline (org-export-get-parent-headline quote-section
)))
1514 (if (org-export-low-level-p headline info
) 0
1515 org-ascii-inner-margin
))))))
1520 (defun org-ascii-radio-target (radio-target contents info
)
1521 "Transcode a RADIO-TARGET object from Org to ASCII.
1522 CONTENTS is the contents of the target. INFO is a plist holding
1523 contextual information."
1529 (defun org-ascii-section (section contents info
)
1530 "Transcode a SECTION element from Org to ASCII.
1531 CONTENTS is the contents of the section. INFO is a plist holding
1532 contextual information."
1533 (org-ascii--indent-string
1536 (when org-ascii-links-to-notes
1537 ;; Add list of links at the end of SECTION.
1538 (let ((links (org-ascii--describe-links
1539 (org-ascii--unique-links section info
)
1540 (org-ascii--current-text-width section info
) info
)))
1541 ;; Separate list of links and section contents.
1542 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1543 ;; Do not apply inner margin if parent headline is low level.
1544 (let ((headline (org-export-get-parent-headline section
)))
1545 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1546 org-ascii-inner-margin
))))
1551 (defun org-ascii-special-block (special-block contents info
)
1552 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1553 CONTENTS holds the contents of the block. INFO is a plist
1554 holding contextual information."
1560 (defun org-ascii-src-block (src-block contents info
)
1561 "Transcode a SRC-BLOCK element from Org to ASCII.
1562 CONTENTS holds the contents of the item. INFO is a plist holding
1563 contextual information."
1564 (let ((caption (org-ascii--build-caption src-block info
))
1565 (code (org-export-format-code-default src-block info
)))
1566 (if (equal code
"") ""
1568 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1569 (org-ascii--box-string code info
)
1570 (when (and caption
(not org-ascii-caption-above
))
1571 (concat "\n" caption
))))))
1574 ;;;; Statistics Cookie
1576 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1577 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1578 CONTENTS is nil. INFO is a plist holding contextual information."
1579 (org-element-property :value statistics-cookie
))
1584 (defun org-ascii-subscript (subscript contents info
)
1585 "Transcode a SUBSCRIPT object from Org to ASCII.
1586 CONTENTS is the contents of the object. INFO is a plist holding
1587 contextual information."
1588 (if (org-element-property :use-brackets-p subscript
)
1589 (format "_{%s}" contents
)
1590 (format "_%s" contents
)))
1595 (defun org-ascii-superscript (superscript contents info
)
1596 "Transcode a SUPERSCRIPT object from Org to ASCII.
1597 CONTENTS is the contents of the object. INFO is a plist holding
1598 contextual information."
1599 (if (org-element-property :use-brackets-p superscript
)
1600 (format "^{%s}" contents
)
1601 (format "^%s" contents
)))
1606 (defun org-ascii-strike-through (strike-through contents info
)
1607 "Transcode STRIKE-THROUGH from Org to ASCII.
1608 CONTENTS is text with strike-through markup. INFO is a plist
1609 holding contextual information."
1610 (format "+%s+" contents
))
1615 (defun org-ascii-table (table contents info
)
1616 "Transcode a TABLE element from Org to ASCII.
1617 CONTENTS is the contents of the table. INFO is a plist holding
1618 contextual information."
1619 (let ((caption (org-ascii--build-caption table info
)))
1621 ;; Possibly add a caption string above.
1622 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1623 ;; Insert table. Note: "table.el" tables are left unmodified.
1624 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1625 ((and org-ascii-table-use-ascii-art
1626 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1627 (require 'ascii-art-to-unicode nil t
))
1629 (insert (org-remove-indentation
1630 (org-element-property :value table
)))
1631 (goto-char (point-min))
1633 (goto-char (point-max))
1634 (skip-chars-backward " \r\t\n")
1635 (buffer-substring (point-min) (point))))
1636 (t (org-remove-indentation (org-element-property :value table
))))
1637 ;; Possible add a caption string below.
1638 (and (not org-ascii-caption-above
) caption
))))
1643 (defun org-ascii--table-cell-width (table-cell info
)
1644 "Return width of TABLE-CELL.
1646 INFO is a plist used as a communication channel.
1648 Width of a cell is determined either by a width cookie in the
1649 same column as the cell, or by the maximum cell's length in that
1652 When `org-ascii-table-widen-columns' is non-nil, width cookies
1654 (let* ((row (org-export-get-parent table-cell
))
1655 (table (org-export-get-parent row
))
1656 (col (let ((cells (org-element-contents row
)))
1657 (- (length cells
) (length (memq table-cell cells
)))))
1659 (or (plist-get info
:ascii-table-cell-width-cache
)
1660 (plist-get (setq info
1661 (plist-put info
:ascii-table-cell-width-cache
1662 (make-hash-table :test
'equal
)))
1663 :ascii-table-cell-width-cache
)))
1664 (key (cons table col
)))
1665 (or (gethash key cache
)
1668 (let ((cookie-width (org-export-table-cell-width table-cell info
)))
1669 (or (and (not org-ascii-table-widen-columns
) cookie-width
)
1670 (let ((contents-width
1671 (let ((max-width 0))
1672 (org-element-map table
'table-row
1677 (org-element-contents
1678 (elt (org-element-contents row
) col
))
1683 (cond ((not cookie-width
) contents-width
)
1684 (org-ascii-table-widen-columns
1685 (max cookie-width contents-width
))
1686 (t cookie-width
)))))
1689 (defun org-ascii-table-cell (table-cell contents info
)
1690 "Transcode a TABLE-CELL object from Org to ASCII.
1691 CONTENTS is the cell contents. INFO is a plist used as
1692 a communication channel."
1693 ;; Determine column width. When `org-ascii-table-widen-columns'
1694 ;; is nil and some width cookie has set it, use that value.
1695 ;; Otherwise, compute the maximum width among transcoded data of
1696 ;; each cell in the column.
1697 (let ((width (org-ascii--table-cell-width table-cell info
)))
1698 ;; When contents are too large, truncate them.
1699 (unless (or org-ascii-table-widen-columns
1700 (<= (string-width (or contents
"")) width
))
1701 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1702 ;; Align contents correctly within the cell.
1703 (let* ((indent-tabs-mode nil
)
1706 (org-ascii--justify-string
1708 (org-export-table-cell-alignment table-cell info
)))))
1711 (make-string (- width
(string-width (or data
""))) ?\s
))))
1713 (concat (format " %s " contents
)
1714 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1715 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1720 (defun org-ascii-table-row (table-row contents info
)
1721 "Transcode a TABLE-ROW element from Org to ASCII.
1722 CONTENTS is the row contents. INFO is a plist used as
1723 a communication channel."
1724 (when (eq (org-element-property :type table-row
) 'standard
)
1727 (lambda (lcorner horiz vert rcorner
)
1731 (org-element-map table-row
'table-cell
1733 (let ((width (org-ascii--table-cell-width cell info
))
1734 (borders (org-export-table-cell-borders cell info
)))
1736 ;; In order to know if CELL starts the row, do
1737 ;; not compare it with the first cell in the
1738 ;; row as there might be a special column.
1739 ;; Instead, compare it with first exportable
1740 ;; cell, obtained with `org-element-map'.
1741 (when (and (memq 'left borders
)
1742 (eq (org-element-map table-row
'table-cell
1746 (make-string (+ 2 width
) (string-to-char horiz
))
1748 ((not (memq 'right borders
)) nil
)
1749 ((eq (car (last (org-element-contents table-row
))) cell
)
1753 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1754 (borders (org-export-table-cell-borders
1755 (org-element-map table-row
'table-cell
'identity info t
)
1758 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1759 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1760 (funcall build-hline
"+" "-" "+" "+")))
1761 ((memq 'above borders
)
1762 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1763 (funcall build-hline
"+" "-" "+" "+"))))
1764 (when (memq 'left borders
) (if utf8p
"│" "|"))
1766 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1767 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1768 (funcall build-hline
"+" "-" "+" "+")))))))
1773 (defun org-ascii-timestamp (timestamp contents info
)
1774 "Transcode a TIMESTAMP object from Org to ASCII.
1775 CONTENTS is nil. INFO is a plist holding contextual information."
1776 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1781 (defun org-ascii-underline (underline contents info
)
1782 "Transcode UNDERLINE from Org to ASCII.
1783 CONTENTS is the text with underline markup. INFO is a plist
1784 holding contextual information."
1785 (format "_%s_" contents
))
1790 (defun org-ascii-verbatim (verbatim contents info
)
1791 "Return a VERBATIM object from Org to ASCII.
1792 CONTENTS is nil. INFO is a plist holding contextual information."
1793 (format org-ascii-verbatim-format
1794 (org-element-property :value verbatim
)))
1799 (defun org-ascii-verse-block (verse-block contents info
)
1800 "Transcode a VERSE-BLOCK element from Org to ASCII.
1801 CONTENTS is verse block contents. INFO is a plist holding
1802 contextual information."
1803 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1804 (org-ascii--indent-string
1805 (org-ascii--justify-string contents verse-width
'left
)
1806 org-ascii-quote-margin
)))
1812 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1813 "Filter controlling number of blank lines after a headline.
1815 HEADLINE is a string representing a transcoded headline.
1816 BACK-END is symbol specifying back-end used for export. INFO is
1817 plist containing the communication channel.
1819 This function only applies to `ascii' back-end. See
1820 `org-ascii-headline-spacing' for information."
1821 (if (not org-ascii-headline-spacing
) headline
1822 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1823 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1825 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1826 "Filter controlling number of blank lines between paragraphs.
1828 TREE is the parse tree. BACK-END is the symbol specifying
1829 back-end used for export. INFO is a plist used as
1830 a communication channel.
1832 See `org-ascii-paragraph-spacing' for information."
1833 (when (wholenump org-ascii-paragraph-spacing
)
1834 (org-element-map tree
'paragraph
1836 (when (eq (org-element-type (org-export-get-next-element p info
))
1838 (org-element-put-property
1839 p
:post-blank org-ascii-paragraph-spacing
)))))
1842 (defun org-ascii-filter-comment-spacing (tree backend info
)
1843 "Filter removing blank lines between comments.
1844 TREE is the parse tree. BACK-END is the symbol specifying
1845 back-end used for export. INFO is a plist used as
1846 a communication channel."
1847 (org-element-map tree
'(comment comment-block
)
1849 (when (memq (org-element-type (org-export-get-next-element c info
))
1850 '(comment comment-block
))
1851 (org-element-put-property c
:post-blank
0))))
1856 ;;; End-user functions
1859 (defun org-ascii-export-as-ascii
1860 (&optional async subtreep visible-only body-only ext-plist
)
1861 "Export current buffer to a text buffer.
1863 If narrowing is active in the current buffer, only export its
1866 If a region is active, export that region.
1868 A non-nil optional argument ASYNC means the process should happen
1869 asynchronously. The resulting buffer should be accessible
1870 through the `org-export-stack' interface.
1872 When optional argument SUBTREEP is non-nil, export the sub-tree
1873 at point, extracting information from the headline properties
1876 When optional argument VISIBLE-ONLY is non-nil, don't export
1877 contents of hidden elements.
1879 When optional argument BODY-ONLY is non-nil, strip title and
1880 table of contents from output.
1882 EXT-PLIST, when provided, is a property list with external
1883 parameters overriding Org default settings, but still inferior to
1884 file-local settings.
1886 Export is done in a buffer named \"*Org ASCII Export*\", which
1887 will be displayed when `org-export-show-temporary-export-buffer'
1890 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
1891 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
1894 (defun org-ascii-export-to-ascii
1895 (&optional async subtreep visible-only body-only ext-plist
)
1896 "Export current buffer to a text file.
1898 If narrowing is active in the current buffer, only export its
1901 If a region is active, export that region.
1903 A non-nil optional argument ASYNC means the process should happen
1904 asynchronously. The resulting file should be accessible through
1905 the `org-export-stack' interface.
1907 When optional argument SUBTREEP is non-nil, export the sub-tree
1908 at point, extracting information from the headline properties
1911 When optional argument VISIBLE-ONLY is non-nil, don't export
1912 contents of hidden elements.
1914 When optional argument BODY-ONLY is non-nil, strip title and
1915 table of contents from output.
1917 EXT-PLIST, when provided, is a property list with external
1918 parameters overriding Org default settings, but still inferior to
1919 file-local settings.
1921 Return output file's name."
1923 (let ((file (org-export-output-file-name ".txt" subtreep
)))
1924 (org-export-to-file 'ascii file
1925 async subtreep visible-only body-only ext-plist
)))
1928 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1929 "Publish an Org file to ASCII.
1931 FILENAME is the filename of the Org file to be published. PLIST
1932 is the property list for the given project. PUB-DIR is the
1933 publishing directory.
1935 Return output file name."
1937 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1940 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1941 "Publish an Org file to Latin-1.
1943 FILENAME is the filename of the Org file to be published. PLIST
1944 is the property list for the given project. PUB-DIR is the
1945 publishing directory.
1947 Return output file name."
1949 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1952 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1953 "Publish an org file to UTF-8.
1955 FILENAME is the filename of the Org file to be published. PLIST
1956 is the property list for the given project. PUB-DIR is the
1957 publishing directory.
1959 Return output file name."
1961 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1967 ;; generated-autoload-file: "org-loaddefs.el"
1971 ;;; ox-ascii.el ends here