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
))
41 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
45 ;; The following setting won't allow to modify preferred charset
46 ;; through a buffer keyword or an option item, but, since the property
47 ;; will appear in communication channel nonetheless, it allows to
48 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
51 ;; We also install a filter for headlines and sections, in order to
52 ;; control blank lines separating them in output string.
54 (org-export-define-backend e-ascii
55 ((bold . org-e-ascii-bold
)
56 (center-block . org-e-ascii-center-block
)
57 (clock . org-e-ascii-clock
)
58 (code . org-e-ascii-code
)
59 (drawer . org-e-ascii-drawer
)
60 (dynamic-block . org-e-ascii-dynamic-block
)
61 (entity . org-e-ascii-entity
)
62 (example-block . org-e-ascii-example-block
)
63 (export-block . org-e-ascii-export-block
)
64 (export-snippet . org-e-ascii-export-snippet
)
65 (fixed-width . org-e-ascii-fixed-width
)
66 (footnote-definition . org-e-ascii-footnote-definition
)
67 (footnote-reference . org-e-ascii-footnote-reference
)
68 (headline . org-e-ascii-headline
)
69 (horizontal-rule . org-e-ascii-horizontal-rule
)
70 (inline-src-block . org-e-ascii-inline-src-block
)
71 (inlinetask . org-e-ascii-inlinetask
)
72 (italic . org-e-ascii-italic
)
73 (item . org-e-ascii-item
)
74 (keyword . org-e-ascii-keyword
)
75 (latex-environment . org-e-ascii-latex-environment
)
76 (latex-fragment . org-e-ascii-latex-fragment
)
77 (line-break . org-e-ascii-line-break
)
78 (link . org-e-ascii-link
)
79 (paragraph . org-e-ascii-paragraph
)
80 (plain-list . org-e-ascii-plain-list
)
81 (plain-text . org-e-ascii-plain-text
)
82 (planning . org-e-ascii-planning
)
83 (quote-block . org-e-ascii-quote-block
)
84 (quote-section . org-e-ascii-quote-section
)
85 (radio-target . org-e-ascii-radio-target
)
86 (section . org-e-ascii-section
)
87 (special-block . org-e-ascii-special-block
)
88 (src-block . org-e-ascii-src-block
)
89 (statistics-cookie . org-e-ascii-statistics-cookie
)
90 (strike-through . org-e-ascii-strike-through
)
91 (subscript . org-e-ascii-subscript
)
92 (superscript . org-e-ascii-superscript
)
93 (table . org-e-ascii-table
)
94 (table-cell . org-e-ascii-table-cell
)
95 (table-row . org-e-ascii-table-row
)
96 (target . org-e-ascii-target
)
97 (template . org-e-ascii-template
)
98 (timestamp . org-e-ascii-timestamp
)
99 (underline . org-e-ascii-underline
)
100 (verbatim . org-e-ascii-verbatim
)
101 (verse-block . org-e-ascii-verse-block
))
102 :export-block
"ASCII"
104 (?t
"Export to Plain Text"
105 ((?A
"As ASCII buffer"
107 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset ascii
))))
110 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset ascii
))))
111 (?L
"As Latin1 buffer"
113 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset latin1
))))
116 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset latin1
))))
117 (?U
"As UTF-8 buffer"
119 (org-e-ascii-export-as-ascii s v b
'(:ascii-charset utf-8
))))
122 (org-e-ascii-export-to-ascii s v b
'(:ascii-charset utf-8
))))))
123 :filters-alist
((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
124 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
125 :options-alist
((:ascii-charset nil nil org-e-ascii-charset
)))
129 ;;; User Configurable Variables
131 (defgroup org-export-e-ascii nil
132 "Options for exporting Org mode files to ASCII."
133 :tag
"Org Export ASCII"
136 (defcustom org-e-ascii-text-width
72
137 "Maximum width of exported text.
138 This number includes margin size, as set in
139 `org-e-ascii-global-margin'."
140 :group
'org-export-e-ascii
143 (defcustom org-e-ascii-global-margin
0
144 "Width of the left margin, in number of characters."
145 :group
'org-export-e-ascii
148 (defcustom org-e-ascii-inner-margin
2
149 "Width of the inner margin, in number of characters.
150 Inner margin is applied between each headline."
151 :group
'org-export-e-ascii
154 (defcustom org-e-ascii-quote-margin
6
155 "Width of margin used for quoting text, in characters.
156 This margin is applied on both sides of the text."
157 :group
'org-export-e-ascii
160 (defcustom org-e-ascii-inlinetask-width
30
161 "Width of inline tasks, in number of characters.
162 This number ignores any margin."
163 :group
'org-export-e-ascii
166 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
167 "Number of blank lines inserted around headlines.
169 This variable can be set to a cons cell. In that case, its car
170 represents the number of blank lines present before headline
171 contents whereas its cdr reflects the number of blank lines after
174 A nil value replicates the number of blank lines found in the
175 original Org buffer at the same place."
176 :group
'org-export-e-ascii
178 (const :tag
"Replicate original spacing" nil
)
179 (cons :tag
"Set an uniform spacing"
180 (integer :tag
"Number of blank lines before contents")
181 (integer :tag
"Number of blank lines after contents"))))
183 (defcustom org-e-ascii-charset
'ascii
184 "The charset allowed to represent various elements and objects.
186 `ascii' Only use plain ASCII characters
187 `latin1' Include Latin-1 characters
188 `utf-8' Use all UTF-8 characters"
189 :group
'org-export-e-ascii
191 (const :tag
"ASCII" ascii
)
192 (const :tag
"Latin-1" latin1
)
193 (const :tag
"UTF-8" utf-8
)))
195 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
197 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
198 "Characters for underlining headings in ASCII export.
200 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
201 and whose value is a list of characters.
203 For each supported charset, this variable associates a sequence
204 of underline characters. In a sequence, the characters will be
205 used in order for headlines level 1, 2, ... If no character is
206 available for a given level, the headline won't be underlined."
207 :group
'org-export-e-ascii
209 (cons :tag
"Underline characters sequence"
210 (const :tag
"ASCII charset" ascii
)
212 (cons :tag
"Underline characters sequence"
213 (const :tag
"Latin-1 charset" latin1
)
215 (cons :tag
"Underline characters sequence"
216 (const :tag
"UTF-8 charset" utf-8
)
217 (repeat character
))))
219 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
222 "Bullet characters for headlines converted to lists in ASCII export.
224 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
225 and whose value is a list of characters.
227 The first character is used for the first level considered as low
228 level, and so on. If there are more levels than characters given
229 here, the list will be repeated.
231 Note that this variable doesn't affect plain lists
233 :group
'org-export-e-ascii
235 (cons :tag
"Bullet characters for low level headlines"
236 (const :tag
"ASCII charset" ascii
)
238 (cons :tag
"Bullet characters for low level headlines"
239 (const :tag
"Latin-1 charset" latin1
)
241 (cons :tag
"Bullet characters for low level headlines"
242 (const :tag
"UTF-8 charset" utf-8
)
243 (repeat character
))))
245 (defcustom org-e-ascii-links-to-notes t
246 "Non-nil means convert links to notes before the next headline.
247 When nil, the link will be exported in place. If the line
248 becomes long in this way, it will be wrapped."
249 :group
'org-export-e-ascii
252 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
253 "Non-nil means keep all vertical lines in ASCII tables.
254 When nil, vertical lines will be removed except for those needed
255 for column grouping."
256 :group
'org-export-e-ascii
259 (defcustom org-e-ascii-table-widen-columns t
260 "Non-nil means widen narrowed columns for export.
261 When nil, narrowed columns will look in ASCII export just like in
262 Org mode, i.e. with \"=>\" as ellipsis."
263 :group
'org-export-e-ascii
266 (defcustom org-e-ascii-table-use-ascii-art nil
267 "Non-nil means table.el tables are turned into ascii-art.
269 It only makes sense when export charset is `utf-8'. It is nil by
270 default since it requires ascii-art-to-unicode.el package. You
271 can download it here:
273 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
275 (defcustom org-e-ascii-caption-above nil
276 "When non-nil, place caption string before the element.
277 Otherwise, place it right after it."
278 :group
'org-export-e-ascii
281 (defcustom org-e-ascii-verbatim-format
"`%s'"
282 "Format string used for verbatim text and inline code."
283 :group
'org-export-e-ascii
286 (defcustom org-e-ascii-format-drawer-function nil
287 "Function called to format a drawer in ASCII.
289 The function must accept two parameters:
290 NAME the drawer name, like \"LOGBOOK\"
291 CONTENTS the contents of the drawer.
292 WIDTH the text width within the drawer.
294 The function should return either the string to be exported or
295 nil to ignore the drawer.
297 For example, the variable could be set to the following function
298 in order to mimic default behaviour:
300 \(defun org-e-ascii-format-drawer-default \(name contents width\)
301 \"Format a drawer element for ASCII export.\"
303 :group
'org-export-e-ascii
306 (defcustom org-e-ascii-format-inlinetask-function nil
307 "Function called to format an inlinetask in ASCII.
309 The function must accept six parameters:
310 TODO the todo keyword, as a string
311 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
312 PRIORITY the inlinetask priority, as a string
313 NAME the inlinetask name, as a string.
314 TAGS the inlinetask tags, as a list of strings.
315 CONTENTS the contents of the inlinetask, as a string.
317 The function should return either the string to be exported or
318 nil to ignore the inline task.
320 For example, the variable could be set to the following function
321 in order to mimic default behaviour:
323 \(defun org-e-ascii-format-inlinetask-default
324 \(todo type priority name tags contents\)
325 \"Format an inline task element for ASCII export.\"
326 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
327 \(width org-e-ascii-inlinetask-width\)
328 \(org-e-ascii--indent-string
330 ;; Top line, with an additional blank line if not in UTF-8.
331 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
332 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
333 ;; Add title. Fill it if wider than inlinetask.
334 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
335 \(if \(<= \(length title\) width\) title
336 \(org-e-ascii--fill-string title width info\)\)\)
338 ;; If CONTENTS is not empty, insert it along with
340 \(when \(org-string-nw-p contents\)
341 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
343 \(make-string width \(if utf8p ?━ ?_\)\)\)
344 ;; Flush the inlinetask to the right.
345 \(- \(plist-get info :ascii-width\)
346 \(plist-get info :ascii-margin\)
347 \(plist-get info :ascii-inner-margin\)
348 \(org-e-ascii--current-text-width inlinetask info\)\)"
349 :group
'org-export-e-ascii
354 ;;; Internal Functions
356 ;; Internal functions fall into three categories.
358 ;; The first one is about text formatting. The core function is
359 ;; `org-e-ascii--current-text-width', which determines the current
360 ;; text width allowed to a given element. In other words, it helps
361 ;; keeping each line width within maximum text width defined in
362 ;; `org-e-ascii-text-width'. Once this information is known,
363 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
364 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
365 ;; operate on a given output string.
367 ;; The second category contains functions handling elements listings,
368 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
369 ;; returns a complete table of contents, `org-e-ascii--list-listings'
370 ;; returns a list of referenceable src-block elements, and
371 ;; `org-e-ascii--list-tables' does the same for table elements.
373 ;; The third category includes general helper functions.
374 ;; `org-e-ascii--build-title' creates the title for a given headline
375 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
376 ;; caption string associated to a table or a src-block.
377 ;; `org-e-ascii--describe-links' creates notes about links for
378 ;; insertion at the end of a section. It uses
379 ;; `org-e-ascii--unique-links' to get the list of links to describe.
380 ;; Eventually, `org-e-ascii--translate' translates a string according
381 ;; to language and charset specification.
384 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
385 "Fill a string with specified text-width and return it.
387 S is the string being filled. TEXT-WIDTH is an integer
388 specifying maximum length of a line. INFO is the plist used as
389 a communication channel.
391 Optional argument JUSTIFY can specify any type of justification
392 among `left', `center', `right' or `full'. A nil value is
393 equivalent to `left'. For a justification that doesn't also fill
394 string, see `org-e-ascii--justify-string'.
396 Return nil if S isn't a string."
397 ;; Don't fill paragraph when break should be preserved.
398 (cond ((not (stringp s
)) nil
)
399 ((plist-get info
:preserve-breaks
) s
)
401 (let ((fill-column text-width
)
402 (use-hard-newlines t
))
404 (fill-region (point-min) (point-max) justify
))
407 (defun org-e-ascii--justify-string (s text-width how
)
409 TEXT-WIDTH is an integer specifying maximum length of a line.
410 HOW determines the type of justification: it can be `left',
411 `right', `full' or `center'."
414 (goto-char (point-min))
415 (let ((fill-column text-width
))
416 (while (< (point) (point-max))
417 (justify-current-line how
)
421 (defun org-e-ascii--indent-string (s width
)
422 "Indent string S by WIDTH white spaces.
423 Empty lines are not indented."
425 (replace-regexp-in-string
426 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
428 (defun org-e-ascii--box-string (s info
)
429 "Return string S with a partial box to its left.
430 INFO is a plist used as a communicaton channel."
431 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
432 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
433 (replace-regexp-in-string
434 "^" (if utf8p
"│ " "| ")
435 ;; Remove last newline character.
436 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
438 (defun org-e-ascii--current-text-width (element info
)
439 "Return maximum text width for ELEMENT's contents.
440 INFO is a plist used as a communication channel."
441 (case (org-element-type element
)
442 ;; Elements with an absolute width: `headline' and `inlinetask'.
443 (inlinetask org-e-ascii-inlinetask-width
)
445 (- org-e-ascii-text-width
446 (let ((low-level-rank (org-export-low-level-p element info
)))
447 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
448 ;; Elements with a relative width: store maximum text width in
451 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
452 ;; Total width is determined by the presence, or not, of an
453 ;; inline task among ELEMENT parents.
455 (if (loop for parent in genealogy
456 thereis
(eq (org-element-type parent
) 'inlinetask
))
457 org-e-ascii-inlinetask-width
458 ;; No inlinetask: Remove global margin from text width.
459 (- org-e-ascii-text-width
460 org-e-ascii-global-margin
461 (let ((parent (org-export-get-parent-headline element
)))
462 ;; Inner margin doesn't apply to text before first
465 (let ((low-level-rank
466 (org-export-low-level-p parent info
)))
467 ;; Inner margin doesn't apply to contents of
468 ;; low level headlines, since they've got their
469 ;; own indentation mechanism.
470 (if low-level-rank
(* low-level-rank
2)
471 org-e-ascii-inner-margin
))))))))
473 ;; Each `quote-block', `quote-section' and `verse-block' above
474 ;; narrows text width by twice the standard margin size.
475 (+ (* (loop for parent in genealogy
476 when
(memq (org-element-type parent
)
477 '(quote-block quote-section verse-block
))
479 2 org-e-ascii-quote-margin
)
480 ;; Text width within a plain-list is restricted by
481 ;; indentation of current item. If that's the case,
482 ;; compute it with the help of `:structure' property from
483 ;; parent item, if any.
485 (if (eq (org-element-type element
) 'item
) element
486 (loop for parent in genealogy
487 when
(eq (org-element-type parent
) 'item
)
489 (if (not parent-item
) 0
490 ;; Compute indentation offset of the current item,
491 ;; that is the sum of the difference between its
492 ;; indentation and the indentation of the top item in
493 ;; the list and current item bullet's length. Also
494 ;; remove checkbox length, and tag length (for
495 ;; description lists) or bullet length.
496 (let ((struct (org-element-property :structure parent-item
))
497 (beg-item (org-element-property :begin parent-item
)))
498 (+ (- (org-list-get-ind beg-item struct
)
500 (org-list-get-top-point struct
) struct
))
501 (length (org-e-ascii--checkbox parent-item info
))
503 (or (org-list-get-tag beg-item struct
)
504 (org-list-get-bullet beg-item struct
)))))))))))))
506 (defun org-e-ascii--build-title
507 (element info text-width
&optional underline notags
)
508 "Format ELEMENT title and return it.
510 ELEMENT is either an `headline' or `inlinetask' element. INFO is
511 a plist used as a communication channel. TEXT-WIDTH is an
512 integer representing the maximum length of a line.
514 When optional argument UNDERLINE is non-nil, underline title,
515 without the tags, according to `org-e-ascii-underline'
518 if optional argument NOTAGS is nil, no tags will be added to the
520 (let* ((headlinep (eq (org-element-type element
) 'headline
))
522 ;; Numbering is specific to headlines.
523 (and headlinep
(org-export-numbered-headline-p element info
)
524 ;; All tests passed: build numbering string.
528 (org-export-get-headline-number element info
) ".")
531 (org-export-data (org-element-property :title element
) info
)))
533 (and (plist-get info
:with-todo-keywords
)
534 (let ((todo (org-element-property :todo-keyword element
)))
535 (and todo
(concat (org-export-data todo info
) " ")))))
536 (tags (and (not notags
)
537 (plist-get info
:with-tags
)
538 (let ((tag-list (org-export-get-tags element info
)))
541 (mapconcat 'identity tag-list
":"))))))
543 (and (plist-get info
:with-priority
)
544 (let ((char (org-element-property :priority element
)))
545 (and char
(format "(#%c) " char
)))))
546 (first-part (concat numbers todo priority text
)))
549 ;; Align tags, if any.
553 (max (- text-width
(1+ (length first-part
))) (length tags
)))
555 ;; Maybe underline text, if ELEMENT type is `headline' and an
556 ;; underline character has been defined.
557 (when (and underline headlinep
)
559 (nth (1- (org-export-get-relative-level element info
))
560 (cdr (assq (plist-get info
:ascii-charset
)
561 org-e-ascii-underline
)))))
564 (make-string (length first-part
) under-char
))))))))
566 (defun org-e-ascii--has-caption-p (element info
)
567 "Non-nil when ELEMENT has a caption affiliated keyword.
568 INFO is a plist used as a communication channel. This function
569 is meant to be used as a predicate for `org-export-get-ordinal'."
570 (org-element-property :caption element
))
572 (defun org-e-ascii--build-caption (element info
)
573 "Return caption string for ELEMENT, if applicable.
575 INFO is a plist used as a communication channel.
577 The caption string contains the sequence number of ELEMENT along
578 with its real caption. Return nil when ELEMENT has no affiliated
580 (let ((caption (org-element-property :caption element
)))
582 ;; Get sequence number of current src-block among every
583 ;; src-block with a caption.
585 (org-export-get-ordinal
586 element info nil
'org-e-ascii--has-caption-p
))
587 (title-fmt (org-e-ascii--translate
588 (case (org-element-type element
)
589 (table "Table %d: %s")
590 (src-block "Listing %d: %s"))
592 (org-e-ascii--fill-string
593 (format title-fmt reference
(org-export-data (car caption
) info
))
594 (org-e-ascii--current-text-width element info
) info
)))))
596 (defun org-e-ascii--build-toc (info &optional n keyword
)
597 "Return a table of contents.
599 INFO is a plist used as a communication channel.
601 Optional argument N, when non-nil, is an integer specifying the
604 Optional argument KEYWORD specifies the TOC keyword, if any, from
605 which the table of contents generation has been initiated."
606 (let ((title (org-e-ascii--translate "Table of Contents" info
)))
609 (make-string (length title
)
610 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
613 (if keyword
(org-e-ascii--current-text-width keyword info
)
614 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
617 (let* ((level (org-export-get-relative-level headline info
))
618 (indent (* (1- level
) 3)))
620 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
621 (org-e-ascii--build-title
622 headline info
(- text-width indent
) nil
623 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
624 (org-export-collect-headlines info n
) "\n")))))
626 (defun org-e-ascii--list-listings (keyword info
)
627 "Return a list of listings.
629 KEYWORD is the keyword that initiated the list of listings
630 generation. INFO is a plist used as a communication channel."
631 (let ((title (org-e-ascii--translate "List of Listings" info
)))
634 (make-string (length title
)
635 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
638 (if keyword
(org-e-ascii--current-text-width keyword info
)
639 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
640 ;; Use a counter instead of retreiving ordinal of each
645 ;; Store initial text so its length can be computed. This is
646 ;; used to properly align caption right to it in case of
647 ;; filling (like contents of a description list item).
649 (format (org-e-ascii--translate "Listing %d:" info
)
654 (org-e-ascii--indent-string
655 (org-e-ascii--fill-string
656 (let ((caption (org-element-property :caption src-block
)))
657 ;; Use short name in priority, if available.
658 (org-export-data (or (cdr caption
) (car caption
)) info
))
659 (- text-width
(length initial-text
)) info
)
660 (length initial-text
))))))
661 (org-export-collect-listings info
) "\n")))))
663 (defun org-e-ascii--list-tables (keyword info
)
664 "Return a list of listings.
666 KEYWORD is the keyword that initiated the list of listings
667 generation. INFO is a plist used as a communication channel."
668 (let ((title (org-e-ascii--translate "List of Tables" info
)))
671 (make-string (length title
)
672 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
675 (if keyword
(org-e-ascii--current-text-width keyword info
)
676 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
677 ;; Use a counter instead of retreiving ordinal of each
682 ;; Store initial text so its length can be computed. This is
683 ;; used to properly align caption right to it in case of
684 ;; filling (like contents of a description list item).
686 (format (org-e-ascii--translate "Table %d:" info
)
691 (org-e-ascii--indent-string
692 (org-e-ascii--fill-string
693 (let ((caption (org-element-property :caption table
)))
694 ;; Use short name in priority, if available.
695 (org-export-data (or (cdr caption
) (car caption
)) info
))
696 (- text-width
(length initial-text
)) info
)
697 (length initial-text
))))))
698 (org-export-collect-tables info
) "\n")))))
700 (defun org-e-ascii--unique-links (element info
)
701 "Return a list of unique link references in ELEMENT.
703 ELEMENT is either an headline element or a section element. INFO
704 is a plist used as a communication channel."
708 ;; Return LINK if it wasn't referenced so far, or nil.
709 ;; Update SEEN links along the way.
712 (cons (org-element-property :raw-link link
)
713 (org-element-contents link
))))
714 (unless (member footprint seen
)
715 (push footprint seen
) link
)))))
716 ;; If at a section, find parent headline, if any, in order to
717 ;; count links that might be in the title.
719 (if (eq (org-element-type element
) 'headline
) element
720 (or (org-export-get-parent-headline element
) element
))))
721 ;; Get all links in HEADLINE.
723 headline
'link
(lambda (link) (funcall unique-link-p link
)) info
)))
725 (defun org-e-ascii--describe-links (links width info
)
726 "Return a string describing a list of links.
728 LINKS is a list of link type objects, as returned by
729 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
730 the output string. INFO is a plist used as a communication
734 (let ((type (org-element-property :type link
))
735 (anchor (let ((desc (org-element-contents link
)))
736 (if (not desc
) (org-element-property :raw-link link
)
737 (org-export-data desc info
)))))
739 ;; Coderefs, radio links and fuzzy links are ignored.
740 ((member type
'("coderef" "radio" "fuzzy")) nil
)
741 ;; Id and custom-id links: Headlines refer to their numbering.
742 ((member type
'("custom-id" "id"))
743 (let ((dest (org-export-resolve-id-link link info
)))
745 (org-e-ascii--fill-string
749 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
751 (org-e-ascii--translate "See section %s" info
)
752 (mapconcat 'number-to-string
753 (org-export-get-headline-number dest info
) "."))))
754 width info
) "\n\n")))
755 ;; Do not add a link that cannot be resolved and doesn't have
756 ;; any description: destination is already visible in the
758 ((not (org-element-contents link
)) nil
)
761 (org-e-ascii--fill-string
762 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
767 (defun org-e-ascii--checkbox (item info
)
768 "Return checkbox string for ITEM or nil.
769 INFO is a plist used as a communication channel."
770 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
771 (case (org-element-property :checkbox item
)
772 (on (if utf8p
"☑ " "[X] "))
773 (off (if utf8p
"☐ " "[ ] "))
774 (trans (if utf8p
"☒ " "[-] ")))))
780 (defun org-e-ascii-template--document-title (info)
781 "Return document title, as a string.
782 INFO is a plist used as a communication channel."
783 (let ((text-width org-e-ascii-text-width
)
784 (title (org-export-data (plist-get info
:title
) info
))
785 (author (and (plist-get info
:with-author
)
786 (let ((auth (plist-get info
:author
)))
787 (and auth
(org-export-data auth info
)))))
788 (email (and (plist-get info
:with-email
)
789 (org-export-data (plist-get info
:email
) info
)))
790 (date (org-export-data (plist-get info
:date
) info
)))
791 ;; There are two types of title blocks depending on the presence
792 ;; of a title to display.
793 (if (string= title
"")
794 ;; Title block without a title. DATE is positioned at the top
795 ;; right of the document, AUTHOR to the top left and EMAIL
798 ((and (org-string-nw-p date
) (org-string-nw-p author
))
801 (make-string (- text-width
(length date
) (length author
)) ?
)
803 (when (org-string-nw-p email
) (concat "\n" email
))
805 ((and (org-string-nw-p date
) (org-string-nw-p email
))
808 (make-string (- text-width
(length date
) (length email
)) ?
)
810 ((org-string-nw-p date
)
812 (org-e-ascii--justify-string date text-width
'right
)
814 ((and (org-string-nw-p author
) (org-string-nw-p email
))
815 (concat author
"\n" email
"\n\n\n"))
816 ((org-string-nw-p author
) (concat author
"\n\n\n"))
817 ((org-string-nw-p email
) (concat email
"\n\n\n")))
818 ;; Title block with a title. Document's TITLE, along with the
819 ;; AUTHOR and its EMAIL are both overlined and an underlined,
820 ;; centered. Date is just below, also centered.
821 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
822 ;; Format TITLE. It may be filled if it is too wide,
823 ;; that is wider than the two thirds of the total width.
824 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
825 (formatted-title (org-e-ascii--fill-string title title-len info
))
828 (min (+ (max title-len
(length author
) (length email
)) 2)
829 text-width
) (if utf8p ?━ ?_
))))
830 (org-e-ascii--justify-string
833 (upcase formatted-title
)
835 ((and (org-string-nw-p author
) (org-string-nw-p email
))
836 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
837 ((org-string-nw-p author
)
838 (concat (if utf8p
"\n\n\n" "\n\n") author
))
839 ((org-string-nw-p email
)
840 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
842 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
843 "\n\n\n") text-width
'center
)))))
845 (defun org-e-ascii-template (contents info
)
846 "Return complete document string after ASCII conversion.
847 CONTENTS is the transcoded contents string. INFO is a plist
848 holding export options."
849 (org-element-normalize-string
850 (org-e-ascii--indent-string
851 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
852 ;; 1. Build title block.
854 (org-e-ascii-template--document-title info
)
855 ;; 2. Table of contents.
856 (let ((depth (plist-get info
:with-toc
)))
859 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
861 ;; 3. Document's body.
863 ;; 4. Footnote definitions.
864 (let ((definitions (org-export-collect-footnote-definitions
865 (plist-get info
:parse-tree
) info
))
866 ;; Insert full links right inside the footnote definition
867 ;; as they have no chance to be inserted later.
868 (org-e-ascii-links-to-notes nil
))
872 (let ((title (org-e-ascii--translate "Footnotes" info
)))
877 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
881 (let ((id (format "[%s] " (car ref
))))
882 ;; Distinguish between inline definitions and
883 ;; full-fledged definitions.
885 (let ((def (nth 2 ref
)))
886 (if (eq (org-element-type def
) 'org-data
)
887 ;; Full-fledged definition: footnote ID is
888 ;; inserted inside the first parsed paragraph
889 ;; (FIRST), if any, to be sure filling will
890 ;; take it into consideration.
891 (let ((first (car (org-element-contents def
))))
892 (if (not (eq (org-element-type first
) 'paragraph
))
893 (concat id
"\n" (org-export-data def info
))
894 (push id
(nthcdr 2 first
))
895 (org-export-data def info
)))
896 ;; Fill paragraph once footnote ID is inserted in
897 ;; order to have a correct length for first line.
898 (org-e-ascii--fill-string
899 (concat id
(org-export-data def info
))
900 text-width info
))))))
901 definitions
"\n\n"))))
902 ;; 5. Creator. Ignore `comment' value as there are no comments in
903 ;; ASCII. Justify it to the bottom right.
904 (let ((creator-info (plist-get info
:with-creator
)))
905 (unless (or (not creator-info
) (eq creator-info
'comment
))
908 (org-e-ascii--fill-string
909 (plist-get info
:creator
) text-width info
'right
))))))
910 org-e-ascii-global-margin
)))
912 (defun org-e-ascii--translate (s info
)
913 "Translate string S according to specified language and charset.
914 INFO is a plist used as a communication channel."
915 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
916 (org-export-translate s charset info
)))
920 ;;; Transcode Functions
924 ;; Babel Calls are ignored.
929 (defun org-e-ascii-bold (bold contents info
)
930 "Transcode BOLD from Org to ASCII.
931 CONTENTS is the text with bold markup. INFO is a plist holding
932 contextual information."
933 (format "*%s*" contents
))
938 (defun org-e-ascii-center-block (center-block contents info
)
939 "Transcode a CENTER-BLOCK element from Org to ASCII.
940 CONTENTS holds the contents of the block. INFO is a plist
941 holding contextual information."
942 (org-e-ascii--justify-string
943 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
948 (defun org-e-ascii-clock (clock contents info
)
949 "Transcode a CLOCK object from Org to ASCII.
950 CONTENTS is nil. INFO is a plist holding contextual
952 (concat org-clock-string
" "
953 (org-translate-time (org-element-property :value clock
))
954 (let ((time (org-element-property :time clock
)))
959 (org-split-string time
":")))))))
964 (defun org-e-ascii-code (code contents info
)
965 "Return a CODE object from Org to ASCII.
966 CONTENTS is nil. INFO is a plist holding contextual
968 (format org-e-ascii-verbatim-format
(org-element-property :value code
)))
973 ;; Comments are ignored.
978 ;; Comment Blocks are ignored.
983 (defun org-e-ascii-drawer (drawer contents info
)
984 "Transcode a DRAWER element from Org to ASCII.
985 CONTENTS holds the contents of the block. INFO is a plist
986 holding contextual information."
987 (let ((name (org-element-property :drawer-name drawer
))
988 (width (org-e-ascii--current-text-width drawer info
)))
989 (if (functionp org-e-ascii-format-drawer-function
)
990 (funcall org-e-ascii-format-drawer-function name contents width
)
991 ;; If there's no user defined function: simply
992 ;; display contents of the drawer.
998 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
999 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1000 CONTENTS holds the contents of the block. INFO is a plist
1001 holding contextual information."
1007 (defun org-e-ascii-entity (entity contents info
)
1008 "Transcode an ENTITY object from Org to ASCII.
1009 CONTENTS are the definition itself. INFO is a plist holding
1010 contextual information."
1011 (org-element-property
1012 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1018 (defun org-e-ascii-example-block (example-block contents info
)
1019 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1020 CONTENTS is nil. INFO is a plist holding contextual information."
1021 (org-e-ascii--box-string
1022 (org-export-format-code-default example-block info
) info
))
1027 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1028 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1029 CONTENTS is nil. INFO is a plist holding contextual information."
1030 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1031 (org-element-property :value export-snippet
)))
1036 (defun org-e-ascii-export-block (export-block contents info
)
1037 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1038 CONTENTS is nil. INFO is a plist holding contextual information."
1039 (when (string= (org-element-property :type export-block
) "ASCII")
1040 (org-remove-indentation (org-element-property :value export-block
))))
1045 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1046 "Transcode a FIXED-WIDTH element from Org to ASCII.
1047 CONTENTS is nil. INFO is a plist holding contextual information."
1048 (org-e-ascii--box-string
1049 (org-remove-indentation
1050 (org-element-property :value fixed-width
)) info
))
1053 ;;;; Footnote Definition
1055 ;; Footnote Definitions are ignored. They are compiled at the end of
1056 ;; the document, by `org-e-ascii-template'.
1059 ;;;; Footnote Reference
1061 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1062 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1063 CONTENTS is nil. INFO is a plist holding contextual information."
1064 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1069 (defun org-e-ascii-headline (headline contents info
)
1070 "Transcode an HEADLINE element from Org to ASCII.
1071 CONTENTS holds the contents of the headline. INFO is a plist
1072 holding contextual information."
1073 ;; Don't export footnote section, which will be handled at the end
1075 (unless (org-element-property :footnote-section-p headline
)
1076 (let* ((low-level-rank (org-export-low-level-p headline info
))
1077 (width (org-e-ascii--current-text-width headline info
))
1078 ;; Blank lines between headline and its contents.
1079 ;; `org-e-ascii-headline-spacing', when set, overwrites
1080 ;; original buffer's spacing.
1083 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1084 (org-element-property :pre-blank headline
)) ?
\n))
1085 ;; Even if HEADLINE has no section, there might be some
1086 ;; links in its title that we shouldn't forget to describe.
1088 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1089 (let ((title (org-element-property :title headline
)))
1091 (org-e-ascii--describe-links
1092 (org-e-ascii--unique-links title info
) width info
))))))
1093 ;; Deep subtree: export it as a list item.
1097 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1098 org-e-ascii-bullets
))))
1100 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1103 (org-e-ascii--build-title headline info width
) "\n"
1104 ;; Contents, indented by length of bullet.
1106 (org-e-ascii--indent-string
1108 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1110 ;; Else: Standard headline.
1112 (org-e-ascii--build-title headline info width
'underline
)
1114 (concat (when (org-string-nw-p links
) links
) contents
))))))
1117 ;;;; Horizontal Rule
1119 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1120 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1121 CONTENTS is nil. INFO is a plist holding contextual
1123 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info
))
1125 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1126 (org-e-ascii--justify-string
1127 (make-string (if (wholenump spec-width
) spec-width text-width
)
1128 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1129 text-width
'center
)))
1132 ;;;; Inline Babel Call
1134 ;; Inline Babel Calls are ignored.
1137 ;;;; Inline Src Block
1139 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1140 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1141 CONTENTS holds the contents of the item. INFO is a plist holding
1142 contextual information."
1143 (format org-e-ascii-verbatim-format
1144 (org-element-property :value inline-src-block
)))
1149 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1150 "Transcode an INLINETASK element from Org to ASCII.
1151 CONTENTS holds the contents of the block. INFO is a plist
1152 holding contextual information."
1153 (let ((width (org-e-ascii--current-text-width inlinetask info
)))
1154 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1155 ;; with appropriate arguments.
1156 (if (functionp org-e-ascii-format-inlinetask-function
)
1157 (funcall org-e-ascii-format-inlinetask-function
1159 (and (plist-get info
:with-todo-keywords
)
1160 (let ((todo (org-element-property
1161 :todo-keyword inlinetask
)))
1162 (and todo
(org-export-data todo info
))))
1164 (org-element-property :todo-type inlinetask
)
1166 (and (plist-get info
:with-priority
)
1167 (org-element-property :priority inlinetask
))
1169 (org-export-data (org-element-property :title inlinetask
) info
)
1171 (and (plist-get info
:with-tags
)
1172 (org-element-property :tags inlinetask
))
1173 ;; contents and width
1175 ;; Otherwise, use a default template.
1176 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1177 (org-e-ascii--indent-string
1179 ;; Top line, with an additional blank line if not in UTF-8.
1180 (make-string width
(if utf8p ?━ ?_
)) "\n"
1181 (unless utf8p
(concat (make-string width ?
) "\n"))
1182 ;; Add title. Fill it if wider than inlinetask.
1183 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1184 (if (<= (length title
) width
) title
1185 (org-e-ascii--fill-string title width info
)))
1187 ;; If CONTENTS is not empty, insert it along with
1189 (when (org-string-nw-p contents
)
1190 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1192 (make-string width
(if utf8p ?━ ?_
)))
1193 ;; Flush the inlinetask to the right.
1194 (- org-e-ascii-text-width org-e-ascii-global-margin
1195 (if (not (org-export-get-parent-headline inlinetask
)) 0
1196 org-e-ascii-inner-margin
)
1197 (org-e-ascii--current-text-width inlinetask info
)))))))
1201 (defun org-e-ascii-italic (italic contents info
)
1202 "Transcode italic from Org to ASCII.
1203 CONTENTS is the text with italic markup. INFO is a plist holding
1204 contextual information."
1205 (format "/%s/" contents
))
1210 (defun org-e-ascii-item (item contents info
)
1211 "Transcode an ITEM element from Org to ASCII.
1212 CONTENTS holds the contents of the item. INFO is a plist holding
1213 contextual information."
1214 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1215 (checkbox (org-e-ascii--checkbox item info
))
1216 (list-type (org-element-property :type
(org-export-get-parent item
)))
1218 ;; First parent of ITEM is always the plain-list. Get
1219 ;; `:type' property from it.
1220 (org-list-bullet-string
1224 (org-export-data (org-element-property :tag item
) info
)
1227 ;; Return correct number for ITEM, paying attention to
1229 (let* ((struct (org-element-property :structure item
))
1230 (bul (org-element-property :bullet item
))
1231 (num (number-to-string
1232 (car (last (org-list-get-item-number
1233 (org-element-property :begin item
)
1235 (org-list-prevs-alist struct
)
1236 (org-list-parents-alist struct
)))))))
1237 (replace-regexp-in-string "[0-9]+" num bul
)))
1238 (t (let ((bul (org-element-property :bullet item
)))
1239 ;; Change bullets into more visible form if UTF-8 is active.
1241 (replace-regexp-in-string
1243 (replace-regexp-in-string
1245 (replace-regexp-in-string "*" "‣" bul
))))))))))
1248 (unless (eq list-type
'descriptive
) checkbox
)
1249 ;; Contents: Pay attention to indentation. Note: check-boxes are
1250 ;; already taken care of at the paragraph level so they don't
1251 ;; interfere with indentation.
1252 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1253 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1255 (concat "\n" contents
))))))
1260 (defun org-e-ascii-keyword (keyword contents info
)
1261 "Transcode a KEYWORD element from Org to ASCII.
1262 CONTENTS is nil. INFO is a plist holding contextual
1264 (let ((key (org-element-property :key keyword
))
1265 (value (org-element-property :value keyword
)))
1267 ((string= key
"ASCII") value
)
1268 ((string= key
"TOC")
1269 (let ((value (downcase value
)))
1271 ((string-match "\\<headlines\\>" value
)
1272 (let ((depth (or (and (string-match "[0-9]+" value
)
1273 (string-to-number (match-string 0 value
)))
1274 (plist-get info
:with-toc
))))
1275 (org-e-ascii--build-toc
1276 info
(and (wholenump depth
) depth
) keyword
)))
1277 ((string= "tables" value
)
1278 (org-e-ascii--list-tables keyword info
))
1279 ((string= "listings" value
)
1280 (org-e-ascii--list-listings keyword info
))))))))
1283 ;;;; Latex Environment
1285 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1286 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1287 CONTENTS is nil. INFO is a plist holding contextual
1289 (org-remove-indentation (org-element-property :value latex-environment
)))
1294 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1295 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1296 CONTENTS is nil. INFO is a plist holding contextual
1298 (org-element-property :value latex-fragment
))
1303 (defun org-e-ascii-line-break (line-break contents info
)
1304 "Transcode a LINE-BREAK object from Org to ASCII.
1305 CONTENTS is nil. INFO is a plist holding contextual
1306 information." hard-newline
)
1311 (defun org-e-ascii-link (link desc info
)
1312 "Transcode a LINK object from Org to ASCII.
1314 DESC is the description part of the link, or the empty string.
1315 INFO is a plist holding contextual information."
1316 (let ((raw-link (org-element-property :raw-link link
))
1317 (type (org-element-property :type link
)))
1319 ((string= type
"coderef")
1320 (let ((ref (org-element-property :path link
)))
1321 (format (org-export-get-coderef-format ref desc
)
1322 (org-export-resolve-coderef ref info
))))
1323 ;; Do not apply a special syntax on radio links. Though, use
1324 ;; transcoded target's contents as output.
1325 ((string= type
"radio")
1326 (let ((destination (org-export-resolve-radio-link link info
)))
1328 (org-export-data (org-element-contents destination
) info
))))
1329 ;; Do not apply a special syntax on fuzzy links pointing to
1331 ((string= type
"fuzzy")
1332 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1333 ;; Ignore invisible "#+TARGET: path".
1334 (unless (eq (org-element-type destination
) 'keyword
)
1335 (if (org-string-nw-p desc
) desc
1338 (org-export-get-ordinal
1339 destination info nil
'org-e-ascii--has-caption-p
)))
1341 (if (atom number
) (number-to-string number
)
1342 (mapconcat 'number-to-string number
".")))))))))
1344 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1346 (format "[%s]" desc
)
1347 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1352 (defun org-e-ascii-paragraph (paragraph contents info
)
1353 "Transcode a PARAGRAPH element from Org to ASCII.
1354 CONTENTS is the contents of the paragraph, as a string. INFO is
1355 the plist used as a communication channel."
1356 (org-e-ascii--fill-string
1358 (org-e-ascii--current-text-width paragraph info
) info
))
1363 (defun org-e-ascii-plain-list (plain-list contents info
)
1364 "Transcode a PLAIN-LIST element from Org to ASCII.
1365 CONTENTS is the contents of the list. INFO is a plist holding
1366 contextual information."
1372 (defun org-e-ascii-plain-text (text info
)
1373 "Transcode a TEXT string from Org to ASCII.
1374 INFO is a plist used as a communication channel."
1375 (if (not (plist-get info
:with-special-strings
)) text
1376 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1377 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) text
1378 ;; Usual replacements in utf-8 with proper option set.
1379 (replace-regexp-in-string
1381 (replace-regexp-in-string
1383 (replace-regexp-in-string "---" "—" text
))))))
1388 (defun org-e-ascii-planning (planning contents info
)
1389 "Transcode a PLANNING element from Org to ASCII.
1390 CONTENTS is nil. INFO is a plist used as a communication
1395 (list (let ((closed (org-element-property :closed planning
)))
1396 (when closed
(concat org-closed-string
" "
1397 (org-translate-time closed
))))
1398 (let ((deadline (org-element-property :deadline planning
)))
1399 (when deadline
(concat org-deadline-string
" "
1400 (org-translate-time deadline
))))
1401 (let ((scheduled (org-element-property :scheduled planning
)))
1402 (when scheduled
(concat org-scheduled-string
" "
1403 (org-translate-time scheduled
))))))
1407 ;;;; Property Drawer
1409 ;; Property drawers are ignored.
1414 (defun org-e-ascii-quote-block (quote-block contents info
)
1415 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1416 CONTENTS holds the contents of the block. INFO is a plist
1417 holding contextual information."
1418 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1419 (org-e-ascii--indent-string
1420 (org-remove-indentation
1421 (org-e-ascii--fill-string contents width info
))
1422 org-e-ascii-quote-margin
)))
1427 (defun org-e-ascii-quote-section (quote-section contents info
)
1428 "Transcode a QUOTE-SECTION element from Org to ASCII.
1429 CONTENTS is nil. INFO is a plist holding contextual information."
1430 (let ((width (org-e-ascii--current-text-width quote-section info
))
1433 (org-remove-indentation (org-element-property :value quote-section
))
1435 (org-e-ascii--indent-string
1437 (+ org-e-ascii-quote-margin
1438 ;; Don't apply inner margin if parent headline is low level.
1439 (let ((headline (org-export-get-parent-headline quote-section
)))
1440 (if (org-export-low-level-p headline info
) 0
1441 org-e-ascii-inner-margin
))))))
1446 (defun org-e-ascii-radio-target (radio-target contents info
)
1447 "Transcode a RADIO-TARGET object from Org to ASCII.
1448 CONTENTS is the contents of the target. INFO is a plist holding
1449 contextual information."
1454 (defun org-e-ascii-section (section contents info
)
1455 "Transcode a SECTION element from Org to ASCII.
1456 CONTENTS is the contents of the section. INFO is a plist holding
1457 contextual information."
1458 (org-e-ascii--indent-string
1461 (when org-e-ascii-links-to-notes
1462 ;; Add list of links at the end of SECTION.
1463 (let ((links (org-e-ascii--describe-links
1464 (org-e-ascii--unique-links section info
)
1465 (org-e-ascii--current-text-width section info
) info
)))
1466 ;; Separate list of links and section contents.
1467 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1468 ;; Do not apply inner margin if parent headline is low level.
1469 (let ((headline (org-export-get-parent-headline section
)))
1470 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1471 org-e-ascii-inner-margin
))))
1476 (defun org-e-ascii-special-block (special-block contents info
)
1477 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1478 CONTENTS holds the contents of the block. INFO is a plist
1479 holding contextual information."
1485 (defun org-e-ascii-src-block (src-block contents info
)
1486 "Transcode a SRC-BLOCK element from Org to ASCII.
1487 CONTENTS holds the contents of the item. INFO is a plist holding
1488 contextual information."
1489 (let ((caption (org-e-ascii--build-caption src-block info
)))
1491 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1492 (org-e-ascii--box-string
1493 (org-export-format-code-default src-block info
) info
)
1494 (when (and caption
(not org-e-ascii-caption-above
))
1495 (concat "\n" caption
)))))
1497 ;;;; Statistics Cookie
1499 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1500 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1501 CONTENTS is nil. INFO is a plist holding contextual information."
1502 (org-element-property :value statistics-cookie
))
1507 (defun org-e-ascii-subscript (subscript contents info
)
1508 "Transcode a SUBSCRIPT object from Org to ASCII.
1509 CONTENTS is the contents of the object. INFO is a plist holding
1510 contextual information."
1511 (if (org-element-property :use-brackets-p subscript
)
1512 (format "_{%s}" contents
)
1513 (format "_%s" contents
)))
1518 (defun org-e-ascii-superscript (superscript contents info
)
1519 "Transcode a SUPERSCRIPT object from Org to ASCII.
1520 CONTENTS is the contents of the object. INFO is a plist holding
1521 contextual information."
1522 (if (org-element-property :use-brackets-p superscript
)
1523 (format "_{%s}" contents
)
1524 (format "_%s" contents
)))
1529 (defun org-e-ascii-strike-through (strike-through contents info
)
1530 "Transcode STRIKE-THROUGH from Org to ASCII.
1531 CONTENTS is text with strike-through markup. INFO is a plist
1532 holding contextual information."
1533 (format "+%s+" contents
))
1538 (defun org-e-ascii-table (table contents info
)
1539 "Transcode a TABLE element from Org to ASCII.
1540 CONTENTS is the contents of the table. INFO is a plist holding
1541 contextual information."
1542 (let ((caption (org-e-ascii--build-caption table info
)))
1544 ;; Possibly add a caption string above.
1545 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1546 ;; Insert table. Note: "table.el" tables are left unmodified.
1547 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1548 ((and org-e-ascii-table-use-ascii-art
1549 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1550 (require 'ascii-art-to-unicode nil t
))
1552 (insert (org-remove-indentation
1553 (org-element-property :value table
)))
1554 (goto-char (point-min))
1556 (goto-char (point-max))
1557 (skip-chars-backward " \r\t\n")
1558 (buffer-substring (point-min) (point))))
1559 (t (org-remove-indentation (org-element-property :value table
))))
1560 ;; Possible add a caption string below.
1561 (when (and caption
(not org-e-ascii-caption-above
))
1562 (concat "\n" caption
)))))
1567 (defun org-e-ascii--table-cell-width (table-cell info
)
1568 "Return width of TABLE-CELL.
1570 INFO is a plist used as a communication channel.
1572 Width of a cell is determined either by a width cookie in the
1573 same column as the cell, or by the maximum cell's length in that
1576 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1578 (or (and (not org-e-ascii-table-widen-columns
)
1579 (org-export-table-cell-width table-cell info
))
1580 (let* ((max-width 0)
1581 (table (org-export-get-parent-table table-cell
))
1582 (specialp (org-export-table-has-special-column-p table
))
1583 (col (cdr (org-export-table-cell-address table-cell info
))))
1590 (org-element-contents
1591 (elt (if specialp
(cdr (org-element-contents row
))
1592 (org-element-contents row
))
1599 (defun org-e-ascii-table-cell (table-cell contents info
)
1600 "Transcode a TABLE-CELL object from Org to ASCII.
1601 CONTENTS is the cell contents. INFO is a plist used as
1602 a communication channel."
1603 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1604 ;; is nil and some width cookie has set it, use that value.
1605 ;; Otherwise, compute the maximum width among transcoded data of
1606 ;; each cell in the column.
1607 (let ((width (org-e-ascii--table-cell-width table-cell info
)))
1608 ;; When contents are too large, truncate them.
1609 (unless (or org-e-ascii-table-widen-columns
(<= (length contents
) width
))
1610 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1611 ;; Align contents correctly within the cell.
1612 (let* ((indent-tabs-mode nil
)
1615 (org-e-ascii--justify-string
1617 (org-export-table-cell-alignment table-cell info
)))))
1618 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1620 (concat (format " %s " contents
)
1621 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1622 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1627 (defun org-e-ascii-table-row (table-row contents info
)
1628 "Transcode a TABLE-ROW element from Org to ASCII.
1629 CONTENTS is the row contents. INFO is a plist used as
1630 a communication channel."
1631 (when (eq (org-element-property :type table-row
) 'standard
)
1634 (lambda (lcorner horiz vert rcorner
)
1639 table-row
'table-cell
1641 (let ((width (org-e-ascii--table-cell-width cell info
))
1642 (borders (org-export-table-cell-borders cell info
)))
1644 ;; In order to know if CELL starts the row, do
1645 ;; not compare it with the first cell in the row
1646 ;; as there might be a special column. Instead,
1647 ;; compare it with the first exportable cell,
1648 ;; obtained with `org-element-map'.
1649 (when (and (memq 'left borders
)
1650 (eq (org-element-map
1651 table-row
'table-cell
'identity info t
)
1654 (make-string (+ 2 width
) (string-to-char horiz
))
1656 ((not (memq 'right borders
)) nil
)
1657 ((eq (car (last (org-element-contents table-row
))) cell
)
1661 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1662 (borders (org-export-table-cell-borders
1663 (org-element-map table-row
'table-cell
'identity info t
)
1666 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1667 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1668 (funcall build-hline
"+" "-" "+" "+")))
1669 ((memq 'above borders
)
1670 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1671 (funcall build-hline
"+" "-" "+" "+"))))
1672 (when (memq 'left borders
) (if utf8p
"│" "|"))
1674 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1675 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1676 (funcall build-hline
"+" "-" "+" "+")))))))
1681 ;; Targets are invisible.
1686 (defun org-e-ascii-timestamp (timestamp contents info
)
1687 "Transcode a TIMESTAMP object from Org to ASCII.
1688 CONTENTS is nil. INFO is a plist holding contextual information."
1689 (let ((value (org-translate-time (org-element-property :value timestamp
)))
1691 (org-translate-time (org-element-property :range-end timestamp
)))
1692 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1694 (when range-end
(concat (if utf8p
"–" "--") range-end
)))))
1699 (defun org-e-ascii-underline (underline contents info
)
1700 "Transcode UNDERLINE from Org to ASCII.
1701 CONTENTS is the text with underline markup. INFO is a plist
1702 holding contextual information."
1703 (format "_%s_" contents
))
1708 (defun org-e-ascii-verbatim (verbatim contents info
)
1709 "Return a VERBATIM object from Org to ASCII.
1710 CONTENTS is nil. INFO is a plist holding contextual information."
1711 (format org-e-ascii-verbatim-format
1712 (org-element-property :value verbatim
)))
1717 (defun org-e-ascii-verse-block (verse-block contents info
)
1718 "Transcode a VERSE-BLOCK element from Org to ASCII.
1719 CONTENTS is verse block contents. INFO is a plist holding
1720 contextual information."
1721 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1722 (org-e-ascii--indent-string
1723 (org-e-ascii--justify-string contents verse-width
'left
)
1724 org-e-ascii-quote-margin
)))
1729 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1730 "Filter controlling number of blank lines after an headline.
1732 HEADLINE is a string representing a transcoded headline.
1733 BACK-END is symbol specifying back-end used for export. INFO is
1734 plist containing the communication channel.
1736 This function only applies to `e-ascii' back-end. See
1737 `org-e-ascii-headline-spacing' for information.
1739 For any other back-end, HEADLINE is returned as-is."
1740 (if (not org-e-ascii-headline-spacing
) headline
1741 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1742 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1746 ;;; Interactive function
1749 (defun org-e-ascii-export-as-ascii
1750 (&optional subtreep visible-only body-only ext-plist
)
1751 "Export current buffer to a text buffer.
1753 If narrowing is active in the current buffer, only export its
1756 If a region is active, export that region.
1758 When optional argument SUBTREEP is non-nil, export the sub-tree
1759 at point, extracting information from the headline properties
1762 When optional argument VISIBLE-ONLY is non-nil, don't export
1763 contents of hidden elements.
1765 When optional argument BODY-ONLY is non-nil, strip title, table
1766 of contents and footnote definitions from output.
1768 EXT-PLIST, when provided, is a property list with external
1769 parameters overriding Org default settings, but still inferior to
1770 file-local settings.
1772 Export is done in a buffer named \"*Org E-ASCII Export*\", which
1773 will be displayed when `org-export-show-temporary-export-buffer'
1776 (let ((outbuf (org-export-to-buffer
1777 'e-ascii
"*Org E-ASCII Export*"
1778 subtreep visible-only body-only ext-plist
)))
1779 (with-current-buffer outbuf
(text-mode))
1780 (when org-export-show-temporary-export-buffer
1781 (switch-to-buffer-other-window outbuf
))))
1784 (defun org-e-ascii-export-to-ascii
1785 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1786 "Export current buffer to a text file.
1788 If narrowing is active in the current buffer, only export its
1791 If a region is active, export that region.
1793 When optional argument SUBTREEP is non-nil, export the sub-tree
1794 at point, extracting information from the headline properties
1797 When optional argument VISIBLE-ONLY is non-nil, don't export
1798 contents of hidden elements.
1800 When optional argument BODY-ONLY is non-nil, strip title, table
1801 of contents and footnote definitions from output.
1803 EXT-PLIST, when provided, is a property list with external
1804 parameters overriding Org default settings, but still inferior to
1805 file-local settings.
1807 When optional argument PUB-DIR is set, use it as the publishing
1810 Return output file's name."
1812 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1814 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1817 (provide 'org-e-ascii
)
1818 ;;; org-e-ascii.el ends here