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 (radio-target . org-ascii-radio-target
)
82 (section . org-ascii-section
)
83 (special-block . org-ascii-special-block
)
84 (src-block . org-ascii-src-block
)
85 (statistics-cookie . org-ascii-statistics-cookie
)
86 (strike-through . org-ascii-strike-through
)
87 (subscript . org-ascii-subscript
)
88 (superscript . org-ascii-superscript
)
89 (table . org-ascii-table
)
90 (table-cell . org-ascii-table-cell
)
91 (table-row . org-ascii-table-row
)
92 (target . org-ascii-target
)
93 (template . org-ascii-template
)
94 (timestamp . org-ascii-timestamp
)
95 (underline . org-ascii-underline
)
96 (verbatim . org-ascii-verbatim
)
97 (verse-block . org-ascii-verse-block
))
100 '(?t
"Export to Plain Text"
101 ((?A
"As ASCII buffer"
103 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
106 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
107 (?L
"As Latin1 buffer"
109 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
112 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
113 (?U
"As UTF-8 buffer"
115 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
118 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
119 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
120 (:filter-parse-tree org-ascii-filter-paragraph-spacing
121 org-ascii-filter-comment-spacing
)
122 (:filter-section . org-ascii-filter-headline-blank-lines
))
123 :options-alist
'((:ascii-charset nil nil org-ascii-charset
)))
127 ;;; User Configurable Variables
129 (defgroup org-export-ascii nil
130 "Options for exporting Org mode files to ASCII."
131 :tag
"Org Export ASCII"
134 (defcustom org-ascii-text-width
72
135 "Maximum width of exported text.
136 This number includes margin size, as set in
137 `org-ascii-global-margin'."
138 :group
'org-export-ascii
140 :package-version
'(Org .
"8.0")
143 (defcustom org-ascii-global-margin
0
144 "Width of the left margin, in number of characters."
145 :group
'org-export-ascii
147 :package-version
'(Org .
"8.0")
150 (defcustom org-ascii-inner-margin
2
151 "Width of the inner margin, in number of characters.
152 Inner margin is applied between each headline."
153 :group
'org-export-ascii
155 :package-version
'(Org .
"8.0")
158 (defcustom org-ascii-quote-margin
6
159 "Width of margin used for quoting text, in characters.
160 This margin is applied on both sides of the text."
161 :group
'org-export-ascii
163 :package-version
'(Org .
"8.0")
166 (defcustom org-ascii-inlinetask-width
30
167 "Width of inline tasks, in number of characters.
168 This number ignores any margin."
169 :group
'org-export-ascii
171 :package-version
'(Org .
"8.0")
174 (defcustom org-ascii-headline-spacing
'(1 .
2)
175 "Number of blank lines inserted around headlines.
177 This variable can be set to a cons cell. In that case, its car
178 represents the number of blank lines present before headline
179 contents whereas its cdr reflects the number of blank lines after
182 A nil value replicates the number of blank lines found in the
183 original Org buffer at the same place."
184 :group
'org-export-ascii
186 :package-version
'(Org .
"8.0")
188 (const :tag
"Replicate original spacing" nil
)
189 (cons :tag
"Set an uniform spacing"
190 (integer :tag
"Number of blank lines before contents")
191 (integer :tag
"Number of blank lines after contents"))))
193 (defcustom org-ascii-indented-line-width
'auto
194 "Additional indentation width for the first line in a paragraph.
195 If the value is an integer, indent the first line of each
196 paragraph by this width, unless it is located at the beginning of
197 a section, in which case indentation is removed from that line.
198 If it is the symbol `auto' preserve indentation from original
200 :group
'org-export-ascii
202 :package-version
'(Org .
"8.0")
204 (integer :tag
"Number of white spaces characters")
205 (const :tag
"Preserve original width" auto
)))
207 (defcustom org-ascii-paragraph-spacing
'auto
208 "Number of white lines between paragraphs.
209 If the value is an integer, add this number of blank lines
210 between contiguous paragraphs. If is it the symbol `auto', keep
211 the same number of blank lines as in the original document."
212 :group
'org-export-ascii
214 :package-version
'(Org .
"8.0")
216 (integer :tag
"Number of blank lines")
217 (const :tag
"Preserve original spacing" auto
)))
219 (defcustom org-ascii-charset
'ascii
220 "The charset allowed to represent various elements and objects.
222 `ascii' Only use plain ASCII characters
223 `latin1' Include Latin-1 characters
224 `utf-8' Use all UTF-8 characters"
225 :group
'org-export-ascii
227 :package-version
'(Org .
"8.0")
229 (const :tag
"ASCII" ascii
)
230 (const :tag
"Latin-1" latin1
)
231 (const :tag
"UTF-8" utf-8
)))
233 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
235 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
236 "Characters for underlining headings in ASCII export.
238 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
239 and whose value is a list of characters.
241 For each supported charset, this variable associates a sequence
242 of underline characters. In a sequence, the characters will be
243 used in order for headlines level 1, 2, ... If no character is
244 available for a given level, the headline won't be underlined."
245 :group
'org-export-ascii
247 :package-version
'(Org .
"8.0")
249 (cons :tag
"Underline characters sequence"
250 (const :tag
"ASCII charset" ascii
)
252 (cons :tag
"Underline characters sequence"
253 (const :tag
"Latin-1 charset" latin1
)
255 (cons :tag
"Underline characters sequence"
256 (const :tag
"UTF-8 charset" utf-8
)
257 (repeat character
))))
259 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
262 "Bullet characters for headlines converted to lists in ASCII export.
264 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
265 and whose value is a list of characters.
267 The first character is used for the first level considered as low
268 level, and so on. If there are more levels than characters given
269 here, the list will be repeated.
271 Note that this variable doesn't affect plain lists
273 :group
'org-export-ascii
275 :package-version
'(Org .
"8.0")
277 (cons :tag
"Bullet characters for low level headlines"
278 (const :tag
"ASCII charset" ascii
)
280 (cons :tag
"Bullet characters for low level headlines"
281 (const :tag
"Latin-1 charset" latin1
)
283 (cons :tag
"Bullet characters for low level headlines"
284 (const :tag
"UTF-8 charset" utf-8
)
285 (repeat character
))))
287 (defcustom org-ascii-links-to-notes t
288 "Non-nil means convert links to notes before the next headline.
289 When nil, the link will be exported in place. If the line
290 becomes long in this way, it will be wrapped."
291 :group
'org-export-ascii
293 :package-version
'(Org .
"8.0")
296 (defcustom org-ascii-table-keep-all-vertical-lines nil
297 "Non-nil means keep all vertical lines in ASCII tables.
298 When nil, vertical lines will be removed except for those needed
299 for column grouping."
300 :group
'org-export-ascii
302 :package-version
'(Org .
"8.0")
305 (defcustom org-ascii-table-widen-columns t
306 "Non-nil means widen narrowed columns for export.
307 When nil, narrowed columns will look in ASCII export just like in
308 Org mode, i.e. with \"=>\" as ellipsis."
309 :group
'org-export-ascii
311 :package-version
'(Org .
"8.0")
314 (defcustom org-ascii-table-use-ascii-art nil
315 "Non-nil means table.el tables are turned into ascii-art.
317 It only makes sense when export charset is `utf-8'. It is nil by
318 default since it requires ascii-art-to-unicode.el package. You
319 can download it here:
321 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
322 :group
'org-export-ascii
324 :package-version
'(Org .
"8.0")
327 (defcustom org-ascii-caption-above nil
328 "When non-nil, place caption string before the element.
329 Otherwise, place it right after it."
330 :group
'org-export-ascii
332 :package-version
'(Org .
"8.0")
335 (defcustom org-ascii-verbatim-format
"`%s'"
336 "Format string used for verbatim text and inline code."
337 :group
'org-export-ascii
339 :package-version
'(Org .
"8.0")
342 (defcustom org-ascii-format-drawer-function
343 (lambda (name contents width
) contents
)
344 "Function called to format a drawer in ASCII.
346 The function must accept three parameters:
347 NAME the drawer name, like \"LOGBOOK\"
348 CONTENTS the contents of the drawer.
349 WIDTH the text width within the drawer.
351 The function should return either the string to be exported or
352 nil to ignore the drawer.
354 The default value simply returns the value of CONTENTS."
355 :group
'org-export-ascii
357 :package-version
'(Org .
"8.0")
360 (defcustom org-ascii-format-inlinetask-function
361 'org-ascii-format-inlinetask-default
362 "Function called to format an inlinetask in ASCII.
364 The function must accept nine parameters:
365 TODO the todo keyword, as a string
366 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
367 PRIORITY the inlinetask priority, as a string
368 NAME the inlinetask name, as a string.
369 TAGS the inlinetask tags, as a list of strings.
370 CONTENTS the contents of the inlinetask, as a string.
371 WIDTH the width of the inlinetask, as a number.
372 INLINETASK the inlinetask itself.
373 INFO the info channel.
375 The function should return either the string to be exported or
376 nil to ignore the inline task."
377 :group
'org-export-ascii
379 :package-version
'(Org .
"8.3")
384 ;;; Internal Functions
386 ;; Internal functions fall into three categories.
388 ;; The first one is about text formatting. The core function is
389 ;; `org-ascii--current-text-width', which determines the current
390 ;; text width allowed to a given element. In other words, it helps
391 ;; keeping each line width within maximum text width defined in
392 ;; `org-ascii-text-width'. Once this information is known,
393 ;; `org-ascii--fill-string', `org-ascii--justify-string',
394 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
395 ;; operate on a given output string.
397 ;; The second category contains functions handling elements listings,
398 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
399 ;; returns a complete table of contents, `org-ascii--list-listings'
400 ;; returns a list of referenceable src-block elements, and
401 ;; `org-ascii--list-tables' does the same for table elements.
403 ;; The third category includes general helper functions.
404 ;; `org-ascii--build-title' creates the title for a given headline
405 ;; or inlinetask element. `org-ascii--build-caption' returns the
406 ;; caption string associated to a table or a src-block.
407 ;; `org-ascii--describe-links' creates notes about links for
408 ;; insertion at the end of a section. It uses
409 ;; `org-ascii--unique-links' to get the list of links to describe.
410 ;; Eventually, `org-ascii--translate' translates a string according
411 ;; to language and charset specification.
414 (defun org-ascii--fill-string (s text-width info
&optional justify
)
415 "Fill a string with specified text-width and return it.
417 S is the string being filled. TEXT-WIDTH is an integer
418 specifying maximum length of a line. INFO is the plist used as
419 a communication channel.
421 Optional argument JUSTIFY can specify any type of justification
422 among `left', `center', `right' or `full'. A nil value is
423 equivalent to `left'. For a justification that doesn't also fill
424 string, see `org-ascii--justify-string'.
426 Return nil if S isn't a string."
427 ;; Don't fill paragraph when break should be preserved.
428 (cond ((not (stringp s
)) nil
)
429 ((plist-get info
:preserve-breaks
) s
)
430 (t (let ((double-space-p sentence-end-double-space
))
432 (let ((fill-column text-width
)
433 (use-hard-newlines t
)
434 (sentence-end-double-space double-space-p
))
436 (fill-region (point-min) (point-max) justify
))
439 (defun org-ascii--justify-string (s text-width how
)
441 TEXT-WIDTH is an integer specifying maximum length of a line.
442 HOW determines the type of justification: it can be `left',
443 `right', `full' or `center'."
446 (goto-char (point-min))
447 (let ((fill-column text-width
)
448 ;; Disable `adaptive-fill-mode' so it doesn't prevent
449 ;; filling lines matching `adaptive-fill-regexp'.
450 (adaptive-fill-mode nil
))
451 (while (< (point) (point-max))
452 (justify-current-line how
)
456 (defun org-ascii--indent-string (s width
)
457 "Indent string S by WIDTH white spaces.
458 Empty lines are not indented."
460 (replace-regexp-in-string
461 "\\(^\\)[ \t]*\\S-" (make-string width ?\s
) s nil nil
1)))
463 (defun org-ascii--box-string (s info
)
464 "Return string S with a partial box to its left.
465 INFO is a plist used as a communication channel."
466 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
467 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
468 (replace-regexp-in-string
469 "^" (if utf8p
"│ " "| ")
470 ;; Remove last newline character.
471 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
473 (defun org-ascii--current-text-width (element info
)
474 "Return maximum text width for ELEMENT's contents.
475 INFO is a plist used as a communication channel."
476 (case (org-element-type element
)
477 ;; Elements with an absolute width: `headline' and `inlinetask'.
478 (inlinetask org-ascii-inlinetask-width
)
480 (- org-ascii-text-width
481 (let ((low-level-rank (org-export-low-level-p element info
)))
482 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
483 ;; Elements with a relative width: store maximum text width in
486 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
487 ;; Total width is determined by the presence, or not, of an
488 ;; inline task among ELEMENT parents.
490 (if (loop for parent in genealogy
491 thereis
(eq (org-element-type parent
) 'inlinetask
))
492 org-ascii-inlinetask-width
493 ;; No inlinetask: Remove global margin from text width.
494 (- org-ascii-text-width
495 org-ascii-global-margin
496 (let ((parent (org-export-get-parent-headline element
)))
497 ;; Inner margin doesn't apply to text before first
500 (let ((low-level-rank
501 (org-export-low-level-p parent info
)))
502 ;; Inner margin doesn't apply to contents of
503 ;; low level headlines, since they've got their
504 ;; own indentation mechanism.
505 (if low-level-rank
(* low-level-rank
2)
506 org-ascii-inner-margin
))))))))
508 ;; Each `quote-block' and `verse-block' above narrows text
509 ;; width by twice the standard margin size.
510 (+ (* (loop for parent in genealogy
511 when
(memq (org-element-type parent
)
512 '(quote-block verse-block
))
514 2 org-ascii-quote-margin
)
515 ;; Text width within a plain-list is restricted by
516 ;; indentation of current item. If that's the case,
517 ;; compute it with the help of `:structure' property from
518 ;; parent item, if any.
520 (if (eq (org-element-type element
) 'item
) element
521 (loop for parent in genealogy
522 when
(eq (org-element-type parent
) 'item
)
524 (if (not parent-item
) 0
525 ;; Compute indentation offset of the current item,
526 ;; that is the sum of the difference between its
527 ;; indentation and the indentation of the top item in
528 ;; the list and current item bullet's length. Also
529 ;; remove checkbox length, and tag length (for
530 ;; description lists) or bullet length.
531 (let ((struct (org-element-property :structure parent-item
))
532 (beg-item (org-element-property :begin parent-item
)))
533 (+ (- (org-list-get-ind beg-item struct
)
535 (org-list-get-top-point struct
) struct
))
536 (string-width (or (org-ascii--checkbox parent-item info
)
539 (or (org-list-get-tag beg-item struct
)
540 (org-list-get-bullet beg-item struct
)))))))))))))
542 (defun org-ascii--build-title
543 (element info text-width
&optional underline notags toc
)
544 "Format ELEMENT title and return it.
546 ELEMENT is either an `headline' or `inlinetask' element. INFO is
547 a plist used as a communication channel. TEXT-WIDTH is an
548 integer representing the maximum length of a line.
550 When optional argument UNDERLINE is non-nil, underline title,
551 without the tags, according to `org-ascii-underline'
554 If optional argument NOTAGS is non-nil, no tags will be added to
557 When optional argument TOC is non-nil, use optional title if
558 possible. It doesn't apply to `inlinetask' elements."
559 (let* ((headlinep (eq (org-element-type element
) 'headline
))
561 ;; Numbering is specific to headlines.
562 (and headlinep
(org-export-numbered-headline-p element info
)
563 ;; All tests passed: build numbering string.
567 (org-export-get-headline-number element info
) ".")
572 (if (and toc headlinep
) (org-export-get-alt-title element info
)
573 (org-element-property :title element
))
576 (and (plist-get info
:with-todo-keywords
)
577 (let ((todo (org-element-property :todo-keyword element
)))
578 (and todo
(concat (org-export-data todo info
) " ")))))
579 (tags (and (not notags
)
580 (plist-get info
:with-tags
)
581 (let ((tag-list (org-export-get-tags element info
)))
584 (mapconcat 'identity tag-list
":"))))))
586 (and (plist-get info
:with-priority
)
587 (let ((char (org-element-property :priority element
)))
588 (and char
(format "(#%c) " char
)))))
589 (first-part (concat numbers todo priority text
)))
592 ;; Align tags, if any.
596 (max (- text-width
(1+ (string-width first-part
)))
597 (string-width tags
)))
599 ;; Maybe underline text, if ELEMENT type is `headline' and an
600 ;; underline character has been defined.
601 (when (and underline headlinep
)
603 (nth (1- (org-export-get-relative-level element info
))
604 (cdr (assq (plist-get info
:ascii-charset
)
605 org-ascii-underline
)))))
608 (make-string (/ (string-width first-part
)
609 (char-width under-char
))
612 (defun org-ascii--has-caption-p (element info
)
613 "Non-nil when ELEMENT has a caption affiliated keyword.
614 INFO is a plist used as a communication channel. This function
615 is meant to be used as a predicate for `org-export-get-ordinal'."
616 (org-element-property :caption element
))
618 (defun org-ascii--build-caption (element info
)
619 "Return caption string for ELEMENT, if applicable.
621 INFO is a plist used as a communication channel.
623 The caption string contains the sequence number of ELEMENT along
624 with its real caption. Return nil when ELEMENT has no affiliated
626 (let ((caption (org-export-get-caption element
)))
628 ;; Get sequence number of current src-block among every
629 ;; src-block with a caption.
631 (org-export-get-ordinal
632 element info nil
'org-ascii--has-caption-p
))
633 (title-fmt (org-ascii--translate
634 (case (org-element-type element
)
636 (src-block "Listing %d:"))
638 (org-ascii--fill-string
639 (concat (format title-fmt reference
)
641 (org-export-data caption info
))
642 (org-ascii--current-text-width element info
) info
)))))
644 (defun org-ascii--build-toc (info &optional n keyword
)
645 "Return a table of contents.
647 INFO is a plist used as a communication channel.
649 Optional argument N, when non-nil, is an integer specifying the
652 Optional argument KEYWORD specifies the TOC keyword, if any, from
653 which the table of contents generation has been initiated."
654 (let ((title (org-ascii--translate "Table of Contents" info
)))
657 (make-string (string-width title
)
658 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
661 (if keyword
(org-ascii--current-text-width keyword info
)
662 (- org-ascii-text-width org-ascii-global-margin
))))
665 (let* ((level (org-export-get-relative-level headline info
))
666 (indent (* (1- level
) 3)))
668 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
669 (org-ascii--build-title
670 headline info
(- text-width indent
) nil
671 (or (not (plist-get info
:with-tags
))
672 (eq (plist-get info
:with-tags
) 'not-in-toc
))
674 (org-export-collect-headlines info n
) "\n")))))
676 (defun org-ascii--list-listings (keyword info
)
677 "Return a list of listings.
679 KEYWORD is the keyword that initiated the list of listings
680 generation. INFO is a plist used as a communication channel."
681 (let ((title (org-ascii--translate "List of Listings" info
)))
684 (make-string (string-width title
)
685 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
688 (if keyword
(org-ascii--current-text-width keyword info
)
689 (- org-ascii-text-width org-ascii-global-margin
)))
690 ;; Use a counter instead of retrieving ordinal of each
695 ;; Store initial text so its length can be computed. This is
696 ;; used to properly align caption right to it in case of
697 ;; filling (like contents of a description list item).
699 (format (org-ascii--translate "Listing %d:" info
)
701 (initial-width (string-width initial-text
)))
705 (org-ascii--indent-string
706 (org-ascii--fill-string
707 ;; Use short name in priority, if available.
708 (let ((caption (or (org-export-get-caption src-block t
)
709 (org-export-get-caption src-block
))))
710 (org-export-data caption info
))
711 (- text-width initial-width
) info
)
713 (org-export-collect-listings info
) "\n")))))
715 (defun org-ascii--list-tables (keyword info
)
716 "Return a list of tables.
718 KEYWORD is the keyword that initiated the list of tables
719 generation. INFO is a plist used as a communication channel."
720 (let ((title (org-ascii--translate "List of Tables" info
)))
723 (make-string (string-width title
)
724 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
727 (if keyword
(org-ascii--current-text-width keyword info
)
728 (- org-ascii-text-width org-ascii-global-margin
)))
729 ;; Use a counter instead of retrieving ordinal of each
734 ;; Store initial text so its length can be computed. This is
735 ;; used to properly align caption right to it in case of
736 ;; filling (like contents of a description list item).
738 (format (org-ascii--translate "Table %d:" info
)
740 (initial-width (string-width initial-text
)))
744 (org-ascii--indent-string
745 (org-ascii--fill-string
746 ;; Use short name in priority, if available.
747 (let ((caption (or (org-export-get-caption table t
)
748 (org-export-get-caption table
))))
749 (org-export-data caption info
))
750 (- text-width initial-width
) info
)
752 (org-export-collect-tables info
) "\n")))))
754 (defun org-ascii--unique-links (element info
)
755 "Return a list of unique link references in ELEMENT.
756 ELEMENT is either a headline element or a section element. INFO
757 is a plist used as a communication channel."
761 ;; Return LINK if it wasn't referenced so far, or nil.
762 ;; Update SEEN links along the way.
765 ;; Normalize description in footprints.
766 (cons (org-element-property :raw-link link
)
767 (let ((contents (org-element-contents link
)))
769 (replace-regexp-in-string
772 (org-element-interpret-data contents
))))))))
773 ;; Ignore LINK if it hasn't been translated already.
774 ;; It can happen if it is located in an affiliated
775 ;; keyword that was ignored.
776 (when (and (org-string-nw-p
777 (gethash link
(plist-get info
:exported-data
)))
778 (not (member footprint seen
)))
779 (push footprint seen
) link
)))))
780 ;; If at a section, find parent headline, if any, in order to
781 ;; count links that might be in the title.
783 (if (eq (org-element-type element
) 'headline
) element
784 (or (org-export-get-parent-headline element
) element
))))
785 ;; Get all links in HEADLINE.
786 (org-element-map headline
'link
787 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
789 (defun org-ascii--describe-links (links width info
)
790 "Return a string describing a list of links.
792 LINKS is a list of link type objects, as returned by
793 `org-ascii--unique-links'. WIDTH is the text width allowed for
794 the output string. INFO is a plist used as a communication
798 (let ((type (org-element-property :type link
))
799 (anchor (let ((desc (org-element-contents link
)))
800 (if desc
(org-export-data desc info
)
801 (org-element-property :raw-link link
)))))
803 ;; Coderefs, radio links and fuzzy links are ignored.
804 ((member type
'("coderef" "radio" "fuzzy")) nil
)
805 ;; Id and custom-id links: Headlines refer to their numbering.
806 ((member type
'("custom-id" "id"))
807 (let ((dest (org-export-resolve-id-link link info
)))
809 (org-ascii--fill-string
813 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
815 (org-ascii--translate "See section %s" info
)
816 (mapconcat 'number-to-string
817 (org-export-get-headline-number dest info
) "."))))
818 width info
) "\n\n")))
819 ;; Do not add a link that cannot be resolved and doesn't have
820 ;; any description: destination is already visible in the
822 ((not (org-element-contents link
)) nil
)
825 (org-ascii--fill-string
826 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
831 (defun org-ascii--checkbox (item info
)
832 "Return checkbox string for ITEM or nil.
833 INFO is a plist used as a communication channel."
834 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
835 (case (org-element-property :checkbox item
)
836 (on (if utf8p
"☑ " "[X] "))
837 (off (if utf8p
"☐ " "[ ] "))
838 (trans (if utf8p
"☒ " "[-] ")))))
844 (defun org-ascii-template--document-title (info)
845 "Return document title, as a string.
846 INFO is a plist used as a communication channel."
847 (let* ((text-width org-ascii-text-width
)
848 ;; Links in the title will not be resolved later, so we make
849 ;; sure their path is located right after them.
850 (org-ascii-links-to-notes nil
)
851 (title (org-export-data (plist-get info
:title
) info
))
852 (author (and (plist-get info
:with-author
)
853 (let ((auth (plist-get info
:author
)))
854 (and auth
(org-export-data auth info
)))))
855 (email (and (plist-get info
:with-email
)
856 (org-export-data (plist-get info
:email
) info
)))
857 (date (and (plist-get info
:with-date
)
858 (org-export-data (org-export-get-date info
) info
))))
859 ;; There are two types of title blocks depending on the presence
860 ;; of a title to display.
861 (if (string= title
"")
862 ;; Title block without a title. DATE is positioned at the top
863 ;; right of the document, AUTHOR to the top left and EMAIL
866 ((and (org-string-nw-p date
) (org-string-nw-p author
))
869 (make-string (- text-width
(string-width date
) (string-width author
))
872 (when (org-string-nw-p email
) (concat "\n" email
))
874 ((and (org-string-nw-p date
) (org-string-nw-p email
))
877 (make-string (- text-width
(string-width date
) (string-width email
))
880 ((org-string-nw-p date
)
882 (org-ascii--justify-string date text-width
'right
)
884 ((and (org-string-nw-p author
) (org-string-nw-p email
))
885 (concat author
"\n" email
"\n\n\n"))
886 ((org-string-nw-p author
) (concat author
"\n\n\n"))
887 ((org-string-nw-p email
) (concat email
"\n\n\n")))
888 ;; Title block with a title. Document's TITLE, along with the
889 ;; AUTHOR and its EMAIL are both overlined and an underlined,
890 ;; centered. Date is just below, also centered.
891 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
892 ;; Format TITLE. It may be filled if it is too wide,
893 ;; that is wider than the two thirds of the total width.
894 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
895 (formatted-title (org-ascii--fill-string title title-len info
))
898 (min (+ (max title-len
899 (string-width (or author
""))
900 (string-width (or email
"")))
902 text-width
) (if utf8p ?━ ?_
))))
903 (org-ascii--justify-string
906 (upcase formatted-title
)
908 ((and (org-string-nw-p author
) (org-string-nw-p email
))
909 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
910 ((org-string-nw-p author
)
911 (concat (if utf8p
"\n\n\n" "\n\n") author
))
912 ((org-string-nw-p email
)
913 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
915 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
916 "\n\n\n") text-width
'center
)))))
918 (defun org-ascii-inner-template (contents info
)
919 "Return complete document string after ASCII conversion.
920 CONTENTS is the transcoded contents string. INFO is a plist
921 holding export options."
922 (org-element-normalize-string
923 (org-ascii--indent-string
925 ;; 1. Document's body.
927 ;; 2. Footnote definitions.
928 (let ((definitions (org-export-collect-footnote-definitions
929 (plist-get info
:parse-tree
) info
))
930 ;; Insert full links right inside the footnote definition
931 ;; as they have no chance to be inserted later.
932 (org-ascii-links-to-notes nil
))
936 (let ((title (org-ascii--translate "Footnotes" info
)))
941 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
943 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
946 (let ((id (format "[%s] " (car ref
))))
947 ;; Distinguish between inline definitions and
948 ;; full-fledged definitions.
950 (let ((def (nth 2 ref
)))
951 (if (eq (org-element-type def
) 'org-data
)
952 ;; Full-fledged definition: footnote ID is
953 ;; inserted inside the first parsed paragraph
954 ;; (FIRST), if any, to be sure filling will
955 ;; take it into consideration.
956 (let ((first (car (org-element-contents def
))))
957 (if (not (eq (org-element-type first
) 'paragraph
))
958 (concat id
"\n" (org-export-data def info
))
959 (push id
(nthcdr 2 first
))
960 (org-export-data def info
)))
961 ;; Fill paragraph once footnote ID is inserted
962 ;; in order to have a correct length for first
964 (org-ascii--fill-string
965 (concat id
(org-export-data def info
))
966 text-width info
))))))
967 definitions
"\n\n"))))))
968 org-ascii-global-margin
)))
970 (defun org-ascii-template (contents info
)
971 "Return complete document string after ASCII conversion.
972 CONTENTS is the transcoded contents string. INFO is a plist
973 holding export options."
975 ;; 1. Build title block.
976 (org-ascii--indent-string
977 (concat (org-ascii-template--document-title info
)
978 ;; 2. Table of contents.
979 (let ((depth (plist-get info
:with-toc
)))
982 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
984 org-ascii-global-margin
)
985 ;; 3. Document's body.
987 ;; 4. Creator. Ignore `comment' value as there are no comments in
988 ;; ASCII. Justify it to the bottom right.
989 (org-ascii--indent-string
990 (let ((creator-info (plist-get info
:with-creator
))
991 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
992 (unless (or (not creator-info
) (eq creator-info
'comment
))
995 (org-ascii--fill-string
996 (plist-get info
:creator
) text-width info
'right
))))
997 org-ascii-global-margin
)))
999 (defun org-ascii--translate (s info
)
1000 "Translate string S according to specified language and charset.
1001 INFO is a plist used as a communication channel."
1002 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1003 (org-export-translate s charset info
)))
1007 ;;; Transcode Functions
1011 (defun org-ascii-bold (bold contents info
)
1012 "Transcode BOLD from Org to ASCII.
1013 CONTENTS is the text with bold markup. INFO is a plist holding
1014 contextual information."
1015 (format "*%s*" contents
))
1020 (defun org-ascii-center-block (center-block contents info
)
1021 "Transcode a CENTER-BLOCK element from Org to ASCII.
1022 CONTENTS holds the contents of the block. INFO is a plist
1023 holding contextual information."
1024 (org-ascii--justify-string
1025 contents
(org-ascii--current-text-width center-block info
) 'center
))
1030 (defun org-ascii-clock (clock contents info
)
1031 "Transcode a CLOCK object from Org to ASCII.
1032 CONTENTS is nil. INFO is a plist holding contextual
1034 (concat org-clock-string
" "
1036 (org-element-property :raw-value
1037 (org-element-property :value clock
)))
1038 (let ((time (org-element-property :duration clock
)))
1043 (org-split-string time
":")))))))
1048 (defun org-ascii-code (code contents info
)
1049 "Return a CODE object from Org to ASCII.
1050 CONTENTS is nil. INFO is a plist holding contextual
1052 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1057 (defun org-ascii-drawer (drawer contents info
)
1058 "Transcode a DRAWER element from Org to ASCII.
1059 CONTENTS holds the contents of the block. INFO is a plist
1060 holding contextual information."
1061 (let ((name (org-element-property :drawer-name drawer
))
1062 (width (org-ascii--current-text-width drawer info
)))
1063 (funcall org-ascii-format-drawer-function name contents width
)))
1068 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1069 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1070 CONTENTS holds the contents of the block. INFO is a plist
1071 holding contextual information."
1077 (defun org-ascii-entity (entity contents info
)
1078 "Transcode an ENTITY object from Org to ASCII.
1079 CONTENTS are the definition itself. INFO is a plist holding
1080 contextual information."
1081 (org-element-property
1082 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1088 (defun org-ascii-example-block (example-block contents info
)
1089 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1090 CONTENTS is nil. INFO is a plist holding contextual information."
1091 (org-ascii--box-string
1092 (org-export-format-code-default example-block info
) info
))
1097 (defun org-ascii-export-snippet (export-snippet contents info
)
1098 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1099 CONTENTS is nil. INFO is a plist holding contextual information."
1100 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1101 (org-element-property :value export-snippet
)))
1106 (defun org-ascii-export-block (export-block contents info
)
1107 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1108 CONTENTS is nil. INFO is a plist holding contextual information."
1109 (when (string= (org-element-property :type export-block
) "ASCII")
1110 (org-remove-indentation (org-element-property :value export-block
))))
1115 (defun org-ascii-fixed-width (fixed-width contents info
)
1116 "Transcode a FIXED-WIDTH element from Org to ASCII.
1117 CONTENTS is nil. INFO is a plist holding contextual information."
1118 (org-ascii--box-string
1119 (org-remove-indentation
1120 (org-element-property :value fixed-width
)) info
))
1123 ;;;; Footnote Definition
1125 ;; Footnote Definitions are ignored. They are compiled at the end of
1126 ;; the document, by `org-ascii-inner-template'.
1129 ;;;; Footnote Reference
1131 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1132 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1133 CONTENTS is nil. INFO is a plist holding contextual information."
1134 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1139 (defun org-ascii-headline (headline contents info
)
1140 "Transcode a HEADLINE element from Org to ASCII.
1141 CONTENTS holds the contents of the headline. INFO is a plist
1142 holding contextual information."
1143 ;; Don't export footnote section, which will be handled at the end
1145 (unless (org-element-property :footnote-section-p headline
)
1146 (let* ((low-level-rank (org-export-low-level-p headline info
))
1147 (width (org-ascii--current-text-width headline info
))
1148 ;; Blank lines between headline and its contents.
1149 ;; `org-ascii-headline-spacing', when set, overwrites
1150 ;; original buffer's spacing.
1153 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1154 (org-element-property :pre-blank headline
)) ?
\n))
1155 ;; Even if HEADLINE has no section, there might be some
1156 ;; links in its title that we shouldn't forget to describe.
1158 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1159 (let ((title (org-element-property :title headline
)))
1161 (org-ascii--describe-links
1162 (org-ascii--unique-links title info
) width info
))))))
1163 ;; Deep subtree: export it as a list item.
1167 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1168 org-ascii-bullets
))))
1170 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1173 (org-ascii--build-title headline info width
) "\n"
1174 ;; Contents, indented by length of bullet.
1176 (org-ascii--indent-string
1178 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1180 ;; Else: Standard headline.
1182 (org-ascii--build-title headline info width
'underline
)
1184 (concat (when (org-string-nw-p links
) links
) contents
))))))
1187 ;;;; Horizontal Rule
1189 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1190 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1191 CONTENTS is nil. INFO is a plist holding contextual
1193 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1195 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1196 (org-ascii--justify-string
1197 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1198 (string-to-number spec-width
)
1200 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1201 text-width
'center
)))
1204 ;;;; Inline Src Block
1206 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1207 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1208 CONTENTS holds the contents of the item. INFO is a plist holding
1209 contextual information."
1210 (format org-ascii-verbatim-format
1211 (org-element-property :value inline-src-block
)))
1216 (defun org-ascii-format-inlinetask-default
1217 (todo type priority name tags contents width inlinetask info
)
1218 "Format an inline task element for ASCII export.
1219 See `org-ascii-format-inlinetask-function' for a description
1221 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1222 (width (or width org-ascii-inlinetask-width
)))
1223 (org-ascii--indent-string
1225 ;; Top line, with an additional blank line if not in UTF-8.
1226 (make-string width
(if utf8p ?━ ?_
)) "\n"
1227 (unless utf8p
(concat (make-string width ?
) "\n"))
1228 ;; Add title. Fill it if wider than inlinetask.
1229 (let ((title (org-ascii--build-title inlinetask info width
)))
1230 (if (<= (string-width title
) width
) title
1231 (org-ascii--fill-string title width info
)))
1233 ;; If CONTENTS is not empty, insert it along with
1235 (when (org-string-nw-p contents
)
1236 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1238 (make-string width
(if utf8p ?━ ?_
)))
1239 ;; Flush the inlinetask to the right.
1240 (- org-ascii-text-width org-ascii-global-margin
1241 (if (not (org-export-get-parent-headline inlinetask
)) 0
1242 org-ascii-inner-margin
)
1243 (org-ascii--current-text-width inlinetask info
)))))
1245 (defun org-ascii-inlinetask (inlinetask contents info
)
1246 "Transcode an INLINETASK element from Org to ASCII.
1247 CONTENTS holds the contents of the block. INFO is a plist
1248 holding contextual information."
1249 (let ((width (org-ascii--current-text-width inlinetask info
)))
1250 (funcall org-ascii-format-inlinetask-function
1252 (and (plist-get info
:with-todo-keywords
)
1253 (let ((todo (org-element-property
1254 :todo-keyword inlinetask
)))
1255 (and todo
(org-export-data todo info
))))
1257 (org-element-property :todo-type inlinetask
)
1259 (and (plist-get info
:with-priority
)
1260 (org-element-property :priority inlinetask
))
1262 (org-export-data (org-element-property :title inlinetask
) info
)
1264 (and (plist-get info
:with-tags
)
1265 (org-element-property :tags inlinetask
))
1266 ;; contents and width
1267 contents width inlinetask info
)))
1272 (defun org-ascii-italic (italic contents info
)
1273 "Transcode italic from Org to ASCII.
1274 CONTENTS is the text with italic markup. INFO is a plist holding
1275 contextual information."
1276 (format "/%s/" contents
))
1281 (defun org-ascii-item (item contents info
)
1282 "Transcode an ITEM element from Org to ASCII.
1283 CONTENTS holds the contents of the item. INFO is a plist holding
1284 contextual information."
1285 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1286 (checkbox (org-ascii--checkbox item info
))
1287 (list-type (org-element-property :type
(org-export-get-parent item
)))
1289 ;; First parent of ITEM is always the plain-list. Get
1290 ;; `:type' property from it.
1291 (org-list-bullet-string
1295 (org-export-data (org-element-property :tag item
) info
)
1298 ;; Return correct number for ITEM, paying attention to
1300 (let* ((struct (org-element-property :structure item
))
1301 (bul (org-element-property :bullet item
))
1302 (num (number-to-string
1303 (car (last (org-list-get-item-number
1304 (org-element-property :begin item
)
1306 (org-list-prevs-alist struct
)
1307 (org-list-parents-alist struct
)))))))
1308 (replace-regexp-in-string "[0-9]+" num bul
)))
1309 (t (let ((bul (org-element-property :bullet item
)))
1310 ;; Change bullets into more visible form if UTF-8 is active.
1312 (replace-regexp-in-string
1314 (replace-regexp-in-string
1316 (replace-regexp-in-string "*" "‣" bul
))))))))))
1319 (unless (eq list-type
'descriptive
) checkbox
)
1320 ;; Contents: Pay attention to indentation. Note: check-boxes are
1321 ;; already taken care of at the paragraph level so they don't
1322 ;; interfere with indentation.
1323 (let ((contents (org-ascii--indent-string contents
(string-width bullet
))))
1324 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1326 (concat "\n" contents
))))))
1331 (defun org-ascii-keyword (keyword contents info
)
1332 "Transcode a KEYWORD element from Org to ASCII.
1333 CONTENTS is nil. INFO is a plist holding contextual
1335 (let ((key (org-element-property :key keyword
))
1336 (value (org-element-property :value keyword
)))
1338 ((string= key
"ASCII") value
)
1339 ((string= key
"TOC")
1340 (let ((value (downcase value
)))
1342 ((string-match "\\<headlines\\>" value
)
1343 (let ((depth (or (and (string-match "[0-9]+" value
)
1344 (string-to-number (match-string 0 value
)))
1345 (plist-get info
:with-toc
))))
1346 (org-ascii--build-toc
1347 info
(and (wholenump depth
) depth
) keyword
)))
1348 ((string= "tables" value
)
1349 (org-ascii--list-tables keyword info
))
1350 ((string= "listings" value
)
1351 (org-ascii--list-listings keyword info
))))))))
1354 ;;;; Latex Environment
1356 (defun org-ascii-latex-environment (latex-environment contents info
)
1357 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1358 CONTENTS is nil. INFO is a plist holding contextual
1360 (when (plist-get info
:with-latex
)
1361 (org-remove-indentation (org-element-property :value latex-environment
))))
1366 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1367 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1368 CONTENTS is nil. INFO is a plist holding contextual
1370 (when (plist-get info
:with-latex
)
1371 (org-element-property :value latex-fragment
)))
1376 (defun org-ascii-line-break (line-break contents info
)
1377 "Transcode a LINE-BREAK object from Org to ASCII.
1378 CONTENTS is nil. INFO is a plist holding contextual
1379 information." hard-newline
)
1384 (defun org-ascii-link (link desc info
)
1385 "Transcode a LINK object from Org to ASCII.
1387 DESC is the description part of the link, or the empty string.
1388 INFO is a plist holding contextual information."
1389 (let ((raw-link (org-element-property :raw-link link
))
1390 (type (org-element-property :type link
)))
1392 ((string= type
"coderef")
1393 (let ((ref (org-element-property :path link
)))
1394 (format (org-export-get-coderef-format ref desc
)
1395 (org-export-resolve-coderef ref info
))))
1396 ;; Do not apply a special syntax on radio links. Though, use
1397 ;; transcoded target's contents as output.
1398 ((string= type
"radio") desc
)
1399 ;; Do not apply a special syntax on fuzzy links pointing to
1401 ((string= type
"fuzzy")
1402 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1403 (if (org-string-nw-p desc
) desc
1406 (org-export-get-ordinal
1407 destination info nil
'org-ascii--has-caption-p
)))
1409 (if (atom number
) (number-to-string number
)
1410 (mapconcat 'number-to-string number
"."))))))))
1412 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1414 (format "[%s]" desc
)
1415 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1418 ;;;; Node Properties
1420 (defun org-ascii-node-property (node-property contents info
)
1421 "Transcode a NODE-PROPERTY element from Org to ASCII.
1422 CONTENTS is nil. INFO is a plist holding contextual
1425 (org-element-property :key node-property
)
1426 (let ((value (org-element-property :value node-property
)))
1427 (if value
(concat " " value
) ""))))
1432 (defun org-ascii-paragraph (paragraph contents info
)
1433 "Transcode a PARAGRAPH element from Org to ASCII.
1434 CONTENTS is the contents of the paragraph, as a string. INFO is
1435 the plist used as a communication channel."
1436 (org-ascii--fill-string
1437 (if (not (wholenump org-ascii-indented-line-width
)) contents
1439 ;; Do not indent first paragraph in a section.
1440 (unless (and (not (org-export-get-previous-element paragraph info
))
1441 (eq (org-element-type (org-export-get-parent paragraph
))
1443 (make-string org-ascii-indented-line-width ?\s
))
1444 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))
1445 (org-ascii--current-text-width paragraph info
) info
))
1450 (defun org-ascii-plain-list (plain-list contents info
)
1451 "Transcode a PLAIN-LIST element from Org to ASCII.
1452 CONTENTS is the contents of the list. INFO is a plist holding
1453 contextual information."
1459 (defun org-ascii-plain-text (text info
)
1460 "Transcode a TEXT string from Org to ASCII.
1461 INFO is a plist used as a communication channel."
1462 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1463 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1464 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1465 (if (not (plist-get info
:with-special-strings
)) text
1466 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1467 (if (not utf8p
) text
1468 ;; Usual replacements in utf-8 with proper option set.
1469 (replace-regexp-in-string
1471 (replace-regexp-in-string
1473 (replace-regexp-in-string "---" "—" text
)))))))
1478 (defun org-ascii-planning (planning contents info
)
1479 "Transcode a PLANNING element from Org to ASCII.
1480 CONTENTS is nil. INFO is a plist used as a communication
1485 (list (let ((closed (org-element-property :closed planning
)))
1487 (concat org-closed-string
" "
1489 (org-element-property :raw-value closed
)))))
1490 (let ((deadline (org-element-property :deadline planning
)))
1492 (concat org-deadline-string
" "
1494 (org-element-property :raw-value deadline
)))))
1495 (let ((scheduled (org-element-property :scheduled planning
)))
1497 (concat org-scheduled-string
" "
1499 (org-element-property :raw-value scheduled
)))))))
1503 ;;;; Property Drawer
1505 (defun org-ascii-property-drawer (property-drawer contents info
)
1506 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1507 CONTENTS holds the contents of the drawer. INFO is a plist
1508 holding contextual information."
1509 (org-string-nw-p contents
))
1514 (defun org-ascii-quote-block (quote-block contents info
)
1515 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1516 CONTENTS holds the contents of the block. INFO is a plist
1517 holding contextual information."
1518 (org-ascii--indent-string contents org-ascii-quote-margin
))
1523 (defun org-ascii-radio-target (radio-target contents info
)
1524 "Transcode a RADIO-TARGET object from Org to ASCII.
1525 CONTENTS is the contents of the target. INFO is a plist holding
1526 contextual information."
1532 (defun org-ascii-section (section contents info
)
1533 "Transcode a SECTION element from Org to ASCII.
1534 CONTENTS is the contents of the section. INFO is a plist holding
1535 contextual information."
1536 (org-ascii--indent-string
1539 (when org-ascii-links-to-notes
1540 ;; Add list of links at the end of SECTION.
1541 (let ((links (org-ascii--describe-links
1542 (org-ascii--unique-links section info
)
1543 (org-ascii--current-text-width section info
) info
)))
1544 ;; Separate list of links and section contents.
1545 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1546 ;; Do not apply inner margin if parent headline is low level.
1547 (let ((headline (org-export-get-parent-headline section
)))
1548 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1549 org-ascii-inner-margin
))))
1554 (defun org-ascii-special-block (special-block contents info
)
1555 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1556 CONTENTS holds the contents of the block. INFO is a plist
1557 holding contextual information."
1563 (defun org-ascii-src-block (src-block contents info
)
1564 "Transcode a SRC-BLOCK element from Org to ASCII.
1565 CONTENTS holds the contents of the item. INFO is a plist holding
1566 contextual information."
1567 (let ((caption (org-ascii--build-caption src-block info
))
1568 (code (org-export-format-code-default src-block info
)))
1569 (if (equal code
"") ""
1571 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1572 (org-ascii--box-string code info
)
1573 (when (and caption
(not org-ascii-caption-above
))
1574 (concat "\n" caption
))))))
1577 ;;;; Statistics Cookie
1579 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1580 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1581 CONTENTS is nil. INFO is a plist holding contextual information."
1582 (org-element-property :value statistics-cookie
))
1587 (defun org-ascii-subscript (subscript contents info
)
1588 "Transcode a SUBSCRIPT object from Org to ASCII.
1589 CONTENTS is the contents of the object. INFO is a plist holding
1590 contextual information."
1591 (if (org-element-property :use-brackets-p subscript
)
1592 (format "_{%s}" contents
)
1593 (format "_%s" contents
)))
1598 (defun org-ascii-superscript (superscript contents info
)
1599 "Transcode a SUPERSCRIPT object from Org to ASCII.
1600 CONTENTS is the contents of the object. INFO is a plist holding
1601 contextual information."
1602 (if (org-element-property :use-brackets-p superscript
)
1603 (format "^{%s}" contents
)
1604 (format "^%s" contents
)))
1609 (defun org-ascii-strike-through (strike-through contents info
)
1610 "Transcode STRIKE-THROUGH from Org to ASCII.
1611 CONTENTS is text with strike-through markup. INFO is a plist
1612 holding contextual information."
1613 (format "+%s+" contents
))
1618 (defun org-ascii-table (table contents info
)
1619 "Transcode a TABLE element from Org to ASCII.
1620 CONTENTS is the contents of the table. INFO is a plist holding
1621 contextual information."
1622 (let ((caption (org-ascii--build-caption table info
)))
1624 ;; Possibly add a caption string above.
1625 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1626 ;; Insert table. Note: "table.el" tables are left unmodified.
1627 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1628 ((and org-ascii-table-use-ascii-art
1629 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1630 (require 'ascii-art-to-unicode nil t
))
1632 (insert (org-remove-indentation
1633 (org-element-property :value table
)))
1634 (goto-char (point-min))
1636 (goto-char (point-max))
1637 (skip-chars-backward " \r\t\n")
1638 (buffer-substring (point-min) (point))))
1639 (t (org-remove-indentation (org-element-property :value table
))))
1640 ;; Possible add a caption string below.
1641 (and (not org-ascii-caption-above
) caption
))))
1646 (defun org-ascii--table-cell-width (table-cell info
)
1647 "Return width of TABLE-CELL.
1649 INFO is a plist used as a communication channel.
1651 Width of a cell is determined either by a width cookie in the
1652 same column as the cell, or by the maximum cell's length in that
1655 When `org-ascii-table-widen-columns' is non-nil, width cookies
1657 (let* ((row (org-export-get-parent table-cell
))
1658 (table (org-export-get-parent row
))
1659 (col (let ((cells (org-element-contents row
)))
1660 (- (length cells
) (length (memq table-cell cells
)))))
1662 (or (plist-get info
:ascii-table-cell-width-cache
)
1663 (plist-get (setq info
1664 (plist-put info
:ascii-table-cell-width-cache
1665 (make-hash-table :test
'equal
)))
1666 :ascii-table-cell-width-cache
)))
1667 (key (cons table col
)))
1668 (or (gethash key cache
)
1671 (or (and (not org-ascii-table-widen-columns
)
1672 (org-export-table-cell-width table-cell info
))
1673 (let* ((max-width 0))
1674 (org-element-map table
'table-row
1679 (org-element-contents
1680 (elt (org-element-contents row
) col
))
1687 (defun org-ascii-table-cell (table-cell contents info
)
1688 "Transcode a TABLE-CELL object from Org to ASCII.
1689 CONTENTS is the cell contents. INFO is a plist used as
1690 a communication channel."
1691 ;; Determine column width. When `org-ascii-table-widen-columns'
1692 ;; is nil and some width cookie has set it, use that value.
1693 ;; Otherwise, compute the maximum width among transcoded data of
1694 ;; each cell in the column.
1695 (let ((width (org-ascii--table-cell-width table-cell info
)))
1696 ;; When contents are too large, truncate them.
1697 (unless (or org-ascii-table-widen-columns
1698 (<= (string-width (or 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
)))))
1709 (make-string (- width
(string-width (or data
""))) ?\s
))))
1711 (concat (format " %s " contents
)
1712 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1713 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1718 (defun org-ascii-table-row (table-row contents info
)
1719 "Transcode a TABLE-ROW element from Org to ASCII.
1720 CONTENTS is the row contents. INFO is a plist used as
1721 a communication channel."
1722 (when (eq (org-element-property :type table-row
) 'standard
)
1725 (lambda (lcorner horiz vert rcorner
)
1729 (org-element-map table-row
'table-cell
1731 (let ((width (org-ascii--table-cell-width cell info
))
1732 (borders (org-export-table-cell-borders cell info
)))
1734 ;; In order to know if CELL starts the row, do
1735 ;; not compare it with the first cell in the
1736 ;; row as there might be a special column.
1737 ;; Instead, compare it with first exportable
1738 ;; cell, obtained with `org-element-map'.
1739 (when (and (memq 'left borders
)
1740 (eq (org-element-map table-row
'table-cell
1744 (make-string (+ 2 width
) (string-to-char horiz
))
1746 ((not (memq 'right borders
)) nil
)
1747 ((eq (car (last (org-element-contents table-row
))) cell
)
1751 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1752 (borders (org-export-table-cell-borders
1753 (org-element-map table-row
'table-cell
'identity info t
)
1756 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1757 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1758 (funcall build-hline
"+" "-" "+" "+")))
1759 ((memq 'above borders
)
1760 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1761 (funcall build-hline
"+" "-" "+" "+"))))
1762 (when (memq 'left borders
) (if utf8p
"│" "|"))
1764 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1765 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1766 (funcall build-hline
"+" "-" "+" "+")))))))
1771 (defun org-ascii-timestamp (timestamp contents info
)
1772 "Transcode a TIMESTAMP object from Org to ASCII.
1773 CONTENTS is nil. INFO is a plist holding contextual information."
1774 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1779 (defun org-ascii-underline (underline contents info
)
1780 "Transcode UNDERLINE from Org to ASCII.
1781 CONTENTS is the text with underline markup. INFO is a plist
1782 holding contextual information."
1783 (format "_%s_" contents
))
1788 (defun org-ascii-verbatim (verbatim contents info
)
1789 "Return a VERBATIM object from Org to ASCII.
1790 CONTENTS is nil. INFO is a plist holding contextual information."
1791 (format org-ascii-verbatim-format
1792 (org-element-property :value verbatim
)))
1797 (defun org-ascii-verse-block (verse-block contents info
)
1798 "Transcode a VERSE-BLOCK element from Org to ASCII.
1799 CONTENTS is verse block contents. INFO is a plist holding
1800 contextual information."
1801 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1802 (org-ascii--indent-string
1803 (org-ascii--justify-string contents verse-width
'left
)
1804 org-ascii-quote-margin
)))
1810 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1811 "Filter controlling number of blank lines after a headline.
1813 HEADLINE is a string representing a transcoded headline.
1814 BACK-END is symbol specifying back-end used for export. INFO is
1815 plist containing the communication channel.
1817 This function only applies to `ascii' back-end. See
1818 `org-ascii-headline-spacing' for information."
1819 (if (not org-ascii-headline-spacing
) headline
1820 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1821 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1823 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1824 "Filter controlling number of blank lines between paragraphs.
1826 TREE is the parse tree. BACK-END is the symbol specifying
1827 back-end used for export. INFO is a plist used as
1828 a communication channel.
1830 See `org-ascii-paragraph-spacing' for information."
1831 (when (wholenump org-ascii-paragraph-spacing
)
1832 (org-element-map tree
'paragraph
1834 (when (eq (org-element-type (org-export-get-next-element p info
))
1836 (org-element-put-property
1837 p
:post-blank org-ascii-paragraph-spacing
)))))
1840 (defun org-ascii-filter-comment-spacing (tree backend info
)
1841 "Filter removing blank lines between comments.
1842 TREE is the parse tree. BACK-END is the symbol specifying
1843 back-end used for export. INFO is a plist used as
1844 a communication channel."
1845 (org-element-map tree
'(comment comment-block
)
1847 (when (memq (org-element-type (org-export-get-next-element c info
))
1848 '(comment comment-block
))
1849 (org-element-put-property c
:post-blank
0))))
1854 ;;; End-user functions
1857 (defun org-ascii-export-as-ascii
1858 (&optional async subtreep visible-only body-only ext-plist
)
1859 "Export current buffer to a text buffer.
1861 If narrowing is active in the current buffer, only export its
1864 If a region is active, export that region.
1866 A non-nil optional argument ASYNC means the process should happen
1867 asynchronously. The resulting buffer should be accessible
1868 through the `org-export-stack' interface.
1870 When optional argument SUBTREEP is non-nil, export the sub-tree
1871 at point, extracting information from the headline properties
1874 When optional argument VISIBLE-ONLY is non-nil, don't export
1875 contents of hidden elements.
1877 When optional argument BODY-ONLY is non-nil, strip title and
1878 table of contents from output.
1880 EXT-PLIST, when provided, is a property list with external
1881 parameters overriding Org default settings, but still inferior to
1882 file-local settings.
1884 Export is done in a buffer named \"*Org ASCII Export*\", which
1885 will be displayed when `org-export-show-temporary-export-buffer'
1888 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
1889 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
1892 (defun org-ascii-export-to-ascii
1893 (&optional async subtreep visible-only body-only ext-plist
)
1894 "Export current buffer to a text file.
1896 If narrowing is active in the current buffer, only export its
1899 If a region is active, export that region.
1901 A non-nil optional argument ASYNC means the process should happen
1902 asynchronously. The resulting file should be accessible through
1903 the `org-export-stack' interface.
1905 When optional argument SUBTREEP is non-nil, export the sub-tree
1906 at point, extracting information from the headline properties
1909 When optional argument VISIBLE-ONLY is non-nil, don't export
1910 contents of hidden elements.
1912 When optional argument BODY-ONLY is non-nil, strip title and
1913 table of contents from output.
1915 EXT-PLIST, when provided, is a property list with external
1916 parameters overriding Org default settings, but still inferior to
1917 file-local settings.
1919 Return output file's name."
1921 (let ((file (org-export-output-file-name ".txt" subtreep
)))
1922 (org-export-to-file 'ascii file
1923 async subtreep visible-only body-only ext-plist
)))
1926 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1927 "Publish an Org file to ASCII.
1929 FILENAME is the filename of the Org file to be published. PLIST
1930 is the property list for the given project. PUB-DIR is the
1931 publishing directory.
1933 Return output file name."
1935 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1938 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1939 "Publish an Org file to Latin-1.
1941 FILENAME is the filename of the Org file to be published. PLIST
1942 is the property list for the given project. PUB-DIR is the
1943 publishing directory.
1945 Return output file name."
1947 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1950 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1951 "Publish an org file to UTF-8.
1953 FILENAME is the filename of the Org file to be published. PLIST
1954 is the property list for the given project. PUB-DIR is the
1955 publishing directory.
1957 Return output file name."
1959 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1965 ;; generated-autoload-file: "org-loaddefs.el"
1966 ;; coding: utf-8-emacs
1969 ;;; ox-ascii.el ends here