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-normalize-string
"org-element" (s))
40 (declare-function org-element-map
"org-element"
41 (data types fun
&optional info first-match
))
42 (declare-function org-element-time-stamp-interpreter
43 "org-element" (time-stamp contents
))
45 (declare-function org-export-clean-table
"org-export" (table specialp
))
46 (declare-function org-export-collect-footnote-definitions
47 "org-export" (data info
))
48 (declare-function org-export-collect-headlines
"org-export" (info &optional n
))
49 (declare-function org-export-collect-listings
"org-export" (info))
50 (declare-function org-export-collect-tables
"org-export" (info))
51 (declare-function org-export-data
"org-export" (data backend info
))
52 (declare-function org-export-expand-macro
"org-export" (macro info
))
53 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
54 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
55 (declare-function org-export-get-headline-number
"org-export" (headline info
))
56 (declare-function org-export-get-ordinal
"org-export"
57 (element info
&optional types within-section predicate
))
58 (declare-function org-export-get-parent-headline
"org-export" (blob info
))
59 (declare-function org-export-get-relative-level
"org-export" (headline info
))
60 (declare-function org-export-handle-code
61 "org-export" (element info
&optional num-fmt ref-fmt delayed
))
62 (declare-function org-export-included-file
"org-export" (keyword backend info
))
63 (declare-function org-export-low-level-p
"org-export" (headline info
))
64 (declare-function org-export-output-file-name
"org-export"
65 (extension &optional subtreep pub-dir
))
66 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
67 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
68 (declare-function org-export-resolve-id-link
"org-export" (link info
))
69 (declare-function org-export-secondary-string
70 "org-export" (secondary backend info
))
71 (declare-function org-export-table-format-info
"org-export" (table))
73 org-export-to-file
"org-export"
74 (backend file
&optional subtreep visible-only body-only ext-plist
))
78 ;;; Internal Variables
80 ;; The following setting won't allow to modify preferred charset
81 ;; through a buffer keyword or an option item, but, since the property
82 ;; will appear in communication channel nonetheless, it allows to
83 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
86 ;; We also install a developer filter for headlines, in order to
87 ;; control blank lines in output string.
89 (defconst org-e-ascii-option-alist
90 '((:ascii-charset nil nil org-e-ascii-charset
)
91 (:filter-headline nil nil
(cons
92 'org-e-ascii-filter-headline-blank-lines
93 org-export-filter-headline-functions
)))
94 "Alist between ASCII export properties and ways to set them.
95 See `org-export-option-alist' for more information on the
96 structure or the values.")
98 (defconst org-e-ascii-dictionary
102 :latin1
"Footnotes\n"
103 :utf-8
"Footnotes\n")
105 :ascii
"Notes de bas de page\n"
106 :latin1
"Notes de bas de page\n"
107 :utf-8
"Notes de bas de page\n"))
110 :ascii
"Listing %d: %s"
111 :latin1
"Listing %d: %s"
112 :utf-8
"Listing %d: %s")
114 :ascii
"Programme %d : %s"
115 :latin1
"Programme %d : %s"
116 :utf-8
"Programme nº %d : %s"))
117 ("List Of Listings\n"
119 :ascii
"List Of Listings\n"
120 :latin1
"List Of Listings\n"
121 :utf-8
"List Of Listings\n")
123 :ascii
"Liste des programmes\n"
124 :latin1
"Liste des programmes\n"
125 :utf-8
"Liste des programmes\n"))
128 :ascii
"List Of Tables\n"
129 :latin1
"List Of Tables\n"
130 :utf-8
"List Of Tables\n")
132 :ascii
"Liste des tableaux\n"
133 :latin1
"Liste des tableaux\n"
134 :utf-8
"Liste des tableaux\n"))
137 :ascii
"Listing %d: "
138 :latin1
"Listing %d: "
139 :utf-8
"Listing %d: ")
141 :ascii
"Programme %d : "
142 :latin1
"Programme %d : "
143 :utf-8
"Programme nº %d : "))
144 ("Table Of Contents\n"
146 :ascii
"Table Of Contents\n"
147 :latin1
"Table Of Contents\n"
148 :utf-8
"Table Of Contents\n")
151 :latin1
"Table des matières\n"
152 :utf-8
"Table des matières\n"))
155 :ascii
"Table %d: %s"
156 :latin1
"Table %d: %s"
157 :utf-8
"Table %d: %s")
159 :ascii
"Tableau %d : %s"
160 :latin1
"Tableau %d : %s"
161 :utf-8
"Tableau nº %d : %s"))
164 :ascii
"See section %s"
165 :latin1
"See section %s"
166 :utf-8
"See section %s")
168 :ascii
"cf. section %s"
169 :latin1
"cf. section %s"
170 :utf-8
"cf. section %s"))
177 :ascii
"Tableau %d : "
178 :latin1
"Tableau %d : "
179 :utf-8
"Tableau nº %d : "))
182 :ascii
"Unknown reference"
183 :latin1
"Unknown reference"
184 :utf-8
"Unknown reference")
186 :ascii
"Destination inconnue"
187 :latin1
"Référence inconnue"
188 :utf-8
"Référence inconnue")))
189 "Dictionary for ASCII back-end.
191 Alist whose car is the string to translate and cdr is an alist
192 whose car is the language string and cdr is a plist whose
193 properties are possible charsets and value the translated term.
195 It is used as a database for `org-e-ascii--translate'.")
199 ;;; User Configurable Variables
201 (defgroup org-export-e-ascii nil
202 "Options for exporting Org mode files to ASCII."
203 :tag
"Org Export ASCII"
206 (defcustom org-e-ascii-text-width
72
207 "Maximum width of exported text.
208 This number includes margin size, as set in
209 `org-e-ascii-global-margin'."
210 :group
'org-export-e-ascii
213 (defcustom org-e-ascii-global-margin
0
214 "Width of the left margin, in number of characters."
215 :group
'org-export-e-ascii
218 (defcustom org-e-ascii-inner-margin
2
219 "Width of the inner margin, in number of characters.
220 Inner margin is applied between each headline."
221 :group
'org-export-e-ascii
224 (defcustom org-e-ascii-quote-margin
6
225 "Width of margin used for quoting text, in characters.
226 This margin is applied on both sides of the text."
227 :group
'org-export-e-ascii
230 (defcustom org-e-ascii-inlinetask-width
30
231 "Width of inline tasks, in number of characters.
232 This number ignores any margin."
233 :group
'org-export-e-ascii
236 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
237 "Number of blank lines inserted around headlines.
239 This variable can be set to a cons cell. In that case, its car
240 represents the number of blank lines present before headline
241 contents whereas its cdr reflects the number of blank lines after
244 A nil value replicates the number of blank lines found in the
245 original Org buffer at the same place."
246 :group
'org-export-e-ascii
248 (const :tag
"Replicate original spacing" nil
)
249 (cons :tag
"Set an uniform spacing"
250 (integer :tag
"Number of blank lines before contents")
251 (integer :tag
"Number of blank lines after contents"))))
253 (defcustom org-e-ascii-charset
'ascii
254 "The charset allowed to represent various elements and objects.
256 `ascii' Only use plain ASCII characters
257 `latin1' Include Latin-1 characters
258 `utf-8' Use all UTF-8 characters"
259 :group
'org-export-e-ascii
261 (const :tag
"ASCII" ascii
)
262 (const :tag
"Latin-1" latin1
)
263 (const :tag
"UTF-8" utf-8
)))
265 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
267 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
268 "Characters for underlining headings in ASCII export.
270 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
271 and whose value is a list of characters.
273 For each supported charset, this variable associates a sequence
274 of underline characters. In a sequence, the characters will be
275 used in order for headlines level 1, 2, ... If no character is
276 available for a given level, the headline won't be underlined."
277 :group
'org-export-e-ascii
279 (cons :tag
"Underline characters sequence"
280 (const :tag
"ASCII charset" ascii
)
282 (cons :tag
"Underline characters sequence"
283 (const :tag
"Latin-1 charset" latin1
)
285 (cons :tag
"Underline characters sequence"
286 (const :tag
"UTF-8 charset" utf-8
)
287 (repeat character
))))
289 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
292 "Bullet characters for headlines converted to lists in ASCII export.
294 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
295 and whose value is a list of characters.
297 The first character is used for the first level considered as low
298 level, and so on. If there are more levels than characters given
299 here, the list will be repeated.
301 Note that this variable doesn't affect plain lists
303 :group
'org-export-e-ascii
305 (cons :tag
"Bullet characters for low level headlines"
306 (const :tag
"ASCII charset" ascii
)
308 (cons :tag
"Bullet characters for low level headlines"
309 (const :tag
"Latin-1 charset" latin1
)
311 (cons :tag
"Bullet characters for low level headlines"
312 (const :tag
"UTF-8 charset" utf-8
)
313 (repeat character
))))
315 (defcustom org-e-ascii-links-to-notes t
316 "Non-nil means convert links to notes before the next headline.
317 When nil, the link will be exported in place. If the line
318 becomes long in this way, it will be wrapped."
319 :group
'org-export-e-ascii
322 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
323 "Non-nil means keep all vertical lines in ASCII tables.
324 When nil, vertical lines will be removed except for those needed
325 for column grouping."
326 :group
'org-export-e-ascii
329 (defcustom org-e-ascii-table-widen-columns t
330 "Non-nil means widen narrowed columns for export.
331 When nil, narrowed columns will look in ASCII export just like in
332 Org mode, i.e. with \"=>\" as ellipsis."
333 :group
'org-export-e-ascii
336 (defcustom org-e-ascii-caption-above nil
337 "When non-nil, place caption string before the element.
338 Otherwise, place it right after it."
339 :group
'org-export-e-ascii
342 (defcustom org-e-ascii-verbatim-format
"`%s'"
343 "Format string used for verbatim text and inline code."
344 :group
'org-export-e-ascii
347 (defcustom org-e-ascii-format-drawer-function nil
348 "Function called to format a drawer in ASCII.
350 The function must accept two parameters:
351 NAME the drawer name, like \"LOGBOOK\"
352 CONTENTS the contents of the drawer.
353 WIDTH the text width within the drawer.
355 The function should return either the string to be exported or
356 nil to ignore the drawer.
358 For example, the variable could be set to the following function
359 in order to mimic default behaviour:
361 \(defun org-e-ascii-format-drawer-default \(name contents width\)
362 \"Format a drawer element for ASCII export.\"
364 :group
'org-export-e-ascii
367 (defcustom org-e-ascii-format-inlinetask-function nil
368 "Function called to format an inlinetask in ASCII.
370 The function must accept six parameters:
371 TODO the todo keyword, as a string
372 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
373 PRIORITY the inlinetask priority, as a string
374 NAME the inlinetask name, as a string.
375 TAGS the inlinetask tags, as a string.
376 CONTENTS the contents of the inlinetask, as a string.
378 The function should return either the string to be exported or
379 nil to ignore the inline task.
381 For example, the variable could be set to the following function
382 in order to mimic default behaviour:
384 \(defun org-e-ascii-format-inlinetask-default
385 \(todo type priority name tags contents\)
386 \"Format an inline task element for ASCII export.\"
387 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
388 \(width org-e-ascii-inlinetask-width\)
389 \(org-e-ascii--indent-string
391 ;; Top line, with an additional blank line if not in UTF-8.
392 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
393 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
394 ;; Add title. Fill it if wider than inlinetask.
395 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
396 \(if \(<= \(length title\) width\) title
397 \(org-e-ascii--fill-string title width info\)\)\)
399 ;; If CONTENTS is not empty, insert it along with
401 \(when \(org-string-nw-p contents\)
402 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
404 \(make-string width \(if utf8p ?━ ?_\)\)\)
405 ;; Flush the inlinetask to the right.
406 \(- \(plist-get info :ascii-width\)
407 \(plist-get info :ascii-margin\)
408 \(plist-get info :ascii-inner-margin\)
409 \(org-e-ascii--current-text-width inlinetask info\)\)"
410 :group
'org-export-e-ascii
415 ;;; Internal Functions
417 ;; Internal functions fall into three categories.
419 ;; The first one is about text formatting. The core function is
420 ;; `org-e-ascii--current-text-width', which determines the current
421 ;; text width allowed to a given element. In other words, it helps
422 ;; keeping each line width within maximum text width defined in
423 ;; `org-e-ascii-text-width'. Once this information is known,
424 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
425 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
426 ;; operate on a given output string.
428 ;; The second category contains functions handling elements listings,
429 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
430 ;; returns a complete table of contents, `org-e-ascii--list-listings'
431 ;; returns a list of referenceable src-block elements, and
432 ;; `org-e-ascii--list-tables' does the same for table elements.
434 ;; The third category includes general helper functions.
435 ;; `org-e-ascii--build-title' creates the title for a given headline
436 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
437 ;; caption string associated to a table or a src-block.
438 ;; `org-e-ascii--describe-links' creates notes about links for
439 ;; insertion at the end of a section. It uses
440 ;; `org-e-ascii--unique-links' to get the list of links to describe.
441 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
442 ;; to internationalize output.
445 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
446 "Fill a string with specified text-width and return it.
448 S is the string being filled. TEXT-WIDTH is an integer
449 specifying maximum length of a line. INFO is the plist used as
450 a communication channel.
452 Optional argument JUSTIFY can specify any type of justification
453 among `left', `center', `right' or `full'. A nil value is
454 equivalent to `left'. For a justification that doesn't also fill
455 string, see `org-e-ascii--justify-string'.
457 Return nil if S isn't a string."
458 ;; Don't fill paragraph when break should be preserved.
459 (cond ((not (stringp s
)) nil
)
460 ((plist-get info
:preserve-breaks
) s
)
462 (let ((fill-column text-width
)
463 (use-hard-newlines t
))
465 (fill-region (point-min) (point-max) justify
))
468 (defun org-e-ascii--justify-string (s text-width how
)
470 TEXT-WIDTH is an integer specifying maximum length of a line.
471 HOW determines the type of justification: it can be `left',
472 `right', `full' or `center'."
475 (goto-char (point-min))
476 (let ((fill-column text-width
))
477 (while (< (point) (point-max))
478 (justify-current-line how
)
482 (defun org-e-ascii--indent-string (s width
)
483 "Indent string S by WIDTH white spaces.
484 Empty lines are not indented."
486 (replace-regexp-in-string
487 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
489 (defun org-e-ascii--box-string (s info
)
490 "Return string S with a partial box to its left.
491 INFO is a plist used as a communicaton channel."
492 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
493 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
494 (replace-regexp-in-string
495 "^" (if utf8p
"│ " "| ")
496 ;; Remove last newline character.
497 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
499 (defun org-e-ascii--current-text-width (element info
)
500 "Return maximum text width for ELEMENT's contents.
501 INFO is a plist used as a communication channel."
503 ;; Elements with an absolute width: `headline' and `inlinetask'.
504 ((eq (car element
) 'inlinetask
) org-e-ascii-inlinetask-width
)
505 ((eq (car element
) 'headline
)
506 (- org-e-ascii-text-width
507 (let ((low-level-rank (org-export-low-level-p element info
)))
508 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
509 ;; Elements with a relative width: store maximum text width in
512 (let* ((genealogy (cons element
(plist-get info
:genealogy
)))
513 ;; Total width is determined by the presence, or not, of an
514 ;; inline task among ELEMENT parents.
516 (if (loop for parent in genealogy
517 thereis
(eq (car parent
) 'inlinetask
))
518 org-e-ascii-inlinetask-width
519 ;; No inlinetask: Remove global margin from text width.
520 (- org-e-ascii-text-width
521 org-e-ascii-global-margin
522 (let ((parent (org-export-get-parent-headline element info
)))
523 ;; Inner margin doesn't apply to text before first
526 (let ((low-level-rank
527 (org-export-low-level-p parent info
)))
528 ;; Inner margin doesn't apply to contents of
529 ;; low level headlines, since they've got their
530 ;; own indentation mechanism.
531 (if low-level-rank
(* low-level-rank
2)
532 org-e-ascii-inner-margin
))))))))
534 ;; Each `quote-block', `quote-section' and `verse-block' above
535 ;; narrows text width by twice the standard margin size.
536 (+ (* (loop for parent in genealogy
537 when
(memq (car parent
)
538 '(quote-block quote-section verse-block
))
540 2 org-e-ascii-quote-margin
)
541 ;; Text width within a plain-list is restricted by
542 ;; indentation of current item. If that's the case,
543 ;; compute it with the help of `:structure' property from
544 ;; parent item, if any.
546 (if (eq (car element
) 'item
) element
547 (loop for parent in genealogy
548 when
(eq (car parent
) 'item
)
550 (if (not parent-item
) 0
551 ;; Compute indentation offset of the current item,
552 ;; that is the sum of the difference between its
553 ;; indentation and the indentation of the top item in
554 ;; the list and current item bullet's length. Also
555 ;; remove tag length (for description lists) or bullet
557 (let ((struct (org-element-get-property :structure parent-item
))
558 (beg-item (org-element-get-property :begin parent-item
)))
559 (+ (- (org-list-get-ind beg-item struct
)
561 (org-list-get-top-point struct
) struct
))
563 (or (org-list-get-tag beg-item struct
)
564 (org-list-get-bullet beg-item struct
)))))))))))))
566 (defun org-e-ascii--build-title
567 (element info text-width
&optional underline notags
)
568 "Format ELEMENT title and return it.
570 ELEMENT is either an `headline' or `inlinetask' element. INFO is
571 a plist used as a communication channel. TEXT-WIDTH is an
572 integer representing the maximum length of a line.
574 When optional argument UNDERLINE is non-nil, underline title,
575 without the tags, according to `org-e-ascii-underline'
578 if optional argument NOTAGS is nil, no tags will be added to the
580 (let* ((headlinep (eq (car element
) 'headline
))
582 ;; Numbering is specific to headlines.
584 ;; Section numbering must be active, and headline's
585 ;; level should be above specified limit, if any.
586 (let ((sec-num (plist-get info
:section-numbers
)))
587 (if (not (wholenump sec-num
)) sec-num
588 (<= (org-export-get-relative-level headline info
) sec-num
)))
589 ;; All tests passed: build numbering string.
593 (org-export-get-headline-number element info
) ".")
595 (text (org-export-secondary-string
596 (org-element-get-property :title element
) 'e-ascii info
))
598 (and (plist-get info
:with-todo-keywords
)
599 (let ((todo (org-element-get-property :todo-keyword element
)))
601 (concat (org-export-secondary-string todo
'e-ascii info
)
603 (tags (and (not notags
)
604 (plist-get info
:with-tags
)
605 (org-element-get-property :tags element
)))
607 (and (plist-get info
:with-priority
)
608 (concat (org-element-get-property :priority element
) " ")))
609 (first-part (concat numbers todo priority text
)))
612 ;; Align tags, if any.
616 (max (- text-width
(1+ (length first-part
))) (length tags
)))
618 ;; Maybe underline text, if ELEMENT type is `headline' and an
619 ;; underline character has been defined.
620 (when (and underline headlinep
)
622 (nth (1- (org-export-get-relative-level element info
))
623 (cdr (assq (plist-get info
:ascii-charset
)
624 org-e-ascii-underline
)))))
627 (make-string (length first-part
) under-char
))))))))
629 (defun org-e-ascii--build-caption (element info
)
630 "Return caption string for ELEMENT, if applicable.
632 INFO is a plist used as a communication channel.
634 The caption string contains the sequence number of ELEMENT if it
635 has a name affiliated keyword, along with the real caption, if
636 any. Return nil when ELEMENT has no affiliated caption or name
638 (let ((caption (org-element-get-property :caption element
))
639 (name (org-element-get-property :name element
)))
640 (when (or caption name
)
641 ;; Get sequence number of current src-block among every
642 ;; src-block with either a caption or a name.
644 (org-export-get-ordinal
646 (lambda (el) (or (org-element-get-property :caption el
)
647 (org-element-get-property :name el
)))))
648 (title-fmt (org-e-ascii--translate
650 (table "Table %d: %s")
651 (src-block "Listing %d: %s")) info
)))
652 (org-e-ascii--fill-string
655 (if (not caption
) name
656 (org-export-secondary-string (car caption
) 'e-ascii info
)))
657 (org-e-ascii--current-text-width element info
) info
)))))
659 (defun org-e-ascii--build-toc (info &optional n keyword
)
660 "Return a table of contents.
662 INFO is a plist used as a communication channel.
664 Optional argument N, when non-nil, is an integer specifying the
667 Optional argument KEYWORD specifies the TOC keyword, if any, from
668 which the table of contents generation has been initiated."
669 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
672 (make-string (1- (length title
))
673 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
676 (if keyword
(org-e-ascii--current-text-width keyword info
)
677 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
680 (let* ((level (org-export-get-relative-level headline info
))
681 (indent (* (1- level
) 3)))
683 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
684 (org-e-ascii--build-title
685 headline info
(- text-width indent
) nil
686 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
687 (org-export-collect-headlines info n
) "\n")))))
689 (defun org-e-ascii--list-listings (keyword info
)
690 "Return a list of listings.
692 KEYWORD is the keyword that initiated the list of listings
693 generation. INFO is a plist used as a communication channel."
694 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
697 (make-string (1- (length title
))
698 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
701 (if keyword
(org-e-ascii--current-text-width keyword info
)
702 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
703 ;; Use a counter instead of retreiving ordinal of each
708 ;; Store initial text so its length can be computed. This is
709 ;; used to properly align caption right to it in case of
710 ;; filling (like contents of a description list item).
712 (format (org-e-ascii--translate "Listing %d: " info
)
717 (org-e-ascii--indent-string
718 (org-e-ascii--fill-string
719 (let ((caption (org-element-get-property :caption src-block
)))
720 (if (not caption
) (org-element-get-property :name src-block
)
721 (org-export-secondary-string
722 ;; Use short name in priority, if available.
723 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
724 (- text-width
(length initial-text
)) info
)
725 (length initial-text
))))))
726 (org-export-collect-listings info
) "\n")))))
728 (defun org-e-ascii--list-tables (keyword info
)
729 "Return a list of listings.
731 KEYWORD is the keyword that initiated the list of listings
732 generation. INFO is a plist used as a communication channel."
733 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
736 (make-string (1- (length title
))
737 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
740 (if keyword
(org-e-ascii--current-text-width keyword info
)
741 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
742 ;; Use a counter instead of retreiving ordinal of each
747 ;; Store initial text so its length can be computed. This is
748 ;; used to properly align caption right to it in case of
749 ;; filling (like contents of a description list item).
751 (format (org-e-ascii--translate "Table %d: " info
)
756 (org-e-ascii--indent-string
757 (org-e-ascii--fill-string
758 (let ((caption (org-element-get-property :caption table
)))
759 (if (not caption
) (org-element-get-property :name table
)
760 ;; Use short name in priority, if available.
761 (org-export-secondary-string
762 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
763 (- text-width
(length initial-text
)) info
)
764 (length initial-text
))))))
765 (org-export-collect-tables info
) "\n")))))
767 (defun org-e-ascii--unique-links (element info
)
768 "Return a list of unique link references in ELEMENT.
770 ELEMENT is either an headline element or a section element. INFO
771 is a plist used as a communication channel.
773 It covers links that may be found current headline's title, in
774 the following section and in any inlinetask's title there."
778 ;; Return LINK if it wasn't referenced so far, or nil.
779 ;; Update SEEN links along the way.
782 (cons (org-element-get-property :raw-link link
)
783 (org-element-get-contents link
))))
784 (unless (member footprint seen
)
785 (push footprint seen
) link
)))))
786 (harvest-links-in-title
788 ;; Return a list of all unique links in ELEMENT. ELEMENT
789 ;; may be an headline or an inlinetask element.
792 (dolist (obj (org-element-get-property :title element
) acc
)
793 (when (and (listp obj
) (eq (car obj
) 'link
))
794 (let ((link (funcall unique-link-p obj
)))
795 (and link
(push link acc
)))))))))
796 ;; Retrieve headline's section, if it exists.
797 (section (if (eq (car element
) 'section
) element
798 (let ((sec (car (org-element-get-contents element
))))
799 (and (eq (car sec
) 'section
) sec
))))
800 (headline (if (eq (car element
) 'headline
) element
801 (org-export-get-parent-headline element info
))))
803 ;; Links that may be in HEADLINE's title.
804 (funcall harvest-links-in-title headline
)
805 ;; Get all links in SECTION.
807 section
'link
(lambda (link local
) (funcall unique-link-p link
)) info
)
808 ;; Links that may be in inlinetasks titles within SECTION.
812 (lambda (inlinetask local
)
813 (push (funcall harvest-links-in-title inlinetask
) acc
))
817 (defun org-e-ascii--describe-links (links width info
)
818 "Return a string describing a list of links.
820 LINKS is a list of link type objects, as returned by
821 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
822 the output string. INFO is a plist used as a communication
826 (let ((type (org-element-get-property :type link
))
827 (anchor (let ((desc (org-element-get-contents link
)))
829 (org-element-get-property :raw-link link
)
830 (org-export-secondary-string desc
'e-ascii info
)))))
832 ;; Coderefs and radio links are ignored.
833 ((member type
'("coderef" "radio")) nil
)
834 ;; Id, custom-id and fuzzy links (with the exception of
835 ;; targets): Headlines refer to their numbering.
836 ((member type
'("custom-id" "fuzzy" "id"))
837 (let ((destination (if (string= type
"fuzzy")
838 (org-export-resolve-fuzzy-link link info
)
839 (org-export-resolve-id-link link info
))))
840 (unless (eq (car destination
) 'target
)
842 (org-e-ascii--fill-string
846 (if (not destination
)
847 (org-e-ascii--translate "Unknown reference" info
)
849 (org-e-ascii--translate "See section %s" info
)
850 (mapconcat 'number-to-string
851 (org-export-get-headline-number destination info
)
853 width info
) "\n\n"))))
854 ;; Do not add a link that cannot be resolved and doesn't have
855 ;; any description: destination is already visible in the
857 ((not (org-element-get-contents link
)) nil
)
860 (org-e-ascii--fill-string
861 (format "[%s] %s" anchor
(org-element-get-property :raw-link link
))
870 (defun org-e-ascii-template--document-title (info)
871 "Return document title, as a string.
872 INFO is a plist used as a communication channel."
873 (let ((text-width org-e-ascii-text-width
)
874 (title (org-export-secondary-string
875 (plist-get info
:title
) 'e-ascii info
))
877 (and (plist-get info
:with-author
)
878 (let ((auth (plist-get info
:author
)))
879 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
881 (and (plist-get info
:with-email
)
882 (org-export-secondary-string
883 (plist-get info
:email
) 'e-ascii info
)))
884 (date (plist-get info
:date
)))
885 ;; There are two types of title blocks depending on the presence
886 ;; of a title to display.
887 (if (string= title
"")
888 ;; Title block without a title. DATE is positioned at the top
889 ;; right of the document, AUTHOR to the top left and EMAIL
892 ((and (org-string-nw-p date
) (org-string-nw-p author
))
895 (make-string (- text-width
(length date
) (length author
)) ?
)
897 (when (org-string-nw-p email
) (concat "\n" email
))
899 ((and (org-string-nw-p date
) (org-string-nw-p email
))
902 (make-string (- text-width
(length date
) (length email
)) ?
)
904 ((org-string-nw-p date
)
906 (org-e-ascii--justify-string date text-width
'right
)
908 ((and (org-string-nw-p author
) (org-string-nw-p email
))
909 (concat author
"\n" email
"\n\n\n"))
910 ((org-string-nw-p author
) (concat author
"\n\n\n"))
911 ((org-string-nw-p email
) (concat email
"\n\n\n")))
912 ;; Title block with a title. Document's TITLE, along with the
913 ;; AUTHOR and its EMAIL are both overlined and an underlined,
914 ;; centered. Date is just below, also centered.
915 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
916 ;; Format TITLE. It may be filled if it is too wide,
917 ;; that is wider than the two thirds of the total width.
918 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
919 (formatted-title (org-e-ascii--fill-string title title-len info
))
922 (min (+ (max title-len
(length author
) (length email
)) 2)
923 text-width
) (if utf8p ?━ ?_
))))
924 (org-e-ascii--justify-string
927 (upcase formatted-title
)
928 (if utf8p
"\n\n\n" "\n\n")
930 ((and (org-string-nw-p author
) (org-string-nw-p email
))
931 (concat author
"\n" email
))
932 ((org-string-nw-p author
) author
)
933 ((org-string-nw-p email
) email
))
935 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
936 "\n\n\n") text-width
'center
)))))
938 (defun org-e-ascii-template (contents info
)
939 "Return complete document string after ASCII conversion.
940 CONTENTS is the transcoded contents string. INFO is a plist
941 holding export options."
942 (org-element-normalize-string
943 (org-e-ascii--indent-string
944 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
945 ;; 1. Build title block.
947 (org-e-ascii-template--document-title info
)
948 ;; 2. Table of contents.
949 (let ((depth (plist-get info
:with-toc
)))
952 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
954 ;; 3. Document's body.
956 ;; 4. Footnote definitions.
957 (let ((definitions (org-export-collect-footnote-definitions
958 (plist-get info
:parse-tree
) info
))
959 ;; Insert full links right inside the footnote definition
960 ;; as they have no chance to be inserted later.
961 (org-e-ascii-links-to-notes nil
))
965 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
970 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
974 (let ((id (format "[%s] " (car ref
))))
975 ;; Distinguish between inline definitions and
976 ;; full-fledged definitions.
978 (let ((def (nth 2 ref
)))
979 (if (eq (car def
) 'org-data
)
980 ;; Full-fledged definition: footnote ID is
981 ;; inserted inside the first parsed paragraph
982 ;; (FIRST), if any, to be sure filling will
983 ;; take it into consideration.
984 (let ((first (car (org-element-get-contents def
))))
985 (if (not (eq (car first
) 'paragraph
))
986 (concat id
"\n" (org-export-data def
'e-ascii info
))
987 (push id
(nthcdr 2 first
))
988 (org-export-data def
'e-ascii info
)))
989 ;; Fill paragraph once footnote ID is inserted in
990 ;; order to have a correct length for first line.
991 (org-e-ascii--fill-string
992 (concat id
(org-export-secondary-string def
'e-ascii info
))
993 text-width info
))))))
994 definitions
"\n\n"))))
995 ;; 5. Creator. Ignore `comment' value as there are no comments in
996 ;; ASCII. Justify it to the bottom right.
997 (let ((creator-info (plist-get info
:with-creator
)))
998 (unless (or (not creator-info
) (eq creator-info
'comment
))
1001 (org-e-ascii--fill-string
1002 (plist-get info
:creator
) text-width info
'right
))))))
1003 org-e-ascii-global-margin
)))
1005 (defun org-e-ascii--translate (s info
)
1006 "Translate string S.
1008 INFO is a plist used as a communication channel.
1010 Translation depends on `:language' property and allowed charset.
1011 If no translation in found for a given language and a given
1012 charset, fall-back to S."
1013 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
1014 (lang (plist-get info
:language
))
1015 (translations (cdr (assoc s org-e-ascii-dictionary
))))
1016 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
1020 ;;; Transcode Functions
1024 ;; Babel Calls are ignored.
1029 (defun org-e-ascii-center-block (center-block contents info
)
1030 "Transcode a CENTER-BLOCK element from Org to ASCII.
1031 CONTENTS holds the contents of the block. INFO is a plist
1032 holding contextual information."
1033 (org-e-ascii--justify-string
1034 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1039 ;; Comments are ignored.
1044 ;; Comment Blocks are ignored.
1049 (defun org-e-ascii-drawer (drawer contents info
)
1050 "Transcode a DRAWER element from Org to ASCII.
1051 CONTENTS holds the contents of the block. INFO is a plist
1052 holding contextual information."
1053 (let ((name (org-element-get-property :drawer-name drawer
))
1054 (width (org-e-ascii--current-text-width drawer info
)))
1055 (if (functionp org-e-ascii-format-drawer-function
)
1056 (funcall org-e-ascii-format-drawer-function name contents width
)
1057 ;; If there's no user defined function: simply
1058 ;; display contents of the drawer.
1064 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1065 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1066 CONTENTS holds the contents of the block. INFO is a plist
1067 holding contextual information. See
1074 (defun org-e-ascii-emphasis (emphasis contents info
)
1075 "Transcode EMPHASIS from Org to ASCII.
1076 CONTENTS is the contents of the emphasized text. INFO is a plist
1077 holding contextual information.."
1078 (let ((marker (org-element-get-property :marker emphasis
)))
1079 ;; Leave emphasis markers as-is.
1080 (concat marker contents marker
)))
1085 (defun org-e-ascii-entity (entity contents info
)
1086 "Transcode an ENTITY object from Org to ASCII.
1087 CONTENTS are the definition itself. INFO is a plist holding
1088 contextual information."
1089 (org-element-get-property
1090 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1096 (defun org-e-ascii-example-block (example-block contents info
)
1097 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1098 CONTENTS is nil. INFO is a plist holding contextual information."
1099 (org-e-ascii--box-string (org-export-handle-code example-block info
) info
))
1104 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1105 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1106 CONTENTS is nil. INFO is a plist holding contextual information."
1107 (org-element-get-property :value export-snippet
))
1112 (defun org-e-ascii-export-block (export-block contents info
)
1113 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1114 CONTENTS is nil. INFO is a plist holding contextual information."
1115 (when (string= (org-element-get-property :type export-block
) "ascii")
1116 (org-remove-indentation (org-element-get-property :value export-block
))))
1121 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1122 "Transcode a FIXED-WIDTH element from Org to ASCII.
1123 CONTENTS is nil. INFO is a plist holding contextual information."
1124 (org-e-ascii--box-string
1125 (replace-regexp-in-string
1126 "^[ \t]*: ?" "" (org-element-get-property :value fixed-width
)) info
))
1129 ;;;; Footnote Definition
1131 ;; Footnote Definitions are ignored. They are compiled at the end of
1132 ;; the document, by `org-e-ascii-template'.
1135 ;;;; Footnote Reference
1137 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1138 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1139 CONTENTS is nil. INFO is a plist holding contextual information."
1140 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1145 (defun org-e-ascii-headline (headline contents info
)
1146 "Transcode an HEADLINE element from Org to ASCII.
1147 CONTENTS holds the contents of the headline. INFO is a plist
1148 holding contextual information."
1149 ;; Don't export footnote section, which will be handled at the end
1151 (unless (org-element-get-property :footnote-section-p headline
)
1152 (let* ((low-level-rank (org-export-low-level-p headline info
))
1153 (width (org-e-ascii--current-text-width headline info
))
1154 ;; Blank lines between headline and its contents.
1155 ;; `org-e-ascii-headline-spacing', when set, overwrites
1156 ;; original buffer's spacing.
1159 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1160 (org-element-get-property :pre-blank headline
)) ?
\n))
1161 ;; Even if HEADLINE has no section, there might be some
1162 ;; links in its title that we shouldn't forget to describe.
1164 (unless (eq (caar (org-element-get-contents headline
)) 'section
)
1165 (org-e-ascii--describe-links
1166 (org-e-ascii--unique-links headline info
) width info
))))
1167 ;; Deep subtree: export it as a list item.
1171 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1172 org-e-ascii-bullets
))))
1174 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1177 (org-e-ascii--build-title headline info width
) "\n"
1178 ;; Contents, indented by length of bullet.
1180 (org-e-ascii--indent-string
1182 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1184 ;; Else: Standard headline.
1186 (org-e-ascii--build-title headline info width
'underline
)
1188 (concat (when (org-string-nw-p links
) links
) contents
))))))
1191 ;;;; Horizontal Rule
1193 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1194 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1195 CONTENTS is nil. INFO is a plist holding contextual
1203 (org-element-get-property :attr_ascii horizontal-rule
)
1205 (make-string (or (and (wholenump (plist-get attr
:width
))
1206 (plist-get attr
:width
))
1207 (org-e-ascii--current-text-width horizontal-rule info
))
1208 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1211 ;;;; Inline Babel Call
1213 ;; Inline Babel Calls are ignored.
1216 ;;;; Inline Src Block
1218 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1219 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1220 CONTENTS holds the contents of the item. INFO is a plist holding
1221 contextual information."
1222 (format org-e-ascii-verbatim-format
1223 (org-element-get-property :value inline-src-block
)))
1228 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1229 "Transcode an INLINETASK element from Org to ASCII.
1230 CONTENTS holds the contents of the block. INFO is a plist
1231 holding contextual information."
1232 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1233 (title (org-export-secondary-string
1234 (org-element-get-property :title inlinetask
) 'e-ascii info
))
1235 (todo (and (plist-get info
:with-todo-keywords
)
1236 (let ((todo (org-element-get-property
1237 :todo-keyword inlinetask
)))
1239 (org-export-secondary-string todo
'e-ascii info
)))))
1240 (todo-type (org-element-get-property :todo-type inlinetask
))
1241 (tags (and (plist-get info
:with-tags
)
1242 (org-element-get-property :tags inlinetask
)))
1243 (priority (and (plist-get info
:with-priority
)
1244 (org-element-get-property :priority inlinetask
))))
1245 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1246 ;; with appropriate arguments.
1247 (if (functionp org-e-ascii-format-inlinetask-function
)
1248 (funcall org-e-ascii-format-inlinetask-function
1249 todo todo-type priority title tags contents width
)
1250 ;; Otherwise, use a default template.
1251 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1252 (org-e-ascii--indent-string
1254 ;; Top line, with an additional blank line if not in UTF-8.
1255 (make-string width
(if utf8p ?━ ?_
)) "\n"
1256 (unless utf8p
(concat (make-string width ?
) "\n"))
1257 ;; Add title. Fill it if wider than inlinetask.
1258 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1259 (if (<= (length title
) width
) title
1260 (org-e-ascii--fill-string title width info
)))
1262 ;; If CONTENTS is not empty, insert it along with
1264 (when (org-string-nw-p contents
)
1265 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1267 (make-string width
(if utf8p ?━ ?_
)))
1268 ;; Flush the inlinetask to the right.
1269 (- org-e-ascii-text-width org-e-ascii-global-margin
1270 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1271 org-e-ascii-inner-margin
)
1272 (org-e-ascii--current-text-width inlinetask info
)))))))
1277 (defun org-e-ascii-item (item contents info
)
1278 "Transcode an ITEM element from Org to ASCII.
1279 CONTENTS holds the contents of the item. INFO is a plist holding
1280 contextual information."
1282 ;; First parent of ITEM is always the plain-list. Get
1283 ;; `:type' property from it.
1284 (let ((type (org-element-get-property
1285 :type
(car (plist-get info
:genealogy
)))))
1286 (if (eq type
'descriptive
)
1288 (org-export-secondary-string
1289 (org-element-get-property :tag item
) 'e-ascii info
) ": ")
1290 (org-element-get-property :bullet item
)))))
1292 ;; Change bullets into more visible form if UTF-8 is active.
1293 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bullet
1294 (replace-regexp-in-string
1296 (replace-regexp-in-string
1298 (replace-regexp-in-string
1300 ;; Contents: Pay attention to indentation. Note: check-boxes are
1301 ;; already taken care of at the paragraph level so they don't
1302 ;; interfere with indentation.
1303 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1304 (if (eq (caar (org-element-get-contents item
)) 'paragraph
)
1306 (concat "\n" contents
))))))
1311 (defun org-e-ascii-keyword (keyword contents info
)
1312 "Transcode a KEYWORD element from Org to ASCII.
1313 CONTENTS is nil. INFO is a plist holding contextual
1315 (let ((key (downcase (org-element-get-property :key keyword
)))
1316 (value (org-element-get-property :value keyword
)))
1318 ((string= key
"ascii") value
)
1319 ((string= key
"toc")
1320 (let ((value (downcase value
)))
1322 ((string-match "\\<headlines\\>" value
)
1323 (let ((depth (or (and (string-match "[0-9]+" value
)
1324 (string-to-number (match-string 0 value
)))
1325 (plist-get info
:with-toc
))))
1326 (org-e-ascii--build-toc
1327 info
(and (wholenump depth
) depth
) keyword
)))
1328 ((string= "tables" value
)
1329 (org-e-ascii--list-tables keyword info
))
1330 ((string= "listings" value
)
1331 (org-e-ascii--list-listings keyword info
)))))
1332 ((string= key
"include")
1333 (org-export-included-file keyword
'e-ascii info
)))))
1336 ;;;; Latex Environment
1338 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1339 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1340 CONTENTS is nil. INFO is a plist holding contextual
1342 (org-remove-indentation (org-element-get-property :value latex-environment
)))
1347 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1348 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1349 CONTENTS is nil. INFO is a plist holding contextual
1351 (org-element-get-property :value latex-fragment
))
1356 (defun org-e-ascii-line-break (line-break contents info
)
1357 "Transcode a LINE-BREAK object from Org to ASCII.
1358 CONTENTS is nil. INFO is a plist holding contextual
1359 information." hard-newline
)
1364 (defun org-e-ascii-link (link desc info
)
1365 "Transcode a LINK object from Org to ASCII.
1367 DESC is the description part of the link, or the empty string.
1368 INFO is a plist holding contextual information."
1369 (let ((raw-link (org-element-get-property :raw-link link
))
1370 (type (org-element-get-property :type link
)))
1372 ((string= type
"coderef")
1373 (let ((ref (org-element-get-property :path link
)))
1374 (format (org-export-get-coderef-format ref desc
)
1375 (org-export-resolve-coderef ref info
))))
1376 ;; Do not apply a special syntax on radio links.
1377 ((string= type
"radio") desc
)
1378 ;; Do not apply a special syntax on fuzzy links pointing to
1380 ((and (string= type
"fuzzy")
1381 (let ((path (org-element-get-property :path link
)))
1382 (loop for target in
(plist-get info
:target-list
)
1384 (org-element-get-property :raw-value target
)
1386 (if (org-string-nw-p desc
) desc raw-link
))
1388 (concat (format "[%s]" (if (org-string-nw-p desc
) desc raw-link
))
1389 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
)))))))
1394 (defun org-e-ascii-macro (macro contents info
)
1395 "Transcode a MACRO element from Org to ASCII.
1396 CONTENTS is nil. INFO is a plist holding contextual
1398 (org-export-expand-macro macro info
))
1403 (defun org-e-ascii-paragraph (paragraph contents info
)
1404 "Transcode a PARAGRAPH element from Org to ASCII.
1405 CONTENTS is the contents of the paragraph, as a string. INFO is
1406 the plist used as a communication channel."
1407 (org-e-ascii--fill-string
1408 (let ((parent (car (plist-get info
:genealogy
))))
1409 ;; If PARAGRAPH is the first one in a list element, be sure to
1410 ;; add the check-box in front of it, before any filling. Later,
1411 ;; it would interfere with line width.
1412 (if (and (eq (car parent
) 'item
)
1413 (equal (car (org-element-get-contents parent
)) paragraph
))
1414 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1415 (concat (case (org-element-get-property :checkbox parent
)
1416 (on (if utf8p
"☑ " "[X] "))
1417 (off (if utf8p
"☐ " "[ ] "))
1418 (trans (if utf8p
"☒ " "[-] ")))
1421 (org-e-ascii--current-text-width paragraph info
) info
))
1426 (defun org-e-ascii-plain-list (plain-list contents info
)
1427 "Transcode a PLAIN-LIST element from Org to ASCII.
1428 CONTENTS is the contents of the list. INFO is a plist holding
1429 contextual information."
1435 (defun org-e-ascii-plain-text (text info
)
1436 "Transcode a TEXT string from Org to ASCII.
1437 INFO is a plist used as a communication channel."
1438 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1439 (plist-get info
:with-special-strings
)))
1441 ;; Usual replacements in utf-8 with proper option set.
1442 (replace-regexp-in-string
1444 (replace-regexp-in-string
1446 (replace-regexp-in-string "---" "—" text
)))))
1449 ;;;; Property Drawer
1451 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1452 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1453 CONTENTS is nil. INFO is a plist used as a communication
1455 ;; The property drawer isn't exported but we want separating blank
1456 ;; lines nonetheless.
1462 (defun org-e-ascii-quote-block (quote-block contents info
)
1463 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1464 CONTENTS holds the contents of the block. INFO is a plist
1465 holding contextual information."
1466 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1467 (org-e-ascii--indent-string
1468 (org-remove-indentation
1469 (org-e-ascii--fill-string contents width info
))
1470 org-e-ascii-quote-margin
)))
1475 (defun org-e-ascii-quote-section (quote-section contents info
)
1476 "Transcode a QUOTE-SECTION element from Org to ASCII.
1477 CONTENTS is nil. INFO is a plist holding contextual information."
1478 (let ((width (org-e-ascii--current-text-width quote-section info
))
1480 (org-export-secondary-string
1481 (org-remove-indentation
1482 (org-element-get-property :value quote-section
)) 'e-ascii info
)))
1483 (org-e-ascii--indent-string
1485 (+ org-e-ascii-quote-margin
1486 ;; Don't apply inner margin if parent headline is low level.
1487 (let ((headline (org-export-get-parent-headline quote-section info
)))
1488 (if (org-export-low-level-p headline info
) 0
1489 org-e-ascii-inner-margin
))))))
1494 (defun org-e-ascii-radio-target (radio-target contents info
)
1495 "Transcode a RADIO-TARGET object from Org to ASCII.
1496 CONTENTS is the contents of the target. INFO is a plist holding
1497 contextual information."
1502 (defun org-e-ascii-section (section contents info
)
1503 "Transcode a SECTION element from Org to ASCII.
1504 CONTENTS is the contents of the section. INFO is a plist holding
1505 contextual information."
1506 (org-e-ascii--indent-string
1509 (when org-e-ascii-links-to-notes
1510 ;; Add list of links at the end of SECTION.
1511 (let ((links (org-e-ascii--describe-links
1512 (org-e-ascii--unique-links section info
)
1513 (org-e-ascii--current-text-width section info
) info
)))
1514 ;; Separate list of links and section contents.
1515 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1516 ;; Do not apply inner margin if parent headline is low level.
1517 (let ((headline (org-export-get-parent-headline section info
)))
1518 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1519 org-e-ascii-inner-margin
))))
1524 (defun org-e-ascii-special-block (special-block contents info
)
1525 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1526 CONTENTS holds the contents of the block. INFO is a plist
1527 holding contextual information."
1533 (defun org-e-ascii-src-block (src-block contents info
)
1534 "Transcode a SRC-BLOCK element from Org to ASCII.
1535 CONTENTS holds the contents of the item. INFO is a plist holding
1536 contextual information."
1537 (let ((caption (org-e-ascii--build-caption src-block info
)))
1539 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1540 (org-e-ascii--box-string (org-export-handle-code src-block info
) info
)
1541 (when (and caption
(not org-e-ascii-caption-above
))
1542 (concat "\n" caption
)))))
1544 ;;;; Statistics Cookie
1546 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1547 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1548 CONTENTS is nil. INFO is a plist holding contextual information."
1549 (org-element-get-property :value statistics-cookie
))
1554 (defun org-e-ascii-subscript (subscript contents info
)
1555 "Transcode a SUBSCRIPT object from Org to ASCII.
1556 CONTENTS is the contents of the object. INFO is a plist holding
1557 contextual information."
1558 (if (org-element-get-property :use-brackets-p subscript
)
1559 (format "_{%s}" contents
)
1560 (format "_%s" contents
)))
1565 (defun org-e-ascii-superscript (superscript contents info
)
1566 "Transcode a SUPERSCRIPT object from Org to ASCII.
1567 CONTENTS is the contents of the object. INFO is a plist holding
1568 contextual information."
1569 (if (org-element-get-property :use-brackets-p superscript
)
1570 (format "_{%s}" contents
)
1571 (format "_%s" contents
)))
1576 ;; While `org-e-ascii-table' is the callback function expected by
1577 ;; org-export mechanism, it requires four subroutines to display
1578 ;; tables accordingly to chosen charset, alignment and width
1581 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1582 ;; for each column in the table,
1583 ;; `org-e-ascii-table--vertical-separators' returns a vector
1584 ;; containing separators (or lack thereof),
1585 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1586 ;; depending on charset, separators and position within the tabl and
1587 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1588 ;; a given cell and width.
1590 (defun org-e-ascii-table (table contents info
)
1591 "Transcode a TABLE element from Org to ASCII.
1592 CONTENTS is nil. INFO is a plist holding contextual information."
1593 (let ((raw-table (org-element-get-property :raw-table table
))
1594 (caption (org-e-ascii--build-caption table info
)))
1596 ;; Possibly add a caption string above.
1597 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1598 ;; Insert table. Note: "table.el" tables are left unmodified.
1599 (if (eq (org-element-get-property :type table
) 'table.el
) raw-table
1600 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1601 ;; Extract information out of the raw table (TABLE-INFO)
1602 ;; and clean it (CLEAN-TABLE).
1603 (table-info (org-export-table-format-info raw-table
))
1604 (special-col-p (plist-get table-info
:special-column-p
))
1605 (alignment (plist-get table-info
:alignment
))
1606 (clean-table (org-export-clean-table raw-table special-col-p
))
1607 ;; Change table into lisp, much like `org-table-to-lisp',
1608 ;; being more careful about keeping the exact length of
1609 ;; cells, for alignment purpose.
1613 (if (string-match org-table-hline-regexp line
) 'hline
1614 (org-split-string (org-trim line
) "\\s-?|\\s-?")))
1615 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1616 ;; Compute real column widths.
1618 (org-e-ascii-table--column-width lisp-table table-info
))
1619 ;; Construct separators according to column groups.
1620 (separators (org-e-ascii-table--vertical-separators table-info
))
1621 ;; Build different `hline' strings, depending on
1622 ;; separators, column widths and position.
1624 (org-e-ascii-table--build-hline
1625 nil separators column-widths info
))
1627 (and utf8p
(org-e-ascii-table--build-hline
1628 'top separators column-widths info
)))
1630 (and utf8p
(org-e-ascii-table--build-hline
1631 'bottom separators column-widths info
))))
1632 ;; Now build table back, with correct alignment, considering
1633 ;; columns widths and separators.
1637 ((eq line
'hline
) hline-standard
)
1638 ((eq line
'hline-bottom
) hline-bottom
)
1639 ((eq line
'hline-top
) hline-top
)
1640 (t (loop for cell in line
1641 for col from
0 to
(length line
)
1644 (let ((sep (aref separators col
)))
1645 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1646 (org-e-ascii-table--format-cell
1647 cell col column-widths alignment info
)) into l
1650 (let ((sep (aref separators col
)))
1651 (if (and utf8p
(not (string= sep
""))) "│"
1653 ;; If charset is `utf-8', make sure lisp-table always starts
1654 ;; with `hline-top' and ends with `hline-bottom'.
1655 (if (not utf8p
) lisp-table
1658 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1662 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1664 '(hline-bottom)))) "\n")))
1665 ;; Possible add a caption string below.
1666 (when (and caption
(not org-e-ascii-caption-above
))
1667 (concat "\n" caption
)))))
1669 (defun org-e-ascii-table--column-width (table table-info
)
1670 "Return vector of TABLE columns width.
1672 TABLE is the Lisp representation of the Org table considered.
1673 TABLE-INFO holds information about the table. See
1674 `org-export-table-format-info'.
1676 Unlike to `:width' property from `org-export-table-format-info',
1677 the return value contains width of every column, not only those
1678 with a width cookie."
1679 (let* ((cookies (plist-get table-info
:width
))
1680 (width (make-vector (length cookies
) 0)))
1684 (unless (eq line
'hline
)
1685 (mapc (lambda (cell)
1686 (let ((len (length cell
)))
1687 (when (> len
(aref width idx
)) (aset width idx len
)))
1691 (unless org-e-ascii-table-widen-columns
1692 ;; When colums are not widened, width cookies have precedence
1693 ;; over string lengths. Thus, overwrite the latter with the
1695 (loop for w across cookies
1696 for idx from
0 to
(length cookies
)
1697 when w do
(aset width idx w
)))
1701 (defun org-e-ascii-table--vertical-separators (table-info)
1702 "Return a vector of strings for vertical separators.
1704 TABLE-INFO holds information about considered table. See
1705 `org-export-table-format-info'.
1707 Return value is a vector whose length is one more than the number
1708 of columns in the table. Special column, if any, is ignored."
1709 (let* ((colgroups (plist-get table-info
:column-groups
))
1710 (separators (make-vector (1+ (length colgroups
)) "")))
1711 (if org-e-ascii-table-keep-all-vertical-lines
1712 (make-vector (length separators
) "|")
1714 (mapc (lambda (group)
1715 (when (memq group
'(start start-end
))
1716 (aset separators column
"|"))
1717 (when (memq group
'(end start-end
))
1718 (aset separators
(1+ column
) "|"))
1721 ;; Remove unneeded special column.
1722 (if (not (plist-get table-info
:special-column-p
)) separators
1723 (substring separators
1))))))
1725 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1726 "Format CELL with column width and alignment constraints.
1728 CELL is the contents of the cell, as a string.
1730 COL is the column containing the cell considered.
1732 WIDTH is a vector holding every column width, as returned by
1733 `org-e-ascii-table--column-width'.
1735 ALIGNMENT is a vector containing alignment strings for every
1738 INFO is a plist used as a communication channel."
1739 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1740 (or (aref width col
) (length cell
)))))
1741 ;; When CELL is too large, it has to be truncated.
1742 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1743 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1744 (let* ((indent-tabs-mode nil
)
1745 (align (aref alignment col
))
1747 (org-e-ascii--justify-string
1748 (org-trim cell
) col-width
1749 (cond ((string= align
"c") 'center
)
1750 ((string= align
"r") 'right
)))))
1751 ;; Return aligned cell, with missing white spaces added and
1752 ;; space separators between columns.
1755 (concat aligned-cell
1756 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1758 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1759 "Return string used as an horizontal line in tables.
1761 POSITION is a symbol among `top', `bottom' and nil, which
1762 specifies position of the horizontal line within the table.
1764 SEPARATORS is a vector strings specifying separators used in the
1765 table, as returned by `org-e-ascii-table--vertical-separators'.
1767 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1768 columns in the table, as returned by
1769 `org-e-ascii-table--column-width'.
1771 INFO is a plist used as a communication channel."
1772 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1773 (loop for idx from
0 to
(length separators
)
1774 for width across column-widths
1777 (cond ((string= (aref separators idx
) "") nil
)
1778 ((and utf8p
(zerop idx
))
1779 (cond ((eq position
'top
) "┍")
1780 ((eq position
'bottom
) "┕")
1783 (cond ((eq position
'top
) "┯")
1784 ((eq position
'bottom
) "┷")
1787 ;; Hline has to cover all the cell and
1788 ;; both white spaces between columns.
1789 (make-string (+ width
2)
1790 (cond ((not utf8p
) ?-
)
1798 ((string= (aref separators idx
) "") nil
)
1799 (utf8p (cond ((eq position
'top
) "┑")
1800 ((eq position
'bottom
) "┙")
1807 (defun org-e-ascii-target (target contents info
)
1808 "Transcode a TARGET object from Org to ASCII.
1809 CONTENTS is the contents of the target. INFO is a plist holding
1810 contextual information."
1816 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1817 "Transcode a TIME-STAMP object from Org to ASCII.
1818 CONTENTS is nil. INFO is a plist holding contextual information."
1819 ;; Return time-stamps as-is.
1820 (org-element-time-stamp-interpreter time-stamp contents
))
1825 (defun org-e-ascii-verbatim (verbatim contents info
)
1826 "Return a VERBATIM object from Org to ASCII.
1827 CONTENTS is nil. INFO is a plist holding contextual information."
1828 (format org-e-ascii-verbatim-format
1829 (org-element-get-property :value verbatim
)))
1834 (defun org-e-ascii-verse-block (verse-block contents info
)
1835 "Transcode a VERSE-BLOCK element from Org to ASCII.
1836 CONTENTS is nil. INFO is a plist holding contextual information."
1837 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1838 (org-e-ascii--indent-string
1839 (org-e-ascii--justify-string
1840 (org-export-secondary-string
1841 (org-element-get-property :value verse-block
) 'e-ascii info
)
1843 org-e-ascii-quote-margin
)))
1848 (defun org-e-ascii-filter-headline-blank-lines (headline back-end
)
1849 "Filter controlling number of blank lines after an headline.
1851 HEADLINE is a string representing a transcoded headline.
1852 BACK-END is symbol specifying back-end used for export.
1854 This function only applies to `e-ascii' back-end. See
1855 `org-e-ascii-headline-spacing' for information.
1857 For any other back-end, HEADLINE is returned as-is."
1858 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1859 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1860 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1864 ;;; Interactive function
1866 (defun org-e-ascii-export-to-ascii
1867 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1868 "Export current buffer to a text file.
1870 If narrowing is active in the current buffer, only export its
1873 If a region is active, export that region.
1875 When optional argument SUBTREEP is non-nil, export the sub-tree
1876 at point, extracting information from the headline properties
1879 When optional argument VISIBLE-ONLY is non-nil, don't export
1880 contents of hidden elements.
1882 When optional argument BODY-ONLY is non-nil, strip title, table
1883 of contents and footnote definitions from output.
1885 EXT-PLIST, when provided, is a property list with external
1886 parameters overriding Org default settings, but still inferior to
1887 file-local settings.
1889 When optional argument PUB-DIR is set, use it as the publishing
1892 Return output file's name."
1894 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1896 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1899 (provide 'org-e-ascii
)
1900 ;;; org-e-ascii.el ends here