org-e-ascii: No trailing whitespace in headlines/inlinetasks titles
[org-mode.git] / contrib / lisp / org-e-ascii.el
blobad93ad955fa5f5b6c8a6bf1b0a74b05a903c4c9a
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 (macro . org-e-ascii-macro)
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"
104 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
105 (:filter-section . org-e-ascii-filter-headline-blank-lines))
106 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))
110 ;;; User Configurable Variables
112 (defgroup org-export-e-ascii nil
113 "Options for exporting Org mode files to ASCII."
114 :tag "Org Export ASCII"
115 :group 'org-export)
117 (defcustom org-e-ascii-text-width 72
118 "Maximum width of exported text.
119 This number includes margin size, as set in
120 `org-e-ascii-global-margin'."
121 :group 'org-export-e-ascii
122 :type 'integer)
124 (defcustom org-e-ascii-global-margin 0
125 "Width of the left margin, in number of characters."
126 :group 'org-export-e-ascii
127 :type 'integer)
129 (defcustom org-e-ascii-inner-margin 2
130 "Width of the inner margin, in number of characters.
131 Inner margin is applied between each headline."
132 :group 'org-export-e-ascii
133 :type 'integer)
135 (defcustom org-e-ascii-quote-margin 6
136 "Width of margin used for quoting text, in characters.
137 This margin is applied on both sides of the text."
138 :group 'org-export-e-ascii
139 :type 'integer)
141 (defcustom org-e-ascii-inlinetask-width 30
142 "Width of inline tasks, in number of characters.
143 This number ignores any margin."
144 :group 'org-export-e-ascii
145 :type 'integer)
147 (defcustom org-e-ascii-headline-spacing '(1 . 2)
148 "Number of blank lines inserted around headlines.
150 This variable can be set to a cons cell. In that case, its car
151 represents the number of blank lines present before headline
152 contents whereas its cdr reflects the number of blank lines after
153 contents.
155 A nil value replicates the number of blank lines found in the
156 original Org buffer at the same place."
157 :group 'org-export-e-ascii
158 :type '(choice
159 (const :tag "Replicate original spacing" nil)
160 (cons :tag "Set an uniform spacing"
161 (integer :tag "Number of blank lines before contents")
162 (integer :tag "Number of blank lines after contents"))))
164 (defcustom org-e-ascii-charset 'ascii
165 "The charset allowed to represent various elements and objects.
166 Possible values are:
167 `ascii' Only use plain ASCII characters
168 `latin1' Include Latin-1 characters
169 `utf-8' Use all UTF-8 characters"
170 :group 'org-export-e-ascii
171 :type '(choice
172 (const :tag "ASCII" ascii)
173 (const :tag "Latin-1" latin1)
174 (const :tag "UTF-8" utf-8)))
176 (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
177 (latin1 ?= ?~ ?-)
178 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
179 "Characters for underlining headings in ASCII export.
181 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
182 and whose value is a list of characters.
184 For each supported charset, this variable associates a sequence
185 of underline characters. In a sequence, the characters will be
186 used in order for headlines level 1, 2, ... If no character is
187 available for a given level, the headline won't be underlined."
188 :group 'org-export-e-ascii
189 :type '(list
190 (cons :tag "Underline characters sequence"
191 (const :tag "ASCII charset" ascii)
192 (repeat character))
193 (cons :tag "Underline characters sequence"
194 (const :tag "Latin-1 charset" latin1)
195 (repeat character))
196 (cons :tag "Underline characters sequence"
197 (const :tag "UTF-8 charset" utf-8)
198 (repeat character))))
200 (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
201 (latin1 ?§ ?¶)
202 (utf-8 ?◊))
203 "Bullet characters for headlines converted to lists in ASCII export.
205 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
206 and whose value is a list of characters.
208 The first character is used for the first level considered as low
209 level, and so on. If there are more levels than characters given
210 here, the list will be repeated.
212 Note that this variable doesn't affect plain lists
213 representation."
214 :group 'org-export-e-ascii
215 :type '(list
216 (cons :tag "Bullet characters for low level headlines"
217 (const :tag "ASCII charset" ascii)
218 (repeat character))
219 (cons :tag "Bullet characters for low level headlines"
220 (const :tag "Latin-1 charset" latin1)
221 (repeat character))
222 (cons :tag "Bullet characters for low level headlines"
223 (const :tag "UTF-8 charset" utf-8)
224 (repeat character))))
226 (defcustom org-e-ascii-links-to-notes t
227 "Non-nil means convert links to notes before the next headline.
228 When nil, the link will be exported in place. If the line
229 becomes long in this way, it will be wrapped."
230 :group 'org-export-e-ascii
231 :type 'boolean)
233 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
234 "Non-nil means keep all vertical lines in ASCII tables.
235 When nil, vertical lines will be removed except for those needed
236 for column grouping."
237 :group 'org-export-e-ascii
238 :type 'boolean)
240 (defcustom org-e-ascii-table-widen-columns t
241 "Non-nil means widen narrowed columns for export.
242 When nil, narrowed columns will look in ASCII export just like in
243 Org mode, i.e. with \"=>\" as ellipsis."
244 :group 'org-export-e-ascii
245 :type 'boolean)
247 (defcustom org-e-ascii-table-use-ascii-art nil
248 "Non-nil means table.el tables are turned into ascii-art.
250 It only makes sense when export charset is `utf-8'. It is nil by
251 default since it requires ascii-art-to-unicode.el package. You
252 can download it here:
254 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
256 (defcustom org-e-ascii-caption-above nil
257 "When non-nil, place caption string before the element.
258 Otherwise, place it right after it."
259 :group 'org-export-e-ascii
260 :type 'boolean)
262 (defcustom org-e-ascii-verbatim-format "`%s'"
263 "Format string used for verbatim text and inline code."
264 :group 'org-export-e-ascii
265 :type 'string)
267 (defcustom org-e-ascii-format-drawer-function nil
268 "Function called to format a drawer in ASCII.
270 The function must accept two parameters:
271 NAME the drawer name, like \"LOGBOOK\"
272 CONTENTS the contents of the drawer.
273 WIDTH the text width within the drawer.
275 The function should return either the string to be exported or
276 nil to ignore the drawer.
278 For example, the variable could be set to the following function
279 in order to mimic default behaviour:
281 \(defun org-e-ascii-format-drawer-default \(name contents width\)
282 \"Format a drawer element for ASCII export.\"
283 contents\)"
284 :group 'org-export-e-ascii
285 :type 'function)
287 (defcustom org-e-ascii-format-inlinetask-function nil
288 "Function called to format an inlinetask in ASCII.
290 The function must accept six parameters:
291 TODO the todo keyword, as a string
292 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
293 PRIORITY the inlinetask priority, as a string
294 NAME the inlinetask name, as a string.
295 TAGS the inlinetask tags, as a list of strings.
296 CONTENTS the contents of the inlinetask, as a string.
298 The function should return either the string to be exported or
299 nil to ignore the inline task.
301 For example, the variable could be set to the following function
302 in order to mimic default behaviour:
304 \(defun org-e-ascii-format-inlinetask-default
305 \(todo type priority name tags contents\)
306 \"Format an inline task element for ASCII export.\"
307 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
308 \(width org-e-ascii-inlinetask-width\)
309 \(org-e-ascii--indent-string
310 \(concat
311 ;; Top line, with an additional blank line if not in UTF-8.
312 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
313 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
314 ;; Add title. Fill it if wider than inlinetask.
315 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
316 \(if \(<= \(length title\) width\) title
317 \(org-e-ascii--fill-string title width info\)\)\)
318 \"\\n\"
319 ;; If CONTENTS is not empty, insert it along with
320 ;; a separator.
321 \(when \(org-string-nw-p contents\)
322 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
323 ;; Bottom line.
324 \(make-string width \(if utf8p ?━ ?_\)\)\)
325 ;; Flush the inlinetask to the right.
326 \(- \(plist-get info :ascii-width\)
327 \(plist-get info :ascii-margin\)
328 \(plist-get info :ascii-inner-margin\)
329 \(org-e-ascii--current-text-width inlinetask info\)\)"
330 :group 'org-export-e-ascii
331 :type 'function)
335 ;;; Internal Functions
337 ;; Internal functions fall into three categories.
339 ;; The first one is about text formatting. The core function is
340 ;; `org-e-ascii--current-text-width', which determines the current
341 ;; text width allowed to a given element. In other words, it helps
342 ;; keeping each line width within maximum text width defined in
343 ;; `org-e-ascii-text-width'. Once this information is known,
344 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
345 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
346 ;; operate on a given output string.
348 ;; The second category contains functions handling elements listings,
349 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
350 ;; returns a complete table of contents, `org-e-ascii--list-listings'
351 ;; returns a list of referenceable src-block elements, and
352 ;; `org-e-ascii--list-tables' does the same for table elements.
354 ;; The third category includes general helper functions.
355 ;; `org-e-ascii--build-title' creates the title for a given headline
356 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
357 ;; caption string associated to a table or a src-block.
358 ;; `org-e-ascii--describe-links' creates notes about links for
359 ;; insertion at the end of a section. It uses
360 ;; `org-e-ascii--unique-links' to get the list of links to describe.
361 ;; Eventually, `org-e-ascii--translate' translates a string according
362 ;; to language and charset specification.
365 (defun org-e-ascii--fill-string (s text-width info &optional justify)
366 "Fill a string with specified text-width and return it.
368 S is the string being filled. TEXT-WIDTH is an integer
369 specifying maximum length of a line. INFO is the plist used as
370 a communication channel.
372 Optional argument JUSTIFY can specify any type of justification
373 among `left', `center', `right' or `full'. A nil value is
374 equivalent to `left'. For a justification that doesn't also fill
375 string, see `org-e-ascii--justify-string'.
377 Return nil if S isn't a string."
378 ;; Don't fill paragraph when break should be preserved.
379 (cond ((not (stringp s)) nil)
380 ((plist-get info :preserve-breaks) s)
381 (t (with-temp-buffer
382 (let ((fill-column text-width)
383 (use-hard-newlines t))
384 (insert s)
385 (fill-region (point-min) (point-max) justify))
386 (buffer-string)))))
388 (defun org-e-ascii--justify-string (s text-width how)
389 "Justify string S.
390 TEXT-WIDTH is an integer specifying maximum length of a line.
391 HOW determines the type of justification: it can be `left',
392 `right', `full' or `center'."
393 (with-temp-buffer
394 (insert s)
395 (goto-char (point-min))
396 (let ((fill-column text-width))
397 (while (< (point) (point-max))
398 (justify-current-line how)
399 (forward-line)))
400 (buffer-string)))
402 (defun org-e-ascii--indent-string (s width)
403 "Indent string S by WIDTH white spaces.
404 Empty lines are not indented."
405 (when (stringp s)
406 (replace-regexp-in-string
407 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
409 (defun org-e-ascii--box-string (s info)
410 "Return string S with a partial box to its left.
411 INFO is a plist used as a communicaton channel."
412 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
413 (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
414 (replace-regexp-in-string
415 "^" (if utf8p "│ " "| ")
416 ;; Remove last newline character.
417 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
419 (defun org-e-ascii--current-text-width (element info)
420 "Return maximum text width for ELEMENT's contents.
421 INFO is a plist used as a communication channel."
422 (case (org-element-type element)
423 ;; Elements with an absolute width: `headline' and `inlinetask'.
424 (inlinetask org-e-ascii-inlinetask-width)
425 ('headline
426 (- org-e-ascii-text-width
427 (let ((low-level-rank (org-export-low-level-p element info)))
428 (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
429 ;; Elements with a relative width: store maximum text width in
430 ;; TOTAL-WIDTH.
431 (otherwise
432 (let* ((genealogy (cons element (org-export-get-genealogy element)))
433 ;; Total width is determined by the presence, or not, of an
434 ;; inline task among ELEMENT parents.
435 (total-width
436 (if (loop for parent in genealogy
437 thereis (eq (org-element-type parent) 'inlinetask))
438 org-e-ascii-inlinetask-width
439 ;; No inlinetask: Remove global margin from text width.
440 (- org-e-ascii-text-width
441 org-e-ascii-global-margin
442 (let ((parent (org-export-get-parent-headline element)))
443 ;; Inner margin doesn't apply to text before first
444 ;; headline.
445 (if (not parent) 0
446 (let ((low-level-rank
447 (org-export-low-level-p parent info)))
448 ;; Inner margin doesn't apply to contents of
449 ;; low level headlines, since they've got their
450 ;; own indentation mechanism.
451 (if low-level-rank (* low-level-rank 2)
452 org-e-ascii-inner-margin))))))))
453 (- total-width
454 ;; Each `quote-block', `quote-section' and `verse-block' above
455 ;; narrows text width by twice the standard margin size.
456 (+ (* (loop for parent in genealogy
457 when (memq (org-element-type parent)
458 '(quote-block quote-section verse-block))
459 count parent)
460 2 org-e-ascii-quote-margin)
461 ;; Text width within a plain-list is restricted by
462 ;; indentation of current item. If that's the case,
463 ;; compute it with the help of `:structure' property from
464 ;; parent item, if any.
465 (let ((parent-item
466 (if (eq (org-element-type element) 'item) element
467 (loop for parent in genealogy
468 when (eq (org-element-type parent) 'item)
469 return parent))))
470 (if (not parent-item) 0
471 ;; Compute indentation offset of the current item,
472 ;; that is the sum of the difference between its
473 ;; indentation and the indentation of the top item in
474 ;; the list and current item bullet's length. Also
475 ;; remove checkbox length, and tag length (for
476 ;; description lists) or bullet length.
477 (let ((struct (org-element-property :structure parent-item))
478 (beg-item (org-element-property :begin parent-item)))
479 (+ (- (org-list-get-ind beg-item struct)
480 (org-list-get-ind
481 (org-list-get-top-point struct) struct))
482 (length (org-e-ascii--checkbox parent-item info))
483 (length
484 (or (org-list-get-tag beg-item struct)
485 (org-list-get-bullet beg-item struct)))))))))))))
487 (defun org-e-ascii--build-title
488 (element info text-width &optional underline notags)
489 "Format ELEMENT title and return it.
491 ELEMENT is either an `headline' or `inlinetask' element. INFO is
492 a plist used as a communication channel. TEXT-WIDTH is an
493 integer representing the maximum length of a line.
495 When optional argument UNDERLINE is non-nil, underline title,
496 without the tags, according to `org-e-ascii-underline'
497 specifications.
499 if optional argument NOTAGS is nil, no tags will be added to the
500 title."
501 (let* ((headlinep (eq (org-element-type element) 'headline))
502 (numbers
503 ;; Numbering is specific to headlines.
504 (and headlinep (org-export-numbered-headline-p element info)
505 ;; All tests passed: build numbering string.
506 (concat
507 (mapconcat
508 'number-to-string
509 (org-export-get-headline-number element info) ".")
510 " ")))
511 (text (org-trim
512 (org-export-data (org-element-property :title element) info)))
513 (todo
514 (and (plist-get info :with-todo-keywords)
515 (let ((todo (org-element-property :todo-keyword element)))
516 (and todo (concat (org-export-data todo info) " ")))))
517 (tags (and (not notags)
518 (plist-get info :with-tags)
519 (let ((tag-list (org-export-get-tags element info)))
520 (and tag-list
521 (format ":%s:"
522 (mapconcat 'identity tag-list ":"))))))
523 (priority
524 (and (plist-get info :with-priority)
525 (let ((char (org-element-property :priority element)))
526 (and char (format "(#%c) " char)))))
527 (first-part (concat numbers todo priority text)))
528 (concat
529 first-part
530 ;; Align tags, if any.
531 (when tags
532 (format
533 (format " %%%ds"
534 (max (- text-width (1+ (length first-part))) (length tags)))
535 tags))
536 ;; Maybe underline text, if ELEMENT type is `headline' and an
537 ;; underline character has been defined.
538 (when (and underline headlinep)
539 (let ((under-char
540 (nth (1- (org-export-get-relative-level element info))
541 (cdr (assq (plist-get info :ascii-charset)
542 org-e-ascii-underline)))))
543 (and under-char
544 (concat "\n"
545 (make-string (length first-part) under-char))))))))
547 (defun org-e-ascii--has-caption-p (element info)
548 "Non-nil when ELEMENT has a caption affiliated keyword.
549 INFO is a plist used as a communication channel. This function
550 is meant to be used as a predicate for `org-export-get-ordinal'."
551 (org-element-property :caption element))
553 (defun org-e-ascii--build-caption (element info)
554 "Return caption string for ELEMENT, if applicable.
556 INFO is a plist used as a communication channel.
558 The caption string contains the sequence number of ELEMENT along
559 with its real caption. Return nil when ELEMENT has no affiliated
560 caption keyword."
561 (let ((caption (org-element-property :caption element)))
562 (when caption
563 ;; Get sequence number of current src-block among every
564 ;; src-block with a caption.
565 (let ((reference
566 (org-export-get-ordinal
567 element info nil 'org-e-ascii--has-caption-p))
568 (title-fmt (org-e-ascii--translate
569 (case (org-element-type element)
570 (table "Table %d: %s")
571 (src-block "Listing %d: %s"))
572 info)))
573 (org-e-ascii--fill-string
574 (format title-fmt reference (org-export-data (car caption) info))
575 (org-e-ascii--current-text-width element info) info)))))
577 (defun org-e-ascii--build-toc (info &optional n keyword)
578 "Return a table of contents.
580 INFO is a plist used as a communication channel.
582 Optional argument N, when non-nil, is an integer specifying the
583 depth of the table.
585 Optional argument KEYWORD specifies the TOC keyword, if any, from
586 which the table of contents generation has been initiated."
587 (let ((title (org-e-ascii--translate "Table of Contents" info)))
588 (concat
589 title "\n"
590 (make-string (length title)
591 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
592 "\n\n"
593 (let ((text-width
594 (if keyword (org-e-ascii--current-text-width keyword info)
595 (- org-e-ascii-text-width org-e-ascii-global-margin))))
596 (mapconcat
597 (lambda (headline)
598 (let* ((level (org-export-get-relative-level headline info))
599 (indent (* (1- level) 3)))
600 (concat
601 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
602 (org-e-ascii--build-title
603 headline info (- text-width indent) nil
604 (eq (plist-get info :with-tags) 'not-in-toc)))))
605 (org-export-collect-headlines info n) "\n")))))
607 (defun org-e-ascii--list-listings (keyword info)
608 "Return a list of listings.
610 KEYWORD is the keyword that initiated the list of listings
611 generation. INFO is a plist used as a communication channel."
612 (let ((title (org-e-ascii--translate "List of Listings" info)))
613 (concat
614 title "\n"
615 (make-string (length title)
616 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
617 "\n\n"
618 (let ((text-width
619 (if keyword (org-e-ascii--current-text-width keyword info)
620 (- org-e-ascii-text-width org-e-ascii-global-margin)))
621 ;; Use a counter instead of retreiving ordinal of each
622 ;; src-block.
623 (count 0))
624 (mapconcat
625 (lambda (src-block)
626 ;; Store initial text so its length can be computed. This is
627 ;; used to properly align caption right to it in case of
628 ;; filling (like contents of a description list item).
629 (let ((initial-text
630 (format (org-e-ascii--translate "Listing %d:" info)
631 (incf count))))
632 (concat
633 initial-text " "
634 (org-trim
635 (org-e-ascii--indent-string
636 (org-e-ascii--fill-string
637 (let ((caption (org-element-property :caption src-block)))
638 ;; Use short name in priority, if available.
639 (org-export-data (or (cdr caption) (car caption)) info))
640 (- text-width (length initial-text)) info)
641 (length initial-text))))))
642 (org-export-collect-listings info) "\n")))))
644 (defun org-e-ascii--list-tables (keyword info)
645 "Return a list of listings.
647 KEYWORD is the keyword that initiated the list of listings
648 generation. INFO is a plist used as a communication channel."
649 (let ((title (org-e-ascii--translate "List of Tables" info)))
650 (concat
651 title "\n"
652 (make-string (length title)
653 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
654 "\n\n"
655 (let ((text-width
656 (if keyword (org-e-ascii--current-text-width keyword info)
657 (- org-e-ascii-text-width org-e-ascii-global-margin)))
658 ;; Use a counter instead of retreiving ordinal of each
659 ;; src-block.
660 (count 0))
661 (mapconcat
662 (lambda (table)
663 ;; Store initial text so its length can be computed. This is
664 ;; used to properly align caption right to it in case of
665 ;; filling (like contents of a description list item).
666 (let ((initial-text
667 (format (org-e-ascii--translate "Table %d:" info)
668 (incf count))))
669 (concat
670 initial-text " "
671 (org-trim
672 (org-e-ascii--indent-string
673 (org-e-ascii--fill-string
674 (let ((caption (org-element-property :caption table)))
675 ;; Use short name in priority, if available.
676 (org-export-data (or (cdr caption) (car caption)) info))
677 (- text-width (length initial-text)) info)
678 (length initial-text))))))
679 (org-export-collect-tables info) "\n")))))
681 (defun org-e-ascii--unique-links (element info)
682 "Return a list of unique link references in ELEMENT.
684 ELEMENT is either an headline element or a section element. INFO
685 is a plist used as a communication channel."
686 (let* (seen
687 (unique-link-p
688 (function
689 ;; Return LINK if it wasn't referenced so far, or nil.
690 ;; Update SEEN links along the way.
691 (lambda (link)
692 (let ((footprint
693 (cons (org-element-property :raw-link link)
694 (org-element-contents link))))
695 (unless (member footprint seen)
696 (push footprint seen) link)))))
697 ;; If at a section, find parent headline, if any, in order to
698 ;; count links that might be in the title.
699 (headline
700 (if (eq (org-element-type element) 'headline) element
701 (or (org-export-get-parent-headline element) element))))
702 ;; Get all links in HEADLINE.
703 (org-element-map
704 headline 'link (lambda (link) (funcall unique-link-p link)) info)))
706 (defun org-e-ascii--describe-links (links width info)
707 "Return a string describing a list of links.
709 LINKS is a list of link type objects, as returned by
710 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
711 the output string. INFO is a plist used as a communication
712 channel."
713 (mapconcat
714 (lambda (link)
715 (let ((type (org-element-property :type link))
716 (anchor (let ((desc (org-element-contents link)))
717 (if (not desc) (org-element-property :raw-link link)
718 (org-export-data desc info)))))
719 (cond
720 ;; Coderefs, radio links and fuzzy links are ignored.
721 ((member type '("coderef" "radio" "fuzzy")) nil)
722 ;; Id and custom-id links: Headlines refer to their numbering.
723 ((member type '("custom-id" "id"))
724 (let ((dest (org-export-resolve-id-link link info)))
725 (concat
726 (org-e-ascii--fill-string
727 (format
728 "[%s] %s"
729 anchor
730 (if (not dest) (org-e-ascii--translate "Unknown reference" info)
731 (format
732 (org-e-ascii--translate "See section %s" info)
733 (mapconcat 'number-to-string
734 (org-export-get-headline-number dest info) "."))))
735 width info) "\n\n")))
736 ;; Do not add a link that cannot be resolved and doesn't have
737 ;; any description: destination is already visible in the
738 ;; paragraph.
739 ((not (org-element-contents link)) nil)
741 (concat
742 (org-e-ascii--fill-string
743 (format "[%s] %s" anchor (org-element-property :raw-link link))
744 width info)
745 "\n\n")))))
746 links ""))
748 (defun org-e-ascii--checkbox (item info)
749 "Return checkbox string for ITEM or nil.
750 INFO is a plist used as a communication channel."
751 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
752 (case (org-element-property :checkbox item)
753 (on (if utf8p "☑ " "[X] "))
754 (off (if utf8p "☐ " "[ ] "))
755 (trans (if utf8p "☒ " "[-] ")))))
759 ;;; Template
761 (defun org-e-ascii-template--document-title (info)
762 "Return document title, as a string.
763 INFO is a plist used as a communication channel."
764 (let ((text-width org-e-ascii-text-width)
765 (title (org-export-data (plist-get info :title) info))
766 (author (and (plist-get info :with-author)
767 (let ((auth (plist-get info :author)))
768 (and auth (org-export-data auth info)))))
769 (email (and (plist-get info :with-email)
770 (org-export-data (plist-get info :email) info)))
771 (date (org-export-data (plist-get info :date) info)))
772 ;; There are two types of title blocks depending on the presence
773 ;; of a title to display.
774 (if (string= title "")
775 ;; Title block without a title. DATE is positioned at the top
776 ;; right of the document, AUTHOR to the top left and EMAIL
777 ;; just below.
778 (cond
779 ((and (org-string-nw-p date) (org-string-nw-p author))
780 (concat
781 author
782 (make-string (- text-width (length date) (length author)) ? )
783 date
784 (when (org-string-nw-p email) (concat "\n" email))
785 "\n\n\n"))
786 ((and (org-string-nw-p date) (org-string-nw-p email))
787 (concat
788 email
789 (make-string (- text-width (length date) (length email)) ? )
790 date "\n\n\n"))
791 ((org-string-nw-p date)
792 (concat
793 (org-e-ascii--justify-string date text-width 'right)
794 "\n\n\n"))
795 ((and (org-string-nw-p author) (org-string-nw-p email))
796 (concat author "\n" email "\n\n\n"))
797 ((org-string-nw-p author) (concat author "\n\n\n"))
798 ((org-string-nw-p email) (concat email "\n\n\n")))
799 ;; Title block with a title. Document's TITLE, along with the
800 ;; AUTHOR and its EMAIL are both overlined and an underlined,
801 ;; centered. Date is just below, also centered.
802 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
803 ;; Format TITLE. It may be filled if it is too wide,
804 ;; that is wider than the two thirds of the total width.
805 (title-len (min (length title) (/ (* 2 text-width) 3)))
806 (formatted-title (org-e-ascii--fill-string title title-len info))
807 (line
808 (make-string
809 (min (+ (max title-len (length author) (length email)) 2)
810 text-width) (if utf8p ?━ ?_))))
811 (org-e-ascii--justify-string
812 (concat line "\n"
813 (unless utf8p "\n")
814 (upcase formatted-title)
815 (cond
816 ((and (org-string-nw-p author) (org-string-nw-p email))
817 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
818 ((org-string-nw-p author)
819 (concat (if utf8p "\n\n\n" "\n\n") author))
820 ((org-string-nw-p email)
821 (concat (if utf8p "\n\n\n" "\n\n") email)))
822 "\n" line
823 (when (org-string-nw-p date) (concat "\n\n\n" date))
824 "\n\n\n") text-width 'center)))))
826 (defun org-e-ascii-template (contents info)
827 "Return complete document string after ASCII conversion.
828 CONTENTS is the transcoded contents string. INFO is a plist
829 holding export options."
830 (org-element-normalize-string
831 (org-e-ascii--indent-string
832 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
833 ;; 1. Build title block.
834 (concat
835 (org-e-ascii-template--document-title info)
836 ;; 2. Table of contents.
837 (let ((depth (plist-get info :with-toc)))
838 (when depth
839 (concat
840 (org-e-ascii--build-toc info (and (wholenump depth) depth))
841 "\n\n\n")))
842 ;; 3. Document's body.
843 contents
844 ;; 4. Footnote definitions.
845 (let ((definitions (org-export-collect-footnote-definitions
846 (plist-get info :parse-tree) info))
847 ;; Insert full links right inside the footnote definition
848 ;; as they have no chance to be inserted later.
849 (org-e-ascii-links-to-notes nil))
850 (when definitions
851 (concat
852 "\n\n\n"
853 (let ((title (org-e-ascii--translate "Footnotes" info)))
854 (concat
855 title "\n"
856 (make-string
857 (length title)
858 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
859 "\n\n"
860 (mapconcat
861 (lambda (ref)
862 (let ((id (format "[%s] " (car ref))))
863 ;; Distinguish between inline definitions and
864 ;; full-fledged definitions.
865 (org-trim
866 (let ((def (nth 2 ref)))
867 (if (eq (org-element-type def) 'org-data)
868 ;; Full-fledged definition: footnote ID is
869 ;; inserted inside the first parsed paragraph
870 ;; (FIRST), if any, to be sure filling will
871 ;; take it into consideration.
872 (let ((first (car (org-element-contents def))))
873 (if (not (eq (org-element-type first) 'paragraph))
874 (concat id "\n" (org-export-data def info))
875 (push id (nthcdr 2 first))
876 (org-export-data def info)))
877 ;; Fill paragraph once footnote ID is inserted in
878 ;; order to have a correct length for first line.
879 (org-e-ascii--fill-string
880 (concat id (org-export-data def info))
881 text-width info))))))
882 definitions "\n\n"))))
883 ;; 5. Creator. Ignore `comment' value as there are no comments in
884 ;; ASCII. Justify it to the bottom right.
885 (let ((creator-info (plist-get info :with-creator)))
886 (unless (or (not creator-info) (eq creator-info 'comment))
887 (concat
888 "\n\n\n"
889 (org-e-ascii--fill-string
890 (plist-get info :creator) text-width info 'right))))))
891 org-e-ascii-global-margin)))
893 (defun org-e-ascii--translate (s info)
894 "Translate string S according to specified language and charset.
895 INFO is a plist used as a communication channel."
896 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
897 (org-export-translate s charset info)))
901 ;;; Transcode Functions
903 ;;;; Babel Call
905 ;; Babel Calls are ignored.
908 ;;;; Bold
910 (defun org-e-ascii-bold (bold contents info)
911 "Transcode BOLD from Org to ASCII.
912 CONTENTS is the text with bold markup. INFO is a plist holding
913 contextual information."
914 (format "*%s*" contents))
917 ;;;; Center Block
919 (defun org-e-ascii-center-block (center-block contents info)
920 "Transcode a CENTER-BLOCK element from Org to ASCII.
921 CONTENTS holds the contents of the block. INFO is a plist
922 holding contextual information."
923 (org-e-ascii--justify-string
924 contents (org-e-ascii--current-text-width center-block info) 'center))
927 ;;;; Clock
929 (defun org-e-ascii-clock (clock contents info)
930 "Transcode a CLOCK object from Org to ASCII.
931 CONTENTS is nil. INFO is a plist holding contextual
932 information."
933 (concat org-clock-string " "
934 (org-translate-time (org-element-property :value clock))
935 (let ((time (org-element-property :time clock)))
936 (and time
937 (concat " => "
938 (apply 'format
939 "%2s:%02s"
940 (org-split-string time ":")))))))
943 ;;;; Code
945 (defun org-e-ascii-code (code contents info)
946 "Return a CODE object from Org to ASCII.
947 CONTENTS is nil. INFO is a plist holding contextual
948 information."
949 (format org-e-ascii-verbatim-format (org-element-property :value code)))
952 ;;;; Comment
954 ;; Comments are ignored.
957 ;;;; Comment Block
959 ;; Comment Blocks are ignored.
962 ;;;; Drawer
964 (defun org-e-ascii-drawer (drawer contents info)
965 "Transcode a DRAWER element from Org to ASCII.
966 CONTENTS holds the contents of the block. INFO is a plist
967 holding contextual information."
968 (let ((name (org-element-property :drawer-name drawer))
969 (width (org-e-ascii--current-text-width drawer info)))
970 (if (functionp org-e-ascii-format-drawer-function)
971 (funcall org-e-ascii-format-drawer-function name contents width)
972 ;; If there's no user defined function: simply
973 ;; display contents of the drawer.
974 contents)))
977 ;;;; Dynamic Block
979 (defun org-e-ascii-dynamic-block (dynamic-block contents info)
980 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
981 CONTENTS holds the contents of the block. INFO is a plist
982 holding contextual information."
983 contents)
986 ;;;; Entity
988 (defun org-e-ascii-entity (entity contents info)
989 "Transcode an ENTITY object from Org to ASCII.
990 CONTENTS are the definition itself. INFO is a plist holding
991 contextual information."
992 (org-element-property
993 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
994 entity))
997 ;;;; Example Block
999 (defun org-e-ascii-example-block (example-block contents info)
1000 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1001 CONTENTS is nil. INFO is a plist holding contextual information."
1002 (org-e-ascii--box-string
1003 (org-export-format-code-default example-block info) info))
1006 ;;;; Export Snippet
1008 (defun org-e-ascii-export-snippet (export-snippet contents info)
1009 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1010 CONTENTS is nil. INFO is a plist holding contextual information."
1011 (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
1012 (org-element-property :value export-snippet)))
1015 ;;;; Export Block
1017 (defun org-e-ascii-export-block (export-block contents info)
1018 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1019 CONTENTS is nil. INFO is a plist holding contextual information."
1020 (when (string= (org-element-property :type export-block) "ASCII")
1021 (org-remove-indentation (org-element-property :value export-block))))
1024 ;;;; Fixed Width
1026 (defun org-e-ascii-fixed-width (fixed-width contents info)
1027 "Transcode a FIXED-WIDTH element from Org to ASCII.
1028 CONTENTS is nil. INFO is a plist holding contextual information."
1029 (org-e-ascii--box-string
1030 (org-remove-indentation
1031 (org-element-property :value fixed-width)) info))
1034 ;;;; Footnote Definition
1036 ;; Footnote Definitions are ignored. They are compiled at the end of
1037 ;; the document, by `org-e-ascii-template'.
1040 ;;;; Footnote Reference
1042 (defun org-e-ascii-footnote-reference (footnote-reference contents info)
1043 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1044 CONTENTS is nil. INFO is a plist holding contextual information."
1045 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1048 ;;;; Headline
1050 (defun org-e-ascii-headline (headline contents info)
1051 "Transcode an HEADLINE element from Org to ASCII.
1052 CONTENTS holds the contents of the headline. INFO is a plist
1053 holding contextual information."
1054 ;; Don't export footnote section, which will be handled at the end
1055 ;; of the template.
1056 (unless (org-element-property :footnote-section-p headline)
1057 (let* ((low-level-rank (org-export-low-level-p headline info))
1058 (width (org-e-ascii--current-text-width headline info))
1059 ;; Blank lines between headline and its contents.
1060 ;; `org-e-ascii-headline-spacing', when set, overwrites
1061 ;; original buffer's spacing.
1062 (pre-blanks
1063 (make-string
1064 (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
1065 (org-element-property :pre-blank headline)) ?\n))
1066 ;; Even if HEADLINE has no section, there might be some
1067 ;; links in its title that we shouldn't forget to describe.
1068 (links
1069 (unless (or (eq (caar (org-element-contents headline)) 'section))
1070 (let ((title (org-element-property :title headline)))
1071 (when (consp title)
1072 (org-e-ascii--describe-links
1073 (org-e-ascii--unique-links title info) width info))))))
1074 ;; Deep subtree: export it as a list item.
1075 (if low-level-rank
1076 (concat
1077 ;; Bullet.
1078 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1079 org-e-ascii-bullets))))
1080 (char-to-string
1081 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1083 ;; Title.
1084 (org-e-ascii--build-title headline info width) "\n"
1085 ;; Contents, indented by length of bullet.
1086 pre-blanks
1087 (org-e-ascii--indent-string
1088 (concat contents
1089 (when (org-string-nw-p links) (concat "\n\n" links)))
1091 ;; Else: Standard headline.
1092 (concat
1093 (org-e-ascii--build-title headline info width 'underline)
1094 "\n" pre-blanks
1095 (concat (when (org-string-nw-p links) links) contents))))))
1098 ;;;; Horizontal Rule
1100 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
1101 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1102 CONTENTS is nil. INFO is a plist holding contextual
1103 information."
1104 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info))
1105 (spec-width
1106 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1107 (org-e-ascii--justify-string
1108 (make-string (if (wholenump spec-width) spec-width text-width)
1109 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1110 text-width 'center)))
1113 ;;;; Inline Babel Call
1115 ;; Inline Babel Calls are ignored.
1118 ;;;; Inline Src Block
1120 (defun org-e-ascii-inline-src-block (inline-src-block contents info)
1121 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1122 CONTENTS holds the contents of the item. INFO is a plist holding
1123 contextual information."
1124 (format org-e-ascii-verbatim-format
1125 (org-element-property :value inline-src-block)))
1128 ;;;; Inlinetask
1130 (defun org-e-ascii-inlinetask (inlinetask contents info)
1131 "Transcode an INLINETASK element from Org to ASCII.
1132 CONTENTS holds the contents of the block. INFO is a plist
1133 holding contextual information."
1134 (let ((width (org-e-ascii--current-text-width inlinetask info)))
1135 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1136 ;; with appropriate arguments.
1137 (if (functionp org-e-ascii-format-inlinetask-function)
1138 (funcall org-e-ascii-format-inlinetask-function
1139 ;; todo.
1140 (and (plist-get info :with-todo-keywords)
1141 (let ((todo (org-element-property
1142 :todo-keyword inlinetask)))
1143 (and todo (org-export-data todo info))))
1144 ;; todo-type
1145 (org-element-property :todo-type inlinetask)
1146 ;; priority
1147 (and (plist-get info :with-priority)
1148 (org-element-property :priority inlinetask))
1149 ;; title
1150 (org-export-data (org-element-property :title inlinetask) info)
1151 ;; tags
1152 (and (plist-get info :with-tags)
1153 (org-element-property :tags inlinetask))
1154 ;; contents and width
1155 contents width)
1156 ;; Otherwise, use a default template.
1157 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1158 (org-e-ascii--indent-string
1159 (concat
1160 ;; Top line, with an additional blank line if not in UTF-8.
1161 (make-string width (if utf8p ?━ ?_)) "\n"
1162 (unless utf8p (concat (make-string width ? ) "\n"))
1163 ;; Add title. Fill it if wider than inlinetask.
1164 (let ((title (org-e-ascii--build-title inlinetask info width)))
1165 (if (<= (length title) width) title
1166 (org-e-ascii--fill-string title width info)))
1167 "\n"
1168 ;; If CONTENTS is not empty, insert it along with
1169 ;; a separator.
1170 (when (org-string-nw-p contents)
1171 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1172 ;; Bottom line.
1173 (make-string width (if utf8p ?━ ?_)))
1174 ;; Flush the inlinetask to the right.
1175 (- org-e-ascii-text-width org-e-ascii-global-margin
1176 (if (not (org-export-get-parent-headline inlinetask)) 0
1177 org-e-ascii-inner-margin)
1178 (org-e-ascii--current-text-width inlinetask info)))))))
1180 ;;;; Italic
1182 (defun org-e-ascii-italic (italic contents info)
1183 "Transcode italic from Org to ASCII.
1184 CONTENTS is the text with italic markup. INFO is a plist holding
1185 contextual information."
1186 (format "/%s/" contents))
1189 ;;;; Item
1191 (defun org-e-ascii-item (item contents info)
1192 "Transcode an ITEM element from Org to ASCII.
1193 CONTENTS holds the contents of the item. INFO is a plist holding
1194 contextual information."
1195 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1196 (checkbox (org-e-ascii--checkbox item info))
1197 (list-type (org-element-property :type (org-export-get-parent item)))
1198 (bullet
1199 ;; First parent of ITEM is always the plain-list. Get
1200 ;; `:type' property from it.
1201 (org-list-bullet-string
1202 (case list-type
1203 (descriptive
1204 (concat checkbox
1205 (org-export-data (org-element-property :tag item) info)
1206 ": "))
1207 (ordered
1208 ;; Return correct number for ITEM, paying attention to
1209 ;; counters.
1210 (let* ((struct (org-element-property :structure item))
1211 (bul (org-element-property :bullet item))
1212 (num (number-to-string
1213 (car (last (org-list-get-item-number
1214 (org-element-property :begin item)
1215 struct
1216 (org-list-prevs-alist struct)
1217 (org-list-parents-alist struct)))))))
1218 (replace-regexp-in-string "[0-9]+" num bul)))
1219 (t (let ((bul (org-element-property :bullet item)))
1220 ;; Change bullets into more visible form if UTF-8 is active.
1221 (if (not utf8p) bul
1222 (replace-regexp-in-string
1223 "-" "•"
1224 (replace-regexp-in-string
1225 "+" "⁃"
1226 (replace-regexp-in-string "*" "‣" bul))))))))))
1227 (concat
1228 bullet
1229 (unless (eq list-type 'descriptive) checkbox)
1230 ;; Contents: Pay attention to indentation. Note: check-boxes are
1231 ;; already taken care of at the paragraph level so they don't
1232 ;; interfere with indentation.
1233 (let ((contents (org-e-ascii--indent-string contents (length bullet))))
1234 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1235 (org-trim contents)
1236 (concat "\n" contents))))))
1239 ;;;; Keyword
1241 (defun org-e-ascii-keyword (keyword contents info)
1242 "Transcode a KEYWORD element from Org to ASCII.
1243 CONTENTS is nil. INFO is a plist holding contextual
1244 information."
1245 (let ((key (org-element-property :key keyword))
1246 (value (org-element-property :value keyword)))
1247 (cond
1248 ((string= key "ASCII") value)
1249 ((string= key "TOC")
1250 (let ((value (downcase value)))
1251 (cond
1252 ((string-match "\\<headlines\\>" value)
1253 (let ((depth (or (and (string-match "[0-9]+" value)
1254 (string-to-number (match-string 0 value)))
1255 (plist-get info :with-toc))))
1256 (org-e-ascii--build-toc
1257 info (and (wholenump depth) depth) keyword)))
1258 ((string= "tables" value)
1259 (org-e-ascii--list-tables keyword info))
1260 ((string= "listings" value)
1261 (org-e-ascii--list-listings keyword info))))))))
1264 ;;;; Latex Environment
1266 (defun org-e-ascii-latex-environment (latex-environment contents info)
1267 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1268 CONTENTS is nil. INFO is a plist holding contextual
1269 information."
1270 (org-remove-indentation (org-element-property :value latex-environment)))
1273 ;;;; Latex Fragment
1275 (defun org-e-ascii-latex-fragment (latex-fragment contents info)
1276 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1277 CONTENTS is nil. INFO is a plist holding contextual
1278 information."
1279 (org-element-property :value latex-fragment))
1282 ;;;; Line Break
1284 (defun org-e-ascii-line-break (line-break contents info)
1285 "Transcode a LINE-BREAK object from Org to ASCII.
1286 CONTENTS is nil. INFO is a plist holding contextual
1287 information." hard-newline)
1290 ;;;; Link
1292 (defun org-e-ascii-link (link desc info)
1293 "Transcode a LINK object from Org to ASCII.
1295 DESC is the description part of the link, or the empty string.
1296 INFO is a plist holding contextual information."
1297 (let ((raw-link (org-element-property :raw-link link))
1298 (type (org-element-property :type link)))
1299 (cond
1300 ((string= type "coderef")
1301 (let ((ref (org-element-property :path link)))
1302 (format (org-export-get-coderef-format ref desc)
1303 (org-export-resolve-coderef ref info))))
1304 ;; Do not apply a special syntax on radio links. Though, use
1305 ;; transcoded target's contents as output.
1306 ((string= type "radio")
1307 (let ((destination (org-export-resolve-radio-link link info)))
1308 (when destination
1309 (org-export-data (org-element-contents destination) info))))
1310 ;; Do not apply a special syntax on fuzzy links pointing to
1311 ;; targets.
1312 ((string= type "fuzzy")
1313 (let ((destination (org-export-resolve-fuzzy-link link info)))
1314 ;; Ignore invisible "#+TARGET: path".
1315 (unless (eq (org-element-type destination) 'keyword)
1316 (if (org-string-nw-p desc) desc
1317 (when destination
1318 (let ((number
1319 (org-export-get-ordinal
1320 destination info nil 'org-e-ascii--has-caption-p)))
1321 (when number
1322 (if (atom number) (number-to-string number)
1323 (mapconcat 'number-to-string number ".")))))))))
1325 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1326 (concat
1327 (format "[%s]" desc)
1328 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
1331 ;;;; Macro
1333 (defun org-e-ascii-macro (macro contents info)
1334 "Transcode a MACRO element from Org to ASCII.
1335 CONTENTS is nil. INFO is a plist holding contextual
1336 information."
1337 (org-export-expand-macro macro info))
1340 ;;;; Paragraph
1342 (defun org-e-ascii-paragraph (paragraph contents info)
1343 "Transcode a PARAGRAPH element from Org to ASCII.
1344 CONTENTS is the contents of the paragraph, as a string. INFO is
1345 the plist used as a communication channel."
1346 (org-e-ascii--fill-string
1347 contents
1348 (org-e-ascii--current-text-width paragraph info) info))
1351 ;;;; Plain List
1353 (defun org-e-ascii-plain-list (plain-list contents info)
1354 "Transcode a PLAIN-LIST element from Org to ASCII.
1355 CONTENTS is the contents of the list. INFO is a plist holding
1356 contextual information."
1357 contents)
1360 ;;;; Plain Text
1362 (defun org-e-ascii-plain-text (text info)
1363 "Transcode a TEXT string from Org to ASCII.
1364 INFO is a plist used as a communication channel."
1365 (if (not (plist-get info :with-special-strings)) text
1366 (setq text (replace-regexp-in-string "\\\\-" "" text))
1367 (if (not (eq (plist-get info :ascii-charset) 'utf-8)) text
1368 ;; Usual replacements in utf-8 with proper option set.
1369 (replace-regexp-in-string
1370 "\\.\\.\\." "…"
1371 (replace-regexp-in-string
1372 "--" "–"
1373 (replace-regexp-in-string "---" "—" text))))))
1376 ;;;; Planning
1378 (defun org-e-ascii-planning (planning contents info)
1379 "Transcode a PLANNING element from Org to ASCII.
1380 CONTENTS is nil. INFO is a plist used as a communication
1381 channel."
1382 (mapconcat
1383 'identity
1384 (delq nil
1385 (list (let ((closed (org-element-property :closed planning)))
1386 (when closed (concat org-closed-string " "
1387 (org-translate-time closed))))
1388 (let ((deadline (org-element-property :deadline planning)))
1389 (when deadline (concat org-deadline-string " "
1390 (org-translate-time deadline))))
1391 (let ((scheduled (org-element-property :scheduled planning)))
1392 (when scheduled (concat org-scheduled-string " "
1393 (org-translate-time scheduled))))))
1394 " "))
1397 ;;;; Property Drawer
1399 ;; Property drawers are ignored.
1402 ;;;; Quote Block
1404 (defun org-e-ascii-quote-block (quote-block contents info)
1405 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1406 CONTENTS holds the contents of the block. INFO is a plist
1407 holding contextual information."
1408 (let ((width (org-e-ascii--current-text-width quote-block info)))
1409 (org-e-ascii--indent-string
1410 (org-remove-indentation
1411 (org-e-ascii--fill-string contents width info))
1412 org-e-ascii-quote-margin)))
1415 ;;;; Quote Section
1417 (defun org-e-ascii-quote-section (quote-section contents info)
1418 "Transcode a QUOTE-SECTION element from Org to ASCII.
1419 CONTENTS is nil. INFO is a plist holding contextual information."
1420 (let ((width (org-e-ascii--current-text-width quote-section info))
1421 (value
1422 (org-export-data
1423 (org-remove-indentation (org-element-property :value quote-section))
1424 info)))
1425 (org-e-ascii--indent-string
1426 value
1427 (+ org-e-ascii-quote-margin
1428 ;; Don't apply inner margin if parent headline is low level.
1429 (let ((headline (org-export-get-parent-headline quote-section)))
1430 (if (org-export-low-level-p headline info) 0
1431 org-e-ascii-inner-margin))))))
1434 ;;;; Radio Target
1436 (defun org-e-ascii-radio-target (radio-target contents info)
1437 "Transcode a RADIO-TARGET object from Org to ASCII.
1438 CONTENTS is the contents of the target. INFO is a plist holding
1439 contextual information."
1440 contents)
1442 ;;;; Section
1444 (defun org-e-ascii-section (section contents info)
1445 "Transcode a SECTION element from Org to ASCII.
1446 CONTENTS is the contents of the section. INFO is a plist holding
1447 contextual information."
1448 (org-e-ascii--indent-string
1449 (concat
1450 contents
1451 (when org-e-ascii-links-to-notes
1452 ;; Add list of links at the end of SECTION.
1453 (let ((links (org-e-ascii--describe-links
1454 (org-e-ascii--unique-links section info)
1455 (org-e-ascii--current-text-width section info) info)))
1456 ;; Separate list of links and section contents.
1457 (when (org-string-nw-p links) (concat "\n\n" links)))))
1458 ;; Do not apply inner margin if parent headline is low level.
1459 (let ((headline (org-export-get-parent-headline section)))
1460 (if (or (not headline) (org-export-low-level-p headline info)) 0
1461 org-e-ascii-inner-margin))))
1464 ;;;; Special Block
1466 (defun org-e-ascii-special-block (special-block contents info)
1467 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1468 CONTENTS holds the contents of the block. INFO is a plist
1469 holding contextual information."
1470 contents)
1473 ;;;; Src Block
1475 (defun org-e-ascii-src-block (src-block contents info)
1476 "Transcode a SRC-BLOCK element from Org to ASCII.
1477 CONTENTS holds the contents of the item. INFO is a plist holding
1478 contextual information."
1479 (let ((caption (org-e-ascii--build-caption src-block info)))
1480 (concat
1481 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1482 (org-e-ascii--box-string
1483 (org-export-format-code-default src-block info) info)
1484 (when (and caption (not org-e-ascii-caption-above))
1485 (concat "\n" caption)))))
1487 ;;;; Statistics Cookie
1489 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
1490 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1491 CONTENTS is nil. INFO is a plist holding contextual information."
1492 (org-element-property :value statistics-cookie))
1495 ;;;; Subscript
1497 (defun org-e-ascii-subscript (subscript contents info)
1498 "Transcode a SUBSCRIPT object from Org to ASCII.
1499 CONTENTS is the contents of the object. INFO is a plist holding
1500 contextual information."
1501 (if (org-element-property :use-brackets-p subscript)
1502 (format "_{%s}" contents)
1503 (format "_%s" contents)))
1506 ;;;; Superscript
1508 (defun org-e-ascii-superscript (superscript contents info)
1509 "Transcode a SUPERSCRIPT object from Org to ASCII.
1510 CONTENTS is the contents of the object. INFO is a plist holding
1511 contextual information."
1512 (if (org-element-property :use-brackets-p superscript)
1513 (format "_{%s}" contents)
1514 (format "_%s" contents)))
1517 ;;;; Strike-through
1519 (defun org-e-ascii-strike-through (strike-through contents info)
1520 "Transcode STRIKE-THROUGH from Org to ASCII.
1521 CONTENTS is text with strike-through markup. INFO is a plist
1522 holding contextual information."
1523 (format "+%s+" contents))
1526 ;;;; Table
1528 (defun org-e-ascii-table (table contents info)
1529 "Transcode a TABLE element from Org to ASCII.
1530 CONTENTS is the contents of the table. INFO is a plist holding
1531 contextual information."
1532 (let ((caption (org-e-ascii--build-caption table info)))
1533 (concat
1534 ;; Possibly add a caption string above.
1535 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1536 ;; Insert table. Note: "table.el" tables are left unmodified.
1537 (cond ((eq (org-element-property :type table) 'org) contents)
1538 ((and org-e-ascii-table-use-ascii-art
1539 (eq (plist-get info :ascii-charset) 'utf-8)
1540 (require 'ascii-art-to-unicode nil t))
1541 (with-temp-buffer
1542 (insert (org-remove-indentation
1543 (org-element-property :value table)))
1544 (goto-char (point-min))
1545 (aa2u)
1546 (goto-char (point-max))
1547 (skip-chars-backward " \r\t\n")
1548 (buffer-substring (point-min) (point))))
1549 (t (org-remove-indentation (org-element-property :value table))))
1550 ;; Possible add a caption string below.
1551 (when (and caption (not org-e-ascii-caption-above))
1552 (concat "\n" caption)))))
1555 ;;;; Table Cell
1557 (defun org-e-ascii--table-cell-width (table-cell info)
1558 "Return width of TABLE-CELL.
1560 INFO is a plist used as a communication channel.
1562 Width of a cell is determined either by a width cookie in the
1563 same column as the cell, or by the maximum cell's length in that
1564 column.
1566 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1567 are ignored."
1568 (or (and (not org-e-ascii-table-widen-columns)
1569 (org-export-table-cell-width table-cell info))
1570 (let* ((max-width 0)
1571 (table (org-export-get-parent-table table-cell))
1572 (specialp (org-export-table-has-special-column-p table))
1573 (col (cdr (org-export-table-cell-address table-cell info))))
1574 (org-element-map
1575 table 'table-row
1576 (lambda (row)
1577 (setq max-width
1578 (max (length
1579 (org-export-data
1580 (org-element-contents
1581 (elt (if specialp (cdr (org-element-contents row))
1582 (org-element-contents row))
1583 col))
1584 info))
1585 max-width)))
1586 info)
1587 max-width)))
1589 (defun org-e-ascii-table-cell (table-cell contents info)
1590 "Transcode a TABLE-CELL object from Org to ASCII.
1591 CONTENTS is the cell contents. INFO is a plist used as
1592 a communication channel."
1593 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1594 ;; is nil and some width cookie has set it, use that value.
1595 ;; Otherwise, compute the maximum width among transcoded data of
1596 ;; each cell in the column.
1597 (let ((width (org-e-ascii--table-cell-width table-cell info)))
1598 ;; When contents are too large, truncate them.
1599 (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
1600 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1601 ;; Align contents correctly within the cell.
1602 (let* ((indent-tabs-mode nil)
1603 (data
1604 (when contents
1605 (org-e-ascii--justify-string
1606 contents width
1607 (org-export-table-cell-alignment table-cell info)))))
1608 (setq contents (concat data (make-string (- width (length data)) ? ))))
1609 ;; Return cell.
1610 (concat (format " %s " contents)
1611 (when (memq 'right (org-export-table-cell-borders table-cell info))
1612 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1615 ;;;; Table Row
1617 (defun org-e-ascii-table-row (table-row contents info)
1618 "Transcode a TABLE-ROW element from Org to ASCII.
1619 CONTENTS is the row contents. INFO is a plist used as
1620 a communication channel."
1621 (when (eq (org-element-property :type table-row) 'standard)
1622 (let ((build-hline
1623 (function
1624 (lambda (lcorner horiz vert rcorner)
1625 (concat
1626 (apply
1627 'concat
1628 (org-element-map
1629 table-row 'table-cell
1630 (lambda (cell)
1631 (let ((width (org-e-ascii--table-cell-width cell info))
1632 (borders (org-export-table-cell-borders cell info)))
1633 (concat
1634 ;; In order to know if CELL starts the row, do
1635 ;; not compare it with the first cell in the row
1636 ;; as there might be a special column. Instead,
1637 ;; compare it with the first exportable cell,
1638 ;; obtained with `org-element-map'.
1639 (when (and (memq 'left borders)
1640 (eq (org-element-map
1641 table-row 'table-cell 'identity info t)
1642 cell))
1643 lcorner)
1644 (make-string (+ 2 width) (string-to-char horiz))
1645 (cond
1646 ((not (memq 'right borders)) nil)
1647 ((eq (car (last (org-element-contents table-row))) cell)
1648 rcorner)
1649 (t vert)))))
1650 info)) "\n"))))
1651 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1652 (borders (org-export-table-cell-borders
1653 (org-element-map table-row 'table-cell 'identity info t)
1654 info)))
1655 (concat (cond
1656 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1657 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1658 (funcall build-hline "+" "-" "+" "+")))
1659 ((memq 'above borders)
1660 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1661 (funcall build-hline "+" "-" "+" "+"))))
1662 (when (memq 'left borders) (if utf8p "│" "|"))
1663 contents "\n"
1664 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1665 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1666 (funcall build-hline "+" "-" "+" "+")))))))
1669 ;;;; Target
1671 ;; Targets are invisible.
1674 ;;;; Timestamp
1676 (defun org-e-ascii-timestamp (timestamp contents info)
1677 "Transcode a TIMESTAMP object from Org to ASCII.
1678 CONTENTS is nil. INFO is a plist holding contextual information."
1679 (let ((value (org-translate-time (org-element-property :value timestamp)))
1680 (range-end
1681 (org-translate-time (org-element-property :range-end timestamp)))
1682 (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1683 (concat value
1684 (when range-end (concat (if utf8p "–" "--") range-end)))))
1687 ;;;; Underline
1689 (defun org-e-ascii-underline (underline contents info)
1690 "Transcode UNDERLINE from Org to ASCII.
1691 CONTENTS is the text with underline markup. INFO is a plist
1692 holding contextual information."
1693 (format "_%s_" contents))
1696 ;;;; Verbatim
1698 (defun org-e-ascii-verbatim (verbatim contents info)
1699 "Return a VERBATIM object from Org to ASCII.
1700 CONTENTS is nil. INFO is a plist holding contextual information."
1701 (format org-e-ascii-verbatim-format
1702 (org-element-property :value verbatim)))
1705 ;;;; Verse Block
1707 (defun org-e-ascii-verse-block (verse-block contents info)
1708 "Transcode a VERSE-BLOCK element from Org to ASCII.
1709 CONTENTS is verse block contents. INFO is a plist holding
1710 contextual information."
1711 (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
1712 (org-e-ascii--indent-string
1713 (org-e-ascii--justify-string contents verse-width 'left)
1714 org-e-ascii-quote-margin)))
1717 ;;; Filter
1719 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
1720 "Filter controlling number of blank lines after an headline.
1722 HEADLINE is a string representing a transcoded headline.
1723 BACK-END is symbol specifying back-end used for export. INFO is
1724 plist containing the communication channel.
1726 This function only applies to `e-ascii' back-end. See
1727 `org-e-ascii-headline-spacing' for information.
1729 For any other back-end, HEADLINE is returned as-is."
1730 (if (not org-e-ascii-headline-spacing) headline
1731 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
1732 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1736 ;;; Interactive function
1738 ;;;###autoload
1739 (defun org-e-ascii-export-as-ascii
1740 (&optional subtreep visible-only body-only ext-plist)
1741 "Export current buffer to a text buffer.
1743 If narrowing is active in the current buffer, only export its
1744 narrowed part.
1746 If a region is active, export that region.
1748 When optional argument SUBTREEP is non-nil, export the sub-tree
1749 at point, extracting information from the headline properties
1750 first.
1752 When optional argument VISIBLE-ONLY is non-nil, don't export
1753 contents of hidden elements.
1755 When optional argument BODY-ONLY is non-nil, strip title, table
1756 of contents and footnote definitions from output.
1758 EXT-PLIST, when provided, is a property list with external
1759 parameters overriding Org default settings, but still inferior to
1760 file-local settings.
1762 Export is done in a buffer named \"*Org E-ASCII Export*\", which
1763 will be displayed when `org-export-show-temporary-export-buffer'
1764 is non-nil."
1765 (interactive)
1766 (let ((outbuf (org-export-to-buffer
1767 'e-ascii "*Org E-ASCII Export*"
1768 subtreep visible-only body-only ext-plist)))
1769 (with-current-buffer outbuf (text-mode))
1770 (when org-export-show-temporary-export-buffer
1771 (switch-to-buffer-other-window outbuf))))
1773 ;;;###autoload
1774 (defun org-e-ascii-export-to-ascii
1775 (&optional subtreep visible-only body-only ext-plist pub-dir)
1776 "Export current buffer to a text file.
1778 If narrowing is active in the current buffer, only export its
1779 narrowed part.
1781 If a region is active, export that region.
1783 When optional argument SUBTREEP is non-nil, export the sub-tree
1784 at point, extracting information from the headline properties
1785 first.
1787 When optional argument VISIBLE-ONLY is non-nil, don't export
1788 contents of hidden elements.
1790 When optional argument BODY-ONLY is non-nil, strip title, table
1791 of contents and footnote definitions from output.
1793 EXT-PLIST, when provided, is a property list with external
1794 parameters overriding Org default settings, but still inferior to
1795 file-local settings.
1797 When optional argument PUB-DIR is set, use it as the publishing
1798 directory.
1800 Return output file's name."
1801 (interactive)
1802 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
1803 (org-export-to-file
1804 'e-ascii outfile subtreep visible-only body-only ext-plist)))
1807 (provide 'org-e-ascii)
1808 ;;; org-e-ascii.el ends here