Export back-ends: add entry in `org-export-dispatch' when loaded
[org-mode.git] / contrib / lisp / org-e-ascii.el
blob1a55a0fc1075fe585a3011c8bdf699019ef031c6
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/>.
21 ;;; Commentary:
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.
36 ;;; Code:
38 (eval-when-compile (require 'cl))
39 (require 'org-export)
41 (declare-function aa2u "ext:ascii-art-to-unicode" ())
43 ;;; Define Back-End
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
49 ;; mechanism.
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"
103 :menu-entry
104 (?t "Export to Plain Text"
105 ((?A "As ASCII buffer"
106 (lambda (s v b)
107 (org-e-ascii-export-as-ascii s v b '(:ascii-charset ascii))))
108 (?a "As ASCII file"
109 (lambda (s v b)
110 (org-e-ascii-export-to-ascii s v b '(:ascii-charset ascii))))
111 (?L "As Latin1 buffer"
112 (lambda (s v b)
113 (org-e-ascii-export-as-ascii s v b '(:ascii-charset latin1))))
114 (?l "As Latin1 file"
115 (lambda (s v b)
116 (org-e-ascii-export-to-ascii s v b '(:ascii-charset latin1))))
117 (?U "As UTF-8 buffer"
118 (lambda (s v b)
119 (org-e-ascii-export-as-ascii s v b '(:ascii-charset utf-8))))
120 (?u "As UTF-8 file"
121 (lambda (s v b)
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"
134 :group 'org-export)
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
141 :type 'integer)
143 (defcustom org-e-ascii-global-margin 0
144 "Width of the left margin, in number of characters."
145 :group 'org-export-e-ascii
146 :type 'integer)
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
152 :type 'integer)
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
158 :type 'integer)
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
164 :type 'integer)
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
172 contents.
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
177 :type '(choice
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.
185 Possible values are:
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
190 :type '(choice
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 ?= ?~ ?-)
196 (latin1 ?= ?~ ?-)
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
208 :type '(list
209 (cons :tag "Underline characters sequence"
210 (const :tag "ASCII charset" ascii)
211 (repeat character))
212 (cons :tag "Underline characters sequence"
213 (const :tag "Latin-1 charset" latin1)
214 (repeat character))
215 (cons :tag "Underline characters sequence"
216 (const :tag "UTF-8 charset" utf-8)
217 (repeat character))))
219 (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
220 (latin1 ?§ ?¶)
221 (utf-8 ?◊))
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
232 representation."
233 :group 'org-export-e-ascii
234 :type '(list
235 (cons :tag "Bullet characters for low level headlines"
236 (const :tag "ASCII charset" ascii)
237 (repeat character))
238 (cons :tag "Bullet characters for low level headlines"
239 (const :tag "Latin-1 charset" latin1)
240 (repeat character))
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
250 :type 'boolean)
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
257 :type 'boolean)
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
264 :type 'boolean)
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
279 :type 'boolean)
281 (defcustom org-e-ascii-verbatim-format "`%s'"
282 "Format string used for verbatim text and inline code."
283 :group 'org-export-e-ascii
284 :type 'string)
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.\"
302 contents\)"
303 :group 'org-export-e-ascii
304 :type 'function)
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
329 \(concat
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\)\)\)
337 \"\\n\"
338 ;; If CONTENTS is not empty, insert it along with
339 ;; a separator.
340 \(when \(org-string-nw-p contents\)
341 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
342 ;; Bottom line.
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
350 :type 'function)
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)
400 (t (with-temp-buffer
401 (let ((fill-column text-width)
402 (use-hard-newlines t))
403 (insert s)
404 (fill-region (point-min) (point-max) justify))
405 (buffer-string)))))
407 (defun org-e-ascii--justify-string (s text-width how)
408 "Justify string S.
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'."
412 (with-temp-buffer
413 (insert s)
414 (goto-char (point-min))
415 (let ((fill-column text-width))
416 (while (< (point) (point-max))
417 (justify-current-line how)
418 (forward-line)))
419 (buffer-string)))
421 (defun org-e-ascii--indent-string (s width)
422 "Indent string S by WIDTH white spaces.
423 Empty lines are not indented."
424 (when (stringp s)
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)
444 ('headline
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
449 ;; TOTAL-WIDTH.
450 (otherwise
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.
454 (total-width
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
463 ;; headline.
464 (if (not parent) 0
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))))))))
472 (- total-width
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))
478 count parent)
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.
484 (let ((parent-item
485 (if (eq (org-element-type element) 'item) element
486 (loop for parent in genealogy
487 when (eq (org-element-type parent) 'item)
488 return parent))))
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)
499 (org-list-get-ind
500 (org-list-get-top-point struct) struct))
501 (length (org-e-ascii--checkbox parent-item info))
502 (length
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'
516 specifications.
518 if optional argument NOTAGS is nil, no tags will be added to the
519 title."
520 (let* ((headlinep (eq (org-element-type element) 'headline))
521 (numbers
522 ;; Numbering is specific to headlines.
523 (and headlinep (org-export-numbered-headline-p element info)
524 ;; All tests passed: build numbering string.
525 (concat
526 (mapconcat
527 'number-to-string
528 (org-export-get-headline-number element info) ".")
529 " ")))
530 (text (org-trim
531 (org-export-data (org-element-property :title element) info)))
532 (todo
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)))
539 (and tag-list
540 (format ":%s:"
541 (mapconcat 'identity tag-list ":"))))))
542 (priority
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)))
547 (concat
548 first-part
549 ;; Align tags, if any.
550 (when tags
551 (format
552 (format " %%%ds"
553 (max (- text-width (1+ (length first-part))) (length tags)))
554 tags))
555 ;; Maybe underline text, if ELEMENT type is `headline' and an
556 ;; underline character has been defined.
557 (when (and underline headlinep)
558 (let ((under-char
559 (nth (1- (org-export-get-relative-level element info))
560 (cdr (assq (plist-get info :ascii-charset)
561 org-e-ascii-underline)))))
562 (and under-char
563 (concat "\n"
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
579 caption keyword."
580 (let ((caption (org-element-property :caption element)))
581 (when caption
582 ;; Get sequence number of current src-block among every
583 ;; src-block with a caption.
584 (let ((reference
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"))
591 info)))
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
602 depth of the table.
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)))
607 (concat
608 title "\n"
609 (make-string (length title)
610 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
611 "\n\n"
612 (let ((text-width
613 (if keyword (org-e-ascii--current-text-width keyword info)
614 (- org-e-ascii-text-width org-e-ascii-global-margin))))
615 (mapconcat
616 (lambda (headline)
617 (let* ((level (org-export-get-relative-level headline info))
618 (indent (* (1- level) 3)))
619 (concat
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)))
632 (concat
633 title "\n"
634 (make-string (length title)
635 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
636 "\n\n"
637 (let ((text-width
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
641 ;; src-block.
642 (count 0))
643 (mapconcat
644 (lambda (src-block)
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).
648 (let ((initial-text
649 (format (org-e-ascii--translate "Listing %d:" info)
650 (incf count))))
651 (concat
652 initial-text " "
653 (org-trim
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)))
669 (concat
670 title "\n"
671 (make-string (length title)
672 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
673 "\n\n"
674 (let ((text-width
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
678 ;; src-block.
679 (count 0))
680 (mapconcat
681 (lambda (table)
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).
685 (let ((initial-text
686 (format (org-e-ascii--translate "Table %d:" info)
687 (incf count))))
688 (concat
689 initial-text " "
690 (org-trim
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."
705 (let* (seen
706 (unique-link-p
707 (function
708 ;; Return LINK if it wasn't referenced so far, or nil.
709 ;; Update SEEN links along the way.
710 (lambda (link)
711 (let ((footprint
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.
718 (headline
719 (if (eq (org-element-type element) 'headline) element
720 (or (org-export-get-parent-headline element) element))))
721 ;; Get all links in HEADLINE.
722 (org-element-map
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
731 channel."
732 (mapconcat
733 (lambda (link)
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)))))
738 (cond
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)))
744 (concat
745 (org-e-ascii--fill-string
746 (format
747 "[%s] %s"
748 anchor
749 (if (not dest) (org-e-ascii--translate "Unknown reference" info)
750 (format
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
757 ;; paragraph.
758 ((not (org-element-contents link)) nil)
760 (concat
761 (org-e-ascii--fill-string
762 (format "[%s] %s" anchor (org-element-property :raw-link link))
763 width info)
764 "\n\n")))))
765 links ""))
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 "☒ " "[-] ")))))
778 ;;; Template
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
796 ;; just below.
797 (cond
798 ((and (org-string-nw-p date) (org-string-nw-p author))
799 (concat
800 author
801 (make-string (- text-width (length date) (length author)) ? )
802 date
803 (when (org-string-nw-p email) (concat "\n" email))
804 "\n\n\n"))
805 ((and (org-string-nw-p date) (org-string-nw-p email))
806 (concat
807 email
808 (make-string (- text-width (length date) (length email)) ? )
809 date "\n\n\n"))
810 ((org-string-nw-p date)
811 (concat
812 (org-e-ascii--justify-string date text-width 'right)
813 "\n\n\n"))
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))
826 (line
827 (make-string
828 (min (+ (max title-len (length author) (length email)) 2)
829 text-width) (if utf8p ?━ ?_))))
830 (org-e-ascii--justify-string
831 (concat line "\n"
832 (unless utf8p "\n")
833 (upcase formatted-title)
834 (cond
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)))
841 "\n" line
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.
853 (concat
854 (org-e-ascii-template--document-title info)
855 ;; 2. Table of contents.
856 (let ((depth (plist-get info :with-toc)))
857 (when depth
858 (concat
859 (org-e-ascii--build-toc info (and (wholenump depth) depth))
860 "\n\n\n")))
861 ;; 3. Document's body.
862 contents
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))
869 (when definitions
870 (concat
871 "\n\n\n"
872 (let ((title (org-e-ascii--translate "Footnotes" info)))
873 (concat
874 title "\n"
875 (make-string
876 (length title)
877 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
878 "\n\n"
879 (mapconcat
880 (lambda (ref)
881 (let ((id (format "[%s] " (car ref))))
882 ;; Distinguish between inline definitions and
883 ;; full-fledged definitions.
884 (org-trim
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))
906 (concat
907 "\n\n\n"
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
922 ;;;; Babel Call
924 ;; Babel Calls are ignored.
927 ;;;; Bold
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))
936 ;;;; Center Block
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))
946 ;;;; Clock
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
951 information."
952 (concat org-clock-string " "
953 (org-translate-time (org-element-property :value clock))
954 (let ((time (org-element-property :time clock)))
955 (and time
956 (concat " => "
957 (apply 'format
958 "%2s:%02s"
959 (org-split-string time ":")))))))
962 ;;;; Code
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
967 information."
968 (format org-e-ascii-verbatim-format (org-element-property :value code)))
971 ;;;; Comment
973 ;; Comments are ignored.
976 ;;;; Comment Block
978 ;; Comment Blocks are ignored.
981 ;;;; Drawer
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.
993 contents)))
996 ;;;; Dynamic Block
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."
1002 contents)
1005 ;;;; Entity
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))))
1013 entity))
1016 ;;;; Example Block
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))
1025 ;;;; Export Snippet
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)))
1034 ;;;; Export Block
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))))
1043 ;;;; Fixed Width
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)))
1067 ;;;; Headline
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
1074 ;; of the template.
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.
1081 (pre-blanks
1082 (make-string
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.
1087 (links
1088 (unless (or (eq (caar (org-element-contents headline)) 'section))
1089 (let ((title (org-element-property :title headline)))
1090 (when (consp title)
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.
1094 (if low-level-rank
1095 (concat
1096 ;; Bullet.
1097 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1098 org-e-ascii-bullets))))
1099 (char-to-string
1100 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1102 ;; Title.
1103 (org-e-ascii--build-title headline info width) "\n"
1104 ;; Contents, indented by length of bullet.
1105 pre-blanks
1106 (org-e-ascii--indent-string
1107 (concat contents
1108 (when (org-string-nw-p links) (concat "\n\n" links)))
1110 ;; Else: Standard headline.
1111 (concat
1112 (org-e-ascii--build-title headline info width 'underline)
1113 "\n" pre-blanks
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
1122 information."
1123 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info))
1124 (spec-width
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)))
1147 ;;;; Inlinetask
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
1158 ;; todo.
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))))
1163 ;; todo-type
1164 (org-element-property :todo-type inlinetask)
1165 ;; priority
1166 (and (plist-get info :with-priority)
1167 (org-element-property :priority inlinetask))
1168 ;; title
1169 (org-export-data (org-element-property :title inlinetask) info)
1170 ;; tags
1171 (and (plist-get info :with-tags)
1172 (org-element-property :tags inlinetask))
1173 ;; contents and width
1174 contents width)
1175 ;; Otherwise, use a default template.
1176 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1177 (org-e-ascii--indent-string
1178 (concat
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)))
1186 "\n"
1187 ;; If CONTENTS is not empty, insert it along with
1188 ;; a separator.
1189 (when (org-string-nw-p contents)
1190 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1191 ;; Bottom line.
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)))))))
1199 ;;;; Italic
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))
1208 ;;;; Item
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)))
1217 (bullet
1218 ;; First parent of ITEM is always the plain-list. Get
1219 ;; `:type' property from it.
1220 (org-list-bullet-string
1221 (case list-type
1222 (descriptive
1223 (concat checkbox
1224 (org-export-data (org-element-property :tag item) info)
1225 ": "))
1226 (ordered
1227 ;; Return correct number for ITEM, paying attention to
1228 ;; counters.
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)
1234 struct
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.
1240 (if (not utf8p) bul
1241 (replace-regexp-in-string
1242 "-" "•"
1243 (replace-regexp-in-string
1244 "+" "⁃"
1245 (replace-regexp-in-string "*" "‣" bul))))))))))
1246 (concat
1247 bullet
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)
1254 (org-trim contents)
1255 (concat "\n" contents))))))
1258 ;;;; Keyword
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
1263 information."
1264 (let ((key (org-element-property :key keyword))
1265 (value (org-element-property :value keyword)))
1266 (cond
1267 ((string= key "ASCII") value)
1268 ((string= key "TOC")
1269 (let ((value (downcase value)))
1270 (cond
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
1288 information."
1289 (org-remove-indentation (org-element-property :value latex-environment)))
1292 ;;;; Latex Fragment
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
1297 information."
1298 (org-element-property :value latex-fragment))
1301 ;;;; Line Break
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)
1309 ;;;; Link
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)))
1318 (cond
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)))
1327 (when destination
1328 (org-export-data (org-element-contents destination) info))))
1329 ;; Do not apply a special syntax on fuzzy links pointing to
1330 ;; targets.
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
1336 (when destination
1337 (let ((number
1338 (org-export-get-ordinal
1339 destination info nil 'org-e-ascii--has-caption-p)))
1340 (when number
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)
1345 (concat
1346 (format "[%s]" desc)
1347 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
1350 ;;;; Paragraph
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
1357 contents
1358 (org-e-ascii--current-text-width paragraph info) info))
1361 ;;;; Plain List
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."
1367 contents)
1370 ;;;; Plain Text
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
1380 "\\.\\.\\." "…"
1381 (replace-regexp-in-string
1382 "--" "–"
1383 (replace-regexp-in-string "---" "—" text))))))
1386 ;;;; Planning
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
1391 channel."
1392 (mapconcat
1393 'identity
1394 (delq nil
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))))))
1404 " "))
1407 ;;;; Property Drawer
1409 ;; Property drawers are ignored.
1412 ;;;; Quote Block
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)))
1425 ;;;; Quote Section
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))
1431 (value
1432 (org-export-data
1433 (org-remove-indentation (org-element-property :value quote-section))
1434 info)))
1435 (org-e-ascii--indent-string
1436 value
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))))))
1444 ;;;; Radio Target
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."
1450 contents)
1452 ;;;; Section
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
1459 (concat
1460 contents
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))))
1474 ;;;; Special Block
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."
1480 contents)
1483 ;;;; Src Block
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)))
1490 (concat
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))
1505 ;;;; Subscript
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)))
1516 ;;;; Superscript
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)))
1527 ;;;; Strike-through
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))
1536 ;;;; Table
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)))
1543 (concat
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))
1551 (with-temp-buffer
1552 (insert (org-remove-indentation
1553 (org-element-property :value table)))
1554 (goto-char (point-min))
1555 (aa2u)
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)))))
1565 ;;;; Table Cell
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
1574 column.
1576 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1577 are ignored."
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))))
1584 (org-element-map
1585 table 'table-row
1586 (lambda (row)
1587 (setq max-width
1588 (max (length
1589 (org-export-data
1590 (org-element-contents
1591 (elt (if specialp (cdr (org-element-contents row))
1592 (org-element-contents row))
1593 col))
1594 info))
1595 max-width)))
1596 info)
1597 max-width)))
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)
1613 (data
1614 (when contents
1615 (org-e-ascii--justify-string
1616 contents width
1617 (org-export-table-cell-alignment table-cell info)))))
1618 (setq contents (concat data (make-string (- width (length data)) ? ))))
1619 ;; Return cell.
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) "│" "|")))))
1625 ;;;; Table Row
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)
1632 (let ((build-hline
1633 (function
1634 (lambda (lcorner horiz vert rcorner)
1635 (concat
1636 (apply
1637 'concat
1638 (org-element-map
1639 table-row 'table-cell
1640 (lambda (cell)
1641 (let ((width (org-e-ascii--table-cell-width cell info))
1642 (borders (org-export-table-cell-borders cell info)))
1643 (concat
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)
1652 cell))
1653 lcorner)
1654 (make-string (+ 2 width) (string-to-char horiz))
1655 (cond
1656 ((not (memq 'right borders)) nil)
1657 ((eq (car (last (org-element-contents table-row))) cell)
1658 rcorner)
1659 (t vert)))))
1660 info)) "\n"))))
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)
1664 info)))
1665 (concat (cond
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 "│" "|"))
1673 contents "\n"
1674 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1675 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1676 (funcall build-hline "+" "-" "+" "+")))))))
1679 ;;;; Target
1681 ;; Targets are invisible.
1684 ;;;; Timestamp
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)))
1690 (range-end
1691 (org-translate-time (org-element-property :range-end timestamp)))
1692 (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1693 (concat value
1694 (when range-end (concat (if utf8p "–" "--") range-end)))))
1697 ;;;; Underline
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))
1706 ;;;; Verbatim
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)))
1715 ;;;; Verse Block
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)))
1727 ;;; Filter
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
1748 ;;;###autoload
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
1754 narrowed part.
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
1760 first.
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'
1774 is non-nil."
1775 (interactive)
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))))
1783 ;;;###autoload
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
1789 narrowed part.
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
1795 first.
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
1808 directory.
1810 Return output file's name."
1811 (interactive)
1812 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
1813 (org-export-to-file
1814 'e-ascii outfile subtreep visible-only body-only ext-plist)))
1817 (provide 'org-e-ascii)
1818 ;;; org-e-ascii.el ends here