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
))
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-resolve-ref-link
"org-export" (link info
))
70 (declare-function org-export-secondary-string
71 "org-export" (secondary backend info
))
72 (declare-function org-export-table-format-info
"org-export" (table))
74 org-export-to-file
"org-export"
75 (backend file
&optional subtreep visible-only body-only ext-plist
))
79 ;;; Internal Variables
81 ;; The following setting won't allow to modify preferred charset
82 ;; through a buffer keyword or an option item, but, since the property
83 ;; will appear in communication channel nonetheless, it allows to
84 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
87 ;; We also install a filter for headlines and sections, in order to
88 ;; control blank lines separating them in output string.
90 (defconst org-e-ascii-option-alist
91 '((:ascii-charset nil nil org-e-ascii-charset
)
93 "Alist between ASCII export properties and ways to set them.
94 See `org-export-option-alist' for more information on the
95 structure or the values.")
97 (defconst org-e-ascii-filters-alist
98 '((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
99 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
100 "Alist between filters keywords and back-end specific filters.
101 See `org-export-filters-alist' for more information.")
103 (defconst org-e-ascii-dictionary
107 :latin1
"Footnotes\n"
108 :utf-8
"Footnotes\n")
110 :ascii
"Notes de bas de page\n"
111 :latin1
"Notes de bas de page\n"
112 :utf-8
"Notes de bas de page\n"))
115 :ascii
"Listing %d: %s"
116 :latin1
"Listing %d: %s"
117 :utf-8
"Listing %d: %s")
119 :ascii
"Programme %d : %s"
120 :latin1
"Programme %d : %s"
121 :utf-8
"Programme nº %d : %s"))
122 ("List Of Listings\n"
124 :ascii
"List Of Listings\n"
125 :latin1
"List Of Listings\n"
126 :utf-8
"List Of Listings\n")
128 :ascii
"Liste des programmes\n"
129 :latin1
"Liste des programmes\n"
130 :utf-8
"Liste des programmes\n"))
133 :ascii
"List Of Tables\n"
134 :latin1
"List Of Tables\n"
135 :utf-8
"List Of Tables\n")
137 :ascii
"Liste des tableaux\n"
138 :latin1
"Liste des tableaux\n"
139 :utf-8
"Liste des tableaux\n"))
142 :ascii
"Listing %d: "
143 :latin1
"Listing %d: "
144 :utf-8
"Listing %d: ")
146 :ascii
"Programme %d : "
147 :latin1
"Programme %d : "
148 :utf-8
"Programme nº %d : "))
149 ("Table Of Contents\n"
151 :ascii
"Table Of Contents\n"
152 :latin1
"Table Of Contents\n"
153 :utf-8
"Table Of Contents\n")
156 :latin1
"Table des matières\n"
157 :utf-8
"Table des matières\n"))
160 :ascii
"Table %d: %s"
161 :latin1
"Table %d: %s"
162 :utf-8
"Table %d: %s")
164 :ascii
"Tableau %d : %s"
165 :latin1
"Tableau %d : %s"
166 :utf-8
"Tableau nº %d : %s"))
169 :ascii
"See section %s"
170 :latin1
"See section %s"
171 :utf-8
"See section %s")
173 :ascii
"cf. section %s"
174 :latin1
"cf. section %s"
175 :utf-8
"cf. section %s"))
182 :ascii
"Tableau %d : "
183 :latin1
"Tableau %d : "
184 :utf-8
"Tableau nº %d : "))
187 :ascii
"Unknown reference"
188 :latin1
"Unknown reference"
189 :utf-8
"Unknown reference")
191 :ascii
"Destination inconnue"
192 :latin1
"Référence inconnue"
193 :utf-8
"Référence inconnue")))
194 "Dictionary for ASCII back-end.
196 Alist whose car is the string to translate and cdr is an alist
197 whose car is the language string and cdr is a plist whose
198 properties are possible charsets and value the translated term.
200 It is used as a database for `org-e-ascii--translate'.")
204 ;;; User Configurable Variables
206 (defgroup org-export-e-ascii nil
207 "Options for exporting Org mode files to ASCII."
208 :tag
"Org Export ASCII"
211 (defcustom org-e-ascii-text-width
72
212 "Maximum width of exported text.
213 This number includes margin size, as set in
214 `org-e-ascii-global-margin'."
215 :group
'org-export-e-ascii
218 (defcustom org-e-ascii-global-margin
0
219 "Width of the left margin, in number of characters."
220 :group
'org-export-e-ascii
223 (defcustom org-e-ascii-inner-margin
2
224 "Width of the inner margin, in number of characters.
225 Inner margin is applied between each headline."
226 :group
'org-export-e-ascii
229 (defcustom org-e-ascii-quote-margin
6
230 "Width of margin used for quoting text, in characters.
231 This margin is applied on both sides of the text."
232 :group
'org-export-e-ascii
235 (defcustom org-e-ascii-inlinetask-width
30
236 "Width of inline tasks, in number of characters.
237 This number ignores any margin."
238 :group
'org-export-e-ascii
241 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
242 "Number of blank lines inserted around headlines.
244 This variable can be set to a cons cell. In that case, its car
245 represents the number of blank lines present before headline
246 contents whereas its cdr reflects the number of blank lines after
249 A nil value replicates the number of blank lines found in the
250 original Org buffer at the same place."
251 :group
'org-export-e-ascii
253 (const :tag
"Replicate original spacing" nil
)
254 (cons :tag
"Set an uniform spacing"
255 (integer :tag
"Number of blank lines before contents")
256 (integer :tag
"Number of blank lines after contents"))))
258 (defcustom org-e-ascii-charset
'ascii
259 "The charset allowed to represent various elements and objects.
261 `ascii' Only use plain ASCII characters
262 `latin1' Include Latin-1 characters
263 `utf-8' Use all UTF-8 characters"
264 :group
'org-export-e-ascii
266 (const :tag
"ASCII" ascii
)
267 (const :tag
"Latin-1" latin1
)
268 (const :tag
"UTF-8" utf-8
)))
270 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
272 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
273 "Characters for underlining headings in ASCII export.
275 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
276 and whose value is a list of characters.
278 For each supported charset, this variable associates a sequence
279 of underline characters. In a sequence, the characters will be
280 used in order for headlines level 1, 2, ... If no character is
281 available for a given level, the headline won't be underlined."
282 :group
'org-export-e-ascii
284 (cons :tag
"Underline characters sequence"
285 (const :tag
"ASCII charset" ascii
)
287 (cons :tag
"Underline characters sequence"
288 (const :tag
"Latin-1 charset" latin1
)
290 (cons :tag
"Underline characters sequence"
291 (const :tag
"UTF-8 charset" utf-8
)
292 (repeat character
))))
294 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
297 "Bullet characters for headlines converted to lists in ASCII export.
299 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
300 and whose value is a list of characters.
302 The first character is used for the first level considered as low
303 level, and so on. If there are more levels than characters given
304 here, the list will be repeated.
306 Note that this variable doesn't affect plain lists
308 :group
'org-export-e-ascii
310 (cons :tag
"Bullet characters for low level headlines"
311 (const :tag
"ASCII charset" ascii
)
313 (cons :tag
"Bullet characters for low level headlines"
314 (const :tag
"Latin-1 charset" latin1
)
316 (cons :tag
"Bullet characters for low level headlines"
317 (const :tag
"UTF-8 charset" utf-8
)
318 (repeat character
))))
320 (defcustom org-e-ascii-links-to-notes t
321 "Non-nil means convert links to notes before the next headline.
322 When nil, the link will be exported in place. If the line
323 becomes long in this way, it will be wrapped."
324 :group
'org-export-e-ascii
327 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
328 "Non-nil means keep all vertical lines in ASCII tables.
329 When nil, vertical lines will be removed except for those needed
330 for column grouping."
331 :group
'org-export-e-ascii
334 (defcustom org-e-ascii-table-widen-columns t
335 "Non-nil means widen narrowed columns for export.
336 When nil, narrowed columns will look in ASCII export just like in
337 Org mode, i.e. with \"=>\" as ellipsis."
338 :group
'org-export-e-ascii
341 (defcustom org-e-ascii-caption-above nil
342 "When non-nil, place caption string before the element.
343 Otherwise, place it right after it."
344 :group
'org-export-e-ascii
347 (defcustom org-e-ascii-verbatim-format
"`%s'"
348 "Format string used for verbatim text and inline code."
349 :group
'org-export-e-ascii
352 (defcustom org-e-ascii-format-drawer-function nil
353 "Function called to format a drawer in ASCII.
355 The function must accept two parameters:
356 NAME the drawer name, like \"LOGBOOK\"
357 CONTENTS the contents of the drawer.
358 WIDTH the text width within the drawer.
360 The function should return either the string to be exported or
361 nil to ignore the drawer.
363 For example, the variable could be set to the following function
364 in order to mimic default behaviour:
366 \(defun org-e-ascii-format-drawer-default \(name contents width\)
367 \"Format a drawer element for ASCII export.\"
369 :group
'org-export-e-ascii
372 (defcustom org-e-ascii-format-inlinetask-function nil
373 "Function called to format an inlinetask in ASCII.
375 The function must accept six parameters:
376 TODO the todo keyword, as a string
377 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
378 PRIORITY the inlinetask priority, as a string
379 NAME the inlinetask name, as a string.
380 TAGS the inlinetask tags, as a string.
381 CONTENTS the contents of the inlinetask, as a string.
383 The function should return either the string to be exported or
384 nil to ignore the inline task.
386 For example, the variable could be set to the following function
387 in order to mimic default behaviour:
389 \(defun org-e-ascii-format-inlinetask-default
390 \(todo type priority name tags contents\)
391 \"Format an inline task element for ASCII export.\"
392 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
393 \(width org-e-ascii-inlinetask-width\)
394 \(org-e-ascii--indent-string
396 ;; Top line, with an additional blank line if not in UTF-8.
397 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
398 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
399 ;; Add title. Fill it if wider than inlinetask.
400 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
401 \(if \(<= \(length title\) width\) title
402 \(org-e-ascii--fill-string title width info\)\)\)
404 ;; If CONTENTS is not empty, insert it along with
406 \(when \(org-string-nw-p contents\)
407 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
409 \(make-string width \(if utf8p ?━ ?_\)\)\)
410 ;; Flush the inlinetask to the right.
411 \(- \(plist-get info :ascii-width\)
412 \(plist-get info :ascii-margin\)
413 \(plist-get info :ascii-inner-margin\)
414 \(org-e-ascii--current-text-width inlinetask info\)\)"
415 :group
'org-export-e-ascii
420 ;;; Internal Functions
422 ;; Internal functions fall into three categories.
424 ;; The first one is about text formatting. The core function is
425 ;; `org-e-ascii--current-text-width', which determines the current
426 ;; text width allowed to a given element. In other words, it helps
427 ;; keeping each line width within maximum text width defined in
428 ;; `org-e-ascii-text-width'. Once this information is known,
429 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
430 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
431 ;; operate on a given output string.
433 ;; The second category contains functions handling elements listings,
434 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
435 ;; returns a complete table of contents, `org-e-ascii--list-listings'
436 ;; returns a list of referenceable src-block elements, and
437 ;; `org-e-ascii--list-tables' does the same for table elements.
439 ;; The third category includes general helper functions.
440 ;; `org-e-ascii--build-title' creates the title for a given headline
441 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
442 ;; caption string associated to a table or a src-block.
443 ;; `org-e-ascii--describe-links' creates notes about links for
444 ;; insertion at the end of a section. It uses
445 ;; `org-e-ascii--unique-links' to get the list of links to describe.
446 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
447 ;; to internationalize output.
450 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
451 "Fill a string with specified text-width and return it.
453 S is the string being filled. TEXT-WIDTH is an integer
454 specifying maximum length of a line. INFO is the plist used as
455 a communication channel.
457 Optional argument JUSTIFY can specify any type of justification
458 among `left', `center', `right' or `full'. A nil value is
459 equivalent to `left'. For a justification that doesn't also fill
460 string, see `org-e-ascii--justify-string'.
462 Return nil if S isn't a string."
463 ;; Don't fill paragraph when break should be preserved.
464 (cond ((not (stringp s
)) nil
)
465 ((plist-get info
:preserve-breaks
) s
)
467 (let ((fill-column text-width
)
468 (use-hard-newlines t
))
470 (fill-region (point-min) (point-max) justify
))
473 (defun org-e-ascii--justify-string (s text-width how
)
475 TEXT-WIDTH is an integer specifying maximum length of a line.
476 HOW determines the type of justification: it can be `left',
477 `right', `full' or `center'."
480 (goto-char (point-min))
481 (let ((fill-column text-width
))
482 (while (< (point) (point-max))
483 (justify-current-line how
)
487 (defun org-e-ascii--indent-string (s width
)
488 "Indent string S by WIDTH white spaces.
489 Empty lines are not indented."
491 (replace-regexp-in-string
492 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
494 (defun org-e-ascii--box-string (s info
)
495 "Return string S with a partial box to its left.
496 INFO is a plist used as a communicaton channel."
497 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
498 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
499 (replace-regexp-in-string
500 "^" (if utf8p
"│ " "| ")
501 ;; Remove last newline character.
502 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
504 (defun org-e-ascii--current-text-width (element info
)
505 "Return maximum text width for ELEMENT's contents.
506 INFO is a plist used as a communication channel."
507 (case (org-element-type element
)
508 ;; Elements with an absolute width: `headline' and `inlinetask'.
509 (inlinetask org-e-ascii-inlinetask-width
)
511 (- org-e-ascii-text-width
512 (let ((low-level-rank (org-export-low-level-p element info
)))
513 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
514 ;; Elements with a relative width: store maximum text width in
517 (let* ((genealogy (cons element
(org-export-get-genealogy element info
)))
518 ;; Total width is determined by the presence, or not, of an
519 ;; inline task among ELEMENT parents.
521 (if (loop for parent in genealogy
522 thereis
(eq (org-element-type parent
) 'inlinetask
))
523 org-e-ascii-inlinetask-width
524 ;; No inlinetask: Remove global margin from text width.
525 (- org-e-ascii-text-width
526 org-e-ascii-global-margin
527 (let ((parent (org-export-get-parent-headline element info
)))
528 ;; Inner margin doesn't apply to text before first
531 (let ((low-level-rank
532 (org-export-low-level-p parent info
)))
533 ;; Inner margin doesn't apply to contents of
534 ;; low level headlines, since they've got their
535 ;; own indentation mechanism.
536 (if low-level-rank
(* low-level-rank
2)
537 org-e-ascii-inner-margin
))))))))
539 ;; Each `quote-block', `quote-section' and `verse-block' above
540 ;; narrows text width by twice the standard margin size.
541 (+ (* (loop for parent in genealogy
542 when
(memq (org-element-type parent
)
543 '(quote-block quote-section verse-block
))
545 2 org-e-ascii-quote-margin
)
546 ;; Text width within a plain-list is restricted by
547 ;; indentation of current item. If that's the case,
548 ;; compute it with the help of `:structure' property from
549 ;; parent item, if any.
551 (if (eq (org-element-type element
) 'item
) element
552 (loop for parent in genealogy
553 when
(eq (org-element-type parent
) 'item
)
555 (if (not parent-item
) 0
556 ;; Compute indentation offset of the current item,
557 ;; that is the sum of the difference between its
558 ;; indentation and the indentation of the top item in
559 ;; the list and current item bullet's length. Also
560 ;; remove tag length (for description lists) or bullet
562 (let ((struct (org-element-property :structure parent-item
))
563 (beg-item (org-element-property :begin parent-item
)))
564 (+ (- (org-list-get-ind beg-item struct
)
566 (org-list-get-top-point struct
) struct
))
568 (or (org-list-get-tag beg-item struct
)
569 (org-list-get-bullet beg-item struct
)))))))))))))
571 (defun org-e-ascii--build-title
572 (element info text-width
&optional underline notags
)
573 "Format ELEMENT title and return it.
575 ELEMENT is either an `headline' or `inlinetask' element. INFO is
576 a plist used as a communication channel. TEXT-WIDTH is an
577 integer representing the maximum length of a line.
579 When optional argument UNDERLINE is non-nil, underline title,
580 without the tags, according to `org-e-ascii-underline'
583 if optional argument NOTAGS is nil, no tags will be added to the
585 (let* ((headlinep (eq (org-element-type element
) 'headline
))
587 ;; Numbering is specific to headlines.
588 (and headlinep
(org-export-numbered-headline-p element info
)
589 ;; All tests passed: build numbering string.
593 (org-export-get-headline-number element info
) ".")
595 (text (org-export-secondary-string
596 (org-element-property :title element
) 'e-ascii info
))
598 (and (plist-get info
:with-todo-keywords
)
599 (let ((todo (org-element-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-property :tags element
)))
607 (and (plist-get info
:with-priority
)
608 (concat (org-element-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-property :caption element
))
639 (name (org-element-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-property :caption el
)
647 (org-element-property :name el
)))))
648 (title-fmt (org-e-ascii--translate
649 (case (org-element-type element
)
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-property :caption src-block
)))
720 (if (not caption
) (org-element-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-property :caption table
)))
759 (if (not caption
) (org-element-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-property :raw-link link
)
783 (org-element-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-property :title element
) acc
)
793 (when (eq (org-element-type 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 (org-element-type element
) 'section
) element
798 (let ((sec (car (org-element-contents element
))))
799 (and (eq (org-element-type sec
) 'section
) sec
))))
800 (headline (if (eq (org-element-type 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) (funcall unique-link-p link
)) info
))))
809 (defun org-e-ascii--describe-links (links width info
)
810 "Return a string describing a list of links.
812 LINKS is a list of link type objects, as returned by
813 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
814 the output string. INFO is a plist used as a communication
818 (let ((type (org-element-property :type link
))
819 (anchor (let ((desc (org-element-contents link
)))
820 (if (not desc
) (org-element-property :raw-link link
)
821 (org-export-secondary-string desc
'e-ascii info
)))))
823 ;; Coderefs, radio links and fuzzy links are ignored.
824 ((member type
'("coderef" "radio" "fuzzy")) nil
)
825 ;; Id and custom-id links: Headlines refer to their numbering.
826 ((member type
'("custom-id" "id"))
827 (let ((dest (org-export-resolve-id-link link info
)))
829 (org-e-ascii--fill-string
833 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
835 (org-e-ascii--translate "See section %s" info
)
836 (mapconcat 'number-to-string
837 (org-export-get-headline-number dest info
) "."))))
838 width info
) "\n\n")))
839 ;; Do not add a link that cannot be resolved and doesn't have
840 ;; any description: destination is already visible in the
842 ((not (org-element-contents link
)) nil
)
845 (org-e-ascii--fill-string
846 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
855 (defun org-e-ascii-template--document-title (info)
856 "Return document title, as a string.
857 INFO is a plist used as a communication channel."
858 (let ((text-width org-e-ascii-text-width
)
859 (title (org-export-secondary-string
860 (plist-get info
:title
) 'e-ascii info
))
862 (and (plist-get info
:with-author
)
863 (let ((auth (plist-get info
:author
)))
864 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
866 (and (plist-get info
:with-email
)
867 (org-export-secondary-string
868 (plist-get info
:email
) 'e-ascii info
)))
869 (date (plist-get info
:date
)))
870 ;; There are two types of title blocks depending on the presence
871 ;; of a title to display.
872 (if (string= title
"")
873 ;; Title block without a title. DATE is positioned at the top
874 ;; right of the document, AUTHOR to the top left and EMAIL
877 ((and (org-string-nw-p date
) (org-string-nw-p author
))
880 (make-string (- text-width
(length date
) (length author
)) ?
)
882 (when (org-string-nw-p email
) (concat "\n" email
))
884 ((and (org-string-nw-p date
) (org-string-nw-p email
))
887 (make-string (- text-width
(length date
) (length email
)) ?
)
889 ((org-string-nw-p date
)
891 (org-e-ascii--justify-string date text-width
'right
)
893 ((and (org-string-nw-p author
) (org-string-nw-p email
))
894 (concat author
"\n" email
"\n\n\n"))
895 ((org-string-nw-p author
) (concat author
"\n\n\n"))
896 ((org-string-nw-p email
) (concat email
"\n\n\n")))
897 ;; Title block with a title. Document's TITLE, along with the
898 ;; AUTHOR and its EMAIL are both overlined and an underlined,
899 ;; centered. Date is just below, also centered.
900 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
901 ;; Format TITLE. It may be filled if it is too wide,
902 ;; that is wider than the two thirds of the total width.
903 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
904 (formatted-title (org-e-ascii--fill-string title title-len info
))
907 (min (+ (max title-len
(length author
) (length email
)) 2)
908 text-width
) (if utf8p ?━ ?_
))))
909 (org-e-ascii--justify-string
912 (upcase formatted-title
)
914 ((and (org-string-nw-p author
) (org-string-nw-p email
))
915 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
916 ((org-string-nw-p author
)
917 (concat (if utf8p
"\n\n\n" "\n\n") author
))
918 ((org-string-nw-p email
)
919 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
921 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
922 "\n\n\n") text-width
'center
)))))
924 (defun org-e-ascii-template (contents info
)
925 "Return complete document string after ASCII conversion.
926 CONTENTS is the transcoded contents string. INFO is a plist
927 holding export options."
928 (org-element-normalize-string
929 (org-e-ascii--indent-string
930 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
931 ;; 1. Build title block.
933 (org-e-ascii-template--document-title info
)
934 ;; 2. Table of contents.
935 (let ((depth (plist-get info
:with-toc
)))
938 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
940 ;; 3. Document's body.
942 ;; 4. Footnote definitions.
943 (let ((definitions (org-export-collect-footnote-definitions
944 (plist-get info
:parse-tree
) info
))
945 ;; Insert full links right inside the footnote definition
946 ;; as they have no chance to be inserted later.
947 (org-e-ascii-links-to-notes nil
))
951 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
956 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
960 (let ((id (format "[%s] " (car ref
))))
961 ;; Distinguish between inline definitions and
962 ;; full-fledged definitions.
964 (let ((def (nth 2 ref
)))
965 (if (eq (org-element-type def
) 'org-data
)
966 ;; Full-fledged definition: footnote ID is
967 ;; inserted inside the first parsed paragraph
968 ;; (FIRST), if any, to be sure filling will
969 ;; take it into consideration.
970 (let ((first (car (org-element-contents def
))))
971 (if (not (eq (org-element-type first
) 'paragraph
))
972 (concat id
"\n" (org-export-data def
'e-ascii info
))
973 (push id
(nthcdr 2 first
))
974 (org-export-data def
'e-ascii info
)))
975 ;; Fill paragraph once footnote ID is inserted in
976 ;; order to have a correct length for first line.
977 (org-e-ascii--fill-string
978 (concat id
(org-export-secondary-string def
'e-ascii info
))
979 text-width info
))))))
980 definitions
"\n\n"))))
981 ;; 5. Creator. Ignore `comment' value as there are no comments in
982 ;; ASCII. Justify it to the bottom right.
983 (let ((creator-info (plist-get info
:with-creator
)))
984 (unless (or (not creator-info
) (eq creator-info
'comment
))
987 (org-e-ascii--fill-string
988 (plist-get info
:creator
) text-width info
'right
))))))
989 org-e-ascii-global-margin
)))
991 (defun org-e-ascii--translate (s info
)
994 INFO is a plist used as a communication channel.
996 Translation depends on `:language' property and allowed charset.
997 If no translation in found for a given language and a given
998 charset, fall-back to S."
999 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
1000 (lang (plist-get info
:language
))
1001 (translations (cdr (assoc s org-e-ascii-dictionary
))))
1002 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
1006 ;;; Transcode Functions
1010 ;; Babel Calls are ignored.
1015 (defun org-e-ascii-center-block (center-block contents info
)
1016 "Transcode a CENTER-BLOCK element from Org to ASCII.
1017 CONTENTS holds the contents of the block. INFO is a plist
1018 holding contextual information."
1019 (org-e-ascii--justify-string
1020 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1025 ;; Comments are ignored.
1030 ;; Comment Blocks are ignored.
1035 (defun org-e-ascii-drawer (drawer contents info
)
1036 "Transcode a DRAWER element from Org to ASCII.
1037 CONTENTS holds the contents of the block. INFO is a plist
1038 holding contextual information."
1039 (let ((name (org-element-property :drawer-name drawer
))
1040 (width (org-e-ascii--current-text-width drawer info
)))
1041 (if (functionp org-e-ascii-format-drawer-function
)
1042 (funcall org-e-ascii-format-drawer-function name contents width
)
1043 ;; If there's no user defined function: simply
1044 ;; display contents of the drawer.
1050 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1051 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1052 CONTENTS holds the contents of the block. INFO is a plist
1053 holding contextual information. See
1060 (defun org-e-ascii-emphasis (emphasis contents info
)
1061 "Transcode EMPHASIS from Org to ASCII.
1062 CONTENTS is the contents of the emphasized text. INFO is a plist
1063 holding contextual information.."
1064 (let ((marker (org-element-property :marker emphasis
)))
1065 ;; Leave emphasis markers as-is.
1066 (concat marker contents marker
)))
1071 (defun org-e-ascii-entity (entity contents info
)
1072 "Transcode an ENTITY object from Org to ASCII.
1073 CONTENTS are the definition itself. INFO is a plist holding
1074 contextual information."
1075 (org-element-property
1076 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1082 (defun org-e-ascii-example-block (example-block contents info
)
1083 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1084 CONTENTS is nil. INFO is a plist holding contextual information."
1085 (org-e-ascii--box-string (org-export-handle-code example-block info
) info
))
1090 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1091 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1092 CONTENTS is nil. INFO is a plist holding contextual information."
1093 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1094 (org-element-property :value export-snippet
)))
1099 (defun org-e-ascii-export-block (export-block contents info
)
1100 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1101 CONTENTS is nil. INFO is a plist holding contextual information."
1102 (when (string= (org-element-property :type export-block
) "ascii")
1103 (org-remove-indentation (org-element-property :value export-block
))))
1108 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1109 "Transcode a FIXED-WIDTH element from Org to ASCII.
1110 CONTENTS is nil. INFO is a plist holding contextual information."
1111 (org-e-ascii--box-string
1112 (replace-regexp-in-string
1113 "^[ \t]*: ?" "" (org-element-property :value fixed-width
)) info
))
1116 ;;;; Footnote Definition
1118 ;; Footnote Definitions are ignored. They are compiled at the end of
1119 ;; the document, by `org-e-ascii-template'.
1122 ;;;; Footnote Reference
1124 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1125 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1126 CONTENTS is nil. INFO is a plist holding contextual information."
1127 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1132 (defun org-e-ascii-headline (headline contents info
)
1133 "Transcode an HEADLINE element from Org to ASCII.
1134 CONTENTS holds the contents of the headline. INFO is a plist
1135 holding contextual information."
1136 ;; Don't export footnote section, which will be handled at the end
1138 (unless (org-element-property :footnote-section-p headline
)
1139 (let* ((low-level-rank (org-export-low-level-p headline info
))
1140 (width (org-e-ascii--current-text-width headline info
))
1141 ;; Blank lines between headline and its contents.
1142 ;; `org-e-ascii-headline-spacing', when set, overwrites
1143 ;; original buffer's spacing.
1146 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1147 (org-element-property :pre-blank headline
)) ?
\n))
1148 ;; Even if HEADLINE has no section, there might be some
1149 ;; links in its title that we shouldn't forget to describe.
1151 (unless (eq (caar (org-element-contents headline
)) 'section
)
1152 (org-e-ascii--describe-links
1153 (org-e-ascii--unique-links headline info
) width info
))))
1154 ;; Deep subtree: export it as a list item.
1158 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1159 org-e-ascii-bullets
))))
1161 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1164 (org-e-ascii--build-title headline info width
) "\n"
1165 ;; Contents, indented by length of bullet.
1167 (org-e-ascii--indent-string
1169 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1171 ;; Else: Standard headline.
1173 (org-e-ascii--build-title headline info width
'underline
)
1175 (concat (when (org-string-nw-p links
) links
) contents
))))))
1178 ;;;; Horizontal Rule
1180 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1181 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1182 CONTENTS is nil. INFO is a plist holding contextual
1190 (org-element-property :attr_ascii horizontal-rule
)
1192 (make-string (or (and (wholenump (plist-get attr
:width
))
1193 (plist-get attr
:width
))
1194 (org-e-ascii--current-text-width horizontal-rule info
))
1195 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1198 ;;;; Inline Babel Call
1200 ;; Inline Babel Calls are ignored.
1203 ;;;; Inline Src Block
1205 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1206 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1207 CONTENTS holds the contents of the item. INFO is a plist holding
1208 contextual information."
1209 (format org-e-ascii-verbatim-format
1210 (org-element-property :value inline-src-block
)))
1215 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1216 "Transcode an INLINETASK element from Org to ASCII.
1217 CONTENTS holds the contents of the block. INFO is a plist
1218 holding contextual information."
1219 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1220 (title (org-export-secondary-string
1221 (org-element-property :title inlinetask
) 'e-ascii info
))
1222 (todo (and (plist-get info
:with-todo-keywords
)
1223 (let ((todo (org-element-property
1224 :todo-keyword inlinetask
)))
1226 (org-export-secondary-string todo
'e-ascii info
)))))
1227 (todo-type (org-element-property :todo-type inlinetask
))
1228 (tags (and (plist-get info
:with-tags
)
1229 (org-element-property :tags inlinetask
)))
1230 (priority (and (plist-get info
:with-priority
)
1231 (org-element-property :priority inlinetask
))))
1232 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1233 ;; with appropriate arguments.
1234 (if (functionp org-e-ascii-format-inlinetask-function
)
1235 (funcall org-e-ascii-format-inlinetask-function
1236 todo todo-type priority title tags contents width
)
1237 ;; Otherwise, use a default template.
1238 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1239 (org-e-ascii--indent-string
1241 ;; Top line, with an additional blank line if not in UTF-8.
1242 (make-string width
(if utf8p ?━ ?_
)) "\n"
1243 (unless utf8p
(concat (make-string width ?
) "\n"))
1244 ;; Add title. Fill it if wider than inlinetask.
1245 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1246 (if (<= (length title
) width
) title
1247 (org-e-ascii--fill-string title width info
)))
1249 ;; If CONTENTS is not empty, insert it along with
1251 (when (org-string-nw-p contents
)
1252 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1254 (make-string width
(if utf8p ?━ ?_
)))
1255 ;; Flush the inlinetask to the right.
1256 (- org-e-ascii-text-width org-e-ascii-global-margin
1257 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1258 org-e-ascii-inner-margin
)
1259 (org-e-ascii--current-text-width inlinetask info
)))))))
1264 (defun org-e-ascii-item (item contents info
)
1265 "Transcode an ITEM element from Org to ASCII.
1266 CONTENTS holds the contents of the item. INFO is a plist holding
1267 contextual information."
1269 ;; First parent of ITEM is always the plain-list. Get
1270 ;; `:type' property from it.
1271 (org-list-bullet-string
1272 (case (org-element-property :type
(org-export-get-parent item info
))
1275 (org-export-secondary-string
1276 (org-element-property :tag item
) 'e-ascii info
) ": "))
1278 ;; Return correct number for ITEM, paying attention to
1280 (let* ((struct (org-element-property :structure item
))
1281 (bul (org-element-property :bullet item
))
1284 (car (last (org-list-get-item-number
1285 (org-element-property :begin item
)
1287 (org-list-prevs-alist struct
)
1288 (org-list-parents-alist struct
)))))))
1289 (replace-regexp-in-string "[0-9]+" num bul
)))
1290 (t (let ((bul (org-element-property :bullet item
)))
1291 ;; Change bullets into more visible form if UTF-8 is active.
1292 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bul
1293 (replace-regexp-in-string
1295 (replace-regexp-in-string
1297 (replace-regexp-in-string "*" "‣" bul
))))))))))
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-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-property :key keyword
)))
1316 (value (org-element-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
))))))))
1334 ;;;; Latex Environment
1336 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1337 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1338 CONTENTS is nil. INFO is a plist holding contextual
1340 (org-remove-indentation (org-element-property :value latex-environment
)))
1345 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1346 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1347 CONTENTS is nil. INFO is a plist holding contextual
1349 (org-element-property :value latex-fragment
))
1354 (defun org-e-ascii-line-break (line-break contents info
)
1355 "Transcode a LINE-BREAK object from Org to ASCII.
1356 CONTENTS is nil. INFO is a plist holding contextual
1357 information." hard-newline
)
1362 (defun org-e-ascii-link (link desc info
)
1363 "Transcode a LINK object from Org to ASCII.
1365 DESC is the description part of the link, or the empty string.
1366 INFO is a plist holding contextual information."
1367 (let ((raw-link (org-element-property :raw-link link
))
1368 (type (org-element-property :type link
)))
1370 ((string= type
"coderef")
1371 (let ((ref (org-element-property :path link
)))
1372 (format (org-export-get-coderef-format ref desc
)
1373 (org-export-resolve-coderef ref info
))))
1374 ;; Do not apply a special syntax on radio links. Though, parse
1375 ;; and transcode path to have a proper display of contents.
1376 ((string= type
"radio")
1377 (org-export-secondary-string
1378 (org-element-parse-secondary-string
1379 (org-element-property :path link
)
1380 (cdr (assq 'radio-target org-element-object-restrictions
)))
1382 ;; Do not apply a special syntax on fuzzy links pointing to
1384 ((string= type
"fuzzy")
1385 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1386 ;; Ignore invisible "#+target: path".
1387 (unless (eq (org-element-type destination
) 'keyword
)
1388 (if (org-string-nw-p desc
) desc
1390 (let ((number (org-export-get-ordinal destination info
)))
1392 (if (atom number
) (number-to-string number
)
1393 (mapconcat 'number-to-string number
".")))))))))
1395 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1397 (format "[%s]" desc
)
1398 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1403 (defun org-e-ascii-macro (macro contents info
)
1404 "Transcode a MACRO element from Org to ASCII.
1405 CONTENTS is nil. INFO is a plist holding contextual
1407 (org-export-expand-macro macro info
))
1412 (defun org-e-ascii-paragraph (paragraph contents info
)
1413 "Transcode a PARAGRAPH element from Org to ASCII.
1414 CONTENTS is the contents of the paragraph, as a string. INFO is
1415 the plist used as a communication channel."
1416 (org-e-ascii--fill-string
1417 (let ((parent (org-export-get-parent paragraph info
)))
1418 ;; If PARAGRAPH is the first one in a list element, be sure to
1419 ;; add the check-box in front of it, before any filling. Later,
1420 ;; it would interfere with line width.
1421 (if (and (eq (org-element-type parent
) 'item
)
1422 (equal (car (org-element-contents parent
)) paragraph
))
1423 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1424 (concat (case (org-element-property :checkbox parent
)
1425 (on (if utf8p
"☑ " "[X] "))
1426 (off (if utf8p
"☐ " "[ ] "))
1427 (trans (if utf8p
"☒ " "[-] ")))
1430 (org-e-ascii--current-text-width paragraph info
) info
))
1435 (defun org-e-ascii-plain-list (plain-list contents info
)
1436 "Transcode a PLAIN-LIST element from Org to ASCII.
1437 CONTENTS is the contents of the list. INFO is a plist holding
1438 contextual information."
1444 (defun org-e-ascii-plain-text (text info
)
1445 "Transcode a TEXT string from Org to ASCII.
1446 INFO is a plist used as a communication channel."
1447 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1448 (plist-get info
:with-special-strings
)))
1450 ;; Usual replacements in utf-8 with proper option set.
1451 (replace-regexp-in-string
1453 (replace-regexp-in-string
1455 (replace-regexp-in-string "---" "—" text
)))))
1458 ;;;; Property Drawer
1460 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1461 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1462 CONTENTS is nil. INFO is a plist used as a communication
1464 ;; The property drawer isn't exported but we want separating blank
1465 ;; lines nonetheless.
1471 (defun org-e-ascii-quote-block (quote-block contents info
)
1472 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1473 CONTENTS holds the contents of the block. INFO is a plist
1474 holding contextual information."
1475 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1476 (org-e-ascii--indent-string
1477 (org-remove-indentation
1478 (org-e-ascii--fill-string contents width info
))
1479 org-e-ascii-quote-margin
)))
1484 (defun org-e-ascii-quote-section (quote-section contents info
)
1485 "Transcode a QUOTE-SECTION element from Org to ASCII.
1486 CONTENTS is nil. INFO is a plist holding contextual information."
1487 (let ((width (org-e-ascii--current-text-width quote-section info
))
1489 (org-export-secondary-string
1490 (org-remove-indentation
1491 (org-element-property :value quote-section
)) 'e-ascii info
)))
1492 (org-e-ascii--indent-string
1494 (+ org-e-ascii-quote-margin
1495 ;; Don't apply inner margin if parent headline is low level.
1496 (let ((headline (org-export-get-parent-headline quote-section info
)))
1497 (if (org-export-low-level-p headline info
) 0
1498 org-e-ascii-inner-margin
))))))
1503 (defun org-e-ascii-radio-target (radio-target contents info
)
1504 "Transcode a RADIO-TARGET object from Org to ASCII.
1505 CONTENTS is the contents of the target. INFO is a plist holding
1506 contextual information."
1511 (defun org-e-ascii-section (section contents info
)
1512 "Transcode a SECTION element from Org to ASCII.
1513 CONTENTS is the contents of the section. INFO is a plist holding
1514 contextual information."
1515 (org-e-ascii--indent-string
1518 (when org-e-ascii-links-to-notes
1519 ;; Add list of links at the end of SECTION.
1520 (let ((links (org-e-ascii--describe-links
1521 (org-e-ascii--unique-links section info
)
1522 (org-e-ascii--current-text-width section info
) info
)))
1523 ;; Separate list of links and section contents.
1524 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1525 ;; Do not apply inner margin if parent headline is low level.
1526 (let ((headline (org-export-get-parent-headline section info
)))
1527 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1528 org-e-ascii-inner-margin
))))
1533 (defun org-e-ascii-special-block (special-block contents info
)
1534 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1535 CONTENTS holds the contents of the block. INFO is a plist
1536 holding contextual information."
1542 (defun org-e-ascii-src-block (src-block contents info
)
1543 "Transcode a SRC-BLOCK element from Org to ASCII.
1544 CONTENTS holds the contents of the item. INFO is a plist holding
1545 contextual information."
1546 (let ((caption (org-e-ascii--build-caption src-block info
)))
1548 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1549 (org-e-ascii--box-string (org-export-handle-code src-block info
) info
)
1550 (when (and caption
(not org-e-ascii-caption-above
))
1551 (concat "\n" caption
)))))
1553 ;;;; Statistics Cookie
1555 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1556 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1557 CONTENTS is nil. INFO is a plist holding contextual information."
1558 (org-element-property :value statistics-cookie
))
1563 (defun org-e-ascii-subscript (subscript contents info
)
1564 "Transcode a SUBSCRIPT object from Org to ASCII.
1565 CONTENTS is the contents of the object. INFO is a plist holding
1566 contextual information."
1567 (if (org-element-property :use-brackets-p subscript
)
1568 (format "_{%s}" contents
)
1569 (format "_%s" contents
)))
1574 (defun org-e-ascii-superscript (superscript contents info
)
1575 "Transcode a SUPERSCRIPT object from Org to ASCII.
1576 CONTENTS is the contents of the object. INFO is a plist holding
1577 contextual information."
1578 (if (org-element-property :use-brackets-p superscript
)
1579 (format "_{%s}" contents
)
1580 (format "_%s" contents
)))
1585 ;; While `org-e-ascii-table' is the callback function expected by
1586 ;; org-export mechanism, it requires four subroutines to display
1587 ;; tables accordingly to chosen charset, alignment and width
1590 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1591 ;; for each column in the table,
1592 ;; `org-e-ascii-table--vertical-separators' returns a vector
1593 ;; containing separators (or lack thereof),
1594 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1595 ;; depending on charset, separators and position within the tabl and
1596 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1597 ;; a given cell and width.
1599 (defun org-e-ascii-table (table contents info
)
1600 "Transcode a TABLE element from Org to ASCII.
1601 CONTENTS is nil. INFO is a plist holding contextual information."
1602 (let ((raw-table (org-element-property :raw-table table
))
1603 (caption (org-e-ascii--build-caption table info
)))
1605 ;; Possibly add a caption string above.
1606 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1607 ;; Insert table. Note: "table.el" tables are left unmodified.
1608 (if (eq (org-element-property :type table
) 'table.el
) raw-table
1609 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1610 ;; Extract information out of the raw table (TABLE-INFO)
1611 ;; and clean it (CLEAN-TABLE).
1612 (table-info (org-export-table-format-info raw-table
))
1613 (special-col-p (plist-get table-info
:special-column-p
))
1614 (alignment (plist-get table-info
:alignment
))
1615 (clean-table (org-export-clean-table raw-table special-col-p
))
1616 ;; Change table into lisp, much like
1617 ;; `org-table-to-lisp', though cells are parsed and
1618 ;; transcoded along the way.
1622 (if (string-match org-table-hline-regexp line
) 'hline
1626 (org-export-secondary-string
1627 (org-element-parse-secondary-string
1629 (cdr (assq 'item org-element-string-restrictions
)))
1631 (org-split-string (org-trim line
) "\\s-?|\\s-?"))))
1632 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1633 ;; Compute real column widths.
1635 (org-e-ascii-table--column-width lisp-table table-info
))
1636 ;; Construct separators according to column groups.
1637 (separators (org-e-ascii-table--vertical-separators table-info
))
1638 ;; Build different `hline' strings, depending on
1639 ;; separators, column widths and position.
1641 (org-e-ascii-table--build-hline
1642 nil separators column-widths info
))
1644 (and utf8p
(org-e-ascii-table--build-hline
1645 'top separators column-widths info
)))
1647 (and utf8p
(org-e-ascii-table--build-hline
1648 'bottom separators column-widths info
))))
1649 ;; Now build table back, with correct alignment, considering
1650 ;; columns widths and separators.
1654 ((eq line
'hline
) hline-standard
)
1655 ((eq line
'hline-bottom
) hline-bottom
)
1656 ((eq line
'hline-top
) hline-top
)
1657 (t (loop for cell in line
1658 for col from
0 to
(length line
)
1661 (let ((sep (aref separators col
)))
1662 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1663 (org-e-ascii-table--format-cell
1664 cell col column-widths alignment info
)) into l
1667 (let ((sep (aref separators col
)))
1668 (if (and utf8p
(not (string= sep
""))) "│"
1670 ;; If charset is `utf-8', make sure lisp-table always starts
1671 ;; with `hline-top' and ends with `hline-bottom'.
1672 (if (not utf8p
) lisp-table
1675 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1679 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1681 '(hline-bottom)))) "\n")))
1682 ;; Possible add a caption string below.
1683 (when (and caption
(not org-e-ascii-caption-above
))
1684 (concat "\n" caption
)))))
1686 (defun org-e-ascii-table--column-width (table table-info
)
1687 "Return vector of TABLE columns width.
1689 TABLE is the Lisp representation of the Org table considered.
1690 TABLE-INFO holds information about the table. See
1691 `org-export-table-format-info'.
1693 Unlike to `:width' property from `org-export-table-format-info',
1694 the return value is a vector containing width of every column,
1695 not only those with an explicit width cookie. Special column, if
1697 ;; All rows have the same length, but be sure to ignore hlines.
1698 (let ((width (make-vector
1699 (loop for row in table
1700 unless
(eq row
'hline
)
1701 return
(length row
))
1703 ;; Set column width to the maximum width of the cells in that
1708 (unless (eq line
'hline
)
1709 (mapc (lambda (cell)
1710 (let ((len (length cell
)))
1711 (when (> len
(aref width idx
)) (aset width idx len
)))
1715 (unless org-e-ascii-table-widen-columns
1716 ;; When colums are not widened, width cookies have precedence
1717 ;; over string lengths. Thus, overwrite the latter with the
1719 (let ((cookies (plist-get table-info
:width
))
1720 (specialp (plist-get table-info
:special-column-p
)))
1721 ;; Remove special column from COOKIES vector, if any.
1722 (loop for w across
(if specialp
(substring cookies
1) cookies
)
1723 for idx from
0 to width
1724 when w do
(aset width idx w
))))
1728 (defun org-e-ascii-table--vertical-separators (table-info)
1729 "Return a vector of strings for vertical separators.
1731 TABLE-INFO holds information about considered table. See
1732 `org-export-table-format-info'.
1734 Return value is a vector whose length is one more than the number
1735 of columns in the table. Special column, if any, is ignored."
1736 (let* ((colgroups (plist-get table-info
:column-groups
))
1737 (separators (make-vector (1+ (length colgroups
)) "")))
1738 (if org-e-ascii-table-keep-all-vertical-lines
1739 (make-vector (length separators
) "|")
1741 (mapc (lambda (group)
1742 (when (memq group
'(start start-end
))
1743 (aset separators column
"|"))
1744 (when (memq group
'(end start-end
))
1745 (aset separators
(1+ column
) "|"))
1748 ;; Remove unneeded special column.
1749 (if (not (plist-get table-info
:special-column-p
)) separators
1750 (substring separators
1))))))
1752 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1753 "Format CELL with column width and alignment constraints.
1755 CELL is the contents of the cell, as a string.
1757 COL is the column containing the cell considered.
1759 WIDTH is a vector holding every column width, as returned by
1760 `org-e-ascii-table--column-width'.
1762 ALIGNMENT is a vector containing alignment strings for every
1765 INFO is a plist used as a communication channel."
1766 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1767 (or (aref width col
) (length cell
)))))
1768 ;; When CELL is too large, it has to be truncated.
1769 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1770 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1771 (let* ((indent-tabs-mode nil
)
1772 (align (aref alignment col
))
1774 (org-e-ascii--justify-string
1775 (org-trim cell
) col-width
1776 (cond ((string= align
"c") 'center
)
1777 ((string= align
"r") 'right
)))))
1778 ;; Return aligned cell, with missing white spaces added and
1779 ;; space separators between columns.
1782 (concat aligned-cell
1783 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1785 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1786 "Return string used as an horizontal line in tables.
1788 POSITION is a symbol among `top', `bottom' and nil, which
1789 specifies position of the horizontal line within the table.
1791 SEPARATORS is a vector strings specifying separators used in the
1792 table, as returned by `org-e-ascii-table--vertical-separators'.
1794 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1795 columns in the table, as returned by
1796 `org-e-ascii-table--column-width'.
1798 INFO is a plist used as a communication channel."
1799 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1800 (loop for idx from
0 to
(length separators
)
1801 for width across column-widths
1804 (cond ((string= (aref separators idx
) "") nil
)
1805 ((and utf8p
(zerop idx
))
1806 (cond ((eq position
'top
) "┍")
1807 ((eq position
'bottom
) "┕")
1810 (cond ((eq position
'top
) "┯")
1811 ((eq position
'bottom
) "┷")
1814 ;; Hline has to cover all the cell and both white spaces
1816 (make-string (+ width
2)
1817 (cond ((not utf8p
) ?-
)
1822 ;; There is one separator more than columns, so handle it
1827 ((string= (aref separators idx
) "") nil
)
1828 (utf8p (cond ((eq position
'top
) "┑")
1829 ((eq position
'bottom
) "┙")
1836 ;; Targets are invisible.
1841 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1842 "Transcode a TIME-STAMP object from Org to ASCII.
1843 CONTENTS is nil. INFO is a plist holding contextual information."
1844 ;; Return time-stamps as-is.
1845 (org-element-time-stamp-interpreter time-stamp contents
))
1850 (defun org-e-ascii-verbatim (verbatim contents info
)
1851 "Return a VERBATIM object from Org to ASCII.
1852 CONTENTS is nil. INFO is a plist holding contextual information."
1853 (format org-e-ascii-verbatim-format
1854 (org-element-property :value verbatim
)))
1859 (defun org-e-ascii-verse-block (verse-block contents info
)
1860 "Transcode a VERSE-BLOCK element from Org to ASCII.
1861 CONTENTS is nil. INFO is a plist holding contextual information."
1862 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1863 (org-e-ascii--indent-string
1864 (org-e-ascii--justify-string
1865 (org-export-secondary-string
1866 (org-element-property :value verse-block
) 'e-ascii info
)
1868 org-e-ascii-quote-margin
)))
1873 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1874 "Filter controlling number of blank lines after an headline.
1876 HEADLINE is a string representing a transcoded headline.
1877 BACK-END is symbol specifying back-end used for export. INFO is
1878 plist containing the communication channel.
1880 This function only applies to `e-ascii' back-end. See
1881 `org-e-ascii-headline-spacing' for information.
1883 For any other back-end, HEADLINE is returned as-is."
1884 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1885 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1886 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1890 ;;; Interactive function
1892 (defun org-e-ascii-export-to-ascii
1893 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1894 "Export current buffer to a text file.
1896 If narrowing is active in the current buffer, only export its
1899 If a region is active, export that region.
1901 When optional argument SUBTREEP is non-nil, export the sub-tree
1902 at point, extracting information from the headline properties
1905 When optional argument VISIBLE-ONLY is non-nil, don't export
1906 contents of hidden elements.
1908 When optional argument BODY-ONLY is non-nil, strip title, table
1909 of contents and footnote definitions from output.
1911 EXT-PLIST, when provided, is a property list with external
1912 parameters overriding Org default settings, but still inferior to
1913 file-local settings.
1915 When optional argument PUB-DIR is set, use it as the publishing
1918 Return output file's name."
1920 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1922 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1925 (provide 'org-e-ascii
)
1926 ;;; org-e-ascii.el ends here