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 number. If it is the symbol `auto' preserve
197 indentation from original document."
198 :group
'org-export-ascii
200 :package-version
'(Org .
"8.0")
202 (integer :tag
"Number of white spaces characters")
203 (const :tag
"Preserve original width" auto
)))
205 (defcustom org-ascii-paragraph-spacing
'auto
206 "Number of white lines between paragraphs.
207 If the value is an integer, add this number of blank lines
208 between contiguous paragraphs. If is it the symbol `auto', keep
209 the same number of blank lines as in the original document."
210 :group
'org-export-ascii
212 :package-version
'(Org .
"8.0")
214 (integer :tag
"Number of blank lines")
215 (const :tag
"Preserve original spacing" auto
)))
217 (defcustom org-ascii-charset
'ascii
218 "The charset allowed to represent various elements and objects.
220 `ascii' Only use plain ASCII characters
221 `latin1' Include Latin-1 characters
222 `utf-8' Use all UTF-8 characters"
223 :group
'org-export-ascii
225 :package-version
'(Org .
"8.0")
227 (const :tag
"ASCII" ascii
)
228 (const :tag
"Latin-1" latin1
)
229 (const :tag
"UTF-8" utf-8
)))
231 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
233 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
234 "Characters for underlining headings in ASCII export.
236 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
237 and whose value is a list of characters.
239 For each supported charset, this variable associates a sequence
240 of underline characters. In a sequence, the characters will be
241 used in order for headlines level 1, 2, ... If no character is
242 available for a given level, the headline won't be underlined."
243 :group
'org-export-ascii
245 :package-version
'(Org .
"8.0")
247 (cons :tag
"Underline characters sequence"
248 (const :tag
"ASCII charset" ascii
)
250 (cons :tag
"Underline characters sequence"
251 (const :tag
"Latin-1 charset" latin1
)
253 (cons :tag
"Underline characters sequence"
254 (const :tag
"UTF-8 charset" utf-8
)
255 (repeat character
))))
257 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
260 "Bullet characters for headlines converted to lists in ASCII export.
262 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
263 and whose value is a list of characters.
265 The first character is used for the first level considered as low
266 level, and so on. If there are more levels than characters given
267 here, the list will be repeated.
269 Note that this variable doesn't affect plain lists
271 :group
'org-export-ascii
273 :package-version
'(Org .
"8.0")
275 (cons :tag
"Bullet characters for low level headlines"
276 (const :tag
"ASCII charset" ascii
)
278 (cons :tag
"Bullet characters for low level headlines"
279 (const :tag
"Latin-1 charset" latin1
)
281 (cons :tag
"Bullet characters for low level headlines"
282 (const :tag
"UTF-8 charset" utf-8
)
283 (repeat character
))))
285 (defcustom org-ascii-links-to-notes t
286 "Non-nil means convert links to notes before the next headline.
287 When nil, the link will be exported in place. If the line
288 becomes long in this way, it will be wrapped."
289 :group
'org-export-ascii
291 :package-version
'(Org .
"8.0")
294 (defcustom org-ascii-table-keep-all-vertical-lines nil
295 "Non-nil means keep all vertical lines in ASCII tables.
296 When nil, vertical lines will be removed except for those needed
297 for column grouping."
298 :group
'org-export-ascii
300 :package-version
'(Org .
"8.0")
303 (defcustom org-ascii-table-widen-columns t
304 "Non-nil means widen narrowed columns for export.
305 When nil, narrowed columns will look in ASCII export just like in
306 Org mode, i.e. with \"=>\" as ellipsis."
307 :group
'org-export-ascii
309 :package-version
'(Org .
"8.0")
312 (defcustom org-ascii-table-use-ascii-art nil
313 "Non-nil means table.el tables are turned into ascii-art.
315 It only makes sense when export charset is `utf-8'. It is nil by
316 default since it requires ascii-art-to-unicode.el package. You
317 can download it here:
319 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
320 :group
'org-export-ascii
322 :package-version
'(Org .
"8.0")
325 (defcustom org-ascii-caption-above nil
326 "When non-nil, place caption string before the element.
327 Otherwise, place it right after it."
328 :group
'org-export-ascii
330 :package-version
'(Org .
"8.0")
333 (defcustom org-ascii-verbatim-format
"`%s'"
334 "Format string used for verbatim text and inline code."
335 :group
'org-export-ascii
337 :package-version
'(Org .
"8.0")
340 (defcustom org-ascii-format-drawer-function
341 (lambda (name contents width
) contents
)
342 "Function called to format a drawer in ASCII.
344 The function must accept three parameters:
345 NAME the drawer name, like \"LOGBOOK\"
346 CONTENTS the contents of the drawer.
347 WIDTH the text width within the drawer.
349 The function should return either the string to be exported or
350 nil to ignore the drawer.
352 The default value simply returns the value of CONTENTS."
353 :group
'org-export-ascii
355 :package-version
'(Org .
"8.0")
358 (defcustom org-ascii-format-inlinetask-function
359 'org-ascii-format-inlinetask-default
360 "Function called to format an inlinetask in ASCII.
362 The function must accept nine parameters:
363 TODO the todo keyword, as a string
364 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
365 PRIORITY the inlinetask priority, as a string
366 NAME the inlinetask name, as a string.
367 TAGS the inlinetask tags, as a list of strings.
368 CONTENTS the contents of the inlinetask, as a string.
369 WIDTH the width of the inlinetask, as a number.
370 INLINETASK the inlinetask itself.
371 INFO the info channel.
373 The function should return either the string to be exported or
374 nil to ignore the inline task."
375 :group
'org-export-ascii
377 :package-version
'(Org .
"8.3")
382 ;;; Internal Functions
384 ;; Internal functions fall into three categories.
386 ;; The first one is about text formatting. The core function is
387 ;; `org-ascii--current-text-width', which determines the current
388 ;; text width allowed to a given element. In other words, it helps
389 ;; keeping each line width within maximum text width defined in
390 ;; `org-ascii-text-width'. Once this information is known,
391 ;; `org-ascii--fill-string', `org-ascii--justify-string',
392 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
393 ;; operate on a given output string.
395 ;; The second category contains functions handling elements listings,
396 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
397 ;; returns a complete table of contents, `org-ascii--list-listings'
398 ;; returns a list of referenceable src-block elements, and
399 ;; `org-ascii--list-tables' does the same for table elements.
401 ;; The third category includes general helper functions.
402 ;; `org-ascii--build-title' creates the title for a given headline
403 ;; or inlinetask element. `org-ascii--build-caption' returns the
404 ;; caption string associated to a table or a src-block.
405 ;; `org-ascii--describe-links' creates notes about links for
406 ;; insertion at the end of a section. It uses
407 ;; `org-ascii--unique-links' to get the list of links to describe.
408 ;; Eventually, `org-ascii--translate' translates a string according
409 ;; to language and charset specification.
412 (defun org-ascii--fill-string (s text-width info
&optional justify
)
413 "Fill a string with specified text-width and return it.
415 S is the string being filled. TEXT-WIDTH is an integer
416 specifying maximum length of a line. INFO is the plist used as
417 a communication channel.
419 Optional argument JUSTIFY can specify any type of justification
420 among `left', `center', `right' or `full'. A nil value is
421 equivalent to `left'. For a justification that doesn't also fill
422 string, see `org-ascii--justify-string'.
424 Return nil if S isn't a string."
425 ;; Don't fill paragraph when break should be preserved.
426 (cond ((not (stringp s
)) nil
)
427 ((plist-get info
:preserve-breaks
) s
)
428 (t (let ((double-space-p sentence-end-double-space
))
430 (let ((fill-column text-width
)
431 (use-hard-newlines t
)
432 (sentence-end-double-space double-space-p
))
434 (fill-region (point-min) (point-max) justify
))
437 (defun org-ascii--justify-string (s text-width how
)
439 TEXT-WIDTH is an integer specifying maximum length of a line.
440 HOW determines the type of justification: it can be `left',
441 `right', `full' or `center'."
444 (goto-char (point-min))
445 (let ((fill-column text-width
)
446 ;; Disable `adaptive-fill-mode' so it doesn't prevent
447 ;; filling lines matching `adaptive-fill-regexp'.
448 (adaptive-fill-mode nil
))
449 (while (< (point) (point-max))
450 (justify-current-line how
)
454 (defun org-ascii--indent-string (s width
)
455 "Indent string S by WIDTH white spaces.
456 Empty lines are not indented."
458 (replace-regexp-in-string
459 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
461 (defun org-ascii--box-string (s info
)
462 "Return string S with a partial box to its left.
463 INFO is a plist used as a communication channel."
464 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
465 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
466 (replace-regexp-in-string
467 "^" (if utf8p
"│ " "| ")
468 ;; Remove last newline character.
469 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
471 (defun org-ascii--current-text-width (element info
)
472 "Return maximum text width for ELEMENT's contents.
473 INFO is a plist used as a communication channel."
474 (case (org-element-type element
)
475 ;; Elements with an absolute width: `headline' and `inlinetask'.
476 (inlinetask org-ascii-inlinetask-width
)
478 (- org-ascii-text-width
479 (let ((low-level-rank (org-export-low-level-p element info
)))
480 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
481 ;; Elements with a relative width: store maximum text width in
484 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
485 ;; Total width is determined by the presence, or not, of an
486 ;; inline task among ELEMENT parents.
488 (if (loop for parent in genealogy
489 thereis
(eq (org-element-type parent
) 'inlinetask
))
490 org-ascii-inlinetask-width
491 ;; No inlinetask: Remove global margin from text width.
492 (- org-ascii-text-width
493 org-ascii-global-margin
494 (let ((parent (org-export-get-parent-headline element
)))
495 ;; Inner margin doesn't apply to text before first
498 (let ((low-level-rank
499 (org-export-low-level-p parent info
)))
500 ;; Inner margin doesn't apply to contents of
501 ;; low level headlines, since they've got their
502 ;; own indentation mechanism.
503 (if low-level-rank
(* low-level-rank
2)
504 org-ascii-inner-margin
))))))))
506 ;; Each `quote-block' and `verse-block' above narrows text
507 ;; width by twice the standard margin size.
508 (+ (* (loop for parent in genealogy
509 when
(memq (org-element-type parent
)
510 '(quote-block verse-block
))
512 2 org-ascii-quote-margin
)
513 ;; Text width within a plain-list is restricted by
514 ;; indentation of current item. If that's the case,
515 ;; compute it with the help of `:structure' property from
516 ;; parent item, if any.
518 (if (eq (org-element-type element
) 'item
) element
519 (loop for parent in genealogy
520 when
(eq (org-element-type parent
) 'item
)
522 (if (not parent-item
) 0
523 ;; Compute indentation offset of the current item,
524 ;; that is the sum of the difference between its
525 ;; indentation and the indentation of the top item in
526 ;; the list and current item bullet's length. Also
527 ;; remove checkbox length, and tag length (for
528 ;; description lists) or bullet length.
529 (let ((struct (org-element-property :structure parent-item
))
530 (beg-item (org-element-property :begin parent-item
)))
531 (+ (- (org-list-get-ind beg-item struct
)
533 (org-list-get-top-point struct
) struct
))
534 (string-width (or (org-ascii--checkbox parent-item info
)
537 (or (org-list-get-tag beg-item struct
)
538 (org-list-get-bullet beg-item struct
)))))))))))))
540 (defun org-ascii--build-title
541 (element info text-width
&optional underline notags toc
)
542 "Format ELEMENT title and return it.
544 ELEMENT is either an `headline' or `inlinetask' element. INFO is
545 a plist used as a communication channel. TEXT-WIDTH is an
546 integer representing the maximum length of a line.
548 When optional argument UNDERLINE is non-nil, underline title,
549 without the tags, according to `org-ascii-underline'
552 If optional argument NOTAGS is non-nil, no tags will be added to
555 When optional argument TOC is non-nil, use optional title if
556 possible. It doesn't apply to `inlinetask' elements."
557 (let* ((headlinep (eq (org-element-type element
) 'headline
))
559 ;; Numbering is specific to headlines.
560 (and headlinep
(org-export-numbered-headline-p element info
)
561 ;; All tests passed: build numbering string.
565 (org-export-get-headline-number element info
) ".")
570 (if (and toc headlinep
) (org-export-get-alt-title element info
)
571 (org-element-property :title element
))
574 (and (plist-get info
:with-todo-keywords
)
575 (let ((todo (org-element-property :todo-keyword element
)))
576 (and todo
(concat (org-export-data todo info
) " ")))))
577 (tags (and (not notags
)
578 (plist-get info
:with-tags
)
579 (let ((tag-list (org-export-get-tags element info
)))
582 (mapconcat 'identity tag-list
":"))))))
584 (and (plist-get info
:with-priority
)
585 (let ((char (org-element-property :priority element
)))
586 (and char
(format "(#%c) " char
)))))
587 (first-part (concat numbers todo priority text
)))
590 ;; Align tags, if any.
594 (max (- text-width
(1+ (string-width first-part
)))
595 (string-width tags
)))
597 ;; Maybe underline text, if ELEMENT type is `headline' and an
598 ;; underline character has been defined.
599 (when (and underline headlinep
)
601 (nth (1- (org-export-get-relative-level element info
))
602 (cdr (assq (plist-get info
:ascii-charset
)
603 org-ascii-underline
)))))
606 (make-string (/ (string-width first-part
)
607 (char-width under-char
))
610 (defun org-ascii--has-caption-p (element info
)
611 "Non-nil when ELEMENT has a caption affiliated keyword.
612 INFO is a plist used as a communication channel. This function
613 is meant to be used as a predicate for `org-export-get-ordinal'."
614 (org-element-property :caption element
))
616 (defun org-ascii--build-caption (element info
)
617 "Return caption string for ELEMENT, if applicable.
619 INFO is a plist used as a communication channel.
621 The caption string contains the sequence number of ELEMENT along
622 with its real caption. Return nil when ELEMENT has no affiliated
624 (let ((caption (org-export-get-caption element
)))
626 ;; Get sequence number of current src-block among every
627 ;; src-block with a caption.
629 (org-export-get-ordinal
630 element info nil
'org-ascii--has-caption-p
))
631 (title-fmt (org-ascii--translate
632 (case (org-element-type element
)
634 (src-block "Listing %d:"))
636 (org-ascii--fill-string
637 (concat (format title-fmt reference
)
639 (org-export-data caption info
))
640 (org-ascii--current-text-width element info
) info
)))))
642 (defun org-ascii--build-toc (info &optional n keyword
)
643 "Return a table of contents.
645 INFO is a plist used as a communication channel.
647 Optional argument N, when non-nil, is an integer specifying the
650 Optional argument KEYWORD specifies the TOC keyword, if any, from
651 which the table of contents generation has been initiated."
652 (let ((title (org-ascii--translate "Table of Contents" info
)))
655 (make-string (string-width title
)
656 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
659 (if keyword
(org-ascii--current-text-width keyword info
)
660 (- org-ascii-text-width org-ascii-global-margin
))))
663 (let* ((level (org-export-get-relative-level headline info
))
664 (indent (* (1- level
) 3)))
666 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
667 (org-ascii--build-title
668 headline info
(- text-width indent
) nil
669 (or (not (plist-get info
:with-tags
))
670 (eq (plist-get info
:with-tags
) 'not-in-toc
))
672 (org-export-collect-headlines info n
) "\n")))))
674 (defun org-ascii--list-listings (keyword info
)
675 "Return a list of listings.
677 KEYWORD is the keyword that initiated the list of listings
678 generation. INFO is a plist used as a communication channel."
679 (let ((title (org-ascii--translate "List of Listings" info
)))
682 (make-string (string-width title
)
683 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
686 (if keyword
(org-ascii--current-text-width keyword info
)
687 (- org-ascii-text-width org-ascii-global-margin
)))
688 ;; Use a counter instead of retrieving ordinal of each
693 ;; Store initial text so its length can be computed. This is
694 ;; used to properly align caption right to it in case of
695 ;; filling (like contents of a description list item).
697 (format (org-ascii--translate "Listing %d:" info
)
699 (initial-width (string-width initial-text
)))
703 (org-ascii--indent-string
704 (org-ascii--fill-string
705 ;; Use short name in priority, if available.
706 (let ((caption (or (org-export-get-caption src-block t
)
707 (org-export-get-caption src-block
))))
708 (org-export-data caption info
))
709 (- text-width initial-width
) info
)
711 (org-export-collect-listings info
) "\n")))))
713 (defun org-ascii--list-tables (keyword info
)
714 "Return a list of tables.
716 KEYWORD is the keyword that initiated the list of tables
717 generation. INFO is a plist used as a communication channel."
718 (let ((title (org-ascii--translate "List of Tables" info
)))
721 (make-string (string-width title
)
722 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
725 (if keyword
(org-ascii--current-text-width keyword info
)
726 (- org-ascii-text-width org-ascii-global-margin
)))
727 ;; Use a counter instead of retrieving ordinal of each
732 ;; Store initial text so its length can be computed. This is
733 ;; used to properly align caption right to it in case of
734 ;; filling (like contents of a description list item).
736 (format (org-ascii--translate "Table %d:" info
)
738 (initial-width (string-width initial-text
)))
742 (org-ascii--indent-string
743 (org-ascii--fill-string
744 ;; Use short name in priority, if available.
745 (let ((caption (or (org-export-get-caption table t
)
746 (org-export-get-caption table
))))
747 (org-export-data caption info
))
748 (- text-width initial-width
) info
)
750 (org-export-collect-tables info
) "\n")))))
752 (defun org-ascii--unique-links (element info
)
753 "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 (cons (org-element-property :raw-link link
)
765 (org-element-contents link
))))
766 ;; Ignore LINK if it hasn't been translated already.
767 ;; It can happen if it is located in an affiliated
768 ;; keyword that was ignored.
769 (when (and (org-string-nw-p
770 (gethash link
(plist-get info
:exported-data
)))
771 (not (member footprint seen
)))
772 (push footprint seen
) link
)))))
773 ;; If at a section, find parent headline, if any, in order to
774 ;; count links that might be in the title.
776 (if (eq (org-element-type element
) 'headline
) element
777 (or (org-export-get-parent-headline element
) element
))))
778 ;; Get all links in HEADLINE.
779 (org-element-map headline
'link
780 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
782 (defun org-ascii--describe-links (links width info
)
783 "Return a string describing a list of links.
785 LINKS is a list of link type objects, as returned by
786 `org-ascii--unique-links'. WIDTH is the text width allowed for
787 the output string. INFO is a plist used as a communication
791 (let ((type (org-element-property :type link
))
792 (anchor (let ((desc (org-element-contents link
)))
793 (if desc
(org-export-data desc info
)
794 (org-element-property :raw-link link
)))))
796 ;; Coderefs, radio links and fuzzy links are ignored.
797 ((member type
'("coderef" "radio" "fuzzy")) nil
)
798 ;; Id and custom-id links: Headlines refer to their numbering.
799 ((member type
'("custom-id" "id"))
800 (let ((dest (org-export-resolve-id-link link info
)))
802 (org-ascii--fill-string
806 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
808 (org-ascii--translate "See section %s" info
)
809 (mapconcat 'number-to-string
810 (org-export-get-headline-number dest info
) "."))))
811 width info
) "\n\n")))
812 ;; Do not add a link that cannot be resolved and doesn't have
813 ;; any description: destination is already visible in the
815 ((not (org-element-contents link
)) nil
)
818 (org-ascii--fill-string
819 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
824 (defun org-ascii--checkbox (item info
)
825 "Return checkbox string for ITEM or nil.
826 INFO is a plist used as a communication channel."
827 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
828 (case (org-element-property :checkbox item
)
829 (on (if utf8p
"☑ " "[X] "))
830 (off (if utf8p
"☐ " "[ ] "))
831 (trans (if utf8p
"☒ " "[-] ")))))
837 (defun org-ascii-template--document-title (info)
838 "Return document title, as a string.
839 INFO is a plist used as a communication channel."
840 (let* ((text-width org-ascii-text-width
)
841 ;; Links in the title will not be resolved later, so we make
842 ;; sure their path is located right after them.
843 (org-ascii-links-to-notes nil
)
844 (title (org-export-data (plist-get info
:title
) info
))
845 (author (and (plist-get info
:with-author
)
846 (let ((auth (plist-get info
:author
)))
847 (and auth
(org-export-data auth info
)))))
848 (email (and (plist-get info
:with-email
)
849 (org-export-data (plist-get info
:email
) info
)))
850 (date (and (plist-get info
:with-date
)
851 (org-export-data (org-export-get-date info
) info
))))
852 ;; There are two types of title blocks depending on the presence
853 ;; of a title to display.
854 (if (string= title
"")
855 ;; Title block without a title. DATE is positioned at the top
856 ;; right of the document, AUTHOR to the top left and EMAIL
859 ((and (org-string-nw-p date
) (org-string-nw-p author
))
862 (make-string (- text-width
(string-width date
) (string-width author
))
865 (when (org-string-nw-p email
) (concat "\n" email
))
867 ((and (org-string-nw-p date
) (org-string-nw-p email
))
870 (make-string (- text-width
(string-width date
) (string-width email
))
873 ((org-string-nw-p date
)
875 (org-ascii--justify-string date text-width
'right
)
877 ((and (org-string-nw-p author
) (org-string-nw-p email
))
878 (concat author
"\n" email
"\n\n\n"))
879 ((org-string-nw-p author
) (concat author
"\n\n\n"))
880 ((org-string-nw-p email
) (concat email
"\n\n\n")))
881 ;; Title block with a title. Document's TITLE, along with the
882 ;; AUTHOR and its EMAIL are both overlined and an underlined,
883 ;; centered. Date is just below, also centered.
884 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
885 ;; Format TITLE. It may be filled if it is too wide,
886 ;; that is wider than the two thirds of the total width.
887 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
888 (formatted-title (org-ascii--fill-string title title-len info
))
891 (min (+ (max title-len
892 (string-width (or author
""))
893 (string-width (or email
"")))
895 text-width
) (if utf8p ?━ ?_
))))
896 (org-ascii--justify-string
899 (upcase formatted-title
)
901 ((and (org-string-nw-p author
) (org-string-nw-p email
))
902 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
903 ((org-string-nw-p author
)
904 (concat (if utf8p
"\n\n\n" "\n\n") author
))
905 ((org-string-nw-p email
)
906 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
908 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
909 "\n\n\n") text-width
'center
)))))
911 (defun org-ascii-inner-template (contents info
)
912 "Return complete document string after ASCII conversion.
913 CONTENTS is the transcoded contents string. INFO is a plist
914 holding export options."
915 (org-element-normalize-string
916 (org-ascii--indent-string
918 ;; 1. Document's body.
920 ;; 2. Footnote definitions.
921 (let ((definitions (org-export-collect-footnote-definitions
922 (plist-get info
:parse-tree
) info
))
923 ;; Insert full links right inside the footnote definition
924 ;; as they have no chance to be inserted later.
925 (org-ascii-links-to-notes nil
))
929 (let ((title (org-ascii--translate "Footnotes" info
)))
934 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
936 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
939 (let ((id (format "[%s] " (car ref
))))
940 ;; Distinguish between inline definitions and
941 ;; full-fledged definitions.
943 (let ((def (nth 2 ref
)))
944 (if (eq (org-element-type def
) 'org-data
)
945 ;; Full-fledged definition: footnote ID is
946 ;; inserted inside the first parsed paragraph
947 ;; (FIRST), if any, to be sure filling will
948 ;; take it into consideration.
949 (let ((first (car (org-element-contents def
))))
950 (if (not (eq (org-element-type first
) 'paragraph
))
951 (concat id
"\n" (org-export-data def info
))
952 (push id
(nthcdr 2 first
))
953 (org-export-data def info
)))
954 ;; Fill paragraph once footnote ID is inserted
955 ;; in order to have a correct length for first
957 (org-ascii--fill-string
958 (concat id
(org-export-data def info
))
959 text-width info
))))))
960 definitions
"\n\n"))))))
961 org-ascii-global-margin
)))
963 (defun org-ascii-template (contents info
)
964 "Return complete document string after ASCII conversion.
965 CONTENTS is the transcoded contents string. INFO is a plist
966 holding export options."
968 ;; 1. Build title block.
969 (org-ascii--indent-string
970 (concat (org-ascii-template--document-title info
)
971 ;; 2. Table of contents.
972 (let ((depth (plist-get info
:with-toc
)))
975 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
977 org-ascii-global-margin
)
978 ;; 3. Document's body.
980 ;; 4. Creator. Ignore `comment' value as there are no comments in
981 ;; ASCII. Justify it to the bottom right.
982 (org-ascii--indent-string
983 (let ((creator-info (plist-get info
:with-creator
))
984 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
985 (unless (or (not creator-info
) (eq creator-info
'comment
))
988 (org-ascii--fill-string
989 (plist-get info
:creator
) text-width info
'right
))))
990 org-ascii-global-margin
)))
992 (defun org-ascii--translate (s info
)
993 "Translate string S according to specified language and charset.
994 INFO is a plist used as a communication channel."
995 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
996 (org-export-translate s charset info
)))
1000 ;;; Transcode Functions
1004 (defun org-ascii-bold (bold contents info
)
1005 "Transcode BOLD from Org to ASCII.
1006 CONTENTS is the text with bold markup. INFO is a plist holding
1007 contextual information."
1008 (format "*%s*" contents
))
1013 (defun org-ascii-center-block (center-block contents info
)
1014 "Transcode a CENTER-BLOCK element from Org to ASCII.
1015 CONTENTS holds the contents of the block. INFO is a plist
1016 holding contextual information."
1017 (org-ascii--justify-string
1018 contents
(org-ascii--current-text-width center-block info
) 'center
))
1023 (defun org-ascii-clock (clock contents info
)
1024 "Transcode a CLOCK object from Org to ASCII.
1025 CONTENTS is nil. INFO is a plist holding contextual
1027 (concat org-clock-string
" "
1029 (org-element-property :raw-value
1030 (org-element-property :value clock
)))
1031 (let ((time (org-element-property :duration clock
)))
1036 (org-split-string time
":")))))))
1041 (defun org-ascii-code (code contents info
)
1042 "Return a CODE object from Org to ASCII.
1043 CONTENTS is nil. INFO is a plist holding contextual
1045 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1050 (defun org-ascii-drawer (drawer contents info
)
1051 "Transcode a DRAWER element from Org to ASCII.
1052 CONTENTS holds the contents of the block. INFO is a plist
1053 holding contextual information."
1054 (let ((name (org-element-property :drawer-name drawer
))
1055 (width (org-ascii--current-text-width drawer info
)))
1056 (funcall org-ascii-format-drawer-function name contents width
)))
1061 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1062 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1063 CONTENTS holds the contents of the block. INFO is a plist
1064 holding contextual information."
1070 (defun org-ascii-entity (entity contents info
)
1071 "Transcode an ENTITY object from Org to ASCII.
1072 CONTENTS are the definition itself. INFO is a plist holding
1073 contextual information."
1074 (org-element-property
1075 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1081 (defun org-ascii-example-block (example-block contents info
)
1082 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1083 CONTENTS is nil. INFO is a plist holding contextual information."
1084 (org-ascii--box-string
1085 (org-export-format-code-default example-block info
) info
))
1090 (defun org-ascii-export-snippet (export-snippet contents info
)
1091 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1092 CONTENTS is nil. INFO is a plist holding contextual information."
1093 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1094 (org-element-property :value export-snippet
)))
1099 (defun org-ascii-export-block (export-block contents info
)
1100 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1101 CONTENTS is nil. INFO is a plist holding contextual information."
1102 (when (string= (org-element-property :type export-block
) "ASCII")
1103 (org-remove-indentation (org-element-property :value export-block
))))
1108 (defun org-ascii-fixed-width (fixed-width contents info
)
1109 "Transcode a FIXED-WIDTH element from Org to ASCII.
1110 CONTENTS is nil. INFO is a plist holding contextual information."
1111 (org-ascii--box-string
1112 (org-remove-indentation
1113 (org-element-property :value fixed-width
)) info
))
1116 ;;;; Footnote Definition
1118 ;; Footnote Definitions are ignored. They are compiled at the end of
1119 ;; the document, by `org-ascii-inner-template'.
1122 ;;;; Footnote Reference
1124 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1125 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1126 CONTENTS is nil. INFO is a plist holding contextual information."
1127 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1132 (defun org-ascii-headline (headline contents info
)
1133 "Transcode a HEADLINE element from Org to ASCII.
1134 CONTENTS holds the contents of the headline. INFO is a plist
1135 holding contextual information."
1136 ;; Don't export footnote section, which will be handled at the end
1138 (unless (org-element-property :footnote-section-p headline
)
1139 (let* ((low-level-rank (org-export-low-level-p headline info
))
1140 (width (org-ascii--current-text-width headline info
))
1141 ;; Blank lines between headline and its contents.
1142 ;; `org-ascii-headline-spacing', when set, overwrites
1143 ;; original buffer's spacing.
1146 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1147 (org-element-property :pre-blank headline
)) ?
\n))
1148 ;; Even if HEADLINE has no section, there might be some
1149 ;; links in its title that we shouldn't forget to describe.
1151 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1152 (let ((title (org-element-property :title headline
)))
1154 (org-ascii--describe-links
1155 (org-ascii--unique-links title info
) width info
))))))
1156 ;; Deep subtree: export it as a list item.
1160 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1161 org-ascii-bullets
))))
1163 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1166 (org-ascii--build-title headline info width
) "\n"
1167 ;; Contents, indented by length of bullet.
1169 (org-ascii--indent-string
1171 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1173 ;; Else: Standard headline.
1175 (org-ascii--build-title headline info width
'underline
)
1177 (concat (when (org-string-nw-p links
) links
) contents
))))))
1180 ;;;; Horizontal Rule
1182 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1183 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1184 CONTENTS is nil. INFO is a plist holding contextual
1186 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1188 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1189 (org-ascii--justify-string
1190 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1191 (string-to-number spec-width
)
1193 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1194 text-width
'center
)))
1197 ;;;; Inline Src Block
1199 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1200 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1201 CONTENTS holds the contents of the item. INFO is a plist holding
1202 contextual information."
1203 (format org-ascii-verbatim-format
1204 (org-element-property :value inline-src-block
)))
1209 (defun org-ascii-format-inlinetask-default
1210 (todo type priority name tags contents width inlinetask info
)
1211 "Format an inline task element for ASCII export.
1212 See `org-ascii-format-inlinetask-function' for a description
1214 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1215 (width (or width org-ascii-inlinetask-width
)))
1216 (org-ascii--indent-string
1218 ;; Top line, with an additional blank line if not in UTF-8.
1219 (make-string width
(if utf8p ?━ ?_
)) "\n"
1220 (unless utf8p
(concat (make-string width ?
) "\n"))
1221 ;; Add title. Fill it if wider than inlinetask.
1222 (let ((title (org-ascii--build-title inlinetask info width
)))
1223 (if (<= (string-width title
) width
) title
1224 (org-ascii--fill-string title width info
)))
1226 ;; If CONTENTS is not empty, insert it along with
1228 (when (org-string-nw-p contents
)
1229 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1231 (make-string width
(if utf8p ?━ ?_
)))
1232 ;; Flush the inlinetask to the right.
1233 (- org-ascii-text-width org-ascii-global-margin
1234 (if (not (org-export-get-parent-headline inlinetask
)) 0
1235 org-ascii-inner-margin
)
1236 (org-ascii--current-text-width inlinetask info
)))))
1238 (defun org-ascii-inlinetask (inlinetask contents info
)
1239 "Transcode an INLINETASK element from Org to ASCII.
1240 CONTENTS holds the contents of the block. INFO is a plist
1241 holding contextual information."
1242 (let ((width (org-ascii--current-text-width inlinetask info
)))
1243 (funcall org-ascii-format-inlinetask-function
1245 (and (plist-get info
:with-todo-keywords
)
1246 (let ((todo (org-element-property
1247 :todo-keyword inlinetask
)))
1248 (and todo
(org-export-data todo info
))))
1250 (org-element-property :todo-type inlinetask
)
1252 (and (plist-get info
:with-priority
)
1253 (org-element-property :priority inlinetask
))
1255 (org-export-data (org-element-property :title inlinetask
) info
)
1257 (and (plist-get info
:with-tags
)
1258 (org-element-property :tags inlinetask
))
1259 ;; contents and width
1260 contents width inlinetask info
)))
1265 (defun org-ascii-italic (italic contents info
)
1266 "Transcode italic from Org to ASCII.
1267 CONTENTS is the text with italic markup. INFO is a plist holding
1268 contextual information."
1269 (format "/%s/" contents
))
1274 (defun org-ascii-item (item contents info
)
1275 "Transcode an ITEM element from Org to ASCII.
1276 CONTENTS holds the contents of the item. INFO is a plist holding
1277 contextual information."
1278 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1279 (checkbox (org-ascii--checkbox item info
))
1280 (list-type (org-element-property :type
(org-export-get-parent item
)))
1282 ;; First parent of ITEM is always the plain-list. Get
1283 ;; `:type' property from it.
1284 (org-list-bullet-string
1288 (org-export-data (org-element-property :tag item
) info
)
1291 ;; Return correct number for ITEM, paying attention to
1293 (let* ((struct (org-element-property :structure item
))
1294 (bul (org-element-property :bullet item
))
1295 (num (number-to-string
1296 (car (last (org-list-get-item-number
1297 (org-element-property :begin item
)
1299 (org-list-prevs-alist struct
)
1300 (org-list-parents-alist struct
)))))))
1301 (replace-regexp-in-string "[0-9]+" num bul
)))
1302 (t (let ((bul (org-element-property :bullet item
)))
1303 ;; Change bullets into more visible form if UTF-8 is active.
1305 (replace-regexp-in-string
1307 (replace-regexp-in-string
1309 (replace-regexp-in-string "*" "‣" bul
))))))))))
1312 (unless (eq list-type
'descriptive
) checkbox
)
1313 ;; Contents: Pay attention to indentation. Note: check-boxes are
1314 ;; already taken care of at the paragraph level so they don't
1315 ;; interfere with indentation.
1316 (let ((contents (org-ascii--indent-string contents
(string-width bullet
))))
1317 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1319 (concat "\n" contents
))))))
1324 (defun org-ascii-keyword (keyword contents info
)
1325 "Transcode a KEYWORD element from Org to ASCII.
1326 CONTENTS is nil. INFO is a plist holding contextual
1328 (let ((key (org-element-property :key keyword
))
1329 (value (org-element-property :value keyword
)))
1331 ((string= key
"ASCII") value
)
1332 ((string= key
"TOC")
1333 (let ((value (downcase value
)))
1335 ((string-match "\\<headlines\\>" value
)
1336 (let ((depth (or (and (string-match "[0-9]+" value
)
1337 (string-to-number (match-string 0 value
)))
1338 (plist-get info
:with-toc
))))
1339 (org-ascii--build-toc
1340 info
(and (wholenump depth
) depth
) keyword
)))
1341 ((string= "tables" value
)
1342 (org-ascii--list-tables keyword info
))
1343 ((string= "listings" value
)
1344 (org-ascii--list-listings keyword info
))))))))
1347 ;;;; Latex Environment
1349 (defun org-ascii-latex-environment (latex-environment contents info
)
1350 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1351 CONTENTS is nil. INFO is a plist holding contextual
1353 (when (plist-get info
:with-latex
)
1354 (org-remove-indentation (org-element-property :value latex-environment
))))
1359 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1360 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1361 CONTENTS is nil. INFO is a plist holding contextual
1363 (when (plist-get info
:with-latex
)
1364 (org-element-property :value latex-fragment
)))
1369 (defun org-ascii-line-break (line-break contents info
)
1370 "Transcode a LINE-BREAK object from Org to ASCII.
1371 CONTENTS is nil. INFO is a plist holding contextual
1372 information." hard-newline
)
1377 (defun org-ascii-link (link desc info
)
1378 "Transcode a LINK object from Org to ASCII.
1380 DESC is the description part of the link, or the empty string.
1381 INFO is a plist holding contextual information."
1382 (let ((raw-link (org-element-property :raw-link link
))
1383 (type (org-element-property :type link
)))
1385 ((string= type
"coderef")
1386 (let ((ref (org-element-property :path link
)))
1387 (format (org-export-get-coderef-format ref desc
)
1388 (org-export-resolve-coderef ref info
))))
1389 ;; Do not apply a special syntax on radio links. Though, use
1390 ;; transcoded target's contents as output.
1391 ((string= type
"radio")
1392 (let ((destination (org-export-resolve-radio-link link info
)))
1394 (org-export-data (org-element-contents destination
) info
))))
1395 ;; Do not apply a special syntax on fuzzy links pointing to
1397 ((string= type
"fuzzy")
1398 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1399 (if (org-string-nw-p desc
) desc
1402 (org-export-get-ordinal
1403 destination info nil
'org-ascii--has-caption-p
)))
1405 (if (atom number
) (number-to-string number
)
1406 (mapconcat 'number-to-string number
"."))))))))
1408 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1410 (format "[%s]" desc
)
1411 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1414 ;;;; Node Properties
1416 (defun org-ascii-node-property (node-property contents info
)
1417 "Transcode a NODE-PROPERTY element from Org to ASCII.
1418 CONTENTS is nil. INFO is a plist holding contextual
1421 (org-element-property :key node-property
)
1422 (let ((value (org-element-property :value node-property
)))
1423 (if value
(concat " " value
) ""))))
1428 (defun org-ascii-paragraph (paragraph contents info
)
1429 "Transcode a PARAGRAPH element from Org to ASCII.
1430 CONTENTS is the contents of the paragraph, as a string. INFO is
1431 the plist used as a communication channel."
1432 (let ((contents (if (not (wholenump org-ascii-indented-line-width
)) contents
1434 (make-string org-ascii-indented-line-width ?
)
1435 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1436 (org-ascii--fill-string
1437 contents
(org-ascii--current-text-width paragraph info
) info
)))
1442 (defun org-ascii-plain-list (plain-list contents info
)
1443 "Transcode a PLAIN-LIST element from Org to ASCII.
1444 CONTENTS is the contents of the list. INFO is a plist holding
1445 contextual information."
1451 (defun org-ascii-plain-text (text info
)
1452 "Transcode a TEXT string from Org to ASCII.
1453 INFO is a plist used as a communication channel."
1454 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1455 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1456 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1457 (if (not (plist-get info
:with-special-strings
)) text
1458 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1459 (if (not utf8p
) text
1460 ;; Usual replacements in utf-8 with proper option set.
1461 (replace-regexp-in-string
1463 (replace-regexp-in-string
1465 (replace-regexp-in-string "---" "—" text
)))))))
1470 (defun org-ascii-planning (planning contents info
)
1471 "Transcode a PLANNING element from Org to ASCII.
1472 CONTENTS is nil. INFO is a plist used as a communication
1477 (list (let ((closed (org-element-property :closed planning
)))
1479 (concat org-closed-string
" "
1481 (org-element-property :raw-value closed
)))))
1482 (let ((deadline (org-element-property :deadline planning
)))
1484 (concat org-deadline-string
" "
1486 (org-element-property :raw-value deadline
)))))
1487 (let ((scheduled (org-element-property :scheduled planning
)))
1489 (concat org-scheduled-string
" "
1491 (org-element-property :raw-value scheduled
)))))))
1495 ;;;; Property Drawer
1497 (defun org-ascii-property-drawer (property-drawer contents info
)
1498 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1499 CONTENTS holds the contents of the drawer. INFO is a plist
1500 holding contextual information."
1501 (org-string-nw-p contents
))
1506 (defun org-ascii-quote-block (quote-block contents info
)
1507 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1508 CONTENTS holds the contents of the block. INFO is a plist
1509 holding contextual information."
1510 (org-ascii--indent-string contents org-ascii-quote-margin
))
1515 (defun org-ascii-radio-target (radio-target contents info
)
1516 "Transcode a RADIO-TARGET object from Org to ASCII.
1517 CONTENTS is the contents of the target. INFO is a plist holding
1518 contextual information."
1524 (defun org-ascii-section (section contents info
)
1525 "Transcode a SECTION element from Org to ASCII.
1526 CONTENTS is the contents of the section. INFO is a plist holding
1527 contextual information."
1528 (org-ascii--indent-string
1531 (when org-ascii-links-to-notes
1532 ;; Add list of links at the end of SECTION.
1533 (let ((links (org-ascii--describe-links
1534 (org-ascii--unique-links section info
)
1535 (org-ascii--current-text-width section info
) info
)))
1536 ;; Separate list of links and section contents.
1537 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1538 ;; Do not apply inner margin if parent headline is low level.
1539 (let ((headline (org-export-get-parent-headline section
)))
1540 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1541 org-ascii-inner-margin
))))
1546 (defun org-ascii-special-block (special-block contents info
)
1547 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1548 CONTENTS holds the contents of the block. INFO is a plist
1549 holding contextual information."
1555 (defun org-ascii-src-block (src-block contents info
)
1556 "Transcode a SRC-BLOCK element from Org to ASCII.
1557 CONTENTS holds the contents of the item. INFO is a plist holding
1558 contextual information."
1559 (let ((caption (org-ascii--build-caption src-block info
))
1560 (code (org-export-format-code-default src-block info
)))
1561 (if (equal code
"") ""
1563 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1564 (org-ascii--box-string code info
)
1565 (when (and caption
(not org-ascii-caption-above
))
1566 (concat "\n" caption
))))))
1569 ;;;; Statistics Cookie
1571 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1572 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1573 CONTENTS is nil. INFO is a plist holding contextual information."
1574 (org-element-property :value statistics-cookie
))
1579 (defun org-ascii-subscript (subscript contents info
)
1580 "Transcode a SUBSCRIPT object from Org to ASCII.
1581 CONTENTS is the contents of the object. INFO is a plist holding
1582 contextual information."
1583 (if (org-element-property :use-brackets-p subscript
)
1584 (format "_{%s}" contents
)
1585 (format "_%s" contents
)))
1590 (defun org-ascii-superscript (superscript contents info
)
1591 "Transcode a SUPERSCRIPT object from Org to ASCII.
1592 CONTENTS is the contents of the object. INFO is a plist holding
1593 contextual information."
1594 (if (org-element-property :use-brackets-p superscript
)
1595 (format "_{%s}" contents
)
1596 (format "_%s" contents
)))
1601 (defun org-ascii-strike-through (strike-through contents info
)
1602 "Transcode STRIKE-THROUGH from Org to ASCII.
1603 CONTENTS is text with strike-through markup. INFO is a plist
1604 holding contextual information."
1605 (format "+%s+" contents
))
1610 (defun org-ascii-table (table contents info
)
1611 "Transcode a TABLE element from Org to ASCII.
1612 CONTENTS is the contents of the table. INFO is a plist holding
1613 contextual information."
1614 (let ((caption (org-ascii--build-caption table info
)))
1616 ;; Possibly add a caption string above.
1617 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1618 ;; Insert table. Note: "table.el" tables are left unmodified.
1619 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1620 ((and org-ascii-table-use-ascii-art
1621 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1622 (require 'ascii-art-to-unicode nil t
))
1624 (insert (org-remove-indentation
1625 (org-element-property :value table
)))
1626 (goto-char (point-min))
1628 (goto-char (point-max))
1629 (skip-chars-backward " \r\t\n")
1630 (buffer-substring (point-min) (point))))
1631 (t (org-remove-indentation (org-element-property :value table
))))
1632 ;; Possible add a caption string below.
1633 (and (not org-ascii-caption-above
) caption
))))
1638 (defun org-ascii--table-cell-width (table-cell info
)
1639 "Return width of TABLE-CELL.
1641 INFO is a plist used as a communication channel.
1643 Width of a cell is determined either by a width cookie in the
1644 same column as the cell, or by the maximum cell's length in that
1647 When `org-ascii-table-widen-columns' is non-nil, width cookies
1649 (let* ((row (org-export-get-parent table-cell
))
1650 (table (org-export-get-parent row
))
1651 (col (let ((cells (org-element-contents row
)))
1652 (- (length cells
) (length (memq table-cell cells
)))))
1654 (or (plist-get info
:ascii-table-cell-width-cache
)
1655 (plist-get (setq info
1656 (plist-put info
:ascii-table-cell-width-cache
1657 (make-hash-table :test
'equal
)))
1658 :ascii-table-cell-width-cache
)))
1659 (key (cons table col
)))
1660 (or (gethash key cache
)
1663 (or (and (not org-ascii-table-widen-columns
)
1664 (org-export-table-cell-width table-cell info
))
1665 (let* ((max-width 0))
1666 (org-element-map table
'table-row
1671 (org-element-contents
1672 (elt (org-element-contents row
) col
))
1679 (defun org-ascii-table-cell (table-cell contents info
)
1680 "Transcode a TABLE-CELL object from Org to ASCII.
1681 CONTENTS is the cell contents. INFO is a plist used as
1682 a communication channel."
1683 ;; Determine column width. When `org-ascii-table-widen-columns'
1684 ;; is nil and some width cookie has set it, use that value.
1685 ;; Otherwise, compute the maximum width among transcoded data of
1686 ;; each cell in the column.
1687 (let ((width (org-ascii--table-cell-width table-cell info
)))
1688 ;; When contents are too large, truncate them.
1689 (unless (or org-ascii-table-widen-columns
1690 (<= (string-width (or contents
"")) width
))
1691 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1692 ;; Align contents correctly within the cell.
1693 (let* ((indent-tabs-mode nil
)
1696 (org-ascii--justify-string
1698 (org-export-table-cell-alignment table-cell info
)))))
1701 (make-string (- width
(string-width (or data
""))) ?\s
))))
1703 (concat (format " %s " contents
)
1704 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1705 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1710 (defun org-ascii-table-row (table-row contents info
)
1711 "Transcode a TABLE-ROW element from Org to ASCII.
1712 CONTENTS is the row contents. INFO is a plist used as
1713 a communication channel."
1714 (when (eq (org-element-property :type table-row
) 'standard
)
1717 (lambda (lcorner horiz vert rcorner
)
1721 (org-element-map table-row
'table-cell
1723 (let ((width (org-ascii--table-cell-width cell info
))
1724 (borders (org-export-table-cell-borders cell info
)))
1726 ;; In order to know if CELL starts the row, do
1727 ;; not compare it with the first cell in the
1728 ;; row as there might be a special column.
1729 ;; Instead, compare it with first exportable
1730 ;; cell, obtained with `org-element-map'.
1731 (when (and (memq 'left borders
)
1732 (eq (org-element-map table-row
'table-cell
1736 (make-string (+ 2 width
) (string-to-char horiz
))
1738 ((not (memq 'right borders
)) nil
)
1739 ((eq (car (last (org-element-contents table-row
))) cell
)
1743 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1744 (borders (org-export-table-cell-borders
1745 (org-element-map table-row
'table-cell
'identity info t
)
1748 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1749 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1750 (funcall build-hline
"+" "-" "+" "+")))
1751 ((memq 'above borders
)
1752 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1753 (funcall build-hline
"+" "-" "+" "+"))))
1754 (when (memq 'left borders
) (if utf8p
"│" "|"))
1756 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1757 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1758 (funcall build-hline
"+" "-" "+" "+")))))))
1763 (defun org-ascii-timestamp (timestamp contents info
)
1764 "Transcode a TIMESTAMP object from Org to ASCII.
1765 CONTENTS is nil. INFO is a plist holding contextual information."
1766 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1771 (defun org-ascii-underline (underline contents info
)
1772 "Transcode UNDERLINE from Org to ASCII.
1773 CONTENTS is the text with underline markup. INFO is a plist
1774 holding contextual information."
1775 (format "_%s_" contents
))
1780 (defun org-ascii-verbatim (verbatim contents info
)
1781 "Return a VERBATIM object from Org to ASCII.
1782 CONTENTS is nil. INFO is a plist holding contextual information."
1783 (format org-ascii-verbatim-format
1784 (org-element-property :value verbatim
)))
1789 (defun org-ascii-verse-block (verse-block contents info
)
1790 "Transcode a VERSE-BLOCK element from Org to ASCII.
1791 CONTENTS is verse block contents. INFO is a plist holding
1792 contextual information."
1793 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1794 (org-ascii--indent-string
1795 (org-ascii--justify-string contents verse-width
'left
)
1796 org-ascii-quote-margin
)))
1802 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1803 "Filter controlling number of blank lines after a headline.
1805 HEADLINE is a string representing a transcoded headline.
1806 BACK-END is symbol specifying back-end used for export. INFO is
1807 plist containing the communication channel.
1809 This function only applies to `ascii' back-end. See
1810 `org-ascii-headline-spacing' for information."
1811 (if (not org-ascii-headline-spacing
) headline
1812 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1813 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1815 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1816 "Filter controlling number of blank lines between paragraphs.
1818 TREE is the parse tree. BACK-END is the symbol specifying
1819 back-end used for export. INFO is a plist used as
1820 a communication channel.
1822 See `org-ascii-paragraph-spacing' for information."
1823 (when (wholenump org-ascii-paragraph-spacing
)
1824 (org-element-map tree
'paragraph
1826 (when (eq (org-element-type (org-export-get-next-element p info
))
1828 (org-element-put-property
1829 p
:post-blank org-ascii-paragraph-spacing
)))))
1832 (defun org-ascii-filter-comment-spacing (tree backend info
)
1833 "Filter removing blank lines between comments.
1834 TREE is the parse tree. BACK-END is the symbol specifying
1835 back-end used for export. INFO is a plist used as
1836 a communication channel."
1837 (org-element-map tree
'(comment comment-block
)
1839 (when (memq (org-element-type (org-export-get-next-element c info
))
1840 '(comment comment-block
))
1841 (org-element-put-property c
:post-blank
0))))
1846 ;;; End-user functions
1849 (defun org-ascii-export-as-ascii
1850 (&optional async subtreep visible-only body-only ext-plist
)
1851 "Export current buffer to a text buffer.
1853 If narrowing is active in the current buffer, only export its
1856 If a region is active, export that region.
1858 A non-nil optional argument ASYNC means the process should happen
1859 asynchronously. The resulting buffer should be accessible
1860 through the `org-export-stack' interface.
1862 When optional argument SUBTREEP is non-nil, export the sub-tree
1863 at point, extracting information from the headline properties
1866 When optional argument VISIBLE-ONLY is non-nil, don't export
1867 contents of hidden elements.
1869 When optional argument BODY-ONLY is non-nil, strip title and
1870 table of contents from output.
1872 EXT-PLIST, when provided, is a property list with external
1873 parameters overriding Org default settings, but still inferior to
1874 file-local settings.
1876 Export is done in a buffer named \"*Org ASCII Export*\", which
1877 will be displayed when `org-export-show-temporary-export-buffer'
1880 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
1881 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
1884 (defun org-ascii-export-to-ascii
1885 (&optional async subtreep visible-only body-only ext-plist
)
1886 "Export current buffer to a text file.
1888 If narrowing is active in the current buffer, only export its
1891 If a region is active, export that region.
1893 A non-nil optional argument ASYNC means the process should happen
1894 asynchronously. The resulting file should be accessible through
1895 the `org-export-stack' interface.
1897 When optional argument SUBTREEP is non-nil, export the sub-tree
1898 at point, extracting information from the headline properties
1901 When optional argument VISIBLE-ONLY is non-nil, don't export
1902 contents of hidden elements.
1904 When optional argument BODY-ONLY is non-nil, strip title and
1905 table of contents from output.
1907 EXT-PLIST, when provided, is a property list with external
1908 parameters overriding Org default settings, but still inferior to
1909 file-local settings.
1911 Return output file's name."
1913 (let ((file (org-export-output-file-name ".txt" subtreep
)))
1914 (org-export-to-file 'ascii file
1915 async subtreep visible-only body-only ext-plist
)))
1918 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1919 "Publish an Org file to ASCII.
1921 FILENAME is the filename of the Org file to be published. PLIST
1922 is the property list for the given project. PUB-DIR is the
1923 publishing directory.
1925 Return output file name."
1927 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1930 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1931 "Publish an Org file to Latin-1.
1933 FILENAME is the filename of the Org file to be published. PLIST
1934 is the property list for the given project. PUB-DIR is the
1935 publishing directory.
1937 Return output file name."
1939 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1942 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1943 "Publish an org file to UTF-8.
1945 FILENAME is the filename of the Org file to be published. PLIST
1946 is the property list for the given project. PUB-DIR is the
1947 publishing directory.
1949 Return output file name."
1951 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1957 ;; generated-autoload-file: "org-loaddefs.el"
1958 ;; coding: utf-8-emacs
1961 ;;; ox-ascii.el ends here