1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; 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 a s v b
'(:ascii-charset ascii
))))
111 (org-e-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
112 (?L
"As Latin1 buffer"
114 (org-e-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
117 (org-e-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
118 (?U
"As UTF-8 buffer"
120 (org-e-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
123 (org-e-ascii-export-to-ascii a 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 three 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 ;; Disable `adaptive-fill-mode' so it doesn't prevent
439 ;; filling lines matching `adaptive-fill-regexp'.
440 (adaptive-fill-mode nil
))
441 (while (< (point) (point-max))
442 (justify-current-line how
)
446 (defun org-e-ascii--indent-string (s width
)
447 "Indent string S by WIDTH white spaces.
448 Empty lines are not indented."
450 (replace-regexp-in-string
451 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
453 (defun org-e-ascii--box-string (s info
)
454 "Return string S with a partial box to its left.
455 INFO is a plist used as a communicaton channel."
456 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
457 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
458 (replace-regexp-in-string
459 "^" (if utf8p
"│ " "| ")
460 ;; Remove last newline character.
461 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
463 (defun org-e-ascii--current-text-width (element info
)
464 "Return maximum text width for ELEMENT's contents.
465 INFO is a plist used as a communication channel."
466 (case (org-element-type element
)
467 ;; Elements with an absolute width: `headline' and `inlinetask'.
468 (inlinetask org-e-ascii-inlinetask-width
)
470 (- org-e-ascii-text-width
471 (let ((low-level-rank (org-export-low-level-p element info
)))
472 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
473 ;; Elements with a relative width: store maximum text width in
476 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
477 ;; Total width is determined by the presence, or not, of an
478 ;; inline task among ELEMENT parents.
480 (if (loop for parent in genealogy
481 thereis
(eq (org-element-type parent
) 'inlinetask
))
482 org-e-ascii-inlinetask-width
483 ;; No inlinetask: Remove global margin from text width.
484 (- org-e-ascii-text-width
485 org-e-ascii-global-margin
486 (let ((parent (org-export-get-parent-headline element
)))
487 ;; Inner margin doesn't apply to text before first
490 (let ((low-level-rank
491 (org-export-low-level-p parent info
)))
492 ;; Inner margin doesn't apply to contents of
493 ;; low level headlines, since they've got their
494 ;; own indentation mechanism.
495 (if low-level-rank
(* low-level-rank
2)
496 org-e-ascii-inner-margin
))))))))
498 ;; Each `quote-block', `quote-section' and `verse-block' above
499 ;; narrows text width by twice the standard margin size.
500 (+ (* (loop for parent in genealogy
501 when
(memq (org-element-type parent
)
502 '(quote-block quote-section verse-block
))
504 2 org-e-ascii-quote-margin
)
505 ;; Text width within a plain-list is restricted by
506 ;; indentation of current item. If that's the case,
507 ;; compute it with the help of `:structure' property from
508 ;; parent item, if any.
510 (if (eq (org-element-type element
) 'item
) element
511 (loop for parent in genealogy
512 when
(eq (org-element-type parent
) 'item
)
514 (if (not parent-item
) 0
515 ;; Compute indentation offset of the current item,
516 ;; that is the sum of the difference between its
517 ;; indentation and the indentation of the top item in
518 ;; the list and current item bullet's length. Also
519 ;; remove checkbox length, and tag length (for
520 ;; description lists) or bullet length.
521 (let ((struct (org-element-property :structure parent-item
))
522 (beg-item (org-element-property :begin parent-item
)))
523 (+ (- (org-list-get-ind beg-item struct
)
525 (org-list-get-top-point struct
) struct
))
526 (length (org-e-ascii--checkbox parent-item info
))
528 (or (org-list-get-tag beg-item struct
)
529 (org-list-get-bullet beg-item struct
)))))))))))))
531 (defun org-e-ascii--build-title
532 (element info text-width
&optional underline notags
)
533 "Format ELEMENT title and return it.
535 ELEMENT is either an `headline' or `inlinetask' element. INFO is
536 a plist used as a communication channel. TEXT-WIDTH is an
537 integer representing the maximum length of a line.
539 When optional argument UNDERLINE is non-nil, underline title,
540 without the tags, according to `org-e-ascii-underline'
543 if optional argument NOTAGS is nil, no tags will be added to the
545 (let* ((headlinep (eq (org-element-type element
) 'headline
))
547 ;; Numbering is specific to headlines.
548 (and headlinep
(org-export-numbered-headline-p element info
)
549 ;; All tests passed: build numbering string.
553 (org-export-get-headline-number element info
) ".")
556 (org-export-data (org-element-property :title element
) info
)))
558 (and (plist-get info
:with-todo-keywords
)
559 (let ((todo (org-element-property :todo-keyword element
)))
560 (and todo
(concat (org-export-data todo info
) " ")))))
561 (tags (and (not notags
)
562 (plist-get info
:with-tags
)
563 (let ((tag-list (org-export-get-tags element info
)))
566 (mapconcat 'identity tag-list
":"))))))
568 (and (plist-get info
:with-priority
)
569 (let ((char (org-element-property :priority element
)))
570 (and char
(format "(#%c) " char
)))))
571 (first-part (concat numbers todo priority text
)))
574 ;; Align tags, if any.
578 (max (- text-width
(1+ (length first-part
))) (length tags
)))
580 ;; Maybe underline text, if ELEMENT type is `headline' and an
581 ;; underline character has been defined.
582 (when (and underline headlinep
)
584 (nth (1- (org-export-get-relative-level element info
))
585 (cdr (assq (plist-get info
:ascii-charset
)
586 org-e-ascii-underline
)))))
589 (make-string (length first-part
) under-char
))))))))
591 (defun org-e-ascii--has-caption-p (element info
)
592 "Non-nil when ELEMENT has a caption affiliated keyword.
593 INFO is a plist used as a communication channel. This function
594 is meant to be used as a predicate for `org-export-get-ordinal'."
595 (org-element-property :caption element
))
597 (defun org-e-ascii--build-caption (element info
)
598 "Return caption string for ELEMENT, if applicable.
600 INFO is a plist used as a communication channel.
602 The caption string contains the sequence number of ELEMENT along
603 with its real caption. Return nil when ELEMENT has no affiliated
605 (let ((caption (org-export-get-caption element
)))
607 ;; Get sequence number of current src-block among every
608 ;; src-block with a caption.
610 (org-export-get-ordinal
611 element info nil
'org-e-ascii--has-caption-p
))
612 (title-fmt (org-e-ascii--translate
613 (case (org-element-type element
)
614 (table "Table %d: %s")
615 (src-block "Listing %d: %s"))
617 (org-e-ascii--fill-string
618 (format title-fmt reference
(org-export-data caption info
))
619 (org-e-ascii--current-text-width element info
) info
)))))
621 (defun org-e-ascii--build-toc (info &optional n keyword
)
622 "Return a table of contents.
624 INFO is a plist used as a communication channel.
626 Optional argument N, when non-nil, is an integer specifying the
629 Optional argument KEYWORD specifies the TOC keyword, if any, from
630 which the table of contents generation has been initiated."
631 (let ((title (org-e-ascii--translate "Table of Contents" 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
))))
642 (let* ((level (org-export-get-relative-level headline info
))
643 (indent (* (1- level
) 3)))
645 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
646 (org-e-ascii--build-title
647 headline info
(- text-width indent
) nil
648 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
649 (org-export-collect-headlines info n
) "\n")))))
651 (defun org-e-ascii--list-listings (keyword info
)
652 "Return a list of listings.
654 KEYWORD is the keyword that initiated the list of listings
655 generation. INFO is a plist used as a communication channel."
656 (let ((title (org-e-ascii--translate "List of Listings" info
)))
659 (make-string (length title
)
660 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
663 (if keyword
(org-e-ascii--current-text-width keyword info
)
664 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
665 ;; Use a counter instead of retreiving ordinal of each
670 ;; Store initial text so its length can be computed. This is
671 ;; used to properly align caption right to it in case of
672 ;; filling (like contents of a description list item).
674 (format (org-e-ascii--translate "Listing %d:" info
)
679 (org-e-ascii--indent-string
680 (org-e-ascii--fill-string
681 ;; Use short name in priority, if available.
682 (let ((caption (or (org-export-get-caption src-block t
)
683 (org-export-get-caption src-block
))))
684 (org-export-data caption info
))
685 (- text-width
(length initial-text
)) info
)
686 (length initial-text
))))))
687 (org-export-collect-listings info
) "\n")))))
689 (defun org-e-ascii--list-tables (keyword info
)
690 "Return a list of listings.
692 KEYWORD is the keyword that initiated the list of listings
693 generation. INFO is a plist used as a communication channel."
694 (let ((title (org-e-ascii--translate "List of Tables" info
)))
697 (make-string (length title
)
698 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
701 (if keyword
(org-e-ascii--current-text-width keyword info
)
702 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
703 ;; Use a counter instead of retreiving ordinal of each
708 ;; Store initial text so its length can be computed. This is
709 ;; used to properly align caption right to it in case of
710 ;; filling (like contents of a description list item).
712 (format (org-e-ascii--translate "Table %d:" info
)
717 (org-e-ascii--indent-string
718 (org-e-ascii--fill-string
719 ;; Use short name in priority, if available.
720 (let ((caption (or (org-export-get-caption table t
)
721 (org-export-get-caption table
))))
722 (org-export-data caption info
))
723 (- text-width
(length initial-text
)) info
)
724 (length initial-text
))))))
725 (org-export-collect-tables info
) "\n")))))
727 (defun org-e-ascii--unique-links (element info
)
728 "Return a list of unique link references in ELEMENT.
730 ELEMENT is either an headline element or a section element. INFO
731 is a plist used as a communication channel."
735 ;; Return LINK if it wasn't referenced so far, or nil.
736 ;; Update SEEN links along the way.
739 (cons (org-element-property :raw-link link
)
740 (org-element-contents link
))))
741 ;; Ignore LINK if it hasn't been translated already.
742 ;; It can happen if it is located in an affiliated
743 ;; keyword that was ignored.
744 (when (and (org-string-nw-p
745 (gethash link
(plist-get info
:exported-data
)))
746 (not (member footprint seen
)))
747 (push footprint seen
) link
)))))
748 ;; If at a section, find parent headline, if any, in order to
749 ;; count links that might be in the title.
751 (if (eq (org-element-type element
) 'headline
) element
752 (or (org-export-get-parent-headline element
) element
))))
753 ;; Get all links in HEADLINE.
755 headline
'link
(lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
757 (defun org-e-ascii--describe-links (links width info
)
758 "Return a string describing a list of links.
760 LINKS is a list of link type objects, as returned by
761 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
762 the output string. INFO is a plist used as a communication
766 (let ((type (org-element-property :type link
))
767 (anchor (let ((desc (org-element-contents link
)))
768 (if desc
(org-export-data desc info
)
769 (org-element-property :raw-link link
)))))
771 ;; Coderefs, radio links and fuzzy links are ignored.
772 ((member type
'("coderef" "radio" "fuzzy")) nil
)
773 ;; Id and custom-id links: Headlines refer to their numbering.
774 ((member type
'("custom-id" "id"))
775 (let ((dest (org-export-resolve-id-link link info
)))
777 (org-e-ascii--fill-string
781 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
783 (org-e-ascii--translate "See section %s" info
)
784 (mapconcat 'number-to-string
785 (org-export-get-headline-number dest info
) "."))))
786 width info
) "\n\n")))
787 ;; Do not add a link that cannot be resolved and doesn't have
788 ;; any description: destination is already visible in the
790 ((not (org-element-contents link
)) nil
)
793 (org-e-ascii--fill-string
794 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
799 (defun org-e-ascii--checkbox (item info
)
800 "Return checkbox string for ITEM or nil.
801 INFO is a plist used as a communication channel."
802 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
803 (case (org-element-property :checkbox item
)
804 (on (if utf8p
"☑ " "[X] "))
805 (off (if utf8p
"☐ " "[ ] "))
806 (trans (if utf8p
"☒ " "[-] ")))))
812 (defun org-e-ascii-template--document-title (info)
813 "Return document title, as a string.
814 INFO is a plist used as a communication channel."
815 (let* ((text-width org-e-ascii-text-width
)
816 ;; Links in the title will not be resolved later, so we make
817 ;; sure their path is located right after them.
818 (org-e-ascii-links-to-notes nil
)
819 (title (org-export-data (plist-get info
:title
) info
))
820 (author (and (plist-get info
:with-author
)
821 (let ((auth (plist-get info
:author
)))
822 (and auth
(org-export-data auth info
)))))
823 (email (and (plist-get info
:with-email
)
824 (org-export-data (plist-get info
:email
) info
)))
825 (date (and (plist-get info
:with-date
)
826 (org-export-data (plist-get info
:date
) info
))))
827 ;; There are two types of title blocks depending on the presence
828 ;; of a title to display.
829 (if (string= title
"")
830 ;; Title block without a title. DATE is positioned at the top
831 ;; right of the document, AUTHOR to the top left and EMAIL
834 ((and (org-string-nw-p date
) (org-string-nw-p author
))
837 (make-string (- text-width
(length date
) (length author
)) ?
)
839 (when (org-string-nw-p email
) (concat "\n" email
))
841 ((and (org-string-nw-p date
) (org-string-nw-p email
))
844 (make-string (- text-width
(length date
) (length email
)) ?
)
846 ((org-string-nw-p date
)
848 (org-e-ascii--justify-string date text-width
'right
)
850 ((and (org-string-nw-p author
) (org-string-nw-p email
))
851 (concat author
"\n" email
"\n\n\n"))
852 ((org-string-nw-p author
) (concat author
"\n\n\n"))
853 ((org-string-nw-p email
) (concat email
"\n\n\n")))
854 ;; Title block with a title. Document's TITLE, along with the
855 ;; AUTHOR and its EMAIL are both overlined and an underlined,
856 ;; centered. Date is just below, also centered.
857 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
858 ;; Format TITLE. It may be filled if it is too wide,
859 ;; that is wider than the two thirds of the total width.
860 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
861 (formatted-title (org-e-ascii--fill-string title title-len info
))
864 (min (+ (max title-len
(length author
) (length email
)) 2)
865 text-width
) (if utf8p ?━ ?_
))))
866 (org-e-ascii--justify-string
869 (upcase formatted-title
)
871 ((and (org-string-nw-p author
) (org-string-nw-p email
))
872 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
873 ((org-string-nw-p author
)
874 (concat (if utf8p
"\n\n\n" "\n\n") author
))
875 ((org-string-nw-p email
)
876 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
878 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
879 "\n\n\n") text-width
'center
)))))
881 (defun org-e-ascii-template (contents info
)
882 "Return complete document string after ASCII conversion.
883 CONTENTS is the transcoded contents string. INFO is a plist
884 holding export options."
885 (org-element-normalize-string
886 (org-e-ascii--indent-string
887 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
888 ;; 1. Build title block.
890 (org-e-ascii-template--document-title info
)
891 ;; 2. Table of contents.
892 (let ((depth (plist-get info
:with-toc
)))
895 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
897 ;; 3. Document's body.
899 ;; 4. Footnote definitions.
900 (let ((definitions (org-export-collect-footnote-definitions
901 (plist-get info
:parse-tree
) info
))
902 ;; Insert full links right inside the footnote definition
903 ;; as they have no chance to be inserted later.
904 (org-e-ascii-links-to-notes nil
))
908 (let ((title (org-e-ascii--translate "Footnotes" info
)))
913 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
917 (let ((id (format "[%s] " (car ref
))))
918 ;; Distinguish between inline definitions and
919 ;; full-fledged definitions.
921 (let ((def (nth 2 ref
)))
922 (if (eq (org-element-type def
) 'org-data
)
923 ;; Full-fledged definition: footnote ID is
924 ;; inserted inside the first parsed paragraph
925 ;; (FIRST), if any, to be sure filling will
926 ;; take it into consideration.
927 (let ((first (car (org-element-contents def
))))
928 (if (not (eq (org-element-type first
) 'paragraph
))
929 (concat id
"\n" (org-export-data def info
))
930 (push id
(nthcdr 2 first
))
931 (org-export-data def info
)))
932 ;; Fill paragraph once footnote ID is inserted in
933 ;; order to have a correct length for first line.
934 (org-e-ascii--fill-string
935 (concat id
(org-export-data def info
))
936 text-width info
))))))
937 definitions
"\n\n"))))
938 ;; 5. Creator. Ignore `comment' value as there are no comments in
939 ;; ASCII. Justify it to the bottom right.
940 (let ((creator-info (plist-get info
:with-creator
)))
941 (unless (or (not creator-info
) (eq creator-info
'comment
))
944 (org-e-ascii--fill-string
945 (plist-get info
:creator
) text-width info
'right
))))))
946 org-e-ascii-global-margin
)))
948 (defun org-e-ascii--translate (s info
)
949 "Translate string S according to specified language and charset.
950 INFO is a plist used as a communication channel."
951 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
952 (org-export-translate s charset info
)))
956 ;;; Transcode Functions
960 (defun org-e-ascii-bold (bold contents info
)
961 "Transcode BOLD from Org to ASCII.
962 CONTENTS is the text with bold markup. INFO is a plist holding
963 contextual information."
964 (format "*%s*" contents
))
969 (defun org-e-ascii-center-block (center-block contents info
)
970 "Transcode a CENTER-BLOCK element from Org to ASCII.
971 CONTENTS holds the contents of the block. INFO is a plist
972 holding contextual information."
973 (org-e-ascii--justify-string
974 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
979 (defun org-e-ascii-clock (clock contents info
)
980 "Transcode a CLOCK object from Org to ASCII.
981 CONTENTS is nil. INFO is a plist holding contextual
983 (concat org-clock-string
" "
985 (org-element-property :raw-value
986 (org-element-property :value clock
)))
987 (let ((time (org-element-property :duration clock
)))
992 (org-split-string time
":")))))))
997 (defun org-e-ascii-code (code contents info
)
998 "Return a CODE object from Org to ASCII.
999 CONTENTS is nil. INFO is a plist holding contextual
1001 (format org-e-ascii-verbatim-format
(org-element-property :value code
)))
1006 (defun org-e-ascii-drawer (drawer contents info
)
1007 "Transcode a DRAWER element from Org to ASCII.
1008 CONTENTS holds the contents of the block. INFO is a plist
1009 holding contextual information."
1010 (let ((name (org-element-property :drawer-name drawer
))
1011 (width (org-e-ascii--current-text-width drawer info
)))
1012 (if (functionp org-e-ascii-format-drawer-function
)
1013 (funcall org-e-ascii-format-drawer-function name contents width
)
1014 ;; If there's no user defined function: simply
1015 ;; display contents of the drawer.
1021 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1022 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1023 CONTENTS holds the contents of the block. INFO is a plist
1024 holding contextual information."
1030 (defun org-e-ascii-entity (entity contents info
)
1031 "Transcode an ENTITY object from Org to ASCII.
1032 CONTENTS are the definition itself. INFO is a plist holding
1033 contextual information."
1034 (org-element-property
1035 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1041 (defun org-e-ascii-example-block (example-block contents info
)
1042 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1043 CONTENTS is nil. INFO is a plist holding contextual information."
1044 (org-e-ascii--box-string
1045 (org-export-format-code-default example-block info
) info
))
1050 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1051 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1052 CONTENTS is nil. INFO is a plist holding contextual information."
1053 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1054 (org-element-property :value export-snippet
)))
1059 (defun org-e-ascii-export-block (export-block contents info
)
1060 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1061 CONTENTS is nil. INFO is a plist holding contextual information."
1062 (when (string= (org-element-property :type export-block
) "ASCII")
1063 (org-remove-indentation (org-element-property :value export-block
))))
1068 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1069 "Transcode a FIXED-WIDTH element from Org to ASCII.
1070 CONTENTS is nil. INFO is a plist holding contextual information."
1071 (org-e-ascii--box-string
1072 (org-remove-indentation
1073 (org-element-property :value fixed-width
)) info
))
1076 ;;;; Footnote Definition
1078 ;; Footnote Definitions are ignored. They are compiled at the end of
1079 ;; the document, by `org-e-ascii-template'.
1082 ;;;; Footnote Reference
1084 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1085 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1086 CONTENTS is nil. INFO is a plist holding contextual information."
1087 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1092 (defun org-e-ascii-headline (headline contents info
)
1093 "Transcode an HEADLINE element from Org to ASCII.
1094 CONTENTS holds the contents of the headline. INFO is a plist
1095 holding contextual information."
1096 ;; Don't export footnote section, which will be handled at the end
1098 (unless (org-element-property :footnote-section-p headline
)
1099 (let* ((low-level-rank (org-export-low-level-p headline info
))
1100 (width (org-e-ascii--current-text-width headline info
))
1101 ;; Blank lines between headline and its contents.
1102 ;; `org-e-ascii-headline-spacing', when set, overwrites
1103 ;; original buffer's spacing.
1106 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1107 (org-element-property :pre-blank headline
)) ?
\n))
1108 ;; Even if HEADLINE has no section, there might be some
1109 ;; links in its title that we shouldn't forget to describe.
1111 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1112 (let ((title (org-element-property :title headline
)))
1114 (org-e-ascii--describe-links
1115 (org-e-ascii--unique-links title info
) width info
))))))
1116 ;; Deep subtree: export it as a list item.
1120 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1121 org-e-ascii-bullets
))))
1123 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1126 (org-e-ascii--build-title headline info width
) "\n"
1127 ;; Contents, indented by length of bullet.
1129 (org-e-ascii--indent-string
1131 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1133 ;; Else: Standard headline.
1135 (org-e-ascii--build-title headline info width
'underline
)
1137 (concat (when (org-string-nw-p links
) links
) contents
))))))
1140 ;;;; Horizontal Rule
1142 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1143 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1144 CONTENTS is nil. INFO is a plist holding contextual
1146 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info
))
1148 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1149 (org-e-ascii--justify-string
1150 (make-string (if (wholenump spec-width
) spec-width text-width
)
1151 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1152 text-width
'center
)))
1155 ;;;; Inline Src Block
1157 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1158 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1159 CONTENTS holds the contents of the item. INFO is a plist holding
1160 contextual information."
1161 (format org-e-ascii-verbatim-format
1162 (org-element-property :value inline-src-block
)))
1167 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1168 "Transcode an INLINETASK element from Org to ASCII.
1169 CONTENTS holds the contents of the block. INFO is a plist
1170 holding contextual information."
1171 (let ((width (org-e-ascii--current-text-width inlinetask info
)))
1172 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1173 ;; with appropriate arguments.
1174 (if (functionp org-e-ascii-format-inlinetask-function
)
1175 (funcall org-e-ascii-format-inlinetask-function
1177 (and (plist-get info
:with-todo-keywords
)
1178 (let ((todo (org-element-property
1179 :todo-keyword inlinetask
)))
1180 (and todo
(org-export-data todo info
))))
1182 (org-element-property :todo-type inlinetask
)
1184 (and (plist-get info
:with-priority
)
1185 (org-element-property :priority inlinetask
))
1187 (org-export-data (org-element-property :title inlinetask
) info
)
1189 (and (plist-get info
:with-tags
)
1190 (org-element-property :tags inlinetask
))
1191 ;; contents and width
1193 ;; Otherwise, use a default template.
1194 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1195 (org-e-ascii--indent-string
1197 ;; Top line, with an additional blank line if not in UTF-8.
1198 (make-string width
(if utf8p ?━ ?_
)) "\n"
1199 (unless utf8p
(concat (make-string width ?
) "\n"))
1200 ;; Add title. Fill it if wider than inlinetask.
1201 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1202 (if (<= (length title
) width
) title
1203 (org-e-ascii--fill-string title width info
)))
1205 ;; If CONTENTS is not empty, insert it along with
1207 (when (org-string-nw-p contents
)
1208 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1210 (make-string width
(if utf8p ?━ ?_
)))
1211 ;; Flush the inlinetask to the right.
1212 (- org-e-ascii-text-width org-e-ascii-global-margin
1213 (if (not (org-export-get-parent-headline inlinetask
)) 0
1214 org-e-ascii-inner-margin
)
1215 (org-e-ascii--current-text-width inlinetask info
)))))))
1220 (defun org-e-ascii-italic (italic contents info
)
1221 "Transcode italic from Org to ASCII.
1222 CONTENTS is the text with italic markup. INFO is a plist holding
1223 contextual information."
1224 (format "/%s/" contents
))
1229 (defun org-e-ascii-item (item contents info
)
1230 "Transcode an ITEM element from Org to ASCII.
1231 CONTENTS holds the contents of the item. INFO is a plist holding
1232 contextual information."
1233 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1234 (checkbox (org-e-ascii--checkbox item info
))
1235 (list-type (org-element-property :type
(org-export-get-parent item
)))
1237 ;; First parent of ITEM is always the plain-list. Get
1238 ;; `:type' property from it.
1239 (org-list-bullet-string
1243 (org-export-data (org-element-property :tag item
) info
)
1246 ;; Return correct number for ITEM, paying attention to
1248 (let* ((struct (org-element-property :structure item
))
1249 (bul (org-element-property :bullet item
))
1250 (num (number-to-string
1251 (car (last (org-list-get-item-number
1252 (org-element-property :begin item
)
1254 (org-list-prevs-alist struct
)
1255 (org-list-parents-alist struct
)))))))
1256 (replace-regexp-in-string "[0-9]+" num bul
)))
1257 (t (let ((bul (org-element-property :bullet item
)))
1258 ;; Change bullets into more visible form if UTF-8 is active.
1260 (replace-regexp-in-string
1262 (replace-regexp-in-string
1264 (replace-regexp-in-string "*" "‣" bul
))))))))))
1267 (unless (eq list-type
'descriptive
) checkbox
)
1268 ;; Contents: Pay attention to indentation. Note: check-boxes are
1269 ;; already taken care of at the paragraph level so they don't
1270 ;; interfere with indentation.
1271 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1272 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1274 (concat "\n" contents
))))))
1279 (defun org-e-ascii-keyword (keyword contents info
)
1280 "Transcode a KEYWORD element from Org to ASCII.
1281 CONTENTS is nil. INFO is a plist holding contextual
1283 (let ((key (org-element-property :key keyword
))
1284 (value (org-element-property :value keyword
)))
1286 ((string= key
"ASCII") value
)
1287 ((string= key
"TOC")
1288 (let ((value (downcase value
)))
1290 ((string-match "\\<headlines\\>" value
)
1291 (let ((depth (or (and (string-match "[0-9]+" value
)
1292 (string-to-number (match-string 0 value
)))
1293 (plist-get info
:with-toc
))))
1294 (org-e-ascii--build-toc
1295 info
(and (wholenump depth
) depth
) keyword
)))
1296 ((string= "tables" value
)
1297 (org-e-ascii--list-tables keyword info
))
1298 ((string= "listings" value
)
1299 (org-e-ascii--list-listings keyword info
))))))))
1302 ;;;; Latex Environment
1304 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1305 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1306 CONTENTS is nil. INFO is a plist holding contextual
1308 (org-remove-indentation (org-element-property :value latex-environment
)))
1313 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1314 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1315 CONTENTS is nil. INFO is a plist holding contextual
1317 (org-element-property :value latex-fragment
))
1322 (defun org-e-ascii-line-break (line-break contents info
)
1323 "Transcode a LINE-BREAK object from Org to ASCII.
1324 CONTENTS is nil. INFO is a plist holding contextual
1325 information." hard-newline
)
1330 (defun org-e-ascii-link (link desc info
)
1331 "Transcode a LINK object from Org to ASCII.
1333 DESC is the description part of the link, or the empty string.
1334 INFO is a plist holding contextual information."
1335 (let ((raw-link (org-element-property :raw-link link
))
1336 (type (org-element-property :type link
)))
1338 ((string= type
"coderef")
1339 (let ((ref (org-element-property :path link
)))
1340 (format (org-export-get-coderef-format ref desc
)
1341 (org-export-resolve-coderef ref info
))))
1342 ;; Do not apply a special syntax on radio links. Though, use
1343 ;; transcoded target's contents as output.
1344 ((string= type
"radio")
1345 (let ((destination (org-export-resolve-radio-link link info
)))
1347 (org-export-data (org-element-contents destination
) info
))))
1348 ;; Do not apply a special syntax on fuzzy links pointing to
1350 ((string= type
"fuzzy")
1351 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1352 ;; Ignore invisible "#+TARGET: path".
1353 (unless (eq (org-element-type destination
) 'keyword
)
1354 (if (org-string-nw-p desc
) desc
1357 (org-export-get-ordinal
1358 destination info nil
'org-e-ascii--has-caption-p
)))
1360 (if (atom number
) (number-to-string number
)
1361 (mapconcat 'number-to-string number
".")))))))))
1363 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1365 (format "[%s]" desc
)
1366 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1371 (defun org-e-ascii-paragraph (paragraph contents info
)
1372 "Transcode a PARAGRAPH element from Org to ASCII.
1373 CONTENTS is the contents of the paragraph, as a string. INFO is
1374 the plist used as a communication channel."
1375 (let ((contents (if (not (wholenump org-e-ascii-indented-line-width
)) contents
1377 (make-string org-e-ascii-indented-line-width ?
)
1378 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1379 (org-e-ascii--fill-string
1380 contents
(org-e-ascii--current-text-width paragraph info
) info
)))
1385 (defun org-e-ascii-plain-list (plain-list contents info
)
1386 "Transcode a PLAIN-LIST element from Org to ASCII.
1387 CONTENTS is the contents of the list. INFO is a plist holding
1388 contextual information."
1394 (defun org-e-ascii-plain-text (text info
)
1395 "Transcode a TEXT string from Org to ASCII.
1396 INFO is a plist used as a communication channel."
1397 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1398 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1399 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1400 (if (not (plist-get info
:with-special-strings
)) text
1401 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1402 (if (not utf8p
) text
1403 ;; Usual replacements in utf-8 with proper option set.
1404 (replace-regexp-in-string
1406 (replace-regexp-in-string
1408 (replace-regexp-in-string "---" "—" text
)))))))
1413 (defun org-e-ascii-planning (planning contents info
)
1414 "Transcode a PLANNING element from Org to ASCII.
1415 CONTENTS is nil. INFO is a plist used as a communication
1420 (list (let ((closed (org-element-property :closed planning
)))
1422 (concat org-closed-string
" "
1424 (org-element-property :raw-value closed
)))))
1425 (let ((deadline (org-element-property :deadline planning
)))
1427 (concat org-deadline-string
" "
1429 (org-element-property :raw-value deadline
)))))
1430 (let ((scheduled (org-element-property :scheduled planning
)))
1432 (concat org-scheduled-string
" "
1434 (org-element-property :raw-value scheduled
)))))))
1440 (defun org-e-ascii-quote-block (quote-block contents info
)
1441 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1442 CONTENTS holds the contents of the block. INFO is a plist
1443 holding contextual information."
1444 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1445 (org-e-ascii--indent-string
1446 (org-remove-indentation
1447 (org-e-ascii--fill-string contents width info
))
1448 org-e-ascii-quote-margin
)))
1453 (defun org-e-ascii-quote-section (quote-section contents info
)
1454 "Transcode a QUOTE-SECTION element from Org to ASCII.
1455 CONTENTS is nil. INFO is a plist holding contextual information."
1456 (let ((width (org-e-ascii--current-text-width quote-section info
))
1459 (org-remove-indentation (org-element-property :value quote-section
))
1461 (org-e-ascii--indent-string
1463 (+ org-e-ascii-quote-margin
1464 ;; Don't apply inner margin if parent headline is low level.
1465 (let ((headline (org-export-get-parent-headline quote-section
)))
1466 (if (org-export-low-level-p headline info
) 0
1467 org-e-ascii-inner-margin
))))))
1472 (defun org-e-ascii-radio-target (radio-target contents info
)
1473 "Transcode a RADIO-TARGET object from Org to ASCII.
1474 CONTENTS is the contents of the target. INFO is a plist holding
1475 contextual information."
1481 (defun org-e-ascii-section (section contents info
)
1482 "Transcode a SECTION element from Org to ASCII.
1483 CONTENTS is the contents of the section. INFO is a plist holding
1484 contextual information."
1485 (org-e-ascii--indent-string
1488 (when org-e-ascii-links-to-notes
1489 ;; Add list of links at the end of SECTION.
1490 (let ((links (org-e-ascii--describe-links
1491 (org-e-ascii--unique-links section info
)
1492 (org-e-ascii--current-text-width section info
) info
)))
1493 ;; Separate list of links and section contents.
1494 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1495 ;; Do not apply inner margin if parent headline is low level.
1496 (let ((headline (org-export-get-parent-headline section
)))
1497 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1498 org-e-ascii-inner-margin
))))
1503 (defun org-e-ascii-special-block (special-block contents info
)
1504 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1505 CONTENTS holds the contents of the block. INFO is a plist
1506 holding contextual information."
1512 (defun org-e-ascii-src-block (src-block contents info
)
1513 "Transcode a SRC-BLOCK element from Org to ASCII.
1514 CONTENTS holds the contents of the item. INFO is a plist holding
1515 contextual information."
1516 (let ((caption (org-e-ascii--build-caption src-block info
)))
1518 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1519 (org-e-ascii--box-string
1520 (org-export-format-code-default src-block info
) info
)
1521 (when (and caption
(not org-e-ascii-caption-above
))
1522 (concat "\n" caption
)))))
1525 ;;;; Statistics Cookie
1527 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1528 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1529 CONTENTS is nil. INFO is a plist holding contextual information."
1530 (org-element-property :value statistics-cookie
))
1535 (defun org-e-ascii-subscript (subscript contents info
)
1536 "Transcode a SUBSCRIPT object from Org to ASCII.
1537 CONTENTS is the contents of the object. INFO is a plist holding
1538 contextual information."
1539 (if (org-element-property :use-brackets-p subscript
)
1540 (format "_{%s}" contents
)
1541 (format "_%s" contents
)))
1546 (defun org-e-ascii-superscript (superscript contents info
)
1547 "Transcode a SUPERSCRIPT object from Org to ASCII.
1548 CONTENTS is the contents of the object. INFO is a plist holding
1549 contextual information."
1550 (if (org-element-property :use-brackets-p superscript
)
1551 (format "_{%s}" contents
)
1552 (format "_%s" contents
)))
1557 (defun org-e-ascii-strike-through (strike-through contents info
)
1558 "Transcode STRIKE-THROUGH from Org to ASCII.
1559 CONTENTS is text with strike-through markup. INFO is a plist
1560 holding contextual information."
1561 (format "+%s+" contents
))
1566 (defun org-e-ascii-table (table contents info
)
1567 "Transcode a TABLE element from Org to ASCII.
1568 CONTENTS is the contents of the table. INFO is a plist holding
1569 contextual information."
1570 (let ((caption (org-e-ascii--build-caption table info
)))
1572 ;; Possibly add a caption string above.
1573 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1574 ;; Insert table. Note: "table.el" tables are left unmodified.
1575 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1576 ((and org-e-ascii-table-use-ascii-art
1577 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1578 (require 'ascii-art-to-unicode nil t
))
1580 (insert (org-remove-indentation
1581 (org-element-property :value table
)))
1582 (goto-char (point-min))
1584 (goto-char (point-max))
1585 (skip-chars-backward " \r\t\n")
1586 (buffer-substring (point-min) (point))))
1587 (t (org-remove-indentation (org-element-property :value table
))))
1588 ;; Possible add a caption string below.
1589 (when (and caption
(not org-e-ascii-caption-above
))
1590 (concat "\n" caption
)))))
1595 (defun org-e-ascii--table-cell-width (table-cell info
)
1596 "Return width of TABLE-CELL.
1598 INFO is a plist used as a communication channel.
1600 Width of a cell is determined either by a width cookie in the
1601 same column as the cell, or by the maximum cell's length in that
1604 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1606 (or (and (not org-e-ascii-table-widen-columns
)
1607 (org-export-table-cell-width table-cell info
))
1608 (let* ((max-width 0)
1609 (table (org-export-get-parent-table table-cell
))
1610 (specialp (org-export-table-has-special-column-p table
))
1611 (col (cdr (org-export-table-cell-address table-cell info
))))
1618 (org-element-contents
1619 (elt (if specialp
(cdr (org-element-contents row
))
1620 (org-element-contents row
))
1627 (defun org-e-ascii-table-cell (table-cell contents info
)
1628 "Transcode a TABLE-CELL object from Org to ASCII.
1629 CONTENTS is the cell contents. INFO is a plist used as
1630 a communication channel."
1631 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1632 ;; is nil and some width cookie has set it, use that value.
1633 ;; Otherwise, compute the maximum width among transcoded data of
1634 ;; each cell in the column.
1635 (let ((width (org-e-ascii--table-cell-width table-cell info
)))
1636 ;; When contents are too large, truncate them.
1637 (unless (or org-e-ascii-table-widen-columns
(<= (length contents
) width
))
1638 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1639 ;; Align contents correctly within the cell.
1640 (let* ((indent-tabs-mode nil
)
1643 (org-e-ascii--justify-string
1645 (org-export-table-cell-alignment table-cell info
)))))
1646 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1648 (concat (format " %s " contents
)
1649 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1650 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1655 (defun org-e-ascii-table-row (table-row contents info
)
1656 "Transcode a TABLE-ROW element from Org to ASCII.
1657 CONTENTS is the row contents. INFO is a plist used as
1658 a communication channel."
1659 (when (eq (org-element-property :type table-row
) 'standard
)
1662 (lambda (lcorner horiz vert rcorner
)
1667 table-row
'table-cell
1669 (let ((width (org-e-ascii--table-cell-width cell info
))
1670 (borders (org-export-table-cell-borders cell info
)))
1672 ;; In order to know if CELL starts the row, do
1673 ;; not compare it with the first cell in the row
1674 ;; as there might be a special column. Instead,
1675 ;; compare it with the first exportable cell,
1676 ;; obtained with `org-element-map'.
1677 (when (and (memq 'left borders
)
1678 (eq (org-element-map
1679 table-row
'table-cell
'identity info t
)
1682 (make-string (+ 2 width
) (string-to-char horiz
))
1684 ((not (memq 'right borders
)) nil
)
1685 ((eq (car (last (org-element-contents table-row
))) cell
)
1689 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1690 (borders (org-export-table-cell-borders
1691 (org-element-map table-row
'table-cell
'identity info t
)
1694 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1695 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1696 (funcall build-hline
"+" "-" "+" "+")))
1697 ((memq 'above borders
)
1698 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1699 (funcall build-hline
"+" "-" "+" "+"))))
1700 (when (memq 'left borders
) (if utf8p
"│" "|"))
1702 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1703 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1704 (funcall build-hline
"+" "-" "+" "+")))))))
1709 (defun org-e-ascii-timestamp (timestamp contents info
)
1710 "Transcode a TIMESTAMP object from Org to ASCII.
1711 CONTENTS is nil. INFO is a plist holding contextual information."
1712 (org-e-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1717 (defun org-e-ascii-underline (underline contents info
)
1718 "Transcode UNDERLINE from Org to ASCII.
1719 CONTENTS is the text with underline markup. INFO is a plist
1720 holding contextual information."
1721 (format "_%s_" contents
))
1726 (defun org-e-ascii-verbatim (verbatim contents info
)
1727 "Return a VERBATIM object from Org to ASCII.
1728 CONTENTS is nil. INFO is a plist holding contextual information."
1729 (format org-e-ascii-verbatim-format
1730 (org-element-property :value verbatim
)))
1735 (defun org-e-ascii-verse-block (verse-block contents info
)
1736 "Transcode a VERSE-BLOCK element from Org to ASCII.
1737 CONTENTS is verse block contents. INFO is a plist holding
1738 contextual information."
1739 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1740 (org-e-ascii--indent-string
1741 (org-e-ascii--justify-string contents verse-width
'left
)
1742 org-e-ascii-quote-margin
)))
1748 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1749 "Filter controlling number of blank lines after an headline.
1751 HEADLINE is a string representing a transcoded headline.
1752 BACK-END is symbol specifying back-end used for export. INFO is
1753 plist containing the communication channel.
1755 This function only applies to `e-ascii' back-end. See
1756 `org-e-ascii-headline-spacing' for information."
1757 (if (not org-e-ascii-headline-spacing
) headline
1758 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1759 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1761 (defun org-e-ascii-filter-paragraph-spacing (tree back-end info
)
1762 "Filter controlling number of blank lines between paragraphs.
1764 TREE is the parse tree. BACK-END is the symbol specifying
1765 back-end used for export. INFO is a plist used as
1766 a communication channel.
1768 This function only applies to `e-ascii' back-end. See
1769 `org-e-ascii-paragraph-spacing' for information."
1770 (when (wholenump org-e-ascii-paragraph-spacing
)
1774 (when (eq (org-element-type (org-export-get-next-element p info
))
1776 (org-element-put-property
1777 p
:post-blank org-e-ascii-paragraph-spacing
)))))
1782 ;;; End-user functions
1785 (defun org-e-ascii-export-as-ascii
1786 (&optional async subtreep visible-only body-only ext-plist
)
1787 "Export current buffer to a text buffer.
1789 If narrowing is active in the current buffer, only export its
1792 If a region is active, export that region.
1794 A non-nil optional argument ASYNC means the process should happen
1795 asynchronously. The resulting buffer should be accessible
1796 through the `org-export-stack' interface.
1798 When optional argument SUBTREEP is non-nil, export the sub-tree
1799 at point, extracting information from the headline properties
1802 When optional argument VISIBLE-ONLY is non-nil, don't export
1803 contents of hidden elements.
1805 When optional argument BODY-ONLY is non-nil, strip title, table
1806 of contents and footnote definitions from output.
1808 EXT-PLIST, when provided, is a property list with external
1809 parameters overriding Org default settings, but still inferior to
1810 file-local settings.
1812 Export is done in a buffer named \"*Org E-ASCII Export*\", which
1813 will be displayed when `org-export-show-temporary-export-buffer'
1817 (org-export-async-start
1819 (with-current-buffer (get-buffer-create "*Org E-ASCII Export*")
1822 (goto-char (point-min))
1824 (org-export-add-to-stack (current-buffer) 'e-ascii
)))
1825 `(org-export-as 'e-ascii
,subtreep
,visible-only
,body-only
1827 (let ((outbuf (org-export-to-buffer
1828 'e-ascii
"*Org E-ASCII Export*"
1829 subtreep visible-only body-only ext-plist
)))
1830 (with-current-buffer outbuf
(text-mode))
1831 (when org-export-show-temporary-export-buffer
1832 (switch-to-buffer-other-window outbuf
)))))
1835 (defun org-e-ascii-export-to-ascii
1836 (&optional async subtreep visible-only body-only ext-plist
)
1837 "Export current buffer to a text file.
1839 If narrowing is active in the current buffer, only export its
1842 If a region is active, export that region.
1844 A non-nil optional argument ASYNC means the process should happen
1845 asynchronously. The resulting file should be accessible through
1846 the `org-export-stack' interface.
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 Return output file's name."
1864 (let ((outfile (org-export-output-file-name ".txt" subtreep
)))
1866 (org-export-async-start
1867 (lambda (f) (org-export-add-to-stack f
'e-ascii
))
1870 'e-ascii
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
1872 'e-ascii outfile subtreep visible-only body-only ext-plist
))))
1875 (defun org-e-ascii-publish-to-ascii (plist filename pub-dir
)
1876 "Publish an Org file to ASCII.
1878 FILENAME is the filename of the Org file to be published. PLIST
1879 is the property list for the given project. PUB-DIR is the
1880 publishing directory.
1882 Return output file name."
1883 (org-e-publish-org-to
1884 'e-ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1887 (defun org-e-ascii-publish-to-latin1 (plist filename pub-dir
)
1888 "Publish an Org file to Latin-1.
1890 FILENAME is the filename of the Org file to be published. PLIST
1891 is the property list for the given project. PUB-DIR is the
1892 publishing directory.
1894 Return output file name."
1895 (org-e-publish-org-to
1896 'e-ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1899 (defun org-e-ascii-publish-to-utf8 (plist filename pub-dir
)
1900 "Publish an org file to UTF-8.
1902 FILENAME is the filename of the Org file to be published. PLIST
1903 is the property list for the given project. PUB-DIR is the
1904 publishing directory.
1906 Return output file name."
1907 (org-e-publish-org-to
1908 'e-ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1911 (provide 'org-e-ascii
)
1912 ;;; org-e-ascii.el ends here