1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements an ASCII back-end for Org generic exporter.
25 ;; It provides two commands for export, depending on the desired
26 ;; output: `org-ascii-export-as-ascii' (temporary buffer) and
27 ;; `org-ascii-export-to-ascii' ("txt" file). Also, three publishing
28 ;; functions are available: `org-ascii-publish-to-ascii',
29 ;; `org-ascii-publish-to-latin1' and `org-ascii-publish-to-utf8'.
31 ;; Output encoding is specified through `org-ascii-charset' variable,
32 ;; among `ascii', `latin1' and `utf-8' symbols.
34 ;; By default, horizontal rules span over the full text with, but with
35 ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
36 ;; they can be shortened and centered.
40 (eval-when-compile (require 'cl
))
44 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
48 ;; The following setting won't allow to modify preferred charset
49 ;; through a buffer keyword or an option item, but, since the property
50 ;; will appear in communication channel nonetheless, it allows to
51 ;; override `org-ascii-charset' variable on the fly by the ext-plist
54 ;; We also install a filter for headlines and sections, in order to
55 ;; control blank lines separating them in output string.
57 (org-export-define-backend 'ascii
58 '((bold . org-ascii-bold
)
59 (center-block . org-ascii-center-block
)
60 (clock . org-ascii-clock
)
61 (code . org-ascii-code
)
62 (comment .
(lambda (&rest args
) ""))
63 (comment-block .
(lambda (&rest args
) ""))
64 (drawer . org-ascii-drawer
)
65 (dynamic-block . org-ascii-dynamic-block
)
66 (entity . org-ascii-entity
)
67 (example-block . org-ascii-example-block
)
68 (export-block . org-ascii-export-block
)
69 (export-snippet . org-ascii-export-snippet
)
70 (fixed-width . org-ascii-fixed-width
)
71 (footnote-reference . org-ascii-footnote-reference
)
72 (headline . org-ascii-headline
)
73 (horizontal-rule . org-ascii-horizontal-rule
)
74 (inline-src-block . org-ascii-inline-src-block
)
75 (inlinetask . org-ascii-inlinetask
)
76 (inner-template . org-ascii-inner-template
)
77 (italic . org-ascii-italic
)
78 (item . org-ascii-item
)
79 (keyword . org-ascii-keyword
)
80 (latex-environment . org-ascii-latex-environment
)
81 (latex-fragment . org-ascii-latex-fragment
)
82 (line-break . org-ascii-line-break
)
83 (link . org-ascii-link
)
84 (paragraph . org-ascii-paragraph
)
85 (plain-list . org-ascii-plain-list
)
86 (plain-text . org-ascii-plain-text
)
87 (planning . org-ascii-planning
)
88 (quote-block . org-ascii-quote-block
)
89 (quote-section . org-ascii-quote-section
)
90 (radio-target . org-ascii-radio-target
)
91 (section . org-ascii-section
)
92 (special-block . org-ascii-special-block
)
93 (src-block . org-ascii-src-block
)
94 (statistics-cookie . org-ascii-statistics-cookie
)
95 (strike-through . org-ascii-strike-through
)
96 (subscript . org-ascii-subscript
)
97 (superscript . org-ascii-superscript
)
98 (table . org-ascii-table
)
99 (table-cell . org-ascii-table-cell
)
100 (table-row . org-ascii-table-row
)
101 (target . org-ascii-target
)
102 (template . org-ascii-template
)
103 (timestamp . org-ascii-timestamp
)
104 (underline . org-ascii-underline
)
105 (verbatim . org-ascii-verbatim
)
106 (verse-block . org-ascii-verse-block
))
107 :export-block
"ASCII"
109 '(?t
"Export to Plain Text"
110 ((?A
"As ASCII buffer"
112 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
115 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
116 (?L
"As Latin1 buffer"
118 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
121 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
122 (?U
"As UTF-8 buffer"
124 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
127 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
128 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
129 (:filter-parse-tree org-ascii-filter-paragraph-spacing
130 org-ascii-filter-comment-spacing
)
131 (:filter-section . org-ascii-filter-headline-blank-lines
))
132 :options-alist
'((:ascii-charset nil nil org-ascii-charset
)))
136 ;;; User Configurable Variables
138 (defgroup org-export-ascii nil
139 "Options for exporting Org mode files to ASCII."
140 :tag
"Org Export ASCII"
143 (defcustom org-ascii-text-width
72
144 "Maximum width of exported text.
145 This number includes margin size, as set in
146 `org-ascii-global-margin'."
147 :group
'org-export-ascii
149 :package-version
'(Org .
"8.0")
152 (defcustom org-ascii-global-margin
0
153 "Width of the left margin, in number of characters."
154 :group
'org-export-ascii
156 :package-version
'(Org .
"8.0")
159 (defcustom org-ascii-inner-margin
2
160 "Width of the inner margin, in number of characters.
161 Inner margin is applied between each headline."
162 :group
'org-export-ascii
164 :package-version
'(Org .
"8.0")
167 (defcustom org-ascii-quote-margin
6
168 "Width of margin used for quoting text, in characters.
169 This margin is applied on both sides of the text."
170 :group
'org-export-ascii
172 :package-version
'(Org .
"8.0")
175 (defcustom org-ascii-inlinetask-width
30
176 "Width of inline tasks, in number of characters.
177 This number ignores any margin."
178 :group
'org-export-ascii
180 :package-version
'(Org .
"8.0")
183 (defcustom org-ascii-headline-spacing
'(1 .
2)
184 "Number of blank lines inserted around headlines.
186 This variable can be set to a cons cell. In that case, its car
187 represents the number of blank lines present before headline
188 contents whereas its cdr reflects the number of blank lines after
191 A nil value replicates the number of blank lines found in the
192 original Org buffer at the same place."
193 :group
'org-export-ascii
195 :package-version
'(Org .
"8.0")
197 (const :tag
"Replicate original spacing" nil
)
198 (cons :tag
"Set an uniform spacing"
199 (integer :tag
"Number of blank lines before contents")
200 (integer :tag
"Number of blank lines after contents"))))
202 (defcustom org-ascii-indented-line-width
'auto
203 "Additional indentation width for the first line in a paragraph.
204 If the value is an integer, indent the first line of each
205 paragraph by this number. If it is the symbol `auto' preserve
206 indentation from original document."
207 :group
'org-export-ascii
209 :package-version
'(Org .
"8.0")
211 (integer :tag
"Number of white spaces characters")
212 (const :tag
"Preserve original width" auto
)))
214 (defcustom org-ascii-paragraph-spacing
'auto
215 "Number of white lines between paragraphs.
216 If the value is an integer, add this number of blank lines
217 between contiguous paragraphs. If is it the symbol `auto', keep
218 the same number of blank lines as in the original document."
219 :group
'org-export-ascii
221 :package-version
'(Org .
"8.0")
223 (integer :tag
"Number of blank lines")
224 (const :tag
"Preserve original spacing" auto
)))
226 (defcustom org-ascii-charset
'ascii
227 "The charset allowed to represent various elements and objects.
229 `ascii' Only use plain ASCII characters
230 `latin1' Include Latin-1 characters
231 `utf-8' Use all UTF-8 characters"
232 :group
'org-export-ascii
234 :package-version
'(Org .
"8.0")
236 (const :tag
"ASCII" ascii
)
237 (const :tag
"Latin-1" latin1
)
238 (const :tag
"UTF-8" utf-8
)))
240 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
242 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
243 "Characters for underlining headings in ASCII export.
245 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
246 and whose value is a list of characters.
248 For each supported charset, this variable associates a sequence
249 of underline characters. In a sequence, the characters will be
250 used in order for headlines level 1, 2, ... If no character is
251 available for a given level, the headline won't be underlined."
252 :group
'org-export-ascii
254 :package-version
'(Org .
"8.0")
256 (cons :tag
"Underline characters sequence"
257 (const :tag
"ASCII charset" ascii
)
259 (cons :tag
"Underline characters sequence"
260 (const :tag
"Latin-1 charset" latin1
)
262 (cons :tag
"Underline characters sequence"
263 (const :tag
"UTF-8 charset" utf-8
)
264 (repeat character
))))
266 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
269 "Bullet characters for headlines converted to lists in ASCII export.
271 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
272 and whose value is a list of characters.
274 The first character is used for the first level considered as low
275 level, and so on. If there are more levels than characters given
276 here, the list will be repeated.
278 Note that this variable doesn't affect plain lists
280 :group
'org-export-ascii
282 :package-version
'(Org .
"8.0")
284 (cons :tag
"Bullet characters for low level headlines"
285 (const :tag
"ASCII charset" ascii
)
287 (cons :tag
"Bullet characters for low level headlines"
288 (const :tag
"Latin-1 charset" latin1
)
290 (cons :tag
"Bullet characters for low level headlines"
291 (const :tag
"UTF-8 charset" utf-8
)
292 (repeat character
))))
294 (defcustom org-ascii-links-to-notes t
295 "Non-nil means convert links to notes before the next headline.
296 When nil, the link will be exported in place. If the line
297 becomes long in this way, it will be wrapped."
298 :group
'org-export-ascii
300 :package-version
'(Org .
"8.0")
303 (defcustom org-ascii-table-keep-all-vertical-lines nil
304 "Non-nil means keep all vertical lines in ASCII tables.
305 When nil, vertical lines will be removed except for those needed
306 for column grouping."
307 :group
'org-export-ascii
309 :package-version
'(Org .
"8.0")
312 (defcustom org-ascii-table-widen-columns t
313 "Non-nil means widen narrowed columns for export.
314 When nil, narrowed columns will look in ASCII export just like in
315 Org mode, i.e. with \"=>\" as ellipsis."
316 :group
'org-export-ascii
318 :package-version
'(Org .
"8.0")
321 (defcustom org-ascii-table-use-ascii-art nil
322 "Non-nil means table.el tables are turned into ascii-art.
324 It only makes sense when export charset is `utf-8'. It is nil by
325 default since it requires ascii-art-to-unicode.el package. You
326 can download it here:
328 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
329 :group
'org-export-ascii
331 :package-version
'(Org .
"8.0")
334 (defcustom org-ascii-caption-above nil
335 "When non-nil, place caption string before the element.
336 Otherwise, place it right after it."
337 :group
'org-export-ascii
339 :package-version
'(Org .
"8.0")
342 (defcustom org-ascii-verbatim-format
"`%s'"
343 "Format string used for verbatim text and inline code."
344 :group
'org-export-ascii
346 :package-version
'(Org .
"8.0")
349 (defcustom org-ascii-format-drawer-function nil
350 "Function called to format a drawer in ASCII.
352 The function must accept three parameters:
353 NAME the drawer name, like \"LOGBOOK\"
354 CONTENTS the contents of the drawer.
355 WIDTH the text width within the drawer.
357 The function should return either the string to be exported or
358 nil to ignore the drawer.
360 For example, the variable could be set to the following function
361 in order to mimic default behaviour:
363 \(defun org-ascii-format-drawer-default (name contents width)
364 \"Format a drawer element for ASCII export.\"
366 :group
'org-export-ascii
368 :package-version
'(Org .
"8.0")
371 (defcustom org-ascii-format-inlinetask-function nil
372 "Function called to format an inlinetask in ASCII.
374 The function must accept six parameters:
375 TODO the todo keyword, as a string
376 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
377 PRIORITY the inlinetask priority, as a string
378 NAME the inlinetask name, as a string.
379 TAGS the inlinetask tags, as a list of strings.
380 CONTENTS the contents of the inlinetask, as a string.
382 The function should return either the string to be exported or
383 nil to ignore the inline task.
385 For example, the variable could be set to the following function
386 in order to mimic default behaviour:
388 \(defun org-ascii-format-inlinetask-default
389 \(todo type priority name tags contents\)
390 \"Format an inline task element for ASCII export.\"
391 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
392 \(width org-ascii-inlinetask-width\)
393 \(org-ascii--indent-string
395 ;; Top line, with an additional blank line if not in UTF-8.
396 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
397 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
398 ;; Add title. Fill it if wider than inlinetask.
399 \(let \(\(title \(org-ascii--build-title inlinetask info width\)\)\)
400 \(if \(<= \(length title\) width\) title
401 \(org-ascii--fill-string title width info\)\)\)
403 ;; If CONTENTS is not empty, insert it along with
405 \(when \(org-string-nw-p contents\)
406 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
408 \(make-string width \(if utf8p ?━ ?_\)\)\)
409 ;; Flush the inlinetask to the right.
410 \(- \(plist-get info :ascii-width\)
411 \(plist-get info :ascii-margin\)
412 \(plist-get info :ascii-inner-margin\)
413 \(org-ascii--current-text-width inlinetask info\)\)"
414 :group
'org-export-ascii
416 :package-version
'(Org .
"8.0")
421 ;;; Internal Functions
423 ;; Internal functions fall into three categories.
425 ;; The first one is about text formatting. The core function is
426 ;; `org-ascii--current-text-width', which determines the current
427 ;; text width allowed to a given element. In other words, it helps
428 ;; keeping each line width within maximum text width defined in
429 ;; `org-ascii-text-width'. Once this information is known,
430 ;; `org-ascii--fill-string', `org-ascii--justify-string',
431 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
432 ;; operate on a given output string.
434 ;; The second category contains functions handling elements listings,
435 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
436 ;; returns a complete table of contents, `org-ascii--list-listings'
437 ;; returns a list of referenceable src-block elements, and
438 ;; `org-ascii--list-tables' does the same for table elements.
440 ;; The third category includes general helper functions.
441 ;; `org-ascii--build-title' creates the title for a given headline
442 ;; or inlinetask element. `org-ascii--build-caption' returns the
443 ;; caption string associated to a table or a src-block.
444 ;; `org-ascii--describe-links' creates notes about links for
445 ;; insertion at the end of a section. It uses
446 ;; `org-ascii--unique-links' to get the list of links to describe.
447 ;; Eventually, `org-ascii--translate' translates a string according
448 ;; to language and charset specification.
451 (defun org-ascii--fill-string (s text-width info
&optional justify
)
452 "Fill a string with specified text-width and return it.
454 S is the string being filled. TEXT-WIDTH is an integer
455 specifying maximum length of a line. INFO is the plist used as
456 a communication channel.
458 Optional argument JUSTIFY can specify any type of justification
459 among `left', `center', `right' or `full'. A nil value is
460 equivalent to `left'. For a justification that doesn't also fill
461 string, see `org-ascii--justify-string'.
463 Return nil if S isn't a string."
464 ;; Don't fill paragraph when break should be preserved.
465 (cond ((not (stringp s
)) nil
)
466 ((plist-get info
:preserve-breaks
) s
)
467 (t (let ((double-space-p sentence-end-double-space
))
469 (let ((fill-column text-width
)
470 (use-hard-newlines t
)
471 (sentence-end-double-space double-space-p
))
473 (fill-region (point-min) (point-max) justify
))
476 (defun org-ascii--justify-string (s text-width how
)
478 TEXT-WIDTH is an integer specifying maximum length of a line.
479 HOW determines the type of justification: it can be `left',
480 `right', `full' or `center'."
483 (goto-char (point-min))
484 (let ((fill-column text-width
)
485 ;; Disable `adaptive-fill-mode' so it doesn't prevent
486 ;; filling lines matching `adaptive-fill-regexp'.
487 (adaptive-fill-mode nil
))
488 (while (< (point) (point-max))
489 (justify-current-line how
)
493 (defun org-ascii--indent-string (s width
)
494 "Indent string S by WIDTH white spaces.
495 Empty lines are not indented."
497 (replace-regexp-in-string
498 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
500 (defun org-ascii--box-string (s info
)
501 "Return string S with a partial box to its left.
502 INFO is a plist used as a communicaton channel."
503 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
504 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
505 (replace-regexp-in-string
506 "^" (if utf8p
"│ " "| ")
507 ;; Remove last newline character.
508 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
510 (defun org-ascii--current-text-width (element info
)
511 "Return maximum text width for ELEMENT's contents.
512 INFO is a plist used as a communication channel."
513 (case (org-element-type element
)
514 ;; Elements with an absolute width: `headline' and `inlinetask'.
515 (inlinetask org-ascii-inlinetask-width
)
517 (- org-ascii-text-width
518 (let ((low-level-rank (org-export-low-level-p element info
)))
519 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
520 ;; Elements with a relative width: store maximum text width in
523 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
524 ;; Total width is determined by the presence, or not, of an
525 ;; inline task among ELEMENT parents.
527 (if (loop for parent in genealogy
528 thereis
(eq (org-element-type parent
) 'inlinetask
))
529 org-ascii-inlinetask-width
530 ;; No inlinetask: Remove global margin from text width.
531 (- org-ascii-text-width
532 org-ascii-global-margin
533 (let ((parent (org-export-get-parent-headline element
)))
534 ;; Inner margin doesn't apply to text before first
537 (let ((low-level-rank
538 (org-export-low-level-p parent info
)))
539 ;; Inner margin doesn't apply to contents of
540 ;; low level headlines, since they've got their
541 ;; own indentation mechanism.
542 (if low-level-rank
(* low-level-rank
2)
543 org-ascii-inner-margin
))))))))
545 ;; Each `quote-block', `quote-section' and `verse-block' above
546 ;; narrows text width by twice the standard margin size.
547 (+ (* (loop for parent in genealogy
548 when
(memq (org-element-type parent
)
549 '(quote-block quote-section verse-block
))
551 2 org-ascii-quote-margin
)
552 ;; Text width within a plain-list is restricted by
553 ;; indentation of current item. If that's the case,
554 ;; compute it with the help of `:structure' property from
555 ;; parent item, if any.
557 (if (eq (org-element-type element
) 'item
) element
558 (loop for parent in genealogy
559 when
(eq (org-element-type parent
) 'item
)
561 (if (not parent-item
) 0
562 ;; Compute indentation offset of the current item,
563 ;; that is the sum of the difference between its
564 ;; indentation and the indentation of the top item in
565 ;; the list and current item bullet's length. Also
566 ;; remove checkbox length, and tag length (for
567 ;; description lists) or bullet length.
568 (let ((struct (org-element-property :structure parent-item
))
569 (beg-item (org-element-property :begin parent-item
)))
570 (+ (- (org-list-get-ind beg-item struct
)
572 (org-list-get-top-point struct
) struct
))
573 (length (org-ascii--checkbox parent-item info
))
575 (or (org-list-get-tag beg-item struct
)
576 (org-list-get-bullet beg-item struct
)))))))))))))
578 (defun org-ascii--build-title
579 (element info text-width
&optional underline notags toc
)
580 "Format ELEMENT title and return it.
582 ELEMENT is either an `headline' or `inlinetask' element. INFO is
583 a plist used as a communication channel. TEXT-WIDTH is an
584 integer representing the maximum length of a line.
586 When optional argument UNDERLINE is non-nil, underline title,
587 without the tags, according to `org-ascii-underline'
590 If optional argument NOTAGS is non-nil, no tags will be added to
593 When optional argument TOC is non-nil, use optional title if
594 possible. It doesn't apply to `inlinetask' elements."
595 (let* ((headlinep (eq (org-element-type element
) 'headline
))
597 ;; Numbering is specific to headlines.
598 (and headlinep
(org-export-numbered-headline-p element info
)
599 ;; All tests passed: build numbering string.
603 (org-export-get-headline-number element info
) ".")
608 (if (and toc headlinep
) (org-export-get-alt-title element info
)
609 (org-element-property :title element
))
612 (and (plist-get info
:with-todo-keywords
)
613 (let ((todo (org-element-property :todo-keyword element
)))
614 (and todo
(concat (org-export-data todo info
) " ")))))
615 (tags (and (not notags
)
616 (plist-get info
:with-tags
)
617 (let ((tag-list (org-export-get-tags element info
)))
620 (mapconcat 'identity tag-list
":"))))))
622 (and (plist-get info
:with-priority
)
623 (let ((char (org-element-property :priority element
)))
624 (and char
(format "(#%c) " char
)))))
625 (first-part (concat numbers todo priority text
)))
628 ;; Align tags, if any.
632 (max (- text-width
(1+ (length first-part
))) (length tags
)))
634 ;; Maybe underline text, if ELEMENT type is `headline' and an
635 ;; underline character has been defined.
636 (when (and underline headlinep
)
638 (nth (1- (org-export-get-relative-level element info
))
639 (cdr (assq (plist-get info
:ascii-charset
)
640 org-ascii-underline
)))))
643 (make-string (length first-part
) under-char
))))))))
645 (defun org-ascii--has-caption-p (element info
)
646 "Non-nil when ELEMENT has a caption affiliated keyword.
647 INFO is a plist used as a communication channel. This function
648 is meant to be used as a predicate for `org-export-get-ordinal'."
649 (org-element-property :caption element
))
651 (defun org-ascii--build-caption (element info
)
652 "Return caption string for ELEMENT, if applicable.
654 INFO is a plist used as a communication channel.
656 The caption string contains the sequence number of ELEMENT along
657 with its real caption. Return nil when ELEMENT has no affiliated
659 (let ((caption (org-export-get-caption element
)))
661 ;; Get sequence number of current src-block among every
662 ;; src-block with a caption.
664 (org-export-get-ordinal
665 element info nil
'org-ascii--has-caption-p
))
666 (title-fmt (org-ascii--translate
667 (case (org-element-type element
)
668 (table "Table %d: %s")
669 (src-block "Listing %d: %s"))
671 (org-ascii--fill-string
672 (format title-fmt reference
(org-export-data caption info
))
673 (org-ascii--current-text-width element info
) info
)))))
675 (defun org-ascii--build-toc (info &optional n keyword
)
676 "Return a table of contents.
678 INFO is a plist used as a communication channel.
680 Optional argument N, when non-nil, is an integer specifying the
683 Optional argument KEYWORD specifies the TOC keyword, if any, from
684 which the table of contents generation has been initiated."
685 (let ((title (org-ascii--translate "Table of Contents" info
)))
688 (make-string (length title
)
689 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
692 (if keyword
(org-ascii--current-text-width keyword info
)
693 (- org-ascii-text-width org-ascii-global-margin
))))
696 (let* ((level (org-export-get-relative-level headline info
))
697 (indent (* (1- level
) 3)))
699 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
700 (org-ascii--build-title
701 headline info
(- text-width indent
) nil
702 (or (not (plist-get info
:with-tags
))
703 (eq (plist-get info
:with-tags
) 'not-in-toc
))
705 (org-export-collect-headlines info n
) "\n")))))
707 (defun org-ascii--list-listings (keyword info
)
708 "Return a list of listings.
710 KEYWORD is the keyword that initiated the list of listings
711 generation. INFO is a plist used as a communication channel."
712 (let ((title (org-ascii--translate "List of Listings" info
)))
715 (make-string (length title
)
716 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
719 (if keyword
(org-ascii--current-text-width keyword info
)
720 (- org-ascii-text-width org-ascii-global-margin
)))
721 ;; Use a counter instead of retreiving ordinal of each
726 ;; Store initial text so its length can be computed. This is
727 ;; used to properly align caption right to it in case of
728 ;; filling (like contents of a description list item).
730 (format (org-ascii--translate "Listing %d:" info
)
735 (org-ascii--indent-string
736 (org-ascii--fill-string
737 ;; Use short name in priority, if available.
738 (let ((caption (or (org-export-get-caption src-block t
)
739 (org-export-get-caption src-block
))))
740 (org-export-data caption info
))
741 (- text-width
(length initial-text
)) info
)
742 (length initial-text
))))))
743 (org-export-collect-listings info
) "\n")))))
745 (defun org-ascii--list-tables (keyword info
)
746 "Return a list of tables.
748 KEYWORD is the keyword that initiated the list of tables
749 generation. INFO is a plist used as a communication channel."
750 (let ((title (org-ascii--translate "List of Tables" info
)))
753 (make-string (length title
)
754 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
757 (if keyword
(org-ascii--current-text-width keyword info
)
758 (- org-ascii-text-width org-ascii-global-margin
)))
759 ;; Use a counter instead of retreiving ordinal of each
764 ;; Store initial text so its length can be computed. This is
765 ;; used to properly align caption right to it in case of
766 ;; filling (like contents of a description list item).
768 (format (org-ascii--translate "Table %d:" info
)
773 (org-ascii--indent-string
774 (org-ascii--fill-string
775 ;; Use short name in priority, if available.
776 (let ((caption (or (org-export-get-caption table t
)
777 (org-export-get-caption table
))))
778 (org-export-data caption info
))
779 (- text-width
(length initial-text
)) info
)
780 (length initial-text
))))))
781 (org-export-collect-tables info
) "\n")))))
783 (defun org-ascii--unique-links (element info
)
784 "Return a list of unique link references in ELEMENT.
786 ELEMENT is either a headline element or a section element. INFO
787 is a plist used as a communication channel."
791 ;; Return LINK if it wasn't referenced so far, or nil.
792 ;; Update SEEN links along the way.
795 (cons (org-element-property :raw-link link
)
796 (org-element-contents link
))))
797 ;; Ignore LINK if it hasn't been translated already.
798 ;; It can happen if it is located in an affiliated
799 ;; keyword that was ignored.
800 (when (and (org-string-nw-p
801 (gethash link
(plist-get info
:exported-data
)))
802 (not (member footprint seen
)))
803 (push footprint seen
) link
)))))
804 ;; If at a section, find parent headline, if any, in order to
805 ;; count links that might be in the title.
807 (if (eq (org-element-type element
) 'headline
) element
808 (or (org-export-get-parent-headline element
) element
))))
809 ;; Get all links in HEADLINE.
810 (org-element-map headline
'link
811 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
813 (defun org-ascii--describe-links (links width info
)
814 "Return a string describing a list of links.
816 LINKS is a list of link type objects, as returned by
817 `org-ascii--unique-links'. WIDTH is the text width allowed for
818 the output string. INFO is a plist used as a communication
822 (let ((type (org-element-property :type link
))
823 (anchor (let ((desc (org-element-contents link
)))
824 (if desc
(org-export-data desc info
)
825 (org-element-property :raw-link link
)))))
827 ;; Coderefs, radio links and fuzzy links are ignored.
828 ((member type
'("coderef" "radio" "fuzzy")) nil
)
829 ;; Id and custom-id links: Headlines refer to their numbering.
830 ((member type
'("custom-id" "id"))
831 (let ((dest (org-export-resolve-id-link link info
)))
833 (org-ascii--fill-string
837 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
839 (org-ascii--translate "See section %s" info
)
840 (mapconcat 'number-to-string
841 (org-export-get-headline-number dest info
) "."))))
842 width info
) "\n\n")))
843 ;; Do not add a link that cannot be resolved and doesn't have
844 ;; any description: destination is already visible in the
846 ((not (org-element-contents link
)) nil
)
849 (org-ascii--fill-string
850 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
855 (defun org-ascii--checkbox (item info
)
856 "Return checkbox string for ITEM or nil.
857 INFO is a plist used as a communication channel."
858 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
859 (case (org-element-property :checkbox item
)
860 (on (if utf8p
"☑ " "[X] "))
861 (off (if utf8p
"☐ " "[ ] "))
862 (trans (if utf8p
"☒ " "[-] ")))))
868 (defun org-ascii-template--document-title (info)
869 "Return document title, as a string.
870 INFO is a plist used as a communication channel."
871 (let* ((text-width org-ascii-text-width
)
872 ;; Links in the title will not be resolved later, so we make
873 ;; sure their path is located right after them.
874 (org-ascii-links-to-notes nil
)
875 (title (org-export-data (plist-get info
:title
) info
))
876 (author (and (plist-get info
:with-author
)
877 (let ((auth (plist-get info
:author
)))
878 (and auth
(org-export-data auth info
)))))
879 (email (and (plist-get info
:with-email
)
880 (org-export-data (plist-get info
:email
) info
)))
881 (date (and (plist-get info
:with-date
)
882 (org-export-data (org-export-get-date info
) info
))))
883 ;; There are two types of title blocks depending on the presence
884 ;; of a title to display.
885 (if (string= title
"")
886 ;; Title block without a title. DATE is positioned at the top
887 ;; right of the document, AUTHOR to the top left and EMAIL
890 ((and (org-string-nw-p date
) (org-string-nw-p author
))
893 (make-string (- text-width
(length date
) (length author
)) ?
)
895 (when (org-string-nw-p email
) (concat "\n" email
))
897 ((and (org-string-nw-p date
) (org-string-nw-p email
))
900 (make-string (- text-width
(length date
) (length email
)) ?
)
902 ((org-string-nw-p date
)
904 (org-ascii--justify-string date text-width
'right
)
906 ((and (org-string-nw-p author
) (org-string-nw-p email
))
907 (concat author
"\n" email
"\n\n\n"))
908 ((org-string-nw-p author
) (concat author
"\n\n\n"))
909 ((org-string-nw-p email
) (concat email
"\n\n\n")))
910 ;; Title block with a title. Document's TITLE, along with the
911 ;; AUTHOR and its EMAIL are both overlined and an underlined,
912 ;; centered. Date is just below, also centered.
913 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
914 ;; Format TITLE. It may be filled if it is too wide,
915 ;; that is wider than the two thirds of the total width.
916 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
917 (formatted-title (org-ascii--fill-string title title-len info
))
920 (min (+ (max title-len
(length author
) (length email
)) 2)
921 text-width
) (if utf8p ?━ ?_
))))
922 (org-ascii--justify-string
925 (upcase formatted-title
)
927 ((and (org-string-nw-p author
) (org-string-nw-p email
))
928 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
929 ((org-string-nw-p author
)
930 (concat (if utf8p
"\n\n\n" "\n\n") author
))
931 ((org-string-nw-p email
)
932 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
934 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
935 "\n\n\n") text-width
'center
)))))
937 (defun org-ascii-inner-template (contents info
)
938 "Return complete document string after ASCII conversion.
939 CONTENTS is the transcoded contents string. INFO is a plist
940 holding export options."
941 (org-element-normalize-string
942 (org-ascii--indent-string
944 ;; 1. Document's body.
946 ;; 2. Footnote definitions.
947 (let ((definitions (org-export-collect-footnote-definitions
948 (plist-get info
:parse-tree
) info
))
949 ;; Insert full links right inside the footnote definition
950 ;; as they have no chance to be inserted later.
951 (org-ascii-links-to-notes nil
))
955 (let ((title (org-ascii--translate "Footnotes" info
)))
960 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
962 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
965 (let ((id (format "[%s] " (car ref
))))
966 ;; Distinguish between inline definitions and
967 ;; full-fledged definitions.
969 (let ((def (nth 2 ref
)))
970 (if (eq (org-element-type def
) 'org-data
)
971 ;; Full-fledged definition: footnote ID is
972 ;; inserted inside the first parsed paragraph
973 ;; (FIRST), if any, to be sure filling will
974 ;; take it into consideration.
975 (let ((first (car (org-element-contents def
))))
976 (if (not (eq (org-element-type first
) 'paragraph
))
977 (concat id
"\n" (org-export-data def info
))
978 (push id
(nthcdr 2 first
))
979 (org-export-data def info
)))
980 ;; Fill paragraph once footnote ID is inserted
981 ;; in order to have a correct length for first
983 (org-ascii--fill-string
984 (concat id
(org-export-data def info
))
985 text-width info
))))))
986 definitions
"\n\n"))))))
987 org-ascii-global-margin
)))
989 (defun org-ascii-template (contents info
)
990 "Return complete document string after ASCII conversion.
991 CONTENTS is the transcoded contents string. INFO is a plist
992 holding export options."
994 ;; 1. Build title block.
995 (org-ascii--indent-string
996 (concat (org-ascii-template--document-title info
)
997 ;; 2. Table of contents.
998 (let ((depth (plist-get info
:with-toc
)))
1001 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
1003 org-ascii-global-margin
)
1004 ;; 3. Document's body.
1006 ;; 4. Creator. Ignore `comment' value as there are no comments in
1007 ;; ASCII. Justify it to the bottom right.
1008 (org-ascii--indent-string
1009 (let ((creator-info (plist-get info
:with-creator
))
1010 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
1011 (unless (or (not creator-info
) (eq creator-info
'comment
))
1014 (org-ascii--fill-string
1015 (plist-get info
:creator
) text-width info
'right
))))
1016 org-ascii-global-margin
)))
1018 (defun org-ascii--translate (s info
)
1019 "Translate string S according to specified language and charset.
1020 INFO is a plist used as a communication channel."
1021 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1022 (org-export-translate s charset info
)))
1026 ;;; Transcode Functions
1030 (defun org-ascii-bold (bold contents info
)
1031 "Transcode BOLD from Org to ASCII.
1032 CONTENTS is the text with bold markup. INFO is a plist holding
1033 contextual information."
1034 (format "*%s*" contents
))
1039 (defun org-ascii-center-block (center-block contents info
)
1040 "Transcode a CENTER-BLOCK element from Org to ASCII.
1041 CONTENTS holds the contents of the block. INFO is a plist
1042 holding contextual information."
1043 (org-ascii--justify-string
1044 contents
(org-ascii--current-text-width center-block info
) 'center
))
1049 (defun org-ascii-clock (clock contents info
)
1050 "Transcode a CLOCK object from Org to ASCII.
1051 CONTENTS is nil. INFO is a plist holding contextual
1053 (concat org-clock-string
" "
1055 (org-element-property :raw-value
1056 (org-element-property :value clock
)))
1057 (let ((time (org-element-property :duration clock
)))
1062 (org-split-string time
":")))))))
1067 (defun org-ascii-code (code contents info
)
1068 "Return a CODE object from Org to ASCII.
1069 CONTENTS is nil. INFO is a plist holding contextual
1071 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1076 (defun org-ascii-drawer (drawer contents info
)
1077 "Transcode a DRAWER element from Org to ASCII.
1078 CONTENTS holds the contents of the block. INFO is a plist
1079 holding contextual information."
1080 (let ((name (org-element-property :drawer-name drawer
))
1081 (width (org-ascii--current-text-width drawer info
)))
1082 (if (functionp org-ascii-format-drawer-function
)
1083 (funcall org-ascii-format-drawer-function name contents width
)
1084 ;; If there's no user defined function: simply
1085 ;; display contents of the drawer.
1091 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1092 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1093 CONTENTS holds the contents of the block. INFO is a plist
1094 holding contextual information."
1100 (defun org-ascii-entity (entity contents info
)
1101 "Transcode an ENTITY object from Org to ASCII.
1102 CONTENTS are the definition itself. INFO is a plist holding
1103 contextual information."
1104 (org-element-property
1105 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1111 (defun org-ascii-example-block (example-block contents info
)
1112 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1113 CONTENTS is nil. INFO is a plist holding contextual information."
1114 (org-ascii--box-string
1115 (org-export-format-code-default example-block info
) info
))
1120 (defun org-ascii-export-snippet (export-snippet contents info
)
1121 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1122 CONTENTS is nil. INFO is a plist holding contextual information."
1123 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1124 (org-element-property :value export-snippet
)))
1129 (defun org-ascii-export-block (export-block contents info
)
1130 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1131 CONTENTS is nil. INFO is a plist holding contextual information."
1132 (when (string= (org-element-property :type export-block
) "ASCII")
1133 (org-remove-indentation (org-element-property :value export-block
))))
1138 (defun org-ascii-fixed-width (fixed-width contents info
)
1139 "Transcode a FIXED-WIDTH element from Org to ASCII.
1140 CONTENTS is nil. INFO is a plist holding contextual information."
1141 (org-ascii--box-string
1142 (org-remove-indentation
1143 (org-element-property :value fixed-width
)) info
))
1146 ;;;; Footnote Definition
1148 ;; Footnote Definitions are ignored. They are compiled at the end of
1149 ;; the document, by `org-ascii-inner-template'.
1152 ;;;; Footnote Reference
1154 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1155 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1156 CONTENTS is nil. INFO is a plist holding contextual information."
1157 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1162 (defun org-ascii-headline (headline contents info
)
1163 "Transcode a HEADLINE element from Org to ASCII.
1164 CONTENTS holds the contents of the headline. INFO is a plist
1165 holding contextual information."
1166 ;; Don't export footnote section, which will be handled at the end
1168 (unless (org-element-property :footnote-section-p headline
)
1169 (let* ((low-level-rank (org-export-low-level-p headline info
))
1170 (width (org-ascii--current-text-width headline info
))
1171 ;; Blank lines between headline and its contents.
1172 ;; `org-ascii-headline-spacing', when set, overwrites
1173 ;; original buffer's spacing.
1176 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1177 (org-element-property :pre-blank headline
)) ?
\n))
1178 ;; Even if HEADLINE has no section, there might be some
1179 ;; links in its title that we shouldn't forget to describe.
1181 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1182 (let ((title (org-element-property :title headline
)))
1184 (org-ascii--describe-links
1185 (org-ascii--unique-links title info
) width info
))))))
1186 ;; Deep subtree: export it as a list item.
1190 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1191 org-ascii-bullets
))))
1193 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1196 (org-ascii--build-title headline info width
) "\n"
1197 ;; Contents, indented by length of bullet.
1199 (org-ascii--indent-string
1201 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1203 ;; Else: Standard headline.
1205 (org-ascii--build-title headline info width
'underline
)
1207 (concat (when (org-string-nw-p links
) links
) contents
))))))
1210 ;;;; Horizontal Rule
1212 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1213 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1214 CONTENTS is nil. INFO is a plist holding contextual
1216 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1218 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1219 (org-ascii--justify-string
1220 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1221 (string-to-number spec-width
)
1223 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1224 text-width
'center
)))
1227 ;;;; Inline Src Block
1229 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1230 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1231 CONTENTS holds the contents of the item. INFO is a plist holding
1232 contextual information."
1233 (format org-ascii-verbatim-format
1234 (org-element-property :value inline-src-block
)))
1239 (defun org-ascii-inlinetask (inlinetask contents info
)
1240 "Transcode an INLINETASK element from Org to ASCII.
1241 CONTENTS holds the contents of the block. INFO is a plist
1242 holding contextual information."
1243 (let ((width (org-ascii--current-text-width inlinetask info
)))
1244 ;; If `org-ascii-format-inlinetask-function' is provided, call it
1245 ;; with appropriate arguments.
1246 (if (functionp org-ascii-format-inlinetask-function
)
1247 (funcall org-ascii-format-inlinetask-function
1249 (and (plist-get info
:with-todo-keywords
)
1250 (let ((todo (org-element-property
1251 :todo-keyword inlinetask
)))
1252 (and todo
(org-export-data todo info
))))
1254 (org-element-property :todo-type inlinetask
)
1256 (and (plist-get info
:with-priority
)
1257 (org-element-property :priority inlinetask
))
1259 (org-export-data (org-element-property :title inlinetask
) info
)
1261 (and (plist-get info
:with-tags
)
1262 (org-element-property :tags inlinetask
))
1263 ;; contents and width
1265 ;; Otherwise, use a default template.
1266 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1267 (org-ascii--indent-string
1269 ;; Top line, with an additional blank line if not in UTF-8.
1270 (make-string width
(if utf8p ?━ ?_
)) "\n"
1271 (unless utf8p
(concat (make-string width ?
) "\n"))
1272 ;; Add title. Fill it if wider than inlinetask.
1273 (let ((title (org-ascii--build-title inlinetask info width
)))
1274 (if (<= (length title
) width
) title
1275 (org-ascii--fill-string title width info
)))
1277 ;; If CONTENTS is not empty, insert it along with
1279 (when (org-string-nw-p contents
)
1280 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1282 (make-string width
(if utf8p ?━ ?_
)))
1283 ;; Flush the inlinetask to the right.
1284 (- org-ascii-text-width org-ascii-global-margin
1285 (if (not (org-export-get-parent-headline inlinetask
)) 0
1286 org-ascii-inner-margin
)
1287 (org-ascii--current-text-width inlinetask info
)))))))
1292 (defun org-ascii-italic (italic contents info
)
1293 "Transcode italic from Org to ASCII.
1294 CONTENTS is the text with italic markup. INFO is a plist holding
1295 contextual information."
1296 (format "/%s/" contents
))
1301 (defun org-ascii-item (item contents info
)
1302 "Transcode an ITEM element from Org to ASCII.
1303 CONTENTS holds the contents of the item. INFO is a plist holding
1304 contextual information."
1305 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1306 (checkbox (org-ascii--checkbox item info
))
1307 (list-type (org-element-property :type
(org-export-get-parent item
)))
1309 ;; First parent of ITEM is always the plain-list. Get
1310 ;; `:type' property from it.
1311 (org-list-bullet-string
1315 (org-export-data (org-element-property :tag item
) info
)
1318 ;; Return correct number for ITEM, paying attention to
1320 (let* ((struct (org-element-property :structure item
))
1321 (bul (org-element-property :bullet item
))
1322 (num (number-to-string
1323 (car (last (org-list-get-item-number
1324 (org-element-property :begin item
)
1326 (org-list-prevs-alist struct
)
1327 (org-list-parents-alist struct
)))))))
1328 (replace-regexp-in-string "[0-9]+" num bul
)))
1329 (t (let ((bul (org-element-property :bullet item
)))
1330 ;; Change bullets into more visible form if UTF-8 is active.
1332 (replace-regexp-in-string
1334 (replace-regexp-in-string
1336 (replace-regexp-in-string "*" "‣" bul
))))))))))
1339 (unless (eq list-type
'descriptive
) checkbox
)
1340 ;; Contents: Pay attention to indentation. Note: check-boxes are
1341 ;; already taken care of at the paragraph level so they don't
1342 ;; interfere with indentation.
1343 (let ((contents (org-ascii--indent-string contents
(length bullet
))))
1344 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1346 (concat "\n" contents
))))))
1351 (defun org-ascii-keyword (keyword contents info
)
1352 "Transcode a KEYWORD element from Org to ASCII.
1353 CONTENTS is nil. INFO is a plist holding contextual
1355 (let ((key (org-element-property :key keyword
))
1356 (value (org-element-property :value keyword
)))
1358 ((string= key
"ASCII") value
)
1359 ((string= key
"TOC")
1360 (let ((value (downcase value
)))
1362 ((string-match "\\<headlines\\>" value
)
1363 (let ((depth (or (and (string-match "[0-9]+" value
)
1364 (string-to-number (match-string 0 value
)))
1365 (plist-get info
:with-toc
))))
1366 (org-ascii--build-toc
1367 info
(and (wholenump depth
) depth
) keyword
)))
1368 ((string= "tables" value
)
1369 (org-ascii--list-tables keyword info
))
1370 ((string= "listings" value
)
1371 (org-ascii--list-listings keyword info
))))))))
1374 ;;;; Latex Environment
1376 (defun org-ascii-latex-environment (latex-environment contents info
)
1377 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1378 CONTENTS is nil. INFO is a plist holding contextual
1380 (when (plist-get info
:with-latex
)
1381 (org-remove-indentation (org-element-property :value latex-environment
))))
1386 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1387 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1388 CONTENTS is nil. INFO is a plist holding contextual
1390 (when (plist-get info
:with-latex
)
1391 (org-element-property :value latex-fragment
)))
1396 (defun org-ascii-line-break (line-break contents info
)
1397 "Transcode a LINE-BREAK object from Org to ASCII.
1398 CONTENTS is nil. INFO is a plist holding contextual
1399 information." hard-newline
)
1404 (defun org-ascii-link (link desc info
)
1405 "Transcode a LINK object from Org to ASCII.
1407 DESC is the description part of the link, or the empty string.
1408 INFO is a plist holding contextual information."
1409 (let ((raw-link (org-element-property :raw-link link
))
1410 (type (org-element-property :type link
)))
1412 ((string= type
"coderef")
1413 (let ((ref (org-element-property :path link
)))
1414 (format (org-export-get-coderef-format ref desc
)
1415 (org-export-resolve-coderef ref info
))))
1416 ;; Do not apply a special syntax on radio links. Though, use
1417 ;; transcoded target's contents as output.
1418 ((string= type
"radio")
1419 (let ((destination (org-export-resolve-radio-link link info
)))
1421 (org-export-data (org-element-contents destination
) info
))))
1422 ;; Do not apply a special syntax on fuzzy links pointing to
1424 ((string= type
"fuzzy")
1425 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1426 (if (org-string-nw-p desc
) desc
1429 (org-export-get-ordinal
1430 destination info nil
'org-ascii--has-caption-p
)))
1432 (if (atom number
) (number-to-string number
)
1433 (mapconcat 'number-to-string number
"."))))))))
1435 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1437 (format "[%s]" desc
)
1438 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1443 (defun org-ascii-paragraph (paragraph contents info
)
1444 "Transcode a PARAGRAPH element from Org to ASCII.
1445 CONTENTS is the contents of the paragraph, as a string. INFO is
1446 the plist used as a communication channel."
1447 (let ((contents (if (not (wholenump org-ascii-indented-line-width
)) contents
1449 (make-string org-ascii-indented-line-width ?
)
1450 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1451 (org-ascii--fill-string
1452 contents
(org-ascii--current-text-width paragraph info
) info
)))
1457 (defun org-ascii-plain-list (plain-list contents info
)
1458 "Transcode a PLAIN-LIST element from Org to ASCII.
1459 CONTENTS is the contents of the list. INFO is a plist holding
1460 contextual information."
1466 (defun org-ascii-plain-text (text info
)
1467 "Transcode a TEXT string from Org to ASCII.
1468 INFO is a plist used as a communication channel."
1469 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1470 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1471 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1472 (if (not (plist-get info
:with-special-strings
)) text
1473 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1474 (if (not utf8p
) text
1475 ;; Usual replacements in utf-8 with proper option set.
1476 (replace-regexp-in-string
1478 (replace-regexp-in-string
1480 (replace-regexp-in-string "---" "—" text
)))))))
1485 (defun org-ascii-planning (planning contents info
)
1486 "Transcode a PLANNING element from Org to ASCII.
1487 CONTENTS is nil. INFO is a plist used as a communication
1492 (list (let ((closed (org-element-property :closed planning
)))
1494 (concat org-closed-string
" "
1496 (org-element-property :raw-value closed
)))))
1497 (let ((deadline (org-element-property :deadline planning
)))
1499 (concat org-deadline-string
" "
1501 (org-element-property :raw-value deadline
)))))
1502 (let ((scheduled (org-element-property :scheduled planning
)))
1504 (concat org-scheduled-string
" "
1506 (org-element-property :raw-value scheduled
)))))))
1512 (defun org-ascii-quote-block (quote-block contents info
)
1513 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1514 CONTENTS holds the contents of the block. INFO is a plist
1515 holding contextual information."
1516 (org-ascii--indent-string contents org-ascii-quote-margin
))
1521 (defun org-ascii-quote-section (quote-section contents info
)
1522 "Transcode a QUOTE-SECTION element from Org to ASCII.
1523 CONTENTS is nil. INFO is a plist holding contextual information."
1524 (let ((width (org-ascii--current-text-width quote-section info
))
1527 (org-remove-indentation (org-element-property :value quote-section
))
1529 (org-ascii--indent-string
1531 (+ org-ascii-quote-margin
1532 ;; Don't apply inner margin if parent headline is low level.
1533 (let ((headline (org-export-get-parent-headline quote-section
)))
1534 (if (org-export-low-level-p headline info
) 0
1535 org-ascii-inner-margin
))))))
1540 (defun org-ascii-radio-target (radio-target contents info
)
1541 "Transcode a RADIO-TARGET object from Org to ASCII.
1542 CONTENTS is the contents of the target. INFO is a plist holding
1543 contextual information."
1549 (defun org-ascii-section (section contents info
)
1550 "Transcode a SECTION element from Org to ASCII.
1551 CONTENTS is the contents of the section. INFO is a plist holding
1552 contextual information."
1553 (org-ascii--indent-string
1556 (when org-ascii-links-to-notes
1557 ;; Add list of links at the end of SECTION.
1558 (let ((links (org-ascii--describe-links
1559 (org-ascii--unique-links section info
)
1560 (org-ascii--current-text-width section info
) info
)))
1561 ;; Separate list of links and section contents.
1562 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1563 ;; Do not apply inner margin if parent headline is low level.
1564 (let ((headline (org-export-get-parent-headline section
)))
1565 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1566 org-ascii-inner-margin
))))
1571 (defun org-ascii-special-block (special-block contents info
)
1572 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1573 CONTENTS holds the contents of the block. INFO is a plist
1574 holding contextual information."
1580 (defun org-ascii-src-block (src-block contents info
)
1581 "Transcode a SRC-BLOCK element from Org to ASCII.
1582 CONTENTS holds the contents of the item. INFO is a plist holding
1583 contextual information."
1584 (let ((caption (org-ascii--build-caption src-block info
))
1585 (code (org-export-format-code-default src-block info
)))
1586 (if (equal code
"") ""
1588 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1589 (org-ascii--box-string code info
)
1590 (when (and caption
(not org-ascii-caption-above
))
1591 (concat "\n" caption
))))))
1594 ;;;; Statistics Cookie
1596 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1597 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1598 CONTENTS is nil. INFO is a plist holding contextual information."
1599 (org-element-property :value statistics-cookie
))
1604 (defun org-ascii-subscript (subscript contents info
)
1605 "Transcode a SUBSCRIPT object from Org to ASCII.
1606 CONTENTS is the contents of the object. INFO is a plist holding
1607 contextual information."
1608 (if (org-element-property :use-brackets-p subscript
)
1609 (format "_{%s}" contents
)
1610 (format "_%s" contents
)))
1615 (defun org-ascii-superscript (superscript contents info
)
1616 "Transcode a SUPERSCRIPT object from Org to ASCII.
1617 CONTENTS is the contents of the object. INFO is a plist holding
1618 contextual information."
1619 (if (org-element-property :use-brackets-p superscript
)
1620 (format "_{%s}" contents
)
1621 (format "_%s" contents
)))
1626 (defun org-ascii-strike-through (strike-through contents info
)
1627 "Transcode STRIKE-THROUGH from Org to ASCII.
1628 CONTENTS is text with strike-through markup. INFO is a plist
1629 holding contextual information."
1630 (format "+%s+" contents
))
1635 (defun org-ascii-table (table contents info
)
1636 "Transcode a TABLE element from Org to ASCII.
1637 CONTENTS is the contents of the table. INFO is a plist holding
1638 contextual information."
1639 (let ((caption (org-ascii--build-caption table info
)))
1641 ;; Possibly add a caption string above.
1642 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1643 ;; Insert table. Note: "table.el" tables are left unmodified.
1644 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1645 ((and org-ascii-table-use-ascii-art
1646 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1647 (require 'ascii-art-to-unicode nil t
))
1649 (insert (org-remove-indentation
1650 (org-element-property :value table
)))
1651 (goto-char (point-min))
1653 (goto-char (point-max))
1654 (skip-chars-backward " \r\t\n")
1655 (buffer-substring (point-min) (point))))
1656 (t (org-remove-indentation (org-element-property :value table
))))
1657 ;; Possible add a caption string below.
1658 (when (and caption
(not org-ascii-caption-above
))
1659 (concat "\n" caption
)))))
1664 (defun org-ascii--table-cell-width (table-cell info
)
1665 "Return width of TABLE-CELL.
1667 INFO is a plist used as a communication channel.
1669 Width of a cell is determined either by a width cookie in the
1670 same column as the cell, or by the maximum cell's length in that
1673 When `org-ascii-table-widen-columns' is non-nil, width cookies
1675 (or (and (not org-ascii-table-widen-columns
)
1676 (org-export-table-cell-width table-cell info
))
1677 (let* ((max-width 0)
1678 (table (org-export-get-parent-table table-cell
))
1679 (specialp (org-export-table-has-special-column-p table
))
1680 (col (cdr (org-export-table-cell-address table-cell info
))))
1681 (org-element-map table
'table-row
1686 (org-element-contents
1687 (elt (if specialp
(cdr (org-element-contents row
))
1688 (org-element-contents row
))
1695 (defun org-ascii-table-cell (table-cell contents info
)
1696 "Transcode a TABLE-CELL object from Org to ASCII.
1697 CONTENTS is the cell contents. INFO is a plist used as
1698 a communication channel."
1699 ;; Determine column width. When `org-ascii-table-widen-columns'
1700 ;; is nil and some width cookie has set it, use that value.
1701 ;; Otherwise, compute the maximum width among transcoded data of
1702 ;; each cell in the column.
1703 (let ((width (org-ascii--table-cell-width table-cell info
)))
1704 ;; When contents are too large, truncate them.
1705 (unless (or org-ascii-table-widen-columns
(<= (length contents
) width
))
1706 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1707 ;; Align contents correctly within the cell.
1708 (let* ((indent-tabs-mode nil
)
1711 (org-ascii--justify-string
1713 (org-export-table-cell-alignment table-cell info
)))))
1714 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1716 (concat (format " %s " contents
)
1717 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1718 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1723 (defun org-ascii-table-row (table-row contents info
)
1724 "Transcode a TABLE-ROW element from Org to ASCII.
1725 CONTENTS is the row contents. INFO is a plist used as
1726 a communication channel."
1727 (when (eq (org-element-property :type table-row
) 'standard
)
1730 (lambda (lcorner horiz vert rcorner
)
1734 (org-element-map table-row
'table-cell
1736 (let ((width (org-ascii--table-cell-width cell info
))
1737 (borders (org-export-table-cell-borders cell info
)))
1739 ;; In order to know if CELL starts the row, do
1740 ;; not compare it with the first cell in the
1741 ;; row as there might be a special column.
1742 ;; Instead, compare it with first exportable
1743 ;; cell, obtained with `org-element-map'.
1744 (when (and (memq 'left borders
)
1745 (eq (org-element-map table-row
'table-cell
1749 (make-string (+ 2 width
) (string-to-char horiz
))
1751 ((not (memq 'right borders
)) nil
)
1752 ((eq (car (last (org-element-contents table-row
))) cell
)
1756 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1757 (borders (org-export-table-cell-borders
1758 (org-element-map table-row
'table-cell
'identity info t
)
1761 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1762 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1763 (funcall build-hline
"+" "-" "+" "+")))
1764 ((memq 'above borders
)
1765 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1766 (funcall build-hline
"+" "-" "+" "+"))))
1767 (when (memq 'left borders
) (if utf8p
"│" "|"))
1769 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1770 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1771 (funcall build-hline
"+" "-" "+" "+")))))))
1776 (defun org-ascii-timestamp (timestamp contents info
)
1777 "Transcode a TIMESTAMP object from Org to ASCII.
1778 CONTENTS is nil. INFO is a plist holding contextual information."
1779 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1784 (defun org-ascii-underline (underline contents info
)
1785 "Transcode UNDERLINE from Org to ASCII.
1786 CONTENTS is the text with underline markup. INFO is a plist
1787 holding contextual information."
1788 (format "_%s_" contents
))
1793 (defun org-ascii-verbatim (verbatim contents info
)
1794 "Return a VERBATIM object from Org to ASCII.
1795 CONTENTS is nil. INFO is a plist holding contextual information."
1796 (format org-ascii-verbatim-format
1797 (org-element-property :value verbatim
)))
1802 (defun org-ascii-verse-block (verse-block contents info
)
1803 "Transcode a VERSE-BLOCK element from Org to ASCII.
1804 CONTENTS is verse block contents. INFO is a plist holding
1805 contextual information."
1806 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1807 (org-ascii--indent-string
1808 (org-ascii--justify-string contents verse-width
'left
)
1809 org-ascii-quote-margin
)))
1815 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1816 "Filter controlling number of blank lines after a headline.
1818 HEADLINE is a string representing a transcoded headline.
1819 BACK-END is symbol specifying back-end used for export. INFO is
1820 plist containing the communication channel.
1822 This function only applies to `ascii' back-end. See
1823 `org-ascii-headline-spacing' for information."
1824 (if (not org-ascii-headline-spacing
) headline
1825 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1826 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1828 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1829 "Filter controlling number of blank lines between paragraphs.
1831 TREE is the parse tree. BACK-END is the symbol specifying
1832 back-end used for export. INFO is a plist used as
1833 a communication channel.
1835 See `org-ascii-paragraph-spacing' for information."
1836 (when (wholenump org-ascii-paragraph-spacing
)
1837 (org-element-map tree
'paragraph
1839 (when (eq (org-element-type (org-export-get-next-element p info
))
1841 (org-element-put-property
1842 p
:post-blank org-ascii-paragraph-spacing
)))))
1845 (defun org-ascii-filter-comment-spacing (tree backend info
)
1846 "Filter removing blank lines between comments.
1847 TREE is the parse tree. BACK-END is the symbol specifying
1848 back-end used for export. INFO is a plist used as
1849 a communication channel."
1850 (org-element-map tree
'(comment comment-block
)
1852 (when (memq (org-element-type (org-export-get-next-element c info
))
1853 '(comment comment-block
))
1854 (org-element-put-property c
:post-blank
0))))
1859 ;;; End-user functions
1862 (defun org-ascii-export-as-ascii
1863 (&optional async subtreep visible-only body-only ext-plist
)
1864 "Export current buffer to a text buffer.
1866 If narrowing is active in the current buffer, only export its
1869 If a region is active, export that region.
1871 A non-nil optional argument ASYNC means the process should happen
1872 asynchronously. The resulting buffer should be accessible
1873 through the `org-export-stack' interface.
1875 When optional argument SUBTREEP is non-nil, export the sub-tree
1876 at point, extracting information from the headline properties
1879 When optional argument VISIBLE-ONLY is non-nil, don't export
1880 contents of hidden elements.
1882 When optional argument BODY-ONLY is non-nil, strip title and
1883 table of contents from output.
1885 EXT-PLIST, when provided, is a property list with external
1886 parameters overriding Org default settings, but still inferior to
1887 file-local settings.
1889 Export is done in a buffer named \"*Org ASCII Export*\", which
1890 will be displayed when `org-export-show-temporary-export-buffer'
1894 (org-export-async-start
1896 (with-current-buffer (get-buffer-create "*Org ASCII Export*")
1899 (goto-char (point-min))
1901 (org-export-add-to-stack (current-buffer) 'ascii
)))
1902 `(org-export-as 'ascii
,subtreep
,visible-only
,body-only
1904 (let ((outbuf (org-export-to-buffer
1905 'ascii
"*Org ASCII Export*"
1906 subtreep visible-only body-only ext-plist
)))
1907 (with-current-buffer outbuf
(text-mode))
1908 (when org-export-show-temporary-export-buffer
1909 (switch-to-buffer-other-window outbuf
)))))
1912 (defun org-ascii-export-to-ascii
1913 (&optional async subtreep visible-only body-only ext-plist
)
1914 "Export current buffer to a text file.
1916 If narrowing is active in the current buffer, only export its
1919 If a region is active, export that region.
1921 A non-nil optional argument ASYNC means the process should happen
1922 asynchronously. The resulting file should be accessible through
1923 the `org-export-stack' interface.
1925 When optional argument SUBTREEP is non-nil, export the sub-tree
1926 at point, extracting information from the headline properties
1929 When optional argument VISIBLE-ONLY is non-nil, don't export
1930 contents of hidden elements.
1932 When optional argument BODY-ONLY is non-nil, strip title and
1933 table of contents from output.
1935 EXT-PLIST, when provided, is a property list with external
1936 parameters overriding Org default settings, but still inferior to
1937 file-local settings.
1939 Return output file's name."
1941 (let ((outfile (org-export-output-file-name ".txt" subtreep
)))
1943 (org-export-async-start
1944 (lambda (f) (org-export-add-to-stack f
'ascii
))
1947 'ascii
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
1949 'ascii outfile subtreep visible-only body-only ext-plist
))))
1952 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1953 "Publish an Org file to ASCII.
1955 FILENAME is the filename of the Org file to be published. PLIST
1956 is the property list for the given project. PUB-DIR is the
1957 publishing directory.
1959 Return output file name."
1961 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1964 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1965 "Publish an Org file to Latin-1.
1967 FILENAME is the filename of the Org file to be published. PLIST
1968 is the property list for the given project. PUB-DIR is the
1969 publishing directory.
1971 Return output file name."
1973 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1976 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1977 "Publish an org file to UTF-8.
1979 FILENAME is the filename of the Org file to be published. PLIST
1980 is the property list for the given project. PUB-DIR is the
1981 publishing directory.
1983 Return output file name."
1985 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1991 ;; generated-autoload-file: "org-loaddefs.el"
1992 ;; coding: utf-8-emacs
1995 ;;; ox-ascii.el ends here