1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements an ASCII back-end for Org generic exporter.
27 ;; M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
29 ;; in an Org mode buffer then switch to that buffer to see the ASCII
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
35 (eval-when-compile (require 'cl
))
37 (declare-function org-element-get-contents
"org-element" (element))
38 (declare-function org-element-get-property
"org-element" (property element
))
39 (declare-function org-element-map
"org-element"
40 (data types fun
&optional info first-match
))
41 (declare-function org-element-time-stamp-interpreter
42 "org-element" (time-stamp contents
))
44 (declare-function org-export-clean-table
"org-export" (table specialp
))
45 (declare-function org-export-collect-footnote-definitions
46 "org-export" (data info
))
47 (declare-function org-export-collect-headlines
"org-export" (info &optional n
))
48 (declare-function org-export-collect-listings
"org-export" (info))
49 (declare-function org-export-collect-tables
"org-export" (info))
50 (declare-function org-export-data
"org-export" (data backend info
))
51 (declare-function org-export-expand-macro
"org-export" (macro info
))
52 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
53 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
54 (declare-function org-export-get-headline-number
"org-export" (headline info
))
55 (declare-function org-export-get-ordinal
"org-export"
56 (element info
&optional types within-section predicate
))
57 (declare-function org-export-get-parent-headline
"org-export" (blob info
))
58 (declare-function org-export-get-relative-level
"org-export" (headline info
))
59 (declare-function org-export-handle-code
60 "org-export" (element info
&optional num-fmt ref-fmt delayed
))
61 (declare-function org-export-included-file
"org-export" (keyword backend info
))
62 (declare-function org-export-low-level-p
"org-export" (headline info
))
63 (declare-function org-export-output-file-name
"org-export"
64 (extension &optional subtreep pub-dir
))
65 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
66 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
67 (declare-function org-export-resolve-id-link
"org-export" (link info
))
68 (declare-function org-export-secondary-string
69 "org-export" (secondary backend info
))
70 (declare-function org-export-table-format-info
"org-export" (table))
72 org-export-to-file
"org-export"
73 (backend file
&optional subtreep visible-only body-only ext-plist
))
77 ;;; Internal Variables
79 ;; The following setting won't allow to modify preferred charset
80 ;; through a buffer keyword or an option item, but, since the property
81 ;; will appear in communication channel nonetheless, it allows to
82 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
85 ;; We also install a developer filter for headlines, in order to
86 ;; control blank lines in output string.
88 (defconst org-e-ascii-option-alist
89 '((:ascii-charset nil nil org-e-ascii-charset
)
90 (:filter-headline nil nil
(cons
91 'org-e-ascii-filter-headline-blank-lines
92 org-export-filter-headline-functions
)))
93 "Alist between ASCII export properties and ways to set them.
94 See `org-export-option-alist' for more information on the
95 structure or the values.")
97 (defconst org-e-ascii-dictionary
101 :latin1
"Footnotes\n"
102 :utf-8
"Footnotes\n")
104 :ascii
"Notes de bas de page\n"
105 :latin1
"Notes de bas de page\n"
106 :utf-8
"Notes de bas de page\n"))
109 :ascii
"Listing %d: %s"
110 :latin1
"Listing %d: %s"
111 :utf-8
"Listing %d: %s")
113 :ascii
"Programme %d : %s"
114 :latin1
"Programme %d : %s"
115 :utf-8
"Programme nº %d : %s"))
116 ("List Of Listings\n"
118 :ascii
"List Of Listings\n"
119 :latin1
"List Of Listings\n"
120 :utf-8
"List Of Listings\n")
122 :ascii
"Liste des programmes\n"
123 :latin1
"Liste des programmes\n"
124 :utf-8
"Liste des programmes\n"))
127 :ascii
"List Of Tables\n"
128 :latin1
"List Of Tables\n"
129 :utf-8
"List Of Tables\n")
131 :ascii
"Liste des tableaux\n"
132 :latin1
"Liste des tableaux\n"
133 :utf-8
"Liste des tableaux\n"))
136 :ascii
"Listing %d: "
137 :latin1
"Listing %d: "
138 :utf-8
"Listing %d: ")
140 :ascii
"Programme %d : "
141 :latin1
"Programme %d : "
142 :utf-8
"Programme nº %d : "))
143 ("Table Of Contents\n"
145 :ascii
"Table Of Contents\n"
146 :latin1
"Table Of Contents\n"
147 :utf-8
"Table Of Contents\n")
150 :latin1
"Table des matières\n"
151 :utf-8
"Table des matières\n"))
154 :ascii
"Table %d: %s"
155 :latin1
"Table %d: %s"
156 :utf-8
"Table %d: %s")
158 :ascii
"Tableau %d : %s"
159 :latin1
"Tableau %d : %s"
160 :utf-8
"Tableau nº %d : %s"))
163 :ascii
"See section %s"
164 :latin1
"See section %s"
165 :utf-8
"See section %s")
167 :ascii
"cf. section %s"
168 :latin1
"cf. section %s"
169 :utf-8
"cf. section %s"))
176 :ascii
"Tableau %d : "
177 :latin1
"Tableau %d : "
178 :utf-8
"Tableau nº %d : "))
181 :ascii
"Unknown reference"
182 :latin1
"Unknown reference"
183 :utf-8
"Unknown reference")
185 :ascii
"Destination inconnue"
186 :latin1
"Référence inconnue"
187 :utf-8
"Référence inconnue")))
188 "Dictionary for ASCII back-end.
190 Alist whose car is the string to translate and cdr is an alist
191 whose car is the language string and cdr is a plist whose
192 properties are possible charsets and value the translated term.
194 It is used as a database for `org-e-ascii--translate'.")
198 ;;; User Configurable Variables
200 (defgroup org-export-e-ascii nil
201 "Options for exporting Org mode files to ASCII."
202 :tag
"Org Export ASCII"
205 (defcustom org-e-ascii-text-width
72
206 "Maximum width of exported text.
207 This number includes margin size, as set in
208 `org-e-ascii-global-margin'."
209 :group
'org-export-e-ascii
212 (defcustom org-e-ascii-global-margin
0
213 "Width of the left margin, in number of characters."
214 :group
'org-export-e-ascii
217 (defcustom org-e-ascii-inner-margin
2
218 "Width of the inner margin, in number of characters.
219 Inner margin is applied between each headline."
220 :group
'org-export-e-ascii
223 (defcustom org-e-ascii-quote-margin
6
224 "Width of margin used for quoting text, in characters.
225 This margin is applied on both sides of the text."
226 :group
'org-export-e-ascii
229 (defcustom org-e-ascii-inlinetask-width
30
230 "Width of inline tasks, in number of characters.
231 This number ignores any margin."
232 :group
'org-export-e-ascii
235 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
236 "Number of blank lines inserted around headlines.
238 This variable can be set to a cons cell. In that case, its car
239 represents the number of blank lines present before headline
240 contents whereas its cdr reflects the number of blank lines after
243 A nil value replicates the number of blank lines found in the
244 original Org buffer at the same place."
245 :group
'org-export-e-ascii
247 (const :tag
"Replicate original spacing" nil
)
248 (cons :tag
"Set an uniform spacing"
249 (integer :tag
"Number of blank lines before contents")
250 (integer :tag
"Number of blank lines after contents"))))
252 (defcustom org-e-ascii-charset
'ascii
253 "The charset allowed to represent various elements and objects.
255 `ascii' Only use plain ASCII characters
256 `latin1' Include Latin-1 characters
257 `utf-8' Use all UTF-8 characters"
258 :group
'org-export-e-ascii
260 (const :tag
"ASCII" ascii
)
261 (const :tag
"Latin-1" latin1
)
262 (const :tag
"UTF-8" utf-8
)))
264 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
266 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
267 "Characters for underlining headings in ASCII export.
269 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
270 and whose value is a list of characters.
272 For each supported charset, this variable associates a sequence
273 of underline characters. In a sequence, the characters will be
274 used in order for headlines level 1, 2, ... If no character is
275 available for a given level, the headline won't be underlined."
276 :group
'org-export-e-ascii
278 (cons :tag
"Underline characters sequence"
279 (const :tag
"ASCII charset" ascii
)
281 (cons :tag
"Underline characters sequence"
282 (const :tag
"Latin-1 charset" latin1
)
284 (cons :tag
"Underline characters sequence"
285 (const :tag
"UTF-8 charset" utf-8
)
286 (repeat character
))))
288 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
291 "Bullet characters for headlines converted to lists in ASCII export.
293 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
294 and whose value is a list of characters.
296 The first character is used for the first level considered as low
297 level, and so on. If there are more levels than characters given
298 here, the list will be repeated.
300 Note that this variable doesn't affect plain lists
302 :group
'org-export-e-ascii
304 (cons :tag
"Bullet characters for low level headlines"
305 (const :tag
"ASCII charset" ascii
)
307 (cons :tag
"Bullet characters for low level headlines"
308 (const :tag
"Latin-1 charset" latin1
)
310 (cons :tag
"Bullet characters for low level headlines"
311 (const :tag
"UTF-8 charset" utf-8
)
312 (repeat character
))))
314 (defcustom org-e-ascii-links-to-notes t
315 "Non-nil means convert links to notes before the next headline.
316 When nil, the link will be exported in place. If the line
317 becomes long in this way, it will be wrapped."
318 :group
'org-export-e-ascii
321 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
322 "Non-nil means keep all vertical lines in ASCII tables.
323 When nil, vertical lines will be removed except for those needed
324 for column grouping."
325 :group
'org-export-e-ascii
328 (defcustom org-e-ascii-table-widen-columns t
329 "Non-nil means widen narrowed columns for export.
330 When nil, narrowed columns will look in ASCII export just like in
331 Org mode, i.e. with \"=>\" as ellipsis."
332 :group
'org-export-e-ascii
335 (defcustom org-e-ascii-caption-above nil
336 "When non-nil, place caption string before the element.
337 Otherwise, place it right after it."
338 :group
'org-export-e-ascii
341 (defcustom org-e-ascii-verbatim-format
"`%s'"
342 "Format string used for verbatim text and inline code."
343 :group
'org-export-e-ascii
346 (defcustom org-e-ascii-format-drawer-function nil
347 "Function called to format a drawer in ASCII.
349 The function must accept two parameters:
350 NAME the drawer name, like \"LOGBOOK\"
351 CONTENTS the contents of the drawer.
352 WIDTH the text width within the drawer.
354 The function should return either the string to be exported or
355 nil to ignore the drawer.
357 For example, the variable could be set to the following function
358 in order to mimic default behaviour:
360 \(defun org-e-ascii-format-drawer-default \(name contents width\)
361 \"Format a drawer element for ASCII export.\"
363 :group
'org-export-e-ascii
366 (defcustom org-e-ascii-format-inlinetask-function nil
367 "Function called to format an inlinetask in ASCII.
369 The function must accept six parameters:
370 TODO the todo keyword, as a string
371 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
372 PRIORITY the inlinetask priority, as a string
373 NAME the inlinetask name, as a string.
374 TAGS the inlinetask tags, as a string.
375 CONTENTS the contents of the inlinetask, as a string.
377 The function should return either the string to be exported or
378 nil to ignore the inline task.
380 For example, the variable could be set to the following function
381 in order to mimic default behaviour:
383 \(defun org-e-ascii-format-inlinetask-default
384 \(todo type priority name tags contents\)
385 \"Format an inline task element for ASCII export.\"
386 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
387 \(width org-e-ascii-inlinetask-width\)
388 \(org-e-ascii--indent-string
390 ;; Top line, with an additional blank line if not in UTF-8.
391 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
392 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
393 ;; Add title. Fill it if wider than inlinetask.
394 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
395 \(if \(<= \(length title\) width\) title
396 \(org-e-ascii--fill-string title width info\)\)\)
398 ;; If CONTENTS is not empty, insert it along with
400 \(when \(org-string-nw-p contents\)
401 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
403 \(make-string width \(if utf8p ?━ ?_\)\)\)
404 ;; Flush the inlinetask to the right.
405 \(- \(plist-get info :ascii-width\)
406 \(plist-get info :ascii-margin\)
407 \(plist-get info :ascii-inner-margin\)
408 \(org-e-ascii--current-text-width inlinetask info\)\)"
409 :group
'org-export-e-ascii
414 ;;; Internal Functions
416 ;; Internal functions fall into three categories.
418 ;; The first one is about text formatting. The core function is
419 ;; `org-e-ascii--current-text-width', which determines the current
420 ;; text width allowed to a given element. In other words, it helps
421 ;; keeping each line width within maximum text width defined in
422 ;; `org-e-ascii-text-width'. Once this information is known,
423 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
424 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
425 ;; operate on a given output string.
427 ;; The second category contains functions handling elements listings,
428 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
429 ;; returns a complete table of contents, `org-e-ascii--list-listings'
430 ;; returns a list of referenceable src-block elements, and
431 ;; `org-e-ascii--list-tables' does the same for table elements.
433 ;; The third category includes general helper functions.
434 ;; `org-e-ascii--build-title' creates the title for a given headline
435 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
436 ;; caption string associated to a table or a src-block.
437 ;; `org-e-ascii--describe-links' creates notes about links for
438 ;; insertion at the end of a section. It uses
439 ;; `org-e-ascii--unique-links' to get the list of links to describe.
440 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
441 ;; to internationalize output.
444 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
445 "Fill a string with specified text-width and return it.
447 S is the string being filled. TEXT-WIDTH is an integer
448 specifying maximum length of a line. INFO is the plist used as
449 a communication channel.
451 Optional argument JUSTIFY can specify any type of justification
452 among `left', `center', `right' or `full'. A nil value is
453 equivalent to `left'. For a justification that doesn't also fill
454 string, see `org-e-ascii--justify-string'.
456 Return nil if S isn't a string."
457 ;; Don't fill paragraph when break should be preserved.
458 (cond ((not (stringp s
)) nil
)
459 ((plist-get info
:preserve-breaks
) s
)
461 (let ((fill-column text-width
)
462 (use-hard-newlines t
))
464 (fill-region (point-min) (point-max) justify
))
467 (defun org-e-ascii--justify-string (s text-width how
)
469 TEXT-WIDTH is an integer specifying maximum length of a line.
470 HOW determines the type of justification: it can be `left',
471 `right', `full' or `center'."
474 (goto-char (point-min))
475 (let ((fill-column text-width
))
476 (while (< (point) (point-max))
477 (justify-current-line how
)
481 (defun org-e-ascii--indent-string (s width
)
482 "Indent string S by WIDTH white spaces.
483 Empty lines are not indented."
485 (replace-regexp-in-string
486 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
488 (defun org-e-ascii--box-string (s info
)
489 "Return string S with a partial box to its left.
490 INFO is a plist used as a communicaton channel."
491 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
492 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
493 (replace-regexp-in-string
494 "^" (if utf8p
"│ " "| ")
495 ;; Remove last newline character.
496 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
498 (defun org-e-ascii--current-text-width (element info
)
499 "Return maximum text width for ELEMENT's contents.
500 INFO is a plist used as a communication channel."
502 ;; Elements with an absolute width: `headline' and `inlinetask'.
503 ((eq (car element
) 'inlinetask
) org-e-ascii-inlinetask-width
)
504 ((eq (car element
) 'headline
)
505 (- org-e-ascii-text-width
506 (let ((low-level-rank (org-export-low-level-p element info
)))
507 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
508 ;; Elements with a relative width: store maximum text width in
511 (let* ((genealogy (cons element
(plist-get info
:genealogy
)))
512 ;; Total width is determined by the presence, or not, of an
513 ;; inline task among ELEMENT parents.
515 (if (loop for parent in genealogy
516 thereis
(eq (car parent
) 'inlinetask
))
517 org-e-ascii-inlinetask-width
518 ;; No inlinetask: Remove global margin from text width.
519 (- org-e-ascii-text-width
520 org-e-ascii-global-margin
521 (let ((parent (org-export-get-parent-headline element info
)))
522 ;; Inner margin doesn't apply to text before first
525 (let ((low-level-rank
526 (org-export-low-level-p parent info
)))
527 ;; Inner margin doesn't apply to contents of
528 ;; low level headlines, since they've got their
529 ;; own indentation mechanism.
530 (if low-level-rank
(* low-level-rank
2)
531 org-e-ascii-inner-margin
))))))))
533 ;; Each `quote-block', `quote-section' and `verse-block' above
534 ;; narrows text width by twice the standard margin size.
535 (+ (* (loop for parent in genealogy
536 when
(memq (car parent
)
537 '(quote-block quote-section verse-block
))
539 2 org-e-ascii-quote-margin
)
540 ;; Text width within a plain-list is restricted by
541 ;; indentation of current item. If that's the case,
542 ;; compute it with the help of `:structure' property from
543 ;; parent item, if any.
545 (if (eq (car element
) 'item
) element
546 (loop for parent in genealogy
547 when
(eq (car parent
) 'item
)
549 (if (not parent-item
) 0
550 ;; Compute indentation offset of the current item,
551 ;; that is the sum of the difference between its
552 ;; indentation and the indentation of the top item in
553 ;; the list and current item bullet's length. Also
554 ;; remove tag length (for description lists) or bullet
556 (let ((struct (org-element-get-property :structure parent-item
))
557 (beg-item (org-element-get-property :begin parent-item
)))
558 (+ (- (org-list-get-ind beg-item struct
)
560 (org-list-get-top-point struct
) struct
))
562 (or (org-list-get-tag beg-item struct
)
563 (org-list-get-bullet beg-item struct
)))))))))))))
565 (defun org-e-ascii--build-title
566 (element info text-width
&optional underline notags
)
567 "Format ELEMENT title and return it.
569 ELEMENT is either an `headline' or `inlinetask' element. INFO is
570 a plist used as a communication channel. TEXT-WIDTH is an
571 integer representing the maximum length of a line.
573 When optional argument UNDERLINE is non-nil, underline title,
574 without the tags, according to `org-e-ascii-underline'
577 if optional argument NOTAGS is nil, no tags will be added to the
579 (let* ((headlinep (eq (car element
) 'headline
))
581 ;; Numbering is specific to headlines.
583 ;; Section numbering must be active, and headline's
584 ;; level should be above specified limit, if any.
585 (let ((sec-num (plist-get info
:section-numbers
)))
586 (if (not (wholenump sec-num
)) sec-num
587 (<= (org-export-get-relative-level headline info
) sec-num
)))
588 ;; All tests passed: build numbering string.
592 (org-export-get-headline-number element info
) ".")
594 (text (org-export-secondary-string
595 (org-element-get-property :title element
) 'e-ascii info
))
597 (and (plist-get info
:with-todo-keywords
)
598 (let ((todo (org-element-get-property :todo-keyword element
)))
600 (concat (org-export-secondary-string todo
'e-ascii info
)
602 (tags (and (not notags
)
603 (plist-get info
:with-tags
)
604 (org-element-get-property :tags element
)))
606 (and (plist-get info
:with-priority
)
607 (concat (org-element-get-property :priority element
) " ")))
608 (first-part (concat numbers todo priority text
)))
611 ;; Align tags, if any.
615 (max (- text-width
(1+ (length first-part
))) (length tags
)))
617 ;; Maybe underline text, if ELEMENT type is `headline' and an
618 ;; underline character has been defined.
619 (when (and underline headlinep
)
621 (nth (1- (org-export-get-relative-level element info
))
622 (cdr (assq (plist-get info
:ascii-charset
)
623 org-e-ascii-underline
)))))
626 (make-string (length first-part
) under-char
))))))))
628 (defun org-e-ascii--build-caption (element info
)
629 "Return caption string for ELEMENT, if applicable.
631 INFO is a plist used as a communication channel.
633 The caption string contains the sequence number of ELEMENT if it
634 has a name affiliated keyword, along with the real caption, if
635 any. Return nil when ELEMENT has no affiliated caption or name
637 (let ((caption (org-element-get-property :caption element
))
638 (name (org-element-get-property :name element
)))
639 (when (or caption name
)
640 ;; Get sequence number of current src-block among every
641 ;; src-block with either a caption or a name.
643 (org-export-get-ordinal
645 (lambda (el) (or (org-element-get-property :caption el
)
646 (org-element-get-property :name el
)))))
647 (title-fmt (org-e-ascii--translate
649 (table "Table %d: %s")
650 (src-block "Listing %d: %s")) info
)))
651 (org-e-ascii--fill-string
654 (if (not caption
) name
655 (org-export-secondary-string (car caption
) 'e-ascii info
)))
656 (org-e-ascii--current-text-width element info
) info
)))))
658 (defun org-e-ascii--build-toc (info &optional n keyword
)
659 "Return a table of contents.
661 INFO is a plist used as a communication channel.
663 Optional argument N, when non-nil, is an integer specifying the
666 Optional argument KEYWORD specifies the TOC keyword, if any, from
667 which the table of contents generation has been initiated."
668 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
671 (make-string (1- (length title
))
672 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
675 (if keyword
(org-e-ascii--current-text-width keyword info
)
676 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
679 (let* ((level (org-export-get-relative-level headline info
))
680 (indent (* (1- level
) 3)))
682 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
683 (org-e-ascii--build-title
684 headline info
(- text-width indent
) nil
685 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
686 (org-export-collect-headlines info n
) "\n")))))
688 (defun org-e-ascii--list-listings (keyword info
)
689 "Return a list of listings.
691 KEYWORD is the keyword that initiated the list of listings
692 generation. INFO is a plist used as a communication channel."
693 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
696 (make-string (1- (length title
))
697 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
700 (if keyword
(org-e-ascii--current-text-width keyword info
)
701 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
702 ;; Use a counter instead of retreiving ordinal of each
707 ;; Store initial text so its length can be computed. This is
708 ;; used to properly align caption right to it in case of
709 ;; filling (like contents of a description list item).
711 (format (org-e-ascii--translate "Listing %d: " info
)
716 (org-e-ascii--indent-string
717 (org-e-ascii--fill-string
718 (let ((caption (org-element-get-property :caption src-block
)))
719 (if (not caption
) (org-element-get-property :name src-block
)
720 (org-export-secondary-string
721 ;; Use short name in priority, if available.
722 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
723 (- text-width
(length initial-text
)) info
)
724 (length initial-text
))))))
725 (org-export-collect-listings info
) "\n")))))
727 (defun org-e-ascii--list-tables (keyword info
)
728 "Return a list of listings.
730 KEYWORD is the keyword that initiated the list of listings
731 generation. INFO is a plist used as a communication channel."
732 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
735 (make-string (1- (length title
))
736 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
739 (if keyword
(org-e-ascii--current-text-width keyword info
)
740 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
741 ;; Use a counter instead of retreiving ordinal of each
746 ;; Store initial text so its length can be computed. This is
747 ;; used to properly align caption right to it in case of
748 ;; filling (like contents of a description list item).
750 (format (org-e-ascii--translate "Table %d: " info
)
755 (org-e-ascii--indent-string
756 (org-e-ascii--fill-string
757 (let ((caption (org-element-get-property :caption table
)))
758 (if (not caption
) (org-element-get-property :name table
)
759 ;; Use short name in priority, if available.
760 (org-export-secondary-string
761 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
762 (- text-width
(length initial-text
)) info
)
763 (length initial-text
))))))
764 (org-export-collect-tables info
) "\n")))))
766 (defun org-e-ascii--unique-links (element info
)
767 "Return a list of unique link references in ELEMENT.
769 ELEMENT is either an headline element or a section element. INFO
770 is a plist used as a communication channel.
772 It covers links that may be found current headline's title, in
773 the following section and in any inlinetask's title there."
777 ;; Return LINK if it wasn't referenced so far, or nil.
778 ;; Update SEEN links along the way.
781 (cons (org-element-get-property :raw-link link
)
782 (org-element-get-contents link
))))
783 (unless (member footprint seen
)
784 (push footprint seen
) link
)))))
785 (harvest-links-in-title
787 ;; Return a list of all unique links in ELEMENT. ELEMENT
788 ;; may be an headline or an inlinetask element.
791 (dolist (obj (org-element-get-property :title element
) acc
)
792 (when (and (listp obj
) (eq (car obj
) 'link
))
793 (let ((link (funcall unique-link-p obj
)))
794 (and link
(push link acc
)))))))))
795 ;; Retrieve headline's section, if it exists.
796 (section (if (eq (car element
) 'section
) element
797 (let ((sec (car (org-element-get-contents element
))))
798 (and (eq (car sec
) 'section
) sec
))))
799 (headline (if (eq (car element
) 'headline
) element
800 (org-export-get-parent-headline element info
))))
802 ;; Links that may be in HEADLINE's title.
803 (funcall harvest-links-in-title headline
)
804 ;; Get all links in SECTION.
806 section
'link
(lambda (link local
) (funcall unique-link-p link
)) info
)
807 ;; Links that may be in inlinetasks titles within SECTION.
811 (lambda (inlinetask local
)
812 (push (funcall harvest-links-in-title inlinetask
) acc
))
816 (defun org-e-ascii--describe-links (links width info
)
817 "Return a string describing a list of links.
819 LINKS is a list of link type objects, as returned by
820 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
821 the output string. INFO is a plist used as a communication
825 (let ((type (org-element-get-property :type link
))
826 (anchor (let ((desc (org-element-get-contents link
)))
828 (org-element-get-property :raw-link link
)
829 (org-export-secondary-string desc
'e-ascii info
)))))
831 ;; Coderefs and radio links are ignored.
832 ((member type
'("coderef" "radio")) nil
)
833 ;; Id, custom-id and fuzzy links (with the exception of
834 ;; targets): Headlines refer to their numbering.
835 ((member type
'("custom-id" "fuzzy" "id"))
836 (let ((destination (if (string= type
"fuzzy")
837 (org-export-resolve-fuzzy-link link info
)
838 (org-export-resolve-id-link link info
))))
839 (unless (eq (car destination
) 'target
)
841 (org-e-ascii--fill-string
845 (if (not destination
)
846 (org-e-ascii--translate "Unknown reference" info
)
848 (org-e-ascii--translate "See section %s" info
)
849 (mapconcat 'number-to-string
850 (org-export-get-headline-number destination info
)
852 width info
) "\n\n"))))
853 ;; Do not add a link that cannot be resolved and doesn't have
854 ;; any description: destination is already visible in the
856 ((not (org-element-get-contents link
)) nil
)
859 (org-e-ascii--fill-string
860 (format "[%s] %s" anchor
(org-element-get-property :raw-link link
))
869 (defun org-e-ascii-template--document-title (info)
870 "Return document title, as a string.
871 INFO is a plist used as a communication channel."
872 (let ((text-width org-e-ascii-text-width
)
873 (title (org-export-secondary-string
874 (plist-get info
:title
) 'e-ascii info
))
876 (and (plist-get info
:with-author
)
877 (let ((auth (plist-get info
:author
)))
878 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
880 (and (plist-get info
:with-email
)
881 (org-export-secondary-string
882 (plist-get info
:email
) 'e-ascii info
)))
883 (date (plist-get info
:date
)))
884 ;; There are two types of title blocks depending on the presence
885 ;; of a title to display.
886 (if (string= title
"")
887 ;; Title block without a title. DATE is positioned at the top
888 ;; right of the document, AUTHOR to the top left and EMAIL
891 ((and (org-string-nw-p date
) (org-string-nw-p author
))
894 (make-string (- text-width
(length date
) (length author
)) ?
)
896 (when (org-string-nw-p email
) (concat "\n" email
))
898 ((and (org-string-nw-p date
) (org-string-nw-p email
))
901 (make-string (- text-width
(length date
) (length email
)) ?
)
903 ((org-string-nw-p date
)
905 (org-e-ascii--justify-string date text-width
'right
)
907 ((and (org-string-nw-p author
) (org-string-nw-p email
))
908 (concat author
"\n" email
"\n\n\n"))
909 ((org-string-nw-p author
) (concat author
"\n\n\n"))
910 ((org-string-nw-p email
) (concat email
"\n\n\n")))
911 ;; Title block with a title. Document's TITLE, along with the
912 ;; AUTHOR and its EMAIL are both overlined and an underlined,
913 ;; centered. Date is just below, also centered.
914 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
915 ;; Format TITLE. It may be filled if it is too wide,
916 ;; that is wider than the two thirds of the total width.
917 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
918 (formatted-title (org-e-ascii--fill-string title title-len info
))
921 (min (+ (max title-len
(length author
) (length email
)) 2)
922 text-width
) (if utf8p ?━ ?_
))))
923 (org-e-ascii--justify-string
926 (upcase formatted-title
)
927 (if utf8p
"\n\n\n" "\n\n")
929 ((and (org-string-nw-p author
) (org-string-nw-p email
))
930 (concat author
"\n" email
))
931 ((org-string-nw-p author
) author
)
932 ((org-string-nw-p email
) email
))
934 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
935 "\n\n\n") text-width
'center
)))))
937 (defun org-e-ascii-template (contents info
)
938 "Return complete document string after ASCII conversion.
939 CONTENTS is the transcoded contents string. INFO is a plist
940 holding export options."
941 (org-e-ascii--indent-string
942 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
943 ;; 1. Build title block.
945 (org-e-ascii-template--document-title info
)
946 ;; 2. Table of contents.
947 (let ((depth (plist-get info
:with-toc
)))
950 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
952 ;; 3. Document's body.
954 ;; 4. Footnote definitions.
955 (let ((definitions (org-export-collect-footnote-definitions
956 (plist-get info
:parse-tree
) info
))
957 ;; Insert full links right inside the footnote definition
958 ;; as they have no chance to be inserted later.
959 (org-e-ascii-links-to-notes nil
))
963 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
968 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
972 (let ((id (format "[%s] " (car ref
))))
973 ;; Distinguish between inline definitions and
974 ;; full-fledged definitions.
976 (let ((def (nth 2 ref
)))
977 (if (eq (car def
) 'org-data
)
978 ;; Full-fledged definition: footnote ID is
979 ;; inserted inside the first parsed paragraph
980 ;; (FIRST), if any, to be sure filling will
981 ;; take it into consideration.
982 (let ((first (car (org-element-get-contents def
))))
983 (if (not (eq (car first
) 'paragraph
))
984 (concat id
"\n" (org-export-data def
'e-ascii info
))
985 (push id
(nthcdr 2 first
))
986 (org-export-data def
'e-ascii info
)))
987 ;; Fill paragraph once footnote ID is inserted in
988 ;; order to have a correct length for first line.
989 (org-e-ascii--fill-string
990 (concat id
(org-export-secondary-string def
'e-ascii info
))
991 text-width info
))))))
992 definitions
"\n\n"))))
993 ;; 5. Creator. Ignore `comment' value as there are no comments in
994 ;; ASCII. Justify it to the bottom right.
995 (let ((creator-info (plist-get info
:with-creator
)))
996 (unless (or (not creator-info
) (eq creator-info
'comment
))
999 (org-e-ascii--fill-string
1000 (plist-get info
:creator
) text-width info
'right
))))))
1001 org-e-ascii-global-margin
))
1003 (defun org-e-ascii--translate (s info
)
1004 "Translate string S.
1006 INFO is a plist used as a communication channel.
1008 Translation depends on `:language' property and allowed charset.
1009 If no translation in found for a given language and a given
1010 charset, fall-back to S."
1011 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
1012 (lang (plist-get info
:language
))
1013 (translations (cdr (assoc s org-e-ascii-dictionary
))))
1014 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
1018 ;;; Transcode Functions
1022 ;; Babel Calls are ignored.
1027 (defun org-e-ascii-center-block (center-block contents info
)
1028 "Transcode a CENTER-BLOCK element from Org to ASCII.
1029 CONTENTS holds the contents of the block. INFO is a plist
1030 holding contextual information."
1031 (org-e-ascii--justify-string
1032 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1037 ;; Comments are ignored.
1042 ;; Comment Blocks are ignored.
1047 (defun org-e-ascii-drawer (drawer contents info
)
1048 "Transcode a DRAWER element from Org to ASCII.
1049 CONTENTS holds the contents of the block. INFO is a plist
1050 holding contextual information."
1051 (let ((name (org-element-get-property :drawer-name drawer
))
1052 (width (org-e-ascii--current-text-width drawer info
)))
1053 (if (functionp org-e-ascii-format-drawer-function
)
1054 (funcall org-e-ascii-format-drawer-function name contents width
)
1055 ;; If there's no user defined function: simply
1056 ;; display contents of the drawer.
1062 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1063 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1064 CONTENTS holds the contents of the block. INFO is a plist
1065 holding contextual information. See
1072 (defun org-e-ascii-emphasis (emphasis contents info
)
1073 "Transcode EMPHASIS from Org to ASCII.
1074 CONTENTS is the contents of the emphasized text. INFO is a plist
1075 holding contextual information.."
1076 (let ((marker (org-element-get-property :marker emphasis
)))
1077 ;; Leave emphasis markers as-is.
1078 (concat marker contents marker
)))
1083 (defun org-e-ascii-entity (entity contents info
)
1084 "Transcode an ENTITY object from Org to ASCII.
1085 CONTENTS are the definition itself. INFO is a plist holding
1086 contextual information."
1087 (org-element-get-property
1088 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1094 (defun org-e-ascii-example-block (example-block contents info
)
1095 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1096 CONTENTS is nil. INFO is a plist holding contextual information."
1097 (org-e-ascii--box-string (org-export-handle-code example-block info
) info
))
1102 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1103 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1104 CONTENTS is nil. INFO is a plist holding contextual information."
1105 (org-element-get-property :value export-snippet
))
1110 (defun org-e-ascii-export-block (export-block contents info
)
1111 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1112 CONTENTS is nil. INFO is a plist holding contextual information."
1113 (when (string= (org-element-get-property :type export-block
) "ascii")
1114 (org-remove-indentation (org-element-get-property :value export-block
))))
1119 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1120 "Transcode a FIXED-WIDTH element from Org to ASCII.
1121 CONTENTS is nil. INFO is a plist holding contextual information."
1122 (org-e-ascii--box-string
1123 (replace-regexp-in-string
1124 "^[ \t]*: ?" "" (org-element-get-property :value fixed-width
)) info
))
1127 ;;;; Footnote Definition
1129 ;; Footnote Definitions are ignored. They are compiled at the end of
1130 ;; the document, by `org-e-ascii-template'.
1133 ;;;; Footnote Reference
1135 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1136 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1137 CONTENTS is nil. INFO is a plist holding contextual information."
1138 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1143 (defun org-e-ascii-headline (headline contents info
)
1144 "Transcode an HEADLINE element from Org to ASCII.
1145 CONTENTS holds the contents of the headline. INFO is a plist
1146 holding contextual information."
1147 ;; Don't export footnote section, which will be handled at the end
1149 (unless (org-element-get-property :footnote-section-p headline
)
1150 (let* ((low-level-rank (org-export-low-level-p headline info
))
1151 (width (org-e-ascii--current-text-width headline info
))
1152 ;; Blank lines between headline and its contents.
1153 ;; `org-e-ascii-headline-spacing', when set, overwrites
1154 ;; original buffer's spacing.
1157 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1158 (org-element-get-property :pre-blank headline
)) ?
\n))
1159 ;; Even if HEADLINE has no section, there might be some
1160 ;; links in its title that we shouldn't forget to describe.
1162 (unless (eq (caar (org-element-get-contents headline
)) 'section
)
1163 (org-e-ascii--describe-links
1164 (org-e-ascii--unique-links headline info
) width info
))))
1165 ;; Deep subtree: export it as a list item.
1169 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1170 org-e-ascii-bullets
))))
1172 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1175 (org-e-ascii--build-title headline info width
) "\n"
1176 ;; Contents, indented by length of bullet.
1178 (org-e-ascii--indent-string
1180 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1182 ;; Else: Standard headline.
1184 (org-e-ascii--build-title headline info width
'underline
)
1186 (concat (when (org-string-nw-p links
) links
) contents
))))))
1189 ;;;; Horizontal Rule
1191 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1192 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1193 CONTENTS is nil. INFO is a plist holding contextual
1201 (org-element-get-property :attr_ascii horizontal-rule
)
1203 (make-string (or (and (wholenump (plist-get attr
:width
))
1204 (plist-get attr
:width
))
1205 (org-e-ascii--current-text-width horizontal-rule info
))
1206 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1209 ;;;; Inline Babel Call
1211 ;; Inline Babel Calls are ignored.
1214 ;;;; Inline Src Block
1216 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1217 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1218 CONTENTS holds the contents of the item. INFO is a plist holding
1219 contextual information."
1220 (format org-e-ascii-verbatim-format
1221 (org-element-get-property :value inline-src-block
)))
1226 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1227 "Transcode an INLINETASK element from Org to ASCII.
1228 CONTENTS holds the contents of the block. INFO is a plist
1229 holding contextual information."
1230 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1231 (title (org-export-secondary-string
1232 (org-element-get-property :title inlinetask
) 'e-ascii info
))
1233 (todo (and (plist-get info
:with-todo-keywords
)
1234 (let ((todo (org-element-get-property
1235 :todo-keyword inlinetask
)))
1237 (org-export-secondary-string todo
'e-ascii info
)))))
1238 (todo-type (org-element-get-property :todo-type inlinetask
))
1239 (tags (and (plist-get info
:with-tags
)
1240 (org-element-get-property :tags inlinetask
)))
1241 (priority (and (plist-get info
:with-priority
)
1242 (org-element-get-property :priority inlinetask
))))
1243 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1244 ;; with appropriate arguments.
1245 (if (functionp org-e-ascii-format-inlinetask-function
)
1246 (funcall org-e-ascii-format-inlinetask-function
1247 todo todo-type priority title tags contents width
)
1248 ;; Otherwise, use a default template.
1249 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1250 (org-e-ascii--indent-string
1252 ;; Top line, with an additional blank line if not in UTF-8.
1253 (make-string width
(if utf8p ?━ ?_
)) "\n"
1254 (unless utf8p
(concat (make-string width ?
) "\n"))
1255 ;; Add title. Fill it if wider than inlinetask.
1256 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1257 (if (<= (length title
) width
) title
1258 (org-e-ascii--fill-string title width info
)))
1260 ;; If CONTENTS is not empty, insert it along with
1262 (when (org-string-nw-p contents
)
1263 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1265 (make-string width
(if utf8p ?━ ?_
)))
1266 ;; Flush the inlinetask to the right.
1267 (- org-e-ascii-text-width org-e-ascii-global-margin
1268 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1269 org-e-ascii-inner-margin
)
1270 (org-e-ascii--current-text-width inlinetask info
)))))))
1275 (defun org-e-ascii-item (item contents info
)
1276 "Transcode an ITEM element from Org to ASCII.
1277 CONTENTS holds the contents of the item. INFO is a plist holding
1278 contextual information."
1280 ;; First parent of ITEM is always the plain-list. Get
1281 ;; `:type' property from it.
1282 (let ((type (org-element-get-property
1283 :type
(car (plist-get info
:genealogy
)))))
1284 (if (eq type
'descriptive
)
1286 (org-export-secondary-string
1287 (org-element-get-property :tag item
) 'e-ascii info
) ": ")
1288 (org-element-get-property :bullet item
)))))
1290 ;; Change bullets into more visible form if UTF-8 is active.
1291 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bullet
1292 (replace-regexp-in-string
1294 (replace-regexp-in-string
1296 (replace-regexp-in-string
1298 ;; Contents: Pay attention to indentation. Note: check-boxes are
1299 ;; already taken care of at the paragraph level so they don't
1300 ;; interfere with indentation.
1301 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1302 (if (eq (caar (org-element-get-contents item
)) 'paragraph
)
1304 (concat "\n" contents
))))))
1309 (defun org-e-ascii-keyword (keyword contents info
)
1310 "Transcode a KEYWORD element from Org to ASCII.
1311 CONTENTS is nil. INFO is a plist holding contextual
1313 (let ((key (downcase (org-element-get-property :key keyword
)))
1314 (value (org-element-get-property :value keyword
)))
1316 ((string= key
"ascii") value
)
1317 ((string= key
"toc")
1318 (let ((value (downcase value
)))
1320 ((string-match "\\<headlines\\>" value
)
1321 (let ((depth (or (and (string-match "[0-9]+" value
)
1322 (string-to-number (match-string 0 value
)))
1323 (plist-get info
:with-toc
))))
1324 (org-e-ascii--build-toc
1325 info
(and (wholenump depth
) depth
) keyword
)))
1326 ((string= "tables" value
)
1327 (org-e-ascii--list-tables keyword info
))
1328 ((string= "listings" value
)
1329 (org-e-ascii--list-listings keyword info
)))))
1330 ((string= key
"include")
1331 (org-export-included-file keyword
'e-ascii info
)))))
1334 ;;;; Latex Environment
1336 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1337 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1338 CONTENTS is nil. INFO is a plist holding contextual
1340 (org-remove-indentation (org-element-get-property :value latex-environment
)))
1345 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1346 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1347 CONTENTS is nil. INFO is a plist holding contextual
1349 (org-element-get-property :value latex-fragment
))
1354 (defun org-e-ascii-line-break (line-break contents info
)
1355 "Transcode a LINE-BREAK object from Org to ASCII.
1356 CONTENTS is nil. INFO is a plist holding contextual
1357 information." hard-newline
)
1362 (defun org-e-ascii-link (link desc info
)
1363 "Transcode a LINK object from Org to ASCII.
1365 DESC is the description part of the link, or the empty string.
1366 INFO is a plist holding contextual information."
1367 (let ((raw-link (org-element-get-property :raw-link link
))
1368 (type (org-element-get-property :type link
)))
1370 ((string= type
"coderef")
1371 (let ((ref (org-element-get-property :path link
)))
1372 (format (org-export-get-coderef-format ref desc
)
1373 (org-export-resolve-coderef ref info
))))
1374 ;; Do not apply a special syntax on radio links.
1375 ((string= type
"radio") desc
)
1376 ;; Do not apply a special syntax on fuzzy links pointing to
1378 ((and (string= type
"fuzzy")
1379 (let ((path (org-element-get-property :path link
)))
1380 (loop for target in
(plist-get info
:target-list
)
1382 (org-element-get-property :raw-value target
)
1384 (if (org-string-nw-p desc
) desc raw-link
))
1386 (concat (format "[%s]" (if (org-string-nw-p desc
) desc raw-link
))
1387 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
)))))))
1392 (defun org-e-ascii-macro (macro contents info
)
1393 "Transcode a MACRO element from Org to ASCII.
1394 CONTENTS is nil. INFO is a plist holding contextual
1396 (org-export-expand-macro macro info
))
1401 (defun org-e-ascii-paragraph (paragraph contents info
)
1402 "Transcode a PARAGRAPH element from Org to ASCII.
1403 CONTENTS is the contents of the paragraph, as a string. INFO is
1404 the plist used as a communication channel."
1405 (org-e-ascii--fill-string
1406 (let ((parent (car (plist-get info
:genealogy
))))
1407 ;; If PARAGRAPH is the first one in a list element, be sure to
1408 ;; add the check-box in front of it, before any filling. Later,
1409 ;; it would interfere with line width.
1410 (if (and (eq (car parent
) 'item
)
1411 (equal (car (org-element-get-contents parent
)) paragraph
))
1412 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1413 (concat (case (org-element-get-property :checkbox parent
)
1414 (on (if utf8p
"☑ " "[X] "))
1415 (off (if utf8p
"☐ " "[ ] "))
1416 (trans (if utf8p
"☒ " "[-] ")))
1419 (org-e-ascii--current-text-width paragraph info
) info
))
1424 (defun org-e-ascii-plain-list (plain-list contents info
)
1425 "Transcode a PLAIN-LIST element from Org to ASCII.
1426 CONTENTS is the contents of the list. INFO is a plist holding
1427 contextual information."
1433 (defun org-e-ascii-plain-text (text info
)
1434 "Transcode a TEXT string from Org to ASCII.
1435 INFO is a plist used as a communication channel."
1436 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1437 (plist-get info
:with-special-strings
)))
1439 ;; Usual replacements in utf-8 with proper option set.
1440 (replace-regexp-in-string
1442 (replace-regexp-in-string
1444 (replace-regexp-in-string "---" "—" text
)))))
1447 ;;;; Property Drawer
1449 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1450 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1451 CONTENTS is nil. INFO is a plist used as a communication
1453 ;; The property drawer isn't exported but we want separating blank
1454 ;; lines nonetheless.
1460 (defun org-e-ascii-quote-block (quote-block contents info
)
1461 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1462 CONTENTS holds the contents of the block. INFO is a plist
1463 holding contextual information."
1464 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1465 (org-e-ascii--indent-string
1466 (org-remove-indentation
1467 (org-e-ascii--fill-string contents width info
))
1468 org-e-ascii-quote-margin
)))
1473 (defun org-e-ascii-quote-section (quote-section contents info
)
1474 "Transcode a QUOTE-SECTION element from Org to ASCII.
1475 CONTENTS is nil. INFO is a plist holding contextual information."
1476 (let ((width (org-e-ascii--current-text-width quote-section info
))
1478 (org-export-secondary-string
1479 (org-remove-indentation
1480 (org-element-get-property :value quote-section
)) 'e-ascii info
)))
1481 (org-e-ascii--indent-string
1483 (+ org-e-ascii-quote-margin
1484 ;; Don't apply inner margin if parent headline is low level.
1485 (let ((headline (org-export-get-parent-headline quote-section info
)))
1486 (if (org-export-low-level-p headline info
) 0
1487 org-e-ascii-inner-margin
))))))
1492 (defun org-e-ascii-radio-target (radio-target contents info
)
1493 "Transcode a RADIO-TARGET object from Org to ASCII.
1494 CONTENTS is the contents of the target. INFO is a plist holding
1495 contextual information."
1500 (defun org-e-ascii-section (section contents info
)
1501 "Transcode a SECTION element from Org to ASCII.
1502 CONTENTS is the contents of the section. INFO is a plist holding
1503 contextual information."
1504 (org-e-ascii--indent-string
1507 (when org-e-ascii-links-to-notes
1508 ;; Add list of links at the end of SECTION.
1509 (let ((links (org-e-ascii--describe-links
1510 (org-e-ascii--unique-links section info
)
1511 (org-e-ascii--current-text-width section info
) info
)))
1512 ;; Separate list of links and section contents.
1513 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1514 ;; Do not apply inner margin if parent headline is low level.
1515 (let ((headline (org-export-get-parent-headline section info
)))
1516 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1517 org-e-ascii-inner-margin
))))
1522 (defun org-e-ascii-special-block (special-block contents info
)
1523 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1524 CONTENTS holds the contents of the block. INFO is a plist
1525 holding contextual information."
1531 (defun org-e-ascii-src-block (src-block contents info
)
1532 "Transcode a SRC-BLOCK element from Org to ASCII.
1533 CONTENTS holds the contents of the item. INFO is a plist holding
1534 contextual information."
1535 (let ((caption (org-e-ascii--build-caption src-block info
)))
1537 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1538 (org-e-ascii--box-string (org-export-handle-code src-block info
) info
)
1539 (when (and caption
(not org-e-ascii-caption-above
))
1540 (concat "\n" caption
)))))
1542 ;;;; Statistics Cookie
1544 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1545 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1546 CONTENTS is nil. INFO is a plist holding contextual information."
1547 (org-element-get-property :value statistics-cookie
))
1552 (defun org-e-ascii-subscript (subscript contents info
)
1553 "Transcode a SUBSCRIPT object from Org to ASCII.
1554 CONTENTS is the contents of the object. INFO is a plist holding
1555 contextual information."
1556 (if (org-element-get-property :use-brackets-p subscript
)
1557 (format "_{%s}" contents
)
1558 (format "_%s" contents
)))
1563 (defun org-e-ascii-superscript (superscript contents info
)
1564 "Transcode a SUPERSCRIPT object from Org to ASCII.
1565 CONTENTS is the contents of the object. INFO is a plist holding
1566 contextual information."
1567 (if (org-element-get-property :use-brackets-p superscript
)
1568 (format "_{%s}" contents
)
1569 (format "_%s" contents
)))
1574 ;; While `org-e-ascii-table' is the callback function expected by
1575 ;; org-export mechanism, it requires four subroutines to display
1576 ;; tables accordingly to chosen charset, alignment and width
1579 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1580 ;; for each column in the table,
1581 ;; `org-e-ascii-table--vertical-separators' returns a vector
1582 ;; containing separators (or lack thereof),
1583 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1584 ;; depending on charset, separators and position within the tabl and
1585 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1586 ;; a given cell and width.
1588 (defun org-e-ascii-table (table contents info
)
1589 "Transcode a TABLE element from Org to ASCII.
1590 CONTENTS is nil. INFO is a plist holding contextual information."
1591 (let ((raw-table (org-element-get-property :raw-table table
))
1592 (caption (org-e-ascii--build-caption table info
)))
1594 ;; Possibly add a caption string above.
1595 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1596 ;; Insert table. Note: "table.el" tables are left unmodified.
1597 (if (eq (org-element-get-property :type table
) 'table.el
) raw-table
1598 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1599 ;; Extract information out of the raw table (TABLE-INFO)
1600 ;; and clean it (CLEAN-TABLE).
1601 (table-info (org-export-table-format-info raw-table
))
1602 (special-col-p (plist-get table-info
:special-column-p
))
1603 (alignment (plist-get table-info
:alignment
))
1604 (clean-table (org-export-clean-table raw-table special-col-p
))
1605 ;; Change table into lisp, much like `org-table-to-lisp',
1606 ;; being more careful about keeping the exact length of
1607 ;; cells, for alignment purpose.
1611 (if (string-match org-table-hline-regexp line
) 'hline
1612 (org-split-string (org-trim line
) "\\s-?|\\s-?")))
1613 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1614 ;; Compute real column widths.
1616 (org-e-ascii-table--column-width lisp-table table-info
))
1617 ;; Construct separators according to column groups.
1618 (separators (org-e-ascii-table--vertical-separators table-info
))
1619 ;; Build different `hline' strings, depending on
1620 ;; separators, column widths and position.
1622 (org-e-ascii-table--build-hline
1623 nil separators column-widths info
))
1625 (and utf8p
(org-e-ascii-table--build-hline
1626 'top separators column-widths info
)))
1628 (and utf8p
(org-e-ascii-table--build-hline
1629 'bottom separators column-widths info
))))
1630 ;; Now build table back, with correct alignment, considering
1631 ;; columns widths and separators.
1635 ((eq line
'hline
) hline-standard
)
1636 ((eq line
'hline-bottom
) hline-bottom
)
1637 ((eq line
'hline-top
) hline-top
)
1638 (t (loop for cell in line
1639 for col from
0 to
(length line
)
1642 (let ((sep (aref separators col
)))
1643 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1644 (org-e-ascii-table--format-cell
1645 cell col column-widths alignment info
)) into l
1648 (let ((sep (aref separators col
)))
1649 (if (and utf8p
(not (string= sep
""))) "│"
1651 ;; If charset is `utf-8', make sure lisp-table always starts
1652 ;; with `hline-top' and ends with `hline-bottom'.
1653 (if (not utf8p
) lisp-table
1656 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1660 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1662 '(hline-bottom)))) "\n")))
1663 ;; Possible add a caption string below.
1664 (when (and caption
(not org-e-ascii-caption-above
))
1665 (concat "\n" caption
)))))
1667 (defun org-e-ascii-table--column-width (table table-info
)
1668 "Return vector of TABLE columns width.
1670 TABLE is the Lisp representation of the Org table considered.
1671 TABLE-INFO holds information about the table. See
1672 `org-export-table-format-info'.
1674 Unlike to `:width' property from `org-export-table-format-info',
1675 the return value contains width of every column, not only those
1676 with a width cookie."
1677 (let* ((cookies (plist-get table-info
:width
))
1678 (width (make-vector (length cookies
) 0)))
1682 (unless (eq line
'hline
)
1683 (mapc (lambda (cell)
1684 (let ((len (length cell
)))
1685 (when (> len
(aref width idx
)) (aset width idx len
)))
1689 (unless org-e-ascii-table-widen-columns
1690 ;; When colums are not widened, width cookies have precedence
1691 ;; over string lengths. Thus, overwrite the latter with the
1693 (loop for w across cookies
1694 for idx from
0 to
(length cookies
)
1695 when w do
(aset width idx w
)))
1699 (defun org-e-ascii-table--vertical-separators (table-info)
1700 "Return a vector of strings for vertical separators.
1702 TABLE-INFO holds information about considered table. See
1703 `org-export-table-format-info'.
1705 Return value is a vector whose length is one more than the number
1706 of columns in the table. Special column, if any, is ignored."
1707 (let* ((colgroups (plist-get table-info
:column-groups
))
1708 (separators (make-vector (1+ (length colgroups
)) "")))
1709 (if org-e-ascii-table-keep-all-vertical-lines
1710 (make-vector (length separators
) "|")
1712 (mapc (lambda (group)
1713 (when (memq group
'(start start-end
))
1714 (aset separators column
"|"))
1715 (when (memq group
'(end start-end
))
1716 (aset separators
(1+ column
) "|"))
1719 ;; Remove unneeded special column.
1720 (if (not (plist-get table-info
:special-column-p
)) separators
1721 (substring separators
1))))))
1723 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1724 "Format CELL with column width and alignment constraints.
1726 CELL is the contents of the cell, as a string.
1728 COL is the column containing the cell considered.
1730 WIDTH is a vector holding every column width, as returned by
1731 `org-e-ascii-table--column-width'.
1733 ALIGNMENT is a vector containing alignment strings for every
1736 INFO is a plist used as a communication channel."
1737 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1738 (or (aref width col
) (length cell
)))))
1739 ;; When CELL is too large, it has to be truncated.
1740 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1741 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1742 (let* ((indent-tabs-mode nil
)
1743 (align (aref alignment col
))
1745 (org-e-ascii--justify-string
1746 (org-trim cell
) col-width
1747 (cond ((string= align
"c") 'center
)
1748 ((string= align
"r") 'right
)))))
1749 ;; Return aligned cell, with missing white spaces added and
1750 ;; space separators between columns.
1753 (concat aligned-cell
1754 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1756 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1757 "Return string used as an horizontal line in tables.
1759 POSITION is a symbol among `top', `bottom' and nil, which
1760 specifies position of the horizontal line within the table.
1762 SEPARATORS is a vector strings specifying separators used in the
1763 table, as returned by `org-e-ascii-table--vertical-separators'.
1765 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1766 columns in the table, as returned by
1767 `org-e-ascii-table--column-width'.
1769 INFO is a plist used as a communication channel."
1770 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1771 (loop for idx from
0 to
(length separators
)
1772 for width across column-widths
1775 (cond ((string= (aref separators idx
) "") nil
)
1776 ((and utf8p
(zerop idx
))
1777 (cond ((eq position
'top
) "┍")
1778 ((eq position
'bottom
) "┕")
1781 (cond ((eq position
'top
) "┯")
1782 ((eq position
'bottom
) "┷")
1785 ;; Hline has to cover all the cell and
1786 ;; both white spaces between columns.
1787 (make-string (+ width
2)
1788 (cond ((not utf8p
) ?-
)
1796 ((string= (aref separators idx
) "") nil
)
1797 (utf8p (cond ((eq position
'top
) "┑")
1798 ((eq position
'bottom
) "┙")
1805 (defun org-e-ascii-target (target contents info
)
1806 "Transcode a TARGET object from Org to ASCII.
1807 CONTENTS is the contents of the target. INFO is a plist holding
1808 contextual information."
1814 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1815 "Transcode a TIME-STAMP object from Org to ASCII.
1816 CONTENTS is nil. INFO is a plist holding contextual information."
1817 ;; Return time-stamps as-is.
1818 (org-element-time-stamp-interpreter time-stamp contents
))
1823 (defun org-e-ascii-verbatim (verbatim contents info
)
1824 "Return a VERBATIM object from Org to ASCII.
1825 CONTENTS is nil. INFO is a plist holding contextual information."
1826 (format org-e-ascii-verbatim-format
1827 (org-element-get-property :value verbatim
)))
1832 (defun org-e-ascii-verse-block (verse-block contents info
)
1833 "Transcode a VERSE-BLOCK element from Org to ASCII.
1834 CONTENTS is nil. INFO is a plist holding contextual information."
1835 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1836 (org-e-ascii--indent-string
1837 (org-e-ascii--justify-string
1838 (org-export-secondary-string
1839 (org-element-get-property :value verse-block
) 'e-ascii info
)
1841 org-e-ascii-quote-margin
)))
1846 (defun org-e-ascii-filter-headline-blank-lines (headline back-end
)
1847 "Filter controlling number of blank lines after an headline.
1849 HEADLINE is a string representing a transcoded headline.
1850 BACK-END is symbol specifying back-end used for export.
1852 This function only applies to `e-ascii' back-end. See
1853 `org-e-ascii-headline-spacing' for information.
1855 For any other back-end, HEADLINE is returned as-is."
1856 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1857 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1858 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1862 ;;; Interactive function
1864 (defun org-e-ascii-export-to-ascii
1865 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1866 "Export current buffer to a text file.
1868 If narrowing is active in the current buffer, only export its
1871 If a region is active, export that region.
1873 When optional argument SUBTREEP is non-nil, export the sub-tree
1874 at point, extracting information from the headline properties
1877 When optional argument VISIBLE-ONLY is non-nil, don't export
1878 contents of hidden elements.
1880 When optional argument BODY-ONLY is non-nil, strip title, table
1881 of contents and footnote definitions from output.
1883 EXT-PLIST, when provided, is a property list with external
1884 parameters overriding Org default settings, but still inferior to
1885 file-local settings.
1887 When optional argument PUB-DIR is set, use it as the publishing
1890 Return output file's name."
1892 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1894 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1897 (provide 'org-e-ascii
)
1898 ;;; org-e-ascii.el ends here