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
))
124 '((:ascii-bullets nil nil org-ascii-bullets
)
125 (:ascii-caption-above nil nil org-ascii-caption-above
)
126 (:ascii-charset nil nil org-ascii-charset
)
127 (:ascii-global-margin nil nil org-ascii-global-margin
)
128 (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function
)
129 (:ascii-format-inlinetask-function
130 nil nil org-ascii-format-inlinetask-function
)
131 (:ascii-headline-spacing nil nil org-ascii-headline-spacing
)
132 (:ascii-indented-line-width nil nil org-ascii-indented-line-width
)
133 (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width
)
134 (:ascii-inner-margin nil nil org-ascii-inner-margin
)
135 (:ascii-links-to-notes nil nil org-ascii-links-to-notes
)
136 (:ascii-list-margin nil nil org-ascii-list-margin
)
137 (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing
)
138 (:ascii-quote-margin nil nil org-ascii-quote-margin
)
139 (:ascii-table-keep-all-vertical-lines
140 nil nil org-ascii-table-keep-all-vertical-lines
)
141 (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art
)
142 (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns
)
143 (:ascii-text-width nil nil org-ascii-text-width
)
144 (:ascii-underline nil nil org-ascii-underline
)
145 (:ascii-verbatim-format nil nil org-ascii-verbatim-format
)))
149 ;;; User Configurable Variables
151 (defgroup org-export-ascii nil
152 "Options for exporting Org mode files to ASCII."
153 :tag
"Org Export ASCII"
156 (defcustom org-ascii-text-width
72
157 "Maximum width of exported text.
158 This number includes margin size, as set in
159 `org-ascii-global-margin'."
160 :group
'org-export-ascii
162 :package-version
'(Org .
"8.0")
165 (defcustom org-ascii-global-margin
0
166 "Width of the left margin, in number of characters."
167 :group
'org-export-ascii
169 :package-version
'(Org .
"8.0")
172 (defcustom org-ascii-inner-margin
2
173 "Width of the inner margin, in number of characters.
174 Inner margin is applied between each headline."
175 :group
'org-export-ascii
177 :package-version
'(Org .
"8.0")
180 (defcustom org-ascii-quote-margin
6
181 "Width of margin used for quoting text, in characters.
182 This margin is applied on both sides of the text."
183 :group
'org-export-ascii
185 :package-version
'(Org .
"8.0")
188 (defcustom org-ascii-list-margin
0
189 "Width of margin used for plain lists, in characters.
190 This margin applies to top level list only, not to its
192 :group
'org-export-ascii
194 :package-version
'(Org .
"8.3")
197 (defcustom org-ascii-inlinetask-width
30
198 "Width of inline tasks, in number of characters.
199 This number ignores any margin."
200 :group
'org-export-ascii
202 :package-version
'(Org .
"8.0")
205 (defcustom org-ascii-headline-spacing
'(1 .
2)
206 "Number of blank lines inserted around headlines.
208 This variable can be set to a cons cell. In that case, its car
209 represents the number of blank lines present before headline
210 contents whereas its cdr reflects the number of blank lines after
213 A nil value replicates the number of blank lines found in the
214 original Org buffer at the same place."
215 :group
'org-export-ascii
217 :package-version
'(Org .
"8.0")
219 (const :tag
"Replicate original spacing" nil
)
220 (cons :tag
"Set an uniform spacing"
221 (integer :tag
"Number of blank lines before contents")
222 (integer :tag
"Number of blank lines after contents"))))
224 (defcustom org-ascii-indented-line-width
'auto
225 "Additional indentation width for the first line in a paragraph.
226 If the value is an integer, indent the first line of each
227 paragraph by this width, unless it is located at the beginning of
228 a section, in which case indentation is removed from that line.
229 If it is the symbol `auto' preserve indentation from original
231 :group
'org-export-ascii
233 :package-version
'(Org .
"8.0")
235 (integer :tag
"Number of white spaces characters")
236 (const :tag
"Preserve original width" auto
)))
238 (defcustom org-ascii-paragraph-spacing
'auto
239 "Number of white lines between paragraphs.
240 If the value is an integer, add this number of blank lines
241 between contiguous paragraphs. If is it the symbol `auto', keep
242 the same number of blank lines as in the original document."
243 :group
'org-export-ascii
245 :package-version
'(Org .
"8.0")
247 (integer :tag
"Number of blank lines")
248 (const :tag
"Preserve original spacing" auto
)))
250 (defcustom org-ascii-charset
'ascii
251 "The charset allowed to represent various elements and objects.
253 `ascii' Only use plain ASCII characters
254 `latin1' Include Latin-1 characters
255 `utf-8' Use all UTF-8 characters"
256 :group
'org-export-ascii
258 :package-version
'(Org .
"8.0")
260 (const :tag
"ASCII" ascii
)
261 (const :tag
"Latin-1" latin1
)
262 (const :tag
"UTF-8" utf-8
)))
264 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
266 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
267 "Characters for underlining headings in ASCII export.
269 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
270 and whose value is a list of characters.
272 For each supported charset, this variable associates a sequence
273 of underline characters. In a sequence, the characters will be
274 used in order for headlines level 1, 2, ... If no character is
275 available for a given level, the headline won't be underlined."
276 :group
'org-export-ascii
278 :package-version
'(Org .
"8.0")
280 (cons :tag
"Underline characters sequence"
281 (const :tag
"ASCII charset" ascii
)
283 (cons :tag
"Underline characters sequence"
284 (const :tag
"Latin-1 charset" latin1
)
286 (cons :tag
"Underline characters sequence"
287 (const :tag
"UTF-8 charset" utf-8
)
288 (repeat character
))))
290 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
293 "Bullet characters for headlines converted to lists in ASCII export.
295 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
296 and whose value is a list of characters.
298 The first character is used for the first level considered as low
299 level, and so on. If there are more levels than characters given
300 here, the list will be repeated.
302 Note that this variable doesn't affect plain lists
304 :group
'org-export-ascii
306 :package-version
'(Org .
"8.0")
308 (cons :tag
"Bullet characters for low level headlines"
309 (const :tag
"ASCII charset" ascii
)
311 (cons :tag
"Bullet characters for low level headlines"
312 (const :tag
"Latin-1 charset" latin1
)
314 (cons :tag
"Bullet characters for low level headlines"
315 (const :tag
"UTF-8 charset" utf-8
)
316 (repeat character
))))
318 (defcustom org-ascii-links-to-notes t
319 "Non-nil means convert links to notes before the next headline.
320 When nil, the link will be exported in place. If the line
321 becomes long in this way, it will be wrapped."
322 :group
'org-export-ascii
324 :package-version
'(Org .
"8.0")
327 (defcustom org-ascii-table-keep-all-vertical-lines nil
328 "Non-nil means keep all vertical lines in ASCII tables.
329 When nil, vertical lines will be removed except for those needed
330 for column grouping."
331 :group
'org-export-ascii
333 :package-version
'(Org .
"8.0")
336 (defcustom org-ascii-table-widen-columns t
337 "Non-nil means widen narrowed columns for export.
338 When nil, narrowed columns will look in ASCII export just like in
339 Org mode, i.e. with \"=>\" as ellipsis."
340 :group
'org-export-ascii
342 :package-version
'(Org .
"8.0")
345 (defcustom org-ascii-table-use-ascii-art nil
346 "Non-nil means table.el tables are turned into ascii-art.
348 It only makes sense when export charset is `utf-8'. It is nil by
349 default since it requires ascii-art-to-unicode.el package. You
350 can download it here:
352 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
353 :group
'org-export-ascii
355 :package-version
'(Org .
"8.0")
358 (defcustom org-ascii-caption-above nil
359 "When non-nil, place caption string before the element.
360 Otherwise, place it right after it."
361 :group
'org-export-ascii
363 :package-version
'(Org .
"8.0")
366 (defcustom org-ascii-verbatim-format
"`%s'"
367 "Format string used for verbatim text and inline code."
368 :group
'org-export-ascii
370 :package-version
'(Org .
"8.0")
373 (defcustom org-ascii-format-drawer-function
374 (lambda (name contents width
) contents
)
375 "Function called to format a drawer in ASCII.
377 The function must accept three parameters:
378 NAME the drawer name, like \"LOGBOOK\"
379 CONTENTS the contents of the drawer.
380 WIDTH the text width within the drawer.
382 The function should return either the string to be exported or
383 nil to ignore the drawer.
385 The default value simply returns the value of CONTENTS."
386 :group
'org-export-ascii
388 :package-version
'(Org .
"8.0")
391 (defcustom org-ascii-format-inlinetask-function
392 'org-ascii-format-inlinetask-default
393 "Function called to format an inlinetask in ASCII.
395 The function must accept nine parameters:
396 TODO the todo keyword, as a string
397 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
398 PRIORITY the inlinetask priority, as a string
399 NAME the inlinetask name, as a string.
400 TAGS the inlinetask tags, as a list of strings.
401 CONTENTS the contents of the inlinetask, as a string.
402 WIDTH the width of the inlinetask, as a number.
403 INLINETASK the inlinetask itself.
404 INFO the info channel.
406 The function should return either the string to be exported or
407 nil to ignore the inline task."
408 :group
'org-export-ascii
410 :package-version
'(Org .
"8.3")
415 ;;; Internal Functions
417 ;; Internal functions fall into three categories.
419 ;; The first one is about text formatting. The core functions are
420 ;; `org-ascii--current-text-width' and
421 ;; `org-ascii--current-justification', which determine, respectively,
422 ;; the current text width allowed to a given element and its expected
423 ;; justification. Once this information is known,
424 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
425 ;; `org-ascii--justify-element' `org-ascii--box-string' and
426 ;; `org-ascii--indent-string' can operate on a given output string.
427 ;; In particular, justification happens at the regular (i.e.,
428 ;; non-greater) element level, which means that when the exporting
429 ;; process reaches a container (e.g., a center block) content are
430 ;; already justified.
432 ;; The second category contains functions handling elements listings,
433 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
434 ;; returns a complete table of contents, `org-ascii--list-listings'
435 ;; returns a list of referenceable src-block elements, and
436 ;; `org-ascii--list-tables' does the same for table elements.
438 ;; The third category includes general helper functions.
439 ;; `org-ascii--build-title' creates the title for a given headline
440 ;; or inlinetask element. `org-ascii--build-caption' returns the
441 ;; caption string associated to a table or a src-block.
442 ;; `org-ascii--describe-links' creates notes about links for
443 ;; insertion at the end of a section. It uses
444 ;; `org-ascii--unique-links' to get the list of links to describe.
445 ;; Eventually, `org-ascii--translate' translates a string according
446 ;; to language and charset specification.
449 (defun org-ascii--fill-string (s text-width info
&optional justify
)
450 "Fill a string with specified text-width and return it.
452 S is the string being filled. TEXT-WIDTH is an integer
453 specifying maximum length of a line. INFO is the plist used as
454 a communication channel.
456 Optional argument JUSTIFY can specify any type of justification
457 among `left', `center', `right' or `full'. A nil value is
458 equivalent to `left'. For a justification that doesn't also fill
459 string, see `org-ascii--justify-lines' and
460 `org-ascii--justify-block'.
462 Return nil if S isn't a string."
464 (let ((double-space-p sentence-end-double-space
))
466 (let ((fill-column text-width
)
467 (use-hard-newlines t
)
468 (sentence-end-double-space double-space-p
))
469 (insert (if (plist-get info
:preserve-breaks
)
470 (replace-regexp-in-string "\n" hard-newline s
)
472 (fill-region (point-min) (point-max) justify
))
475 (defun org-ascii--justify-lines (s text-width how
)
476 "Justify all lines in string S.
477 TEXT-WIDTH is an integer specifying maximum length of a line.
478 HOW determines the type of justification: it can be `left',
479 `right', `full' or `center'."
482 (goto-char (point-min))
483 (let ((fill-column text-width
)
484 ;; Disable `adaptive-fill-mode' so it doesn't prevent
485 ;; filling lines matching `adaptive-fill-regexp'.
486 (adaptive-fill-mode nil
))
487 (while (< (point) (point-max))
488 (justify-current-line how
)
492 (defun org-ascii--justify-element (contents element info
)
493 "Justify CONTENTS of ELEMENT.
494 INFO is a plist used as a communication channel. Justification
495 is done according to the type of element. More accurately,
496 paragraphs are filled and other elements are justified as blocks,
497 that is according to the widest non blank line in CONTENTS."
498 (if (not (org-string-nw-p contents
)) contents
499 (let ((text-width (org-ascii--current-text-width element info
))
500 (how (org-ascii--current-justification element
)))
502 ((eq (org-element-type element
) 'paragraph
)
503 ;; Paragraphs are treated specially as they need to be filled.
504 (org-ascii--fill-string contents text-width info how
))
505 ((eq how
'left
) contents
)
508 (goto-char (point-min))
511 ;; Compute maximum width. Bail out if it is greater
512 ;; than page width, since no justification is
516 (unless (org-looking-at-p "[ \t]*$")
518 (let ((column (current-column)))
520 ((>= column text-width
) (throw 'exit contents
))
521 ((> column max-width
) (setq max-width column
)))))
523 ;; Justify every line according to TEXT-WIDTH and
525 (let ((offset (/ (- text-width max-width
)
526 (if (eq how
'right
) 1 2))))
527 (if (zerop offset
) (throw 'exit contents
)
529 (unless (org-looking-at-p "[ \t]*$")
530 (org-indent-to-column offset
))
532 (buffer-string))))))))
534 (defun org-ascii--indent-string (s width
)
535 "Indent string S by WIDTH white spaces.
536 Empty lines are not indented."
538 (replace-regexp-in-string
539 "\\(^\\)[ \t]*\\S-" (make-string width ?\s
) s nil nil
1)))
541 (defun org-ascii--box-string (s info
)
542 "Return string S with a partial box to its left.
543 INFO is a plist used as a communication channel."
544 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
545 (format (if utf8p
"┌────\n%s\n└────" ",----\n%s\n`----")
546 (replace-regexp-in-string
547 "^" (if utf8p
"│ " "| ")
548 ;; Remove last newline character.
549 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
551 (defun org-ascii--current-text-width (element info
)
552 "Return maximum text width for ELEMENT's contents.
553 INFO is a plist used as a communication channel."
554 (case (org-element-type element
)
555 ;; Elements with an absolute width: `headline' and `inlinetask'.
556 (inlinetask (plist-get info
:ascii-inlinetask-width
))
558 (- (plist-get info
:ascii-text-width
)
559 (let ((low-level-rank (org-export-low-level-p element info
)))
560 (if low-level-rank
(* low-level-rank
2)
561 (plist-get info
:ascii-global-margin
)))))
562 ;; Elements with a relative width: store maximum text width in
565 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
566 ;; Total width is determined by the presence, or not, of an
567 ;; inline task among ELEMENT parents.
569 (if (loop for parent in genealogy
570 thereis
(eq (org-element-type parent
) 'inlinetask
))
571 (plist-get info
:ascii-inlinetask-width
)
572 ;; No inlinetask: Remove global margin from text width.
573 (- (plist-get info
:ascii-text-width
)
574 (plist-get info
:ascii-global-margin
)
575 (let ((parent (org-export-get-parent-headline element
)))
576 ;; Inner margin doesn't apply to text before first
579 (let ((low-level-rank
580 (org-export-low-level-p parent info
)))
581 ;; Inner margin doesn't apply to contents of
582 ;; low level headlines, since they've got their
583 ;; own indentation mechanism.
584 (if low-level-rank
(* low-level-rank
2)
585 (plist-get info
:ascii-inner-margin
)))))))))
587 ;; Each `quote-block' and `verse-block' above narrows text
588 ;; width by twice the standard margin size.
589 (+ (* (loop for parent in genealogy
590 when
(memq (org-element-type parent
)
591 '(quote-block verse-block
))
593 2 (plist-get info
:ascii-quote-margin
))
594 ;; Apply list margin once per "top-level" plain-list
595 ;; containing current line
597 (dolist (e genealogy count
)
598 (and (eq (org-element-type e
) 'plain-list
)
599 (not (eq (org-element-type (org-export-get-parent e
))
602 (plist-get info
:ascii-list-margin
))
603 ;; Text width within a plain-list is restricted by
604 ;; indentation of current item. If that's the case,
605 ;; compute it with the help of `:structure' property from
606 ;; parent item, if any.
608 (if (eq (org-element-type element
) 'item
) element
609 (loop for parent in genealogy
610 when
(eq (org-element-type parent
) 'item
)
612 (if (not parent-item
) 0
613 ;; Compute indentation offset of the current item,
614 ;; that is the sum of the difference between its
615 ;; indentation and the indentation of the top item in
616 ;; the list and current item bullet's length. Also
617 ;; remove checkbox length, and tag length (for
618 ;; description lists) or bullet length.
619 (let ((struct (org-element-property :structure parent-item
))
620 (beg-item (org-element-property :begin parent-item
)))
621 (+ (- (org-list-get-ind beg-item struct
)
623 (org-list-get-top-point struct
) struct
))
624 (string-width (or (org-ascii--checkbox parent-item info
)
627 (or (org-list-get-tag beg-item struct
)
628 (org-list-get-bullet beg-item struct
)))))))))))))
630 (defun org-ascii--current-justification (element)
631 "Return expected justification for ELEMENT's contents.
632 Return value is a symbol among `left', `center', `right' and
635 (while (and (not justification
)
636 (setq element
(org-element-property :parent element
)))
637 (case (org-element-type element
)
638 (center-block (setq justification
'center
))
640 (let ((name (org-element-property :type element
)))
641 (cond ((string= name
"JUSTIFYRIGHT") (setq justification
'right
))
642 ((string= name
"JUSTIFYLEFT") (setq justification
'left
)))))))
643 (or justification
'left
)))
645 (defun org-ascii--build-title
646 (element info text-width
&optional underline notags toc
)
647 "Format ELEMENT title and return it.
649 ELEMENT is either an `headline' or `inlinetask' element. INFO is
650 a plist used as a communication channel. TEXT-WIDTH is an
651 integer representing the maximum length of a line.
653 When optional argument UNDERLINE is non-nil, underline title,
654 without the tags, according to `org-ascii-underline'
657 If optional argument NOTAGS is non-nil, no tags will be added to
660 When optional argument TOC is non-nil, use optional title if
661 possible. It doesn't apply to `inlinetask' elements."
662 (let* ((headlinep (eq (org-element-type element
) 'headline
))
664 ;; Numbering is specific to headlines.
665 (and headlinep
(org-export-numbered-headline-p element info
)
666 ;; All tests passed: build numbering string.
670 (org-export-get-headline-number element info
) ".")
675 (if (and toc headlinep
) (org-export-get-alt-title element info
)
676 (org-element-property :title element
))
679 (and (plist-get info
:with-todo-keywords
)
680 (let ((todo (org-element-property :todo-keyword element
)))
681 (and todo
(concat (org-export-data todo info
) " ")))))
682 (tags (and (not notags
)
683 (plist-get info
:with-tags
)
684 (let ((tag-list (org-export-get-tags element info
)))
687 (mapconcat 'identity tag-list
":"))))))
689 (and (plist-get info
:with-priority
)
690 (let ((char (org-element-property :priority element
)))
691 (and char
(format "(#%c) " char
)))))
692 (first-part (concat numbers todo priority text
)))
695 ;; Align tags, if any.
699 (max (- text-width
(1+ (string-width first-part
)))
700 (string-width tags
)))
702 ;; Maybe underline text, if ELEMENT type is `headline' and an
703 ;; underline character has been defined.
704 (when (and underline headlinep
)
706 (nth (1- (org-export-get-relative-level element info
))
707 (cdr (assq (plist-get info
:ascii-charset
)
708 (plist-get info
:ascii-underline
))))))
711 (make-string (/ (string-width first-part
)
712 (char-width under-char
))
715 (defun org-ascii--has-caption-p (element info
)
716 "Non-nil when ELEMENT has a caption affiliated keyword.
717 INFO is a plist used as a communication channel. This function
718 is meant to be used as a predicate for `org-export-get-ordinal'."
719 (org-element-property :caption element
))
721 (defun org-ascii--build-caption (element info
)
722 "Return caption string for ELEMENT, if applicable.
724 INFO is a plist used as a communication channel.
726 The caption string contains the sequence number of ELEMENT along
727 with its real caption. Return nil when ELEMENT has no affiliated
729 (let ((caption (org-export-get-caption element
)))
731 ;; Get sequence number of current src-block among every
732 ;; src-block with a caption.
734 (org-export-get-ordinal
735 element info nil
'org-ascii--has-caption-p
))
736 (title-fmt (org-ascii--translate
737 (case (org-element-type element
)
739 (src-block "Listing %d:"))
741 (org-ascii--fill-string
742 (concat (format title-fmt reference
)
744 (org-export-data caption info
))
745 (org-ascii--current-text-width element info
) info
)))))
747 (defun org-ascii--build-toc (info &optional n keyword
)
748 "Return a table of contents.
750 INFO is a plist used as a communication channel.
752 Optional argument N, when non-nil, is an integer specifying the
755 Optional argument KEYWORD specifies the TOC keyword, if any, from
756 which the table of contents generation has been initiated."
757 (let ((title (org-ascii--translate "Table of Contents" info
)))
760 (make-string (string-width title
)
761 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
764 (if keyword
(org-ascii--current-text-width keyword info
)
765 (- (plist-get info
:ascii-text-width
)
766 (plist-get info
:ascii-global-margin
)))))
769 (let* ((level (org-export-get-relative-level headline info
))
770 (indent (* (1- level
) 3)))
772 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
773 (org-ascii--build-title
774 headline info
(- text-width indent
) nil
775 (or (not (plist-get info
:with-tags
))
776 (eq (plist-get info
:with-tags
) 'not-in-toc
))
778 (org-export-collect-headlines info n
) "\n")))))
780 (defun org-ascii--list-listings (keyword info
)
781 "Return a list of listings.
783 KEYWORD is the keyword that initiated the list of listings
784 generation. INFO is a plist used as a communication channel."
785 (let ((title (org-ascii--translate "List of Listings" info
)))
788 (make-string (string-width title
)
789 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
792 (if keyword
(org-ascii--current-text-width keyword info
)
793 (- (plist-get info
:ascii-text-width
)
794 (plist-get info
:ascii-global-margin
))))
795 ;; Use a counter instead of retrieving ordinal of each
800 ;; Store initial text so its length can be computed. This is
801 ;; used to properly align caption right to it in case of
802 ;; filling (like contents of a description list item).
804 (format (org-ascii--translate "Listing %d:" info
)
806 (initial-width (string-width initial-text
)))
810 (org-ascii--indent-string
811 (org-ascii--fill-string
812 ;; Use short name in priority, if available.
813 (let ((caption (or (org-export-get-caption src-block t
)
814 (org-export-get-caption src-block
))))
815 (org-export-data caption info
))
816 (- text-width initial-width
) info
)
818 (org-export-collect-listings info
) "\n")))))
820 (defun org-ascii--list-tables (keyword info
)
821 "Return a list of tables.
823 KEYWORD is the keyword that initiated the list of tables
824 generation. INFO is a plist used as a communication channel."
825 (let ((title (org-ascii--translate "List of Tables" info
)))
828 (make-string (string-width title
)
829 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
832 (if keyword
(org-ascii--current-text-width keyword info
)
833 (- (plist-get info
:ascii-text-width
)
834 (plist-get info
:ascii-global-margin
))))
835 ;; Use a counter instead of retrieving ordinal of each
840 ;; Store initial text so its length can be computed. This is
841 ;; used to properly align caption right to it in case of
842 ;; filling (like contents of a description list item).
844 (format (org-ascii--translate "Table %d:" info
)
846 (initial-width (string-width initial-text
)))
850 (org-ascii--indent-string
851 (org-ascii--fill-string
852 ;; Use short name in priority, if available.
853 (let ((caption (or (org-export-get-caption table t
)
854 (org-export-get-caption table
))))
855 (org-export-data caption info
))
856 (- text-width initial-width
) info
)
858 (org-export-collect-tables info
) "\n")))))
860 (defun org-ascii--unique-links (element info
)
861 "Return a list of unique link references in ELEMENT.
862 ELEMENT is either a headline element or a section element. INFO
863 is a plist used as a communication channel."
867 ;; Return LINK if it wasn't referenced so far, or nil.
868 ;; Update SEEN links along the way.
871 ;; Normalize description in footprints.
872 (cons (org-element-property :raw-link link
)
873 (let ((contents (org-element-contents link
)))
875 (replace-regexp-in-string
878 (org-element-interpret-data contents
))))))))
879 ;; Ignore LINK if it hasn't been translated already.
880 ;; It can happen if it is located in an affiliated
881 ;; keyword that was ignored.
882 (when (and (org-string-nw-p
883 (gethash link
(plist-get info
:exported-data
)))
884 (not (member footprint seen
)))
885 (push footprint seen
) link
)))))
886 ;; If at a section, find parent headline, if any, in order to
887 ;; count links that might be in the title.
889 (if (eq (org-element-type element
) 'headline
) element
890 (or (org-export-get-parent-headline element
) element
))))
891 ;; Get all links in HEADLINE.
892 (org-element-map headline
'link
893 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
895 (defun org-ascii--describe-links (links width info
)
896 "Return a string describing a list of links.
898 LINKS is a list of link type objects, as returned by
899 `org-ascii--unique-links'. WIDTH is the text width allowed for
900 the output string. INFO is a plist used as a communication
904 (let ((type (org-element-property :type link
))
905 (anchor (let ((desc (org-element-contents link
)))
906 (if desc
(org-export-data desc info
)
907 (org-element-property :raw-link link
)))))
909 ;; Coderefs, radio links and fuzzy links are ignored.
910 ((member type
'("coderef" "radio" "fuzzy")) nil
)
911 ;; Id and custom-id links: Headlines refer to their numbering.
912 ((member type
'("custom-id" "id"))
913 (let ((dest (org-export-resolve-id-link link info
)))
915 (org-ascii--fill-string
919 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
921 (org-ascii--translate "See section %s" info
)
922 (mapconcat 'number-to-string
923 (org-export-get-headline-number dest info
) "."))))
924 width info
) "\n\n")))
925 ;; Do not add a link that cannot be resolved and doesn't have
926 ;; any description: destination is already visible in the
928 ((not (org-element-contents link
)) nil
)
931 (org-ascii--fill-string
932 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
937 (defun org-ascii--checkbox (item info
)
938 "Return checkbox string for ITEM or nil.
939 INFO is a plist used as a communication channel."
940 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
941 (case (org-element-property :checkbox item
)
942 (on (if utf8p
"☑ " "[X] "))
943 (off (if utf8p
"☐ " "[ ] "))
944 (trans (if utf8p
"☒ " "[-] ")))))
950 (defun org-ascii-template--document-title (info)
951 "Return document title, as a string.
952 INFO is a plist used as a communication channel."
953 (let* ((text-width (plist-get info
:ascii-text-width
))
954 ;; Links in the title will not be resolved later, so we make
955 ;; sure their path is located right after them.
956 (info (org-combine-plists info
'(:ascii-links-to-notes nil
)))
957 (title (org-export-data (plist-get info
:title
) info
))
958 (author (and (plist-get info
:with-author
)
959 (let ((auth (plist-get info
:author
)))
960 (and auth
(org-export-data auth info
)))))
961 (email (and (plist-get info
:with-email
)
962 (org-export-data (plist-get info
:email
) info
)))
963 (date (and (plist-get info
:with-date
)
964 (org-export-data (org-export-get-date info
) info
))))
965 ;; There are two types of title blocks depending on the presence
966 ;; of a title to display.
967 (if (string= title
"")
968 ;; Title block without a title. DATE is positioned at the top
969 ;; right of the document, AUTHOR to the top left and EMAIL
972 ((and (org-string-nw-p date
) (org-string-nw-p author
))
975 (make-string (- text-width
(string-width date
) (string-width author
))
978 (when (org-string-nw-p email
) (concat "\n" email
))
980 ((and (org-string-nw-p date
) (org-string-nw-p email
))
983 (make-string (- text-width
(string-width date
) (string-width email
))
986 ((org-string-nw-p date
)
988 (org-ascii--justify-lines date text-width
'right
)
990 ((and (org-string-nw-p author
) (org-string-nw-p email
))
991 (concat author
"\n" email
"\n\n\n"))
992 ((org-string-nw-p author
) (concat author
"\n\n\n"))
993 ((org-string-nw-p email
) (concat email
"\n\n\n")))
994 ;; Title block with a title. Document's TITLE, along with the
995 ;; AUTHOR and its EMAIL are both overlined and an underlined,
996 ;; centered. Date is just below, also centered.
997 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
998 ;; Format TITLE. It may be filled if it is too wide,
999 ;; that is wider than the two thirds of the total width.
1000 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
1001 (formatted-title (org-ascii--fill-string title title-len info
))
1004 (min (+ (max title-len
1005 (string-width (or author
""))
1006 (string-width (or email
"")))
1008 text-width
) (if utf8p ?━ ?_
))))
1009 (org-ascii--justify-lines
1012 (upcase formatted-title
)
1014 ((and (org-string-nw-p author
) (org-string-nw-p email
))
1015 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
1016 ((org-string-nw-p author
)
1017 (concat (if utf8p
"\n\n\n" "\n\n") author
))
1018 ((org-string-nw-p email
)
1019 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
1021 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
1022 "\n\n\n") text-width
'center
)))))
1024 (defun org-ascii-inner-template (contents info
)
1025 "Return complete document string after ASCII conversion.
1026 CONTENTS is the transcoded contents string. INFO is a plist
1027 holding export options."
1028 (org-element-normalize-string
1029 (let ((global-margin (plist-get info
:ascii-global-margin
)))
1030 (org-ascii--indent-string
1032 ;; 1. Document's body.
1034 ;; 2. Footnote definitions.
1035 (let ((definitions (org-export-collect-footnote-definitions
1036 (plist-get info
:parse-tree
) info
))
1037 ;; Insert full links right inside the footnote definition
1038 ;; as they have no chance to be inserted later.
1039 (info (org-combine-plists info
'(:ascii-links-to-notes nil
))))
1043 (let ((title (org-ascii--translate "Footnotes" info
)))
1047 (string-width title
)
1048 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
1050 (let ((text-width (- (plist-get info
:ascii-text-width
)
1054 (let ((id (format "[%s] " (car ref
))))
1055 ;; Distinguish between inline definitions and
1056 ;; full-fledged definitions.
1058 (let ((def (nth 2 ref
)))
1059 (if (eq (org-element-type def
) 'org-data
)
1060 ;; Full-fledged definition: footnote ID is
1061 ;; inserted inside the first parsed
1062 ;; paragraph (FIRST), if any, to be sure
1063 ;; filling will take it into consideration.
1064 (let ((first (car (org-element-contents def
))))
1065 (if (not (eq (org-element-type first
) 'paragraph
))
1066 (concat id
"\n" (org-export-data def info
))
1067 (push id
(nthcdr 2 first
))
1068 (org-export-data def info
)))
1069 ;; Fill paragraph once footnote ID is inserted
1070 ;; in order to have a correct length for first
1072 (org-ascii--fill-string
1073 (concat id
(org-export-data def info
))
1074 text-width info
))))))
1075 definitions
"\n\n"))))))
1078 (defun org-ascii-template (contents info
)
1079 "Return complete document string after ASCII conversion.
1080 CONTENTS is the transcoded contents string. INFO is a plist
1081 holding export options."
1082 (let ((global-margin (plist-get info
:ascii-global-margin
)))
1084 ;; 1. Build title block.
1085 (org-ascii--indent-string
1086 (concat (org-ascii-template--document-title info
)
1087 ;; 2. Table of contents.
1088 (let ((depth (plist-get info
:with-toc
)))
1091 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
1094 ;; 3. Document's body.
1096 ;; 4. Creator. Ignore `comment' value as there are no comments in
1097 ;; ASCII. Justify it to the bottom right.
1098 (org-ascii--indent-string
1099 (let ((creator-info (plist-get info
:with-creator
))
1100 (text-width (- (plist-get info
:ascii-text-width
) global-margin
)))
1101 (unless (or (not creator-info
) (eq creator-info
'comment
))
1104 (org-ascii--fill-string
1105 (plist-get info
:creator
) text-width info
'right
))))
1108 (defun org-ascii--translate (s info
)
1109 "Translate string S according to specified language and charset.
1110 INFO is a plist used as a communication channel."
1111 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1112 (org-export-translate s charset info
)))
1116 ;;; Transcode Functions
1120 (defun org-ascii-bold (bold contents info
)
1121 "Transcode BOLD from Org to ASCII.
1122 CONTENTS is the text with bold markup. INFO is a plist holding
1123 contextual information."
1124 (format "*%s*" contents
))
1129 (defun org-ascii-center-block (center-block contents info
)
1130 "Transcode a CENTER-BLOCK element from Org to ASCII.
1131 CONTENTS holds the contents of the block. INFO is a plist
1132 holding contextual information."
1133 ;; Center has already been taken care of at a lower level, so
1134 ;; there's nothing left to do.
1140 (defun org-ascii-clock (clock contents info
)
1141 "Transcode a CLOCK object from Org to ASCII.
1142 CONTENTS is nil. INFO is a plist holding contextual
1144 (org-ascii--justify-element
1145 (concat org-clock-string
" "
1147 (org-element-property :raw-value
1148 (org-element-property :value clock
)))
1149 (let ((time (org-element-property :duration clock
)))
1154 (org-split-string time
":"))))))
1160 (defun org-ascii-code (code contents info
)
1161 "Return a CODE object from Org to ASCII.
1162 CONTENTS is nil. INFO is a plist holding contextual
1164 (format (plist-get info
:ascii-verbatim-format
)
1165 (org-element-property :value code
)))
1170 (defun org-ascii-drawer (drawer contents info
)
1171 "Transcode a DRAWER element from Org to ASCII.
1172 CONTENTS holds the contents of the block. INFO is a plist
1173 holding contextual information."
1174 (let ((name (org-element-property :drawer-name drawer
))
1175 (width (org-ascii--current-text-width drawer info
)))
1176 (funcall (plist-get info
:ascii-format-drawer-function
)
1177 name contents width
)))
1182 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1183 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1184 CONTENTS holds the contents of the block. INFO is a plist
1185 holding contextual information."
1191 (defun org-ascii-entity (entity contents info
)
1192 "Transcode an ENTITY object from Org to ASCII.
1193 CONTENTS are the definition itself. INFO is a plist holding
1194 contextual information."
1195 (org-element-property
1196 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1202 (defun org-ascii-example-block (example-block contents info
)
1203 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1204 CONTENTS is nil. INFO is a plist holding contextual information."
1205 (org-ascii--justify-element
1206 (org-ascii--box-string
1207 (org-export-format-code-default example-block info
) info
)
1208 example-block info
))
1213 (defun org-ascii-export-snippet (export-snippet contents info
)
1214 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1215 CONTENTS is nil. INFO is a plist holding contextual information."
1216 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1217 (org-element-property :value export-snippet
)))
1222 (defun org-ascii-export-block (export-block contents info
)
1223 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1224 CONTENTS is nil. INFO is a plist holding contextual information."
1225 (when (string= (org-element-property :type export-block
) "ASCII")
1226 (org-ascii--justify-element
1227 (org-element-property :value export-block
) export-block info
)))
1232 (defun org-ascii-fixed-width (fixed-width contents info
)
1233 "Transcode a FIXED-WIDTH element from Org to ASCII.
1234 CONTENTS is nil. INFO is a plist holding contextual information."
1235 (org-ascii--justify-element
1236 (org-ascii--box-string
1237 (org-remove-indentation
1238 (org-element-property :value fixed-width
)) info
)
1242 ;;;; Footnote Definition
1244 ;; Footnote Definitions are ignored. They are compiled at the end of
1245 ;; the document, by `org-ascii-inner-template'.
1248 ;;;; Footnote Reference
1250 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1251 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1252 CONTENTS is nil. INFO is a plist holding contextual information."
1253 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1258 (defun org-ascii-headline (headline contents info
)
1259 "Transcode a HEADLINE element from Org to ASCII.
1260 CONTENTS holds the contents of the headline. INFO is a plist
1261 holding contextual information."
1262 ;; Don't export footnote section, which will be handled at the end
1264 (unless (org-element-property :footnote-section-p headline
)
1265 (let* ((low-level-rank (org-export-low-level-p headline info
))
1266 (width (org-ascii--current-text-width headline info
))
1267 ;; Blank lines between headline and its contents.
1268 ;; `org-ascii-headline-spacing', when set, overwrites
1269 ;; original buffer's spacing.
1272 (or (car (plist-get info
:ascii-headline-spacing
))
1273 (org-element-property :pre-blank headline
))
1275 ;; Even if HEADLINE has no section, there might be some
1276 ;; links in its title that we shouldn't forget to describe.
1278 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1279 (let ((title (org-element-property :title headline
)))
1281 (org-ascii--describe-links
1282 (org-ascii--unique-links title info
) width info
))))))
1283 ;; Deep subtree: export it as a list item.
1287 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1288 (plist-get info
:ascii-bullets
)))))
1290 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1293 (org-ascii--build-title headline info width
) "\n"
1294 ;; Contents, indented by length of bullet.
1296 (org-ascii--indent-string
1298 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1300 ;; Else: Standard headline.
1302 (org-ascii--build-title headline info width
'underline
)
1304 (concat (when (org-string-nw-p links
) links
) contents
))))))
1307 ;;;; Horizontal Rule
1309 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1310 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1311 CONTENTS is nil. INFO is a plist holding contextual
1313 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1315 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1316 (org-ascii--justify-lines
1317 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1318 (string-to-number spec-width
)
1320 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1321 text-width
'center
)))
1324 ;;;; Inline Src Block
1326 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1327 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1328 CONTENTS holds the contents of the item. INFO is a plist holding
1329 contextual information."
1330 (format (plist-get info
:ascii-verbatim-format
)
1331 (org-element-property :value inline-src-block
)))
1336 (defun org-ascii-format-inlinetask-default
1337 (todo type priority name tags contents width inlinetask info
)
1338 "Format an inline task element for ASCII export.
1339 See `org-ascii-format-inlinetask-function' for a description
1341 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1342 (width (or width
(plist-get info
:ascii-inlinetask-width
))))
1343 (org-ascii--indent-string
1345 ;; Top line, with an additional blank line if not in UTF-8.
1346 (make-string width
(if utf8p ?━ ?_
)) "\n"
1347 (unless utf8p
(concat (make-string width ?
) "\n"))
1348 ;; Add title. Fill it if wider than inlinetask.
1349 (let ((title (org-ascii--build-title inlinetask info width
)))
1350 (if (<= (string-width title
) width
) title
1351 (org-ascii--fill-string title width info
)))
1353 ;; If CONTENTS is not empty, insert it along with
1355 (when (org-string-nw-p contents
)
1356 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1358 (make-string width
(if utf8p ?━ ?_
)))
1359 ;; Flush the inlinetask to the right.
1360 (- (plist-get info
:ascii-text-width
) (plist-get info
:ascii-global-margin
)
1361 (if (not (org-export-get-parent-headline inlinetask
)) 0
1362 (plist-get info
:ascii-inner-margin
))
1363 (org-ascii--current-text-width inlinetask info
)))))
1365 (defun org-ascii-inlinetask (inlinetask contents info
)
1366 "Transcode an INLINETASK element from Org to ASCII.
1367 CONTENTS holds the contents of the block. INFO is a plist
1368 holding contextual information."
1369 (let ((width (org-ascii--current-text-width inlinetask info
)))
1370 (funcall (plist-get info
:ascii-format-inlinetask-function
)
1372 (and (plist-get info
:with-todo-keywords
)
1373 (let ((todo (org-element-property
1374 :todo-keyword inlinetask
)))
1375 (and todo
(org-export-data todo info
))))
1377 (org-element-property :todo-type inlinetask
)
1379 (and (plist-get info
:with-priority
)
1380 (org-element-property :priority inlinetask
))
1382 (org-export-data (org-element-property :title inlinetask
) info
)
1384 (and (plist-get info
:with-tags
)
1385 (org-element-property :tags inlinetask
))
1386 ;; contents and width
1387 contents width inlinetask info
)))
1392 (defun org-ascii-italic (italic contents info
)
1393 "Transcode italic from Org to ASCII.
1394 CONTENTS is the text with italic markup. INFO is a plist holding
1395 contextual information."
1396 (format "/%s/" contents
))
1401 (defun org-ascii-item (item contents info
)
1402 "Transcode an ITEM element from Org to ASCII.
1403 CONTENTS holds the contents of the item. INFO is a plist holding
1404 contextual information."
1405 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1406 (checkbox (org-ascii--checkbox item info
))
1407 (list-type (org-element-property :type
(org-export-get-parent item
)))
1409 ;; First parent of ITEM is always the plain-list. Get
1410 ;; `:type' property from it.
1411 (org-list-bullet-string
1415 (org-export-data (org-element-property :tag item
) info
)
1418 ;; Return correct number for ITEM, paying attention to
1420 (let* ((struct (org-element-property :structure item
))
1421 (bul (org-element-property :bullet item
))
1422 (num (number-to-string
1423 (car (last (org-list-get-item-number
1424 (org-element-property :begin item
)
1426 (org-list-prevs-alist struct
)
1427 (org-list-parents-alist struct
)))))))
1428 (replace-regexp-in-string "[0-9]+" num bul
)))
1429 (t (let ((bul (org-element-property :bullet item
)))
1430 ;; Change bullets into more visible form if UTF-8 is active.
1432 (replace-regexp-in-string
1434 (replace-regexp-in-string
1436 (replace-regexp-in-string "*" "‣" bul
))))))))))
1439 (unless (eq list-type
'descriptive
) checkbox
)
1440 ;; Contents: Pay attention to indentation. Note: check-boxes are
1441 ;; already taken care of at the paragraph level so they don't
1442 ;; interfere with indentation.
1443 (let ((contents (org-ascii--indent-string contents
(string-width bullet
))))
1444 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1446 (concat "\n" contents
))))))
1451 (defun org-ascii-keyword (keyword contents info
)
1452 "Transcode a KEYWORD element from Org to ASCII.
1453 CONTENTS is nil. INFO is a plist holding contextual
1455 (let ((key (org-element-property :key keyword
)))
1457 ((string= key
"ASCII")
1458 (org-ascii--justify-element
1459 (org-element-property :value keyword
) keyword info
))
1460 ((string= key
"TOC")
1461 (org-ascii--justify-element
1462 (let ((value (downcase (org-element-property :value keyword
))))
1464 ((string-match "\\<headlines\\>" value
)
1465 (let ((depth (or (and (string-match "[0-9]+" value
)
1466 (string-to-number (match-string 0 value
)))
1467 (plist-get info
:with-toc
))))
1468 (org-ascii--build-toc
1469 info
(and (wholenump depth
) depth
) keyword
)))
1470 ((string= "tables" value
)
1471 (org-ascii--list-tables keyword info
))
1472 ((string= "listings" value
)
1473 (org-ascii--list-listings keyword info
))))
1477 ;;;; Latex Environment
1479 (defun org-ascii-latex-environment (latex-environment contents info
)
1480 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1481 CONTENTS is nil. INFO is a plist holding contextual
1483 (when (plist-get info
:with-latex
)
1484 (org-ascii--justify-element
1485 (org-remove-indentation (org-element-property :value latex-environment
))
1486 latex-environment info
)))
1491 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1492 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1493 CONTENTS is nil. INFO is a plist holding contextual
1495 (when (plist-get info
:with-latex
)
1496 (org-element-property :value latex-fragment
)))
1501 (defun org-ascii-line-break (line-break contents info
)
1502 "Transcode a LINE-BREAK object from Org to ASCII.
1503 CONTENTS is nil. INFO is a plist holding contextual
1504 information." hard-newline
)
1509 (defun org-ascii-link (link desc info
)
1510 "Transcode a LINK object from Org to ASCII.
1512 DESC is the description part of the link, or the empty string.
1513 INFO is a plist holding contextual information."
1514 (let ((raw-link (org-element-property :raw-link link
))
1515 (type (org-element-property :type link
)))
1517 ((string= type
"coderef")
1518 (let ((ref (org-element-property :path link
)))
1519 (format (org-export-get-coderef-format ref desc
)
1520 (org-export-resolve-coderef ref info
))))
1521 ;; Do not apply a special syntax on radio links. Though, use
1522 ;; transcoded target's contents as output.
1523 ((string= type
"radio") desc
)
1524 ;; Do not apply a special syntax on fuzzy links pointing to
1526 ((string= type
"fuzzy")
1527 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1528 (if (org-string-nw-p desc
) desc
1531 (org-export-get-ordinal
1532 destination info nil
'org-ascii--has-caption-p
)))
1534 (if (atom number
) (number-to-string number
)
1535 (mapconcat 'number-to-string number
"."))))))))
1537 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1538 (concat (format "[%s]" desc
)
1539 (and (not (plist-get info
:ascii-links-to-notes
))
1540 (format " (%s)" raw-link
))))))))
1543 ;;;; Node Properties
1545 (defun org-ascii-node-property (node-property contents info
)
1546 "Transcode a NODE-PROPERTY element from Org to ASCII.
1547 CONTENTS is nil. INFO is a plist holding contextual
1550 (org-element-property :key node-property
)
1551 (let ((value (org-element-property :value node-property
)))
1552 (if value
(concat " " value
) ""))))
1557 (defun org-ascii-paragraph (paragraph contents info
)
1558 "Transcode a PARAGRAPH element from Org to ASCII.
1559 CONTENTS is the contents of the paragraph, as a string. INFO is
1560 the plist used as a communication channel."
1561 (org-ascii--justify-element
1562 (let ((indented-line-width (plist-get info
:ascii-indented-line-width
)))
1563 (if (not (wholenump indented-line-width
)) contents
1565 ;; Do not indent first paragraph in a section.
1566 (unless (and (not (org-export-get-previous-element paragraph info
))
1567 (eq (org-element-type (org-export-get-parent paragraph
))
1569 (make-string indented-line-width ?\s
))
1570 (replace-regexp-in-string "\\`[ \t]+" "" contents
))))
1576 (defun org-ascii-plain-list (plain-list contents info
)
1577 "Transcode a PLAIN-LIST element from Org to ASCII.
1578 CONTENTS is the contents of the list. INFO is a plist holding
1579 contextual information."
1580 (let ((margin (plist-get info
:ascii-list-margin
)))
1581 (if (or (< margin
1)
1582 (eq (org-element-type (org-export-get-parent plain-list
)) 'item
))
1584 (org-ascii--indent-string contents margin
))))
1589 (defun org-ascii-plain-text (text info
)
1590 "Transcode a TEXT string from Org to ASCII.
1591 INFO is a plist used as a communication channel."
1592 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1593 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1594 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1595 (if (not (plist-get info
:with-special-strings
)) text
1596 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1597 (if (not utf8p
) text
1598 ;; Usual replacements in utf-8 with proper option set.
1599 (replace-regexp-in-string
1601 (replace-regexp-in-string
1603 (replace-regexp-in-string "---" "—" text
)))))))
1608 (defun org-ascii-planning (planning contents info
)
1609 "Transcode a PLANNING element from Org to ASCII.
1610 CONTENTS is nil. INFO is a plist used as a communication
1612 (org-ascii--justify-element
1616 (list (let ((closed (org-element-property :closed planning
)))
1618 (concat org-closed-string
" "
1620 (org-element-property :raw-value closed
)))))
1621 (let ((deadline (org-element-property :deadline planning
)))
1623 (concat org-deadline-string
" "
1625 (org-element-property :raw-value deadline
)))))
1626 (let ((scheduled (org-element-property :scheduled planning
)))
1628 (concat org-scheduled-string
" "
1630 (org-element-property :raw-value scheduled
)))))))
1635 ;;;; Property Drawer
1637 (defun org-ascii-property-drawer (property-drawer contents info
)
1638 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1639 CONTENTS holds the contents of the drawer. INFO is a plist
1640 holding contextual information."
1641 (and (org-string-nw-p contents
)
1642 (org-ascii--justify-element contents property-drawer info
)))
1647 (defun org-ascii-quote-block (quote-block contents info
)
1648 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1649 CONTENTS holds the contents of the block. INFO is a plist
1650 holding contextual information."
1651 (org-ascii--indent-string contents
(plist-get info
:ascii-quote-margin
)))
1656 (defun org-ascii-radio-target (radio-target contents info
)
1657 "Transcode a RADIO-TARGET object from Org to ASCII.
1658 CONTENTS is the contents of the target. INFO is a plist holding
1659 contextual information."
1665 (defun org-ascii-section (section contents info
)
1666 "Transcode a SECTION element from Org to ASCII.
1667 CONTENTS is the contents of the section. INFO is a plist holding
1668 contextual information."
1669 (org-ascii--indent-string
1672 (when (plist-get info
:ascii-links-to-notes
)
1673 ;; Add list of links at the end of SECTION.
1674 (let ((links (org-ascii--describe-links
1675 (org-ascii--unique-links section info
)
1676 (org-ascii--current-text-width section info
) info
)))
1677 ;; Separate list of links and section contents.
1678 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1679 ;; Do not apply inner margin if parent headline is low level.
1680 (let ((headline (org-export-get-parent-headline section
)))
1681 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1682 (plist-get info
:ascii-inner-margin
)))))
1687 (defun org-ascii-special-block (special-block contents info
)
1688 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1689 CONTENTS holds the contents of the block. INFO is a plist
1690 holding contextual information."
1691 ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
1692 ;; at a lower level. There is no other special block type to
1699 (defun org-ascii-src-block (src-block contents info
)
1700 "Transcode a SRC-BLOCK element from Org to ASCII.
1701 CONTENTS holds the contents of the item. INFO is a plist holding
1702 contextual information."
1703 (let ((caption (org-ascii--build-caption src-block info
))
1704 (caption-above-p (plist-get info
:ascii-caption-above
))
1705 (code (org-export-format-code-default src-block info
)))
1706 (if (equal code
"") ""
1707 (org-ascii--justify-element
1709 (and caption caption-above-p
(concat caption
"\n"))
1710 (org-ascii--box-string code info
)
1711 (and caption
(not caption-above-p
) (concat "\n" caption
)))
1715 ;;;; Statistics Cookie
1717 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1718 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1719 CONTENTS is nil. INFO is a plist holding contextual information."
1720 (org-element-property :value statistics-cookie
))
1725 (defun org-ascii-subscript (subscript contents info
)
1726 "Transcode a SUBSCRIPT object from Org to ASCII.
1727 CONTENTS is the contents of the object. INFO is a plist holding
1728 contextual information."
1729 (if (org-element-property :use-brackets-p subscript
)
1730 (format "_{%s}" contents
)
1731 (format "_%s" contents
)))
1736 (defun org-ascii-superscript (superscript contents info
)
1737 "Transcode a SUPERSCRIPT object from Org to ASCII.
1738 CONTENTS is the contents of the object. INFO is a plist holding
1739 contextual information."
1740 (if (org-element-property :use-brackets-p superscript
)
1741 (format "^{%s}" contents
)
1742 (format "^%s" contents
)))
1747 (defun org-ascii-strike-through (strike-through contents info
)
1748 "Transcode STRIKE-THROUGH from Org to ASCII.
1749 CONTENTS is text with strike-through markup. INFO is a plist
1750 holding contextual information."
1751 (format "+%s+" contents
))
1756 (defun org-ascii-table (table contents info
)
1757 "Transcode a TABLE element from Org to ASCII.
1758 CONTENTS is the contents of the table. INFO is a plist holding
1759 contextual information."
1760 (let ((caption (org-ascii--build-caption table info
))
1761 (caption-above-p (plist-get info
:ascii-caption-above
)))
1762 (org-ascii--justify-element
1764 ;; Possibly add a caption string above.
1765 (and caption caption-above-p
(concat caption
"\n"))
1766 ;; Insert table. Note: "table.el" tables are left unmodified.
1767 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1768 ((and (plist-get info
:ascii-table-use-ascii-art
)
1769 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1770 (require 'ascii-art-to-unicode nil t
))
1772 (insert (org-remove-indentation
1773 (org-element-property :value table
)))
1774 (goto-char (point-min))
1776 (goto-char (point-max))
1777 (skip-chars-backward " \r\t\n")
1778 (buffer-substring (point-min) (point))))
1779 (t (org-remove-indentation (org-element-property :value table
))))
1780 ;; Possible add a caption string below.
1781 (and (not caption-above-p
) caption
))
1787 (defun org-ascii--table-cell-width (table-cell info
)
1788 "Return width of TABLE-CELL.
1790 INFO is a plist used as a communication channel.
1792 Width of a cell is determined either by a width cookie in the
1793 same column as the cell, or by the maximum cell's length in that
1796 When `org-ascii-table-widen-columns' is non-nil, width cookies
1798 (let* ((row (org-export-get-parent table-cell
))
1799 (table (org-export-get-parent row
))
1800 (col (let ((cells (org-element-contents row
)))
1801 (- (length cells
) (length (memq table-cell cells
)))))
1803 (or (plist-get info
:ascii-table-cell-width-cache
)
1804 (plist-get (setq info
1805 (plist-put info
:ascii-table-cell-width-cache
1806 (make-hash-table :test
'equal
)))
1807 :ascii-table-cell-width-cache
)))
1808 (key (cons table col
))
1809 (widenp (plist-get info
:ascii-table-widen-columns
)))
1810 (or (gethash key cache
)
1813 (let ((cookie-width (org-export-table-cell-width table-cell info
)))
1814 (or (and (not widenp
) cookie-width
)
1815 (let ((contents-width
1816 (let ((max-width 0))
1817 (org-element-map table
'table-row
1822 (org-element-contents
1823 (elt (org-element-contents row
) col
))
1828 (cond ((not cookie-width
) contents-width
)
1829 (widenp (max cookie-width contents-width
))
1830 (t cookie-width
)))))
1833 (defun org-ascii-table-cell (table-cell contents info
)
1834 "Transcode a TABLE-CELL object from Org to ASCII.
1835 CONTENTS is the cell contents. INFO is a plist used as
1836 a communication channel."
1837 ;; Determine column width. When `org-ascii-table-widen-columns'
1838 ;; is nil and some width cookie has set it, use that value.
1839 ;; Otherwise, compute the maximum width among transcoded data of
1840 ;; each cell in the column.
1841 (let ((width (org-ascii--table-cell-width table-cell info
)))
1842 ;; When contents are too large, truncate them.
1843 (unless (or (plist-get info
:ascii-table-widen-columns
)
1844 (<= (string-width (or contents
"")) width
))
1845 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1846 ;; Align contents correctly within the cell.
1847 (let* ((indent-tabs-mode nil
)
1850 (org-ascii--justify-lines
1852 (org-export-table-cell-alignment table-cell info
)))))
1855 (make-string (- width
(string-width (or data
""))) ?\s
))))
1857 (concat (format " %s " contents
)
1858 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1859 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1864 (defun org-ascii-table-row (table-row contents info
)
1865 "Transcode a TABLE-ROW element from Org to ASCII.
1866 CONTENTS is the row contents. INFO is a plist used as
1867 a communication channel."
1868 (when (eq (org-element-property :type table-row
) 'standard
)
1871 (lambda (lcorner horiz vert rcorner
)
1875 (org-element-map table-row
'table-cell
1877 (let ((width (org-ascii--table-cell-width cell info
))
1878 (borders (org-export-table-cell-borders cell info
)))
1880 ;; In order to know if CELL starts the row, do
1881 ;; not compare it with the first cell in the
1882 ;; row as there might be a special column.
1883 ;; Instead, compare it with first exportable
1884 ;; cell, obtained with `org-element-map'.
1885 (when (and (memq 'left borders
)
1886 (eq (org-element-map table-row
'table-cell
1890 (make-string (+ 2 width
) (string-to-char horiz
))
1892 ((not (memq 'right borders
)) nil
)
1893 ((eq (car (last (org-element-contents table-row
))) cell
)
1897 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1898 (borders (org-export-table-cell-borders
1899 (org-element-map table-row
'table-cell
'identity info t
)
1902 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1903 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1904 (funcall build-hline
"+" "-" "+" "+")))
1905 ((memq 'above borders
)
1906 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1907 (funcall build-hline
"+" "-" "+" "+"))))
1908 (when (memq 'left borders
) (if utf8p
"│" "|"))
1910 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1911 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1912 (funcall build-hline
"+" "-" "+" "+")))))))
1917 (defun org-ascii-timestamp (timestamp contents info
)
1918 "Transcode a TIMESTAMP object from Org to ASCII.
1919 CONTENTS is nil. INFO is a plist holding contextual information."
1920 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1925 (defun org-ascii-underline (underline contents info
)
1926 "Transcode UNDERLINE from Org to ASCII.
1927 CONTENTS is the text with underline markup. INFO is a plist
1928 holding contextual information."
1929 (format "_%s_" contents
))
1934 (defun org-ascii-verbatim (verbatim contents info
)
1935 "Return a VERBATIM object from Org to ASCII.
1936 CONTENTS is nil. INFO is a plist holding contextual information."
1937 (format (plist-get info
:ascii-verbatim-format
)
1938 (org-element-property :value verbatim
)))
1943 (defun org-ascii-verse-block (verse-block contents info
)
1944 "Transcode a VERSE-BLOCK element from Org to ASCII.
1945 CONTENTS is verse block contents. INFO is a plist holding
1946 contextual information."
1947 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1948 (org-ascii--indent-string
1949 (org-ascii--justify-element contents verse-block info
)
1950 (plist-get info
:ascii-quote-margin
))))
1956 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1957 "Filter controlling number of blank lines after a headline.
1959 HEADLINE is a string representing a transcoded headline.
1960 BACK-END is symbol specifying back-end used for export. INFO is
1961 plist containing the communication channel.
1963 This function only applies to `ascii' back-end. See
1964 `org-ascii-headline-spacing' for information."
1965 (let ((headline-spacing (plist-get info
:ascii-headline-spacing
)))
1966 (if (not headline-spacing
) headline
1967 (let ((blanks (make-string (1+ (cdr headline-spacing
)) ?
\n)))
1968 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
)))))
1970 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1971 "Filter controlling number of blank lines between paragraphs.
1973 TREE is the parse tree. BACK-END is the symbol specifying
1974 back-end used for export. INFO is a plist used as
1975 a communication channel.
1977 See `org-ascii-paragraph-spacing' for information."
1978 (let ((paragraph-spacing (plist-get info
:ascii-paragraph-spacing
)))
1979 (when (wholenump paragraph-spacing
)
1980 (org-element-map tree
'paragraph
1982 (when (eq (org-element-type (org-export-get-next-element p info
))
1984 (org-element-put-property p
:post-blank paragraph-spacing
))))))
1987 (defun org-ascii-filter-comment-spacing (tree backend info
)
1988 "Filter removing blank lines between comments.
1989 TREE is the parse tree. BACK-END is the symbol specifying
1990 back-end used for export. INFO is a plist used as
1991 a communication channel."
1992 (org-element-map tree
'(comment comment-block
)
1994 (when (memq (org-element-type (org-export-get-next-element c info
))
1995 '(comment comment-block
))
1996 (org-element-put-property c
:post-blank
0))))
2001 ;;; End-user functions
2004 (defun org-ascii-export-as-ascii
2005 (&optional async subtreep visible-only body-only ext-plist
)
2006 "Export current buffer to a text buffer.
2008 If narrowing is active in the current buffer, only export its
2011 If a region is active, export that region.
2013 A non-nil optional argument ASYNC means the process should happen
2014 asynchronously. The resulting buffer should be accessible
2015 through the `org-export-stack' interface.
2017 When optional argument SUBTREEP is non-nil, export the sub-tree
2018 at point, extracting information from the headline properties
2021 When optional argument VISIBLE-ONLY is non-nil, don't export
2022 contents of hidden elements.
2024 When optional argument BODY-ONLY is non-nil, strip title and
2025 table of contents from output.
2027 EXT-PLIST, when provided, is a property list with external
2028 parameters overriding Org default settings, but still inferior to
2029 file-local settings.
2031 Export is done in a buffer named \"*Org ASCII Export*\", which
2032 will be displayed when `org-export-show-temporary-export-buffer'
2035 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
2036 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
2039 (defun org-ascii-export-to-ascii
2040 (&optional async subtreep visible-only body-only ext-plist
)
2041 "Export current buffer to a text file.
2043 If narrowing is active in the current buffer, only export its
2046 If a region is active, export that region.
2048 A non-nil optional argument ASYNC means the process should happen
2049 asynchronously. The resulting file should be accessible through
2050 the `org-export-stack' interface.
2052 When optional argument SUBTREEP is non-nil, export the sub-tree
2053 at point, extracting information from the headline properties
2056 When optional argument VISIBLE-ONLY is non-nil, don't export
2057 contents of hidden elements.
2059 When optional argument BODY-ONLY is non-nil, strip title and
2060 table of contents from output.
2062 EXT-PLIST, when provided, is a property list with external
2063 parameters overriding Org default settings, but still inferior to
2064 file-local settings.
2066 Return output file's name."
2068 (let ((file (org-export-output-file-name ".txt" subtreep
)))
2069 (org-export-to-file 'ascii file
2070 async subtreep visible-only body-only ext-plist
)))
2073 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
2074 "Publish an Org file to ASCII.
2076 FILENAME is the filename of the Org file to be published. PLIST
2077 is the property list for the given project. PUB-DIR is the
2078 publishing directory.
2080 Return output file name."
2082 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
2085 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
2086 "Publish an Org file to Latin-1.
2088 FILENAME is the filename of the Org file to be published. PLIST
2089 is the property list for the given project. PUB-DIR is the
2090 publishing directory.
2092 Return output file name."
2094 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
2097 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
2098 "Publish an org file to UTF-8.
2100 FILENAME is the filename of the Org file to be published. PLIST
2101 is the property list for the given project. PUB-DIR is the
2102 publishing directory.
2104 Return output file name."
2106 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
2112 ;; generated-autoload-file: "org-loaddefs.el"
2113 ;; coding: utf-8-emacs
2116 ;;; ox-ascii.el ends here