1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements an ASCII back-end for Org generic exporter.
27 ;; M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
29 ;; in an Org mode buffer then switch to that buffer to see the ASCII
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
35 (eval-when-compile (require 'cl
))
37 (declare-function org-element-contents
"org-element" (element))
38 (declare-function org-element-property
"org-element" (property element
))
39 (declare-function org-element-normalize-string
"org-element" (s))
40 (declare-function org-element-map
"org-element"
41 (data types fun
&optional info first-match
))
42 (declare-function org-element-time-stamp-interpreter
43 "org-element" (time-stamp contents
))
45 (declare-function org-export-clean-table
"org-export" (table specialp
))
46 (declare-function org-export-collect-footnote-definitions
47 "org-export" (data info
))
48 (declare-function org-export-collect-headlines
"org-export" (info &optional n
))
49 (declare-function org-export-collect-listings
"org-export" (info))
50 (declare-function org-export-collect-tables
"org-export" (info))
51 (declare-function org-export-data
"org-export" (data backend info
))
52 (declare-function org-export-expand-macro
"org-export" (macro info
))
53 (declare-function org-export-format-code-default
"org-export" (element info
))
54 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
55 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
56 (declare-function org-export-get-headline-number
"org-export" (headline info
))
57 (declare-function org-export-get-ordinal
"org-export"
58 (element info
&optional types predicate
))
59 (declare-function org-export-get-parent-headline
"org-export" (blob info
))
60 (declare-function org-export-get-relative-level
"org-export" (headline info
))
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-resolve-ref-link
"org-export" (link info
))
69 (declare-function org-export-secondary-string
70 "org-export" (secondary backend info
))
71 (declare-function org-export-table-format-info
"org-export" (table))
73 org-export-to-file
"org-export"
74 (backend file
&optional subtreep visible-only body-only ext-plist
))
78 ;;; Internal Variables
80 ;; The following setting won't allow to modify preferred charset
81 ;; through a buffer keyword or an option item, but, since the property
82 ;; will appear in communication channel nonetheless, it allows to
83 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
86 ;; We also install a filter for headlines and sections, in order to
87 ;; control blank lines separating them in output string.
89 (defconst org-e-ascii-option-alist
90 '((:ascii-charset nil nil org-e-ascii-charset
)
92 "Alist between ASCII export properties and ways to set them.
93 See `org-export-option-alist' for more information on the
94 structure or the values.")
96 (defconst org-e-ascii-filters-alist
97 '((:filter-headline . org-e-ascii-filter-headline-blank-lines
)
98 (:filter-section . org-e-ascii-filter-headline-blank-lines
))
99 "Alist between filters keywords and back-end specific filters.
100 See `org-export-filters-alist' for more information.")
102 (defconst org-e-ascii-dictionary
106 :latin1
"Footnotes\n"
107 :utf-8
"Footnotes\n")
109 :ascii
"Notes de bas de page\n"
110 :latin1
"Notes de bas de page\n"
111 :utf-8
"Notes de bas de page\n"))
114 :ascii
"Listing %d: %s"
115 :latin1
"Listing %d: %s"
116 :utf-8
"Listing %d: %s")
118 :ascii
"Programme %d : %s"
119 :latin1
"Programme %d : %s"
120 :utf-8
"Programme nº %d : %s"))
121 ("List Of Listings\n"
123 :ascii
"List Of Listings\n"
124 :latin1
"List Of Listings\n"
125 :utf-8
"List Of Listings\n")
127 :ascii
"Liste des programmes\n"
128 :latin1
"Liste des programmes\n"
129 :utf-8
"Liste des programmes\n"))
132 :ascii
"List Of Tables\n"
133 :latin1
"List Of Tables\n"
134 :utf-8
"List Of Tables\n")
136 :ascii
"Liste des tableaux\n"
137 :latin1
"Liste des tableaux\n"
138 :utf-8
"Liste des tableaux\n"))
141 :ascii
"Listing %d: "
142 :latin1
"Listing %d: "
143 :utf-8
"Listing %d: ")
145 :ascii
"Programme %d : "
146 :latin1
"Programme %d : "
147 :utf-8
"Programme nº %d : "))
148 ("Table Of Contents\n"
150 :ascii
"Table Of Contents\n"
151 :latin1
"Table Of Contents\n"
152 :utf-8
"Table Of Contents\n")
155 :latin1
"Table des matières\n"
156 :utf-8
"Table des matières\n"))
159 :ascii
"Table %d: %s"
160 :latin1
"Table %d: %s"
161 :utf-8
"Table %d: %s")
163 :ascii
"Tableau %d : %s"
164 :latin1
"Tableau %d : %s"
165 :utf-8
"Tableau nº %d : %s"))
168 :ascii
"See section %s"
169 :latin1
"See section %s"
170 :utf-8
"See section %s")
172 :ascii
"cf. section %s"
173 :latin1
"cf. section %s"
174 :utf-8
"cf. section %s"))
181 :ascii
"Tableau %d : "
182 :latin1
"Tableau %d : "
183 :utf-8
"Tableau nº %d : "))
186 :ascii
"Unknown reference"
187 :latin1
"Unknown reference"
188 :utf-8
"Unknown reference")
190 :ascii
"Destination inconnue"
191 :latin1
"Référence inconnue"
192 :utf-8
"Référence inconnue")))
193 "Dictionary for ASCII back-end.
195 Alist whose car is the string to translate and cdr is an alist
196 whose car is the language string and cdr is a plist whose
197 properties are possible charsets and value the translated term.
199 It is used as a database for `org-e-ascii--translate'.")
203 ;;; User Configurable Variables
205 (defgroup org-export-e-ascii nil
206 "Options for exporting Org mode files to ASCII."
207 :tag
"Org Export ASCII"
210 (defcustom org-e-ascii-text-width
72
211 "Maximum width of exported text.
212 This number includes margin size, as set in
213 `org-e-ascii-global-margin'."
214 :group
'org-export-e-ascii
217 (defcustom org-e-ascii-global-margin
0
218 "Width of the left margin, in number of characters."
219 :group
'org-export-e-ascii
222 (defcustom org-e-ascii-inner-margin
2
223 "Width of the inner margin, in number of characters.
224 Inner margin is applied between each headline."
225 :group
'org-export-e-ascii
228 (defcustom org-e-ascii-quote-margin
6
229 "Width of margin used for quoting text, in characters.
230 This margin is applied on both sides of the text."
231 :group
'org-export-e-ascii
234 (defcustom org-e-ascii-inlinetask-width
30
235 "Width of inline tasks, in number of characters.
236 This number ignores any margin."
237 :group
'org-export-e-ascii
240 (defcustom org-e-ascii-headline-spacing
'(1 .
2)
241 "Number of blank lines inserted around headlines.
243 This variable can be set to a cons cell. In that case, its car
244 represents the number of blank lines present before headline
245 contents whereas its cdr reflects the number of blank lines after
248 A nil value replicates the number of blank lines found in the
249 original Org buffer at the same place."
250 :group
'org-export-e-ascii
252 (const :tag
"Replicate original spacing" nil
)
253 (cons :tag
"Set an uniform spacing"
254 (integer :tag
"Number of blank lines before contents")
255 (integer :tag
"Number of blank lines after contents"))))
257 (defcustom org-e-ascii-charset
'ascii
258 "The charset allowed to represent various elements and objects.
260 `ascii' Only use plain ASCII characters
261 `latin1' Include Latin-1 characters
262 `utf-8' Use all UTF-8 characters"
263 :group
'org-export-e-ascii
265 (const :tag
"ASCII" ascii
)
266 (const :tag
"Latin-1" latin1
)
267 (const :tag
"UTF-8" utf-8
)))
269 (defcustom org-e-ascii-underline
'((ascii ?
= ?~ ?-
)
271 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
272 "Characters for underlining headings in ASCII export.
274 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
275 and whose value is a list of characters.
277 For each supported charset, this variable associates a sequence
278 of underline characters. In a sequence, the characters will be
279 used in order for headlines level 1, 2, ... If no character is
280 available for a given level, the headline won't be underlined."
281 :group
'org-export-e-ascii
283 (cons :tag
"Underline characters sequence"
284 (const :tag
"ASCII charset" ascii
)
286 (cons :tag
"Underline characters sequence"
287 (const :tag
"Latin-1 charset" latin1
)
289 (cons :tag
"Underline characters sequence"
290 (const :tag
"UTF-8 charset" utf-8
)
291 (repeat character
))))
293 (defcustom org-e-ascii-bullets
'((ascii ?
* ?
+ ?-
)
296 "Bullet characters for headlines converted to lists in ASCII export.
298 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
299 and whose value is a list of characters.
301 The first character is used for the first level considered as low
302 level, and so on. If there are more levels than characters given
303 here, the list will be repeated.
305 Note that this variable doesn't affect plain lists
307 :group
'org-export-e-ascii
309 (cons :tag
"Bullet characters for low level headlines"
310 (const :tag
"ASCII charset" ascii
)
312 (cons :tag
"Bullet characters for low level headlines"
313 (const :tag
"Latin-1 charset" latin1
)
315 (cons :tag
"Bullet characters for low level headlines"
316 (const :tag
"UTF-8 charset" utf-8
)
317 (repeat character
))))
319 (defcustom org-e-ascii-links-to-notes t
320 "Non-nil means convert links to notes before the next headline.
321 When nil, the link will be exported in place. If the line
322 becomes long in this way, it will be wrapped."
323 :group
'org-export-e-ascii
326 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
327 "Non-nil means keep all vertical lines in ASCII tables.
328 When nil, vertical lines will be removed except for those needed
329 for column grouping."
330 :group
'org-export-e-ascii
333 (defcustom org-e-ascii-table-widen-columns t
334 "Non-nil means widen narrowed columns for export.
335 When nil, narrowed columns will look in ASCII export just like in
336 Org mode, i.e. with \"=>\" as ellipsis."
337 :group
'org-export-e-ascii
340 (defcustom org-e-ascii-caption-above nil
341 "When non-nil, place caption string before the element.
342 Otherwise, place it right after it."
343 :group
'org-export-e-ascii
346 (defcustom org-e-ascii-verbatim-format
"`%s'"
347 "Format string used for verbatim text and inline code."
348 :group
'org-export-e-ascii
351 (defcustom org-e-ascii-format-drawer-function nil
352 "Function called to format a drawer in ASCII.
354 The function must accept two parameters:
355 NAME the drawer name, like \"LOGBOOK\"
356 CONTENTS the contents of the drawer.
357 WIDTH the text width within the drawer.
359 The function should return either the string to be exported or
360 nil to ignore the drawer.
362 For example, the variable could be set to the following function
363 in order to mimic default behaviour:
365 \(defun org-e-ascii-format-drawer-default \(name contents width\)
366 \"Format a drawer element for ASCII export.\"
368 :group
'org-export-e-ascii
371 (defcustom org-e-ascii-format-inlinetask-function nil
372 "Function called to format an inlinetask in ASCII.
374 The function must accept six parameters:
375 TODO the todo keyword, as a string
376 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
377 PRIORITY the inlinetask priority, as a string
378 NAME the inlinetask name, as a string.
379 TAGS the inlinetask tags, as a string.
380 CONTENTS the contents of the inlinetask, as a string.
382 The function should return either the string to be exported or
383 nil to ignore the inline task.
385 For example, the variable could be set to the following function
386 in order to mimic default behaviour:
388 \(defun org-e-ascii-format-inlinetask-default
389 \(todo type priority name tags contents\)
390 \"Format an inline task element for ASCII export.\"
391 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
392 \(width org-e-ascii-inlinetask-width\)
393 \(org-e-ascii--indent-string
395 ;; Top line, with an additional blank line if not in UTF-8.
396 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
397 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
398 ;; Add title. Fill it if wider than inlinetask.
399 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
400 \(if \(<= \(length title\) width\) title
401 \(org-e-ascii--fill-string title width info\)\)\)
403 ;; If CONTENTS is not empty, insert it along with
405 \(when \(org-string-nw-p contents\)
406 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
408 \(make-string width \(if utf8p ?━ ?_\)\)\)
409 ;; Flush the inlinetask to the right.
410 \(- \(plist-get info :ascii-width\)
411 \(plist-get info :ascii-margin\)
412 \(plist-get info :ascii-inner-margin\)
413 \(org-e-ascii--current-text-width inlinetask info\)\)"
414 :group
'org-export-e-ascii
419 ;;; Internal Functions
421 ;; Internal functions fall into three categories.
423 ;; The first one is about text formatting. The core function is
424 ;; `org-e-ascii--current-text-width', which determines the current
425 ;; text width allowed to a given element. In other words, it helps
426 ;; keeping each line width within maximum text width defined in
427 ;; `org-e-ascii-text-width'. Once this information is known,
428 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
429 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
430 ;; operate on a given output string.
432 ;; The second category contains functions handling elements listings,
433 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
434 ;; returns a complete table of contents, `org-e-ascii--list-listings'
435 ;; returns a list of referenceable src-block elements, and
436 ;; `org-e-ascii--list-tables' does the same for table elements.
438 ;; The third category includes general helper functions.
439 ;; `org-e-ascii--build-title' creates the title for a given headline
440 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
441 ;; caption string associated to a table or a src-block.
442 ;; `org-e-ascii--describe-links' creates notes about links for
443 ;; insertion at the end of a section. It uses
444 ;; `org-e-ascii--unique-links' to get the list of links to describe.
445 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
446 ;; to internationalize output.
449 (defun org-e-ascii--fill-string (s text-width info
&optional justify
)
450 "Fill a string with specified text-width and return it.
452 S is the string being filled. TEXT-WIDTH is an integer
453 specifying maximum length of a line. INFO is the plist used as
454 a communication channel.
456 Optional argument JUSTIFY can specify any type of justification
457 among `left', `center', `right' or `full'. A nil value is
458 equivalent to `left'. For a justification that doesn't also fill
459 string, see `org-e-ascii--justify-string'.
461 Return nil if S isn't a string."
462 ;; Don't fill paragraph when break should be preserved.
463 (cond ((not (stringp s
)) nil
)
464 ((plist-get info
:preserve-breaks
) s
)
466 (let ((fill-column text-width
)
467 (use-hard-newlines t
))
469 (fill-region (point-min) (point-max) justify
))
472 (defun org-e-ascii--justify-string (s text-width how
)
474 TEXT-WIDTH is an integer specifying maximum length of a line.
475 HOW determines the type of justification: it can be `left',
476 `right', `full' or `center'."
479 (goto-char (point-min))
480 (let ((fill-column text-width
))
481 (while (< (point) (point-max))
482 (justify-current-line how
)
486 (defun org-e-ascii--indent-string (s width
)
487 "Indent string S by WIDTH white spaces.
488 Empty lines are not indented."
490 (replace-regexp-in-string
491 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
493 (defun org-e-ascii--box-string (s info
)
494 "Return string S with a partial box to its left.
495 INFO is a plist used as a communicaton channel."
496 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
497 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
498 (replace-regexp-in-string
499 "^" (if utf8p
"│ " "| ")
500 ;; Remove last newline character.
501 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
503 (defun org-e-ascii--current-text-width (element info
)
504 "Return maximum text width for ELEMENT's contents.
505 INFO is a plist used as a communication channel."
506 (case (org-element-type element
)
507 ;; Elements with an absolute width: `headline' and `inlinetask'.
508 (inlinetask org-e-ascii-inlinetask-width
)
510 (- org-e-ascii-text-width
511 (let ((low-level-rank (org-export-low-level-p element info
)))
512 (if low-level-rank
(* low-level-rank
2) org-e-ascii-global-margin
))))
513 ;; Elements with a relative width: store maximum text width in
516 (let* ((genealogy (cons element
(org-export-get-genealogy element info
)))
517 ;; Total width is determined by the presence, or not, of an
518 ;; inline task among ELEMENT parents.
520 (if (loop for parent in genealogy
521 thereis
(eq (org-element-type parent
) 'inlinetask
))
522 org-e-ascii-inlinetask-width
523 ;; No inlinetask: Remove global margin from text width.
524 (- org-e-ascii-text-width
525 org-e-ascii-global-margin
526 (let ((parent (org-export-get-parent-headline element info
)))
527 ;; Inner margin doesn't apply to text before first
530 (let ((low-level-rank
531 (org-export-low-level-p parent info
)))
532 ;; Inner margin doesn't apply to contents of
533 ;; low level headlines, since they've got their
534 ;; own indentation mechanism.
535 (if low-level-rank
(* low-level-rank
2)
536 org-e-ascii-inner-margin
))))))))
538 ;; Each `quote-block', `quote-section' and `verse-block' above
539 ;; narrows text width by twice the standard margin size.
540 (+ (* (loop for parent in genealogy
541 when
(memq (org-element-type parent
)
542 '(quote-block quote-section verse-block
))
544 2 org-e-ascii-quote-margin
)
545 ;; Text width within a plain-list is restricted by
546 ;; indentation of current item. If that's the case,
547 ;; compute it with the help of `:structure' property from
548 ;; parent item, if any.
550 (if (eq (org-element-type element
) 'item
) element
551 (loop for parent in genealogy
552 when
(eq (org-element-type parent
) 'item
)
554 (if (not parent-item
) 0
555 ;; Compute indentation offset of the current item,
556 ;; that is the sum of the difference between its
557 ;; indentation and the indentation of the top item in
558 ;; the list and current item bullet's length. Also
559 ;; remove tag length (for description lists) or bullet
561 (let ((struct (org-element-property :structure parent-item
))
562 (beg-item (org-element-property :begin parent-item
)))
563 (+ (- (org-list-get-ind beg-item struct
)
565 (org-list-get-top-point struct
) struct
))
567 (or (org-list-get-tag beg-item struct
)
568 (org-list-get-bullet beg-item struct
)))))))))))))
570 (defun org-e-ascii--build-title
571 (element info text-width
&optional underline notags
)
572 "Format ELEMENT title and return it.
574 ELEMENT is either an `headline' or `inlinetask' element. INFO is
575 a plist used as a communication channel. TEXT-WIDTH is an
576 integer representing the maximum length of a line.
578 When optional argument UNDERLINE is non-nil, underline title,
579 without the tags, according to `org-e-ascii-underline'
582 if optional argument NOTAGS is nil, no tags will be added to the
584 (let* ((headlinep (eq (org-element-type element
) 'headline
))
586 ;; Numbering is specific to headlines.
587 (and headlinep
(org-export-numbered-headline-p element info
)
588 ;; All tests passed: build numbering string.
592 (org-export-get-headline-number element info
) ".")
594 (text (org-export-secondary-string
595 (org-element-property :title element
) 'e-ascii info
))
597 (and (plist-get info
:with-todo-keywords
)
598 (let ((todo (org-element-property :todo-keyword element
)))
600 (concat (org-export-secondary-string todo
'e-ascii info
)
602 (tags (and (not notags
)
603 (plist-get info
:with-tags
)
604 (org-element-property :tags element
)))
606 (and (plist-get info
:with-priority
)
607 (concat (org-element-property :priority element
) " ")))
608 (first-part (concat numbers todo priority text
)))
611 ;; Align tags, if any.
615 (max (- text-width
(1+ (length first-part
))) (length tags
)))
617 ;; Maybe underline text, if ELEMENT type is `headline' and an
618 ;; underline character has been defined.
619 (when (and underline headlinep
)
621 (nth (1- (org-export-get-relative-level element info
))
622 (cdr (assq (plist-get info
:ascii-charset
)
623 org-e-ascii-underline
)))))
626 (make-string (length first-part
) under-char
))))))))
628 (defun org-e-ascii--build-caption (element info
)
629 "Return caption string for ELEMENT, if applicable.
631 INFO is a plist used as a communication channel.
633 The caption string contains the sequence number of ELEMENT if it
634 has a name affiliated keyword, along with the real caption, if
635 any. Return nil when ELEMENT has no affiliated caption or name
637 (let ((caption (org-element-property :caption element
))
638 (name (org-element-property :name element
)))
639 (when (or caption name
)
640 ;; Get sequence number of current src-block among every
641 ;; src-block with either a caption or a name.
643 (org-export-get-ordinal
645 (lambda (el) (or (org-element-property :caption el
)
646 (org-element-property :name el
)))))
647 (title-fmt (org-e-ascii--translate
648 (case (org-element-type element
)
649 (table "Table %d: %s")
650 (src-block "Listing %d: %s")) info
)))
651 (org-e-ascii--fill-string
654 (if (not caption
) name
655 (org-export-secondary-string (car caption
) 'e-ascii info
)))
656 (org-e-ascii--current-text-width element info
) info
)))))
658 (defun org-e-ascii--build-toc (info &optional n keyword
)
659 "Return a table of contents.
661 INFO is a plist used as a communication channel.
663 Optional argument N, when non-nil, is an integer specifying the
666 Optional argument KEYWORD specifies the TOC keyword, if any, from
667 which the table of contents generation has been initiated."
668 (let ((title (org-e-ascii--translate "Table Of Contents\n" info
)))
671 (make-string (1- (length title
))
672 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
675 (if keyword
(org-e-ascii--current-text-width keyword info
)
676 (- org-e-ascii-text-width org-e-ascii-global-margin
))))
679 (let* ((level (org-export-get-relative-level headline info
))
680 (indent (* (1- level
) 3)))
682 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
683 (org-e-ascii--build-title
684 headline info
(- text-width indent
) nil
685 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
686 (org-export-collect-headlines info n
) "\n")))))
688 (defun org-e-ascii--list-listings (keyword info
)
689 "Return a list of listings.
691 KEYWORD is the keyword that initiated the list of listings
692 generation. INFO is a plist used as a communication channel."
693 (let ((title (org-e-ascii--translate "List Of Listings\n" info
)))
696 (make-string (1- (length title
))
697 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
700 (if keyword
(org-e-ascii--current-text-width keyword info
)
701 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
702 ;; Use a counter instead of retreiving ordinal of each
707 ;; Store initial text so its length can be computed. This is
708 ;; used to properly align caption right to it in case of
709 ;; filling (like contents of a description list item).
711 (format (org-e-ascii--translate "Listing %d: " info
)
716 (org-e-ascii--indent-string
717 (org-e-ascii--fill-string
718 (let ((caption (org-element-property :caption src-block
)))
719 (if (not caption
) (org-element-property :name src-block
)
720 (org-export-secondary-string
721 ;; Use short name in priority, if available.
722 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
723 (- text-width
(length initial-text
)) info
)
724 (length initial-text
))))))
725 (org-export-collect-listings info
) "\n")))))
727 (defun org-e-ascii--list-tables (keyword info
)
728 "Return a list of listings.
730 KEYWORD is the keyword that initiated the list of listings
731 generation. INFO is a plist used as a communication channel."
732 (let ((title (org-e-ascii--translate "List Of Tables\n" info
)))
735 (make-string (1- (length title
))
736 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
739 (if keyword
(org-e-ascii--current-text-width keyword info
)
740 (- org-e-ascii-text-width org-e-ascii-global-margin
)))
741 ;; Use a counter instead of retreiving ordinal of each
746 ;; Store initial text so its length can be computed. This is
747 ;; used to properly align caption right to it in case of
748 ;; filling (like contents of a description list item).
750 (format (org-e-ascii--translate "Table %d: " info
)
755 (org-e-ascii--indent-string
756 (org-e-ascii--fill-string
757 (let ((caption (org-element-property :caption table
)))
758 (if (not caption
) (org-element-property :name table
)
759 ;; Use short name in priority, if available.
760 (org-export-secondary-string
761 (or (cdr caption
) (car caption
)) 'e-ascii info
)))
762 (- text-width
(length initial-text
)) info
)
763 (length initial-text
))))))
764 (org-export-collect-tables info
) "\n")))))
766 (defun org-e-ascii--unique-links (element info
)
767 "Return a list of unique link references in ELEMENT.
769 ELEMENT is either an headline element or a section element. INFO
770 is a plist used as a communication channel.
772 It covers links that may be found current headline's title, in
773 the following section and in any inlinetask's title there."
777 ;; Return LINK if it wasn't referenced so far, or nil.
778 ;; Update SEEN links along the way.
781 (cons (org-element-property :raw-link link
)
782 (org-element-contents link
))))
783 (unless (member footprint seen
)
784 (push footprint seen
) link
)))))
785 (harvest-links-in-title
787 ;; Return a list of all unique links in ELEMENT. ELEMENT
788 ;; may be an headline or an inlinetask element.
791 (dolist (obj (org-element-property :title element
) acc
)
792 (when (eq (org-element-type obj
) 'link
)
793 (let ((link (funcall unique-link-p obj
)))
794 (and link
(push link acc
)))))))))
795 ;; Retrieve HEADLINE's section, if it exists.
796 (section (if (eq (org-element-type element
) 'section
) element
797 (let ((sec (car (org-element-contents element
))))
798 (and (eq (org-element-type sec
) 'section
) sec
))))
799 (headline (if (eq (org-element-type element
) 'headline
) element
800 (org-export-get-parent-headline element info
))))
802 ;; Links that may be in HEADLINE's title.
803 (funcall harvest-links-in-title headline
)
804 ;; Get all links in SECTION.
806 section
'link
(lambda (link) (funcall unique-link-p link
)) info
))))
808 (defun org-e-ascii--describe-links (links width info
)
809 "Return a string describing a list of links.
811 LINKS is a list of link type objects, as returned by
812 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
813 the output string. INFO is a plist used as a communication
817 (let ((type (org-element-property :type link
))
818 (anchor (let ((desc (org-element-contents link
)))
819 (if (not desc
) (org-element-property :raw-link link
)
820 (org-export-secondary-string desc
'e-ascii info
)))))
822 ;; Coderefs, radio links and fuzzy links are ignored.
823 ((member type
'("coderef" "radio" "fuzzy")) nil
)
824 ;; Id and custom-id links: Headlines refer to their numbering.
825 ((member type
'("custom-id" "id"))
826 (let ((dest (org-export-resolve-id-link link info
)))
828 (org-e-ascii--fill-string
832 (if (not dest
) (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 dest info
) "."))))
837 width info
) "\n\n")))
838 ;; Do not add a link that cannot be resolved and doesn't have
839 ;; any description: destination is already visible in the
841 ((not (org-element-contents link
)) nil
)
844 (org-e-ascii--fill-string
845 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
854 (defun org-e-ascii-template--document-title (info)
855 "Return document title, as a string.
856 INFO is a plist used as a communication channel."
857 (let ((text-width org-e-ascii-text-width
)
858 (title (org-export-secondary-string
859 (plist-get info
:title
) 'e-ascii info
))
861 (and (plist-get info
:with-author
)
862 (let ((auth (plist-get info
:author
)))
863 (and auth
(org-export-secondary-string auth
'e-ascii info
)))))
865 (and (plist-get info
:with-email
)
866 (org-export-secondary-string
867 (plist-get info
:email
) 'e-ascii info
)))
868 (date (plist-get info
:date
)))
869 ;; There are two types of title blocks depending on the presence
870 ;; of a title to display.
871 (if (string= title
"")
872 ;; Title block without a title. DATE is positioned at the top
873 ;; right of the document, AUTHOR to the top left and EMAIL
876 ((and (org-string-nw-p date
) (org-string-nw-p author
))
879 (make-string (- text-width
(length date
) (length author
)) ?
)
881 (when (org-string-nw-p email
) (concat "\n" email
))
883 ((and (org-string-nw-p date
) (org-string-nw-p email
))
886 (make-string (- text-width
(length date
) (length email
)) ?
)
888 ((org-string-nw-p date
)
890 (org-e-ascii--justify-string date text-width
'right
)
892 ((and (org-string-nw-p author
) (org-string-nw-p email
))
893 (concat author
"\n" email
"\n\n\n"))
894 ((org-string-nw-p author
) (concat author
"\n\n\n"))
895 ((org-string-nw-p email
) (concat email
"\n\n\n")))
896 ;; Title block with a title. Document's TITLE, along with the
897 ;; AUTHOR and its EMAIL are both overlined and an underlined,
898 ;; centered. Date is just below, also centered.
899 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
900 ;; Format TITLE. It may be filled if it is too wide,
901 ;; that is wider than the two thirds of the total width.
902 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
903 (formatted-title (org-e-ascii--fill-string title title-len info
))
906 (min (+ (max title-len
(length author
) (length email
)) 2)
907 text-width
) (if utf8p ?━ ?_
))))
908 (org-e-ascii--justify-string
911 (upcase formatted-title
)
913 ((and (org-string-nw-p author
) (org-string-nw-p email
))
914 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
915 ((org-string-nw-p author
)
916 (concat (if utf8p
"\n\n\n" "\n\n") author
))
917 ((org-string-nw-p email
)
918 (concat (if utf8p
"\n\n\n" "\n\n") 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-element-normalize-string
928 (org-e-ascii--indent-string
929 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin
)))
930 ;; 1. Build title block.
932 (org-e-ascii-template--document-title info
)
933 ;; 2. Table of contents.
934 (let ((depth (plist-get info
:with-toc
)))
937 (org-e-ascii--build-toc info
(and (wholenump depth
) depth
))
939 ;; 3. Document's body.
941 ;; 4. Footnote definitions.
942 (let ((definitions (org-export-collect-footnote-definitions
943 (plist-get info
:parse-tree
) info
))
944 ;; Insert full links right inside the footnote definition
945 ;; as they have no chance to be inserted later.
946 (org-e-ascii-links-to-notes nil
))
950 (let ((title (org-e-ascii--translate "Footnotes\n" info
)))
955 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
959 (let ((id (format "[%s] " (car ref
))))
960 ;; Distinguish between inline definitions and
961 ;; full-fledged definitions.
963 (let ((def (nth 2 ref
)))
964 (if (eq (org-element-type def
) 'org-data
)
965 ;; Full-fledged definition: footnote ID is
966 ;; inserted inside the first parsed paragraph
967 ;; (FIRST), if any, to be sure filling will
968 ;; take it into consideration.
969 (let ((first (car (org-element-contents def
))))
970 (if (not (eq (org-element-type first
) 'paragraph
))
971 (concat id
"\n" (org-export-data def
'e-ascii info
))
972 (push id
(nthcdr 2 first
))
973 (org-export-data def
'e-ascii info
)))
974 ;; Fill paragraph once footnote ID is inserted in
975 ;; order to have a correct length for first line.
976 (org-e-ascii--fill-string
977 (concat id
(org-export-secondary-string def
'e-ascii info
))
978 text-width info
))))))
979 definitions
"\n\n"))))
980 ;; 5. Creator. Ignore `comment' value as there are no comments in
981 ;; ASCII. Justify it to the bottom right.
982 (let ((creator-info (plist-get info
:with-creator
)))
983 (unless (or (not creator-info
) (eq creator-info
'comment
))
986 (org-e-ascii--fill-string
987 (plist-get info
:creator
) text-width info
'right
))))))
988 org-e-ascii-global-margin
)))
990 (defun org-e-ascii--translate (s info
)
993 INFO is a plist used as a communication channel.
995 Translation depends on `:language' property and allowed charset.
996 If no translation in found for a given language and a given
997 charset, fall-back to S."
998 (let* ((charset (intern (format ":%s" (plist-get info
:ascii-charset
))))
999 (lang (plist-get info
:language
))
1000 (translations (cdr (assoc s org-e-ascii-dictionary
))))
1001 (or (plist-get (cdr (assoc lang translations
)) charset
) s
)))
1005 ;;; Transcode Functions
1009 ;; Babel Calls are ignored.
1014 (defun org-e-ascii-center-block (center-block contents info
)
1015 "Transcode a CENTER-BLOCK element from Org to ASCII.
1016 CONTENTS holds the contents of the block. INFO is a plist
1017 holding contextual information."
1018 (org-e-ascii--justify-string
1019 contents
(org-e-ascii--current-text-width center-block info
) 'center
))
1024 ;; Comments are ignored.
1029 ;; Comment Blocks are ignored.
1034 (defun org-e-ascii-drawer (drawer contents info
)
1035 "Transcode a DRAWER element from Org to ASCII.
1036 CONTENTS holds the contents of the block. INFO is a plist
1037 holding contextual information."
1038 (let ((name (org-element-property :drawer-name drawer
))
1039 (width (org-e-ascii--current-text-width drawer info
)))
1040 (if (functionp org-e-ascii-format-drawer-function
)
1041 (funcall org-e-ascii-format-drawer-function name contents width
)
1042 ;; If there's no user defined function: simply
1043 ;; display contents of the drawer.
1049 (defun org-e-ascii-dynamic-block (dynamic-block contents info
)
1050 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1051 CONTENTS holds the contents of the block. INFO is a plist
1052 holding contextual information. See
1059 (defun org-e-ascii-emphasis (emphasis contents info
)
1060 "Transcode EMPHASIS from Org to ASCII.
1061 CONTENTS is the contents of the emphasized text. INFO is a plist
1062 holding contextual information.."
1063 (let ((marker (org-element-property :marker emphasis
)))
1064 ;; Leave emphasis markers as-is.
1065 (concat marker contents marker
)))
1070 (defun org-e-ascii-entity (entity contents info
)
1071 "Transcode an ENTITY object from Org to ASCII.
1072 CONTENTS are the definition itself. INFO is a plist holding
1073 contextual information."
1074 (org-element-property
1075 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1081 (defun org-e-ascii-example-block (example-block contents info
)
1082 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1083 CONTENTS is nil. INFO is a plist holding contextual information."
1084 (org-e-ascii--box-string
1085 (org-export-format-code-default example-block info
) info
))
1090 (defun org-e-ascii-export-snippet (export-snippet contents info
)
1091 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1092 CONTENTS is nil. INFO is a plist holding contextual information."
1093 (when (eq (org-export-snippet-backend export-snippet
) 'e-ascii
)
1094 (org-element-property :value export-snippet
)))
1099 (defun org-e-ascii-export-block (export-block contents info
)
1100 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1101 CONTENTS is nil. INFO is a plist holding contextual information."
1102 (when (string= (org-element-property :type export-block
) "ascii")
1103 (org-remove-indentation (org-element-property :value export-block
))))
1108 (defun org-e-ascii-fixed-width (fixed-width contents info
)
1109 "Transcode a FIXED-WIDTH element from Org to ASCII.
1110 CONTENTS is nil. INFO is a plist holding contextual information."
1111 (org-e-ascii--box-string
1112 (replace-regexp-in-string
1113 "^[ \t]*: ?" "" (org-element-property :value fixed-width
)) info
))
1116 ;;;; Footnote Definition
1118 ;; Footnote Definitions are ignored. They are compiled at the end of
1119 ;; the document, by `org-e-ascii-template'.
1122 ;;;; Footnote Reference
1124 (defun org-e-ascii-footnote-reference (footnote-reference contents info
)
1125 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1126 CONTENTS is nil. INFO is a plist holding contextual information."
1127 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1132 (defun org-e-ascii-headline (headline contents info
)
1133 "Transcode an HEADLINE element from Org to ASCII.
1134 CONTENTS holds the contents of the headline. INFO is a plist
1135 holding contextual information."
1136 ;; Don't export footnote section, which will be handled at the end
1138 (unless (org-element-property :footnote-section-p headline
)
1139 (let* ((low-level-rank (org-export-low-level-p headline info
))
1140 (width (org-e-ascii--current-text-width headline info
))
1141 ;; Blank lines between headline and its contents.
1142 ;; `org-e-ascii-headline-spacing', when set, overwrites
1143 ;; original buffer's spacing.
1146 (if org-e-ascii-headline-spacing
(car org-e-ascii-headline-spacing
)
1147 (org-element-property :pre-blank headline
)) ?
\n))
1148 ;; Even if HEADLINE has no section, there might be some
1149 ;; links in its title that we shouldn't forget to describe.
1151 (unless (eq (caar (org-element-contents headline
)) 'section
)
1152 (org-e-ascii--describe-links
1153 (org-e-ascii--unique-links headline info
) width info
))))
1154 ;; Deep subtree: export it as a list item.
1158 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1159 org-e-ascii-bullets
))))
1161 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1164 (org-e-ascii--build-title headline info width
) "\n"
1165 ;; Contents, indented by length of bullet.
1167 (org-e-ascii--indent-string
1169 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1171 ;; Else: Standard headline.
1173 (org-e-ascii--build-title headline info width
'underline
)
1175 (concat (when (org-string-nw-p links
) links
) contents
))))))
1178 ;;;; Horizontal Rule
1180 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info
)
1181 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1182 CONTENTS is nil. INFO is a plist holding contextual
1190 (org-element-property :attr_ascii horizontal-rule
)
1192 (make-string (or (and (wholenump (plist-get attr
:width
))
1193 (plist-get attr
:width
))
1194 (org-e-ascii--current-text-width horizontal-rule info
))
1195 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))))
1198 ;;;; Inline Babel Call
1200 ;; Inline Babel Calls are ignored.
1203 ;;;; Inline Src Block
1205 (defun org-e-ascii-inline-src-block (inline-src-block contents info
)
1206 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1207 CONTENTS holds the contents of the item. INFO is a plist holding
1208 contextual information."
1209 (format org-e-ascii-verbatim-format
1210 (org-element-property :value inline-src-block
)))
1215 (defun org-e-ascii-inlinetask (inlinetask contents info
)
1216 "Transcode an INLINETASK element from Org to ASCII.
1217 CONTENTS holds the contents of the block. INFO is a plist
1218 holding contextual information."
1219 (let ((width (org-e-ascii--current-text-width inlinetask info
))
1220 (title (org-export-secondary-string
1221 (org-element-property :title inlinetask
) 'e-ascii info
))
1222 (todo (and (plist-get info
:with-todo-keywords
)
1223 (let ((todo (org-element-property
1224 :todo-keyword inlinetask
)))
1226 (org-export-secondary-string todo
'e-ascii info
)))))
1227 (todo-type (org-element-property :todo-type inlinetask
))
1228 (tags (and (plist-get info
:with-tags
)
1229 (org-element-property :tags inlinetask
)))
1230 (priority (and (plist-get info
:with-priority
)
1231 (org-element-property :priority inlinetask
))))
1232 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1233 ;; with appropriate arguments.
1234 (if (functionp org-e-ascii-format-inlinetask-function
)
1235 (funcall org-e-ascii-format-inlinetask-function
1236 todo todo-type priority title tags contents width
)
1237 ;; Otherwise, use a default template.
1238 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1239 (org-e-ascii--indent-string
1241 ;; Top line, with an additional blank line if not in UTF-8.
1242 (make-string width
(if utf8p ?━ ?_
)) "\n"
1243 (unless utf8p
(concat (make-string width ?
) "\n"))
1244 ;; Add title. Fill it if wider than inlinetask.
1245 (let ((title (org-e-ascii--build-title inlinetask info width
)))
1246 (if (<= (length title
) width
) title
1247 (org-e-ascii--fill-string title width info
)))
1249 ;; If CONTENTS is not empty, insert it along with
1251 (when (org-string-nw-p contents
)
1252 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1254 (make-string width
(if utf8p ?━ ?_
)))
1255 ;; Flush the inlinetask to the right.
1256 (- org-e-ascii-text-width org-e-ascii-global-margin
1257 (if (not (org-export-get-parent-headline inlinetask info
)) 0
1258 org-e-ascii-inner-margin
)
1259 (org-e-ascii--current-text-width inlinetask info
)))))))
1264 (defun org-e-ascii-item (item contents info
)
1265 "Transcode an ITEM element from Org to ASCII.
1266 CONTENTS holds the contents of the item. INFO is a plist holding
1267 contextual information."
1269 ;; First parent of ITEM is always the plain-list. Get
1270 ;; `:type' property from it.
1271 (org-list-bullet-string
1272 (case (org-element-property :type
(org-export-get-parent item info
))
1275 (org-export-secondary-string
1276 (org-element-property :tag item
) 'e-ascii info
) ": "))
1278 ;; Return correct number for ITEM, paying attention to
1280 (let* ((struct (org-element-property :structure item
))
1281 (bul (org-element-property :bullet item
))
1284 (car (last (org-list-get-item-number
1285 (org-element-property :begin item
)
1287 (org-list-prevs-alist struct
)
1288 (org-list-parents-alist struct
)))))))
1289 (replace-regexp-in-string "[0-9]+" num bul
)))
1290 (t (let ((bul (org-element-property :bullet item
)))
1291 ;; Change bullets into more visible form if UTF-8 is active.
1292 (if (not (eq (plist-get info
:ascii-charset
) 'utf-8
)) bul
1293 (replace-regexp-in-string
1295 (replace-regexp-in-string
1297 (replace-regexp-in-string "*" "‣" bul
))))))))))
1300 ;; Contents: Pay attention to indentation. Note: check-boxes are
1301 ;; already taken care of at the paragraph level so they don't
1302 ;; interfere with indentation.
1303 (let ((contents (org-e-ascii--indent-string contents
(length bullet
))))
1304 (if (eq (caar (org-element-contents item
)) 'paragraph
)
1306 (concat "\n" contents
))))))
1311 (defun org-e-ascii-keyword (keyword contents info
)
1312 "Transcode a KEYWORD element from Org to ASCII.
1313 CONTENTS is nil. INFO is a plist holding contextual
1315 (let ((key (org-element-property :key keyword
))
1316 (value (org-element-property :value keyword
)))
1318 ((string= key
"ASCII") value
)
1319 ((string= key
"TOC")
1320 (let ((value (downcase value
)))
1322 ((string-match "\\<headlines\\>" value
)
1323 (let ((depth (or (and (string-match "[0-9]+" value
)
1324 (string-to-number (match-string 0 value
)))
1325 (plist-get info
:with-toc
))))
1326 (org-e-ascii--build-toc
1327 info
(and (wholenump depth
) depth
) keyword
)))
1328 ((string= "tables" value
)
1329 (org-e-ascii--list-tables keyword info
))
1330 ((string= "listings" value
)
1331 (org-e-ascii--list-listings keyword info
))))))))
1334 ;;;; Latex Environment
1336 (defun org-e-ascii-latex-environment (latex-environment contents info
)
1337 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1338 CONTENTS is nil. INFO is a plist holding contextual
1340 (org-remove-indentation (org-element-property :value latex-environment
)))
1345 (defun org-e-ascii-latex-fragment (latex-fragment contents info
)
1346 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1347 CONTENTS is nil. INFO is a plist holding contextual
1349 (org-element-property :value latex-fragment
))
1354 (defun org-e-ascii-line-break (line-break contents info
)
1355 "Transcode a LINE-BREAK object from Org to ASCII.
1356 CONTENTS is nil. INFO is a plist holding contextual
1357 information." hard-newline
)
1362 (defun org-e-ascii-link (link desc info
)
1363 "Transcode a LINK object from Org to ASCII.
1365 DESC is the description part of the link, or the empty string.
1366 INFO is a plist holding contextual information."
1367 (let ((raw-link (org-element-property :raw-link link
))
1368 (type (org-element-property :type link
)))
1370 ((string= type
"coderef")
1371 (let ((ref (org-element-property :path link
)))
1372 (format (org-export-get-coderef-format ref desc
)
1373 (org-export-resolve-coderef ref info
))))
1374 ;; Do not apply a special syntax on radio links. Though, parse
1375 ;; and transcode path to have a proper display of contents.
1376 ((string= type
"radio")
1377 (org-export-secondary-string
1378 (org-element-parse-secondary-string
1379 (org-element-property :path link
)
1380 (cdr (assq 'radio-target org-element-object-restrictions
)))
1382 ;; Do not apply a special syntax on fuzzy links pointing to
1384 ((string= type
"fuzzy")
1385 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1386 ;; Ignore invisible "#+target: path".
1387 (unless (eq (org-element-type destination
) 'keyword
)
1388 (if (org-string-nw-p desc
) desc
1390 (let ((number (org-export-get-ordinal destination info
)))
1392 (if (atom number
) (number-to-string number
)
1393 (mapconcat 'number-to-string number
".")))))))))
1395 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1397 (format "[%s]" desc
)
1398 (unless org-e-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1403 (defun org-e-ascii-macro (macro contents info
)
1404 "Transcode a MACRO element from Org to ASCII.
1405 CONTENTS is nil. INFO is a plist holding contextual
1407 (org-export-expand-macro macro info
))
1412 (defun org-e-ascii-paragraph (paragraph contents info
)
1413 "Transcode a PARAGRAPH element from Org to ASCII.
1414 CONTENTS is the contents of the paragraph, as a string. INFO is
1415 the plist used as a communication channel."
1416 (org-e-ascii--fill-string
1417 (let ((parent (org-export-get-parent paragraph info
)))
1418 ;; If PARAGRAPH is the first one in a list element, be sure to
1419 ;; add the check-box in front of it, before any filling. Later,
1420 ;; it would interfere with line width.
1421 (if (and (eq (org-element-type parent
) 'item
)
1422 (equal (car (org-element-contents parent
)) paragraph
))
1423 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1424 (concat (case (org-element-property :checkbox parent
)
1425 (on (if utf8p
"☑ " "[X] "))
1426 (off (if utf8p
"☐ " "[ ] "))
1427 (trans (if utf8p
"☒ " "[-] ")))
1430 (org-e-ascii--current-text-width paragraph info
) info
))
1435 (defun org-e-ascii-plain-list (plain-list contents info
)
1436 "Transcode a PLAIN-LIST element from Org to ASCII.
1437 CONTENTS is the contents of the list. INFO is a plist holding
1438 contextual information."
1444 (defun org-e-ascii-plain-text (text info
)
1445 "Transcode a TEXT string from Org to ASCII.
1446 INFO is a plist used as a communication channel."
1447 (if (not (and (eq (plist-get info
:ascii-charset
) 'utf-8
)
1448 (plist-get info
:with-special-strings
)))
1450 ;; Usual replacements in utf-8 with proper option set.
1451 (replace-regexp-in-string
1453 (replace-regexp-in-string
1455 (replace-regexp-in-string "---" "—" text
)))))
1458 ;;;; Property Drawer
1460 (defun org-e-ascii-property-drawer (property-drawer contents info
)
1461 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1462 CONTENTS is nil. INFO is a plist used as a communication
1464 ;; The property drawer isn't exported but we want separating blank
1465 ;; lines nonetheless.
1471 (defun org-e-ascii-quote-block (quote-block contents info
)
1472 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1473 CONTENTS holds the contents of the block. INFO is a plist
1474 holding contextual information."
1475 (let ((width (org-e-ascii--current-text-width quote-block info
)))
1476 (org-e-ascii--indent-string
1477 (org-remove-indentation
1478 (org-e-ascii--fill-string contents width info
))
1479 org-e-ascii-quote-margin
)))
1484 (defun org-e-ascii-quote-section (quote-section contents info
)
1485 "Transcode a QUOTE-SECTION element from Org to ASCII.
1486 CONTENTS is nil. INFO is a plist holding contextual information."
1487 (let ((width (org-e-ascii--current-text-width quote-section info
))
1489 (org-export-secondary-string
1490 (org-remove-indentation
1491 (org-element-property :value quote-section
)) 'e-ascii info
)))
1492 (org-e-ascii--indent-string
1494 (+ org-e-ascii-quote-margin
1495 ;; Don't apply inner margin if parent headline is low level.
1496 (let ((headline (org-export-get-parent-headline quote-section info
)))
1497 (if (org-export-low-level-p headline info
) 0
1498 org-e-ascii-inner-margin
))))))
1503 (defun org-e-ascii-radio-target (radio-target contents info
)
1504 "Transcode a RADIO-TARGET object from Org to ASCII.
1505 CONTENTS is the contents of the target. INFO is a plist holding
1506 contextual information."
1511 (defun org-e-ascii-section (section contents info
)
1512 "Transcode a SECTION element from Org to ASCII.
1513 CONTENTS is the contents of the section. INFO is a plist holding
1514 contextual information."
1515 (org-e-ascii--indent-string
1518 (when org-e-ascii-links-to-notes
1519 ;; Add list of links at the end of SECTION.
1520 (let ((links (org-e-ascii--describe-links
1521 (org-e-ascii--unique-links section info
)
1522 (org-e-ascii--current-text-width section info
) info
)))
1523 ;; Separate list of links and section contents.
1524 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1525 ;; Do not apply inner margin if parent headline is low level.
1526 (let ((headline (org-export-get-parent-headline section info
)))
1527 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1528 org-e-ascii-inner-margin
))))
1533 (defun org-e-ascii-special-block (special-block contents info
)
1534 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1535 CONTENTS holds the contents of the block. INFO is a plist
1536 holding contextual information."
1542 (defun org-e-ascii-src-block (src-block contents info
)
1543 "Transcode a SRC-BLOCK element from Org to ASCII.
1544 CONTENTS holds the contents of the item. INFO is a plist holding
1545 contextual information."
1546 (let ((caption (org-e-ascii--build-caption src-block info
)))
1548 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1549 (org-e-ascii--box-string
1550 (org-export-format-code-default src-block info
) info
)
1551 (when (and caption
(not org-e-ascii-caption-above
))
1552 (concat "\n" caption
)))))
1554 ;;;; Statistics Cookie
1556 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info
)
1557 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1558 CONTENTS is nil. INFO is a plist holding contextual information."
1559 (org-element-property :value statistics-cookie
))
1564 (defun org-e-ascii-subscript (subscript contents info
)
1565 "Transcode a SUBSCRIPT object from Org to ASCII.
1566 CONTENTS is the contents of the object. INFO is a plist holding
1567 contextual information."
1568 (if (org-element-property :use-brackets-p subscript
)
1569 (format "_{%s}" contents
)
1570 (format "_%s" contents
)))
1575 (defun org-e-ascii-superscript (superscript contents info
)
1576 "Transcode a SUPERSCRIPT object from Org to ASCII.
1577 CONTENTS is the contents of the object. INFO is a plist holding
1578 contextual information."
1579 (if (org-element-property :use-brackets-p superscript
)
1580 (format "_{%s}" contents
)
1581 (format "_%s" contents
)))
1586 ;; While `org-e-ascii-table' is the callback function expected by
1587 ;; org-export mechanism, it requires four subroutines to display
1588 ;; tables accordingly to chosen charset, alignment and width
1591 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1592 ;; for each column in the table,
1593 ;; `org-e-ascii-table--vertical-separators' returns a vector
1594 ;; containing separators (or lack thereof),
1595 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1596 ;; depending on charset, separators and position within the tabl and
1597 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1598 ;; a given cell and width.
1600 (defun org-e-ascii-table (table contents info
)
1601 "Transcode a TABLE element from Org to ASCII.
1602 CONTENTS is nil. INFO is a plist holding contextual information."
1603 (let ((raw-table (org-element-property :raw-table table
))
1604 (caption (org-e-ascii--build-caption table info
)))
1606 ;; Possibly add a caption string above.
1607 (when (and caption org-e-ascii-caption-above
) (concat caption
"\n"))
1608 ;; Insert table. Note: "table.el" tables are left unmodified.
1609 (if (eq (org-element-property :type table
) 'table.el
) raw-table
1610 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1611 ;; Extract information out of the raw table (TABLE-INFO)
1612 ;; and clean it (CLEAN-TABLE).
1613 (table-info (org-export-table-format-info raw-table
))
1614 (special-col-p (plist-get table-info
:special-column-p
))
1615 (alignment (plist-get table-info
:alignment
))
1616 (clean-table (org-export-clean-table raw-table special-col-p
))
1617 ;; Change table into lisp, much like
1618 ;; `org-table-to-lisp', though cells are parsed and
1619 ;; transcoded along the way.
1623 (if (string-match org-table-hline-regexp line
) 'hline
1627 (org-export-secondary-string
1628 (org-element-parse-secondary-string
1630 (cdr (assq 'item org-element-string-restrictions
)))
1632 (org-split-string (org-trim line
) "\\s-?|\\s-?"))))
1633 (org-split-string clean-table
"[ \t]*\n[ \t]*")))
1634 ;; Compute real column widths.
1636 (org-e-ascii-table--column-width lisp-table table-info
))
1637 ;; Construct separators according to column groups.
1638 (separators (org-e-ascii-table--vertical-separators table-info
))
1639 ;; Build different `hline' strings, depending on
1640 ;; separators, column widths and position.
1642 (org-e-ascii-table--build-hline
1643 nil separators column-widths info
))
1645 (and utf8p
(org-e-ascii-table--build-hline
1646 'top separators column-widths info
)))
1648 (and utf8p
(org-e-ascii-table--build-hline
1649 'bottom separators column-widths info
))))
1650 ;; Now build table back, with correct alignment, considering
1651 ;; columns widths and separators.
1655 ((eq line
'hline
) hline-standard
)
1656 ((eq line
'hline-bottom
) hline-bottom
)
1657 ((eq line
'hline-top
) hline-top
)
1658 (t (loop for cell in line
1659 for col from
0 to
(length line
)
1662 (let ((sep (aref separators col
)))
1663 (if (and utf8p
(not (string= sep
""))) "│" sep
))
1664 (org-e-ascii-table--format-cell
1665 cell col column-widths alignment info
)) into l
1668 (let ((sep (aref separators col
)))
1669 (if (and utf8p
(not (string= sep
""))) "│"
1671 ;; If charset is `utf-8', make sure lisp-table always starts
1672 ;; with `hline-top' and ends with `hline-bottom'.
1673 (if (not utf8p
) lisp-table
1676 (if (eq (car lisp-table
) 'hline
) (cdr lisp-table
)
1680 (if (eq (car (last lisp-table
)) 'hline
) (butlast lisp-table
)
1682 '(hline-bottom)))) "\n")))
1683 ;; Possible add a caption string below.
1684 (when (and caption
(not org-e-ascii-caption-above
))
1685 (concat "\n" caption
)))))
1687 (defun org-e-ascii-table--column-width (table table-info
)
1688 "Return vector of TABLE columns width.
1690 TABLE is the Lisp representation of the Org table considered.
1691 TABLE-INFO holds information about the table. See
1692 `org-export-table-format-info'.
1694 Unlike to `:width' property from `org-export-table-format-info',
1695 the return value is a vector containing width of every column,
1696 not only those with an explicit width cookie. Special column, if
1698 ;; All rows have the same length, but be sure to ignore hlines.
1699 (let ((width (make-vector
1700 (loop for row in table
1701 unless
(eq row
'hline
)
1702 return
(length row
))
1704 ;; Set column width to the maximum width of the cells in that
1709 (unless (eq line
'hline
)
1710 (mapc (lambda (cell)
1711 (let ((len (length cell
)))
1712 (when (> len
(aref width idx
)) (aset width idx len
)))
1716 (unless org-e-ascii-table-widen-columns
1717 ;; When colums are not widened, width cookies have precedence
1718 ;; over string lengths. Thus, overwrite the latter with the
1720 (let ((cookies (plist-get table-info
:width
))
1721 (specialp (plist-get table-info
:special-column-p
)))
1722 ;; Remove special column from COOKIES vector, if any.
1723 (loop for w across
(if specialp
(substring cookies
1) cookies
)
1724 for idx from
0 to width
1725 when w do
(aset width idx w
))))
1729 (defun org-e-ascii-table--vertical-separators (table-info)
1730 "Return a vector of strings for vertical separators.
1732 TABLE-INFO holds information about considered table. See
1733 `org-export-table-format-info'.
1735 Return value is a vector whose length is one more than the number
1736 of columns in the table. Special column, if any, is ignored."
1737 (let* ((colgroups (plist-get table-info
:column-groups
))
1738 (separators (make-vector (1+ (length colgroups
)) "")))
1739 (if org-e-ascii-table-keep-all-vertical-lines
1740 (make-vector (length separators
) "|")
1742 (mapc (lambda (group)
1743 (when (memq group
'(start start-end
))
1744 (aset separators column
"|"))
1745 (when (memq group
'(end start-end
))
1746 (aset separators
(1+ column
) "|"))
1749 ;; Remove unneeded special column.
1750 (if (not (plist-get table-info
:special-column-p
)) separators
1751 (substring separators
1))))))
1753 (defun org-e-ascii-table--format-cell (cell col width alignment info
)
1754 "Format CELL with column width and alignment constraints.
1756 CELL is the contents of the cell, as a string.
1758 COL is the column containing the cell considered.
1760 WIDTH is a vector holding every column width, as returned by
1761 `org-e-ascii-table--column-width'.
1763 ALIGNMENT is a vector containing alignment strings for every
1766 INFO is a plist used as a communication channel."
1767 (let ((col-width (if org-e-ascii-table-widen-columns
(aref width col
)
1768 (or (aref width col
) (length cell
)))))
1769 ;; When CELL is too large, it has to be truncated.
1770 (unless (or org-e-ascii-table-widen-columns
(<= (length cell
) col-width
))
1771 (setq cell
(concat (substring cell
0 (- col-width
2)) "=>")))
1772 (let* ((indent-tabs-mode nil
)
1773 (align (aref alignment col
))
1775 (org-e-ascii--justify-string
1776 (org-trim cell
) col-width
1777 (cond ((string= align
"c") 'center
)
1778 ((string= align
"r") 'right
)))))
1779 ;; Return aligned cell, with missing white spaces added and
1780 ;; space separators between columns.
1783 (concat aligned-cell
1784 (make-string (- col-width
(length aligned-cell
)) ?
))))))
1786 (defun org-e-ascii-table--build-hline (position separators column-widths info
)
1787 "Return string used as an horizontal line in tables.
1789 POSITION is a symbol among `top', `bottom' and nil, which
1790 specifies position of the horizontal line within the table.
1792 SEPARATORS is a vector strings specifying separators used in the
1793 table, as returned by `org-e-ascii-table--vertical-separators'.
1795 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1796 columns in the table, as returned by
1797 `org-e-ascii-table--column-width'.
1799 INFO is a plist used as a communication channel."
1800 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1801 (loop for idx from
0 to
(length separators
)
1802 for width across column-widths
1805 (cond ((string= (aref separators idx
) "") nil
)
1806 ((and utf8p
(zerop idx
))
1807 (cond ((eq position
'top
) "┍")
1808 ((eq position
'bottom
) "┕")
1811 (cond ((eq position
'top
) "┯")
1812 ((eq position
'bottom
) "┷")
1815 ;; Hline has to cover all the cell and both white spaces
1817 (make-string (+ width
2)
1818 (cond ((not utf8p
) ?-
)
1823 ;; There is one separator more than columns, so handle it
1828 ((string= (aref separators idx
) "") nil
)
1829 (utf8p (cond ((eq position
'top
) "┑")
1830 ((eq position
'bottom
) "┙")
1837 ;; Targets are invisible.
1842 (defun org-e-ascii-time-stamp (time-stamp contents info
)
1843 "Transcode a TIME-STAMP object from Org to ASCII.
1844 CONTENTS is nil. INFO is a plist holding contextual information."
1845 ;; Return time-stamps as-is.
1846 (org-element-time-stamp-interpreter time-stamp contents
))
1851 (defun org-e-ascii-verbatim (verbatim contents info
)
1852 "Return a VERBATIM object from Org to ASCII.
1853 CONTENTS is nil. INFO is a plist holding contextual information."
1854 (format org-e-ascii-verbatim-format
1855 (org-element-property :value verbatim
)))
1860 (defun org-e-ascii-verse-block (verse-block contents info
)
1861 "Transcode a VERSE-BLOCK element from Org to ASCII.
1862 CONTENTS is nil. INFO is a plist holding contextual information."
1863 (let ((verse-width (org-e-ascii--current-text-width verse-block info
)))
1864 (org-e-ascii--indent-string
1865 (org-e-ascii--justify-string
1866 (org-export-secondary-string
1867 (org-element-property :value verse-block
) 'e-ascii info
)
1869 org-e-ascii-quote-margin
)))
1874 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info
)
1875 "Filter controlling number of blank lines after an headline.
1877 HEADLINE is a string representing a transcoded headline.
1878 BACK-END is symbol specifying back-end used for export. INFO is
1879 plist containing the communication channel.
1881 This function only applies to `e-ascii' back-end. See
1882 `org-e-ascii-headline-spacing' for information.
1884 For any other back-end, HEADLINE is returned as-is."
1885 (if (not (and (eq back-end
'e-ascii
) org-e-ascii-headline-spacing
)) headline
1886 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing
)) ?
\n)))
1887 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1891 ;;; Interactive function
1893 (defun org-e-ascii-export-to-ascii
1894 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1895 "Export current buffer to a text file.
1897 If narrowing is active in the current buffer, only export its
1900 If a region is active, export that region.
1902 When optional argument SUBTREEP is non-nil, export the sub-tree
1903 at point, extracting information from the headline properties
1906 When optional argument VISIBLE-ONLY is non-nil, don't export
1907 contents of hidden elements.
1909 When optional argument BODY-ONLY is non-nil, strip title, table
1910 of contents and footnote definitions from output.
1912 EXT-PLIST, when provided, is a property list with external
1913 parameters overriding Org default settings, but still inferior to
1914 file-local settings.
1916 When optional argument PUB-DIR is set, use it as the publishing
1919 Return output file's name."
1921 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir
)))
1923 'e-ascii outfile subtreep visible-only body-only ext-plist
)))
1926 (provide 'org-e-ascii
)
1927 ;;; org-e-ascii.el ends here