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 (node-property . org-ascii-node-property
)
85 (paragraph . org-ascii-paragraph
)
86 (plain-list . org-ascii-plain-list
)
87 (plain-text . org-ascii-plain-text
)
88 (planning . org-ascii-planning
)
89 (property-drawer . org-ascii-property-drawer
)
90 (quote-block . org-ascii-quote-block
)
91 (quote-section . org-ascii-quote-section
)
92 (radio-target . org-ascii-radio-target
)
93 (section . org-ascii-section
)
94 (special-block . org-ascii-special-block
)
95 (src-block . org-ascii-src-block
)
96 (statistics-cookie . org-ascii-statistics-cookie
)
97 (strike-through . org-ascii-strike-through
)
98 (subscript . org-ascii-subscript
)
99 (superscript . org-ascii-superscript
)
100 (table . org-ascii-table
)
101 (table-cell . org-ascii-table-cell
)
102 (table-row . org-ascii-table-row
)
103 (target . org-ascii-target
)
104 (template . org-ascii-template
)
105 (timestamp . org-ascii-timestamp
)
106 (underline . org-ascii-underline
)
107 (verbatim . org-ascii-verbatim
)
108 (verse-block . org-ascii-verse-block
))
109 :export-block
"ASCII"
111 '(?t
"Export to Plain Text"
112 ((?A
"As ASCII buffer"
114 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
117 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
118 (?L
"As Latin1 buffer"
120 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
123 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
124 (?U
"As UTF-8 buffer"
126 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
129 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
130 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
131 (:filter-parse-tree org-ascii-filter-paragraph-spacing
132 org-ascii-filter-comment-spacing
)
133 (:filter-section . org-ascii-filter-headline-blank-lines
))
134 :options-alist
'((:ascii-charset nil nil org-ascii-charset
)))
138 ;;; User Configurable Variables
140 (defgroup org-export-ascii nil
141 "Options for exporting Org mode files to ASCII."
142 :tag
"Org Export ASCII"
145 (defcustom org-ascii-text-width
72
146 "Maximum width of exported text.
147 This number includes margin size, as set in
148 `org-ascii-global-margin'."
149 :group
'org-export-ascii
151 :package-version
'(Org .
"8.0")
154 (defcustom org-ascii-global-margin
0
155 "Width of the left margin, in number of characters."
156 :group
'org-export-ascii
158 :package-version
'(Org .
"8.0")
161 (defcustom org-ascii-inner-margin
2
162 "Width of the inner margin, in number of characters.
163 Inner margin is applied between each headline."
164 :group
'org-export-ascii
166 :package-version
'(Org .
"8.0")
169 (defcustom org-ascii-quote-margin
6
170 "Width of margin used for quoting text, in characters.
171 This margin is applied on both sides of the text."
172 :group
'org-export-ascii
174 :package-version
'(Org .
"8.0")
177 (defcustom org-ascii-inlinetask-width
30
178 "Width of inline tasks, in number of characters.
179 This number ignores any margin."
180 :group
'org-export-ascii
182 :package-version
'(Org .
"8.0")
185 (defcustom org-ascii-headline-spacing
'(1 .
2)
186 "Number of blank lines inserted around headlines.
188 This variable can be set to a cons cell. In that case, its car
189 represents the number of blank lines present before headline
190 contents whereas its cdr reflects the number of blank lines after
193 A nil value replicates the number of blank lines found in the
194 original Org buffer at the same place."
195 :group
'org-export-ascii
197 :package-version
'(Org .
"8.0")
199 (const :tag
"Replicate original spacing" nil
)
200 (cons :tag
"Set an uniform spacing"
201 (integer :tag
"Number of blank lines before contents")
202 (integer :tag
"Number of blank lines after contents"))))
204 (defcustom org-ascii-indented-line-width
'auto
205 "Additional indentation width for the first line in a paragraph.
206 If the value is an integer, indent the first line of each
207 paragraph by this number. If it is the symbol `auto' preserve
208 indentation from original document."
209 :group
'org-export-ascii
211 :package-version
'(Org .
"8.0")
213 (integer :tag
"Number of white spaces characters")
214 (const :tag
"Preserve original width" auto
)))
216 (defcustom org-ascii-paragraph-spacing
'auto
217 "Number of white lines between paragraphs.
218 If the value is an integer, add this number of blank lines
219 between contiguous paragraphs. If is it the symbol `auto', keep
220 the same number of blank lines as in the original document."
221 :group
'org-export-ascii
223 :package-version
'(Org .
"8.0")
225 (integer :tag
"Number of blank lines")
226 (const :tag
"Preserve original spacing" auto
)))
228 (defcustom org-ascii-charset
'ascii
229 "The charset allowed to represent various elements and objects.
231 `ascii' Only use plain ASCII characters
232 `latin1' Include Latin-1 characters
233 `utf-8' Use all UTF-8 characters"
234 :group
'org-export-ascii
236 :package-version
'(Org .
"8.0")
238 (const :tag
"ASCII" ascii
)
239 (const :tag
"Latin-1" latin1
)
240 (const :tag
"UTF-8" utf-8
)))
242 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
244 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
245 "Characters for underlining headings in ASCII export.
247 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
248 and whose value is a list of characters.
250 For each supported charset, this variable associates a sequence
251 of underline characters. In a sequence, the characters will be
252 used in order for headlines level 1, 2, ... If no character is
253 available for a given level, the headline won't be underlined."
254 :group
'org-export-ascii
256 :package-version
'(Org .
"8.0")
258 (cons :tag
"Underline characters sequence"
259 (const :tag
"ASCII charset" ascii
)
261 (cons :tag
"Underline characters sequence"
262 (const :tag
"Latin-1 charset" latin1
)
264 (cons :tag
"Underline characters sequence"
265 (const :tag
"UTF-8 charset" utf-8
)
266 (repeat character
))))
268 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
271 "Bullet characters for headlines converted to lists in ASCII export.
273 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
274 and whose value is a list of characters.
276 The first character is used for the first level considered as low
277 level, and so on. If there are more levels than characters given
278 here, the list will be repeated.
280 Note that this variable doesn't affect plain lists
282 :group
'org-export-ascii
284 :package-version
'(Org .
"8.0")
286 (cons :tag
"Bullet characters for low level headlines"
287 (const :tag
"ASCII charset" ascii
)
289 (cons :tag
"Bullet characters for low level headlines"
290 (const :tag
"Latin-1 charset" latin1
)
292 (cons :tag
"Bullet characters for low level headlines"
293 (const :tag
"UTF-8 charset" utf-8
)
294 (repeat character
))))
296 (defcustom org-ascii-links-to-notes t
297 "Non-nil means convert links to notes before the next headline.
298 When nil, the link will be exported in place. If the line
299 becomes long in this way, it will be wrapped."
300 :group
'org-export-ascii
302 :package-version
'(Org .
"8.0")
305 (defcustom org-ascii-table-keep-all-vertical-lines nil
306 "Non-nil means keep all vertical lines in ASCII tables.
307 When nil, vertical lines will be removed except for those needed
308 for column grouping."
309 :group
'org-export-ascii
311 :package-version
'(Org .
"8.0")
314 (defcustom org-ascii-table-widen-columns t
315 "Non-nil means widen narrowed columns for export.
316 When nil, narrowed columns will look in ASCII export just like in
317 Org mode, i.e. with \"=>\" as ellipsis."
318 :group
'org-export-ascii
320 :package-version
'(Org .
"8.0")
323 (defcustom org-ascii-table-use-ascii-art nil
324 "Non-nil means table.el tables are turned into ascii-art.
326 It only makes sense when export charset is `utf-8'. It is nil by
327 default since it requires ascii-art-to-unicode.el package. You
328 can download it here:
330 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
331 :group
'org-export-ascii
333 :package-version
'(Org .
"8.0")
336 (defcustom org-ascii-caption-above nil
337 "When non-nil, place caption string before the element.
338 Otherwise, place it right after it."
339 :group
'org-export-ascii
341 :package-version
'(Org .
"8.0")
344 (defcustom org-ascii-verbatim-format
"`%s'"
345 "Format string used for verbatim text and inline code."
346 :group
'org-export-ascii
348 :package-version
'(Org .
"8.0")
351 (defcustom org-ascii-format-drawer-function nil
352 "Function called to format a drawer in ASCII.
354 The function must accept three parameters:
355 NAME the drawer name, like \"LOGBOOK\"
356 CONTENTS the contents of the drawer.
357 WIDTH the text width within the drawer.
359 The function should return either the string to be exported or
360 nil to ignore the drawer.
362 For example, the variable could be set to the following function
363 in order to mimic default behaviour:
365 \(defun org-ascii-format-drawer-default (name contents width)
366 \"Format a drawer element for ASCII export.\"
368 :group
'org-export-ascii
370 :package-version
'(Org .
"8.0")
373 (defcustom org-ascii-format-inlinetask-function nil
374 "Function called to format an inlinetask in ASCII.
376 The function must accept six parameters:
377 TODO the todo keyword, as a string
378 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
379 PRIORITY the inlinetask priority, as a string
380 NAME the inlinetask name, as a string.
381 TAGS the inlinetask tags, as a list of strings.
382 CONTENTS the contents of the inlinetask, as a string.
384 The function should return either the string to be exported or
385 nil to ignore the inline task.
387 For example, the variable could be set to the following function
388 in order to mimic default behaviour:
390 \(defun org-ascii-format-inlinetask-default
391 \(todo type priority name tags contents\)
392 \"Format an inline task element for ASCII export.\"
393 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
394 \(width org-ascii-inlinetask-width\)
395 \(org-ascii--indent-string
397 ;; Top line, with an additional blank line if not in UTF-8.
398 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
399 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
400 ;; Add title. Fill it if wider than inlinetask.
401 \(let \(\(title \(org-ascii--build-title inlinetask info width\)\)\)
402 \(if \(<= \(length title\) width\) title
403 \(org-ascii--fill-string title width info\)\)\)
405 ;; If CONTENTS is not empty, insert it along with
407 \(when \(org-string-nw-p contents\)
408 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
410 \(make-string width \(if utf8p ?━ ?_\)\)\)
411 ;; Flush the inlinetask to the right.
412 \(- \(plist-get info :ascii-width\)
413 \(plist-get info :ascii-margin\)
414 \(plist-get info :ascii-inner-margin\)
415 \(org-ascii--current-text-width inlinetask info\)\)"
416 :group
'org-export-ascii
418 :package-version
'(Org .
"8.0")
423 ;;; Internal Functions
425 ;; Internal functions fall into three categories.
427 ;; The first one is about text formatting. The core function is
428 ;; `org-ascii--current-text-width', which determines the current
429 ;; text width allowed to a given element. In other words, it helps
430 ;; keeping each line width within maximum text width defined in
431 ;; `org-ascii-text-width'. Once this information is known,
432 ;; `org-ascii--fill-string', `org-ascii--justify-string',
433 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
434 ;; operate on a given output string.
436 ;; The second category contains functions handling elements listings,
437 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
438 ;; returns a complete table of contents, `org-ascii--list-listings'
439 ;; returns a list of referenceable src-block elements, and
440 ;; `org-ascii--list-tables' does the same for table elements.
442 ;; The third category includes general helper functions.
443 ;; `org-ascii--build-title' creates the title for a given headline
444 ;; or inlinetask element. `org-ascii--build-caption' returns the
445 ;; caption string associated to a table or a src-block.
446 ;; `org-ascii--describe-links' creates notes about links for
447 ;; insertion at the end of a section. It uses
448 ;; `org-ascii--unique-links' to get the list of links to describe.
449 ;; Eventually, `org-ascii--translate' translates a string according
450 ;; to language and charset specification.
453 (defun org-ascii--fill-string (s text-width info
&optional justify
)
454 "Fill a string with specified text-width and return it.
456 S is the string being filled. TEXT-WIDTH is an integer
457 specifying maximum length of a line. INFO is the plist used as
458 a communication channel.
460 Optional argument JUSTIFY can specify any type of justification
461 among `left', `center', `right' or `full'. A nil value is
462 equivalent to `left'. For a justification that doesn't also fill
463 string, see `org-ascii--justify-string'.
465 Return nil if S isn't a string."
466 ;; Don't fill paragraph when break should be preserved.
467 (cond ((not (stringp s
)) nil
)
468 ((plist-get info
:preserve-breaks
) s
)
469 (t (let ((double-space-p sentence-end-double-space
))
471 (let ((fill-column text-width
)
472 (use-hard-newlines t
)
473 (sentence-end-double-space double-space-p
))
475 (fill-region (point-min) (point-max) justify
))
478 (defun org-ascii--justify-string (s text-width how
)
480 TEXT-WIDTH is an integer specifying maximum length of a line.
481 HOW determines the type of justification: it can be `left',
482 `right', `full' or `center'."
485 (goto-char (point-min))
486 (let ((fill-column text-width
)
487 ;; Disable `adaptive-fill-mode' so it doesn't prevent
488 ;; filling lines matching `adaptive-fill-regexp'.
489 (adaptive-fill-mode nil
))
490 (while (< (point) (point-max))
491 (justify-current-line how
)
495 (defun org-ascii--indent-string (s width
)
496 "Indent string S by WIDTH white spaces.
497 Empty lines are not indented."
499 (replace-regexp-in-string
500 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
502 (defun org-ascii--box-string (s info
)
503 "Return string S with a partial box to its left.
504 INFO is a plist used as a communicaton channel."
505 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
506 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
507 (replace-regexp-in-string
508 "^" (if utf8p
"│ " "| ")
509 ;; Remove last newline character.
510 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
512 (defun org-ascii--current-text-width (element info
)
513 "Return maximum text width for ELEMENT's contents.
514 INFO is a plist used as a communication channel."
515 (case (org-element-type element
)
516 ;; Elements with an absolute width: `headline' and `inlinetask'.
517 (inlinetask org-ascii-inlinetask-width
)
519 (- org-ascii-text-width
520 (let ((low-level-rank (org-export-low-level-p element info
)))
521 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
522 ;; Elements with a relative width: store maximum text width in
525 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
526 ;; Total width is determined by the presence, or not, of an
527 ;; inline task among ELEMENT parents.
529 (if (loop for parent in genealogy
530 thereis
(eq (org-element-type parent
) 'inlinetask
))
531 org-ascii-inlinetask-width
532 ;; No inlinetask: Remove global margin from text width.
533 (- org-ascii-text-width
534 org-ascii-global-margin
535 (let ((parent (org-export-get-parent-headline element
)))
536 ;; Inner margin doesn't apply to text before first
539 (let ((low-level-rank
540 (org-export-low-level-p parent info
)))
541 ;; Inner margin doesn't apply to contents of
542 ;; low level headlines, since they've got their
543 ;; own indentation mechanism.
544 (if low-level-rank
(* low-level-rank
2)
545 org-ascii-inner-margin
))))))))
547 ;; Each `quote-block', `quote-section' and `verse-block' above
548 ;; narrows text width by twice the standard margin size.
549 (+ (* (loop for parent in genealogy
550 when
(memq (org-element-type parent
)
551 '(quote-block quote-section verse-block
))
553 2 org-ascii-quote-margin
)
554 ;; Text width within a plain-list is restricted by
555 ;; indentation of current item. If that's the case,
556 ;; compute it with the help of `:structure' property from
557 ;; parent item, if any.
559 (if (eq (org-element-type element
) 'item
) element
560 (loop for parent in genealogy
561 when
(eq (org-element-type parent
) 'item
)
563 (if (not parent-item
) 0
564 ;; Compute indentation offset of the current item,
565 ;; that is the sum of the difference between its
566 ;; indentation and the indentation of the top item in
567 ;; the list and current item bullet's length. Also
568 ;; remove checkbox length, and tag length (for
569 ;; description lists) or bullet length.
570 (let ((struct (org-element-property :structure parent-item
))
571 (beg-item (org-element-property :begin parent-item
)))
572 (+ (- (org-list-get-ind beg-item struct
)
574 (org-list-get-top-point struct
) struct
))
575 (length (org-ascii--checkbox parent-item info
))
577 (or (org-list-get-tag beg-item struct
)
578 (org-list-get-bullet beg-item struct
)))))))))))))
580 (defun org-ascii--build-title
581 (element info text-width
&optional underline notags toc
)
582 "Format ELEMENT title and return it.
584 ELEMENT is either an `headline' or `inlinetask' element. INFO is
585 a plist used as a communication channel. TEXT-WIDTH is an
586 integer representing the maximum length of a line.
588 When optional argument UNDERLINE is non-nil, underline title,
589 without the tags, according to `org-ascii-underline'
592 If optional argument NOTAGS is non-nil, no tags will be added to
595 When optional argument TOC is non-nil, use optional title if
596 possible. It doesn't apply to `inlinetask' elements."
597 (let* ((headlinep (eq (org-element-type element
) 'headline
))
599 ;; Numbering is specific to headlines.
600 (and headlinep
(org-export-numbered-headline-p element info
)
601 ;; All tests passed: build numbering string.
605 (org-export-get-headline-number element info
) ".")
610 (if (and toc headlinep
) (org-export-get-alt-title element info
)
611 (org-element-property :title element
))
614 (and (plist-get info
:with-todo-keywords
)
615 (let ((todo (org-element-property :todo-keyword element
)))
616 (and todo
(concat (org-export-data todo info
) " ")))))
617 (tags (and (not notags
)
618 (plist-get info
:with-tags
)
619 (let ((tag-list (org-export-get-tags element info
)))
622 (mapconcat 'identity tag-list
":"))))))
624 (and (plist-get info
:with-priority
)
625 (let ((char (org-element-property :priority element
)))
626 (and char
(format "(#%c) " char
)))))
627 (first-part (concat numbers todo priority text
)))
630 ;; Align tags, if any.
634 (max (- text-width
(1+ (length first-part
))) (length tags
)))
636 ;; Maybe underline text, if ELEMENT type is `headline' and an
637 ;; underline character has been defined.
638 (when (and underline headlinep
)
640 (nth (1- (org-export-get-relative-level element info
))
641 (cdr (assq (plist-get info
:ascii-charset
)
642 org-ascii-underline
)))))
645 (make-string (length first-part
) under-char
))))))))
647 (defun org-ascii--has-caption-p (element info
)
648 "Non-nil when ELEMENT has a caption affiliated keyword.
649 INFO is a plist used as a communication channel. This function
650 is meant to be used as a predicate for `org-export-get-ordinal'."
651 (org-element-property :caption element
))
653 (defun org-ascii--build-caption (element info
)
654 "Return caption string for ELEMENT, if applicable.
656 INFO is a plist used as a communication channel.
658 The caption string contains the sequence number of ELEMENT along
659 with its real caption. Return nil when ELEMENT has no affiliated
661 (let ((caption (org-export-get-caption element
)))
663 ;; Get sequence number of current src-block among every
664 ;; src-block with a caption.
666 (org-export-get-ordinal
667 element info nil
'org-ascii--has-caption-p
))
668 (title-fmt (org-ascii--translate
669 (case (org-element-type element
)
671 (src-block "Listing %d:"))
673 (org-ascii--fill-string
674 (concat (format title-fmt reference
)
676 (org-export-data caption info
))
677 (org-ascii--current-text-width element info
) info
)))))
679 (defun org-ascii--build-toc (info &optional n keyword
)
680 "Return a table of contents.
682 INFO is a plist used as a communication channel.
684 Optional argument N, when non-nil, is an integer specifying the
687 Optional argument KEYWORD specifies the TOC keyword, if any, from
688 which the table of contents generation has been initiated."
689 (let ((title (org-ascii--translate "Table of Contents" info
)))
692 (make-string (length title
)
693 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
696 (if keyword
(org-ascii--current-text-width keyword info
)
697 (- org-ascii-text-width org-ascii-global-margin
))))
700 (let* ((level (org-export-get-relative-level headline info
))
701 (indent (* (1- level
) 3)))
703 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
704 (org-ascii--build-title
705 headline info
(- text-width indent
) nil
706 (or (not (plist-get info
:with-tags
))
707 (eq (plist-get info
:with-tags
) 'not-in-toc
))
709 (org-export-collect-headlines info n
) "\n")))))
711 (defun org-ascii--list-listings (keyword info
)
712 "Return a list of listings.
714 KEYWORD is the keyword that initiated the list of listings
715 generation. INFO is a plist used as a communication channel."
716 (let ((title (org-ascii--translate "List of Listings" info
)))
719 (make-string (length title
)
720 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
723 (if keyword
(org-ascii--current-text-width keyword info
)
724 (- org-ascii-text-width org-ascii-global-margin
)))
725 ;; Use a counter instead of retreiving ordinal of each
730 ;; Store initial text so its length can be computed. This is
731 ;; used to properly align caption right to it in case of
732 ;; filling (like contents of a description list item).
734 (format (org-ascii--translate "Listing %d:" info
)
739 (org-ascii--indent-string
740 (org-ascii--fill-string
741 ;; Use short name in priority, if available.
742 (let ((caption (or (org-export-get-caption src-block t
)
743 (org-export-get-caption src-block
))))
744 (org-export-data caption info
))
745 (- text-width
(length initial-text
)) info
)
746 (length initial-text
))))))
747 (org-export-collect-listings info
) "\n")))))
749 (defun org-ascii--list-tables (keyword info
)
750 "Return a list of tables.
752 KEYWORD is the keyword that initiated the list of tables
753 generation. INFO is a plist used as a communication channel."
754 (let ((title (org-ascii--translate "List of Tables" info
)))
757 (make-string (length title
)
758 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
761 (if keyword
(org-ascii--current-text-width keyword info
)
762 (- org-ascii-text-width org-ascii-global-margin
)))
763 ;; Use a counter instead of retreiving ordinal of each
768 ;; Store initial text so its length can be computed. This is
769 ;; used to properly align caption right to it in case of
770 ;; filling (like contents of a description list item).
772 (format (org-ascii--translate "Table %d:" info
)
777 (org-ascii--indent-string
778 (org-ascii--fill-string
779 ;; Use short name in priority, if available.
780 (let ((caption (or (org-export-get-caption table t
)
781 (org-export-get-caption table
))))
782 (org-export-data caption info
))
783 (- text-width
(length initial-text
)) info
)
784 (length initial-text
))))))
785 (org-export-collect-tables info
) "\n")))))
787 (defun org-ascii--unique-links (element info
)
788 "Return a list of unique link references in ELEMENT.
790 ELEMENT is either a headline element or a section element. INFO
791 is a plist used as a communication channel."
795 ;; Return LINK if it wasn't referenced so far, or nil.
796 ;; Update SEEN links along the way.
799 (cons (org-element-property :raw-link link
)
800 (org-element-contents link
))))
801 ;; Ignore LINK if it hasn't been translated already.
802 ;; It can happen if it is located in an affiliated
803 ;; keyword that was ignored.
804 (when (and (org-string-nw-p
805 (gethash link
(plist-get info
:exported-data
)))
806 (not (member footprint seen
)))
807 (push footprint seen
) link
)))))
808 ;; If at a section, find parent headline, if any, in order to
809 ;; count links that might be in the title.
811 (if (eq (org-element-type element
) 'headline
) element
812 (or (org-export-get-parent-headline element
) element
))))
813 ;; Get all links in HEADLINE.
814 (org-element-map headline
'link
815 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
817 (defun org-ascii--describe-links (links width info
)
818 "Return a string describing a list of links.
820 LINKS is a list of link type objects, as returned by
821 `org-ascii--unique-links'. WIDTH is the text width allowed for
822 the output string. INFO is a plist used as a communication
826 (let ((type (org-element-property :type link
))
827 (anchor (let ((desc (org-element-contents link
)))
828 (if desc
(org-export-data desc info
)
829 (org-element-property :raw-link link
)))))
831 ;; Coderefs, radio links and fuzzy links are ignored.
832 ((member type
'("coderef" "radio" "fuzzy")) nil
)
833 ;; Id and custom-id links: Headlines refer to their numbering.
834 ((member type
'("custom-id" "id"))
835 (let ((dest (org-export-resolve-id-link link info
)))
837 (org-ascii--fill-string
841 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
843 (org-ascii--translate "See section %s" info
)
844 (mapconcat 'number-to-string
845 (org-export-get-headline-number dest info
) "."))))
846 width info
) "\n\n")))
847 ;; Do not add a link that cannot be resolved and doesn't have
848 ;; any description: destination is already visible in the
850 ((not (org-element-contents link
)) nil
)
853 (org-ascii--fill-string
854 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
859 (defun org-ascii--checkbox (item info
)
860 "Return checkbox string for ITEM or nil.
861 INFO is a plist used as a communication channel."
862 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
863 (case (org-element-property :checkbox item
)
864 (on (if utf8p
"☑ " "[X] "))
865 (off (if utf8p
"☐ " "[ ] "))
866 (trans (if utf8p
"☒ " "[-] ")))))
872 (defun org-ascii-template--document-title (info)
873 "Return document title, as a string.
874 INFO is a plist used as a communication channel."
875 (let* ((text-width org-ascii-text-width
)
876 ;; Links in the title will not be resolved later, so we make
877 ;; sure their path is located right after them.
878 (org-ascii-links-to-notes nil
)
879 (title (org-export-data (plist-get info
:title
) info
))
880 (author (and (plist-get info
:with-author
)
881 (let ((auth (plist-get info
:author
)))
882 (and auth
(org-export-data auth info
)))))
883 (email (and (plist-get info
:with-email
)
884 (org-export-data (plist-get info
:email
) info
)))
885 (date (and (plist-get info
:with-date
)
886 (org-export-data (org-export-get-date info
) info
))))
887 ;; There are two types of title blocks depending on the presence
888 ;; of a title to display.
889 (if (string= title
"")
890 ;; Title block without a title. DATE is positioned at the top
891 ;; right of the document, AUTHOR to the top left and EMAIL
894 ((and (org-string-nw-p date
) (org-string-nw-p author
))
897 (make-string (- text-width
(length date
) (length author
)) ?
)
899 (when (org-string-nw-p email
) (concat "\n" email
))
901 ((and (org-string-nw-p date
) (org-string-nw-p email
))
904 (make-string (- text-width
(length date
) (length email
)) ?
)
906 ((org-string-nw-p date
)
908 (org-ascii--justify-string date text-width
'right
)
910 ((and (org-string-nw-p author
) (org-string-nw-p email
))
911 (concat author
"\n" email
"\n\n\n"))
912 ((org-string-nw-p author
) (concat author
"\n\n\n"))
913 ((org-string-nw-p email
) (concat email
"\n\n\n")))
914 ;; Title block with a title. Document's TITLE, along with the
915 ;; AUTHOR and its EMAIL are both overlined and an underlined,
916 ;; centered. Date is just below, also centered.
917 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
918 ;; Format TITLE. It may be filled if it is too wide,
919 ;; that is wider than the two thirds of the total width.
920 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
921 (formatted-title (org-ascii--fill-string title title-len info
))
924 (min (+ (max title-len
(length author
) (length email
)) 2)
925 text-width
) (if utf8p ?━ ?_
))))
926 (org-ascii--justify-string
929 (upcase formatted-title
)
931 ((and (org-string-nw-p author
) (org-string-nw-p email
))
932 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
933 ((org-string-nw-p author
)
934 (concat (if utf8p
"\n\n\n" "\n\n") author
))
935 ((org-string-nw-p email
)
936 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
938 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
939 "\n\n\n") text-width
'center
)))))
941 (defun org-ascii-inner-template (contents info
)
942 "Return complete document string after ASCII conversion.
943 CONTENTS is the transcoded contents string. INFO is a plist
944 holding export options."
945 (org-element-normalize-string
946 (org-ascii--indent-string
948 ;; 1. Document's body.
950 ;; 2. Footnote definitions.
951 (let ((definitions (org-export-collect-footnote-definitions
952 (plist-get info
:parse-tree
) info
))
953 ;; Insert full links right inside the footnote definition
954 ;; as they have no chance to be inserted later.
955 (org-ascii-links-to-notes nil
))
959 (let ((title (org-ascii--translate "Footnotes" info
)))
964 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
966 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
969 (let ((id (format "[%s] " (car ref
))))
970 ;; Distinguish between inline definitions and
971 ;; full-fledged definitions.
973 (let ((def (nth 2 ref
)))
974 (if (eq (org-element-type def
) 'org-data
)
975 ;; Full-fledged definition: footnote ID is
976 ;; inserted inside the first parsed paragraph
977 ;; (FIRST), if any, to be sure filling will
978 ;; take it into consideration.
979 (let ((first (car (org-element-contents def
))))
980 (if (not (eq (org-element-type first
) 'paragraph
))
981 (concat id
"\n" (org-export-data def info
))
982 (push id
(nthcdr 2 first
))
983 (org-export-data def info
)))
984 ;; Fill paragraph once footnote ID is inserted
985 ;; in order to have a correct length for first
987 (org-ascii--fill-string
988 (concat id
(org-export-data def info
))
989 text-width info
))))))
990 definitions
"\n\n"))))))
991 org-ascii-global-margin
)))
993 (defun org-ascii-template (contents info
)
994 "Return complete document string after ASCII conversion.
995 CONTENTS is the transcoded contents string. INFO is a plist
996 holding export options."
998 ;; 1. Build title block.
999 (org-ascii--indent-string
1000 (concat (org-ascii-template--document-title info
)
1001 ;; 2. Table of contents.
1002 (let ((depth (plist-get info
:with-toc
)))
1005 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
1007 org-ascii-global-margin
)
1008 ;; 3. Document's body.
1010 ;; 4. Creator. Ignore `comment' value as there are no comments in
1011 ;; ASCII. Justify it to the bottom right.
1012 (org-ascii--indent-string
1013 (let ((creator-info (plist-get info
:with-creator
))
1014 (text-width (- org-ascii-text-width org-ascii-global-margin
)))
1015 (unless (or (not creator-info
) (eq creator-info
'comment
))
1018 (org-ascii--fill-string
1019 (plist-get info
:creator
) text-width info
'right
))))
1020 org-ascii-global-margin
)))
1022 (defun org-ascii--translate (s info
)
1023 "Translate string S according to specified language and charset.
1024 INFO is a plist used as a communication channel."
1025 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1026 (org-export-translate s charset info
)))
1030 ;;; Transcode Functions
1034 (defun org-ascii-bold (bold contents info
)
1035 "Transcode BOLD from Org to ASCII.
1036 CONTENTS is the text with bold markup. INFO is a plist holding
1037 contextual information."
1038 (format "*%s*" contents
))
1043 (defun org-ascii-center-block (center-block contents info
)
1044 "Transcode a CENTER-BLOCK element from Org to ASCII.
1045 CONTENTS holds the contents of the block. INFO is a plist
1046 holding contextual information."
1047 (org-ascii--justify-string
1048 contents
(org-ascii--current-text-width center-block info
) 'center
))
1053 (defun org-ascii-clock (clock contents info
)
1054 "Transcode a CLOCK object from Org to ASCII.
1055 CONTENTS is nil. INFO is a plist holding contextual
1057 (concat org-clock-string
" "
1059 (org-element-property :raw-value
1060 (org-element-property :value clock
)))
1061 (let ((time (org-element-property :duration clock
)))
1066 (org-split-string time
":")))))))
1071 (defun org-ascii-code (code contents info
)
1072 "Return a CODE object from Org to ASCII.
1073 CONTENTS is nil. INFO is a plist holding contextual
1075 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1080 (defun org-ascii-drawer (drawer contents info
)
1081 "Transcode a DRAWER element from Org to ASCII.
1082 CONTENTS holds the contents of the block. INFO is a plist
1083 holding contextual information."
1084 (let ((name (org-element-property :drawer-name drawer
))
1085 (width (org-ascii--current-text-width drawer info
)))
1086 (if (functionp org-ascii-format-drawer-function
)
1087 (funcall org-ascii-format-drawer-function name contents width
)
1088 ;; If there's no user defined function: simply
1089 ;; display contents of the drawer.
1095 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1096 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1097 CONTENTS holds the contents of the block. INFO is a plist
1098 holding contextual information."
1104 (defun org-ascii-entity (entity contents info
)
1105 "Transcode an ENTITY object from Org to ASCII.
1106 CONTENTS are the definition itself. INFO is a plist holding
1107 contextual information."
1108 (org-element-property
1109 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1115 (defun org-ascii-example-block (example-block contents info
)
1116 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1117 CONTENTS is nil. INFO is a plist holding contextual information."
1118 (org-ascii--box-string
1119 (org-export-format-code-default example-block info
) info
))
1124 (defun org-ascii-export-snippet (export-snippet contents info
)
1125 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1126 CONTENTS is nil. INFO is a plist holding contextual information."
1127 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1128 (org-element-property :value export-snippet
)))
1133 (defun org-ascii-export-block (export-block contents info
)
1134 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1135 CONTENTS is nil. INFO is a plist holding contextual information."
1136 (when (string= (org-element-property :type export-block
) "ASCII")
1137 (org-remove-indentation (org-element-property :value export-block
))))
1142 (defun org-ascii-fixed-width (fixed-width contents info
)
1143 "Transcode a FIXED-WIDTH element from Org to ASCII.
1144 CONTENTS is nil. INFO is a plist holding contextual information."
1145 (org-ascii--box-string
1146 (org-remove-indentation
1147 (org-element-property :value fixed-width
)) info
))
1150 ;;;; Footnote Definition
1152 ;; Footnote Definitions are ignored. They are compiled at the end of
1153 ;; the document, by `org-ascii-inner-template'.
1156 ;;;; Footnote Reference
1158 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1159 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1160 CONTENTS is nil. INFO is a plist holding contextual information."
1161 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1166 (defun org-ascii-headline (headline contents info
)
1167 "Transcode a HEADLINE element from Org to ASCII.
1168 CONTENTS holds the contents of the headline. INFO is a plist
1169 holding contextual information."
1170 ;; Don't export footnote section, which will be handled at the end
1172 (unless (org-element-property :footnote-section-p headline
)
1173 (let* ((low-level-rank (org-export-low-level-p headline info
))
1174 (width (org-ascii--current-text-width headline info
))
1175 ;; Blank lines between headline and its contents.
1176 ;; `org-ascii-headline-spacing', when set, overwrites
1177 ;; original buffer's spacing.
1180 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1181 (org-element-property :pre-blank headline
)) ?
\n))
1182 ;; Even if HEADLINE has no section, there might be some
1183 ;; links in its title that we shouldn't forget to describe.
1185 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1186 (let ((title (org-element-property :title headline
)))
1188 (org-ascii--describe-links
1189 (org-ascii--unique-links title info
) width info
))))))
1190 ;; Deep subtree: export it as a list item.
1194 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1195 org-ascii-bullets
))))
1197 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1200 (org-ascii--build-title headline info width
) "\n"
1201 ;; Contents, indented by length of bullet.
1203 (org-ascii--indent-string
1205 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1207 ;; Else: Standard headline.
1209 (org-ascii--build-title headline info width
'underline
)
1211 (concat (when (org-string-nw-p links
) links
) contents
))))))
1214 ;;;; Horizontal Rule
1216 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1217 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1218 CONTENTS is nil. INFO is a plist holding contextual
1220 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1222 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1223 (org-ascii--justify-string
1224 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1225 (string-to-number spec-width
)
1227 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1228 text-width
'center
)))
1231 ;;;; Inline Src Block
1233 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1234 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1235 CONTENTS holds the contents of the item. INFO is a plist holding
1236 contextual information."
1237 (format org-ascii-verbatim-format
1238 (org-element-property :value inline-src-block
)))
1243 (defun org-ascii-inlinetask (inlinetask contents info
)
1244 "Transcode an INLINETASK element from Org to ASCII.
1245 CONTENTS holds the contents of the block. INFO is a plist
1246 holding contextual information."
1247 (let ((width (org-ascii--current-text-width inlinetask info
)))
1248 ;; If `org-ascii-format-inlinetask-function' is provided, call it
1249 ;; with appropriate arguments.
1250 (if (functionp org-ascii-format-inlinetask-function
)
1251 (funcall org-ascii-format-inlinetask-function
1253 (and (plist-get info
:with-todo-keywords
)
1254 (let ((todo (org-element-property
1255 :todo-keyword inlinetask
)))
1256 (and todo
(org-export-data todo info
))))
1258 (org-element-property :todo-type inlinetask
)
1260 (and (plist-get info
:with-priority
)
1261 (org-element-property :priority inlinetask
))
1263 (org-export-data (org-element-property :title inlinetask
) info
)
1265 (and (plist-get info
:with-tags
)
1266 (org-element-property :tags inlinetask
))
1267 ;; contents and width
1269 ;; Otherwise, use a default template.
1270 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1271 (org-ascii--indent-string
1273 ;; Top line, with an additional blank line if not in UTF-8.
1274 (make-string width
(if utf8p ?━ ?_
)) "\n"
1275 (unless utf8p
(concat (make-string width ?
) "\n"))
1276 ;; Add title. Fill it if wider than inlinetask.
1277 (let ((title (org-ascii--build-title inlinetask info width
)))
1278 (if (<= (length title
) width
) title
1279 (org-ascii--fill-string title width info
)))
1281 ;; If CONTENTS is not empty, insert it along with
1283 (when (org-string-nw-p contents
)
1284 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1286 (make-string width
(if utf8p ?━ ?_
)))
1287 ;; Flush the inlinetask to the right.
1288 (- org-ascii-text-width org-ascii-global-margin
1289 (if (not (org-export-get-parent-headline inlinetask
)) 0
1290 org-ascii-inner-margin
)
1291 (org-ascii--current-text-width inlinetask info
)))))))
1296 (defun org-ascii-italic (italic contents info
)
1297 "Transcode italic from Org to ASCII.
1298 CONTENTS is the text with italic markup. INFO is a plist holding
1299 contextual information."
1300 (format "/%s/" contents
))
1305 (defun org-ascii-item (item contents info
)
1306 "Transcode an ITEM element from Org to ASCII.
1307 CONTENTS holds the contents of the item. INFO is a plist holding
1308 contextual information."
1309 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1310 (checkbox (org-ascii--checkbox item info
))
1311 (list-type (org-element-property :type
(org-export-get-parent item
)))
1313 ;; First parent of ITEM is always the plain-list. Get
1314 ;; `:type' property from it.
1315 (org-list-bullet-string
1319 (org-export-data (org-element-property :tag item
) info
)
1322 ;; Return correct number for ITEM, paying attention to
1324 (let* ((struct (org-element-property :structure item
))
1325 (bul (org-element-property :bullet item
))
1326 (num (number-to-string
1327 (car (last (org-list-get-item-number
1328 (org-element-property :begin item
)
1330 (org-list-prevs-alist struct
)
1331 (org-list-parents-alist struct
)))))))
1332 (replace-regexp-in-string "[0-9]+" num bul
)))
1333 (t (let ((bul (org-element-property :bullet item
)))
1334 ;; Change bullets into more visible form if UTF-8 is active.
1336 (replace-regexp-in-string
1338 (replace-regexp-in-string
1340 (replace-regexp-in-string "*" "‣" bul
))))))))))
1343 (unless (eq list-type
'descriptive
) checkbox
)
1344 ;; Contents: Pay attention to indentation. Note: check-boxes are
1345 ;; already taken care of at the paragraph level so they don't
1346 ;; interfere with indentation.
1347 (let ((contents (org-ascii--indent-string contents
(length bullet
))))
1348 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1350 (concat "\n" contents
))))))
1355 (defun org-ascii-keyword (keyword contents info
)
1356 "Transcode a KEYWORD element from Org to ASCII.
1357 CONTENTS is nil. INFO is a plist holding contextual
1359 (let ((key (org-element-property :key keyword
))
1360 (value (org-element-property :value keyword
)))
1362 ((string= key
"ASCII") value
)
1363 ((string= key
"TOC")
1364 (let ((value (downcase value
)))
1366 ((string-match "\\<headlines\\>" value
)
1367 (let ((depth (or (and (string-match "[0-9]+" value
)
1368 (string-to-number (match-string 0 value
)))
1369 (plist-get info
:with-toc
))))
1370 (org-ascii--build-toc
1371 info
(and (wholenump depth
) depth
) keyword
)))
1372 ((string= "tables" value
)
1373 (org-ascii--list-tables keyword info
))
1374 ((string= "listings" value
)
1375 (org-ascii--list-listings keyword info
))))))))
1378 ;;;; Latex Environment
1380 (defun org-ascii-latex-environment (latex-environment contents info
)
1381 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1382 CONTENTS is nil. INFO is a plist holding contextual
1384 (when (plist-get info
:with-latex
)
1385 (org-remove-indentation (org-element-property :value latex-environment
))))
1390 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1391 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1392 CONTENTS is nil. INFO is a plist holding contextual
1394 (when (plist-get info
:with-latex
)
1395 (org-element-property :value latex-fragment
)))
1400 (defun org-ascii-line-break (line-break contents info
)
1401 "Transcode a LINE-BREAK object from Org to ASCII.
1402 CONTENTS is nil. INFO is a plist holding contextual
1403 information." hard-newline
)
1408 (defun org-ascii-link (link desc info
)
1409 "Transcode a LINK object from Org to ASCII.
1411 DESC is the description part of the link, or the empty string.
1412 INFO is a plist holding contextual information."
1413 (let ((raw-link (org-element-property :raw-link link
))
1414 (type (org-element-property :type link
)))
1416 ((string= type
"coderef")
1417 (let ((ref (org-element-property :path link
)))
1418 (format (org-export-get-coderef-format ref desc
)
1419 (org-export-resolve-coderef ref info
))))
1420 ;; Do not apply a special syntax on radio links. Though, use
1421 ;; transcoded target's contents as output.
1422 ((string= type
"radio")
1423 (let ((destination (org-export-resolve-radio-link link info
)))
1425 (org-export-data (org-element-contents destination
) info
))))
1426 ;; Do not apply a special syntax on fuzzy links pointing to
1428 ((string= type
"fuzzy")
1429 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1430 (if (org-string-nw-p desc
) desc
1433 (org-export-get-ordinal
1434 destination info nil
'org-ascii--has-caption-p
)))
1436 (if (atom number
) (number-to-string number
)
1437 (mapconcat 'number-to-string number
"."))))))))
1439 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1441 (format "[%s]" desc
)
1442 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1445 ;;;; Node Properties
1447 (defun org-ascii-node-property (node-property contents info
)
1448 "Transcode a NODE-PROPERTY element from Org to ASCII.
1449 CONTENTS is nil. INFO is a plist holding contextual
1452 (org-element-property :key node-property
)
1453 (let ((value (org-element-property :value node-property
)))
1454 (if value
(concat " " value
) ""))))
1459 (defun org-ascii-paragraph (paragraph contents info
)
1460 "Transcode a PARAGRAPH element from Org to ASCII.
1461 CONTENTS is the contents of the paragraph, as a string. INFO is
1462 the plist used as a communication channel."
1463 (let ((contents (if (not (wholenump org-ascii-indented-line-width
)) contents
1465 (make-string org-ascii-indented-line-width ?
)
1466 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1467 (org-ascii--fill-string
1468 contents
(org-ascii--current-text-width paragraph info
) info
)))
1473 (defun org-ascii-plain-list (plain-list contents info
)
1474 "Transcode a PLAIN-LIST element from Org to ASCII.
1475 CONTENTS is the contents of the list. INFO is a plist holding
1476 contextual information."
1482 (defun org-ascii-plain-text (text info
)
1483 "Transcode a TEXT string from Org to ASCII.
1484 INFO is a plist used as a communication channel."
1485 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1486 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1487 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1488 (if (not (plist-get info
:with-special-strings
)) text
1489 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1490 (if (not utf8p
) text
1491 ;; Usual replacements in utf-8 with proper option set.
1492 (replace-regexp-in-string
1494 (replace-regexp-in-string
1496 (replace-regexp-in-string "---" "—" text
)))))))
1501 (defun org-ascii-planning (planning contents info
)
1502 "Transcode a PLANNING element from Org to ASCII.
1503 CONTENTS is nil. INFO is a plist used as a communication
1508 (list (let ((closed (org-element-property :closed planning
)))
1510 (concat org-closed-string
" "
1512 (org-element-property :raw-value closed
)))))
1513 (let ((deadline (org-element-property :deadline planning
)))
1515 (concat org-deadline-string
" "
1517 (org-element-property :raw-value deadline
)))))
1518 (let ((scheduled (org-element-property :scheduled planning
)))
1520 (concat org-scheduled-string
" "
1522 (org-element-property :raw-value scheduled
)))))))
1526 ;;;; Property Drawer
1528 (defun org-ascii-property-drawer (property-drawer contents info
)
1529 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1530 CONTENTS holds the contents of the drawer. INFO is a plist
1531 holding contextual information."
1532 (org-string-nw-p contents
))
1537 (defun org-ascii-quote-block (quote-block contents info
)
1538 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1539 CONTENTS holds the contents of the block. INFO is a plist
1540 holding contextual information."
1541 (org-ascii--indent-string contents org-ascii-quote-margin
))
1546 (defun org-ascii-quote-section (quote-section contents info
)
1547 "Transcode a QUOTE-SECTION element from Org to ASCII.
1548 CONTENTS is nil. INFO is a plist holding contextual information."
1549 (let ((width (org-ascii--current-text-width quote-section info
))
1552 (org-remove-indentation (org-element-property :value quote-section
))
1554 (org-ascii--indent-string
1556 (+ org-ascii-quote-margin
1557 ;; Don't apply inner margin if parent headline is low level.
1558 (let ((headline (org-export-get-parent-headline quote-section
)))
1559 (if (org-export-low-level-p headline info
) 0
1560 org-ascii-inner-margin
))))))
1565 (defun org-ascii-radio-target (radio-target contents info
)
1566 "Transcode a RADIO-TARGET object from Org to ASCII.
1567 CONTENTS is the contents of the target. INFO is a plist holding
1568 contextual information."
1574 (defun org-ascii-section (section contents info
)
1575 "Transcode a SECTION element from Org to ASCII.
1576 CONTENTS is the contents of the section. INFO is a plist holding
1577 contextual information."
1578 (org-ascii--indent-string
1581 (when org-ascii-links-to-notes
1582 ;; Add list of links at the end of SECTION.
1583 (let ((links (org-ascii--describe-links
1584 (org-ascii--unique-links section info
)
1585 (org-ascii--current-text-width section info
) info
)))
1586 ;; Separate list of links and section contents.
1587 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1588 ;; Do not apply inner margin if parent headline is low level.
1589 (let ((headline (org-export-get-parent-headline section
)))
1590 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1591 org-ascii-inner-margin
))))
1596 (defun org-ascii-special-block (special-block contents info
)
1597 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1598 CONTENTS holds the contents of the block. INFO is a plist
1599 holding contextual information."
1605 (defun org-ascii-src-block (src-block contents info
)
1606 "Transcode a SRC-BLOCK element from Org to ASCII.
1607 CONTENTS holds the contents of the item. INFO is a plist holding
1608 contextual information."
1609 (let ((caption (org-ascii--build-caption src-block info
))
1610 (code (org-export-format-code-default src-block info
)))
1611 (if (equal code
"") ""
1613 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1614 (org-ascii--box-string code info
)
1615 (when (and caption
(not org-ascii-caption-above
))
1616 (concat "\n" caption
))))))
1619 ;;;; Statistics Cookie
1621 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1622 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1623 CONTENTS is nil. INFO is a plist holding contextual information."
1624 (org-element-property :value statistics-cookie
))
1629 (defun org-ascii-subscript (subscript contents info
)
1630 "Transcode a SUBSCRIPT object from Org to ASCII.
1631 CONTENTS is the contents of the object. INFO is a plist holding
1632 contextual information."
1633 (if (org-element-property :use-brackets-p subscript
)
1634 (format "_{%s}" contents
)
1635 (format "_%s" contents
)))
1640 (defun org-ascii-superscript (superscript contents info
)
1641 "Transcode a SUPERSCRIPT object from Org to ASCII.
1642 CONTENTS is the contents of the object. INFO is a plist holding
1643 contextual information."
1644 (if (org-element-property :use-brackets-p superscript
)
1645 (format "_{%s}" contents
)
1646 (format "_%s" contents
)))
1651 (defun org-ascii-strike-through (strike-through contents info
)
1652 "Transcode STRIKE-THROUGH from Org to ASCII.
1653 CONTENTS is text with strike-through markup. INFO is a plist
1654 holding contextual information."
1655 (format "+%s+" contents
))
1660 (defun org-ascii-table (table contents info
)
1661 "Transcode a TABLE element from Org to ASCII.
1662 CONTENTS is the contents of the table. INFO is a plist holding
1663 contextual information."
1664 (let ((caption (org-ascii--build-caption table info
)))
1666 ;; Possibly add a caption string above.
1667 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1668 ;; Insert table. Note: "table.el" tables are left unmodified.
1669 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1670 ((and org-ascii-table-use-ascii-art
1671 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1672 (require 'ascii-art-to-unicode nil t
))
1674 (insert (org-remove-indentation
1675 (org-element-property :value table
)))
1676 (goto-char (point-min))
1678 (goto-char (point-max))
1679 (skip-chars-backward " \r\t\n")
1680 (buffer-substring (point-min) (point))))
1681 (t (org-remove-indentation (org-element-property :value table
))))
1682 ;; Possible add a caption string below.
1683 (and (not org-ascii-caption-above
) caption
))))
1688 (defun org-ascii--table-cell-width (table-cell info
)
1689 "Return width of TABLE-CELL.
1691 INFO is a plist used as a communication channel.
1693 Width of a cell is determined either by a width cookie in the
1694 same column as the cell, or by the maximum cell's length in that
1697 When `org-ascii-table-widen-columns' is non-nil, width cookies
1699 (let* ((row (org-export-get-parent table-cell
))
1700 (table (org-export-get-parent row
))
1701 (col (let ((cells (org-element-contents row
)))
1702 (- (length cells
) (length (memq table-cell cells
)))))
1704 (or (plist-get info
:ascii-table-cell-width-cache
)
1705 (plist-get (setq info
1706 (plist-put info
:ascii-table-cell-width-cache
1707 (make-hash-table :test
'equal
)))
1708 :ascii-table-cell-width-cache
)))
1709 (key (cons table col
)))
1710 (or (gethash key cache
)
1713 (or (and (not org-ascii-table-widen-columns
)
1714 (org-export-table-cell-width table-cell info
))
1715 (let* ((max-width 0))
1716 (org-element-map table
'table-row
1721 (org-element-contents
1722 (elt (org-element-contents row
) col
))
1729 (defun org-ascii-table-cell (table-cell contents info
)
1730 "Transcode a TABLE-CELL object from Org to ASCII.
1731 CONTENTS is the cell contents. INFO is a plist used as
1732 a communication channel."
1733 ;; Determine column width. When `org-ascii-table-widen-columns'
1734 ;; is nil and some width cookie has set it, use that value.
1735 ;; Otherwise, compute the maximum width among transcoded data of
1736 ;; each cell in the column.
1737 (let ((width (org-ascii--table-cell-width table-cell info
)))
1738 ;; When contents are too large, truncate them.
1739 (unless (or org-ascii-table-widen-columns
(<= (length contents
) width
))
1740 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1741 ;; Align contents correctly within the cell.
1742 (let* ((indent-tabs-mode nil
)
1745 (org-ascii--justify-string
1747 (org-export-table-cell-alignment table-cell info
)))))
1748 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1750 (concat (format " %s " contents
)
1751 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1752 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1757 (defun org-ascii-table-row (table-row contents info
)
1758 "Transcode a TABLE-ROW element from Org to ASCII.
1759 CONTENTS is the row contents. INFO is a plist used as
1760 a communication channel."
1761 (when (eq (org-element-property :type table-row
) 'standard
)
1764 (lambda (lcorner horiz vert rcorner
)
1768 (org-element-map table-row
'table-cell
1770 (let ((width (org-ascii--table-cell-width cell info
))
1771 (borders (org-export-table-cell-borders cell info
)))
1773 ;; In order to know if CELL starts the row, do
1774 ;; not compare it with the first cell in the
1775 ;; row as there might be a special column.
1776 ;; Instead, compare it with first exportable
1777 ;; cell, obtained with `org-element-map'.
1778 (when (and (memq 'left borders
)
1779 (eq (org-element-map table-row
'table-cell
1783 (make-string (+ 2 width
) (string-to-char horiz
))
1785 ((not (memq 'right borders
)) nil
)
1786 ((eq (car (last (org-element-contents table-row
))) cell
)
1790 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1791 (borders (org-export-table-cell-borders
1792 (org-element-map table-row
'table-cell
'identity info t
)
1795 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1796 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1797 (funcall build-hline
"+" "-" "+" "+")))
1798 ((memq 'above borders
)
1799 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1800 (funcall build-hline
"+" "-" "+" "+"))))
1801 (when (memq 'left borders
) (if utf8p
"│" "|"))
1803 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1804 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1805 (funcall build-hline
"+" "-" "+" "+")))))))
1810 (defun org-ascii-timestamp (timestamp contents info
)
1811 "Transcode a TIMESTAMP object from Org to ASCII.
1812 CONTENTS is nil. INFO is a plist holding contextual information."
1813 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1818 (defun org-ascii-underline (underline contents info
)
1819 "Transcode UNDERLINE from Org to ASCII.
1820 CONTENTS is the text with underline markup. INFO is a plist
1821 holding contextual information."
1822 (format "_%s_" contents
))
1827 (defun org-ascii-verbatim (verbatim contents info
)
1828 "Return a VERBATIM object from Org to ASCII.
1829 CONTENTS is nil. INFO is a plist holding contextual information."
1830 (format org-ascii-verbatim-format
1831 (org-element-property :value verbatim
)))
1836 (defun org-ascii-verse-block (verse-block contents info
)
1837 "Transcode a VERSE-BLOCK element from Org to ASCII.
1838 CONTENTS is verse block contents. INFO is a plist holding
1839 contextual information."
1840 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1841 (org-ascii--indent-string
1842 (org-ascii--justify-string contents verse-width
'left
)
1843 org-ascii-quote-margin
)))
1849 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1850 "Filter controlling number of blank lines after a headline.
1852 HEADLINE is a string representing a transcoded headline.
1853 BACK-END is symbol specifying back-end used for export. INFO is
1854 plist containing the communication channel.
1856 This function only applies to `ascii' back-end. See
1857 `org-ascii-headline-spacing' for information."
1858 (if (not org-ascii-headline-spacing
) headline
1859 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1860 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1862 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1863 "Filter controlling number of blank lines between paragraphs.
1865 TREE is the parse tree. BACK-END is the symbol specifying
1866 back-end used for export. INFO is a plist used as
1867 a communication channel.
1869 See `org-ascii-paragraph-spacing' for information."
1870 (when (wholenump org-ascii-paragraph-spacing
)
1871 (org-element-map tree
'paragraph
1873 (when (eq (org-element-type (org-export-get-next-element p info
))
1875 (org-element-put-property
1876 p
:post-blank org-ascii-paragraph-spacing
)))))
1879 (defun org-ascii-filter-comment-spacing (tree backend info
)
1880 "Filter removing blank lines between comments.
1881 TREE is the parse tree. BACK-END is the symbol specifying
1882 back-end used for export. INFO is a plist used as
1883 a communication channel."
1884 (org-element-map tree
'(comment comment-block
)
1886 (when (memq (org-element-type (org-export-get-next-element c info
))
1887 '(comment comment-block
))
1888 (org-element-put-property c
:post-blank
0))))
1893 ;;; End-user functions
1896 (defun org-ascii-export-as-ascii
1897 (&optional async subtreep visible-only body-only ext-plist
)
1898 "Export current buffer to a text buffer.
1900 If narrowing is active in the current buffer, only export its
1903 If a region is active, export that region.
1905 A non-nil optional argument ASYNC means the process should happen
1906 asynchronously. The resulting buffer should be accessible
1907 through the `org-export-stack' interface.
1909 When optional argument SUBTREEP is non-nil, export the sub-tree
1910 at point, extracting information from the headline properties
1913 When optional argument VISIBLE-ONLY is non-nil, don't export
1914 contents of hidden elements.
1916 When optional argument BODY-ONLY is non-nil, strip title and
1917 table of contents from output.
1919 EXT-PLIST, when provided, is a property list with external
1920 parameters overriding Org default settings, but still inferior to
1921 file-local settings.
1923 Export is done in a buffer named \"*Org ASCII Export*\", which
1924 will be displayed when `org-export-show-temporary-export-buffer'
1927 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
1928 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
1931 (defun org-ascii-export-to-ascii
1932 (&optional async subtreep visible-only body-only ext-plist
)
1933 "Export current buffer to a text file.
1935 If narrowing is active in the current buffer, only export its
1938 If a region is active, export that region.
1940 A non-nil optional argument ASYNC means the process should happen
1941 asynchronously. The resulting file should be accessible through
1942 the `org-export-stack' interface.
1944 When optional argument SUBTREEP is non-nil, export the sub-tree
1945 at point, extracting information from the headline properties
1948 When optional argument VISIBLE-ONLY is non-nil, don't export
1949 contents of hidden elements.
1951 When optional argument BODY-ONLY is non-nil, strip title and
1952 table of contents from output.
1954 EXT-PLIST, when provided, is a property list with external
1955 parameters overriding Org default settings, but still inferior to
1956 file-local settings.
1958 Return output file's name."
1960 (let ((file (org-export-output-file-name ".txt" subtreep
)))
1961 (org-export-to-file 'ascii file
1962 async subtreep visible-only body-only ext-plist
)))
1965 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1966 "Publish an Org file to ASCII.
1968 FILENAME is the filename of the Org file to be published. PLIST
1969 is the property list for the given project. PUB-DIR is the
1970 publishing directory.
1972 Return output file name."
1974 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1977 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1978 "Publish an Org file to Latin-1.
1980 FILENAME is the filename of the Org file to be published. PLIST
1981 is the property list for the given project. PUB-DIR is the
1982 publishing directory.
1984 Return output file name."
1986 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1989 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1990 "Publish an org file to UTF-8.
1992 FILENAME is the filename of the Org file to be published. PLIST
1993 is the property list for the given project. PUB-DIR is the
1994 publishing directory.
1996 Return output file name."
1998 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
2004 ;; generated-autoload-file: "org-loaddefs.el"
2005 ;; coding: utf-8-emacs
2008 ;;; ox-ascii.el ends here