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-contents
"org-element" (element))
38 (declare-function org-element-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
))
43 (declare-function org-export-collect-footnote-definitions
44 "org-export" (data info
))
45 (declare-function org-export-collect-headlines
"org-export" (info &optional n
))
46 (declare-function org-export-collect-listings
"org-export" (info))
47 (declare-function org-export-collect-tables
"org-export" (info))
48 (declare-function org-export-data
"org-export" (data info
))
49 (declare-function org-export-expand-macro
"org-export" (macro info
))
50 (declare-function org-export-format-code-default
"org-export" (element info
))
51 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
52 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
53 (declare-function org-export-get-headline-number
"org-export" (headline info
))
54 (declare-function org-export-get-ordinal
"org-export"
55 (element info
&optional types predicate
))
56 (declare-function org-export-get-parent-headline
"org-export" (blob info
))
57 (declare-function org-export-get-relative-level
"org-export" (headline info
))
58 (declare-function org-export-low-level-p
"org-export" (headline info
))
59 (declare-function org-export-output-file-name
"org-export"
60 (extension &optional subtreep pub-dir
))
61 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
62 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
63 (declare-function org-export-resolve-id-link
"org-export" (link info
))
64 (declare-function org-export-resolve-ref-link
"org-export" (link info
))
66 org-export-to-file
"org-export"
67 (backend file
&optional subtreep visible-only body-only ext-plist
))
71 ;;; Internal Variables
73 ;; The following setting won't allow to modify preferred charset
74 ;; through a buffer keyword or an option item, but, since the property
75 ;; will appear in communication channel nonetheless, it allows to
76 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
79 ;; We also install a filter for headlines and sections, in order to
80 ;; control blank lines separating them in output string.
82 (defconst org-e-ascii-option-alist
83 '((:ascii-charset nil nil org-e-ascii-charset
))
84 "Alist between ASCII export properties and ways to set them.
85 See `org-export-option-alist' for more information on the
86 structure or the values.")
88 (defconst org-e-ascii-filters-alist
89 '((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
90 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
91 "Alist between filters keywords and back-end specific filters.
92 See `org-export-filters-alist' for more information.")
94 (defconst org-e-ascii-dictionary
101 :ascii
"Notes de bas de page\n"
102 :latin1
"Notes de bas de page\n"
103 :utf-8
"Notes de bas de page\n"))
106 :ascii
"Listing %d: %s"
107 :latin1
"Listing %d: %s"
108 :utf-8
"Listing %d: %s")
110 :ascii
"Programme %d : %s"
111 :latin1
"Programme %d : %s"
112 :utf-8
"Programme nº %d : %s"))
113 ("List Of Listings\n"
115 :ascii
"List Of Listings\n"
116 :latin1
"List Of Listings\n"
117 :utf-8
"List Of Listings\n")
119 :ascii
"Liste des programmes\n"
120 :latin1
"Liste des programmes\n"
121 :utf-8
"Liste des programmes\n"))
124 :ascii
"List Of Tables\n"
125 :latin1
"List Of Tables\n"
126 :utf-8
"List Of Tables\n")
128 :ascii
"Liste des tableaux\n"
129 :latin1
"Liste des tableaux\n"
130 :utf-8
"Liste des tableaux\n"))
133 :ascii
"Listing %d: "
134 :latin1
"Listing %d: "
135 :utf-8
"Listing %d: ")
137 :ascii
"Programme %d : "
138 :latin1
"Programme %d : "
139 :utf-8
"Programme nº %d : "))
140 ("Table Of Contents\n"
142 :ascii
"Table Of Contents\n"
143 :latin1
"Table Of Contents\n"
144 :utf-8
"Table Of Contents\n")
147 :latin1
"Table des matières\n"
148 :utf-8
"Table des matières\n"))
151 :ascii
"Table %d: %s"
152 :latin1
"Table %d: %s"
153 :utf-8
"Table %d: %s")
155 :ascii
"Tableau %d : %s"
156 :latin1
"Tableau %d : %s"
157 :utf-8
"Tableau nº %d : %s"))
160 :ascii
"See section %s"
161 :latin1
"See section %s"
162 :utf-8
"See section %s")
164 :ascii
"cf. section %s"
165 :latin1
"cf. section %s"
166 :utf-8
"cf. section %s"))
173 :ascii
"Tableau %d : "
174 :latin1
"Tableau %d : "
175 :utf-8
"Tableau nº %d : "))
178 :ascii
"Unknown reference"
179 :latin1
"Unknown reference"
180 :utf-8
"Unknown reference")
182 :ascii
"Destination inconnue"
183 :latin1
"Référence inconnue"
184 :utf-8
"Référence inconnue")))
185 "Dictionary for ASCII back-end.
187 Alist whose car is the string to translate and cdr is an alist
188 whose car is the language string and cdr is a plist whose
189 properties are possible charsets and value the translated term.
191 It is used as a database for `org-e-ascii--translate'.")
195 ;;; User Configurable Variables
197 (defgroup org-export-e-ascii nil
198 "Options for exporting Org mode files to ASCII."
199 :tag
"Org Export ASCII"
202 (defcustom org-e-ascii-text-width
72
203 "Maximum width of exported text.
204 This number includes margin size, as set in
205 `org-e-ascii-global-margin'."
206 :group
'org-export-e-ascii
209 (defcustom org-e-ascii-global-margin
0
210 "Width of the left margin, in number of characters."
211 :group
'org-export-e-ascii
214 (defcustom org-e-ascii-inner-margin
2
215 "Width of the inner margin, in number of characters.
216 Inner margin is applied between each headline."
217 :group
'org-export-e-ascii
220 (defcustom org-e-ascii-quote-margin
6
221 "Width of margin used for quoting text, in characters.
222 This margin is applied on both sides of the text."
223 :group
'org-export-e-ascii
226 (defcustom org-e-ascii-inlinetask-width
30
227 "Width of inline tasks, in number of characters.
228 This number ignores any margin."
229 :group
'org-export-e-ascii
232 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
233 "Number of blank lines inserted around headlines.
235 This variable can be set to a cons cell. In that case, its car
236 represents the number of blank lines present before headline
237 contents whereas its cdr reflects the number of blank lines after
240 A nil value replicates the number of blank lines found in the
241 original Org buffer at the same place."
242 :group
'org-export-e-ascii
244 (const :tag
"Replicate original spacing" nil
)
245 (cons :tag
"Set an uniform spacing"
246 (integer :tag
"Number of blank lines before contents")
247 (integer :tag
"Number of blank lines after contents"))))
249 (defcustom org-e-ascii-charset
'ascii
250 "The charset allowed to represent various elements and objects.
252 `ascii' Only use plain ASCII characters
253 `latin1' Include Latin-1 characters
254 `utf-8' Use all UTF-8 characters"
255 :group
'org-export-e-ascii
257 (const :tag
"ASCII" ascii
)
258 (const :tag
"Latin-1" latin1
)
259 (const :tag
"UTF-8" utf-8
)))
261 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
263 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
264 "Characters for underlining headings in ASCII export.
266 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
267 and whose value is a list of characters.
269 For each supported charset, this variable associates a sequence
270 of underline characters. In a sequence, the characters will be
271 used in order for headlines level 1, 2, ... If no character is
272 available for a given level, the headline won't be underlined."
273 :group
'org-export-e-ascii
275 (cons :tag
"Underline characters sequence"
276 (const :tag
"ASCII charset" ascii
)
278 (cons :tag
"Underline characters sequence"
279 (const :tag
"Latin-1 charset" latin1
)
281 (cons :tag
"Underline characters sequence"
282 (const :tag
"UTF-8 charset" utf-8
)
283 (repeat character
))))
285 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
288 "Bullet characters for headlines converted to lists in ASCII export.
290 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
291 and whose value is a list of characters.
293 The first character is used for the first level considered as low
294 level, and so on. If there are more levels than characters given
295 here, the list will be repeated.
297 Note that this variable doesn't affect plain lists
299 :group
'org-export-e-ascii
301 (cons :tag
"Bullet characters for low level headlines"
302 (const :tag
"ASCII charset" ascii
)
304 (cons :tag
"Bullet characters for low level headlines"
305 (const :tag
"Latin-1 charset" latin1
)
307 (cons :tag
"Bullet characters for low level headlines"
308 (const :tag
"UTF-8 charset" utf-8
)
309 (repeat character
))))
311 (defcustom org-e-ascii-links-to-notes t
312 "Non-nil means convert links to notes before the next headline.
313 When nil, the link will be exported in place. If the line
314 becomes long in this way, it will be wrapped."
315 :group
'org-export-e-ascii
318 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
319 "Non-nil means keep all vertical lines in ASCII tables.
320 When nil, vertical lines will be removed except for those needed
321 for column grouping."
322 :group
'org-export-e-ascii
325 (defcustom org-e-ascii-table-widen-columns t
326 "Non-nil means widen narrowed columns for export.
327 When nil, narrowed columns will look in ASCII export just like in
328 Org mode, i.e. with \"=>\" as ellipsis."
329 :group
'org-export-e-ascii
332 (defcustom org-e-ascii-caption-above nil
333 "When non-nil, place caption string before the element.
334 Otherwise, place it right after it."
335 :group
'org-export-e-ascii
338 (defcustom org-e-ascii-verbatim-format
"`%s'"
339 "Format string used for verbatim text and inline code."
340 :group
'org-export-e-ascii
343 (defcustom org-e-ascii-format-drawer-function nil
344 "Function called to format a drawer in ASCII.
346 The function must accept two parameters:
347 NAME the drawer name, like \"LOGBOOK\"
348 CONTENTS the contents of the drawer.
349 WIDTH the text width within the drawer.
351 The function should return either the string to be exported or
352 nil to ignore the drawer.
354 For example, the variable could be set to the following function
355 in order to mimic default behaviour:
357 \(defun org-e-ascii-format-drawer-default \(name contents width\)
358 \"Format a drawer element for ASCII export.\"
360 :group
'org-export-e-ascii
363 (defcustom org-e-ascii-format-inlinetask-function nil
364 "Function called to format an inlinetask in ASCII.
366 The function must accept six parameters:
367 TODO the todo keyword, as a string
368 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
369 PRIORITY the inlinetask priority, as a string
370 NAME the inlinetask name, as a string.
371 TAGS the inlinetask tags, as a string.
372 CONTENTS the contents of the inlinetask, as a string.
374 The function should return either the string to be exported or
375 nil to ignore the inline task.
377 For example, the variable could be set to the following function
378 in order to mimic default behaviour:
380 \(defun org-e-ascii-format-inlinetask-default
381 \(todo type priority name tags contents\)
382 \"Format an inline task element for ASCII export.\"
383 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
384 \(width org-e-ascii-inlinetask-width\)
385 \(org-e-ascii--indent-string
387 ;; Top line, with an additional blank line if not in UTF-8.
388 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
389 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
390 ;; Add title. Fill it if wider than inlinetask.
391 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
392 \(if \(<= \(length title\) width\) title
393 \(org-e-ascii--fill-string title width info\)\)\)
395 ;; If CONTENTS is not empty, insert it along with
397 \(when \(org-string-nw-p contents\)
398 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
400 \(make-string width \(if utf8p ?━ ?_\)\)\)
401 ;; Flush the inlinetask to the right.
402 \(- \(plist-get info :ascii-width\)
403 \(plist-get info :ascii-margin\)
404 \(plist-get info :ascii-inner-margin\)
405 \(org-e-ascii--current-text-width inlinetask info\)\)"
406 :group
'org-export-e-ascii
411 ;;; Internal Functions
413 ;; Internal functions fall into three categories.
415 ;; The first one is about text formatting. The core function is
416 ;; `org-e-ascii--current-text-width', which determines the current
417 ;; text width allowed to a given element. In other words, it helps
418 ;; keeping each line width within maximum text width defined in
419 ;; `org-e-ascii-text-width'. Once this information is known,
420 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
421 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
422 ;; operate on a given output string.
424 ;; The second category contains functions handling elements listings,
425 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
426 ;; returns a complete table of contents, `org-e-ascii--list-listings'
427 ;; returns a list of referenceable src-block elements, and
428 ;; `org-e-ascii--list-tables' does the same for table elements.
430 ;; The third category includes general helper functions.
431 ;; `org-e-ascii--build-title' creates the title for a given headline
432 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
433 ;; caption string associated to a table or a src-block.
434 ;; `org-e-ascii--describe-links' creates notes about links for
435 ;; insertion at the end of a section. It uses
436 ;; `org-e-ascii--unique-links' to get the list of links to describe.
437 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
438 ;; to internationalize output.
441 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
442 "Fill a string with specified text-width and return it.
444 S is the string being filled. TEXT-WIDTH is an integer
445 specifying maximum length of a line. INFO is the plist used as
446 a communication channel.
448 Optional argument JUSTIFY can specify any type of justification
449 among `left', `center', `right' or `full'. A nil value is
450 equivalent to `left'. For a justification that doesn't also fill
451 string, see `org-e-ascii--justify-string'.
453 Return nil if S isn't a string."
454 ;; Don't fill paragraph when break should be preserved.
455 (cond ((not (stringp s
)) nil
)
456 ((plist-get info
:preserve-breaks
) s
)
458 (let ((fill-column text-width
)
459 (use-hard-newlines t
))
461 (fill-region (point-min) (point-max) justify
))
464 (defun org-e-ascii--justify-string (s text-width how
)
466 TEXT-WIDTH is an integer specifying maximum length of a line.
467 HOW determines the type of justification: it can be `left',
468 `right', `full' or `center'."
471 (goto-char (point-min))
472 (let ((fill-column text-width
))
473 (while (< (point) (point-max))
474 (justify-current-line how
)
478 (defun org-e-ascii--indent-string (s width
)
479 "Indent string S by WIDTH white spaces.
480 Empty lines are not indented."
482 (replace-regexp-in-string
483 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
485 (defun org-e-ascii--box-string (s info
)
486 "Return string S with a partial box to its left.
487 INFO is a plist used as a communicaton channel."
488 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
489 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
490 (replace-regexp-in-string
491 "^" (if utf8p
"│ " "| ")
492 ;; Remove last newline character.
493 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
495 (defun org-e-ascii--current-text-width (element info
)
496 "Return maximum text width for ELEMENT's contents.
497 INFO is a plist used as a communication channel."
498 (case (org-element-type element
)
499 ;; Elements with an absolute width: `headline' and `inlinetask'.
500 (inlinetask org-e-ascii-inlinetask-width
)
502 (- org-e-ascii-text-width
503 (let ((low-level-rank (org-export-low-level-p element info
)))
504 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
505 ;; Elements with a relative width: store maximum text width in
508 (let* ((genealogy (cons element
(org-export-get-genealogy element info
)))
509 ;; Total width is determined by the presence, or not, of an
510 ;; inline task among ELEMENT parents.
512 (if (loop for parent in genealogy
513 thereis
(eq (org-element-type parent
) 'inlinetask
))
514 org-e-ascii-inlinetask-width
515 ;; No inlinetask: Remove global margin from text width.
516 (- org-e-ascii-text-width
517 org-e-ascii-global-margin
518 (let ((parent (org-export-get-parent-headline element info
)))
519 ;; Inner margin doesn't apply to text before first
522 (let ((low-level-rank
523 (org-export-low-level-p parent info
)))
524 ;; Inner margin doesn't apply to contents of
525 ;; low level headlines, since they've got their
526 ;; own indentation mechanism.
527 (if low-level-rank
(* low-level-rank
2)
528 org-e-ascii-inner-margin
))))))))
530 ;; Each `quote-block', `quote-section' and `verse-block' above
531 ;; narrows text width by twice the standard margin size.
532 (+ (* (loop for parent in genealogy
533 when
(memq (org-element-type parent
)
534 '(quote-block quote-section verse-block
))
536 2 org-e-ascii-quote-margin
)
537 ;; Text width within a plain-list is restricted by
538 ;; indentation of current item. If that's the case,
539 ;; compute it with the help of `:structure' property from
540 ;; parent item, if any.
542 (if (eq (org-element-type element
) 'item
) element
543 (loop for parent in genealogy
544 when
(eq (org-element-type parent
) 'item
)
546 (if (not parent-item
) 0
547 ;; Compute indentation offset of the current item,
548 ;; that is the sum of the difference between its
549 ;; indentation and the indentation of the top item in
550 ;; the list and current item bullet's length. Also
551 ;; remove tag length (for description lists) or bullet
553 (let ((struct (org-element-property :structure parent-item
))
554 (beg-item (org-element-property :begin parent-item
)))
555 (+ (- (org-list-get-ind beg-item struct
)
557 (org-list-get-top-point struct
) struct
))
559 (or (org-list-get-tag beg-item struct
)
560 (org-list-get-bullet beg-item struct
)))))))))))))
562 (defun org-e-ascii--build-title
563 (element info text-width
&optional underline notags
)
564 "Format ELEMENT title and return it.
566 ELEMENT is either an `headline' or `inlinetask' element. INFO is
567 a plist used as a communication channel. TEXT-WIDTH is an
568 integer representing the maximum length of a line.
570 When optional argument UNDERLINE is non-nil, underline title,
571 without the tags, according to `org-e-ascii-underline'
574 if optional argument NOTAGS is nil, no tags will be added to the
576 (let* ((headlinep (eq (org-element-type element
) 'headline
))
578 ;; Numbering is specific to headlines.
579 (and headlinep
(org-export-numbered-headline-p element info
)
580 ;; All tests passed: build numbering string.
584 (org-export-get-headline-number element info
) ".")
586 (text (org-export-data (org-element-property :title element
) info
))
588 (and (plist-get info
:with-todo-keywords
)
589 (let ((todo (org-element-property :todo-keyword element
)))
590 (and todo
(concat (org-export-data todo info
) " ")))))
591 (tags (and (not notags
)
592 (plist-get info
:with-tags
)
593 (org-element-property :tags element
)))
595 (and (plist-get info
:with-priority
)
596 (concat (org-element-property :priority element
) " ")))
597 (first-part (concat numbers todo priority text
)))
600 ;; Align tags, if any.
604 (max (- text-width
(1+ (length first-part
))) (length tags
)))
606 ;; Maybe underline text, if ELEMENT type is `headline' and an
607 ;; underline character has been defined.
608 (when (and underline headlinep
)
610 (nth (1- (org-export-get-relative-level element info
))
611 (cdr (assq (plist-get info
:ascii-charset
)
612 org-e-ascii-underline
)))))
615 (make-string (length first-part
) under-char
))))))))
617 (defun org-e-ascii--build-caption (element info
)
618 "Return caption string for ELEMENT, if applicable.
620 INFO is a plist used as a communication channel.
622 The caption string contains the sequence number of ELEMENT if it
623 has a name affiliated keyword, along with the real caption, if
624 any. Return nil when ELEMENT has no affiliated caption or name
626 (let ((caption (org-element-property :caption element
))
627 (name (org-element-property :name element
)))
628 (when (or caption name
)
629 ;; Get sequence number of current src-block among every
630 ;; src-block with either a caption or a name.
632 (org-export-get-ordinal
634 (lambda (el info
) (or (org-element-property :caption el
)
635 (org-element-property :name el
)))))
636 (title-fmt (org-e-ascii--translate
637 (case (org-element-type element
)
638 (table "Table %d: %s")
639 (src-block "Listing %d: %s")) info
)))
640 (org-e-ascii--fill-string
643 (if (not caption
) name
(org-export-data (car caption
) info
)))
644 (org-e-ascii--current-text-width element info
) info
)))))
646 (defun org-e-ascii--build-toc (info &optional n keyword
)
647 "Return a table of contents.
649 INFO is a plist used as a communication channel.
651 Optional argument N, when non-nil, is an integer specifying the
654 Optional argument KEYWORD specifies the TOC keyword, if any, from
655 which the table of contents generation has been initiated."
656 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
659 (make-string (1- (length title
))
660 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
663 (if keyword
(org-e-ascii--current-text-width keyword info
)
664 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
667 (let* ((level (org-export-get-relative-level headline info
))
668 (indent (* (1- level
) 3)))
670 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
671 (org-e-ascii--build-title
672 headline info
(- text-width indent
) nil
673 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
674 (org-export-collect-headlines info n
) "\n")))))
676 (defun org-e-ascii--list-listings (keyword info
)
677 "Return a list of listings.
679 KEYWORD is the keyword that initiated the list of listings
680 generation. INFO is a plist used as a communication channel."
681 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
684 (make-string (1- (length title
))
685 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
688 (if keyword
(org-e-ascii--current-text-width keyword info
)
689 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
690 ;; Use a counter instead of retreiving ordinal of each
695 ;; Store initial text so its length can be computed. This is
696 ;; used to properly align caption right to it in case of
697 ;; filling (like contents of a description list item).
699 (format (org-e-ascii--translate "Listing %d: " info
)
704 (org-e-ascii--indent-string
705 (org-e-ascii--fill-string
706 (let ((caption (org-element-property :caption src-block
)))
707 (if (not caption
) (org-element-property :name src-block
)
708 ;; Use short name in priority, if available.
709 (org-export-data (or (cdr caption
) (car caption
)) info
)))
710 (- text-width
(length initial-text
)) info
)
711 (length initial-text
))))))
712 (org-export-collect-listings info
) "\n")))))
714 (defun org-e-ascii--list-tables (keyword info
)
715 "Return a list of listings.
717 KEYWORD is the keyword that initiated the list of listings
718 generation. INFO is a plist used as a communication channel."
719 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
722 (make-string (1- (length title
))
723 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
726 (if keyword
(org-e-ascii--current-text-width keyword info
)
727 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
728 ;; Use a counter instead of retreiving ordinal of each
733 ;; Store initial text so its length can be computed. This is
734 ;; used to properly align caption right to it in case of
735 ;; filling (like contents of a description list item).
737 (format (org-e-ascii--translate "Table %d: " info
)
742 (org-e-ascii--indent-string
743 (org-e-ascii--fill-string
744 (let ((caption (org-element-property :caption table
)))
745 (if (not caption
) (org-element-property :name table
)
746 ;; Use short name in priority, if available.
747 (org-export-data (or (cdr caption
) (car caption
)) info
)))
748 (- text-width
(length initial-text
)) info
)
749 (length initial-text
))))))
750 (org-export-collect-tables info
) "\n")))))
752 (defun org-e-ascii--unique-links (element info
)
753 "Return a list of unique link references in ELEMENT.
755 ELEMENT is either an headline element or a section element. INFO
756 is a plist used as a communication channel.
758 It covers links that may be found current headline's title, in
759 the following section and in any inlinetask's title there."
763 ;; Return LINK if it wasn't referenced so far, or nil.
764 ;; Update SEEN links along the way.
767 (cons (org-element-property :raw-link link
)
768 (org-element-contents link
))))
769 (unless (member footprint seen
)
770 (push footprint seen
) link
)))))
771 (harvest-links-in-title
773 ;; Return a list of all unique links in ELEMENT. ELEMENT
774 ;; may be an headline or an inlinetask element.
777 (dolist (obj (org-element-property :title element
) acc
)
778 (when (eq (org-element-type obj
) 'link
)
779 (let ((link (funcall unique-link-p obj
)))
780 (and link
(push link acc
)))))))))
781 ;; Retrieve HEADLINE's section, if it exists.
782 (section (if (eq (org-element-type element
) 'section
) element
783 (let ((sec (car (org-element-contents element
))))
784 (and (eq (org-element-type sec
) 'section
) sec
))))
785 (headline (if (eq (org-element-type element
) 'headline
) element
786 (org-export-get-parent-headline element info
))))
788 ;; Links that may be in HEADLINE's title.
789 (funcall harvest-links-in-title headline
)
790 ;; Get all links in SECTION.
792 section
'link
(lambda (link) (funcall unique-link-p link
)) info
))))
794 (defun org-e-ascii--describe-links (links width info
)
795 "Return a string describing a list of links.
797 LINKS is a list of link type objects, as returned by
798 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
799 the output string. INFO is a plist used as a communication
803 (let ((type (org-element-property :type link
))
804 (anchor (let ((desc (org-element-contents link
)))
805 (if (not desc
) (org-element-property :raw-link link
)
806 (org-export-data desc info
)))))
808 ;; Coderefs, radio links and fuzzy links are ignored.
809 ((member type
'("coderef" "radio" "fuzzy")) nil
)
810 ;; Id and custom-id links: Headlines refer to their numbering.
811 ((member type
'("custom-id" "id"))
812 (let ((dest (org-export-resolve-id-link link info
)))
814 (org-e-ascii--fill-string
818 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
820 (org-e-ascii--translate "See section %s" info
)
821 (mapconcat 'number-to-string
822 (org-export-get-headline-number dest info
) "."))))
823 width info
) "\n\n")))
824 ;; Do not add a link that cannot be resolved and doesn't have
825 ;; any description: destination is already visible in the
827 ((not (org-element-contents link
)) nil
)
830 (org-e-ascii--fill-string
831 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
840 (defun org-e-ascii-template--document-title (info)
841 "Return document title, as a string.
842 INFO is a plist used as a communication channel."
843 (let ((text-width org-e-ascii-text-width
)
844 (title (org-export-data (plist-get info
:title
) info
))
845 (author (and (plist-get info
:with-author
)
846 (let ((auth (plist-get info
:author
)))
847 (and auth
(org-export-data auth info
)))))
848 (email (and (plist-get info
:with-email
)
849 (org-export-data (plist-get info
:email
) info
)))
850 (date (plist-get info
:date
)))
851 ;; There are two types of title blocks depending on the presence
852 ;; of a title to display.
853 (if (string= title
"")
854 ;; Title block without a title. DATE is positioned at the top
855 ;; right of the document, AUTHOR to the top left and EMAIL
858 ((and (org-string-nw-p date
) (org-string-nw-p author
))
861 (make-string (- text-width
(length date
) (length author
)) ?
)
863 (when (org-string-nw-p email
) (concat "\n" email
))
865 ((and (org-string-nw-p date
) (org-string-nw-p email
))
868 (make-string (- text-width
(length date
) (length email
)) ?
)
870 ((org-string-nw-p date
)
872 (org-e-ascii--justify-string date text-width
'right
)
874 ((and (org-string-nw-p author
) (org-string-nw-p email
))
875 (concat author
"\n" email
"\n\n\n"))
876 ((org-string-nw-p author
) (concat author
"\n\n\n"))
877 ((org-string-nw-p email
) (concat email
"\n\n\n")))
878 ;; Title block with a title. Document's TITLE, along with the
879 ;; AUTHOR and its EMAIL are both overlined and an underlined,
880 ;; centered. Date is just below, also centered.
881 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
882 ;; Format TITLE. It may be filled if it is too wide,
883 ;; that is wider than the two thirds of the total width.
884 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
885 (formatted-title (org-e-ascii--fill-string title title-len info
))
888 (min (+ (max title-len
(length author
) (length email
)) 2)
889 text-width
) (if utf8p ?━ ?_
))))
890 (org-e-ascii--justify-string
893 (upcase formatted-title
)
895 ((and (org-string-nw-p author
) (org-string-nw-p email
))
896 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
897 ((org-string-nw-p author
)
898 (concat (if utf8p
"\n\n\n" "\n\n") author
))
899 ((org-string-nw-p email
)
900 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
902 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
903 "\n\n\n") text-width
'center
)))))
905 (defun org-e-ascii-template (contents info
)
906 "Return complete document string after ASCII conversion.
907 CONTENTS is the transcoded contents string. INFO is a plist
908 holding export options."
909 (org-element-normalize-string
910 (org-e-ascii--indent-string
911 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
912 ;; 1. Build title block.
914 (org-e-ascii-template--document-title info
)
915 ;; 2. Table of contents.
916 (let ((depth (plist-get info
:with-toc
)))
919 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
921 ;; 3. Document's body.
923 ;; 4. Footnote definitions.
924 (let ((definitions (org-export-collect-footnote-definitions
925 (plist-get info
:parse-tree
) info
))
926 ;; Insert full links right inside the footnote definition
927 ;; as they have no chance to be inserted later.
928 (org-e-ascii-links-to-notes nil
))
932 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
937 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
941 (let ((id (format "[%s] " (car ref
))))
942 ;; Distinguish between inline definitions and
943 ;; full-fledged definitions.
945 (let ((def (nth 2 ref
)))
946 (if (eq (org-element-type def
) 'org-data
)
947 ;; Full-fledged definition: footnote ID is
948 ;; inserted inside the first parsed paragraph
949 ;; (FIRST), if any, to be sure filling will
950 ;; take it into consideration.
951 (let ((first (car (org-element-contents def
))))
952 (if (not (eq (org-element-type first
) 'paragraph
))
953 (concat id
"\n" (org-export-data def info
))
954 (push id
(nthcdr 2 first
))
955 (org-export-data def info
)))
956 ;; Fill paragraph once footnote ID is inserted in
957 ;; order to have a correct length for first line.
958 (org-e-ascii--fill-string
959 (concat id
(org-export-data def info
))
960 text-width info
))))))
961 definitions
"\n\n"))))
962 ;; 5. Creator. Ignore `comment' value as there are no comments in
963 ;; ASCII. Justify it to the bottom right.
964 (let ((creator-info (plist-get info
:with-creator
)))
965 (unless (or (not creator-info
) (eq creator-info
'comment
))
968 (org-e-ascii--fill-string
969 (plist-get info
:creator
) text-width info
'right
))))))
970 org-e-ascii-global-margin
)))
972 (defun org-e-ascii--translate (s info
)
975 INFO is a plist used as a communication channel.
977 Translation depends on `:language' property and allowed charset.
978 If no translation in found for a given language and a given
979 charset, fall-back to S."
980 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
981 (lang (plist-get info
:language
))
982 (translations (cdr (assoc s org-e-ascii-dictionary
))))
983 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
987 ;;; Transcode Functions
991 ;; Babel Calls are ignored.
996 (defun org-e-ascii-bold (bold contents info
)
997 "Transcode BOLD from Org to ASCII.
998 CONTENTS is the text with bold markup. INFO is a plist holding
999 contextual information."
1000 (format "*%s*" contents
))
1005 (defun org-e-ascii-center-block (center-block contents info
)
1006 "Transcode a CENTER-BLOCK element from Org to ASCII.
1007 CONTENTS holds the contents of the block. INFO is a plist
1008 holding contextual information."
1009 (org-e-ascii--justify-string
1010 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1015 (defun org-e-ascii-clock (clock contents info
)
1016 "Transcode a CLOCK object from Org to ASCII.
1017 CONTENTS is nil. INFO is a plist holding contextual
1019 (concat org-clock-string
" "
1020 (org-translate-time (org-element-property :value clock
))
1021 (let ((time (org-element-property :time clock
)))
1026 (org-split-string time
":")))))))
1031 (defun org-e-ascii-code (code contents info
)
1032 "Return a CODE object from Org to ASCII.
1033 CONTENTS is nil. INFO is a plist holding contextual
1035 (format org-e-ascii-verbatim-format
(org-element-property :value code
)))
1040 ;; Comments are ignored.
1045 ;; Comment Blocks are ignored.
1050 (defun org-e-ascii-drawer (drawer contents info
)
1051 "Transcode a DRAWER element from Org to ASCII.
1052 CONTENTS holds the contents of the block. INFO is a plist
1053 holding contextual information."
1054 (let ((name (org-element-property :drawer-name drawer
))
1055 (width (org-e-ascii--current-text-width drawer info
)))
1056 (if (functionp org-e-ascii-format-drawer-function
)
1057 (funcall org-e-ascii-format-drawer-function name contents width
)
1058 ;; If there's no user defined function: simply
1059 ;; display contents of the drawer.
1065 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1066 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1067 CONTENTS holds the contents of the block. INFO is a plist
1068 holding contextual information. See `org-export-data'."
1074 (defun org-e-ascii-entity (entity contents info
)
1075 "Transcode an ENTITY object from Org to ASCII.
1076 CONTENTS are the definition itself. INFO is a plist holding
1077 contextual information."
1078 (org-element-property
1079 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1085 (defun org-e-ascii-example-block (example-block contents info
)
1086 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1087 CONTENTS is nil. INFO is a plist holding contextual information."
1088 (org-e-ascii--box-string
1089 (org-export-format-code-default example-block info
) info
))
1094 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1095 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1096 CONTENTS is nil. INFO is a plist holding contextual information."
1097 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1098 (org-element-property :value export-snippet
)))
1103 (defun org-e-ascii-export-block (export-block contents info
)
1104 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1105 CONTENTS is nil. INFO is a plist holding contextual information."
1106 (when (string= (org-element-property :type export-block
) "ascii")
1107 (org-remove-indentation (org-element-property :value export-block
))))
1112 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1113 "Transcode a FIXED-WIDTH element from Org to ASCII.
1114 CONTENTS is nil. INFO is a plist holding contextual information."
1115 (org-e-ascii--box-string
1116 (replace-regexp-in-string
1117 "^[ \t]*: ?" "" (org-element-property :value fixed-width
)) info
))
1120 ;;;; Footnote Definition
1122 ;; Footnote Definitions are ignored. They are compiled at the end of
1123 ;; the document, by `org-e-ascii-template'.
1126 ;;;; Footnote Reference
1128 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1129 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1130 CONTENTS is nil. INFO is a plist holding contextual information."
1131 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1136 (defun org-e-ascii-headline (headline contents info
)
1137 "Transcode an HEADLINE element from Org to ASCII.
1138 CONTENTS holds the contents of the headline. INFO is a plist
1139 holding contextual information."
1140 ;; Don't export footnote section, which will be handled at the end
1142 (unless (org-element-property :footnote-section-p headline
)
1143 (let* ((low-level-rank (org-export-low-level-p headline info
))
1144 (width (org-e-ascii--current-text-width headline info
))
1145 ;; Blank lines between headline and its contents.
1146 ;; `org-e-ascii-headline-spacing', when set, overwrites
1147 ;; original buffer's spacing.
1150 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1151 (org-element-property :pre-blank headline
)) ?
\n))
1152 ;; Even if HEADLINE has no section, there might be some
1153 ;; links in its title that we shouldn't forget to describe.
1155 (unless (eq (caar (org-element-contents headline
)) 'section
)
1156 (org-e-ascii--describe-links
1157 (org-e-ascii--unique-links headline info
) width info
))))
1158 ;; Deep subtree: export it as a list item.
1162 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1163 org-e-ascii-bullets
))))
1165 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1168 (org-e-ascii--build-title headline info width
) "\n"
1169 ;; Contents, indented by length of bullet.
1171 (org-e-ascii--indent-string
1173 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1175 ;; Else: Standard headline.
1177 (org-e-ascii--build-title headline info width
'underline
)
1179 (concat (when (org-string-nw-p links
) links
) contents
))))))
1182 ;;;; Horizontal Rule
1184 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1185 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1186 CONTENTS is nil. INFO is a plist holding contextual
1194 (org-element-property :attr_ascii horizontal-rule
)
1196 (make-string (or (and (wholenump (plist-get attr
:width
))
1197 (plist-get attr
:width
))
1198 (org-e-ascii--current-text-width horizontal-rule info
))
1199 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1202 ;;;; Inline Babel Call
1204 ;; Inline Babel Calls are ignored.
1207 ;;;; Inline Src Block
1209 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1210 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1211 CONTENTS holds the contents of the item. INFO is a plist holding
1212 contextual information."
1213 (format org-e-ascii-verbatim-format
1214 (org-element-property :value inline-src-block
)))
1219 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1220 "Transcode an INLINETASK element from Org to ASCII.
1221 CONTENTS holds the contents of the block. INFO is a plist
1222 holding contextual information."
1223 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1224 (title (org-export-data (org-element-property :title inlinetask
) info
))
1225 (todo (and (plist-get info
:with-todo-keywords
)
1226 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1227 (and todo
(org-export-data todo info
)))))
1228 (todo-type (org-element-property :todo-type inlinetask
))
1229 (tags (and (plist-get info
:with-tags
)
1230 (org-element-property :tags inlinetask
)))
1231 (priority (and (plist-get info
:with-priority
)
1232 (org-element-property :priority inlinetask
))))
1233 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1234 ;; with appropriate arguments.
1235 (if (functionp org-e-ascii-format-inlinetask-function
)
1236 (funcall org-e-ascii-format-inlinetask-function
1237 todo todo-type priority title tags contents width
)
1238 ;; Otherwise, use a default template.
1239 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1240 (org-e-ascii--indent-string
1242 ;; Top line, with an additional blank line if not in UTF-8.
1243 (make-string width
(if utf8p ?━ ?_
)) "\n"
1244 (unless utf8p
(concat (make-string width ?
) "\n"))
1245 ;; Add title. Fill it if wider than inlinetask.
1246 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1247 (if (<= (length title
) width
) title
1248 (org-e-ascii--fill-string title width info
)))
1250 ;; If CONTENTS is not empty, insert it along with
1252 (when (org-string-nw-p contents
)
1253 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1255 (make-string width
(if utf8p ?━ ?_
)))
1256 ;; Flush the inlinetask to the right.
1257 (- org-e-ascii-text-width org-e-ascii-global-margin
1258 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1259 org-e-ascii-inner-margin
)
1260 (org-e-ascii--current-text-width inlinetask info
)))))))
1264 (defun org-e-ascii-italic (italic contents info
)
1265 "Transcode italic from Org to ASCII.
1266 CONTENTS is the text with italic markup. INFO is a plist holding
1267 contextual information."
1268 (format "/%s/" contents
))
1273 (defun org-e-ascii-item (item contents info
)
1274 "Transcode an ITEM element from Org to ASCII.
1275 CONTENTS holds the contents of the item. INFO is a plist holding
1276 contextual information."
1278 ;; First parent of ITEM is always the plain-list. Get
1279 ;; `:type' property from it.
1280 (org-list-bullet-string
1281 (case (org-element-property :type
(org-export-get-parent item info
))
1283 (concat (org-export-data (org-element-property :tag item
) info
)
1286 ;; Return correct number for ITEM, paying attention to
1288 (let* ((struct (org-element-property :structure item
))
1289 (bul (org-element-property :bullet item
))
1292 (car (last (org-list-get-item-number
1293 (org-element-property :begin item
)
1295 (org-list-prevs-alist struct
)
1296 (org-list-parents-alist struct
)))))))
1297 (replace-regexp-in-string "[0-9]+" num bul
)))
1298 (t (let ((bul (org-element-property :bullet item
)))
1299 ;; Change bullets into more visible form if UTF-8 is active.
1300 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bul
1301 (replace-regexp-in-string
1303 (replace-regexp-in-string
1305 (replace-regexp-in-string "*" "‣" bul
))))))))))
1308 ;; Contents: Pay attention to indentation. Note: check-boxes are
1309 ;; already taken care of at the paragraph level so they don't
1310 ;; interfere with indentation.
1311 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1312 (if (eq (caar (org-element-contents item
)) 'paragraph
)
1314 (concat "\n" contents
))))))
1319 (defun org-e-ascii-keyword (keyword contents info
)
1320 "Transcode a KEYWORD element from Org to ASCII.
1321 CONTENTS is nil. INFO is a plist holding contextual
1323 (let ((key (org-element-property :key keyword
))
1324 (value (org-element-property :value keyword
)))
1326 ((string= key
"ASCII") value
)
1327 ((string= key
"TOC")
1328 (let ((value (downcase value
)))
1330 ((string-match "\\<headlines\\>" value
)
1331 (let ((depth (or (and (string-match "[0-9]+" value
)
1332 (string-to-number (match-string 0 value
)))
1333 (plist-get info
:with-toc
))))
1334 (org-e-ascii--build-toc
1335 info
(and (wholenump depth
) depth
) keyword
)))
1336 ((string= "tables" value
)
1337 (org-e-ascii--list-tables keyword info
))
1338 ((string= "listings" value
)
1339 (org-e-ascii--list-listings keyword info
))))))))
1342 ;;;; Latex Environment
1344 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1345 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1346 CONTENTS is nil. INFO is a plist holding contextual
1348 (org-remove-indentation (org-element-property :value latex-environment
)))
1353 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1354 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1355 CONTENTS is nil. INFO is a plist holding contextual
1357 (org-element-property :value latex-fragment
))
1362 (defun org-e-ascii-line-break (line-break contents info
)
1363 "Transcode a LINE-BREAK object from Org to ASCII.
1364 CONTENTS is nil. INFO is a plist holding contextual
1365 information." hard-newline
)
1370 (defun org-e-ascii-link (link desc info
)
1371 "Transcode a LINK object from Org to ASCII.
1373 DESC is the description part of the link, or the empty string.
1374 INFO is a plist holding contextual information."
1375 (let ((raw-link (org-element-property :raw-link link
))
1376 (type (org-element-property :type link
)))
1378 ((string= type
"coderef")
1379 (let ((ref (org-element-property :path link
)))
1380 (format (org-export-get-coderef-format ref desc
)
1381 (org-export-resolve-coderef ref info
))))
1382 ;; Do not apply a special syntax on radio links. Though, parse
1383 ;; and transcode path to have a proper display of contents.
1384 ((string= type
"radio")
1386 (org-element-parse-secondary-string
1387 (org-element-property :path link
)
1388 (cdr (assq 'radio-target org-element-object-restrictions
)))
1390 ;; Do not apply a special syntax on fuzzy links pointing to
1392 ((string= type
"fuzzy")
1393 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1394 ;; Ignore invisible "#+target: path".
1395 (unless (eq (org-element-type destination
) 'keyword
)
1396 (if (org-string-nw-p desc
) desc
1398 (let ((number (org-export-get-ordinal destination info
)))
1400 (if (atom number
) (number-to-string number
)
1401 (mapconcat 'number-to-string number
".")))))))))
1403 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1405 (format "[%s]" desc
)
1406 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1411 (defun org-e-ascii-macro (macro contents info
)
1412 "Transcode a MACRO element from Org to ASCII.
1413 CONTENTS is nil. INFO is a plist holding contextual
1415 (org-export-expand-macro macro info
))
1420 (defun org-e-ascii-paragraph (paragraph contents info
)
1421 "Transcode a PARAGRAPH element from Org to ASCII.
1422 CONTENTS is the contents of the paragraph, as a string. INFO is
1423 the plist used as a communication channel."
1424 (org-e-ascii--fill-string
1425 (let ((parent (org-export-get-parent paragraph info
)))
1426 ;; If PARAGRAPH is the first one in a list element, be sure to
1427 ;; add the check-box in front of it, before any filling. Later,
1428 ;; it would interfere with line width.
1429 (if (and (eq (org-element-type parent
) 'item
)
1430 (equal (car (org-element-contents parent
)) paragraph
))
1431 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1432 (concat (case (org-element-property :checkbox parent
)
1433 (on (if utf8p
"☑ " "[X] "))
1434 (off (if utf8p
"☐ " "[ ] "))
1435 (trans (if utf8p
"☒ " "[-] ")))
1438 (org-e-ascii--current-text-width paragraph info
) info
))
1443 (defun org-e-ascii-plain-list (plain-list contents info
)
1444 "Transcode a PLAIN-LIST element from Org to ASCII.
1445 CONTENTS is the contents of the list. INFO is a plist holding
1446 contextual information."
1452 (defun org-e-ascii-plain-text (text info
)
1453 "Transcode a TEXT string from Org to ASCII.
1454 INFO is a plist used as a communication channel."
1455 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1456 (plist-get info
:with-special-strings
)))
1458 ;; Usual replacements in utf-8 with proper option set.
1459 (replace-regexp-in-string
1461 (replace-regexp-in-string
1463 (replace-regexp-in-string "---" "—" text
)))))
1468 (defun org-e-ascii-planning (planning contents info
)
1469 "Transcode a PLANNING element from Org to ASCII.
1470 CONTENTS is nil. INFO is a plist used as a communication
1475 (list (let ((closed (org-element-property :closed planning
)))
1476 (when closed
(concat org-closed-string
" "
1477 (org-translate-time closed
))))
1478 (let ((deadline (org-element-property :deadline planning
)))
1479 (when deadline
(concat org-deadline-string
" "
1480 (org-translate-time deadline
))))
1481 (let ((scheduled (org-element-property :scheduled planning
)))
1482 (when scheduled
(concat org-scheduled-string
" "
1483 (org-translate-time scheduled
))))))
1487 ;;;; Property Drawer
1489 ;; Property drawers are ignored.
1494 (defun org-e-ascii-quote-block (quote-block contents info
)
1495 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1496 CONTENTS holds the contents of the block. INFO is a plist
1497 holding contextual information."
1498 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1499 (org-e-ascii--indent-string
1500 (org-remove-indentation
1501 (org-e-ascii--fill-string contents width info
))
1502 org-e-ascii-quote-margin
)))
1507 (defun org-e-ascii-quote-section (quote-section contents info
)
1508 "Transcode a QUOTE-SECTION element from Org to ASCII.
1509 CONTENTS is nil. INFO is a plist holding contextual information."
1510 (let ((width (org-e-ascii--current-text-width quote-section info
))
1513 (org-remove-indentation (org-element-property :value quote-section
))
1515 (org-e-ascii--indent-string
1517 (+ org-e-ascii-quote-margin
1518 ;; Don't apply inner margin if parent headline is low level.
1519 (let ((headline (org-export-get-parent-headline quote-section info
)))
1520 (if (org-export-low-level-p headline info
) 0
1521 org-e-ascii-inner-margin
))))))
1526 (defun org-e-ascii-radio-target (radio-target contents info
)
1527 "Transcode a RADIO-TARGET object from Org to ASCII.
1528 CONTENTS is the contents of the target. INFO is a plist holding
1529 contextual information."
1534 (defun org-e-ascii-section (section contents info
)
1535 "Transcode a SECTION element from Org to ASCII.
1536 CONTENTS is the contents of the section. INFO is a plist holding
1537 contextual information."
1538 (org-e-ascii--indent-string
1541 (when org-e-ascii-links-to-notes
1542 ;; Add list of links at the end of SECTION.
1543 (let ((links (org-e-ascii--describe-links
1544 (org-e-ascii--unique-links section info
)
1545 (org-e-ascii--current-text-width section info
) info
)))
1546 ;; Separate list of links and section contents.
1547 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1548 ;; Do not apply inner margin if parent headline is low level.
1549 (let ((headline (org-export-get-parent-headline section info
)))
1550 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1551 org-e-ascii-inner-margin
))))
1556 (defun org-e-ascii-special-block (special-block contents info
)
1557 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1558 CONTENTS holds the contents of the block. INFO is a plist
1559 holding contextual information."
1565 (defun org-e-ascii-src-block (src-block contents info
)
1566 "Transcode a SRC-BLOCK element from Org to ASCII.
1567 CONTENTS holds the contents of the item. INFO is a plist holding
1568 contextual information."
1569 (let ((caption (org-e-ascii--build-caption src-block info
)))
1571 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1572 (org-e-ascii--box-string
1573 (org-export-format-code-default src-block info
) info
)
1574 (when (and caption
(not org-e-ascii-caption-above
))
1575 (concat "\n" caption
)))))
1577 ;;;; Statistics Cookie
1579 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1580 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1581 CONTENTS is nil. INFO is a plist holding contextual information."
1582 (org-element-property :value statistics-cookie
))
1587 (defun org-e-ascii-subscript (subscript contents info
)
1588 "Transcode a SUBSCRIPT object from Org to ASCII.
1589 CONTENTS is the contents of the object. INFO is a plist holding
1590 contextual information."
1591 (if (org-element-property :use-brackets-p subscript
)
1592 (format "_{%s}" contents
)
1593 (format "_%s" contents
)))
1598 (defun org-e-ascii-superscript (superscript contents info
)
1599 "Transcode a SUPERSCRIPT object from Org to ASCII.
1600 CONTENTS is the contents of the object. INFO is a plist holding
1601 contextual information."
1602 (if (org-element-property :use-brackets-p superscript
)
1603 (format "_{%s}" contents
)
1604 (format "_%s" contents
)))
1609 (defun org-e-ascii-strike-through (strike-through contents info
)
1610 "Transcode STRIKE-THROUGH from Org to ASCII.
1611 CONTENTS is text with strike-through markup. INFO is a plist
1612 holding contextual information."
1613 (format "+%s+" contents
))
1618 (defun org-e-ascii-table (table contents info
)
1619 "Transcode a TABLE element from Org to ASCII.
1620 CONTENTS is nil. INFO is a plist holding contextual information."
1621 (let ((caption (org-e-ascii--build-caption table info
)))
1623 ;; Possibly add a caption string above.
1624 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1625 ;; Insert table. Note: "table.el" tables are left unmodified.
1626 (if (eq (org-element-property :type table
) 'org
) contents
1627 (org-remove-indentation (org-element-property :value table
)))
1628 ;; Possible add a caption string below.
1629 (when (and caption
(not org-e-ascii-caption-above
))
1630 (concat "\n" caption
)))))
1635 (defun org-e-ascii--table-cell-width (table-cell info
)
1636 "Return width of TABLE-CELL.
1638 Width of a cell is determined either by a width cookie in the
1639 same column as the cell, or by the length of its contents.
1641 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1643 (or (and (not org-e-ascii-table-widen-columns
)
1644 (org-export-table-cell-width table-cell info
))
1645 (let* ((max-width 0)
1646 (table (org-export-get-parent-table table-cell info
))
1647 (specialp (org-export-table-has-special-column-p table
))
1648 (col (cdr (org-export-table-cell-address table-cell info
))))
1655 (lambda (obj) (org-export-data obj info
))
1656 (org-element-contents
1657 (elt (if specialp
(car (org-element-contents row
))
1658 (org-element-contents row
))
1664 (defun org-e-ascii-table-cell (table-cell contents info
)
1665 "Transcode a TABLE-CELL object from Org to ASCII.
1666 CONTENTS is the cell contents. INFO is a plist used as
1667 a communication channel."
1668 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1669 ;; is nil and some width cookie has set it, use that value.
1670 ;; Otherwise, compute the maximum width among transcoded data of
1671 ;; each cell in the column.
1672 (let ((width (org-e-ascii--table-cell-width table-cell info
)))
1673 ;; When contents are too large, truncate them.
1674 (unless (or org-e-ascii-table-widen-columns
(<= (length contents
) width
))
1675 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1676 ;; Align contents correctly within the cell.
1677 (let* ((indent-tabs-mode nil
)
1680 (org-e-ascii--justify-string
1682 (org-export-table-cell-alignment table-cell info
)))))
1683 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1685 (concat (format " %s " contents
)
1686 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1687 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1692 (defun org-e-ascii-table-row (table-row contents info
)
1693 "Transcode a TABLE-ROW element from Org to ASCII.
1694 CONTENTS is the row contents. INFO is a plist used as
1695 a communication channel."
1696 (when (eq (org-element-property :type table-row
) 'standard
)
1699 (lambda (lcorner horiz vert rcorner
)
1704 table-row
'table-cell
1706 (let ((width (org-e-ascii--table-cell-width cell info
))
1707 (borders (org-export-table-cell-borders cell info
)))
1709 (when (and (memq 'left borders
)
1710 (equal (org-element-map
1711 table-row
'table-cell
'identity info t
)
1713 (make-string (+ 2 width
) (string-to-char horiz
))
1715 ((not (memq 'right borders
)) nil
)
1716 ((equal (car (last (org-element-contents table-row
)))
1721 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1722 (borders (org-export-table-cell-borders
1723 (org-element-map table-row
'table-cell
'identity info t
)
1726 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1727 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1728 (funcall build-hline
"+" "-" "+" "+")))
1729 ((memq 'above borders
)
1730 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1731 (funcall build-hline
"+" "-" "+" "+"))))
1732 (when (memq 'left borders
) (if utf8p
"│" "|"))
1734 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1735 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1736 (funcall build-hline
"+" "-" "+" "+")))))))
1741 ;; Targets are invisible.
1746 (defun org-e-ascii-timestamp (timestamp contents info
)
1747 "Transcode a TIMESTAMP object from Org to ASCII.
1748 CONTENTS is nil. INFO is a plist holding contextual information."
1749 (org-translate-time (org-element-property :value timestamp
)))
1754 (defun org-e-ascii-underline (underline contents info
)
1755 "Transcode UNDERLINE from Org to ASCII.
1756 CONTENTS is the text with underline markup. INFO is a plist
1757 holding contextual information."
1758 (format "_%s_" contents
))
1763 (defun org-e-ascii-verbatim (verbatim contents info
)
1764 "Return a VERBATIM object from Org to ASCII.
1765 CONTENTS is nil. INFO is a plist holding contextual information."
1766 (format org-e-ascii-verbatim-format
1767 (org-element-property :value verbatim
)))
1772 (defun org-e-ascii-verse-block (verse-block contents info
)
1773 "Transcode a VERSE-BLOCK element from Org to ASCII.
1774 CONTENTS is verse block contents. INFO is a plist holding
1775 contextual information."
1776 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1777 (org-e-ascii--indent-string
1778 (org-e-ascii--justify-string contents verse-width
'left
)
1779 org-e-ascii-quote-margin
)))
1784 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1785 "Filter controlling number of blank lines after an headline.
1787 HEADLINE is a string representing a transcoded headline.
1788 BACK-END is symbol specifying back-end used for export. INFO is
1789 plist containing the communication channel.
1791 This function only applies to `e-ascii' back-end. See
1792 `org-e-ascii-headline-spacing' for information.
1794 For any other back-end, HEADLINE is returned as-is."
1795 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1796 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1797 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1801 ;;; Interactive function
1803 (defun org-e-ascii-export-to-ascii
1804 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1805 "Export current buffer to a text file.
1807 If narrowing is active in the current buffer, only export its
1810 If a region is active, export that region.
1812 When optional argument SUBTREEP is non-nil, export the sub-tree
1813 at point, extracting information from the headline properties
1816 When optional argument VISIBLE-ONLY is non-nil, don't export
1817 contents of hidden elements.
1819 When optional argument BODY-ONLY is non-nil, strip title, table
1820 of contents and footnote definitions from output.
1822 EXT-PLIST, when provided, is a property list with external
1823 parameters overriding Org default settings, but still inferior to
1824 file-local settings.
1826 When optional argument PUB-DIR is set, use it as the publishing
1829 Return output file's name."
1831 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1833 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1836 (provide 'org-e-ascii
)
1837 ;;; org-e-ascii.el ends here