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.
589 ;; Section numbering must be active, and headline's
590 ;; level should be above specified limit, if any.
591 (let ((sec-num (plist-get info
:section-numbers
)))
592 (if (not (wholenump sec-num
)) sec-num
593 (<= (org-export-get-relative-level headline info
) sec-num
)))
594 ;; All tests passed: build numbering string.
598 (org-export-get-headline-number element info
) ".")
600 (text (org-export-secondary-string
601 (org-element-property :title element
) 'e-ascii info
))
603 (and (plist-get info
:with-todo-keywords
)
604 (let ((todo (org-element-property :todo-keyword element
)))
606 (concat (org-export-secondary-string todo
'e-ascii info
)
608 (tags (and (not notags
)
609 (plist-get info
:with-tags
)
610 (org-element-property :tags element
)))
612 (and (plist-get info
:with-priority
)
613 (concat (org-element-property :priority element
) " ")))
614 (first-part (concat numbers todo priority text
)))
617 ;; Align tags, if any.
621 (max (- text-width
(1+ (length first-part
))) (length tags
)))
623 ;; Maybe underline text, if ELEMENT type is `headline' and an
624 ;; underline character has been defined.
625 (when (and underline headlinep
)
627 (nth (1- (org-export-get-relative-level element info
))
628 (cdr (assq (plist-get info
:ascii-charset
)
629 org-e-ascii-underline
)))))
632 (make-string (length first-part
) under-char
))))))))
634 (defun org-e-ascii--build-caption (element info
)
635 "Return caption string for ELEMENT, if applicable.
637 INFO is a plist used as a communication channel.
639 The caption string contains the sequence number of ELEMENT if it
640 has a name affiliated keyword, along with the real caption, if
641 any. Return nil when ELEMENT has no affiliated caption or name
643 (let ((caption (org-element-property :caption element
))
644 (name (org-element-property :name element
)))
645 (when (or caption name
)
646 ;; Get sequence number of current src-block among every
647 ;; src-block with either a caption or a name.
649 (org-export-get-ordinal
651 (lambda (el) (or (org-element-property :caption el
)
652 (org-element-property :name el
)))))
653 (title-fmt (org-e-ascii--translate
654 (case (org-element-type element
)
655 (table "Table %d: %s")
656 (src-block "Listing %d: %s")) info
)))
657 (org-e-ascii--fill-string
660 (if (not caption
) name
661 (org-export-secondary-string (car caption
) 'e-ascii info
)))
662 (org-e-ascii--current-text-width element info
) info
)))))
664 (defun org-e-ascii--build-toc (info &optional n keyword
)
665 "Return a table of contents.
667 INFO is a plist used as a communication channel.
669 Optional argument N, when non-nil, is an integer specifying the
672 Optional argument KEYWORD specifies the TOC keyword, if any, from
673 which the table of contents generation has been initiated."
674 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
677 (make-string (1- (length title
))
678 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
681 (if keyword
(org-e-ascii--current-text-width keyword info
)
682 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
685 (let* ((level (org-export-get-relative-level headline info
))
686 (indent (* (1- level
) 3)))
688 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
689 (org-e-ascii--build-title
690 headline info
(- text-width indent
) nil
691 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
692 (org-export-collect-headlines info n
) "\n")))))
694 (defun org-e-ascii--list-listings (keyword info
)
695 "Return a list of listings.
697 KEYWORD is the keyword that initiated the list of listings
698 generation. INFO is a plist used as a communication channel."
699 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
702 (make-string (1- (length title
))
703 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
706 (if keyword
(org-e-ascii--current-text-width keyword info
)
707 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
708 ;; Use a counter instead of retreiving ordinal of each
713 ;; Store initial text so its length can be computed. This is
714 ;; used to properly align caption right to it in case of
715 ;; filling (like contents of a description list item).
717 (format (org-e-ascii--translate "Listing %d: " info
)
722 (org-e-ascii--indent-string
723 (org-e-ascii--fill-string
724 (let ((caption (org-element-property :caption src-block
)))
725 (if (not caption
) (org-element-property :name src-block
)
726 (org-export-secondary-string
727 ;; Use short name in priority, if available.
728 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
729 (- text-width
(length initial-text
)) info
)
730 (length initial-text
))))))
731 (org-export-collect-listings info
) "\n")))))
733 (defun org-e-ascii--list-tables (keyword info
)
734 "Return a list of listings.
736 KEYWORD is the keyword that initiated the list of listings
737 generation. INFO is a plist used as a communication channel."
738 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
741 (make-string (1- (length title
))
742 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
745 (if keyword
(org-e-ascii--current-text-width keyword info
)
746 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
747 ;; Use a counter instead of retreiving ordinal of each
752 ;; Store initial text so its length can be computed. This is
753 ;; used to properly align caption right to it in case of
754 ;; filling (like contents of a description list item).
756 (format (org-e-ascii--translate "Table %d: " info
)
761 (org-e-ascii--indent-string
762 (org-e-ascii--fill-string
763 (let ((caption (org-element-property :caption table
)))
764 (if (not caption
) (org-element-property :name table
)
765 ;; Use short name in priority, if available.
766 (org-export-secondary-string
767 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
768 (- text-width
(length initial-text
)) info
)
769 (length initial-text
))))))
770 (org-export-collect-tables info
) "\n")))))
772 (defun org-e-ascii--unique-links (element info
)
773 "Return a list of unique link references in ELEMENT.
775 ELEMENT is either an headline element or a section element. INFO
776 is a plist used as a communication channel.
778 It covers links that may be found current headline's title, in
779 the following section and in any inlinetask's title there."
783 ;; Return LINK if it wasn't referenced so far, or nil.
784 ;; Update SEEN links along the way.
787 (cons (org-element-property :raw-link link
)
788 (org-element-contents link
))))
789 (unless (member footprint seen
)
790 (push footprint seen
) link
)))))
791 (harvest-links-in-title
793 ;; Return a list of all unique links in ELEMENT. ELEMENT
794 ;; may be an headline or an inlinetask element.
797 (dolist (obj (org-element-property :title element
) acc
)
798 (when (eq (org-element-type obj
) 'link
)
799 (let ((link (funcall unique-link-p obj
)))
800 (and link
(push link acc
)))))))))
801 ;; Retrieve HEADLINE's section, if it exists.
802 (section (if (eq (org-element-type element
) 'section
) element
803 (let ((sec (car (org-element-contents element
))))
804 (and (eq (org-element-type sec
) 'section
) sec
))))
805 (headline (if (eq (org-element-type element
) 'headline
) element
806 (org-export-get-parent-headline element info
))))
808 ;; Links that may be in HEADLINE's title.
809 (funcall harvest-links-in-title headline
)
810 ;; Get all links in SECTION.
812 section
'link
(lambda (link local
) (funcall unique-link-p link
)) info
))))
814 (defun org-e-ascii--describe-links (links width info
)
815 "Return a string describing a list of links.
817 LINKS is a list of link type objects, as returned by
818 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
819 the output string. INFO is a plist used as a communication
823 (let ((type (org-element-property :type link
))
824 (anchor (let ((desc (org-element-contents link
)))
825 (if (not desc
) (org-element-property :raw-link link
)
826 (org-export-secondary-string desc
'e-ascii info
)))))
828 ;; Coderefs, radio links and ref links are ignored.
829 ((member type
'("coderef" "radio" "ref")) nil
)
830 ;; Id, custom-id and fuzzy links (with the exception of
831 ;; targets): Headlines refer to their numbering.
832 ((member type
'("custom-id" "fuzzy" "id"))
833 (let ((destination (if (string= type
"fuzzy")
834 (org-export-resolve-fuzzy-link link info
)
835 (org-export-resolve-id-link link info
))))
836 (unless (eq (org-element-type destination
) 'target
)
838 (org-e-ascii--fill-string
842 (if (not destination
)
843 (org-e-ascii--translate "Unknown reference" info
)
845 (org-e-ascii--translate "See section %s" info
)
846 (mapconcat 'number-to-string
847 (org-export-get-headline-number destination info
)
849 width info
) "\n\n"))))
850 ;; Do not add a link that cannot be resolved and doesn't have
851 ;; any description: destination is already visible in the
853 ((not (org-element-contents link
)) nil
)
856 (org-e-ascii--fill-string
857 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
866 (defun org-e-ascii-template--document-title (info)
867 "Return document title, as a string.
868 INFO is a plist used as a communication channel."
869 (let ((text-width org-e-ascii-text-width
)
870 (title (org-export-secondary-string
871 (plist-get info
:title
) 'e-ascii info
))
873 (and (plist-get info
:with-author
)
874 (let ((auth (plist-get info
:author
)))
875 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
877 (and (plist-get info
:with-email
)
878 (org-export-secondary-string
879 (plist-get info
:email
) 'e-ascii info
)))
880 (date (plist-get info
:date
)))
881 ;; There are two types of title blocks depending on the presence
882 ;; of a title to display.
883 (if (string= title
"")
884 ;; Title block without a title. DATE is positioned at the top
885 ;; right of the document, AUTHOR to the top left and EMAIL
888 ((and (org-string-nw-p date
) (org-string-nw-p author
))
891 (make-string (- text-width
(length date
) (length author
)) ?
)
893 (when (org-string-nw-p email
) (concat "\n" email
))
895 ((and (org-string-nw-p date
) (org-string-nw-p email
))
898 (make-string (- text-width
(length date
) (length email
)) ?
)
900 ((org-string-nw-p date
)
902 (org-e-ascii--justify-string date text-width
'right
)
904 ((and (org-string-nw-p author
) (org-string-nw-p email
))
905 (concat author
"\n" email
"\n\n\n"))
906 ((org-string-nw-p author
) (concat author
"\n\n\n"))
907 ((org-string-nw-p email
) (concat email
"\n\n\n")))
908 ;; Title block with a title. Document's TITLE, along with the
909 ;; AUTHOR and its EMAIL are both overlined and an underlined,
910 ;; centered. Date is just below, also centered.
911 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
912 ;; Format TITLE. It may be filled if it is too wide,
913 ;; that is wider than the two thirds of the total width.
914 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
915 (formatted-title (org-e-ascii--fill-string title title-len info
))
918 (min (+ (max title-len
(length author
) (length email
)) 2)
919 text-width
) (if utf8p ?━ ?_
))))
920 (org-e-ascii--justify-string
923 (upcase formatted-title
)
925 ((and (org-string-nw-p author
) (org-string-nw-p email
))
926 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
927 ((org-string-nw-p author
)
928 (concat (if utf8p
"\n\n\n" "\n\n") author
))
929 ((org-string-nw-p email
)
930 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
932 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
933 "\n\n\n") text-width
'center
)))))
935 (defun org-e-ascii-template (contents info
)
936 "Return complete document string after ASCII conversion.
937 CONTENTS is the transcoded contents string. INFO is a plist
938 holding export options."
939 (org-element-normalize-string
940 (org-e-ascii--indent-string
941 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
942 ;; 1. Build title block.
944 (org-e-ascii-template--document-title info
)
945 ;; 2. Table of contents.
946 (let ((depth (plist-get info
:with-toc
)))
949 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
951 ;; 3. Document's body.
953 ;; 4. Footnote definitions.
954 (let ((definitions (org-export-collect-footnote-definitions
955 (plist-get info
:parse-tree
) info
))
956 ;; Insert full links right inside the footnote definition
957 ;; as they have no chance to be inserted later.
958 (org-e-ascii-links-to-notes nil
))
962 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
967 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
971 (let ((id (format "[%s] " (car ref
))))
972 ;; Distinguish between inline definitions and
973 ;; full-fledged definitions.
975 (let ((def (nth 2 ref
)))
976 (if (eq (org-element-type def
) 'org-data
)
977 ;; Full-fledged definition: footnote ID is
978 ;; inserted inside the first parsed paragraph
979 ;; (FIRST), if any, to be sure filling will
980 ;; take it into consideration.
981 (let ((first (car (org-element-contents def
))))
982 (if (not (eq (org-element-type first
) 'paragraph
))
983 (concat id
"\n" (org-export-data def
'e-ascii info
))
984 (push id
(nthcdr 2 first
))
985 (org-export-data def
'e-ascii info
)))
986 ;; Fill paragraph once footnote ID is inserted in
987 ;; order to have a correct length for first line.
988 (org-e-ascii--fill-string
989 (concat id
(org-export-secondary-string def
'e-ascii info
))
990 text-width info
))))))
991 definitions
"\n\n"))))
992 ;; 5. Creator. Ignore `comment' value as there are no comments in
993 ;; ASCII. Justify it to the bottom right.
994 (let ((creator-info (plist-get info
:with-creator
)))
995 (unless (or (not creator-info
) (eq creator-info
'comment
))
998 (org-e-ascii--fill-string
999 (plist-get info
:creator
) text-width info
'right
))))))
1000 org-e-ascii-global-margin
)))
1002 (defun org-e-ascii--translate (s info
)
1003 "Translate string S.
1005 INFO is a plist used as a communication channel.
1007 Translation depends on `:language' property and allowed charset.
1008 If no translation in found for a given language and a given
1009 charset, fall-back to S."
1010 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
1011 (lang (plist-get info
:language
))
1012 (translations (cdr (assoc s org-e-ascii-dictionary
))))
1013 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
1017 ;;; Transcode Functions
1021 ;; Babel Calls are ignored.
1026 (defun org-e-ascii-center-block (center-block contents info
)
1027 "Transcode a CENTER-BLOCK element from Org to ASCII.
1028 CONTENTS holds the contents of the block. INFO is a plist
1029 holding contextual information."
1030 (org-e-ascii--justify-string
1031 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1036 ;; Comments are ignored.
1041 ;; Comment Blocks are ignored.
1046 (defun org-e-ascii-drawer (drawer contents info
)
1047 "Transcode a DRAWER element from Org to ASCII.
1048 CONTENTS holds the contents of the block. INFO is a plist
1049 holding contextual information."
1050 (let ((name (org-element-property :drawer-name drawer
))
1051 (width (org-e-ascii--current-text-width drawer info
)))
1052 (if (functionp org-e-ascii-format-drawer-function
)
1053 (funcall org-e-ascii-format-drawer-function name contents width
)
1054 ;; If there's no user defined function: simply
1055 ;; display contents of the drawer.
1061 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1062 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1063 CONTENTS holds the contents of the block. INFO is a plist
1064 holding contextual information. See
1071 (defun org-e-ascii-emphasis (emphasis contents info
)
1072 "Transcode EMPHASIS from Org to ASCII.
1073 CONTENTS is the contents of the emphasized text. INFO is a plist
1074 holding contextual information.."
1075 (let ((marker (org-element-property :marker emphasis
)))
1076 ;; Leave emphasis markers as-is.
1077 (concat marker contents marker
)))
1082 (defun org-e-ascii-entity (entity contents info
)
1083 "Transcode an ENTITY object from Org to ASCII.
1084 CONTENTS are the definition itself. INFO is a plist holding
1085 contextual information."
1086 (org-element-property
1087 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1093 (defun org-e-ascii-example-block (example-block contents info
)
1094 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1095 CONTENTS is nil. INFO is a plist holding contextual information."
1096 (org-e-ascii--box-string (org-export-handle-code example-block info
) info
))
1101 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1102 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1103 CONTENTS is nil. INFO is a plist holding contextual information."
1104 (org-element-property :value export-snippet
))
1109 (defun org-e-ascii-export-block (export-block contents info
)
1110 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1111 CONTENTS is nil. INFO is a plist holding contextual information."
1112 (when (string= (org-element-property :type export-block
) "ascii")
1113 (org-remove-indentation (org-element-property :value export-block
))))
1118 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1119 "Transcode a FIXED-WIDTH element from Org to ASCII.
1120 CONTENTS is nil. INFO is a plist holding contextual information."
1121 (org-e-ascii--box-string
1122 (replace-regexp-in-string
1123 "^[ \t]*: ?" "" (org-element-property :value fixed-width
)) info
))
1126 ;;;; Footnote Definition
1128 ;; Footnote Definitions are ignored. They are compiled at the end of
1129 ;; the document, by `org-e-ascii-template'.
1132 ;;;; Footnote Reference
1134 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1135 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1136 CONTENTS is nil. INFO is a plist holding contextual information."
1137 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1142 (defun org-e-ascii-headline (headline contents info
)
1143 "Transcode an HEADLINE element from Org to ASCII.
1144 CONTENTS holds the contents of the headline. INFO is a plist
1145 holding contextual information."
1146 ;; Don't export footnote section, which will be handled at the end
1148 (unless (org-element-property :footnote-section-p headline
)
1149 (let* ((low-level-rank (org-export-low-level-p headline info
))
1150 (width (org-e-ascii--current-text-width headline info
))
1151 ;; Blank lines between headline and its contents.
1152 ;; `org-e-ascii-headline-spacing', when set, overwrites
1153 ;; original buffer's spacing.
1156 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1157 (org-element-property :pre-blank headline
)) ?
\n))
1158 ;; Even if HEADLINE has no section, there might be some
1159 ;; links in its title that we shouldn't forget to describe.
1161 (unless (eq (caar (org-element-contents headline
)) 'section
)
1162 (org-e-ascii--describe-links
1163 (org-e-ascii--unique-links headline info
) width info
))))
1164 ;; Deep subtree: export it as a list item.
1168 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1169 org-e-ascii-bullets
))))
1171 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1174 (org-e-ascii--build-title headline info width
) "\n"
1175 ;; Contents, indented by length of bullet.
1177 (org-e-ascii--indent-string
1179 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1181 ;; Else: Standard headline.
1183 (org-e-ascii--build-title headline info width
'underline
)
1185 (concat (when (org-string-nw-p links
) links
) contents
))))))
1188 ;;;; Horizontal Rule
1190 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1191 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1192 CONTENTS is nil. INFO is a plist holding contextual
1200 (org-element-property :attr_ascii horizontal-rule
)
1202 (make-string (or (and (wholenump (plist-get attr
:width
))
1203 (plist-get attr
:width
))
1204 (org-e-ascii--current-text-width horizontal-rule info
))
1205 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1208 ;;;; Inline Babel Call
1210 ;; Inline Babel Calls are ignored.
1213 ;;;; Inline Src Block
1215 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1216 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1217 CONTENTS holds the contents of the item. INFO is a plist holding
1218 contextual information."
1219 (format org-e-ascii-verbatim-format
1220 (org-element-property :value inline-src-block
)))
1225 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1226 "Transcode an INLINETASK element from Org to ASCII.
1227 CONTENTS holds the contents of the block. INFO is a plist
1228 holding contextual information."
1229 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1230 (title (org-export-secondary-string
1231 (org-element-property :title inlinetask
) 'e-ascii info
))
1232 (todo (and (plist-get info
:with-todo-keywords
)
1233 (let ((todo (org-element-property
1234 :todo-keyword inlinetask
)))
1236 (org-export-secondary-string todo
'e-ascii info
)))))
1237 (todo-type (org-element-property :todo-type inlinetask
))
1238 (tags (and (plist-get info
:with-tags
)
1239 (org-element-property :tags inlinetask
)))
1240 (priority (and (plist-get info
:with-priority
)
1241 (org-element-property :priority inlinetask
))))
1242 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1243 ;; with appropriate arguments.
1244 (if (functionp org-e-ascii-format-inlinetask-function
)
1245 (funcall org-e-ascii-format-inlinetask-function
1246 todo todo-type priority title tags contents width
)
1247 ;; Otherwise, use a default template.
1248 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1249 (org-e-ascii--indent-string
1251 ;; Top line, with an additional blank line if not in UTF-8.
1252 (make-string width
(if utf8p ?━ ?_
)) "\n"
1253 (unless utf8p
(concat (make-string width ?
) "\n"))
1254 ;; Add title. Fill it if wider than inlinetask.
1255 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1256 (if (<= (length title
) width
) title
1257 (org-e-ascii--fill-string title width info
)))
1259 ;; If CONTENTS is not empty, insert it along with
1261 (when (org-string-nw-p contents
)
1262 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1264 (make-string width
(if utf8p ?━ ?_
)))
1265 ;; Flush the inlinetask to the right.
1266 (- org-e-ascii-text-width org-e-ascii-global-margin
1267 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1268 org-e-ascii-inner-margin
)
1269 (org-e-ascii--current-text-width inlinetask info
)))))))
1274 (defun org-e-ascii-item (item contents info
)
1275 "Transcode an ITEM element from Org to ASCII.
1276 CONTENTS holds the contents of the item. INFO is a plist holding
1277 contextual information."
1279 ;; First parent of ITEM is always the plain-list. Get
1280 ;; `:type' property from it.
1281 (org-list-bullet-string
1282 (case (org-element-property :type
(org-export-get-parent item info
))
1285 (org-export-secondary-string
1286 (org-element-property :tag item
) 'e-ascii info
) ": "))
1288 ;; Return correct number for ITEM, paying attention to
1290 (let* ((struct (org-element-property :structure item
))
1291 (bul (org-element-property :bullet item
))
1294 (car (last (org-list-get-item-number
1295 (org-element-property :begin item
)
1297 (org-list-prevs-alist struct
)
1298 (org-list-parents-alist struct
)))))))
1299 (replace-regexp-in-string "[0-9]+" num bul
)))
1300 (t (let ((bul (org-element-property :bullet item
)))
1301 ;; Change bullets into more visible form if UTF-8 is active.
1302 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bul
1303 (replace-regexp-in-string
1305 (replace-regexp-in-string
1307 (replace-regexp-in-string "*" "‣" bul
))))))))))
1310 ;; Contents: Pay attention to indentation. Note: check-boxes are
1311 ;; already taken care of at the paragraph level so they don't
1312 ;; interfere with indentation.
1313 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1314 (if (eq (caar (org-element-contents item
)) 'paragraph
)
1316 (concat "\n" contents
))))))
1321 (defun org-e-ascii-keyword (keyword contents info
)
1322 "Transcode a KEYWORD element from Org to ASCII.
1323 CONTENTS is nil. INFO is a plist holding contextual
1325 (let ((key (downcase (org-element-property :key keyword
)))
1326 (value (org-element-property :value keyword
)))
1328 ((string= key
"ascii") value
)
1329 ((string= key
"toc")
1330 (let ((value (downcase value
)))
1332 ((string-match "\\<headlines\\>" value
)
1333 (let ((depth (or (and (string-match "[0-9]+" value
)
1334 (string-to-number (match-string 0 value
)))
1335 (plist-get info
:with-toc
))))
1336 (org-e-ascii--build-toc
1337 info
(and (wholenump depth
) depth
) keyword
)))
1338 ((string= "tables" value
)
1339 (org-e-ascii--list-tables keyword info
))
1340 ((string= "listings" value
)
1341 (org-e-ascii--list-listings keyword info
))))))))
1344 ;;;; Latex Environment
1346 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1347 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1348 CONTENTS is nil. INFO is a plist holding contextual
1350 (org-remove-indentation (org-element-property :value latex-environment
)))
1355 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1356 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1357 CONTENTS is nil. INFO is a plist holding contextual
1359 (org-element-property :value latex-fragment
))
1364 (defun org-e-ascii-line-break (line-break contents info
)
1365 "Transcode a LINE-BREAK object from Org to ASCII.
1366 CONTENTS is nil. INFO is a plist holding contextual
1367 information." hard-newline
)
1372 (defun org-e-ascii-link (link desc info
)
1373 "Transcode a LINK object from Org to ASCII.
1375 DESC is the description part of the link, or the empty string.
1376 INFO is a plist holding contextual information."
1377 (let ((raw-link (org-element-property :raw-link link
))
1378 (type (org-element-property :type link
)))
1380 ((string= type
"coderef")
1381 (let ((ref (org-element-property :path link
)))
1382 (format (org-export-get-coderef-format ref desc
)
1383 (org-export-resolve-coderef ref info
))))
1384 ;; Do not apply a special syntax on radio links. Though, parse
1385 ;; and transcode path to have a proper display of contents.
1386 ((string= type
"radio")
1387 (org-export-secondary-string
1388 (org-element-parse-secondary-string
1389 (org-element-property :path link
)
1390 (cdr (assq 'radio-target org-element-object-restrictions
)))
1392 ;; Ref link: If there's no description (DESC, return link's
1393 ;; destination sequence number among elements of same
1394 ;; type. Otherwise, use DESC.
1395 ((string= type
"ref")
1396 (if (org-string-nw-p desc
) desc
1398 (org-export-get-ordinal
1399 (org-export-resolve-ref-link link info
)
1401 (lambda (el) (or (org-element-property :caption el
)
1402 (org-element-property :name el
)))))))
1403 ;; Do not apply a special syntax on fuzzy links pointing to
1405 ((and (string= type
"fuzzy")
1406 (let ((path (org-element-property :path link
)))
1407 (loop for target in
(plist-get info
:target-list
)
1409 (org-element-property :raw-value target
)
1411 (if (org-string-nw-p desc
) desc raw-link
))
1413 (concat (format "[%s]" (if (org-string-nw-p desc
) desc raw-link
))
1414 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
)))))))
1419 (defun org-e-ascii-macro (macro contents info
)
1420 "Transcode a MACRO element from Org to ASCII.
1421 CONTENTS is nil. INFO is a plist holding contextual
1423 (org-export-expand-macro macro info
))
1428 (defun org-e-ascii-paragraph (paragraph contents info
)
1429 "Transcode a PARAGRAPH element from Org to ASCII.
1430 CONTENTS is the contents of the paragraph, as a string. INFO is
1431 the plist used as a communication channel."
1432 (org-e-ascii--fill-string
1433 (let ((parent (org-export-get-parent paragraph info
)))
1434 ;; If PARAGRAPH is the first one in a list element, be sure to
1435 ;; add the check-box in front of it, before any filling. Later,
1436 ;; it would interfere with line width.
1437 (if (and (eq (org-element-type parent
) 'item
)
1438 (equal (car (org-element-contents parent
)) paragraph
))
1439 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1440 (concat (case (org-element-property :checkbox parent
)
1441 (on (if utf8p
"☑ " "[X] "))
1442 (off (if utf8p
"☐ " "[ ] "))
1443 (trans (if utf8p
"☒ " "[-] ")))
1446 (org-e-ascii--current-text-width paragraph info
) info
))
1451 (defun org-e-ascii-plain-list (plain-list contents info
)
1452 "Transcode a PLAIN-LIST element from Org to ASCII.
1453 CONTENTS is the contents of the list. INFO is a plist holding
1454 contextual information."
1460 (defun org-e-ascii-plain-text (text info
)
1461 "Transcode a TEXT string from Org to ASCII.
1462 INFO is a plist used as a communication channel."
1463 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1464 (plist-get info
:with-special-strings
)))
1466 ;; Usual replacements in utf-8 with proper option set.
1467 (replace-regexp-in-string
1469 (replace-regexp-in-string
1471 (replace-regexp-in-string "---" "—" text
)))))
1474 ;;;; Property Drawer
1476 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1477 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1478 CONTENTS is nil. INFO is a plist used as a communication
1480 ;; The property drawer isn't exported but we want separating blank
1481 ;; lines nonetheless.
1487 (defun org-e-ascii-quote-block (quote-block contents info
)
1488 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1489 CONTENTS holds the contents of the block. INFO is a plist
1490 holding contextual information."
1491 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1492 (org-e-ascii--indent-string
1493 (org-remove-indentation
1494 (org-e-ascii--fill-string contents width info
))
1495 org-e-ascii-quote-margin
)))
1500 (defun org-e-ascii-quote-section (quote-section contents info
)
1501 "Transcode a QUOTE-SECTION element from Org to ASCII.
1502 CONTENTS is nil. INFO is a plist holding contextual information."
1503 (let ((width (org-e-ascii--current-text-width quote-section info
))
1505 (org-export-secondary-string
1506 (org-remove-indentation
1507 (org-element-property :value quote-section
)) 'e-ascii info
)))
1508 (org-e-ascii--indent-string
1510 (+ org-e-ascii-quote-margin
1511 ;; Don't apply inner margin if parent headline is low level.
1512 (let ((headline (org-export-get-parent-headline quote-section info
)))
1513 (if (org-export-low-level-p headline info
) 0
1514 org-e-ascii-inner-margin
))))))
1519 (defun org-e-ascii-radio-target (radio-target contents info
)
1520 "Transcode a RADIO-TARGET object from Org to ASCII.
1521 CONTENTS is the contents of the target. INFO is a plist holding
1522 contextual information."
1527 (defun org-e-ascii-section (section contents info
)
1528 "Transcode a SECTION element from Org to ASCII.
1529 CONTENTS is the contents of the section. INFO is a plist holding
1530 contextual information."
1531 (org-e-ascii--indent-string
1534 (when org-e-ascii-links-to-notes
1535 ;; Add list of links at the end of SECTION.
1536 (let ((links (org-e-ascii--describe-links
1537 (org-e-ascii--unique-links section info
)
1538 (org-e-ascii--current-text-width section info
) info
)))
1539 ;; Separate list of links and section contents.
1540 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1541 ;; Do not apply inner margin if parent headline is low level.
1542 (let ((headline (org-export-get-parent-headline section info
)))
1543 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1544 org-e-ascii-inner-margin
))))
1549 (defun org-e-ascii-special-block (special-block contents info
)
1550 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1551 CONTENTS holds the contents of the block. INFO is a plist
1552 holding contextual information."
1558 (defun org-e-ascii-src-block (src-block contents info
)
1559 "Transcode a SRC-BLOCK element from Org to ASCII.
1560 CONTENTS holds the contents of the item. INFO is a plist holding
1561 contextual information."
1562 (let ((caption (org-e-ascii--build-caption src-block info
)))
1564 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1565 (org-e-ascii--box-string (org-export-handle-code src-block info
) info
)
1566 (when (and caption
(not org-e-ascii-caption-above
))
1567 (concat "\n" caption
)))))
1569 ;;;; Statistics Cookie
1571 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1572 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1573 CONTENTS is nil. INFO is a plist holding contextual information."
1574 (org-element-property :value statistics-cookie
))
1579 (defun org-e-ascii-subscript (subscript contents info
)
1580 "Transcode a SUBSCRIPT object from Org to ASCII.
1581 CONTENTS is the contents of the object. INFO is a plist holding
1582 contextual information."
1583 (if (org-element-property :use-brackets-p subscript
)
1584 (format "_{%s}" contents
)
1585 (format "_%s" contents
)))
1590 (defun org-e-ascii-superscript (superscript contents info
)
1591 "Transcode a SUPERSCRIPT object from Org to ASCII.
1592 CONTENTS is the contents of the object. INFO is a plist holding
1593 contextual information."
1594 (if (org-element-property :use-brackets-p superscript
)
1595 (format "_{%s}" contents
)
1596 (format "_%s" contents
)))
1601 ;; While `org-e-ascii-table' is the callback function expected by
1602 ;; org-export mechanism, it requires four subroutines to display
1603 ;; tables accordingly to chosen charset, alignment and width
1606 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1607 ;; for each column in the table,
1608 ;; `org-e-ascii-table--vertical-separators' returns a vector
1609 ;; containing separators (or lack thereof),
1610 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1611 ;; depending on charset, separators and position within the tabl and
1612 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1613 ;; a given cell and width.
1615 (defun org-e-ascii-table (table contents info
)
1616 "Transcode a TABLE element from Org to ASCII.
1617 CONTENTS is nil. INFO is a plist holding contextual information."
1618 (let ((raw-table (org-element-property :raw-table table
))
1619 (caption (org-e-ascii--build-caption table info
)))
1621 ;; Possibly add a caption string above.
1622 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1623 ;; Insert table. Note: "table.el" tables are left unmodified.
1624 (if (eq (org-element-property :type table
) 'table.el
) raw-table
1625 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1626 ;; Extract information out of the raw table (TABLE-INFO)
1627 ;; and clean it (CLEAN-TABLE).
1628 (table-info (org-export-table-format-info raw-table
))
1629 (special-col-p (plist-get table-info
:special-column-p
))
1630 (alignment (plist-get table-info
:alignment
))
1631 (clean-table (org-export-clean-table raw-table special-col-p
))
1632 ;; Change table into lisp, much like
1633 ;; `org-table-to-lisp', though cells are parsed and
1634 ;; transcoded along the way.
1638 (if (string-match org-table-hline-regexp line
) 'hline
1642 (org-export-secondary-string
1643 (org-element-parse-secondary-string
1645 (cdr (assq 'item org-element-string-restrictions
)))
1647 (org-split-string (org-trim line
) "\\s-?|\\s-?"))))
1648 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1649 ;; Compute real column widths.
1651 (org-e-ascii-table--column-width lisp-table table-info
))
1652 ;; Construct separators according to column groups.
1653 (separators (org-e-ascii-table--vertical-separators table-info
))
1654 ;; Build different `hline' strings, depending on
1655 ;; separators, column widths and position.
1657 (org-e-ascii-table--build-hline
1658 nil separators column-widths info
))
1660 (and utf8p
(org-e-ascii-table--build-hline
1661 'top separators column-widths info
)))
1663 (and utf8p
(org-e-ascii-table--build-hline
1664 'bottom separators column-widths info
))))
1665 ;; Now build table back, with correct alignment, considering
1666 ;; columns widths and separators.
1670 ((eq line
'hline
) hline-standard
)
1671 ((eq line
'hline-bottom
) hline-bottom
)
1672 ((eq line
'hline-top
) hline-top
)
1673 (t (loop for cell in line
1674 for col from
0 to
(length line
)
1677 (let ((sep (aref separators col
)))
1678 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1679 (org-e-ascii-table--format-cell
1680 cell col column-widths alignment info
)) into l
1683 (let ((sep (aref separators col
)))
1684 (if (and utf8p
(not (string= sep
""))) "│"
1686 ;; If charset is `utf-8', make sure lisp-table always starts
1687 ;; with `hline-top' and ends with `hline-bottom'.
1688 (if (not utf8p
) lisp-table
1691 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1695 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1697 '(hline-bottom)))) "\n")))
1698 ;; Possible add a caption string below.
1699 (when (and caption
(not org-e-ascii-caption-above
))
1700 (concat "\n" caption
)))))
1702 (defun org-e-ascii-table--column-width (table table-info
)
1703 "Return vector of TABLE columns width.
1705 TABLE is the Lisp representation of the Org table considered.
1706 TABLE-INFO holds information about the table. See
1707 `org-export-table-format-info'.
1709 Unlike to `:width' property from `org-export-table-format-info',
1710 the return value is a vector containing width of every column,
1711 not only those with an explicit width cookie. Special column, if
1713 ;; All rows have the same length, but be sure to ignore hlines.
1714 (let ((width (make-vector
1715 (loop for row in table
1716 unless
(eq row
'hline
)
1717 return
(length row
))
1719 ;; Set column width to the maximum width of the cells in that
1724 (unless (eq line
'hline
)
1725 (mapc (lambda (cell)
1726 (let ((len (length cell
)))
1727 (when (> len
(aref width idx
)) (aset width idx len
)))
1731 (unless org-e-ascii-table-widen-columns
1732 ;; When colums are not widened, width cookies have precedence
1733 ;; over string lengths. Thus, overwrite the latter with the
1735 (let ((cookies (plist-get table-info
:width
))
1736 (specialp (plist-get table-info
:special-column-p
)))
1737 ;; Remove special column from COOKIES vector, if any.
1738 (loop for w across
(if specialp
(substring cookies
1) cookies
)
1739 for idx from
0 to width
1740 when w do
(aset width idx w
))))
1744 (defun org-e-ascii-table--vertical-separators (table-info)
1745 "Return a vector of strings for vertical separators.
1747 TABLE-INFO holds information about considered table. See
1748 `org-export-table-format-info'.
1750 Return value is a vector whose length is one more than the number
1751 of columns in the table. Special column, if any, is ignored."
1752 (let* ((colgroups (plist-get table-info
:column-groups
))
1753 (separators (make-vector (1+ (length colgroups
)) "")))
1754 (if org-e-ascii-table-keep-all-vertical-lines
1755 (make-vector (length separators
) "|")
1757 (mapc (lambda (group)
1758 (when (memq group
'(start start-end
))
1759 (aset separators column
"|"))
1760 (when (memq group
'(end start-end
))
1761 (aset separators
(1+ column
) "|"))
1764 ;; Remove unneeded special column.
1765 (if (not (plist-get table-info
:special-column-p
)) separators
1766 (substring separators
1))))))
1768 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1769 "Format CELL with column width and alignment constraints.
1771 CELL is the contents of the cell, as a string.
1773 COL is the column containing the cell considered.
1775 WIDTH is a vector holding every column width, as returned by
1776 `org-e-ascii-table--column-width'.
1778 ALIGNMENT is a vector containing alignment strings for every
1781 INFO is a plist used as a communication channel."
1782 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1783 (or (aref width col
) (length cell
)))))
1784 ;; When CELL is too large, it has to be truncated.
1785 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1786 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1787 (let* ((indent-tabs-mode nil
)
1788 (align (aref alignment col
))
1790 (org-e-ascii--justify-string
1791 (org-trim cell
) col-width
1792 (cond ((string= align
"c") 'center
)
1793 ((string= align
"r") 'right
)))))
1794 ;; Return aligned cell, with missing white spaces added and
1795 ;; space separators between columns.
1798 (concat aligned-cell
1799 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1801 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1802 "Return string used as an horizontal line in tables.
1804 POSITION is a symbol among `top', `bottom' and nil, which
1805 specifies position of the horizontal line within the table.
1807 SEPARATORS is a vector strings specifying separators used in the
1808 table, as returned by `org-e-ascii-table--vertical-separators'.
1810 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1811 columns in the table, as returned by
1812 `org-e-ascii-table--column-width'.
1814 INFO is a plist used as a communication channel."
1815 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1816 (loop for idx from
0 to
(length separators
)
1817 for width across column-widths
1820 (cond ((string= (aref separators idx
) "") nil
)
1821 ((and utf8p
(zerop idx
))
1822 (cond ((eq position
'top
) "┍")
1823 ((eq position
'bottom
) "┕")
1826 (cond ((eq position
'top
) "┯")
1827 ((eq position
'bottom
) "┷")
1830 ;; Hline has to cover all the cell and both white spaces
1832 (make-string (+ width
2)
1833 (cond ((not utf8p
) ?-
)
1838 ;; There is one separator more than columns, so handle it
1843 ((string= (aref separators idx
) "") nil
)
1844 (utf8p (cond ((eq position
'top
) "┑")
1845 ((eq position
'bottom
) "┙")
1852 (defun org-e-ascii-target (target contents info
)
1853 "Transcode a TARGET object from Org to ASCII.
1854 CONTENTS is the contents of the target. INFO is a plist holding
1855 contextual information."
1861 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1862 "Transcode a TIME-STAMP object from Org to ASCII.
1863 CONTENTS is nil. INFO is a plist holding contextual information."
1864 ;; Return time-stamps as-is.
1865 (org-element-time-stamp-interpreter time-stamp contents
))
1870 (defun org-e-ascii-verbatim (verbatim contents info
)
1871 "Return a VERBATIM object from Org to ASCII.
1872 CONTENTS is nil. INFO is a plist holding contextual information."
1873 (format org-e-ascii-verbatim-format
1874 (org-element-property :value verbatim
)))
1879 (defun org-e-ascii-verse-block (verse-block contents info
)
1880 "Transcode a VERSE-BLOCK element from Org to ASCII.
1881 CONTENTS is nil. INFO is a plist holding contextual information."
1882 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1883 (org-e-ascii--indent-string
1884 (org-e-ascii--justify-string
1885 (org-export-secondary-string
1886 (org-element-property :value verse-block
) 'e-ascii info
)
1888 org-e-ascii-quote-margin
)))
1893 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1894 "Filter controlling number of blank lines after an headline.
1896 HEADLINE is a string representing a transcoded headline.
1897 BACK-END is symbol specifying back-end used for export. INFO is
1898 plist containing the communication channel.
1900 This function only applies to `e-ascii' back-end. See
1901 `org-e-ascii-headline-spacing' for information.
1903 For any other back-end, HEADLINE is returned as-is."
1904 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1905 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1906 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1910 ;;; Interactive function
1912 (defun org-e-ascii-export-to-ascii
1913 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1914 "Export current buffer to a text file.
1916 If narrowing is active in the current buffer, only export its
1919 If a region is active, export that region.
1921 When optional argument SUBTREEP is non-nil, export the sub-tree
1922 at point, extracting information from the headline properties
1925 When optional argument VISIBLE-ONLY is non-nil, don't export
1926 contents of hidden elements.
1928 When optional argument BODY-ONLY is non-nil, strip title, table
1929 of contents and footnote definitions from output.
1931 EXT-PLIST, when provided, is a property list with external
1932 parameters overriding Org default settings, but still inferior to
1933 file-local settings.
1935 When optional argument PUB-DIR is set, use it as the publishing
1938 Return output file's name."
1940 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1942 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1945 (provide 'org-e-ascii
)
1946 ;;; org-e-ascii.el ends here