1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements an ASCII back-end for Org generic exporter.
27 ;; M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
29 ;; in an Org mode buffer then switch to that buffer to see the ASCII
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
35 (eval-when-compile (require 'cl
))
37 (declare-function org-element-get-contents
"org-element" (element))
38 (declare-function org-element-get-property
"org-element" (property element
))
39 (declare-function org-element-map
"org-element"
40 (data types fun
&optional info first-match
))
41 (declare-function org-element-time-stamp-interpreter
42 "org-element" (time-stamp contents
))
44 (declare-function org-export-clean-table
"org-export" (table specialp
))
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 backend info
))
51 (declare-function org-export-expand-macro
"org-export" (macro info
))
52 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
53 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
54 (declare-function org-export-get-headline-number
"org-export" (headline info
))
55 (declare-function org-export-get-ordinal
"org-export"
56 (element info
&optional types within-section predicate
))
57 (declare-function org-export-get-parent-headline
"org-export" (blob info
))
58 (declare-function org-export-get-relative-level
"org-export" (headline info
))
59 (declare-function org-export-handle-code
60 "org-export" (element info
&optional num-fmt ref-fmt delayed
))
61 (declare-function org-export-included-file
"org-export" (keyword backend info
))
62 (declare-function org-export-low-level-p
"org-export" (headline info
))
63 (declare-function org-export-output-file-name
"org-export"
64 (extension &optional subtreep pub-dir
))
65 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
66 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
67 (declare-function org-export-resolve-id-link
"org-export" (link info
))
68 (declare-function org-export-secondary-string
69 "org-export" (secondary backend info
))
70 (declare-function org-export-table-format-info
"org-export" (table))
72 org-export-to-file
"org-export"
73 (backend file
&optional subtreep visible-only body-only ext-plist
))
77 ;;; Internal Variables
79 ;; The following setting won't allow to modify preferred charset
80 ;; through a buffer keyword or an option item, but, since the property
81 ;; will appear in communication channel nonetheless, it allows to
82 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
85 ;; We also install a developer filter for headlines, in order to
86 ;; control blank lines in output string.
88 (defconst org-e-ascii-option-alist
89 '((:ascii-charset nil nil org-e-ascii-charset
)
90 (:filter-headline nil nil
(cons
91 'org-e-ascii-filter-headline-blank-lines
92 org-export-filter-headline-functions
)))
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-dictionary
98 '(("Table Of Contents\n"
100 :ascii
"Table Of Contents\n"
101 :latin1
"Table Of Contents\n"
102 :utf-8
"Table Of Contents\n")
105 :latin1
"Table des matières\n"
106 :utf-8
"Table des matières\n"))
109 :ascii
"Table %d: %s"
110 :latin1
"Table %d: %s"
111 :utf-8
"Table %d: %s")
113 :ascii
"Tableau %d : %s"
114 :latin1
"Tableau %d : %s"
115 :utf-8
"Tableau nº %d : %s"))
118 :ascii
"Listing %d: %s"
119 :latin1
"Listing %d: %s"
120 :utf-8
"Listing %d: %s")
122 :ascii
"Programme %d : %s"
123 :latin1
"Programme %d : %s"
124 :utf-8
"Programme nº %d : %s"))
125 ("List Of Listings\n"
127 :ascii
"List Of Listings\n"
128 :latin1
"List Of Listings\n"
129 :utf-8
"List Of Listings\n")
131 :ascii
"Liste des programmes\n"
132 :latin1
"Liste des programmes\n"
133 :utf-8
"Liste des programmes\n"))
136 :ascii
"List Of Tables\n"
137 :latin1
"List Of Tables\n"
138 :utf-8
"List Of Tables\n")
140 :ascii
"Liste des tableaux\n"
141 :latin1
"Liste des tableaux\n"
142 :utf-8
"Liste des tableaux\n"))
145 :ascii
"Listing %d: "
146 :latin1
"Listing %d: "
147 :utf-8
"Listing %d: ")
149 :ascii
"Programme %d : "
150 :latin1
"Programme %d : "
151 :utf-8
"Programme nº %d : "))
158 :ascii
"Tableau %d : "
159 :latin1
"Tableau %d : "
160 :utf-8
"Tableau nº %d : "))
163 :ascii
"Unknown reference"
164 :latin1
"Unknown reference"
165 :utf-8
"Unknown reference")
167 :ascii
"Destination inconnue"
168 :latin1
"Référence inconnue"
169 :utf-8
"Référence inconnue"))
172 :ascii
"See section %s"
173 :latin1
"See section %s"
174 :utf-8
"See section %s")
176 :ascii
"cf. section %s"
177 :latin1
"cf. section %s"
178 :utf-8
"cf. section %s")))
179 "Dictionary for ASCII back-end.
181 Alist whose car is the string to translate and cdr is an alist
182 whose car is the language string and cdr is a plist whose
183 properties are possible charsets and value the translated term.
185 It is used as a database for `org-e-ascii--translate'.")
189 ;;; User Configurable Variables
191 (defgroup org-export-e-ascii nil
192 "Options for exporting Org mode files to ASCII."
193 :tag
"Org Export ASCII"
196 (defcustom org-e-ascii-text-width
72
197 "Maximum width of exported text.
198 This number includes margin size, as set in
199 `org-e-ascii-global-margin'."
200 :group
'org-export-e-ascii
203 (defcustom org-e-ascii-global-margin
0
204 "Width of the left margin, in number of characters."
205 :group
'org-export-e-ascii
208 (defcustom org-e-ascii-inner-margin
2
209 "Width of the inner margin, in number of characters.
210 Inner margin is applied between each headline."
211 :group
'org-export-e-ascii
214 (defcustom org-e-ascii-inlinetask-width
30
215 "Width of inline tasks, in number of characters.
216 This number ignores any margin."
217 :group
'org-export-e-ascii
220 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
221 "Number of blank lines inserted around headlines.
223 This variable can be set to a cons cell. In that case, its car
224 represents the number of blank lines present before headline
225 contents whereas its cdr reflects the number of blank lines after
228 A nil value replicates the number of blank lines found in the
229 original Org buffer at the same place."
230 :group
'org-export-e-ascii
232 (const :tag
"Replicate original spacing" nil
)
233 (cons :tag
"Set an uniform spacing"
234 (integer :tag
"Number of blank lines before contents")
235 (integer :tag
"Number of blank lines after contents"))))
237 (defcustom org-e-ascii-charset
'ascii
238 "The charset allowed to represent various elements and objects.
240 `ascii' Only use plain ASCII characters
241 `latin1' Include Latin-1 characters
242 `utf-8' Use all UTF-8 characters"
243 :group
'org-export-e-ascii
245 (const :tag
"ASCII" ascii
)
246 (const :tag
"Latin-1" latin1
)
247 (const :tag
"UTF-8" utf-8
)))
249 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
251 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
252 "Characters for underlining headings in ASCII export.
254 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
255 and whose value is a list of characters.
257 For each supported charset, this variable associates a sequence
258 of underline characters. In a sequence, the characters will be
259 used in order for headlines level 1, 2, ... If no character is
260 available for a given level, the headline won't be underlined."
261 :group
'org-export-e-ascii
263 (cons :tag
"Underline characters sequence"
264 (const :tag
"ASCII charset" ascii
)
266 (cons :tag
"Underline characters sequence"
267 (const :tag
"Latin-1 charset" latin1
)
269 (cons :tag
"Underline characters sequence"
270 (const :tag
"UTF-8 charset" utf-8
)
271 (repeat character
))))
273 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
276 "Bullet characters for headlines converted to lists in ASCII export.
278 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
279 and whose value is a list of characters.
281 The first character is used for the first level considered as low
282 level, and so on. If there are more levels than characters given
283 here, the list will be repeated.
285 Note that this variable doesn't affect plain lists
287 :group
'org-export-e-ascii
289 (cons :tag
"Bullet characters for low level headlines"
290 (const :tag
"ASCII charset" ascii
)
292 (cons :tag
"Bullet characters for low level headlines"
293 (const :tag
"Latin-1 charset" latin1
)
295 (cons :tag
"Bullet characters for low level headlines"
296 (const :tag
"UTF-8 charset" utf-8
)
297 (repeat character
))))
299 (defcustom org-e-ascii-links-to-notes t
300 "Non-nil means convert links to notes before the next headline.
301 When nil, the link will be exported in place. If the line
302 becomes long in this way, it will be wrapped."
303 :group
'org-export-e-ascii
306 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
307 "Non-nil means keep all vertical lines in ASCII tables.
308 When nil, vertical lines will be removed except for those needed
309 for column grouping."
310 :group
'org-export-e-ascii
313 (defcustom org-e-ascii-table-widen-columns t
314 "Non-nil means widen narrowed columns for export.
315 When nil, narrowed columns will look in ASCII export just like in
316 Org mode, i.e. with \"=>\" as ellipsis."
317 :group
'org-export-e-ascii
320 (defcustom org-e-ascii-caption-above nil
321 "When non-nil, place caption string before the element.
322 Otherwise, place it right after it."
323 :group
'org-export-e-ascii
326 (defcustom org-e-ascii-verbatim-format
"`%s'"
327 "Format string used for verbatim text and inline code."
328 :group
'org-export-e-ascii
331 (defcustom org-e-ascii-format-drawer-function nil
332 "Function called to format a drawer in ASCII.
334 The function must accept two parameters:
335 NAME the drawer name, like \"LOGBOOK\"
336 CONTENTS the contents of the drawer.
337 WIDTH the text width within the drawer.
339 The function should return either the string to be exported or
340 nil to ignore the drawer.
342 For example, the variable could be set to the following function
343 in order to mimic default behaviour:
345 \(defun org-e-ascii-format-drawer-default \(name contents width\)
346 \"Format a drawer element for ASCII export.\"
348 :group
'org-export-e-ascii
351 (defcustom org-e-ascii-format-inlinetask-function nil
352 "Function called to format an inlinetask in ASCII.
354 The function must accept six parameters:
355 TODO the todo keyword, as a string
356 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
357 PRIORITY the inlinetask priority, as a string
358 NAME the inlinetask name, as a string.
359 TAGS the inlinetask tags, as a string.
360 CONTENTS the contents of the inlinetask, as a string.
362 The function should return either the string to be exported or
363 nil to ignore the inline task.
365 For example, the variable could be set to the following function
366 in order to mimic default behaviour:
368 \(defun org-e-ascii-format-inlinetask-default
369 \(todo type priority name tags contents\)
370 \"Format an inline task element for ASCII export.\"
371 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
372 \(width org-e-ascii-inlinetask-width\)
373 \(org-e-ascii--indent-string
375 ;; Top line, with an additional blank line if not in UTF-8.
376 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
377 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
378 ;; Add title. Fill it if wider than inlinetask.
379 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
380 \(if \(<= \(length title\) width\) title
381 \(org-e-ascii--fill-string title width info\)\)\)
383 ;; If CONTENTS is not empty, insert it along with
385 \(when \(org-string-nw-p contents\)
386 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
388 \(make-string width \(if utf8p ?━ ?_\)\)\)
389 ;; Flush the inlinetask to the right.
390 \(- \(plist-get info :ascii-width\)
391 \(plist-get info :ascii-margin\)
392 \(plist-get info :ascii-inner-margin\)
393 \(org-e-ascii--current-text-width inlinetask info\)\)"
394 :group
'org-export-e-ascii
399 ;;; Internal Functions
401 ;; Internal functions fall into three categories.
403 ;; The first one is about text formatting. The core function is
404 ;; `org-e-ascii--current-text-width', which determines the current
405 ;; text width allowed to a given element. In other words, it helps
406 ;; keeping each line width within maximum text width defined in
407 ;; `org-e-ascii-text-width'. Once this information is known,
408 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
409 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
410 ;; operate on a given output string.
412 ;; The second category contains functions handling elements listings,
413 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
414 ;; returns a complete table of contents, `org-e-ascii--list-listings'
415 ;; returns a list of referenceable src-block elements, and
416 ;; `org-e-ascii--list-tables' does the same for table elements.
418 ;; The third category includes general helper functions.
419 ;; `org-e-ascii--build-title' creates the title for a given headline
420 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
421 ;; caption string associated to a table or a src-block.
422 ;; `org-e-ascii--describe-links' creates notes about links for
423 ;; insertion at the end of a section. It uses
424 ;; `org-e-ascii--unique-links' to get the list of links to describe.
425 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
426 ;; to internationalize output.
429 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
430 "Fill a string with specified text-width and return it.
432 S is the string being filled. TEXT-WIDTH is an integer
433 specifying maximum length of a line. INFO is the plist used as
434 a communication channel.
436 Optional argument JUSTIFY can specify any type of justification
437 among `left', `center', `right' or `full'. A nil value is
438 equivalent to `left'. For a justification that doesn't also fill
439 string, see `org-e-ascii--justify-string'.
441 Return nil if S isn't a string."
442 ;; Don't fill paragraph when break should be preserved.
443 (cond ((not (stringp s
)) nil
)
444 ((plist-get info
:preserve-breaks
) s
)
446 (let ((fill-column text-width
)
447 (use-hard-newlines t
))
449 (fill-region (point-min) (point-max) justify
))
452 (defun org-e-ascii--justify-string (s text-width how
)
454 TEXT-WIDTH is an integer specifying maximum length of a line.
455 HOW determines the type of justification: it can be `left',
456 `right', `full' or `center'."
459 (goto-char (point-min))
460 (let ((fill-column text-width
))
461 (while (< (point) (point-max))
462 (justify-current-line how
)
466 (defun org-e-ascii--indent-string (s width
)
467 "Indent string S by WIDTH white spaces.
468 Empty lines are not indented."
470 (replace-regexp-in-string
471 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
473 (defun org-e-ascii--box-string (s info
)
474 "Return string S with a partial box to its left.
475 INFO is a plist used as a communicaton channel."
476 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
477 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
478 (replace-regexp-in-string
479 "^" (if utf8p
"│ " "| ")
480 ;; Remove last newline character.
481 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
483 (defun org-e-ascii--current-text-width (element info
)
484 "Return maximum text width for ELEMENT's contents.
485 INFO is a plist used as a communication channel."
487 ;; Elements with an absolute width: `headline' and `inlinetask'.
488 ((eq (car element
) 'inlinetask
) org-e-ascii-inlinetask-width
)
489 ((eq (car element
) 'headline
)
490 (- org-e-ascii-text-width
491 (let ((low-level-rank (org-export-low-level-p element info
)))
492 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
493 ;; Elements with a relative width: store maximum text width in
496 (let* ((genealogy (cons element
(plist-get info
:genealogy
)))
497 ;; Total width is determined by the presence, or not, of an
498 ;; inline task among ELEMENT parents.
500 (if (loop for parent in genealogy
501 thereis
(eq (car parent
) 'inlinetask
))
502 org-e-ascii-inlinetask-width
503 ;; No inlinetask: Remove global margin from text width.
504 (- org-e-ascii-text-width
505 org-e-ascii-global-margin
506 (let ((parent (org-export-get-parent-headline element info
)))
507 ;; Inner margin doesn't apply to text before first
510 (let ((low-level-rank
511 (org-export-low-level-p parent info
)))
512 ;; Inner margin doesn't apply to contents of
513 ;; low level headlines, since they've got their
514 ;; own indentation mechanism.
515 (if low-level-rank
(* low-level-rank
2)
516 org-e-ascii-inner-margin
))))))))
518 ;; Each `quote-block', `quote-section' and `verse-block' above
519 ;; narrows text width by twice the standard margin size.
520 (+ (let ((margin (max (floor (/ total-width
12)) 2)))
521 (* (loop for parent in genealogy
522 when
(memq (car parent
)
523 '(quote-block quote-section verse-block
))
526 ;; Text width within a plain-list is restricted by
527 ;; indentation of current item. If that's the case,
528 ;; compute it with the help of `:structure' property from
529 ;; parent item, if any.
531 (if (eq (car element
) 'item
) element
532 (loop for parent in genealogy
533 when
(eq (car parent
) 'item
)
535 (if (not parent-item
) 0
536 ;; Compute indentation offset of the current item,
537 ;; that is the sum of the difference between its
538 ;; indentation and the indentation of the top item in
539 ;; the list and current item bullet's length. Also
540 ;; remove tag length (for description lists) or bullet
542 (let ((struct (org-element-get-property :structure parent-item
))
543 (beg-item (org-element-get-property :begin parent-item
)))
544 (+ (- (org-list-get-ind beg-item struct
)
546 (org-list-get-top-point struct
) struct
))
548 (or (org-list-get-tag beg-item struct
)
549 (org-list-get-bullet beg-item struct
)))))))))))))
551 (defun org-e-ascii--build-title
552 (element info text-width
&optional underline notags
)
553 "Format ELEMENT title and return it.
555 ELEMENT is either an `headline' or `inlinetask' element. INFO is
556 a plist used as a communication channel. TEXT-WIDTH is an
557 integer representing the maximum length of a line.
559 When optional argument UNDERLINE is non-nil, underline title,
560 without the tags, according to `org-e-ascii-underline'
563 if optional argument NOTAGS is nil, no tags will be added to the
565 (let* ((headlinep (eq (car element
) 'headline
))
567 ;; Numbering is specific to headlines.
569 ;; Section numbering must be active, and headline's
570 ;; level should be above specified limit, if any.
571 (let ((sec-num (plist-get info
:section-numbers
)))
572 (if (not (wholenump sec-num
)) sec-num
573 (<= (org-export-get-relative-level headline info
) sec-num
)))
574 ;; All tests passed: build numbering string.
578 (org-export-get-headline-number element info
) ".")
580 (text (org-export-secondary-string
581 (org-element-get-property :title element
) 'e-ascii info
))
583 (and (plist-get info
:with-todo-keywords
)
584 (let ((todo (org-element-get-property :todo-keyword element
)))
586 (concat (org-export-secondary-string todo
'e-ascii info
)
588 (tags (and (not notags
)
589 (plist-get info
:with-tags
)
590 (org-element-get-property :tags element
)))
592 (and (plist-get info
:with-priority
)
593 (concat (org-element-get-property :priority element
) " ")))
594 (first-part (concat numbers todo priority text
)))
597 ;; Align tags, if any.
601 (max (- text-width
(1+ (length first-part
))) (length tags
)))
603 ;; Maybe underline text, if ELEMENT type is `headline' and an
604 ;; underline character has been defined.
605 (when (and underline headlinep
)
607 (nth (1- (org-export-get-relative-level element info
))
608 (cdr (assq (plist-get info
:ascii-charset
)
609 org-e-ascii-underline
)))))
612 (make-string (length first-part
) under-char
))))))))
614 (defun org-e-ascii--build-caption (element info
)
615 "Return caption string for ELEMENT, if applicable.
617 INFO is a plist used as a communication channel.
619 The caption string contains the sequence number of ELEMENT if it
620 has a name affiliated keyword, along with the real caption, if
621 any. Return nil when ELEMENT has no affiliated caption or name
623 (let ((caption (org-element-get-property :caption element
))
624 (name (org-element-get-property :name element
)))
625 (when (or caption name
)
626 ;; Get sequence number of current src-block among every
627 ;; src-block with either a caption or a name.
629 (org-export-get-ordinal
631 (lambda (el) (or (org-element-get-property :caption el
)
632 (org-element-get-property :name el
)))))
633 (title-fmt (org-e-ascii--translate
635 (table "Table %d: %s")
636 (src-block "Listing %d: %s")) info
)))
637 (org-e-ascii--fill-string
640 (if (not caption
) name
641 (org-export-secondary-string (car caption
) 'e-ascii info
)))
642 (org-e-ascii--current-text-width element info
) info
)))))
644 (defun org-e-ascii--build-toc (info &optional n keyword
)
645 "Return a table of contents.
647 INFO is a plist used as a communication channel.
649 Optional argument N, when non-nil, is an integer specifying the
652 Optional argument KEYWORD specifies the TOC keyword, if any, from
653 which the table of contents generation has been initiated."
654 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
657 (make-string (1- (length title
))
658 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
661 (if keyword
(org-e-ascii--current-text-width keyword info
)
662 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
665 (let* ((level (org-export-get-relative-level headline info
))
666 (indent (* (1- level
) 3)))
668 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
669 (org-e-ascii--build-title
670 headline info
(- text-width indent
) nil
671 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
672 (org-export-collect-headlines info n
) "\n")))))
674 (defun org-e-ascii--list-listings (keyword info
)
675 "Return a list of listings.
677 KEYWORD is the keyword that initiated the list of listings
678 generation. INFO is a plist used as a communication channel."
679 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
682 (make-string (1- (length title
))
683 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
686 (if keyword
(org-e-ascii--current-text-width keyword info
)
687 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
688 ;; Use a counter instead of retreiving ordinal of each
693 ;; Store initial text so its length can be computed. This is
694 ;; used to properly align caption right to it in case of
695 ;; filling (like contents of a description list item).
697 (format (org-e-ascii--translate "Listing %d: " info
)
702 (org-e-ascii--indent-string
703 (org-e-ascii--fill-string
704 (let ((caption (org-element-get-property :caption src-block
)))
705 (if (not caption
) (org-element-get-property :name src-block
)
706 (org-export-secondary-string
707 ;; Use short name in priority, if available.
708 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
709 (- text-width
(length initial-text
)) info
)
710 (length initial-text
))))))
711 (org-export-collect-listings info
) "\n")))))
713 (defun org-e-ascii--list-tables (keyword info
)
714 "Return a list of listings.
716 KEYWORD is the keyword that initiated the list of listings
717 generation. INFO is a plist used as a communication channel."
718 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
721 (make-string (1- (length title
))
722 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
725 (if keyword
(org-e-ascii--current-text-width keyword info
)
726 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
727 ;; Use a counter instead of retreiving ordinal of each
732 ;; Store initial text so its length can be computed. This is
733 ;; used to properly align caption right to it in case of
734 ;; filling (like contents of a description list item).
736 (format (org-e-ascii--translate "Table %d: " info
)
741 (org-e-ascii--indent-string
742 (org-e-ascii--fill-string
743 (let ((caption (org-element-get-property :caption table
)))
744 (if (not caption
) (org-element-get-property :name table
)
745 ;; Use short name in priority, if available.
746 (org-export-secondary-string
747 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
748 (- text-width
(length initial-text
)) info
)
749 (length initial-text
))))))
750 (org-export-collect-tables info
) "\n")))))
752 (defun org-e-ascii--unique-links (element info
)
753 "Return a list of unique link references in ELEMENT.
755 ELEMENT is either an headline element or a section element. INFO
756 is a plist used as a communication channel.
758 It covers links that may be found current headline's title, in
759 the following section and in any inlinetask's title there."
763 ;; Return LINK if it wasn't referenced so far, or nil.
764 ;; Update SEEN links along the way.
767 (cons (org-element-get-property :raw-link link
)
768 (org-element-get-contents link
))))
769 (unless (member footprint seen
)
770 (push footprint seen
) link
)))))
771 (harvest-links-in-title
773 ;; Return a list of all unique links in ELEMENT. ELEMENT
774 ;; may be an headline or an inlinetask element.
777 (dolist (obj (org-element-get-property :title element
) acc
)
778 (when (and (listp obj
) (eq (car obj
) 'link
))
779 (let ((link (funcall unique-link-p obj
)))
780 (and link
(push link acc
)))))))))
781 ;; Retrieve headline's section, if it exists.
782 (section (if (eq (car element
) 'section
) element
783 (let ((sec (car (org-element-get-contents element
))))
784 (and (eq (car sec
) 'section
) sec
))))
785 (headline (if (eq (car element
) 'headline
) element
786 (org-export-get-parent-headline element info
))))
788 ;; Links that may be in HEADLINE's title.
789 (funcall harvest-links-in-title headline
)
790 ;; Get all links in SECTION.
792 section
'link
(lambda (link local
) (funcall unique-link-p link
)) info
)
793 ;; Links that may be in inlinetasks titles within SECTION.
797 (lambda (inlinetask local
)
798 (push (funcall harvest-links-in-title inlinetask
) acc
))
802 (defun org-e-ascii--describe-links (links width info
)
803 "Return a string describing a list of links.
805 LINKS is a list of link type objects, as returned by
806 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
807 the output string. INFO is a plist used as a communication
811 (let ((type (org-element-get-property :type link
))
812 (anchor (let ((desc (org-element-get-contents link
)))
814 (org-element-get-property :raw-link link
)
815 (org-export-secondary-string desc
'e-ascii info
)))))
817 ;; Coderefs and radio links are ignored.
818 ((member type
'("coderef" "radio")) nil
)
819 ;; Id, custom-id and fuzzy links (with the exception of
820 ;; targets): Headlines refer to their numbering.
821 ((member type
'("custom-id" "fuzzy" "id"))
822 (let ((destination (if (string= type
"fuzzy")
823 (org-export-resolve-fuzzy-link link info
)
824 (org-export-resolve-id-link link info
))))
825 (unless (eq (car destination
) 'target
)
827 (org-e-ascii--fill-string
831 (if (not destination
)
832 (org-e-ascii--translate "Unknown reference" info
)
834 (org-e-ascii--translate "See section %s" info
)
835 (mapconcat 'number-to-string
836 (org-export-get-headline-number destination info
)
838 width info
) "\n\n"))))
839 ;; Do not add a link that cannot be resolved and doesn't have
840 ;; any description: destination is already visible in the
842 ((not (org-element-get-contents link
)) nil
)
845 (org-e-ascii--fill-string
846 (format "[%s] %s" anchor
(org-element-get-property :raw-link link
))
855 (defun org-e-ascii-template--document-title (info)
856 "Return document title, as a string.
857 INFO is a plist used as a communication channel."
858 (let ((text-width org-e-ascii-text-width
)
859 (title (org-export-secondary-string
860 (plist-get info
:title
) 'e-ascii info
))
862 (and (plist-get info
:with-author
)
863 (let ((auth (plist-get info
:author
)))
864 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
866 (and (plist-get info
:with-email
)
867 (org-export-secondary-string
868 (plist-get info
:email
) 'e-ascii info
)))
869 (date (plist-get info
:date
)))
870 ;; There are two types of title blocks depending on the presence
871 ;; of a title to display.
872 (if (string= title
"")
873 ;; Title block without a title. DATE is positioned at the top
874 ;; right of the document, AUTHOR to the top left and EMAIL
877 ((and (org-string-nw-p date
) (org-string-nw-p author
))
880 (make-string (- text-width
(length date
) (length author
)) ?
)
882 (when (org-string-nw-p email
) (concat "\n" email
))
884 ((and (org-string-nw-p date
) (org-string-nw-p email
))
887 (make-string (- text-width
(length date
) (length email
)) ?
)
889 ((org-string-nw-p date
)
891 (org-e-ascii--justify-string date text-width
'right
)
893 ((and (org-string-nw-p author
) (org-string-nw-p email
))
894 (concat author
"\n" email
"\n\n\n"))
895 ((org-string-nw-p author
) (concat author
"\n\n\n"))
896 ((org-string-nw-p email
) (concat email
"\n\n\n")))
897 ;; Title block with a title. Document's TITLE, along with the
898 ;; AUTHOR and its EMAIL are both overlined and an underlined,
899 ;; centered. Date is just below, also centered.
900 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
901 ;; Format TITLE. It may be filled if it is too wide,
902 ;; that is wider than the two thirds of the total width.
903 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
904 (formatted-title (org-e-ascii--fill-string title title-len info
))
907 (min (+ (max title-len
(length author
) (length email
)) 2)
908 text-width
) (if utf8p ?━ ?_
))))
909 (org-e-ascii--justify-string
912 (upcase formatted-title
)
913 (if utf8p
"\n\n\n" "\n\n")
915 ((and (org-string-nw-p author
) (org-string-nw-p email
))
916 (concat author
"\n" email
))
917 ((org-string-nw-p author
) author
)
918 ((org-string-nw-p email
) email
))
920 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
921 "\n\n\n") text-width
'center
)))))
923 (defun org-e-ascii-template (contents info
)
924 "Return complete document string after ASCII conversion.
925 CONTENTS is the transcoded contents string. INFO is a plist
926 holding export options."
927 (org-e-ascii--indent-string
928 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
929 ;; 1. Build title block.
931 (org-e-ascii-template--document-title info
)
932 ;; 2. Table of contents.
933 (let ((depth (plist-get info
:with-toc
)))
936 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
938 ;; 3. Document's body.
940 ;; 4. Footnote definitions.
941 (let ((definitions (org-export-collect-footnote-definitions
942 (plist-get info
:parse-tree
) info
))
943 ;; Insert full links right inside the footnote definition
944 ;; as they have no chance to be inserted later.
945 (org-e-ascii-links-to-notes nil
))
951 (let ((id (format "[%s] " (car ref
))))
952 ;; Distinguish between inline definitions and
953 ;; full-fledged definitions.
955 (let ((def (nth 2 ref
)))
956 (if (eq (car def
) 'org-data
)
957 ;; Full-fledged definition: footnote ID is
958 ;; inserted inside the first parsed paragraph
959 ;; (FIRST), if any, to be sure filling will
960 ;; take it into consideration.
961 (let ((first (car (org-element-get-contents def
))))
962 (if (not (eq (car first
) 'paragraph
))
963 (concat id
"\n" (org-export-data def
'e-ascii info
))
964 (push id
(nthcdr 2 first
))
965 (org-export-data def
'e-ascii info
)))
966 ;; Fill paragraph once footnote ID is inserted in
967 ;; order to have a correct length for first line.
968 (org-e-ascii--fill-string
969 (concat id
(org-export-secondary-string def
'e-ascii info
))
970 text-width info
))))))
971 definitions
"\n\n"))))
972 ;; 5. Creator. Ignore `comment' value as there are no comments in
973 ;; ASCII. Justify it to the bottom right.
974 (let ((creator-info (plist-get info
:with-creator
)))
975 (unless (or (not creator-info
) (eq creator-info
'comment
))
978 (org-e-ascii--fill-string
979 (plist-get info
:creator
) text-width info
'right
))))))
980 org-e-ascii-global-margin
))
982 (defun org-e-ascii--translate (s info
)
985 INFO is a plist used as a communication channel.
987 Translation depends on `:language' property and allowed charset.
988 If no translation in found for a given language and a given
989 charset, fall-back to S."
990 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
991 (lang (plist-get info
:language
))
992 (translations (cdr (assoc s org-e-ascii-dictionary
))))
993 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
997 ;;; Transcode Functions
1001 ;; Babel Calls are ignored.
1006 (defun org-e-ascii-center-block (center-block contents info
)
1007 "Transcode a CENTER-BLOCK element from Org to ASCII.
1008 CONTENTS holds the contents of the block. INFO is a plist
1009 holding contextual information."
1010 (org-e-ascii--justify-string
1011 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1016 ;; Comments are ignored.
1021 ;; Comment Blocks are ignored.
1026 (defun org-e-ascii-drawer (drawer contents info
)
1027 "Transcode a DRAWER element from Org to ASCII.
1028 CONTENTS holds the contents of the block. INFO is a plist
1029 holding contextual information."
1030 (let ((name (org-element-get-property :drawer-name drawer
))
1031 (width (org-e-ascii--current-text-width drawer info
)))
1032 (if (functionp org-e-ascii-format-drawer-function
)
1033 (funcall org-e-ascii-format-drawer-function name contents width
)
1034 ;; If there's no user defined function: simply
1035 ;; display contents of the drawer.
1041 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1042 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1043 CONTENTS holds the contents of the block. INFO is a plist
1044 holding contextual information. See
1051 (defun org-e-ascii-emphasis (emphasis contents info
)
1052 "Transcode EMPHASIS from Org to ASCII.
1053 CONTENTS is the contents of the emphasized text. INFO is a plist
1054 holding contextual information.."
1055 (let ((marker (org-element-get-property :marker emphasis
)))
1056 ;; Leave emphasis markers as-is.
1057 (concat marker contents marker
)))
1062 (defun org-e-ascii-entity (entity contents info
)
1063 "Transcode an ENTITY object from Org to ASCII.
1064 CONTENTS are the definition itself. INFO is a plist holding
1065 contextual information."
1066 (org-element-get-property
1067 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1073 (defun org-e-ascii-example-block (example-block contents info
)
1074 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1075 CONTENTS is nil. INFO is a plist holding contextual information."
1076 (org-e-ascii--box-string (org-export-handle-code example-block info
) info
))
1081 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1082 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1083 CONTENTS is nil. INFO is a plist holding contextual information."
1084 (org-element-get-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-get-property :type export-block
) "ascii")
1093 (org-remove-indentation (org-element-get-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-get-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-get-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-get-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-get-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-get-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-get-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-get-property :title inlinetask
) 'e-ascii info
))
1212 (todo (and (plist-get info
:with-todo-keywords
)
1213 (let ((todo (org-element-get-property
1214 :todo-keyword inlinetask
)))
1216 (org-export-secondary-string todo
'e-ascii info
)))))
1217 (todo-type (org-element-get-property :todo-type inlinetask
))
1218 (tags (and (plist-get info
:with-tags
)
1219 (org-element-get-property :tags inlinetask
)))
1220 (priority (and (plist-get info
:with-priority
)
1221 (org-element-get-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 (let ((type (org-element-get-property
1262 :type
(car (plist-get info
:genealogy
)))))
1263 (if (eq type
'descriptive
)
1265 (org-export-secondary-string
1266 (org-element-get-property :tag item
) 'e-ascii info
) ": ")
1267 (org-element-get-property :bullet item
)))))
1269 ;; Change bullets into more visible form if UTF-8 is active.
1270 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bullet
1271 (replace-regexp-in-string
1273 (replace-regexp-in-string
1275 (replace-regexp-in-string
1277 ;; Contents: Pay attention to indentation. Note: check-boxes are
1278 ;; already taken care of at the paragraph level so they don't
1279 ;; interfere with indentation.
1280 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1281 (if (eq (caar (org-element-get-contents item
)) 'paragraph
)
1283 (concat "\n" contents
))))))
1288 (defun org-e-ascii-keyword (keyword contents info
)
1289 "Transcode a KEYWORD element from Org to ASCII.
1290 CONTENTS is nil. INFO is a plist holding contextual
1292 (let ((key (downcase (org-element-get-property :key keyword
)))
1293 (value (org-element-get-property :value keyword
)))
1295 ((string= key
"ascii") value
)
1296 ((string= key
"toc")
1297 (let ((value (downcase value
)))
1299 ((string-match "\\<headlines\\>" value
)
1300 (let ((depth (or (and (string-match "[0-9]+" value
)
1301 (string-to-number (match-string 0 value
)))
1302 (plist-get info
:with-toc
))))
1303 (org-e-ascii--build-toc
1304 info
(and (wholenump depth
) depth
) keyword
)))
1305 ((string= "tables" value
)
1306 (org-e-ascii--list-tables keyword info
))
1307 ((string= "listings" value
)
1308 (org-e-ascii--list-listings keyword info
)))))
1309 ((string= key
"include")
1310 (org-export-included-file keyword
'e-ascii info
)))))
1313 ;;;; Latex Environment
1315 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1316 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1317 CONTENTS is nil. INFO is a plist holding contextual
1319 (org-remove-indentation (org-element-get-property :value latex-environment
)))
1324 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1325 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1326 CONTENTS is nil. INFO is a plist holding contextual
1328 (org-element-get-property :value latex-fragment
))
1333 (defun org-e-ascii-line-break (line-break contents info
)
1334 "Transcode a LINE-BREAK object from Org to ASCII.
1335 CONTENTS is nil. INFO is a plist holding contextual
1336 information." hard-newline
)
1341 (defun org-e-ascii-link (link desc info
)
1342 "Transcode a LINK object from Org to ASCII.
1344 DESC is the description part of the link, or the empty string.
1345 INFO is a plist holding contextual information."
1346 (let ((raw-link (org-element-get-property :raw-link link
))
1347 (type (org-element-get-property :type link
)))
1349 ((string= type
"coderef")
1350 (let ((ref (org-element-get-property :path link
)))
1351 (format (org-export-get-coderef-format ref desc
)
1352 (org-export-resolve-coderef ref info
))))
1353 ;; Do not apply a special syntax on radio links.
1354 ((string= type
"radio") desc
)
1355 ;; Do not apply a special syntax on fuzzy links pointing to
1357 ((and (string= type
"fuzzy")
1358 (let ((path (org-element-get-property :path link
)))
1359 (loop for target in
(plist-get info
:target-list
)
1361 (org-element-get-property :raw-value target
)
1363 (if (org-string-nw-p desc
) desc raw-link
))
1365 (concat (format "[%s]" (if (org-string-nw-p desc
) desc raw-link
))
1366 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
)))))))
1371 (defun org-e-ascii-macro (macro contents info
)
1372 "Transcode a MACRO element from Org to ASCII.
1373 CONTENTS is nil. INFO is a plist holding contextual
1375 (org-export-expand-macro macro info
))
1380 (defun org-e-ascii-paragraph (paragraph contents info
)
1381 "Transcode a PARAGRAPH element from Org to ASCII.
1382 CONTENTS is the contents of the paragraph, as a string. INFO is
1383 the plist used as a communication channel."
1384 (org-e-ascii--fill-string
1385 (let ((parent (car (plist-get info
:genealogy
))))
1386 ;; If PARAGRAPH is the first one in a list element, be sure to
1387 ;; add the check-box in front of it, before any filling. Later,
1388 ;; it would interfere with line width.
1389 (if (and (eq (car parent
) 'item
)
1390 (equal (car (org-element-get-contents parent
)) paragraph
))
1391 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1392 (concat (case (org-element-get-property :checkbox parent
)
1393 (on (if utf8p
"☑ " "[X] "))
1394 (off (if utf8p
"☐ " "[ ] "))
1395 (trans (if utf8p
"☒ " "[-] ")))
1398 (org-e-ascii--current-text-width paragraph info
) info
))
1403 (defun org-e-ascii-plain-list (plain-list contents info
)
1404 "Transcode a PLAIN-LIST element from Org to ASCII.
1405 CONTENTS is the contents of the list. INFO is a plist holding
1406 contextual information."
1412 (defun org-e-ascii-plain-text (text info
)
1413 "Transcode a TEXT string from Org to ASCII.
1414 INFO is a plist used as a communication channel."
1415 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1416 (plist-get info
:with-special-strings
)))
1418 ;; Usual replacements in utf-8 with proper option set.
1419 (replace-regexp-in-string
1421 (replace-regexp-in-string
1423 (replace-regexp-in-string "---" "—" text
)))))
1426 ;;;; Property Drawer
1428 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1429 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1430 CONTENTS is nil. INFO is a plist used as a communication
1432 ;; The property drawer isn't exported but we want separating blank
1433 ;; lines nonetheless.
1439 (defun org-e-ascii-quote-block (quote-block contents info
)
1440 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1441 CONTENTS holds the contents of the block. INFO is a plist
1442 holding contextual information."
1443 (let* ((width (org-e-ascii--current-text-width quote-block info
))
1444 (margin-width (max (floor (/ width
12)) 2)))
1445 (org-e-ascii--indent-string
1446 (org-remove-indentation
1447 (org-e-ascii--fill-string contents width info
)) margin-width
)))
1452 (defun org-e-ascii-quote-section (quote-section contents info
)
1453 "Transcode a QUOTE-SECTION element from Org to ASCII.
1454 CONTENTS is nil. INFO is a plist holding contextual information."
1455 (let* ((width (org-e-ascii--current-text-width quote-section info
))
1456 (margin-width (max (floor (/ width
12)) 2))
1458 (org-export-secondary-string
1459 (org-remove-indentation
1460 (org-element-get-property :value quote-section
)) 'e-ascii info
)))
1461 (org-e-ascii--indent-string
1464 ;; Don't apply inner margin if parent headline is low level.
1465 (let ((headline (org-export-get-parent-headline quote-section info
)))
1466 (if (org-export-low-level-p headline info
) 0
1467 org-e-ascii-inner-margin
))))))
1472 (defun org-e-ascii-radio-target (radio-target contents info
)
1473 "Transcode a RADIO-TARGET object from Org to ASCII.
1474 CONTENTS is the contents of the target. INFO is a plist holding
1475 contextual information."
1480 (defun org-e-ascii-section (section contents info
)
1481 "Transcode a SECTION element from Org to ASCII.
1482 CONTENTS is the contents of the section. INFO is a plist holding
1483 contextual information."
1484 (org-e-ascii--indent-string
1487 (when org-e-ascii-links-to-notes
1488 ;; Add list of links at the end of SECTION.
1489 (let ((links (org-e-ascii--describe-links
1490 (org-e-ascii--unique-links section info
)
1491 (org-e-ascii--current-text-width section info
) info
)))
1492 ;; Separate list of links and section contents.
1493 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1494 ;; Do not apply inner margin if parent headline is low level.
1495 (let ((headline (org-export-get-parent-headline section info
)))
1496 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1497 org-e-ascii-inner-margin
))))
1502 (defun org-e-ascii-special-block (special-block contents info
)
1503 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1504 CONTENTS holds the contents of the block. INFO is a plist
1505 holding contextual information."
1511 (defun org-e-ascii-src-block (src-block contents info
)
1512 "Transcode a SRC-BLOCK element from Org to ASCII.
1513 CONTENTS holds the contents of the item. INFO is a plist holding
1514 contextual information."
1515 (let ((caption (org-e-ascii--build-caption src-block info
)))
1517 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1518 (org-e-ascii--box-string (org-export-handle-code src-block info
) info
)
1519 (when (and caption
(not org-e-ascii-caption-above
))
1520 (concat "\n" caption
)))))
1522 ;;;; Statistics Cookie
1524 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1525 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1526 CONTENTS is nil. INFO is a plist holding contextual information."
1527 (org-element-get-property :value statistics-cookie
))
1532 (defun org-e-ascii-subscript (subscript contents info
)
1533 "Transcode a SUBSCRIPT object from Org to ASCII.
1534 CONTENTS is the contents of the object. INFO is a plist holding
1535 contextual information."
1536 (if (org-element-get-property :use-brackets-p subscript
)
1537 (format "_{%s}" contents
)
1538 (format "_%s" contents
)))
1543 (defun org-e-ascii-superscript (superscript contents info
)
1544 "Transcode a SUPERSCRIPT object from Org to ASCII.
1545 CONTENTS is the contents of the object. INFO is a plist holding
1546 contextual information."
1547 (if (org-element-get-property :use-brackets-p superscript
)
1548 (format "_{%s}" contents
)
1549 (format "_%s" contents
)))
1554 ;; While `org-e-ascii-table' is the callback function expected by
1555 ;; org-export mechanism, it requires four subroutines to display
1556 ;; tables accordingly to chosen charset, alignment and width
1559 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1560 ;; for each column in the table,
1561 ;; `org-e-ascii-table--vertical-separators' returns a vector
1562 ;; containing separators (or lack thereof),
1563 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1564 ;; depending on charset, separators and position within the tabl and
1565 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1566 ;; a given cell and width.
1568 (defun org-e-ascii-table (table contents info
)
1569 "Transcode a TABLE element from Org to ASCII.
1570 CONTENTS is nil. INFO is a plist holding contextual information."
1571 (let ((raw-table (org-element-get-property :raw-table table
))
1572 (caption (org-e-ascii--build-caption table info
)))
1574 ;; Possibly add a caption string above.
1575 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1576 ;; Insert table. Note: "table.el" tables are left unmodified.
1577 (if (eq (org-element-get-property :type table
) 'table.el
) raw-table
1578 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1579 ;; Extract information out of the raw table (TABLE-INFO)
1580 ;; and clean it (CLEAN-TABLE).
1581 (table-info (org-export-table-format-info raw-table
))
1582 (special-col-p (plist-get table-info
:special-column-p
))
1583 (alignment (plist-get table-info
:alignment
))
1584 (clean-table (org-export-clean-table raw-table special-col-p
))
1585 ;; Change table into lisp, much like `org-table-to-lisp',
1586 ;; being more careful about keeping the exact length of
1587 ;; cells, for alignment purpose.
1591 (if (string-match org-table-hline-regexp line
) 'hline
1592 (org-split-string (org-trim line
) "\\s-?|\\s-?")))
1593 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1594 ;; Compute real column widths.
1596 (org-e-ascii-table--column-width lisp-table table-info
))
1597 ;; Construct separators according to column groups.
1598 (separators (org-e-ascii-table--vertical-separators table-info
))
1599 ;; Build different `hline' strings, depending on
1600 ;; separators, column widths and position.
1602 (org-e-ascii-table--build-hline
1603 nil separators column-widths info
))
1605 (and utf8p
(org-e-ascii-table--build-hline
1606 'top separators column-widths info
)))
1608 (and utf8p
(org-e-ascii-table--build-hline
1609 'bottom separators column-widths info
))))
1610 ;; Now build table back, with correct alignment, considering
1611 ;; columns widths and separators.
1615 ((eq line
'hline
) hline-standard
)
1616 ((eq line
'hline-bottom
) hline-bottom
)
1617 ((eq line
'hline-top
) hline-top
)
1618 (t (loop for cell in line
1619 for col from
0 to
(length line
)
1622 (let ((sep (aref separators col
)))
1623 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1624 (org-e-ascii-table--format-cell
1625 cell col column-widths alignment info
)) into l
1628 (let ((sep (aref separators col
)))
1629 (if (and utf8p
(not (string= sep
""))) "│"
1631 ;; If charset is `utf-8', make sure lisp-table always starts
1632 ;; with `hline-top' and ends with `hline-bottom'.
1633 (if (not utf8p
) lisp-table
1636 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1640 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1642 '(hline-bottom)))) "\n")))
1643 ;; Possible add a caption string below.
1644 (when (and caption
(not org-e-ascii-caption-above
))
1645 (concat "\n" caption
)))))
1647 (defun org-e-ascii-table--column-width (table table-info
)
1648 "Return vector of TABLE columns width.
1650 TABLE is the Lisp representation of the Org table considered.
1651 TABLE-INFO holds information about the table. See
1652 `org-export-table-format-info'.
1654 Unlike to `:width' property from `org-export-table-format-info',
1655 the return value contains width of every column, not only those
1656 with a width cookie."
1657 (let* ((cookies (plist-get table-info
:width
))
1658 (width (make-vector (length cookies
) 0)))
1662 (unless (eq line
'hline
)
1663 (mapc (lambda (cell)
1664 (let ((len (length cell
)))
1665 (when (> len
(aref width idx
)) (aset width idx len
)))
1669 (unless org-e-ascii-table-widen-columns
1670 ;; When colums are not widened, width cookies have precedence
1671 ;; over string lengths. Thus, overwrite the latter with the
1673 (loop for w across cookies
1674 for idx from
0 to
(length cookies
)
1675 when w do
(aset width idx w
)))
1679 (defun org-e-ascii-table--vertical-separators (table-info)
1680 "Return a vector of strings for vertical separators.
1682 TABLE-INFO holds information about considered table. See
1683 `org-export-table-format-info'.
1685 Return value is a vector whose length is one more than the number
1686 of columns in the table. Special column, if any, is ignored."
1687 (let* ((colgroups (plist-get table-info
:column-groups
))
1688 (separators (make-vector (1+ (length colgroups
)) "")))
1689 (if org-e-ascii-table-keep-all-vertical-lines
1690 (make-vector (length separators
) "|")
1692 (mapc (lambda (group)
1693 (when (memq group
'(start start-end
))
1694 (aset separators column
"|"))
1695 (when (memq group
'(end start-end
))
1696 (aset separators
(1+ column
) "|"))
1699 ;; Remove unneeded special column.
1700 (if (not (plist-get table-info
:special-column-p
)) separators
1701 (substring separators
1))))))
1703 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1704 "Format CELL with column width and alignment constraints.
1706 CELL is the contents of the cell, as a string.
1708 COL is the column containing the cell considered.
1710 WIDTH is a vector holding every column width, as returned by
1711 `org-e-ascii-table--column-width'.
1713 ALIGNMENT is a vector containing alignment strings for every
1716 INFO is a plist used as a communication channel."
1717 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1718 (or (aref width col
) (length cell
)))))
1719 ;; When CELL is too large, it has to be truncated.
1720 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1721 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1722 (let* ((indent-tabs-mode nil
)
1723 (align (aref alignment col
))
1725 (org-e-ascii--justify-string
1726 (org-trim cell
) col-width
1727 (cond ((string= align
"c") 'center
)
1728 ((string= align
"r") 'right
)))))
1729 ;; Return aligned cell, with missing white spaces added and
1730 ;; space separators between columns.
1733 (concat aligned-cell
1734 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1736 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1737 "Return string used as an horizontal line in tables.
1739 POSITION is a symbol among `top', `bottom' and nil, which
1740 specifies position of the horizontal line within the table.
1742 SEPARATORS is a vector strings specifying separators used in the
1743 table, as returned by `org-e-ascii-table--vertical-separators'.
1745 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1746 columns in the table, as returned by
1747 `org-e-ascii-table--column-width'.
1749 INFO is a plist used as a communication channel."
1750 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1751 (loop for idx from
0 to
(length separators
)
1752 for width across column-widths
1755 (cond ((string= (aref separators idx
) "") nil
)
1756 ((and utf8p
(zerop idx
))
1757 (cond ((eq position
'top
) "┍")
1758 ((eq position
'bottom
) "┕")
1761 (cond ((eq position
'top
) "┯")
1762 ((eq position
'bottom
) "┷")
1765 ;; Hline has to cover all the cell and
1766 ;; both white spaces between columns.
1767 (make-string (+ width
2)
1768 (cond ((not utf8p
) ?-
)
1776 ((string= (aref separators idx
) "") nil
)
1777 (utf8p (cond ((eq position
'top
) "┑")
1778 ((eq position
'bottom
) "┙")
1785 (defun org-e-ascii-target (target contents info
)
1786 "Transcode a TARGET object from Org to ASCII.
1787 CONTENTS is the contents of the target. INFO is a plist holding
1788 contextual information."
1794 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1795 "Transcode a TIME-STAMP object from Org to ASCII.
1796 CONTENTS is nil. INFO is a plist holding contextual information."
1797 ;; Return time-stamps as-is.
1798 (org-element-time-stamp-interpreter time-stamp contents
))
1803 (defun org-e-ascii-verbatim (verbatim contents info
)
1804 "Return a VERBATIM object from Org to ASCII.
1805 CONTENTS is nil. INFO is a plist holding contextual information."
1806 (format org-e-ascii-verbatim-format
1807 (org-element-get-property :value verbatim
)))
1812 (defun org-e-ascii-verse-block (verse-block contents info
)
1813 "Transcode a VERSE-BLOCK element from Org to ASCII.
1814 CONTENTS is nil. INFO is a plist holding contextual information."
1815 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1816 (org-e-ascii--indent-string
1817 (org-e-ascii--justify-string
1818 (org-export-secondary-string
1819 (org-element-get-property :value verse-block
) 'e-ascii info
)
1821 (max (floor (/ verse-width
12)) 2))))
1826 (defun org-e-ascii-filter-headline-blank-lines (headline back-end
)
1827 "Filter controlling number of blank lines after an headline.
1829 HEADLINE is a string representing a transcoded headline.
1830 BACK-END is symbol specifying back-end used for export.
1832 This function only applies to `e-ascii' back-end. See
1833 `org-e-ascii-headline-spacing' for information.
1835 For any other back-end, HEADLINE is returned as-is."
1836 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1837 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1838 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1842 ;;; Interactive function
1844 (defun org-e-ascii-export-to-ascii
1845 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1846 "Export current buffer to a text file.
1848 If narrowing is active in the current buffer, only export its
1851 If a region is active, export that region.
1853 When optional argument SUBTREEP is non-nil, export the sub-tree
1854 at point, extracting information from the headline properties
1857 When optional argument VISIBLE-ONLY is non-nil, don't export
1858 contents of hidden elements.
1860 When optional argument BODY-ONLY is non-nil, strip title, table
1861 of contents and footnote definitions from output.
1863 EXT-PLIST, when provided, is a property list with external
1864 parameters overriding Org default settings, but still inferior to
1865 file-local settings.
1867 When optional argument PUB-DIR is set, use it as the publishing
1870 Return output file's name."
1872 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1874 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1877 (provide 'org-e-ascii
)
1878 ;;; org-e-ascii.el ends here