1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program 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 ;; This program 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 this program. 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-e-ascii-export-as-ascii' (temporary buffer) and
27 ;; `org-e-ascii-export-to-ascii' ("txt" file).
29 ;; Output encoding is specified through `org-e-ascii-charset'
30 ;; variable, among `ascii', `latin1' and `utf-8' symbols.
32 ;; By default, horizontal rules span over the full text with, but with
33 ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
34 ;; they can be shortened and centered.
38 (eval-when-compile (require 'cl
))
40 (require 'org-e-publish
)
42 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
46 ;; The following setting won't allow to modify preferred charset
47 ;; through a buffer keyword or an option item, but, since the property
48 ;; will appear in communication channel nonetheless, it allows to
49 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
52 ;; We also install a filter for headlines and sections, in order to
53 ;; control blank lines separating them in output string.
55 (org-export-define-backend e-ascii
56 ((bold . org-e-ascii-bold
)
57 (center-block . org-e-ascii-center-block
)
58 (clock . org-e-ascii-clock
)
59 (code . org-e-ascii-code
)
60 (drawer . org-e-ascii-drawer
)
61 (dynamic-block . org-e-ascii-dynamic-block
)
62 (entity . org-e-ascii-entity
)
63 (example-block . org-e-ascii-example-block
)
64 (export-block . org-e-ascii-export-block
)
65 (export-snippet . org-e-ascii-export-snippet
)
66 (fixed-width . org-e-ascii-fixed-width
)
67 (footnote-definition . org-e-ascii-footnote-definition
)
68 (footnote-reference . org-e-ascii-footnote-reference
)
69 (headline . org-e-ascii-headline
)
70 (horizontal-rule . org-e-ascii-horizontal-rule
)
71 (inline-src-block . org-e-ascii-inline-src-block
)
72 (inlinetask . org-e-ascii-inlinetask
)
73 (italic . org-e-ascii-italic
)
74 (item . org-e-ascii-item
)
75 (keyword . org-e-ascii-keyword
)
76 (latex-environment . org-e-ascii-latex-environment
)
77 (latex-fragment . org-e-ascii-latex-fragment
)
78 (line-break . org-e-ascii-line-break
)
79 (link . org-e-ascii-link
)
80 (paragraph . org-e-ascii-paragraph
)
81 (plain-list . org-e-ascii-plain-list
)
82 (plain-text . org-e-ascii-plain-text
)
83 (planning . org-e-ascii-planning
)
84 (quote-block . org-e-ascii-quote-block
)
85 (quote-section . org-e-ascii-quote-section
)
86 (radio-target . org-e-ascii-radio-target
)
87 (section . org-e-ascii-section
)
88 (special-block . org-e-ascii-special-block
)
89 (src-block . org-e-ascii-src-block
)
90 (statistics-cookie . org-e-ascii-statistics-cookie
)
91 (strike-through . org-e-ascii-strike-through
)
92 (subscript . org-e-ascii-subscript
)
93 (superscript . org-e-ascii-superscript
)
94 (table . org-e-ascii-table
)
95 (table-cell . org-e-ascii-table-cell
)
96 (table-row . org-e-ascii-table-row
)
97 (target . org-e-ascii-target
)
98 (template . org-e-ascii-template
)
99 (timestamp . org-e-ascii-timestamp
)
100 (underline . org-e-ascii-underline
)
101 (verbatim . org-e-ascii-verbatim
)
102 (verse-block . org-e-ascii-verse-block
))
103 :export-block
"ASCII"
105 (?t
"Export to Plain Text"
106 ((?A
"As ASCII buffer"
108 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset ascii
))))
111 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset ascii
))))
112 (?L
"As Latin1 buffer"
114 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset latin1
))))
117 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset latin1
))))
118 (?U
"As UTF-8 buffer"
120 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset utf-8
))))
123 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset utf-8
))))))
124 :filters-alist
((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
125 (:filter-parse-tree . org-e-ascii-filter-paragraph-spacing
)
126 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
127 :options-alist
((:ascii-charset nil nil org-e-ascii-charset
)))
131 ;;; User Configurable Variables
133 (defgroup org-export-e-ascii nil
134 "Options for exporting Org mode files to ASCII."
135 :tag
"Org Export ASCII"
138 (defcustom org-e-ascii-text-width
72
139 "Maximum width of exported text.
140 This number includes margin size, as set in
141 `org-e-ascii-global-margin'."
142 :group
'org-export-e-ascii
145 (defcustom org-e-ascii-global-margin
0
146 "Width of the left margin, in number of characters."
147 :group
'org-export-e-ascii
150 (defcustom org-e-ascii-inner-margin
2
151 "Width of the inner margin, in number of characters.
152 Inner margin is applied between each headline."
153 :group
'org-export-e-ascii
156 (defcustom org-e-ascii-quote-margin
6
157 "Width of margin used for quoting text, in characters.
158 This margin is applied on both sides of the text."
159 :group
'org-export-e-ascii
162 (defcustom org-e-ascii-inlinetask-width
30
163 "Width of inline tasks, in number of characters.
164 This number ignores any margin."
165 :group
'org-export-e-ascii
168 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
169 "Number of blank lines inserted around headlines.
171 This variable can be set to a cons cell. In that case, its car
172 represents the number of blank lines present before headline
173 contents whereas its cdr reflects the number of blank lines after
176 A nil value replicates the number of blank lines found in the
177 original Org buffer at the same place."
178 :group
'org-export-e-ascii
180 (const :tag
"Replicate original spacing" nil
)
181 (cons :tag
"Set an uniform spacing"
182 (integer :tag
"Number of blank lines before contents")
183 (integer :tag
"Number of blank lines after contents"))))
185 (defcustom org-e-ascii-indented-line-width
'auto
186 "Additional indentation width for the first line in a paragraph.
187 If the value is an integer, indent the first line of each
188 paragraph by this number. If it is the symbol `auto' preserve
189 indentation from original document."
190 :group
'org-export-e-ascii
192 (integer :tag
"Number of white spaces characters")
193 (const :tag
"Preserve original width" auto
)))
195 (defcustom org-e-ascii-paragraph-spacing
'auto
196 "Number of white lines between paragraphs.
197 If the value is an integer, add this number of blank lines
198 between contiguous paragraphs. If is it the symbol `auto', keep
199 the same number of blank lines as in the original document."
200 :group
'org-export-e-ascii
202 (integer :tag
"Number of blank lines")
203 (const :tag
"Preserve original spacing" auto
)))
205 (defcustom org-e-ascii-charset
'ascii
206 "The charset allowed to represent various elements and objects.
208 `ascii' Only use plain ASCII characters
209 `latin1' Include Latin-1 characters
210 `utf-8' Use all UTF-8 characters"
211 :group
'org-export-e-ascii
213 (const :tag
"ASCII" ascii
)
214 (const :tag
"Latin-1" latin1
)
215 (const :tag
"UTF-8" utf-8
)))
217 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
219 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
220 "Characters for underlining headings in ASCII export.
222 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
223 and whose value is a list of characters.
225 For each supported charset, this variable associates a sequence
226 of underline characters. In a sequence, the characters will be
227 used in order for headlines level 1, 2, ... If no character is
228 available for a given level, the headline won't be underlined."
229 :group
'org-export-e-ascii
231 (cons :tag
"Underline characters sequence"
232 (const :tag
"ASCII charset" ascii
)
234 (cons :tag
"Underline characters sequence"
235 (const :tag
"Latin-1 charset" latin1
)
237 (cons :tag
"Underline characters sequence"
238 (const :tag
"UTF-8 charset" utf-8
)
239 (repeat character
))))
241 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
244 "Bullet characters for headlines converted to lists in ASCII export.
246 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
247 and whose value is a list of characters.
249 The first character is used for the first level considered as low
250 level, and so on. If there are more levels than characters given
251 here, the list will be repeated.
253 Note that this variable doesn't affect plain lists
255 :group
'org-export-e-ascii
257 (cons :tag
"Bullet characters for low level headlines"
258 (const :tag
"ASCII charset" ascii
)
260 (cons :tag
"Bullet characters for low level headlines"
261 (const :tag
"Latin-1 charset" latin1
)
263 (cons :tag
"Bullet characters for low level headlines"
264 (const :tag
"UTF-8 charset" utf-8
)
265 (repeat character
))))
267 (defcustom org-e-ascii-links-to-notes t
268 "Non-nil means convert links to notes before the next headline.
269 When nil, the link will be exported in place. If the line
270 becomes long in this way, it will be wrapped."
271 :group
'org-export-e-ascii
274 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
275 "Non-nil means keep all vertical lines in ASCII tables.
276 When nil, vertical lines will be removed except for those needed
277 for column grouping."
278 :group
'org-export-e-ascii
281 (defcustom org-e-ascii-table-widen-columns t
282 "Non-nil means widen narrowed columns for export.
283 When nil, narrowed columns will look in ASCII export just like in
284 Org mode, i.e. with \"=>\" as ellipsis."
285 :group
'org-export-e-ascii
288 (defcustom org-e-ascii-table-use-ascii-art nil
289 "Non-nil means table.el tables are turned into ascii-art.
291 It only makes sense when export charset is `utf-8'. It is nil by
292 default since it requires ascii-art-to-unicode.el package. You
293 can download it here:
295 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
297 (defcustom org-e-ascii-caption-above nil
298 "When non-nil, place caption string before the element.
299 Otherwise, place it right after it."
300 :group
'org-export-e-ascii
303 (defcustom org-e-ascii-verbatim-format
"`%s'"
304 "Format string used for verbatim text and inline code."
305 :group
'org-export-e-ascii
308 (defcustom org-e-ascii-format-drawer-function nil
309 "Function called to format a drawer in ASCII.
311 The function must accept two parameters:
312 NAME the drawer name, like \"LOGBOOK\"
313 CONTENTS the contents of the drawer.
314 WIDTH the text width within the drawer.
316 The function should return either the string to be exported or
317 nil to ignore the drawer.
319 For example, the variable could be set to the following function
320 in order to mimic default behaviour:
322 \(defun org-e-ascii-format-drawer-default \(name contents width\)
323 \"Format a drawer element for ASCII export.\"
325 :group
'org-export-e-ascii
328 (defcustom org-e-ascii-format-inlinetask-function nil
329 "Function called to format an inlinetask in ASCII.
331 The function must accept six parameters:
332 TODO the todo keyword, as a string
333 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
334 PRIORITY the inlinetask priority, as a string
335 NAME the inlinetask name, as a string.
336 TAGS the inlinetask tags, as a list of strings.
337 CONTENTS the contents of the inlinetask, as a string.
339 The function should return either the string to be exported or
340 nil to ignore the inline task.
342 For example, the variable could be set to the following function
343 in order to mimic default behaviour:
345 \(defun org-e-ascii-format-inlinetask-default
346 \(todo type priority name tags contents\)
347 \"Format an inline task element for ASCII export.\"
348 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
349 \(width org-e-ascii-inlinetask-width\)
350 \(org-e-ascii--indent-string
352 ;; Top line, with an additional blank line if not in UTF-8.
353 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
354 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
355 ;; Add title. Fill it if wider than inlinetask.
356 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
357 \(if \(<= \(length title\) width\) title
358 \(org-e-ascii--fill-string title width info\)\)\)
360 ;; If CONTENTS is not empty, insert it along with
362 \(when \(org-string-nw-p contents\)
363 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
365 \(make-string width \(if utf8p ?━ ?_\)\)\)
366 ;; Flush the inlinetask to the right.
367 \(- \(plist-get info :ascii-width\)
368 \(plist-get info :ascii-margin\)
369 \(plist-get info :ascii-inner-margin\)
370 \(org-e-ascii--current-text-width inlinetask info\)\)"
371 :group
'org-export-e-ascii
376 ;;; Internal Functions
378 ;; Internal functions fall into three categories.
380 ;; The first one is about text formatting. The core function is
381 ;; `org-e-ascii--current-text-width', which determines the current
382 ;; text width allowed to a given element. In other words, it helps
383 ;; keeping each line width within maximum text width defined in
384 ;; `org-e-ascii-text-width'. Once this information is known,
385 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
386 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
387 ;; operate on a given output string.
389 ;; The second category contains functions handling elements listings,
390 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
391 ;; returns a complete table of contents, `org-e-ascii--list-listings'
392 ;; returns a list of referenceable src-block elements, and
393 ;; `org-e-ascii--list-tables' does the same for table elements.
395 ;; The third category includes general helper functions.
396 ;; `org-e-ascii--build-title' creates the title for a given headline
397 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
398 ;; caption string associated to a table or a src-block.
399 ;; `org-e-ascii--describe-links' creates notes about links for
400 ;; insertion at the end of a section. It uses
401 ;; `org-e-ascii--unique-links' to get the list of links to describe.
402 ;; Eventually, `org-e-ascii--translate' translates a string according
403 ;; to language and charset specification.
406 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
407 "Fill a string with specified text-width and return it.
409 S is the string being filled. TEXT-WIDTH is an integer
410 specifying maximum length of a line. INFO is the plist used as
411 a communication channel.
413 Optional argument JUSTIFY can specify any type of justification
414 among `left', `center', `right' or `full'. A nil value is
415 equivalent to `left'. For a justification that doesn't also fill
416 string, see `org-e-ascii--justify-string'.
418 Return nil if S isn't a string."
419 ;; Don't fill paragraph when break should be preserved.
420 (cond ((not (stringp s
)) nil
)
421 ((plist-get info
:preserve-breaks
) s
)
423 (let ((fill-column text-width
)
424 (use-hard-newlines t
))
426 (fill-region (point-min) (point-max) justify
))
429 (defun org-e-ascii--justify-string (s text-width how
)
431 TEXT-WIDTH is an integer specifying maximum length of a line.
432 HOW determines the type of justification: it can be `left',
433 `right', `full' or `center'."
436 (goto-char (point-min))
437 (let ((fill-column text-width
))
438 (while (< (point) (point-max))
439 (justify-current-line how
)
443 (defun org-e-ascii--indent-string (s width
)
444 "Indent string S by WIDTH white spaces.
445 Empty lines are not indented."
447 (replace-regexp-in-string
448 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
450 (defun org-e-ascii--box-string (s info
)
451 "Return string S with a partial box to its left.
452 INFO is a plist used as a communicaton channel."
453 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
454 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
455 (replace-regexp-in-string
456 "^" (if utf8p
"│ " "| ")
457 ;; Remove last newline character.
458 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
460 (defun org-e-ascii--current-text-width (element info
)
461 "Return maximum text width for ELEMENT's contents.
462 INFO is a plist used as a communication channel."
463 (case (org-element-type element
)
464 ;; Elements with an absolute width: `headline' and `inlinetask'.
465 (inlinetask org-e-ascii-inlinetask-width
)
467 (- org-e-ascii-text-width
468 (let ((low-level-rank (org-export-low-level-p element info
)))
469 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
470 ;; Elements with a relative width: store maximum text width in
473 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
474 ;; Total width is determined by the presence, or not, of an
475 ;; inline task among ELEMENT parents.
477 (if (loop for parent in genealogy
478 thereis
(eq (org-element-type parent
) 'inlinetask
))
479 org-e-ascii-inlinetask-width
480 ;; No inlinetask: Remove global margin from text width.
481 (- org-e-ascii-text-width
482 org-e-ascii-global-margin
483 (let ((parent (org-export-get-parent-headline element
)))
484 ;; Inner margin doesn't apply to text before first
487 (let ((low-level-rank
488 (org-export-low-level-p parent info
)))
489 ;; Inner margin doesn't apply to contents of
490 ;; low level headlines, since they've got their
491 ;; own indentation mechanism.
492 (if low-level-rank
(* low-level-rank
2)
493 org-e-ascii-inner-margin
))))))))
495 ;; Each `quote-block', `quote-section' and `verse-block' above
496 ;; narrows text width by twice the standard margin size.
497 (+ (* (loop for parent in genealogy
498 when
(memq (org-element-type parent
)
499 '(quote-block quote-section verse-block
))
501 2 org-e-ascii-quote-margin
)
502 ;; Text width within a plain-list is restricted by
503 ;; indentation of current item. If that's the case,
504 ;; compute it with the help of `:structure' property from
505 ;; parent item, if any.
507 (if (eq (org-element-type element
) 'item
) element
508 (loop for parent in genealogy
509 when
(eq (org-element-type parent
) 'item
)
511 (if (not parent-item
) 0
512 ;; Compute indentation offset of the current item,
513 ;; that is the sum of the difference between its
514 ;; indentation and the indentation of the top item in
515 ;; the list and current item bullet's length. Also
516 ;; remove checkbox length, and tag length (for
517 ;; description lists) or bullet length.
518 (let ((struct (org-element-property :structure parent-item
))
519 (beg-item (org-element-property :begin parent-item
)))
520 (+ (- (org-list-get-ind beg-item struct
)
522 (org-list-get-top-point struct
) struct
))
523 (length (org-e-ascii--checkbox parent-item info
))
525 (or (org-list-get-tag beg-item struct
)
526 (org-list-get-bullet beg-item struct
)))))))))))))
528 (defun org-e-ascii--build-title
529 (element info text-width
&optional underline notags
)
530 "Format ELEMENT title and return it.
532 ELEMENT is either an `headline' or `inlinetask' element. INFO is
533 a plist used as a communication channel. TEXT-WIDTH is an
534 integer representing the maximum length of a line.
536 When optional argument UNDERLINE is non-nil, underline title,
537 without the tags, according to `org-e-ascii-underline'
540 if optional argument NOTAGS is nil, no tags will be added to the
542 (let* ((headlinep (eq (org-element-type element
) 'headline
))
544 ;; Numbering is specific to headlines.
545 (and headlinep
(org-export-numbered-headline-p element info
)
546 ;; All tests passed: build numbering string.
550 (org-export-get-headline-number element info
) ".")
553 (org-export-data (org-element-property :title element
) info
)))
555 (and (plist-get info
:with-todo-keywords
)
556 (let ((todo (org-element-property :todo-keyword element
)))
557 (and todo
(concat (org-export-data todo info
) " ")))))
558 (tags (and (not notags
)
559 (plist-get info
:with-tags
)
560 (let ((tag-list (org-export-get-tags element info
)))
563 (mapconcat 'identity tag-list
":"))))))
565 (and (plist-get info
:with-priority
)
566 (let ((char (org-element-property :priority element
)))
567 (and char
(format "(#%c) " char
)))))
568 (first-part (concat numbers todo priority text
)))
571 ;; Align tags, if any.
575 (max (- text-width
(1+ (length first-part
))) (length tags
)))
577 ;; Maybe underline text, if ELEMENT type is `headline' and an
578 ;; underline character has been defined.
579 (when (and underline headlinep
)
581 (nth (1- (org-export-get-relative-level element info
))
582 (cdr (assq (plist-get info
:ascii-charset
)
583 org-e-ascii-underline
)))))
586 (make-string (length first-part
) under-char
))))))))
588 (defun org-e-ascii--has-caption-p (element info
)
589 "Non-nil when ELEMENT has a caption affiliated keyword.
590 INFO is a plist used as a communication channel. This function
591 is meant to be used as a predicate for `org-export-get-ordinal'."
592 (org-element-property :caption element
))
594 (defun org-e-ascii--build-caption (element info
)
595 "Return caption string for ELEMENT, if applicable.
597 INFO is a plist used as a communication channel.
599 The caption string contains the sequence number of ELEMENT along
600 with its real caption. Return nil when ELEMENT has no affiliated
602 (let ((caption (org-export-get-caption element
)))
604 ;; Get sequence number of current src-block among every
605 ;; src-block with a caption.
607 (org-export-get-ordinal
608 element info nil
'org-e-ascii--has-caption-p
))
609 (title-fmt (org-e-ascii--translate
610 (case (org-element-type element
)
611 (table "Table %d: %s")
612 (src-block "Listing %d: %s"))
614 (org-e-ascii--fill-string
615 (format title-fmt reference
(org-export-data caption info
))
616 (org-e-ascii--current-text-width element info
) info
)))))
618 (defun org-e-ascii--build-toc (info &optional n keyword
)
619 "Return a table of contents.
621 INFO is a plist used as a communication channel.
623 Optional argument N, when non-nil, is an integer specifying the
626 Optional argument KEYWORD specifies the TOC keyword, if any, from
627 which the table of contents generation has been initiated."
628 (let ((title (org-e-ascii--translate "Table of Contents" info
)))
631 (make-string (length title
)
632 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
635 (if keyword
(org-e-ascii--current-text-width keyword info
)
636 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
639 (let* ((level (org-export-get-relative-level headline info
))
640 (indent (* (1- level
) 3)))
642 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
643 (org-e-ascii--build-title
644 headline info
(- text-width indent
) nil
645 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
646 (org-export-collect-headlines info n
) "\n")))))
648 (defun org-e-ascii--list-listings (keyword info
)
649 "Return a list of listings.
651 KEYWORD is the keyword that initiated the list of listings
652 generation. INFO is a plist used as a communication channel."
653 (let ((title (org-e-ascii--translate "List of Listings" info
)))
656 (make-string (length title
)
657 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
660 (if keyword
(org-e-ascii--current-text-width keyword info
)
661 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
662 ;; Use a counter instead of retreiving ordinal of each
667 ;; Store initial text so its length can be computed. This is
668 ;; used to properly align caption right to it in case of
669 ;; filling (like contents of a description list item).
671 (format (org-e-ascii--translate "Listing %d:" info
)
676 (org-e-ascii--indent-string
677 (org-e-ascii--fill-string
678 ;; Use short name in priority, if available.
679 (let ((caption (or (org-export-get-caption src-block t
)
680 (org-export-get-caption src-block
))))
681 (org-export-data caption info
))
682 (- text-width
(length initial-text
)) info
)
683 (length initial-text
))))))
684 (org-export-collect-listings info
) "\n")))))
686 (defun org-e-ascii--list-tables (keyword info
)
687 "Return a list of listings.
689 KEYWORD is the keyword that initiated the list of listings
690 generation. INFO is a plist used as a communication channel."
691 (let ((title (org-e-ascii--translate "List of Tables" info
)))
694 (make-string (length title
)
695 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
698 (if keyword
(org-e-ascii--current-text-width keyword info
)
699 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
700 ;; Use a counter instead of retreiving ordinal of each
705 ;; Store initial text so its length can be computed. This is
706 ;; used to properly align caption right to it in case of
707 ;; filling (like contents of a description list item).
709 (format (org-e-ascii--translate "Table %d:" info
)
714 (org-e-ascii--indent-string
715 (org-e-ascii--fill-string
716 ;; Use short name in priority, if available.
717 (let ((caption (or (org-export-get-caption table t
)
718 (org-export-get-caption table
))))
719 (org-export-data caption info
))
720 (- text-width
(length initial-text
)) info
)
721 (length initial-text
))))))
722 (org-export-collect-tables info
) "\n")))))
724 (defun org-e-ascii--unique-links (element info
)
725 "Return a list of unique link references in ELEMENT.
727 ELEMENT is either an headline element or a section element. INFO
728 is a plist used as a communication channel."
732 ;; Return LINK if it wasn't referenced so far, or nil.
733 ;; Update SEEN links along the way.
736 (cons (org-element-property :raw-link link
)
737 (org-element-contents link
))))
738 ;; Ignore LINK if it hasn't been translated already.
739 ;; It can happen if it is located in an affiliated
740 ;; keyword that was ignored.
741 (when (and (org-string-nw-p
742 (gethash link
(plist-get info
:exported-data
)))
743 (not (member footprint seen
)))
744 (push footprint seen
) link
)))))
745 ;; If at a section, find parent headline, if any, in order to
746 ;; count links that might be in the title.
748 (if (eq (org-element-type element
) 'headline
) element
749 (or (org-export-get-parent-headline element
) element
))))
750 ;; Get all links in HEADLINE.
752 headline
'link
(lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
754 (defun org-e-ascii--describe-links (links width info
)
755 "Return a string describing a list of links.
757 LINKS is a list of link type objects, as returned by
758 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
759 the output string. INFO is a plist used as a communication
763 (let ((type (org-element-property :type link
))
764 (anchor (let ((desc (org-element-contents link
)))
765 (if desc
(org-export-data desc info
)
766 (org-element-property :raw-link link
)))))
768 ;; Coderefs, radio links and fuzzy links are ignored.
769 ((member type
'("coderef" "radio" "fuzzy")) nil
)
770 ;; Id and custom-id links: Headlines refer to their numbering.
771 ((member type
'("custom-id" "id"))
772 (let ((dest (org-export-resolve-id-link link info
)))
774 (org-e-ascii--fill-string
778 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
780 (org-e-ascii--translate "See section %s" info
)
781 (mapconcat 'number-to-string
782 (org-export-get-headline-number dest info
) "."))))
783 width info
) "\n\n")))
784 ;; Do not add a link that cannot be resolved and doesn't have
785 ;; any description: destination is already visible in the
787 ((not (org-element-contents link
)) nil
)
790 (org-e-ascii--fill-string
791 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
796 (defun org-e-ascii--checkbox (item info
)
797 "Return checkbox string for ITEM or nil.
798 INFO is a plist used as a communication channel."
799 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
800 (case (org-element-property :checkbox item
)
801 (on (if utf8p
"☑ " "[X] "))
802 (off (if utf8p
"☐ " "[ ] "))
803 (trans (if utf8p
"☒ " "[-] ")))))
809 (defun org-e-ascii-template--document-title (info)
810 "Return document title, as a string.
811 INFO is a plist used as a communication channel."
812 (let* ((text-width org-e-ascii-text-width
)
813 ;; Links in the title will not be resolved later, so we make
814 ;; sure their path is located right after them.
815 (org-e-ascii-links-to-notes nil
)
816 (title (org-export-data (plist-get info
:title
) info
))
817 (author (and (plist-get info
:with-author
)
818 (let ((auth (plist-get info
:author
)))
819 (and auth
(org-export-data auth info
)))))
820 (email (and (plist-get info
:with-email
)
821 (org-export-data (plist-get info
:email
) info
)))
822 (date (org-export-data (plist-get info
:date
) info
)))
823 ;; There are two types of title blocks depending on the presence
824 ;; of a title to display.
825 (if (string= title
"")
826 ;; Title block without a title. DATE is positioned at the top
827 ;; right of the document, AUTHOR to the top left and EMAIL
830 ((and (org-string-nw-p date
) (org-string-nw-p author
))
833 (make-string (- text-width
(length date
) (length author
)) ?
)
835 (when (org-string-nw-p email
) (concat "\n" email
))
837 ((and (org-string-nw-p date
) (org-string-nw-p email
))
840 (make-string (- text-width
(length date
) (length email
)) ?
)
842 ((org-string-nw-p date
)
844 (org-e-ascii--justify-string date text-width
'right
)
846 ((and (org-string-nw-p author
) (org-string-nw-p email
))
847 (concat author
"\n" email
"\n\n\n"))
848 ((org-string-nw-p author
) (concat author
"\n\n\n"))
849 ((org-string-nw-p email
) (concat email
"\n\n\n")))
850 ;; Title block with a title. Document's TITLE, along with the
851 ;; AUTHOR and its EMAIL are both overlined and an underlined,
852 ;; centered. Date is just below, also centered.
853 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
854 ;; Format TITLE. It may be filled if it is too wide,
855 ;; that is wider than the two thirds of the total width.
856 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
857 (formatted-title (org-e-ascii--fill-string title title-len info
))
860 (min (+ (max title-len
(length author
) (length email
)) 2)
861 text-width
) (if utf8p ?━ ?_
))))
862 (org-e-ascii--justify-string
865 (upcase formatted-title
)
867 ((and (org-string-nw-p author
) (org-string-nw-p email
))
868 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
869 ((org-string-nw-p author
)
870 (concat (if utf8p
"\n\n\n" "\n\n") author
))
871 ((org-string-nw-p email
)
872 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
874 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
875 "\n\n\n") text-width
'center
)))))
877 (defun org-e-ascii-template (contents info
)
878 "Return complete document string after ASCII conversion.
879 CONTENTS is the transcoded contents string. INFO is a plist
880 holding export options."
881 (org-element-normalize-string
882 (org-e-ascii--indent-string
883 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
884 ;; 1. Build title block.
886 (org-e-ascii-template--document-title info
)
887 ;; 2. Table of contents.
888 (let ((depth (plist-get info
:with-toc
)))
891 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
893 ;; 3. Document's body.
895 ;; 4. Footnote definitions.
896 (let ((definitions (org-export-collect-footnote-definitions
897 (plist-get info
:parse-tree
) info
))
898 ;; Insert full links right inside the footnote definition
899 ;; as they have no chance to be inserted later.
900 (org-e-ascii-links-to-notes nil
))
904 (let ((title (org-e-ascii--translate "Footnotes" info
)))
909 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
913 (let ((id (format "[%s] " (car ref
))))
914 ;; Distinguish between inline definitions and
915 ;; full-fledged definitions.
917 (let ((def (nth 2 ref
)))
918 (if (eq (org-element-type def
) 'org-data
)
919 ;; Full-fledged definition: footnote ID is
920 ;; inserted inside the first parsed paragraph
921 ;; (FIRST), if any, to be sure filling will
922 ;; take it into consideration.
923 (let ((first (car (org-element-contents def
))))
924 (if (not (eq (org-element-type first
) 'paragraph
))
925 (concat id
"\n" (org-export-data def info
))
926 (push id
(nthcdr 2 first
))
927 (org-export-data def info
)))
928 ;; Fill paragraph once footnote ID is inserted in
929 ;; order to have a correct length for first line.
930 (org-e-ascii--fill-string
931 (concat id
(org-export-data def info
))
932 text-width info
))))))
933 definitions
"\n\n"))))
934 ;; 5. Creator. Ignore `comment' value as there are no comments in
935 ;; ASCII. Justify it to the bottom right.
936 (let ((creator-info (plist-get info
:with-creator
)))
937 (unless (or (not creator-info
) (eq creator-info
'comment
))
940 (org-e-ascii--fill-string
941 (plist-get info
:creator
) text-width info
'right
))))))
942 org-e-ascii-global-margin
)))
944 (defun org-e-ascii--translate (s info
)
945 "Translate string S according to specified language and charset.
946 INFO is a plist used as a communication channel."
947 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
948 (org-export-translate s charset info
)))
952 ;;; Transcode Functions
956 ;; Babel Calls are ignored.
961 (defun org-e-ascii-bold (bold contents info
)
962 "Transcode BOLD from Org to ASCII.
963 CONTENTS is the text with bold markup. INFO is a plist holding
964 contextual information."
965 (format "*%s*" contents
))
970 (defun org-e-ascii-center-block (center-block contents info
)
971 "Transcode a CENTER-BLOCK element from Org to ASCII.
972 CONTENTS holds the contents of the block. INFO is a plist
973 holding contextual information."
974 (org-e-ascii--justify-string
975 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
980 (defun org-e-ascii-clock (clock contents info
)
981 "Transcode a CLOCK object from Org to ASCII.
982 CONTENTS is nil. INFO is a plist holding contextual
984 (concat org-clock-string
" "
985 (org-translate-time (org-element-property :value clock
))
986 (let ((time (org-element-property :time clock
)))
991 (org-split-string time
":")))))))
996 (defun org-e-ascii-code (code contents info
)
997 "Return a CODE object from Org to ASCII.
998 CONTENTS is nil. INFO is a plist holding contextual
1000 (format org-e-ascii-verbatim-format
(org-element-property :value code
)))
1005 ;; Comments are ignored.
1010 ;; Comment Blocks are ignored.
1015 (defun org-e-ascii-drawer (drawer contents info
)
1016 "Transcode a DRAWER element from Org to ASCII.
1017 CONTENTS holds the contents of the block. INFO is a plist
1018 holding contextual information."
1019 (let ((name (org-element-property :drawer-name drawer
))
1020 (width (org-e-ascii--current-text-width drawer info
)))
1021 (if (functionp org-e-ascii-format-drawer-function
)
1022 (funcall org-e-ascii-format-drawer-function name contents width
)
1023 ;; If there's no user defined function: simply
1024 ;; display contents of the drawer.
1030 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1031 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1032 CONTENTS holds the contents of the block. INFO is a plist
1033 holding contextual information."
1039 (defun org-e-ascii-entity (entity contents info
)
1040 "Transcode an ENTITY object from Org to ASCII.
1041 CONTENTS are the definition itself. INFO is a plist holding
1042 contextual information."
1043 (org-element-property
1044 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1050 (defun org-e-ascii-example-block (example-block contents info
)
1051 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1052 CONTENTS is nil. INFO is a plist holding contextual information."
1053 (org-e-ascii--box-string
1054 (org-export-format-code-default example-block info
) info
))
1059 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1060 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1061 CONTENTS is nil. INFO is a plist holding contextual information."
1062 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1063 (org-element-property :value export-snippet
)))
1068 (defun org-e-ascii-export-block (export-block contents info
)
1069 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1070 CONTENTS is nil. INFO is a plist holding contextual information."
1071 (when (string= (org-element-property :type export-block
) "ASCII")
1072 (org-remove-indentation (org-element-property :value export-block
))))
1077 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1078 "Transcode a FIXED-WIDTH element from Org to ASCII.
1079 CONTENTS is nil. INFO is a plist holding contextual information."
1080 (org-e-ascii--box-string
1081 (org-remove-indentation
1082 (org-element-property :value fixed-width
)) info
))
1085 ;;;; Footnote Definition
1087 ;; Footnote Definitions are ignored. They are compiled at the end of
1088 ;; the document, by `org-e-ascii-template'.
1091 ;;;; Footnote Reference
1093 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1094 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1095 CONTENTS is nil. INFO is a plist holding contextual information."
1096 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1101 (defun org-e-ascii-headline (headline contents info
)
1102 "Transcode an HEADLINE element from Org to ASCII.
1103 CONTENTS holds the contents of the headline. INFO is a plist
1104 holding contextual information."
1105 ;; Don't export footnote section, which will be handled at the end
1107 (unless (org-element-property :footnote-section-p headline
)
1108 (let* ((low-level-rank (org-export-low-level-p headline info
))
1109 (width (org-e-ascii--current-text-width headline info
))
1110 ;; Blank lines between headline and its contents.
1111 ;; `org-e-ascii-headline-spacing', when set, overwrites
1112 ;; original buffer's spacing.
1115 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1116 (org-element-property :pre-blank headline
)) ?
\n))
1117 ;; Even if HEADLINE has no section, there might be some
1118 ;; links in its title that we shouldn't forget to describe.
1120 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1121 (let ((title (org-element-property :title headline
)))
1123 (org-e-ascii--describe-links
1124 (org-e-ascii--unique-links title info
) width info
))))))
1125 ;; Deep subtree: export it as a list item.
1129 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1130 org-e-ascii-bullets
))))
1132 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1135 (org-e-ascii--build-title headline info width
) "\n"
1136 ;; Contents, indented by length of bullet.
1138 (org-e-ascii--indent-string
1140 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1142 ;; Else: Standard headline.
1144 (org-e-ascii--build-title headline info width
'underline
)
1146 (concat (when (org-string-nw-p links
) links
) contents
))))))
1149 ;;;; Horizontal Rule
1151 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1152 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1153 CONTENTS is nil. INFO is a plist holding contextual
1155 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info
))
1157 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1158 (org-e-ascii--justify-string
1159 (make-string (if (wholenump spec-width
) spec-width text-width
)
1160 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1161 text-width
'center
)))
1164 ;;;; Inline Babel Call
1166 ;; Inline Babel Calls are ignored.
1169 ;;;; Inline Src Block
1171 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1172 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1173 CONTENTS holds the contents of the item. INFO is a plist holding
1174 contextual information."
1175 (format org-e-ascii-verbatim-format
1176 (org-element-property :value inline-src-block
)))
1181 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1182 "Transcode an INLINETASK element from Org to ASCII.
1183 CONTENTS holds the contents of the block. INFO is a plist
1184 holding contextual information."
1185 (let ((width (org-e-ascii--current-text-width inlinetask info
)))
1186 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1187 ;; with appropriate arguments.
1188 (if (functionp org-e-ascii-format-inlinetask-function
)
1189 (funcall org-e-ascii-format-inlinetask-function
1191 (and (plist-get info
:with-todo-keywords
)
1192 (let ((todo (org-element-property
1193 :todo-keyword inlinetask
)))
1194 (and todo
(org-export-data todo info
))))
1196 (org-element-property :todo-type inlinetask
)
1198 (and (plist-get info
:with-priority
)
1199 (org-element-property :priority inlinetask
))
1201 (org-export-data (org-element-property :title inlinetask
) info
)
1203 (and (plist-get info
:with-tags
)
1204 (org-element-property :tags inlinetask
))
1205 ;; contents and width
1207 ;; Otherwise, use a default template.
1208 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1209 (org-e-ascii--indent-string
1211 ;; Top line, with an additional blank line if not in UTF-8.
1212 (make-string width
(if utf8p ?━ ?_
)) "\n"
1213 (unless utf8p
(concat (make-string width ?
) "\n"))
1214 ;; Add title. Fill it if wider than inlinetask.
1215 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1216 (if (<= (length title
) width
) title
1217 (org-e-ascii--fill-string title width info
)))
1219 ;; If CONTENTS is not empty, insert it along with
1221 (when (org-string-nw-p contents
)
1222 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1224 (make-string width
(if utf8p ?━ ?_
)))
1225 ;; Flush the inlinetask to the right.
1226 (- org-e-ascii-text-width org-e-ascii-global-margin
1227 (if (not (org-export-get-parent-headline inlinetask
)) 0
1228 org-e-ascii-inner-margin
)
1229 (org-e-ascii--current-text-width inlinetask info
)))))))
1233 (defun org-e-ascii-italic (italic contents info
)
1234 "Transcode italic from Org to ASCII.
1235 CONTENTS is the text with italic markup. INFO is a plist holding
1236 contextual information."
1237 (format "/%s/" contents
))
1242 (defun org-e-ascii-item (item contents info
)
1243 "Transcode an ITEM element from Org to ASCII.
1244 CONTENTS holds the contents of the item. INFO is a plist holding
1245 contextual information."
1246 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1247 (checkbox (org-e-ascii--checkbox item info
))
1248 (list-type (org-element-property :type
(org-export-get-parent item
)))
1250 ;; First parent of ITEM is always the plain-list. Get
1251 ;; `:type' property from it.
1252 (org-list-bullet-string
1256 (org-export-data (org-element-property :tag item
) info
)
1259 ;; Return correct number for ITEM, paying attention to
1261 (let* ((struct (org-element-property :structure item
))
1262 (bul (org-element-property :bullet item
))
1263 (num (number-to-string
1264 (car (last (org-list-get-item-number
1265 (org-element-property :begin item
)
1267 (org-list-prevs-alist struct
)
1268 (org-list-parents-alist struct
)))))))
1269 (replace-regexp-in-string "[0-9]+" num bul
)))
1270 (t (let ((bul (org-element-property :bullet item
)))
1271 ;; Change bullets into more visible form if UTF-8 is active.
1273 (replace-regexp-in-string
1275 (replace-regexp-in-string
1277 (replace-regexp-in-string "*" "‣" bul
))))))))))
1280 (unless (eq list-type
'descriptive
) checkbox
)
1281 ;; Contents: Pay attention to indentation. Note: check-boxes are
1282 ;; already taken care of at the paragraph level so they don't
1283 ;; interfere with indentation.
1284 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1285 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1287 (concat "\n" contents
))))))
1292 (defun org-e-ascii-keyword (keyword contents info
)
1293 "Transcode a KEYWORD element from Org to ASCII.
1294 CONTENTS is nil. INFO is a plist holding contextual
1296 (let ((key (org-element-property :key keyword
))
1297 (value (org-element-property :value keyword
)))
1299 ((string= key
"ASCII") value
)
1300 ((string= key
"TOC")
1301 (let ((value (downcase value
)))
1303 ((string-match "\\<headlines\\>" value
)
1304 (let ((depth (or (and (string-match "[0-9]+" value
)
1305 (string-to-number (match-string 0 value
)))
1306 (plist-get info
:with-toc
))))
1307 (org-e-ascii--build-toc
1308 info
(and (wholenump depth
) depth
) keyword
)))
1309 ((string= "tables" value
)
1310 (org-e-ascii--list-tables keyword info
))
1311 ((string= "listings" value
)
1312 (org-e-ascii--list-listings keyword info
))))))))
1315 ;;;; Latex Environment
1317 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1318 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1319 CONTENTS is nil. INFO is a plist holding contextual
1321 (org-remove-indentation (org-element-property :value latex-environment
)))
1326 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1327 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1328 CONTENTS is nil. INFO is a plist holding contextual
1330 (org-element-property :value latex-fragment
))
1335 (defun org-e-ascii-line-break (line-break contents info
)
1336 "Transcode a LINE-BREAK object from Org to ASCII.
1337 CONTENTS is nil. INFO is a plist holding contextual
1338 information." hard-newline
)
1343 (defun org-e-ascii-link (link desc info
)
1344 "Transcode a LINK object from Org to ASCII.
1346 DESC is the description part of the link, or the empty string.
1347 INFO is a plist holding contextual information."
1348 (let ((raw-link (org-element-property :raw-link link
))
1349 (type (org-element-property :type link
)))
1351 ((string= type
"coderef")
1352 (let ((ref (org-element-property :path link
)))
1353 (format (org-export-get-coderef-format ref desc
)
1354 (org-export-resolve-coderef ref info
))))
1355 ;; Do not apply a special syntax on radio links. Though, use
1356 ;; transcoded target's contents as output.
1357 ((string= type
"radio")
1358 (let ((destination (org-export-resolve-radio-link link info
)))
1360 (org-export-data (org-element-contents destination
) info
))))
1361 ;; Do not apply a special syntax on fuzzy links pointing to
1363 ((string= type
"fuzzy")
1364 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1365 ;; Ignore invisible "#+TARGET: path".
1366 (unless (eq (org-element-type destination
) 'keyword
)
1367 (if (org-string-nw-p desc
) desc
1370 (org-export-get-ordinal
1371 destination info nil
'org-e-ascii--has-caption-p
)))
1373 (if (atom number
) (number-to-string number
)
1374 (mapconcat 'number-to-string number
".")))))))))
1376 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1378 (format "[%s]" desc
)
1379 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1384 (defun org-e-ascii-paragraph (paragraph contents info
)
1385 "Transcode a PARAGRAPH element from Org to ASCII.
1386 CONTENTS is the contents of the paragraph, as a string. INFO is
1387 the plist used as a communication channel."
1388 (let ((contents (if (not (wholenump org-e-ascii-indented-line-width
)) contents
1390 (make-string org-e-ascii-indented-line-width ?
)
1391 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1392 (org-e-ascii--fill-string
1393 contents
(org-e-ascii--current-text-width paragraph info
) info
)))
1398 (defun org-e-ascii-plain-list (plain-list contents info
)
1399 "Transcode a PLAIN-LIST element from Org to ASCII.
1400 CONTENTS is the contents of the list. INFO is a plist holding
1401 contextual information."
1407 (defun org-e-ascii-plain-text (text info
)
1408 "Transcode a TEXT string from Org to ASCII.
1409 INFO is a plist used as a communication channel."
1410 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1411 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1412 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1413 (if (not (plist-get info
:with-special-strings
)) text
1414 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1415 (if (not utf8p
) text
1416 ;; Usual replacements in utf-8 with proper option set.
1417 (replace-regexp-in-string
1419 (replace-regexp-in-string
1421 (replace-regexp-in-string "---" "—" text
)))))))
1426 (defun org-e-ascii-planning (planning contents info
)
1427 "Transcode a PLANNING element from Org to ASCII.
1428 CONTENTS is nil. INFO is a plist used as a communication
1433 (list (let ((closed (org-element-property :closed planning
)))
1434 (when closed
(concat org-closed-string
" "
1435 (org-translate-time closed
))))
1436 (let ((deadline (org-element-property :deadline planning
)))
1437 (when deadline
(concat org-deadline-string
" "
1438 (org-translate-time deadline
))))
1439 (let ((scheduled (org-element-property :scheduled planning
)))
1440 (when scheduled
(concat org-scheduled-string
" "
1441 (org-translate-time scheduled
))))))
1445 ;;;; Property Drawer
1447 ;; Property drawers are ignored.
1452 (defun org-e-ascii-quote-block (quote-block contents info
)
1453 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1454 CONTENTS holds the contents of the block. INFO is a plist
1455 holding contextual information."
1456 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1457 (org-e-ascii--indent-string
1458 (org-remove-indentation
1459 (org-e-ascii--fill-string contents width info
))
1460 org-e-ascii-quote-margin
)))
1465 (defun org-e-ascii-quote-section (quote-section contents info
)
1466 "Transcode a QUOTE-SECTION element from Org to ASCII.
1467 CONTENTS is nil. INFO is a plist holding contextual information."
1468 (let ((width (org-e-ascii--current-text-width quote-section info
))
1471 (org-remove-indentation (org-element-property :value quote-section
))
1473 (org-e-ascii--indent-string
1475 (+ org-e-ascii-quote-margin
1476 ;; Don't apply inner margin if parent headline is low level.
1477 (let ((headline (org-export-get-parent-headline quote-section
)))
1478 (if (org-export-low-level-p headline info
) 0
1479 org-e-ascii-inner-margin
))))))
1484 (defun org-e-ascii-radio-target (radio-target contents info
)
1485 "Transcode a RADIO-TARGET object from Org to ASCII.
1486 CONTENTS is the contents of the target. INFO is a plist holding
1487 contextual information."
1492 (defun org-e-ascii-section (section contents info
)
1493 "Transcode a SECTION element from Org to ASCII.
1494 CONTENTS is the contents of the section. INFO is a plist holding
1495 contextual information."
1496 (org-e-ascii--indent-string
1499 (when org-e-ascii-links-to-notes
1500 ;; Add list of links at the end of SECTION.
1501 (let ((links (org-e-ascii--describe-links
1502 (org-e-ascii--unique-links section info
)
1503 (org-e-ascii--current-text-width section info
) info
)))
1504 ;; Separate list of links and section contents.
1505 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1506 ;; Do not apply inner margin if parent headline is low level.
1507 (let ((headline (org-export-get-parent-headline section
)))
1508 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1509 org-e-ascii-inner-margin
))))
1514 (defun org-e-ascii-special-block (special-block contents info
)
1515 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1516 CONTENTS holds the contents of the block. INFO is a plist
1517 holding contextual information."
1523 (defun org-e-ascii-src-block (src-block contents info
)
1524 "Transcode a SRC-BLOCK element from Org to ASCII.
1525 CONTENTS holds the contents of the item. INFO is a plist holding
1526 contextual information."
1527 (let ((caption (org-e-ascii--build-caption src-block info
)))
1529 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1530 (org-e-ascii--box-string
1531 (org-export-format-code-default src-block info
) info
)
1532 (when (and caption
(not org-e-ascii-caption-above
))
1533 (concat "\n" caption
)))))
1535 ;;;; Statistics Cookie
1537 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1538 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1539 CONTENTS is nil. INFO is a plist holding contextual information."
1540 (org-element-property :value statistics-cookie
))
1545 (defun org-e-ascii-subscript (subscript contents info
)
1546 "Transcode a SUBSCRIPT object from Org to ASCII.
1547 CONTENTS is the contents of the object. INFO is a plist holding
1548 contextual information."
1549 (if (org-element-property :use-brackets-p subscript
)
1550 (format "_{%s}" contents
)
1551 (format "_%s" contents
)))
1556 (defun org-e-ascii-superscript (superscript contents info
)
1557 "Transcode a SUPERSCRIPT object from Org to ASCII.
1558 CONTENTS is the contents of the object. INFO is a plist holding
1559 contextual information."
1560 (if (org-element-property :use-brackets-p superscript
)
1561 (format "_{%s}" contents
)
1562 (format "_%s" contents
)))
1567 (defun org-e-ascii-strike-through (strike-through contents info
)
1568 "Transcode STRIKE-THROUGH from Org to ASCII.
1569 CONTENTS is text with strike-through markup. INFO is a plist
1570 holding contextual information."
1571 (format "+%s+" contents
))
1576 (defun org-e-ascii-table (table contents info
)
1577 "Transcode a TABLE element from Org to ASCII.
1578 CONTENTS is the contents of the table. INFO is a plist holding
1579 contextual information."
1580 (let ((caption (org-e-ascii--build-caption table info
)))
1582 ;; Possibly add a caption string above.
1583 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1584 ;; Insert table. Note: "table.el" tables are left unmodified.
1585 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1586 ((and org-e-ascii-table-use-ascii-art
1587 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1588 (require 'ascii-art-to-unicode nil t
))
1590 (insert (org-remove-indentation
1591 (org-element-property :value table
)))
1592 (goto-char (point-min))
1594 (goto-char (point-max))
1595 (skip-chars-backward " \r\t\n")
1596 (buffer-substring (point-min) (point))))
1597 (t (org-remove-indentation (org-element-property :value table
))))
1598 ;; Possible add a caption string below.
1599 (when (and caption
(not org-e-ascii-caption-above
))
1600 (concat "\n" caption
)))))
1605 (defun org-e-ascii--table-cell-width (table-cell info
)
1606 "Return width of TABLE-CELL.
1608 INFO is a plist used as a communication channel.
1610 Width of a cell is determined either by a width cookie in the
1611 same column as the cell, or by the maximum cell's length in that
1614 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1616 (or (and (not org-e-ascii-table-widen-columns
)
1617 (org-export-table-cell-width table-cell info
))
1618 (let* ((max-width 0)
1619 (table (org-export-get-parent-table table-cell
))
1620 (specialp (org-export-table-has-special-column-p table
))
1621 (col (cdr (org-export-table-cell-address table-cell info
))))
1628 (org-element-contents
1629 (elt (if specialp
(cdr (org-element-contents row
))
1630 (org-element-contents row
))
1637 (defun org-e-ascii-table-cell (table-cell contents info
)
1638 "Transcode a TABLE-CELL object from Org to ASCII.
1639 CONTENTS is the cell contents. INFO is a plist used as
1640 a communication channel."
1641 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1642 ;; is nil and some width cookie has set it, use that value.
1643 ;; Otherwise, compute the maximum width among transcoded data of
1644 ;; each cell in the column.
1645 (let ((width (org-e-ascii--table-cell-width table-cell info
)))
1646 ;; When contents are too large, truncate them.
1647 (unless (or org-e-ascii-table-widen-columns
(<= (length contents
) width
))
1648 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1649 ;; Align contents correctly within the cell.
1650 (let* ((indent-tabs-mode nil
)
1653 (org-e-ascii--justify-string
1655 (org-export-table-cell-alignment table-cell info
)))))
1656 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1658 (concat (format " %s " contents
)
1659 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1660 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1665 (defun org-e-ascii-table-row (table-row contents info
)
1666 "Transcode a TABLE-ROW element from Org to ASCII.
1667 CONTENTS is the row contents. INFO is a plist used as
1668 a communication channel."
1669 (when (eq (org-element-property :type table-row
) 'standard
)
1672 (lambda (lcorner horiz vert rcorner
)
1677 table-row
'table-cell
1679 (let ((width (org-e-ascii--table-cell-width cell info
))
1680 (borders (org-export-table-cell-borders cell info
)))
1682 ;; In order to know if CELL starts the row, do
1683 ;; not compare it with the first cell in the row
1684 ;; as there might be a special column. Instead,
1685 ;; compare it with the first exportable cell,
1686 ;; obtained with `org-element-map'.
1687 (when (and (memq 'left borders
)
1688 (eq (org-element-map
1689 table-row
'table-cell
'identity info t
)
1692 (make-string (+ 2 width
) (string-to-char horiz
))
1694 ((not (memq 'right borders
)) nil
)
1695 ((eq (car (last (org-element-contents table-row
))) cell
)
1699 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1700 (borders (org-export-table-cell-borders
1701 (org-element-map table-row
'table-cell
'identity info t
)
1704 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1705 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1706 (funcall build-hline
"+" "-" "+" "+")))
1707 ((memq 'above borders
)
1708 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1709 (funcall build-hline
"+" "-" "+" "+"))))
1710 (when (memq 'left borders
) (if utf8p
"│" "|"))
1712 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1713 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1714 (funcall build-hline
"+" "-" "+" "+")))))))
1719 ;; Targets are invisible.
1724 (defun org-e-ascii-timestamp (timestamp contents info
)
1725 "Transcode a TIMESTAMP object from Org to ASCII.
1726 CONTENTS is nil. INFO is a plist holding contextual information."
1727 (let ((value (org-translate-time (org-element-property :value timestamp
)))
1729 (org-translate-time (org-element-property :range-end timestamp
)))
1730 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1732 (when range-end
(concat (if utf8p
"–" "--") range-end
)))))
1737 (defun org-e-ascii-underline (underline contents info
)
1738 "Transcode UNDERLINE from Org to ASCII.
1739 CONTENTS is the text with underline markup. INFO is a plist
1740 holding contextual information."
1741 (format "_%s_" contents
))
1746 (defun org-e-ascii-verbatim (verbatim contents info
)
1747 "Return a VERBATIM object from Org to ASCII.
1748 CONTENTS is nil. INFO is a plist holding contextual information."
1749 (format org-e-ascii-verbatim-format
1750 (org-element-property :value verbatim
)))
1755 (defun org-e-ascii-verse-block (verse-block contents info
)
1756 "Transcode a VERSE-BLOCK element from Org to ASCII.
1757 CONTENTS is verse block contents. INFO is a plist holding
1758 contextual information."
1759 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1760 (org-e-ascii--indent-string
1761 (org-e-ascii--justify-string contents verse-width
'left
)
1762 org-e-ascii-quote-margin
)))
1767 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1768 "Filter controlling number of blank lines after an headline.
1770 HEADLINE is a string representing a transcoded headline.
1771 BACK-END is symbol specifying back-end used for export. INFO is
1772 plist containing the communication channel.
1774 This function only applies to `e-ascii' back-end. See
1775 `org-e-ascii-headline-spacing' for information."
1776 (if (not org-e-ascii-headline-spacing
) headline
1777 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1778 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1780 (defun org-e-ascii-filter-paragraph-spacing (tree back-end info
)
1781 "Filter controlling number of blank lines between paragraphs.
1783 TREE is the parse tree. BACK-END is the symbol specifying
1784 back-end used for export. INFO is a plist used as
1785 a communication channel.
1787 This function only applies to `e-ascii' back-end. See
1788 `org-e-ascii-paragraph-spacing' for information."
1789 (when (wholenump org-e-ascii-paragraph-spacing
)
1793 (when (eq (org-element-type (org-export-get-next-element p info
))
1795 (org-element-put-property
1796 p
:post-blank org-e-ascii-paragraph-spacing
)))))
1801 ;;; End-user functions
1804 (defun org-e-ascii-export-as-ascii
1805 (&optional subtreep visible-only body-only ext-plist
)
1806 "Export current buffer to a text buffer.
1808 If narrowing is active in the current buffer, only export its
1811 If a region is active, export that region.
1813 When optional argument SUBTREEP is non-nil, export the sub-tree
1814 at point, extracting information from the headline properties
1817 When optional argument VISIBLE-ONLY is non-nil, don't export
1818 contents of hidden elements.
1820 When optional argument BODY-ONLY is non-nil, strip title, table
1821 of contents and footnote definitions from output.
1823 EXT-PLIST, when provided, is a property list with external
1824 parameters overriding Org default settings, but still inferior to
1825 file-local settings.
1827 Export is done in a buffer named \"*Org E-ASCII Export*\", which
1828 will be displayed when `org-export-show-temporary-export-buffer'
1831 (let ((outbuf (org-export-to-buffer
1832 'e-ascii
"*Org E-ASCII Export*"
1833 subtreep visible-only body-only ext-plist
)))
1834 (with-current-buffer outbuf
(text-mode))
1835 (when org-export-show-temporary-export-buffer
1836 (switch-to-buffer-other-window outbuf
))))
1839 (defun org-e-ascii-export-to-ascii
1840 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1841 "Export current buffer to a text file.
1843 If narrowing is active in the current buffer, only export its
1846 If a region is active, export that region.
1848 When optional argument SUBTREEP is non-nil, export the sub-tree
1849 at point, extracting information from the headline properties
1852 When optional argument VISIBLE-ONLY is non-nil, don't export
1853 contents of hidden elements.
1855 When optional argument BODY-ONLY is non-nil, strip title, table
1856 of contents and footnote definitions from output.
1858 EXT-PLIST, when provided, is a property list with external
1859 parameters overriding Org default settings, but still inferior to
1860 file-local settings.
1862 When optional argument PUB-DIR is set, use it as the publishing
1865 Return output file's name."
1867 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1869 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1872 (defun org-e-ascii-publish-to-ascii (plist filename pub-dir
)
1873 "Publish an Org file to ASCII.
1875 FILENAME is the filename of the Org file to be published. PLIST
1876 is the property list for the given project. PUB-DIR is the
1877 publishing directory.
1879 Return output file name."
1880 (org-e-publish-org-to
1881 'e-ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1884 (defun org-e-ascii-publish-to-latin1 (plist filename pub-dir
)
1885 "Publish an Org file to Latin-1.
1887 FILENAME is the filename of the Org file to be published. PLIST
1888 is the property list for the given project. PUB-DIR is the
1889 publishing directory.
1891 Return output file name."
1892 (org-e-publish-org-to
1893 'e-ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1896 (defun org-e-ascii-publish-to-utf8 (plist filename pub-dir
)
1897 "Publish an org file to UTF-8.
1899 FILENAME is the filename of the Org file to be published. PLIST
1900 is the property list for the given project. PUB-DIR is the
1901 publishing directory.
1903 Return output file name."
1904 (org-e-publish-org-to
1905 'e-ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1908 (provide 'org-e-ascii
)
1909 ;;; org-e-ascii.el ends here