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-collect-footnote-definitions
46 "org-export" (data info
))
47 (declare-function org-export-collect-headlines
"org-export" (info &optional n
))
48 (declare-function org-export-collect-listings
"org-export" (info))
49 (declare-function org-export-collect-tables
"org-export" (info))
50 (declare-function org-export-data
"org-export" (data info
))
51 (declare-function org-export-expand-macro
"org-export" (macro info
))
52 (declare-function org-export-format-code-default
"org-export" (element 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 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-low-level-p
"org-export" (headline info
))
61 (declare-function org-export-output-file-name
"org-export"
62 (extension &optional subtreep pub-dir
))
63 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
64 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
65 (declare-function org-export-resolve-id-link
"org-export" (link info
))
66 (declare-function org-export-resolve-ref-link
"org-export" (link info
))
67 (declare-function org-export-secondary-string
"org-export" (secondary info
))
69 org-export-to-file
"org-export"
70 (backend file
&optional subtreep visible-only body-only ext-plist
))
74 ;;; Internal Variables
76 ;; The following setting won't allow to modify preferred charset
77 ;; through a buffer keyword or an option item, but, since the property
78 ;; will appear in communication channel nonetheless, it allows to
79 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
82 ;; We also install a filter for headlines and sections, in order to
83 ;; control blank lines separating them in output string.
85 (defconst org-e-ascii-option-alist
86 '((:ascii-charset nil nil org-e-ascii-charset
)
88 "Alist between ASCII export properties and ways to set them.
89 See `org-export-option-alist' for more information on the
90 structure or the values.")
92 (defconst org-e-ascii-filters-alist
93 '((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
94 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
95 "Alist between filters keywords and back-end specific filters.
96 See `org-export-filters-alist' for more information.")
98 (defconst org-e-ascii-dictionary
102 :latin1
"Footnotes\n"
103 :utf-8
"Footnotes\n")
105 :ascii
"Notes de bas de page\n"
106 :latin1
"Notes de bas de page\n"
107 :utf-8
"Notes de bas de page\n"))
110 :ascii
"Listing %d: %s"
111 :latin1
"Listing %d: %s"
112 :utf-8
"Listing %d: %s")
114 :ascii
"Programme %d : %s"
115 :latin1
"Programme %d : %s"
116 :utf-8
"Programme nº %d : %s"))
117 ("List Of Listings\n"
119 :ascii
"List Of Listings\n"
120 :latin1
"List Of Listings\n"
121 :utf-8
"List Of Listings\n")
123 :ascii
"Liste des programmes\n"
124 :latin1
"Liste des programmes\n"
125 :utf-8
"Liste des programmes\n"))
128 :ascii
"List Of Tables\n"
129 :latin1
"List Of Tables\n"
130 :utf-8
"List Of Tables\n")
132 :ascii
"Liste des tableaux\n"
133 :latin1
"Liste des tableaux\n"
134 :utf-8
"Liste des tableaux\n"))
137 :ascii
"Listing %d: "
138 :latin1
"Listing %d: "
139 :utf-8
"Listing %d: ")
141 :ascii
"Programme %d : "
142 :latin1
"Programme %d : "
143 :utf-8
"Programme nº %d : "))
144 ("Table Of Contents\n"
146 :ascii
"Table Of Contents\n"
147 :latin1
"Table Of Contents\n"
148 :utf-8
"Table Of Contents\n")
151 :latin1
"Table des matières\n"
152 :utf-8
"Table des matières\n"))
155 :ascii
"Table %d: %s"
156 :latin1
"Table %d: %s"
157 :utf-8
"Table %d: %s")
159 :ascii
"Tableau %d : %s"
160 :latin1
"Tableau %d : %s"
161 :utf-8
"Tableau nº %d : %s"))
164 :ascii
"See section %s"
165 :latin1
"See section %s"
166 :utf-8
"See section %s")
168 :ascii
"cf. section %s"
169 :latin1
"cf. section %s"
170 :utf-8
"cf. section %s"))
177 :ascii
"Tableau %d : "
178 :latin1
"Tableau %d : "
179 :utf-8
"Tableau nº %d : "))
182 :ascii
"Unknown reference"
183 :latin1
"Unknown reference"
184 :utf-8
"Unknown reference")
186 :ascii
"Destination inconnue"
187 :latin1
"Référence inconnue"
188 :utf-8
"Référence inconnue")))
189 "Dictionary for ASCII back-end.
191 Alist whose car is the string to translate and cdr is an alist
192 whose car is the language string and cdr is a plist whose
193 properties are possible charsets and value the translated term.
195 It is used as a database for `org-e-ascii--translate'.")
199 ;;; User Configurable Variables
201 (defgroup org-export-e-ascii nil
202 "Options for exporting Org mode files to ASCII."
203 :tag
"Org Export ASCII"
206 (defcustom org-e-ascii-text-width
72
207 "Maximum width of exported text.
208 This number includes margin size, as set in
209 `org-e-ascii-global-margin'."
210 :group
'org-export-e-ascii
213 (defcustom org-e-ascii-global-margin
0
214 "Width of the left margin, in number of characters."
215 :group
'org-export-e-ascii
218 (defcustom org-e-ascii-inner-margin
2
219 "Width of the inner margin, in number of characters.
220 Inner margin is applied between each headline."
221 :group
'org-export-e-ascii
224 (defcustom org-e-ascii-quote-margin
6
225 "Width of margin used for quoting text, in characters.
226 This margin is applied on both sides of the text."
227 :group
'org-export-e-ascii
230 (defcustom org-e-ascii-inlinetask-width
30
231 "Width of inline tasks, in number of characters.
232 This number ignores any margin."
233 :group
'org-export-e-ascii
236 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
237 "Number of blank lines inserted around headlines.
239 This variable can be set to a cons cell. In that case, its car
240 represents the number of blank lines present before headline
241 contents whereas its cdr reflects the number of blank lines after
244 A nil value replicates the number of blank lines found in the
245 original Org buffer at the same place."
246 :group
'org-export-e-ascii
248 (const :tag
"Replicate original spacing" nil
)
249 (cons :tag
"Set an uniform spacing"
250 (integer :tag
"Number of blank lines before contents")
251 (integer :tag
"Number of blank lines after contents"))))
253 (defcustom org-e-ascii-charset
'ascii
254 "The charset allowed to represent various elements and objects.
256 `ascii' Only use plain ASCII characters
257 `latin1' Include Latin-1 characters
258 `utf-8' Use all UTF-8 characters"
259 :group
'org-export-e-ascii
261 (const :tag
"ASCII" ascii
)
262 (const :tag
"Latin-1" latin1
)
263 (const :tag
"UTF-8" utf-8
)))
265 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
267 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
268 "Characters for underlining headings in ASCII export.
270 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
271 and whose value is a list of characters.
273 For each supported charset, this variable associates a sequence
274 of underline characters. In a sequence, the characters will be
275 used in order for headlines level 1, 2, ... If no character is
276 available for a given level, the headline won't be underlined."
277 :group
'org-export-e-ascii
279 (cons :tag
"Underline characters sequence"
280 (const :tag
"ASCII charset" ascii
)
282 (cons :tag
"Underline characters sequence"
283 (const :tag
"Latin-1 charset" latin1
)
285 (cons :tag
"Underline characters sequence"
286 (const :tag
"UTF-8 charset" utf-8
)
287 (repeat character
))))
289 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
292 "Bullet characters for headlines converted to lists in ASCII export.
294 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
295 and whose value is a list of characters.
297 The first character is used for the first level considered as low
298 level, and so on. If there are more levels than characters given
299 here, the list will be repeated.
301 Note that this variable doesn't affect plain lists
303 :group
'org-export-e-ascii
305 (cons :tag
"Bullet characters for low level headlines"
306 (const :tag
"ASCII charset" ascii
)
308 (cons :tag
"Bullet characters for low level headlines"
309 (const :tag
"Latin-1 charset" latin1
)
311 (cons :tag
"Bullet characters for low level headlines"
312 (const :tag
"UTF-8 charset" utf-8
)
313 (repeat character
))))
315 (defcustom org-e-ascii-links-to-notes t
316 "Non-nil means convert links to notes before the next headline.
317 When nil, the link will be exported in place. If the line
318 becomes long in this way, it will be wrapped."
319 :group
'org-export-e-ascii
322 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
323 "Non-nil means keep all vertical lines in ASCII tables.
324 When nil, vertical lines will be removed except for those needed
325 for column grouping."
326 :group
'org-export-e-ascii
329 (defcustom org-e-ascii-table-widen-columns t
330 "Non-nil means widen narrowed columns for export.
331 When nil, narrowed columns will look in ASCII export just like in
332 Org mode, i.e. with \"=>\" as ellipsis."
333 :group
'org-export-e-ascii
336 (defcustom org-e-ascii-caption-above nil
337 "When non-nil, place caption string before the element.
338 Otherwise, place it right after it."
339 :group
'org-export-e-ascii
342 (defcustom org-e-ascii-verbatim-format
"`%s'"
343 "Format string used for verbatim text and inline code."
344 :group
'org-export-e-ascii
347 (defcustom org-e-ascii-format-drawer-function nil
348 "Function called to format a drawer in ASCII.
350 The function must accept two parameters:
351 NAME the drawer name, like \"LOGBOOK\"
352 CONTENTS the contents of the drawer.
353 WIDTH the text width within the drawer.
355 The function should return either the string to be exported or
356 nil to ignore the drawer.
358 For example, the variable could be set to the following function
359 in order to mimic default behaviour:
361 \(defun org-e-ascii-format-drawer-default \(name contents width\)
362 \"Format a drawer element for ASCII export.\"
364 :group
'org-export-e-ascii
367 (defcustom org-e-ascii-format-inlinetask-function nil
368 "Function called to format an inlinetask in ASCII.
370 The function must accept six parameters:
371 TODO the todo keyword, as a string
372 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
373 PRIORITY the inlinetask priority, as a string
374 NAME the inlinetask name, as a string.
375 TAGS the inlinetask tags, as a string.
376 CONTENTS the contents of the inlinetask, as a string.
378 The function should return either the string to be exported or
379 nil to ignore the inline task.
381 For example, the variable could be set to the following function
382 in order to mimic default behaviour:
384 \(defun org-e-ascii-format-inlinetask-default
385 \(todo type priority name tags contents\)
386 \"Format an inline task element for ASCII export.\"
387 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
388 \(width org-e-ascii-inlinetask-width\)
389 \(org-e-ascii--indent-string
391 ;; Top line, with an additional blank line if not in UTF-8.
392 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
393 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
394 ;; Add title. Fill it if wider than inlinetask.
395 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
396 \(if \(<= \(length title\) width\) title
397 \(org-e-ascii--fill-string title width info\)\)\)
399 ;; If CONTENTS is not empty, insert it along with
401 \(when \(org-string-nw-p contents\)
402 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
404 \(make-string width \(if utf8p ?━ ?_\)\)\)
405 ;; Flush the inlinetask to the right.
406 \(- \(plist-get info :ascii-width\)
407 \(plist-get info :ascii-margin\)
408 \(plist-get info :ascii-inner-margin\)
409 \(org-e-ascii--current-text-width inlinetask info\)\)"
410 :group
'org-export-e-ascii
415 ;;; Internal Functions
417 ;; Internal functions fall into three categories.
419 ;; The first one is about text formatting. The core function is
420 ;; `org-e-ascii--current-text-width', which determines the current
421 ;; text width allowed to a given element. In other words, it helps
422 ;; keeping each line width within maximum text width defined in
423 ;; `org-e-ascii-text-width'. Once this information is known,
424 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
425 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
426 ;; operate on a given output string.
428 ;; The second category contains functions handling elements listings,
429 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
430 ;; returns a complete table of contents, `org-e-ascii--list-listings'
431 ;; returns a list of referenceable src-block elements, and
432 ;; `org-e-ascii--list-tables' does the same for table elements.
434 ;; The third category includes general helper functions.
435 ;; `org-e-ascii--build-title' creates the title for a given headline
436 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
437 ;; caption string associated to a table or a src-block.
438 ;; `org-e-ascii--describe-links' creates notes about links for
439 ;; insertion at the end of a section. It uses
440 ;; `org-e-ascii--unique-links' to get the list of links to describe.
441 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
442 ;; to internationalize output.
445 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
446 "Fill a string with specified text-width and return it.
448 S is the string being filled. TEXT-WIDTH is an integer
449 specifying maximum length of a line. INFO is the plist used as
450 a communication channel.
452 Optional argument JUSTIFY can specify any type of justification
453 among `left', `center', `right' or `full'. A nil value is
454 equivalent to `left'. For a justification that doesn't also fill
455 string, see `org-e-ascii--justify-string'.
457 Return nil if S isn't a string."
458 ;; Don't fill paragraph when break should be preserved.
459 (cond ((not (stringp s
)) nil
)
460 ((plist-get info
:preserve-breaks
) s
)
462 (let ((fill-column text-width
)
463 (use-hard-newlines t
))
465 (fill-region (point-min) (point-max) justify
))
468 (defun org-e-ascii--justify-string (s text-width how
)
470 TEXT-WIDTH is an integer specifying maximum length of a line.
471 HOW determines the type of justification: it can be `left',
472 `right', `full' or `center'."
475 (goto-char (point-min))
476 (let ((fill-column text-width
))
477 (while (< (point) (point-max))
478 (justify-current-line how
)
482 (defun org-e-ascii--indent-string (s width
)
483 "Indent string S by WIDTH white spaces.
484 Empty lines are not indented."
486 (replace-regexp-in-string
487 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
489 (defun org-e-ascii--box-string (s info
)
490 "Return string S with a partial box to its left.
491 INFO is a plist used as a communicaton channel."
492 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
493 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
494 (replace-regexp-in-string
495 "^" (if utf8p
"│ " "| ")
496 ;; Remove last newline character.
497 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
499 (defun org-e-ascii--current-text-width (element info
)
500 "Return maximum text width for ELEMENT's contents.
501 INFO is a plist used as a communication channel."
502 (case (org-element-type element
)
503 ;; Elements with an absolute width: `headline' and `inlinetask'.
504 (inlinetask org-e-ascii-inlinetask-width
)
506 (- org-e-ascii-text-width
507 (let ((low-level-rank (org-export-low-level-p element info
)))
508 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
509 ;; Elements with a relative width: store maximum text width in
512 (let* ((genealogy (cons element
(org-export-get-genealogy element info
)))
513 ;; Total width is determined by the presence, or not, of an
514 ;; inline task among ELEMENT parents.
516 (if (loop for parent in genealogy
517 thereis
(eq (org-element-type parent
) 'inlinetask
))
518 org-e-ascii-inlinetask-width
519 ;; No inlinetask: Remove global margin from text width.
520 (- org-e-ascii-text-width
521 org-e-ascii-global-margin
522 (let ((parent (org-export-get-parent-headline element info
)))
523 ;; Inner margin doesn't apply to text before first
526 (let ((low-level-rank
527 (org-export-low-level-p parent info
)))
528 ;; Inner margin doesn't apply to contents of
529 ;; low level headlines, since they've got their
530 ;; own indentation mechanism.
531 (if low-level-rank
(* low-level-rank
2)
532 org-e-ascii-inner-margin
))))))))
534 ;; Each `quote-block', `quote-section' and `verse-block' above
535 ;; narrows text width by twice the standard margin size.
536 (+ (* (loop for parent in genealogy
537 when
(memq (org-element-type parent
)
538 '(quote-block quote-section verse-block
))
540 2 org-e-ascii-quote-margin
)
541 ;; Text width within a plain-list is restricted by
542 ;; indentation of current item. If that's the case,
543 ;; compute it with the help of `:structure' property from
544 ;; parent item, if any.
546 (if (eq (org-element-type element
) 'item
) element
547 (loop for parent in genealogy
548 when
(eq (org-element-type parent
) 'item
)
550 (if (not parent-item
) 0
551 ;; Compute indentation offset of the current item,
552 ;; that is the sum of the difference between its
553 ;; indentation and the indentation of the top item in
554 ;; the list and current item bullet's length. Also
555 ;; remove tag length (for description lists) or bullet
557 (let ((struct (org-element-property :structure parent-item
))
558 (beg-item (org-element-property :begin parent-item
)))
559 (+ (- (org-list-get-ind beg-item struct
)
561 (org-list-get-top-point struct
) struct
))
563 (or (org-list-get-tag beg-item struct
)
564 (org-list-get-bullet beg-item struct
)))))))))))))
566 (defun org-e-ascii--build-title
567 (element info text-width
&optional underline notags
)
568 "Format ELEMENT title and return it.
570 ELEMENT is either an `headline' or `inlinetask' element. INFO is
571 a plist used as a communication channel. TEXT-WIDTH is an
572 integer representing the maximum length of a line.
574 When optional argument UNDERLINE is non-nil, underline title,
575 without the tags, according to `org-e-ascii-underline'
578 if optional argument NOTAGS is nil, no tags will be added to the
580 (let* ((headlinep (eq (org-element-type element
) 'headline
))
582 ;; Numbering is specific to headlines.
583 (and headlinep
(org-export-numbered-headline-p element info
)
584 ;; All tests passed: build numbering string.
588 (org-export-get-headline-number element info
) ".")
590 (text (org-export-secondary-string
591 (org-element-property :title element
) info
))
593 (and (plist-get info
:with-todo-keywords
)
594 (let ((todo (org-element-property :todo-keyword element
)))
596 (concat (org-export-secondary-string todo info
) " ")))))
597 (tags (and (not notags
)
598 (plist-get info
:with-tags
)
599 (org-element-property :tags element
)))
601 (and (plist-get info
:with-priority
)
602 (concat (org-element-property :priority element
) " ")))
603 (first-part (concat numbers todo priority text
)))
606 ;; Align tags, if any.
610 (max (- text-width
(1+ (length first-part
))) (length tags
)))
612 ;; Maybe underline text, if ELEMENT type is `headline' and an
613 ;; underline character has been defined.
614 (when (and underline headlinep
)
616 (nth (1- (org-export-get-relative-level element info
))
617 (cdr (assq (plist-get info
:ascii-charset
)
618 org-e-ascii-underline
)))))
621 (make-string (length first-part
) under-char
))))))))
623 (defun org-e-ascii--build-caption (element info
)
624 "Return caption string for ELEMENT, if applicable.
626 INFO is a plist used as a communication channel.
628 The caption string contains the sequence number of ELEMENT if it
629 has a name affiliated keyword, along with the real caption, if
630 any. Return nil when ELEMENT has no affiliated caption or name
632 (let ((caption (org-element-property :caption element
))
633 (name (org-element-property :name element
)))
634 (when (or caption name
)
635 ;; Get sequence number of current src-block among every
636 ;; src-block with either a caption or a name.
638 (org-export-get-ordinal
640 (lambda (el info
) (or (org-element-property :caption el
)
641 (org-element-property :name el
)))))
642 (title-fmt (org-e-ascii--translate
643 (case (org-element-type element
)
644 (table "Table %d: %s")
645 (src-block "Listing %d: %s")) info
)))
646 (org-e-ascii--fill-string
649 (if (not caption
) name
650 (org-export-secondary-string (car caption
) info
)))
651 (org-e-ascii--current-text-width element info
) info
)))))
653 (defun org-e-ascii--build-toc (info &optional n keyword
)
654 "Return a table of contents.
656 INFO is a plist used as a communication channel.
658 Optional argument N, when non-nil, is an integer specifying the
661 Optional argument KEYWORD specifies the TOC keyword, if any, from
662 which the table of contents generation has been initiated."
663 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
666 (make-string (1- (length title
))
667 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
670 (if keyword
(org-e-ascii--current-text-width keyword info
)
671 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
674 (let* ((level (org-export-get-relative-level headline info
))
675 (indent (* (1- level
) 3)))
677 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
678 (org-e-ascii--build-title
679 headline info
(- text-width indent
) nil
680 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
681 (org-export-collect-headlines info n
) "\n")))))
683 (defun org-e-ascii--list-listings (keyword info
)
684 "Return a list of listings.
686 KEYWORD is the keyword that initiated the list of listings
687 generation. INFO is a plist used as a communication channel."
688 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
691 (make-string (1- (length title
))
692 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
695 (if keyword
(org-e-ascii--current-text-width keyword info
)
696 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
697 ;; Use a counter instead of retreiving ordinal of each
702 ;; Store initial text so its length can be computed. This is
703 ;; used to properly align caption right to it in case of
704 ;; filling (like contents of a description list item).
706 (format (org-e-ascii--translate "Listing %d: " info
)
711 (org-e-ascii--indent-string
712 (org-e-ascii--fill-string
713 (let ((caption (org-element-property :caption src-block
)))
714 (if (not caption
) (org-element-property :name src-block
)
715 (org-export-secondary-string
716 ;; Use short name in priority, if available.
717 (or (cdr caption
) (car caption
)) info
)))
718 (- text-width
(length initial-text
)) info
)
719 (length initial-text
))))))
720 (org-export-collect-listings info
) "\n")))))
722 (defun org-e-ascii--list-tables (keyword info
)
723 "Return a list of listings.
725 KEYWORD is the keyword that initiated the list of listings
726 generation. INFO is a plist used as a communication channel."
727 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
730 (make-string (1- (length title
))
731 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
734 (if keyword
(org-e-ascii--current-text-width keyword info
)
735 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
736 ;; Use a counter instead of retreiving ordinal of each
741 ;; Store initial text so its length can be computed. This is
742 ;; used to properly align caption right to it in case of
743 ;; filling (like contents of a description list item).
745 (format (org-e-ascii--translate "Table %d: " info
)
750 (org-e-ascii--indent-string
751 (org-e-ascii--fill-string
752 (let ((caption (org-element-property :caption table
)))
753 (if (not caption
) (org-element-property :name table
)
754 ;; Use short name in priority, if available.
755 (org-export-secondary-string
756 (or (cdr caption
) (car caption
)) info
)))
757 (- text-width
(length initial-text
)) info
)
758 (length initial-text
))))))
759 (org-export-collect-tables info
) "\n")))))
761 (defun org-e-ascii--unique-links (element info
)
762 "Return a list of unique link references in ELEMENT.
764 ELEMENT is either an headline element or a section element. INFO
765 is a plist used as a communication channel.
767 It covers links that may be found current headline's title, in
768 the following section and in any inlinetask's title there."
772 ;; Return LINK if it wasn't referenced so far, or nil.
773 ;; Update SEEN links along the way.
776 (cons (org-element-property :raw-link link
)
777 (org-element-contents link
))))
778 (unless (member footprint seen
)
779 (push footprint seen
) link
)))))
780 (harvest-links-in-title
782 ;; Return a list of all unique links in ELEMENT. ELEMENT
783 ;; may be an headline or an inlinetask element.
786 (dolist (obj (org-element-property :title element
) acc
)
787 (when (eq (org-element-type obj
) 'link
)
788 (let ((link (funcall unique-link-p obj
)))
789 (and link
(push link acc
)))))))))
790 ;; Retrieve HEADLINE's section, if it exists.
791 (section (if (eq (org-element-type element
) 'section
) element
792 (let ((sec (car (org-element-contents element
))))
793 (and (eq (org-element-type sec
) 'section
) sec
))))
794 (headline (if (eq (org-element-type element
) 'headline
) element
795 (org-export-get-parent-headline element info
))))
797 ;; Links that may be in HEADLINE's title.
798 (funcall harvest-links-in-title headline
)
799 ;; Get all links in SECTION.
801 section
'link
(lambda (link) (funcall unique-link-p link
)) info
))))
803 (defun org-e-ascii--describe-links (links width info
)
804 "Return a string describing a list of links.
806 LINKS is a list of link type objects, as returned by
807 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
808 the output string. INFO is a plist used as a communication
812 (let ((type (org-element-property :type link
))
813 (anchor (let ((desc (org-element-contents link
)))
814 (if (not desc
) (org-element-property :raw-link link
)
815 (org-export-secondary-string desc info
)))))
817 ;; Coderefs, radio links and fuzzy links are ignored.
818 ((member type
'("coderef" "radio" "fuzzy")) nil
)
819 ;; Id and custom-id links: Headlines refer to their numbering.
820 ((member type
'("custom-id" "id"))
821 (let ((dest (org-export-resolve-id-link link info
)))
823 (org-e-ascii--fill-string
827 (if (not dest
) (org-e-ascii--translate "Unknown reference" info
)
829 (org-e-ascii--translate "See section %s" info
)
830 (mapconcat 'number-to-string
831 (org-export-get-headline-number dest info
) "."))))
832 width info
) "\n\n")))
833 ;; Do not add a link that cannot be resolved and doesn't have
834 ;; any description: destination is already visible in the
836 ((not (org-element-contents link
)) nil
)
839 (org-e-ascii--fill-string
840 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
849 (defun org-e-ascii-template--document-title (info)
850 "Return document title, as a string.
851 INFO is a plist used as a communication channel."
852 (let ((text-width org-e-ascii-text-width
)
853 (title (org-export-secondary-string (plist-get info
:title
) info
))
854 (author (and (plist-get info
:with-author
)
855 (let ((auth (plist-get info
:author
)))
856 (and auth
(org-export-secondary-string auth info
)))))
857 (email (and (plist-get info
:with-email
)
858 (org-export-secondary-string (plist-get info
:email
) info
)))
859 (date (plist-get info
:date
)))
860 ;; There are two types of title blocks depending on the presence
861 ;; of a title to display.
862 (if (string= title
"")
863 ;; Title block without a title. DATE is positioned at the top
864 ;; right of the document, AUTHOR to the top left and EMAIL
867 ((and (org-string-nw-p date
) (org-string-nw-p author
))
870 (make-string (- text-width
(length date
) (length author
)) ?
)
872 (when (org-string-nw-p email
) (concat "\n" email
))
874 ((and (org-string-nw-p date
) (org-string-nw-p email
))
877 (make-string (- text-width
(length date
) (length email
)) ?
)
879 ((org-string-nw-p date
)
881 (org-e-ascii--justify-string date text-width
'right
)
883 ((and (org-string-nw-p author
) (org-string-nw-p email
))
884 (concat author
"\n" email
"\n\n\n"))
885 ((org-string-nw-p author
) (concat author
"\n\n\n"))
886 ((org-string-nw-p email
) (concat email
"\n\n\n")))
887 ;; Title block with a title. Document's TITLE, along with the
888 ;; AUTHOR and its EMAIL are both overlined and an underlined,
889 ;; centered. Date is just below, also centered.
890 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
891 ;; Format TITLE. It may be filled if it is too wide,
892 ;; that is wider than the two thirds of the total width.
893 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
894 (formatted-title (org-e-ascii--fill-string title title-len info
))
897 (min (+ (max title-len
(length author
) (length email
)) 2)
898 text-width
) (if utf8p ?━ ?_
))))
899 (org-e-ascii--justify-string
902 (upcase formatted-title
)
904 ((and (org-string-nw-p author
) (org-string-nw-p email
))
905 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
906 ((org-string-nw-p author
)
907 (concat (if utf8p
"\n\n\n" "\n\n") author
))
908 ((org-string-nw-p email
)
909 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
911 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
912 "\n\n\n") text-width
'center
)))))
914 (defun org-e-ascii-template (contents info
)
915 "Return complete document string after ASCII conversion.
916 CONTENTS is the transcoded contents string. INFO is a plist
917 holding export options."
918 (org-element-normalize-string
919 (org-e-ascii--indent-string
920 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
921 ;; 1. Build title block.
923 (org-e-ascii-template--document-title info
)
924 ;; 2. Table of contents.
925 (let ((depth (plist-get info
:with-toc
)))
928 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
930 ;; 3. Document's body.
932 ;; 4. Footnote definitions.
933 (let ((definitions (org-export-collect-footnote-definitions
934 (plist-get info
:parse-tree
) info
))
935 ;; Insert full links right inside the footnote definition
936 ;; as they have no chance to be inserted later.
937 (org-e-ascii-links-to-notes nil
))
941 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
946 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
950 (let ((id (format "[%s] " (car ref
))))
951 ;; Distinguish between inline definitions and
952 ;; full-fledged definitions.
954 (let ((def (nth 2 ref
)))
955 (if (eq (org-element-type def
) 'org-data
)
956 ;; Full-fledged definition: footnote ID is
957 ;; inserted inside the first parsed paragraph
958 ;; (FIRST), if any, to be sure filling will
959 ;; take it into consideration.
960 (let ((first (car (org-element-contents def
))))
961 (if (not (eq (org-element-type first
) 'paragraph
))
962 (concat id
"\n" (org-export-data def info
))
963 (push id
(nthcdr 2 first
))
964 (org-export-data def info
)))
965 ;; Fill paragraph once footnote ID is inserted in
966 ;; order to have a correct length for first line.
967 (org-e-ascii--fill-string
968 (concat id
(org-export-secondary-string def info
))
969 text-width info
))))))
970 definitions
"\n\n"))))
971 ;; 5. Creator. Ignore `comment' value as there are no comments in
972 ;; ASCII. Justify it to the bottom right.
973 (let ((creator-info (plist-get info
:with-creator
)))
974 (unless (or (not creator-info
) (eq creator-info
'comment
))
977 (org-e-ascii--fill-string
978 (plist-get info
:creator
) text-width info
'right
))))))
979 org-e-ascii-global-margin
)))
981 (defun org-e-ascii--translate (s info
)
984 INFO is a plist used as a communication channel.
986 Translation depends on `:language' property and allowed charset.
987 If no translation in found for a given language and a given
988 charset, fall-back to S."
989 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
990 (lang (plist-get info
:language
))
991 (translations (cdr (assoc s org-e-ascii-dictionary
))))
992 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
996 ;;; Transcode Functions
1000 ;; Babel Calls are ignored.
1005 (defun org-e-ascii-center-block (center-block contents info
)
1006 "Transcode a CENTER-BLOCK element from Org to ASCII.
1007 CONTENTS holds the contents of the block. INFO is a plist
1008 holding contextual information."
1009 (org-e-ascii--justify-string
1010 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1015 ;; Comments are ignored.
1020 ;; Comment Blocks are ignored.
1025 (defun org-e-ascii-drawer (drawer contents info
)
1026 "Transcode a DRAWER element from Org to ASCII.
1027 CONTENTS holds the contents of the block. INFO is a plist
1028 holding contextual information."
1029 (let ((name (org-element-property :drawer-name drawer
))
1030 (width (org-e-ascii--current-text-width drawer info
)))
1031 (if (functionp org-e-ascii-format-drawer-function
)
1032 (funcall org-e-ascii-format-drawer-function name contents width
)
1033 ;; If there's no user defined function: simply
1034 ;; display contents of the drawer.
1040 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1041 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1042 CONTENTS holds the contents of the block. INFO is a plist
1043 holding contextual information. See `org-export-data'."
1049 (defun org-e-ascii-emphasis (emphasis contents info
)
1050 "Transcode EMPHASIS from Org to ASCII.
1051 CONTENTS is the contents of the emphasized text. INFO is a plist
1052 holding contextual information.."
1053 (let ((marker (org-element-property :marker emphasis
)))
1054 ;; Leave emphasis markers as-is.
1055 (concat marker contents marker
)))
1060 (defun org-e-ascii-entity (entity contents info
)
1061 "Transcode an ENTITY object from Org to ASCII.
1062 CONTENTS are the definition itself. INFO is a plist holding
1063 contextual information."
1064 (org-element-property
1065 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1071 (defun org-e-ascii-example-block (example-block contents info
)
1072 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1073 CONTENTS is nil. INFO is a plist holding contextual information."
1074 (org-e-ascii--box-string
1075 (org-export-format-code-default example-block info
) info
))
1080 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1081 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1082 CONTENTS is nil. INFO is a plist holding contextual information."
1083 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1084 (org-element-property :value export-snippet
)))
1089 (defun org-e-ascii-export-block (export-block contents info
)
1090 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1091 CONTENTS is nil. INFO is a plist holding contextual information."
1092 (when (string= (org-element-property :type export-block
) "ascii")
1093 (org-remove-indentation (org-element-property :value export-block
))))
1098 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1099 "Transcode a FIXED-WIDTH element from Org to ASCII.
1100 CONTENTS is nil. INFO is a plist holding contextual information."
1101 (org-e-ascii--box-string
1102 (replace-regexp-in-string
1103 "^[ \t]*: ?" "" (org-element-property :value fixed-width
)) info
))
1106 ;;;; Footnote Definition
1108 ;; Footnote Definitions are ignored. They are compiled at the end of
1109 ;; the document, by `org-e-ascii-template'.
1112 ;;;; Footnote Reference
1114 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1115 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1116 CONTENTS is nil. INFO is a plist holding contextual information."
1117 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1122 (defun org-e-ascii-headline (headline contents info
)
1123 "Transcode an HEADLINE element from Org to ASCII.
1124 CONTENTS holds the contents of the headline. INFO is a plist
1125 holding contextual information."
1126 ;; Don't export footnote section, which will be handled at the end
1128 (unless (org-element-property :footnote-section-p headline
)
1129 (let* ((low-level-rank (org-export-low-level-p headline info
))
1130 (width (org-e-ascii--current-text-width headline info
))
1131 ;; Blank lines between headline and its contents.
1132 ;; `org-e-ascii-headline-spacing', when set, overwrites
1133 ;; original buffer's spacing.
1136 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1137 (org-element-property :pre-blank headline
)) ?
\n))
1138 ;; Even if HEADLINE has no section, there might be some
1139 ;; links in its title that we shouldn't forget to describe.
1141 (unless (eq (caar (org-element-contents headline
)) 'section
)
1142 (org-e-ascii--describe-links
1143 (org-e-ascii--unique-links headline info
) width info
))))
1144 ;; Deep subtree: export it as a list item.
1148 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1149 org-e-ascii-bullets
))))
1151 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1154 (org-e-ascii--build-title headline info width
) "\n"
1155 ;; Contents, indented by length of bullet.
1157 (org-e-ascii--indent-string
1159 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1161 ;; Else: Standard headline.
1163 (org-e-ascii--build-title headline info width
'underline
)
1165 (concat (when (org-string-nw-p links
) links
) contents
))))))
1168 ;;;; Horizontal Rule
1170 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1171 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1172 CONTENTS is nil. INFO is a plist holding contextual
1180 (org-element-property :attr_ascii horizontal-rule
)
1182 (make-string (or (and (wholenump (plist-get attr
:width
))
1183 (plist-get attr
:width
))
1184 (org-e-ascii--current-text-width horizontal-rule info
))
1185 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1188 ;;;; Inline Babel Call
1190 ;; Inline Babel Calls are ignored.
1193 ;;;; Inline Src Block
1195 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1196 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1197 CONTENTS holds the contents of the item. INFO is a plist holding
1198 contextual information."
1199 (format org-e-ascii-verbatim-format
1200 (org-element-property :value inline-src-block
)))
1205 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1206 "Transcode an INLINETASK element from Org to ASCII.
1207 CONTENTS holds the contents of the block. INFO is a plist
1208 holding contextual information."
1209 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1210 (title (org-export-secondary-string
1211 (org-element-property :title inlinetask
) info
))
1212 (todo (and (plist-get info
:with-todo-keywords
)
1213 (let ((todo (org-element-property
1214 :todo-keyword inlinetask
)))
1216 (org-export-secondary-string todo info
)))))
1217 (todo-type (org-element-property :todo-type inlinetask
))
1218 (tags (and (plist-get info
:with-tags
)
1219 (org-element-property :tags inlinetask
)))
1220 (priority (and (plist-get info
:with-priority
)
1221 (org-element-property :priority inlinetask
))))
1222 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1223 ;; with appropriate arguments.
1224 (if (functionp org-e-ascii-format-inlinetask-function
)
1225 (funcall org-e-ascii-format-inlinetask-function
1226 todo todo-type priority title tags contents width
)
1227 ;; Otherwise, use a default template.
1228 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1229 (org-e-ascii--indent-string
1231 ;; Top line, with an additional blank line if not in UTF-8.
1232 (make-string width
(if utf8p ?━ ?_
)) "\n"
1233 (unless utf8p
(concat (make-string width ?
) "\n"))
1234 ;; Add title. Fill it if wider than inlinetask.
1235 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1236 (if (<= (length title
) width
) title
1237 (org-e-ascii--fill-string title width info
)))
1239 ;; If CONTENTS is not empty, insert it along with
1241 (when (org-string-nw-p contents
)
1242 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1244 (make-string width
(if utf8p ?━ ?_
)))
1245 ;; Flush the inlinetask to the right.
1246 (- org-e-ascii-text-width org-e-ascii-global-margin
1247 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1248 org-e-ascii-inner-margin
)
1249 (org-e-ascii--current-text-width inlinetask info
)))))))
1254 (defun org-e-ascii-item (item contents info
)
1255 "Transcode an ITEM element from Org to ASCII.
1256 CONTENTS holds the contents of the item. INFO is a plist holding
1257 contextual information."
1259 ;; First parent of ITEM is always the plain-list. Get
1260 ;; `:type' property from it.
1261 (org-list-bullet-string
1262 (case (org-element-property :type
(org-export-get-parent item info
))
1265 (org-export-secondary-string
1266 (org-element-property :tag item
) info
)
1269 ;; Return correct number for ITEM, paying attention to
1271 (let* ((struct (org-element-property :structure item
))
1272 (bul (org-element-property :bullet item
))
1275 (car (last (org-list-get-item-number
1276 (org-element-property :begin item
)
1278 (org-list-prevs-alist struct
)
1279 (org-list-parents-alist struct
)))))))
1280 (replace-regexp-in-string "[0-9]+" num bul
)))
1281 (t (let ((bul (org-element-property :bullet item
)))
1282 ;; Change bullets into more visible form if UTF-8 is active.
1283 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bul
1284 (replace-regexp-in-string
1286 (replace-regexp-in-string
1288 (replace-regexp-in-string "*" "‣" bul
))))))))))
1291 ;; Contents: Pay attention to indentation. Note: check-boxes are
1292 ;; already taken care of at the paragraph level so they don't
1293 ;; interfere with indentation.
1294 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1295 (if (eq (caar (org-element-contents item
)) 'paragraph
)
1297 (concat "\n" contents
))))))
1302 (defun org-e-ascii-keyword (keyword contents info
)
1303 "Transcode a KEYWORD element from Org to ASCII.
1304 CONTENTS is nil. INFO is a plist holding contextual
1306 (let ((key (org-element-property :key keyword
))
1307 (value (org-element-property :value keyword
)))
1309 ((string= key
"ASCII") value
)
1310 ((string= key
"TOC")
1311 (let ((value (downcase value
)))
1313 ((string-match "\\<headlines\\>" value
)
1314 (let ((depth (or (and (string-match "[0-9]+" value
)
1315 (string-to-number (match-string 0 value
)))
1316 (plist-get info
:with-toc
))))
1317 (org-e-ascii--build-toc
1318 info
(and (wholenump depth
) depth
) keyword
)))
1319 ((string= "tables" value
)
1320 (org-e-ascii--list-tables keyword info
))
1321 ((string= "listings" value
)
1322 (org-e-ascii--list-listings keyword info
))))))))
1325 ;;;; Latex Environment
1327 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1328 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1329 CONTENTS is nil. INFO is a plist holding contextual
1331 (org-remove-indentation (org-element-property :value latex-environment
)))
1336 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1337 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1338 CONTENTS is nil. INFO is a plist holding contextual
1340 (org-element-property :value latex-fragment
))
1345 (defun org-e-ascii-line-break (line-break contents info
)
1346 "Transcode a LINE-BREAK object from Org to ASCII.
1347 CONTENTS is nil. INFO is a plist holding contextual
1348 information." hard-newline
)
1353 (defun org-e-ascii-link (link desc info
)
1354 "Transcode a LINK object from Org to ASCII.
1356 DESC is the description part of the link, or the empty string.
1357 INFO is a plist holding contextual information."
1358 (let ((raw-link (org-element-property :raw-link link
))
1359 (type (org-element-property :type link
)))
1361 ((string= type
"coderef")
1362 (let ((ref (org-element-property :path link
)))
1363 (format (org-export-get-coderef-format ref desc
)
1364 (org-export-resolve-coderef ref info
))))
1365 ;; Do not apply a special syntax on radio links. Though, parse
1366 ;; and transcode path to have a proper display of contents.
1367 ((string= type
"radio")
1368 (org-export-secondary-string
1369 (org-element-parse-secondary-string
1370 (org-element-property :path link
)
1371 (cdr (assq 'radio-target org-element-object-restrictions
)))
1373 ;; Do not apply a special syntax on fuzzy links pointing to
1375 ((string= type
"fuzzy")
1376 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1377 ;; Ignore invisible "#+target: path".
1378 (unless (eq (org-element-type destination
) 'keyword
)
1379 (if (org-string-nw-p desc
) desc
1381 (let ((number (org-export-get-ordinal destination info
)))
1383 (if (atom number
) (number-to-string number
)
1384 (mapconcat 'number-to-string number
".")))))))))
1386 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1388 (format "[%s]" desc
)
1389 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1394 (defun org-e-ascii-macro (macro contents info
)
1395 "Transcode a MACRO element from Org to ASCII.
1396 CONTENTS is nil. INFO is a plist holding contextual
1398 (org-export-expand-macro macro info
))
1403 (defun org-e-ascii-paragraph (paragraph contents info
)
1404 "Transcode a PARAGRAPH element from Org to ASCII.
1405 CONTENTS is the contents of the paragraph, as a string. INFO is
1406 the plist used as a communication channel."
1407 (org-e-ascii--fill-string
1408 (let ((parent (org-export-get-parent paragraph info
)))
1409 ;; If PARAGRAPH is the first one in a list element, be sure to
1410 ;; add the check-box in front of it, before any filling. Later,
1411 ;; it would interfere with line width.
1412 (if (and (eq (org-element-type parent
) 'item
)
1413 (equal (car (org-element-contents parent
)) paragraph
))
1414 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1415 (concat (case (org-element-property :checkbox parent
)
1416 (on (if utf8p
"☑ " "[X] "))
1417 (off (if utf8p
"☐ " "[ ] "))
1418 (trans (if utf8p
"☒ " "[-] ")))
1421 (org-e-ascii--current-text-width paragraph info
) info
))
1426 (defun org-e-ascii-plain-list (plain-list contents info
)
1427 "Transcode a PLAIN-LIST element from Org to ASCII.
1428 CONTENTS is the contents of the list. INFO is a plist holding
1429 contextual information."
1435 (defun org-e-ascii-plain-text (text info
)
1436 "Transcode a TEXT string from Org to ASCII.
1437 INFO is a plist used as a communication channel."
1438 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1439 (plist-get info
:with-special-strings
)))
1441 ;; Usual replacements in utf-8 with proper option set.
1442 (replace-regexp-in-string
1444 (replace-regexp-in-string
1446 (replace-regexp-in-string "---" "—" text
)))))
1449 ;;;; Property Drawer
1451 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1452 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1453 CONTENTS is nil. INFO is a plist used as a communication
1455 ;; The property drawer isn't exported but we want separating blank
1456 ;; lines nonetheless.
1462 (defun org-e-ascii-quote-block (quote-block contents info
)
1463 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1464 CONTENTS holds the contents of the block. INFO is a plist
1465 holding contextual information."
1466 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1467 (org-e-ascii--indent-string
1468 (org-remove-indentation
1469 (org-e-ascii--fill-string contents width info
))
1470 org-e-ascii-quote-margin
)))
1475 (defun org-e-ascii-quote-section (quote-section contents info
)
1476 "Transcode a QUOTE-SECTION element from Org to ASCII.
1477 CONTENTS is nil. INFO is a plist holding contextual information."
1478 (let ((width (org-e-ascii--current-text-width quote-section info
))
1480 (org-export-secondary-string
1481 (org-remove-indentation
1482 (org-element-property :value quote-section
))
1484 (org-e-ascii--indent-string
1486 (+ org-e-ascii-quote-margin
1487 ;; Don't apply inner margin if parent headline is low level.
1488 (let ((headline (org-export-get-parent-headline quote-section info
)))
1489 (if (org-export-low-level-p headline info
) 0
1490 org-e-ascii-inner-margin
))))))
1495 (defun org-e-ascii-radio-target (radio-target contents info
)
1496 "Transcode a RADIO-TARGET object from Org to ASCII.
1497 CONTENTS is the contents of the target. INFO is a plist holding
1498 contextual information."
1503 (defun org-e-ascii-section (section contents info
)
1504 "Transcode a SECTION element from Org to ASCII.
1505 CONTENTS is the contents of the section. INFO is a plist holding
1506 contextual information."
1507 (org-e-ascii--indent-string
1510 (when org-e-ascii-links-to-notes
1511 ;; Add list of links at the end of SECTION.
1512 (let ((links (org-e-ascii--describe-links
1513 (org-e-ascii--unique-links section info
)
1514 (org-e-ascii--current-text-width section info
) info
)))
1515 ;; Separate list of links and section contents.
1516 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1517 ;; Do not apply inner margin if parent headline is low level.
1518 (let ((headline (org-export-get-parent-headline section info
)))
1519 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1520 org-e-ascii-inner-margin
))))
1525 (defun org-e-ascii-special-block (special-block contents info
)
1526 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1527 CONTENTS holds the contents of the block. INFO is a plist
1528 holding contextual information."
1534 (defun org-e-ascii-src-block (src-block contents info
)
1535 "Transcode a SRC-BLOCK element from Org to ASCII.
1536 CONTENTS holds the contents of the item. INFO is a plist holding
1537 contextual information."
1538 (let ((caption (org-e-ascii--build-caption src-block info
)))
1540 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1541 (org-e-ascii--box-string
1542 (org-export-format-code-default src-block info
) info
)
1543 (when (and caption
(not org-e-ascii-caption-above
))
1544 (concat "\n" caption
)))))
1546 ;;;; Statistics Cookie
1548 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1549 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1550 CONTENTS is nil. INFO is a plist holding contextual information."
1551 (org-element-property :value statistics-cookie
))
1556 (defun org-e-ascii-subscript (subscript contents info
)
1557 "Transcode a SUBSCRIPT object from Org to ASCII.
1558 CONTENTS is the contents of the object. INFO is a plist holding
1559 contextual information."
1560 (if (org-element-property :use-brackets-p subscript
)
1561 (format "_{%s}" contents
)
1562 (format "_%s" contents
)))
1567 (defun org-e-ascii-superscript (superscript contents info
)
1568 "Transcode a SUPERSCRIPT object from Org to ASCII.
1569 CONTENTS is the contents of the object. INFO is a plist holding
1570 contextual information."
1571 (if (org-element-property :use-brackets-p superscript
)
1572 (format "_{%s}" contents
)
1573 (format "_%s" contents
)))
1578 (defun org-e-ascii-table (table contents info
)
1579 "Transcode a TABLE element from Org to ASCII.
1580 CONTENTS is nil. INFO is a plist holding contextual information."
1581 (let ((caption (org-e-ascii--build-caption table info
)))
1583 ;; Possibly add a caption string above.
1584 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1585 ;; Insert table. Note: "table.el" tables are left unmodified.
1586 (if (eq (org-element-property :type table
) 'org
) contents
1587 (org-remove-indentation (org-element-property :value table
)))
1588 ;; Possible add a caption string below.
1589 (when (and caption
(not org-e-ascii-caption-above
))
1590 (concat "\n" caption
)))))
1596 (defun org-e-ascii--table-cell-width (table-cell info
)
1597 "Return width of TABLE-CELL.
1599 Width of a cell is determined either by a width cookie in the
1600 same column as the cell, or by the length of its contents.
1602 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1604 (or (and (not org-e-ascii-table-widen-columns
)
1605 (org-export-table-cell-width table-cell info
))
1606 (let* ((max-width 0)
1607 (table (org-export-get-parent-table table-cell info
))
1608 (specialp (org-export-table-has-special-column-p table
))
1609 (col (cdr (org-export-table-cell-address table-cell info
))))
1616 (elt (if specialp
(car (org-element-contents row
))
1617 (org-element-contents row
))
1623 (defun org-e-ascii-table-cell (table-cell contents info
)
1624 "Transcode a TABLE-CELL object from Org to ASCII.
1625 CONTENTS is the cell contents. INFO is a plist used as
1626 a communication channel."
1627 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1628 ;; is nil and some width cookie has set it, use that value.
1629 ;; Otherwise, compute the maximum width among transcoded data of
1630 ;; each cell in the column.
1631 (let ((width (org-e-ascii--table-cell-width table-cell info
)))
1632 ;; When contents are too large, truncate them.
1633 (unless (or org-e-ascii-table-widen-columns
(<= (length contents
) width
))
1634 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1635 ;; Align contents correctly within the cell.
1636 (let* ((indent-tabs-mode nil
)
1639 (org-e-ascii--justify-string
1641 (org-export-table-cell-alignment table-cell info
)))))
1642 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1644 (concat (format " %s " contents
)
1645 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1646 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1651 (defun org-e-ascii-table-row (table-row contents info
)
1652 "Transcode a TABLE-ROW element from Org to ASCII.
1653 CONTENTS is the row contents. INFO is a plist used as
1654 a communication channel."
1655 (when (eq (org-element-property :type table-row
) 'standard
)
1658 (lambda (lcorner horiz vert rcorner
)
1663 table-row
'table-cell
1665 (let ((width (org-e-ascii--table-cell-width cell info
))
1666 (borders (org-export-table-cell-borders cell info
)))
1668 (when (and (memq 'left borders
)
1669 (equal (org-element-map
1670 table-row
'table-cell
'identity info t
)
1672 (make-string (+ 2 width
) (string-to-char horiz
))
1674 ((not (memq 'right borders
)) nil
)
1675 ((equal (car (last (org-element-contents table-row
)))
1680 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1681 (borders (org-export-table-cell-borders
1682 (org-element-map table-row
'table-cell
'identity info t
)
1685 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1686 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1687 (funcall build-hline
"+" "-" "+" "+")))
1688 ((memq 'above borders
)
1689 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1690 (funcall build-hline
"+" "-" "+" "+"))))
1691 (when (memq 'left borders
) (if utf8p
"│" "|"))
1693 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1694 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1695 (funcall build-hline
"+" "-" "+" "+")))))))
1700 ;; Targets are invisible.
1705 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1706 "Transcode a TIME-STAMP object from Org to ASCII.
1707 CONTENTS is nil. INFO is a plist holding contextual information."
1708 ;; Return time-stamps as-is.
1709 (org-element-time-stamp-interpreter time-stamp contents
))
1714 (defun org-e-ascii-verbatim (verbatim contents info
)
1715 "Return a VERBATIM object from Org to ASCII.
1716 CONTENTS is nil. INFO is a plist holding contextual information."
1717 (format org-e-ascii-verbatim-format
1718 (org-element-property :value verbatim
)))
1723 (defun org-e-ascii-verse-block (verse-block contents info
)
1724 "Transcode a VERSE-BLOCK element from Org to ASCII.
1725 CONTENTS is verse block contents. INFO is a plist holding
1726 contextual information."
1727 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1728 (org-e-ascii--indent-string
1729 (org-e-ascii--justify-string contents verse-width
'left
)
1730 org-e-ascii-quote-margin
)))
1735 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1736 "Filter controlling number of blank lines after an headline.
1738 HEADLINE is a string representing a transcoded headline.
1739 BACK-END is symbol specifying back-end used for export. INFO is
1740 plist containing the communication channel.
1742 This function only applies to `e-ascii' back-end. See
1743 `org-e-ascii-headline-spacing' for information.
1745 For any other back-end, HEADLINE is returned as-is."
1746 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1747 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1748 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1752 ;;; Interactive function
1754 (defun org-e-ascii-export-to-ascii
1755 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1756 "Export current buffer to a text file.
1758 If narrowing is active in the current buffer, only export its
1761 If a region is active, export that region.
1763 When optional argument SUBTREEP is non-nil, export the sub-tree
1764 at point, extracting information from the headline properties
1767 When optional argument VISIBLE-ONLY is non-nil, don't export
1768 contents of hidden elements.
1770 When optional argument BODY-ONLY is non-nil, strip title, table
1771 of contents and footnote definitions from output.
1773 EXT-PLIST, when provided, is a property list with external
1774 parameters overriding Org default settings, but still inferior to
1775 file-local settings.
1777 When optional argument PUB-DIR is set, use it as the publishing
1780 Return output file's name."
1782 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1784 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1787 (provide 'org-e-ascii
)
1788 ;;; org-e-ascii.el ends here