1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements an ASCII back-end for Org generic exporter.
26 ;; See Org manual for more information.
30 (eval-when-compile (require 'cl
))
34 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
38 ;; The following setting won't allow to modify preferred charset
39 ;; through a buffer keyword or an option item, but, since the property
40 ;; will appear in communication channel nonetheless, it allows to
41 ;; override `org-ascii-charset' variable on the fly by the ext-plist
44 ;; We also install a filter for headlines and sections, in order to
45 ;; control blank lines separating them in output string.
47 (org-export-define-backend 'ascii
48 '((bold . org-ascii-bold
)
49 (center-block . org-ascii-center-block
)
50 (clock . org-ascii-clock
)
51 (code . org-ascii-code
)
52 (comment .
(lambda (&rest args
) ""))
53 (comment-block .
(lambda (&rest args
) ""))
54 (drawer . org-ascii-drawer
)
55 (dynamic-block . org-ascii-dynamic-block
)
56 (entity . org-ascii-entity
)
57 (example-block . org-ascii-example-block
)
58 (export-snippet . org-ascii-export-snippet
)
59 (fixed-width . org-ascii-fixed-width
)
60 (footnote-reference . org-ascii-footnote-reference
)
61 (headline . org-ascii-headline
)
62 (horizontal-rule . org-ascii-horizontal-rule
)
63 (inline-src-block . org-ascii-inline-src-block
)
64 (inlinetask . org-ascii-inlinetask
)
65 (inner-template . org-ascii-inner-template
)
66 (italic . org-ascii-italic
)
67 (item . org-ascii-item
)
68 (keyword . org-ascii-keyword
)
69 (latex-environment . org-ascii-latex-environment
)
70 (latex-fragment . org-ascii-latex-fragment
)
71 (line-break . org-ascii-line-break
)
72 (link . org-ascii-link
)
73 (node-property . org-ascii-node-property
)
74 (paragraph . org-ascii-paragraph
)
75 (plain-list . org-ascii-plain-list
)
76 (plain-text . org-ascii-plain-text
)
77 (planning . org-ascii-planning
)
78 (property-drawer . org-ascii-property-drawer
)
79 (quote-block . org-ascii-quote-block
)
80 (radio-target . org-ascii-radio-target
)
81 (section . org-ascii-section
)
82 (special-block . org-ascii-special-block
)
83 (src-block . org-ascii-src-block
)
84 (statistics-cookie . org-ascii-statistics-cookie
)
85 (strike-through . org-ascii-strike-through
)
86 (subscript . org-ascii-subscript
)
87 (superscript . org-ascii-superscript
)
88 (table . org-ascii-table
)
89 (table-cell . org-ascii-table-cell
)
90 (table-row . org-ascii-table-row
)
91 (target . org-ascii-target
)
92 (template . org-ascii-template
)
93 (timestamp . org-ascii-timestamp
)
94 (underline . org-ascii-underline
)
95 (verbatim . org-ascii-verbatim
)
96 (verse-block . org-ascii-verse-block
))
99 '(?t
"Export to Plain Text"
100 ((?A
"As ASCII buffer"
102 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
105 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
106 (?L
"As Latin1 buffer"
108 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
111 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
112 (?U
"As UTF-8 buffer"
114 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
117 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
118 :filters-alist
'((:filter-headline . org-ascii-filter-headline-blank-lines
)
119 (:filter-parse-tree org-ascii-filter-paragraph-spacing
120 org-ascii-filter-comment-spacing
)
121 (:filter-section . org-ascii-filter-headline-blank-lines
))
123 '((:ascii-bullets nil nil org-ascii-bullets
)
124 (:ascii-caption-above nil nil org-ascii-caption-above
)
125 (:ascii-charset nil nil org-ascii-charset
)
126 (:ascii-global-margin nil nil org-ascii-global-margin
)
127 (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function
)
128 (:ascii-format-inlinetask-function
129 nil nil org-ascii-format-inlinetask-function
)
130 (:ascii-headline-spacing nil nil org-ascii-headline-spacing
)
131 (:ascii-indented-line-width nil nil org-ascii-indented-line-width
)
132 (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width
)
133 (:ascii-inner-margin nil nil org-ascii-inner-margin
)
134 (:ascii-links-to-notes nil nil org-ascii-links-to-notes
)
135 (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing
)
136 (:ascii-quote-margin nil nil org-ascii-quote-margin
)
137 (:ascii-table-keep-all-vertical-lines
138 nil nil org-ascii-table-keep-all-vertical-lines
)
139 (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art
)
140 (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns
)
141 (:ascii-text-width nil nil org-ascii-text-width
)
142 (:ascii-underline nil nil org-ascii-underline
)
143 (:ascii-verbatim-format nil nil org-ascii-verbatim-format
)))
147 ;;; User Configurable Variables
149 (defgroup org-export-ascii nil
150 "Options for exporting Org mode files to ASCII."
151 :tag
"Org Export ASCII"
154 (defcustom org-ascii-text-width
72
155 "Maximum width of exported text.
156 This number includes margin size, as set in
157 `org-ascii-global-margin'."
158 :group
'org-export-ascii
160 :package-version
'(Org .
"8.0")
163 (defcustom org-ascii-global-margin
0
164 "Width of the left margin, in number of characters."
165 :group
'org-export-ascii
167 :package-version
'(Org .
"8.0")
170 (defcustom org-ascii-inner-margin
2
171 "Width of the inner margin, in number of characters.
172 Inner margin is applied between each headline."
173 :group
'org-export-ascii
175 :package-version
'(Org .
"8.0")
178 (defcustom org-ascii-quote-margin
6
179 "Width of margin used for quoting text, in characters.
180 This margin is applied on both sides of the text."
181 :group
'org-export-ascii
183 :package-version
'(Org .
"8.0")
186 (defcustom org-ascii-inlinetask-width
30
187 "Width of inline tasks, in number of characters.
188 This number ignores any margin."
189 :group
'org-export-ascii
191 :package-version
'(Org .
"8.0")
194 (defcustom org-ascii-headline-spacing
'(1 .
2)
195 "Number of blank lines inserted around headlines.
197 This variable can be set to a cons cell. In that case, its car
198 represents the number of blank lines present before headline
199 contents whereas its cdr reflects the number of blank lines after
202 A nil value replicates the number of blank lines found in the
203 original Org buffer at the same place."
204 :group
'org-export-ascii
206 :package-version
'(Org .
"8.0")
208 (const :tag
"Replicate original spacing" nil
)
209 (cons :tag
"Set an uniform spacing"
210 (integer :tag
"Number of blank lines before contents")
211 (integer :tag
"Number of blank lines after contents"))))
213 (defcustom org-ascii-indented-line-width
'auto
214 "Additional indentation width for the first line in a paragraph.
215 If the value is an integer, indent the first line of each
216 paragraph by this width, unless it is located at the beginning of
217 a section, in which case indentation is removed from that line.
218 If it is the symbol `auto' preserve indentation from original
220 :group
'org-export-ascii
222 :package-version
'(Org .
"8.0")
224 (integer :tag
"Number of white spaces characters")
225 (const :tag
"Preserve original width" auto
)))
227 (defcustom org-ascii-paragraph-spacing
'auto
228 "Number of white lines between paragraphs.
229 If the value is an integer, add this number of blank lines
230 between contiguous paragraphs. If is it the symbol `auto', keep
231 the same number of blank lines as in the original document."
232 :group
'org-export-ascii
234 :package-version
'(Org .
"8.0")
236 (integer :tag
"Number of blank lines")
237 (const :tag
"Preserve original spacing" auto
)))
239 (defcustom org-ascii-charset
'ascii
240 "The charset allowed to represent various elements and objects.
242 `ascii' Only use plain ASCII characters
243 `latin1' Include Latin-1 characters
244 `utf-8' Use all UTF-8 characters"
245 :group
'org-export-ascii
247 :package-version
'(Org .
"8.0")
249 (const :tag
"ASCII" ascii
)
250 (const :tag
"Latin-1" latin1
)
251 (const :tag
"UTF-8" utf-8
)))
253 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
255 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
256 "Characters for underlining headings in ASCII export.
258 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
259 and whose value is a list of characters.
261 For each supported charset, this variable associates a sequence
262 of underline characters. In a sequence, the characters will be
263 used in order for headlines level 1, 2, ... If no character is
264 available for a given level, the headline won't be underlined."
265 :group
'org-export-ascii
267 :package-version
'(Org .
"8.0")
269 (cons :tag
"Underline characters sequence"
270 (const :tag
"ASCII charset" ascii
)
272 (cons :tag
"Underline characters sequence"
273 (const :tag
"Latin-1 charset" latin1
)
275 (cons :tag
"Underline characters sequence"
276 (const :tag
"UTF-8 charset" utf-8
)
277 (repeat character
))))
279 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
282 "Bullet characters for headlines converted to lists in ASCII export.
284 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
285 and whose value is a list of characters.
287 The first character is used for the first level considered as low
288 level, and so on. If there are more levels than characters given
289 here, the list will be repeated.
291 Note that this variable doesn't affect plain lists
293 :group
'org-export-ascii
295 :package-version
'(Org .
"8.0")
297 (cons :tag
"Bullet characters for low level headlines"
298 (const :tag
"ASCII charset" ascii
)
300 (cons :tag
"Bullet characters for low level headlines"
301 (const :tag
"Latin-1 charset" latin1
)
303 (cons :tag
"Bullet characters for low level headlines"
304 (const :tag
"UTF-8 charset" utf-8
)
305 (repeat character
))))
307 (defcustom org-ascii-links-to-notes t
308 "Non-nil means convert links to notes before the next headline.
309 When nil, the link will be exported in place. If the line
310 becomes long in this way, it will be wrapped."
311 :group
'org-export-ascii
313 :package-version
'(Org .
"8.0")
316 (defcustom org-ascii-table-keep-all-vertical-lines nil
317 "Non-nil means keep all vertical lines in ASCII tables.
318 When nil, vertical lines will be removed except for those needed
319 for column grouping."
320 :group
'org-export-ascii
322 :package-version
'(Org .
"8.0")
325 (defcustom org-ascii-table-widen-columns t
326 "Non-nil means widen narrowed columns for export.
327 When nil, narrowed columns will look in ASCII export just like in
328 Org mode, i.e. with \"=>\" as ellipsis."
329 :group
'org-export-ascii
331 :package-version
'(Org .
"8.0")
334 (defcustom org-ascii-table-use-ascii-art nil
335 "Non-nil means table.el tables are turned into ascii-art.
337 It only makes sense when export charset is `utf-8'. It is nil by
338 default since it requires ascii-art-to-unicode.el package. You
339 can download it here:
341 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
342 :group
'org-export-ascii
344 :package-version
'(Org .
"8.0")
347 (defcustom org-ascii-caption-above nil
348 "When non-nil, place caption string before the element.
349 Otherwise, place it right after it."
350 :group
'org-export-ascii
352 :package-version
'(Org .
"8.0")
355 (defcustom org-ascii-verbatim-format
"`%s'"
356 "Format string used for verbatim text and inline code."
357 :group
'org-export-ascii
359 :package-version
'(Org .
"8.0")
362 (defcustom org-ascii-format-drawer-function
363 (lambda (name contents width
) contents
)
364 "Function called to format a drawer in ASCII.
366 The function must accept three parameters:
367 NAME the drawer name, like \"LOGBOOK\"
368 CONTENTS the contents of the drawer.
369 WIDTH the text width within the drawer.
371 The function should return either the string to be exported or
372 nil to ignore the drawer.
374 The default value simply returns the value of CONTENTS."
375 :group
'org-export-ascii
377 :package-version
'(Org .
"8.0")
380 (defcustom org-ascii-format-inlinetask-function
381 'org-ascii-format-inlinetask-default
382 "Function called to format an inlinetask in ASCII.
384 The function must accept nine parameters:
385 TODO the todo keyword, as a string
386 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
387 PRIORITY the inlinetask priority, as a string
388 NAME the inlinetask name, as a string.
389 TAGS the inlinetask tags, as a list of strings.
390 CONTENTS the contents of the inlinetask, as a string.
391 WIDTH the width of the inlinetask, as a number.
392 INLINETASK the inlinetask itself.
393 INFO the info channel.
395 The function should return either the string to be exported or
396 nil to ignore the inline task."
397 :group
'org-export-ascii
399 :package-version
'(Org .
"8.3")
404 ;;; Internal Functions
406 ;; Internal functions fall into three categories.
408 ;; The first one is about text formatting. The core functions are
409 ;; `org-ascii--current-text-width' and
410 ;; `org-ascii--current-justification', which determine, respectively,
411 ;; the current text width allowed to a given element and its expected
412 ;; justification. Once this information is known,
413 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
414 ;; `org-ascii--justify-element' `org-ascii--box-string' and
415 ;; `org-ascii--indent-string' can operate on a given output string.
416 ;; In particular, justification happens at the regular (i.e.,
417 ;; non-greater) element level, which means that when the exporting
418 ;; process reaches a container (e.g., a center block) content are
419 ;; already justified.
421 ;; The second category contains functions handling elements listings,
422 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
423 ;; returns a complete table of contents, `org-ascii--list-listings'
424 ;; returns a list of referenceable src-block elements, and
425 ;; `org-ascii--list-tables' does the same for table elements.
427 ;; The third category includes general helper functions.
428 ;; `org-ascii--build-title' creates the title for a given headline
429 ;; or inlinetask element. `org-ascii--build-caption' returns the
430 ;; caption string associated to a table or a src-block.
431 ;; `org-ascii--describe-links' creates notes about links for
432 ;; insertion at the end of a section. It uses
433 ;; `org-ascii--unique-links' to get the list of links to describe.
434 ;; Eventually, `org-ascii--translate' translates a string according
435 ;; to language and charset specification.
438 (defun org-ascii--fill-string (s text-width info
&optional justify
)
439 "Fill a string with specified text-width and return it.
441 S is the string being filled. TEXT-WIDTH is an integer
442 specifying maximum length of a line. INFO is the plist used as
443 a communication channel.
445 Optional argument JUSTIFY can specify any type of justification
446 among `left', `center', `right' or `full'. A nil value is
447 equivalent to `left'. For a justification that doesn't also fill
448 string, see `org-ascii--justify-lines' and
449 `org-ascii--justify-block'.
451 Return nil if S isn't a string."
452 ;; Don't fill paragraph when break should be preserved.
453 (cond ((not (stringp s
)) nil
)
454 ((plist-get info
:preserve-breaks
) s
)
455 (t (let ((double-space-p sentence-end-double-space
))
457 (let ((fill-column text-width
)
458 (use-hard-newlines t
)
459 (sentence-end-double-space double-space-p
))
461 (fill-region (point-min) (point-max) justify
))
464 (defun org-ascii--justify-lines (s text-width how
)
465 "Justify all lines in string S.
466 TEXT-WIDTH is an integer specifying maximum length of a line.
467 HOW determines the type of justification: it can be `left',
468 `right', `full' or `center'."
471 (goto-char (point-min))
472 (let ((fill-column text-width
)
473 ;; Disable `adaptive-fill-mode' so it doesn't prevent
474 ;; filling lines matching `adaptive-fill-regexp'.
475 (adaptive-fill-mode nil
))
476 (while (< (point) (point-max))
477 (justify-current-line how
)
481 (defun org-ascii--justify-element (contents element info
)
482 "Justify CONTENTS of ELEMENT.
483 INFO is a plist used as a communication channel. Justification
484 is done according to the type of element. More accurately,
485 paragraphs are filled and other elements are justified as blocks,
486 that is according to the widest non blank line in CONTENTS."
487 (if (not (org-string-nw-p contents
)) contents
488 (let ((text-width (org-ascii--current-text-width element info
))
489 (how (org-ascii--current-justification element
)))
491 ((eq (org-element-type element
) 'paragraph
)
492 ;; Paragraphs are treated specially as they need to be filled.
493 (org-ascii--fill-string contents text-width info how
))
494 ((eq how
'left
) contents
)
497 (goto-char (point-min))
500 ;; Compute maximum width. Bail out if it is greater
501 ;; than page width, since no justification is
505 (unless (org-looking-at-p "[ \t]*$")
507 (let ((column (current-column)))
509 ((>= column text-width
) (throw 'exit contents
))
510 ((> column max-width
) (setq max-width column
)))))
512 ;; Justify every line according to TEXT-WIDTH and
514 (let ((offset (/ (- text-width max-width
)
515 (if (eq how
'right
) 1 2))))
516 (if (zerop offset
) (throw 'exit contents
)
518 (unless (org-looking-at-p "[ \t]*$")
519 (org-indent-to-column offset
))
521 (buffer-string))))))))
523 (defun org-ascii--indent-string (s width
)
524 "Indent string S by WIDTH white spaces.
525 Empty lines are not indented."
527 (replace-regexp-in-string
528 "\\(^\\)[ \t]*\\S-" (make-string width ?\s
) s nil nil
1)))
530 (defun org-ascii--box-string (s info
)
531 "Return string S with a partial box to its left.
532 INFO is a plist used as a communication channel."
533 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
534 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
535 (replace-regexp-in-string
536 "^" (if utf8p
"│ " "| ")
537 ;; Remove last newline character.
538 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
540 (defun org-ascii--current-text-width (element info
)
541 "Return maximum text width for ELEMENT's contents.
542 INFO is a plist used as a communication channel."
543 (case (org-element-type element
)
544 ;; Elements with an absolute width: `headline' and `inlinetask'.
545 (inlinetask (plist-get info
:ascii-inlinetask-width
))
547 (- (plist-get info
:ascii-text-width
)
548 (let ((low-level-rank (org-export-low-level-p element info
)))
549 (if low-level-rank
(* low-level-rank
2)
550 (plist-get info
:ascii-global-margin
)))))
551 ;; Elements with a relative width: store maximum text width in
554 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
555 ;; Total width is determined by the presence, or not, of an
556 ;; inline task among ELEMENT parents.
558 (if (loop for parent in genealogy
559 thereis
(eq (org-element-type parent
) 'inlinetask
))
560 (plist-get info
:ascii-inlinetask-width
)
561 ;; No inlinetask: Remove global margin from text width.
562 (- (plist-get info
:ascii-text-width
)
563 (plist-get info
:ascii-global-margin
)
564 (let ((parent (org-export-get-parent-headline element
)))
565 ;; Inner margin doesn't apply to text before first
568 (let ((low-level-rank
569 (org-export-low-level-p parent info
)))
570 ;; Inner margin doesn't apply to contents of
571 ;; low level headlines, since they've got their
572 ;; own indentation mechanism.
573 (if low-level-rank
(* low-level-rank
2)
574 (plist-get info
:ascii-inner-margin
)))))))))
576 ;; Each `quote-block' and `verse-block' above narrows text
577 ;; width by twice the standard margin size.
578 (+ (* (loop for parent in genealogy
579 when
(memq (org-element-type parent
)
580 '(quote-block verse-block
))
582 2 (plist-get info
:ascii-quote-margin
))
583 ;; Text width within a plain-list is restricted by
584 ;; indentation of current item. If that's the case,
585 ;; compute it with the help of `:structure' property from
586 ;; parent item, if any.
588 (if (eq (org-element-type element
) 'item
) element
589 (loop for parent in genealogy
590 when
(eq (org-element-type parent
) 'item
)
592 (if (not parent-item
) 0
593 ;; Compute indentation offset of the current item,
594 ;; that is the sum of the difference between its
595 ;; indentation and the indentation of the top item in
596 ;; the list and current item bullet's length. Also
597 ;; remove checkbox length, and tag length (for
598 ;; description lists) or bullet length.
599 (let ((struct (org-element-property :structure parent-item
))
600 (beg-item (org-element-property :begin parent-item
)))
601 (+ (- (org-list-get-ind beg-item struct
)
603 (org-list-get-top-point struct
) struct
))
604 (string-width (or (org-ascii--checkbox parent-item info
)
607 (or (org-list-get-tag beg-item struct
)
608 (org-list-get-bullet beg-item struct
)))))))))))))
610 (defun org-ascii--current-justification (element)
611 "Return expected justification for ELEMENT's contents.
612 Return value is a symbol among `left', `center', `right' and
615 (while (and (not justification
)
616 (setq element
(org-element-property :parent element
)))
617 (case (org-element-type element
)
618 (center-block (setq justification
'center
))
620 (let ((name (org-element-property :type element
)))
621 (cond ((string= name
"JUSTIFYRIGHT") (setq justification
'right
))
622 ((string= name
"JUSTIFYLEFT") (setq justification
'left
)))))))
623 (or justification
'left
)))
625 (defun org-ascii--build-title
626 (element info text-width
&optional underline notags toc
)
627 "Format ELEMENT title and return it.
629 ELEMENT is either an `headline' or `inlinetask' element. INFO is
630 a plist used as a communication channel. TEXT-WIDTH is an
631 integer representing the maximum length of a line.
633 When optional argument UNDERLINE is non-nil, underline title,
634 without the tags, according to `org-ascii-underline'
637 If optional argument NOTAGS is non-nil, no tags will be added to
640 When optional argument TOC is non-nil, use optional title if
641 possible. It doesn't apply to `inlinetask' elements."
642 (let* ((headlinep (eq (org-element-type element
) 'headline
))
644 ;; Numbering is specific to headlines.
645 (and headlinep
(org-export-numbered-headline-p element info
)
646 ;; All tests passed: build numbering string.
650 (org-export-get-headline-number element info
) ".")
655 (if (and toc headlinep
) (org-export-get-alt-title element info
)
656 (org-element-property :title element
))
659 (and (plist-get info
:with-todo-keywords
)
660 (let ((todo (org-element-property :todo-keyword element
)))
661 (and todo
(concat (org-export-data todo info
) " ")))))
662 (tags (and (not notags
)
663 (plist-get info
:with-tags
)
664 (let ((tag-list (org-export-get-tags element info
)))
667 (mapconcat 'identity tag-list
":"))))))
669 (and (plist-get info
:with-priority
)
670 (let ((char (org-element-property :priority element
)))
671 (and char
(format "(#%c) " char
)))))
672 (first-part (concat numbers todo priority text
)))
675 ;; Align tags, if any.
679 (max (- text-width
(1+ (string-width first-part
)))
680 (string-width tags
)))
682 ;; Maybe underline text, if ELEMENT type is `headline' and an
683 ;; underline character has been defined.
684 (when (and underline headlinep
)
686 (nth (1- (org-export-get-relative-level element info
))
687 (cdr (assq (plist-get info
:ascii-charset
)
688 (plist-get info
:ascii-underline
))))))
691 (make-string (/ (string-width first-part
)
692 (char-width under-char
))
695 (defun org-ascii--has-caption-p (element info
)
696 "Non-nil when ELEMENT has a caption affiliated keyword.
697 INFO is a plist used as a communication channel. This function
698 is meant to be used as a predicate for `org-export-get-ordinal'."
699 (org-element-property :caption element
))
701 (defun org-ascii--build-caption (element info
)
702 "Return caption string for ELEMENT, if applicable.
704 INFO is a plist used as a communication channel.
706 The caption string contains the sequence number of ELEMENT along
707 with its real caption. Return nil when ELEMENT has no affiliated
709 (let ((caption (org-export-get-caption element
)))
711 ;; Get sequence number of current src-block among every
712 ;; src-block with a caption.
714 (org-export-get-ordinal
715 element info nil
'org-ascii--has-caption-p
))
716 (title-fmt (org-ascii--translate
717 (case (org-element-type element
)
719 (src-block "Listing %d:"))
721 (org-ascii--fill-string
722 (concat (format title-fmt reference
)
724 (org-export-data caption info
))
725 (org-ascii--current-text-width element info
) info
)))))
727 (defun org-ascii--build-toc (info &optional n keyword
)
728 "Return a table of contents.
730 INFO is a plist used as a communication channel.
732 Optional argument N, when non-nil, is an integer specifying the
735 Optional argument KEYWORD specifies the TOC keyword, if any, from
736 which the table of contents generation has been initiated."
737 (let ((title (org-ascii--translate "Table of Contents" info
)))
740 (make-string (string-width title
)
741 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
744 (if keyword
(org-ascii--current-text-width keyword info
)
745 (- (plist-get info
:ascii-text-width
)
746 (plist-get info
:ascii-global-margin
)))))
749 (let* ((level (org-export-get-relative-level headline info
))
750 (indent (* (1- level
) 3)))
752 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
753 (org-ascii--build-title
754 headline info
(- text-width indent
) nil
755 (or (not (plist-get info
:with-tags
))
756 (eq (plist-get info
:with-tags
) 'not-in-toc
))
758 (org-export-collect-headlines info n
) "\n")))))
760 (defun org-ascii--list-listings (keyword info
)
761 "Return a list of listings.
763 KEYWORD is the keyword that initiated the list of listings
764 generation. INFO is a plist used as a communication channel."
765 (let ((title (org-ascii--translate "List of Listings" info
)))
768 (make-string (string-width title
)
769 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
772 (if keyword
(org-ascii--current-text-width keyword info
)
773 (- (plist-get info
:ascii-text-width
)
774 (plist-get info
:ascii-global-margin
))))
775 ;; Use a counter instead of retrieving ordinal of each
780 ;; Store initial text so its length can be computed. This is
781 ;; used to properly align caption right to it in case of
782 ;; filling (like contents of a description list item).
784 (format (org-ascii--translate "Listing %d:" info
)
786 (initial-width (string-width initial-text
)))
790 (org-ascii--indent-string
791 (org-ascii--fill-string
792 ;; Use short name in priority, if available.
793 (let ((caption (or (org-export-get-caption src-block t
)
794 (org-export-get-caption src-block
))))
795 (org-export-data caption info
))
796 (- text-width initial-width
) info
)
798 (org-export-collect-listings info
) "\n")))))
800 (defun org-ascii--list-tables (keyword info
)
801 "Return a list of tables.
803 KEYWORD is the keyword that initiated the list of tables
804 generation. INFO is a plist used as a communication channel."
805 (let ((title (org-ascii--translate "List of Tables" info
)))
808 (make-string (string-width title
)
809 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
812 (if keyword
(org-ascii--current-text-width keyword info
)
813 (- (plist-get info
:ascii-text-width
)
814 (plist-get info
:ascii-global-margin
))))
815 ;; Use a counter instead of retrieving ordinal of each
820 ;; Store initial text so its length can be computed. This is
821 ;; used to properly align caption right to it in case of
822 ;; filling (like contents of a description list item).
824 (format (org-ascii--translate "Table %d:" info
)
826 (initial-width (string-width initial-text
)))
830 (org-ascii--indent-string
831 (org-ascii--fill-string
832 ;; Use short name in priority, if available.
833 (let ((caption (or (org-export-get-caption table t
)
834 (org-export-get-caption table
))))
835 (org-export-data caption info
))
836 (- text-width initial-width
) info
)
838 (org-export-collect-tables info
) "\n")))))
840 (defun org-ascii--unique-links (element info
)
841 "Return a list of unique link references in ELEMENT.
842 ELEMENT is either a headline element or a section element. INFO
843 is a plist used as a communication channel."
847 ;; Return LINK if it wasn't referenced so far, or nil.
848 ;; Update SEEN links along the way.
851 ;; Normalize description in footprints.
852 (cons (org-element-property :raw-link link
)
853 (let ((contents (org-element-contents link
)))
855 (replace-regexp-in-string
858 (org-element-interpret-data contents
))))))))
859 ;; Ignore LINK if it hasn't been translated already.
860 ;; It can happen if it is located in an affiliated
861 ;; keyword that was ignored.
862 (when (and (org-string-nw-p
863 (gethash link
(plist-get info
:exported-data
)))
864 (not (member footprint seen
)))
865 (push footprint seen
) link
)))))
866 ;; If at a section, find parent headline, if any, in order to
867 ;; count links that might be in the title.
869 (if (eq (org-element-type element
) 'headline
) element
870 (or (org-export-get-parent-headline element
) element
))))
871 ;; Get all links in HEADLINE.
872 (org-element-map headline
'link
873 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
875 (defun org-ascii--describe-links (links width info
)
876 "Return a string describing a list of links.
878 LINKS is a list of link type objects, as returned by
879 `org-ascii--unique-links'. WIDTH is the text width allowed for
880 the output string. INFO is a plist used as a communication
884 (let ((type (org-element-property :type link
))
885 (anchor (let ((desc (org-element-contents link
)))
886 (if desc
(org-export-data desc info
)
887 (org-element-property :raw-link link
)))))
889 ;; Coderefs, radio links and fuzzy links are ignored.
890 ((member type
'("coderef" "radio" "fuzzy")) nil
)
891 ;; Id and custom-id links: Headlines refer to their numbering.
892 ((member type
'("custom-id" "id"))
893 (let ((dest (org-export-resolve-id-link link info
)))
895 (org-ascii--fill-string
899 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
901 (org-ascii--translate "See section %s" info
)
902 (mapconcat 'number-to-string
903 (org-export-get-headline-number dest info
) "."))))
904 width info
) "\n\n")))
905 ;; Do not add a link that cannot be resolved and doesn't have
906 ;; any description: destination is already visible in the
908 ((not (org-element-contents link
)) nil
)
911 (org-ascii--fill-string
912 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
917 (defun org-ascii--checkbox (item info
)
918 "Return checkbox string for ITEM or nil.
919 INFO is a plist used as a communication channel."
920 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
921 (case (org-element-property :checkbox item
)
922 (on (if utf8p
"☑ " "[X] "))
923 (off (if utf8p
"☐ " "[ ] "))
924 (trans (if utf8p
"☒ " "[-] ")))))
930 (defun org-ascii-template--document-title (info)
931 "Return document title, as a string.
932 INFO is a plist used as a communication channel."
933 (let* ((text-width (plist-get info
:ascii-text-width
))
934 ;; Links in the title will not be resolved later, so we make
935 ;; sure their path is located right after them.
936 (info (org-combine-plists info
'(:ascii-links-to-notes nil
)))
937 (title (org-export-data (plist-get info
:title
) info
))
938 (author (and (plist-get info
:with-author
)
939 (let ((auth (plist-get info
:author
)))
940 (and auth
(org-export-data auth info
)))))
941 (email (and (plist-get info
:with-email
)
942 (org-export-data (plist-get info
:email
) info
)))
943 (date (and (plist-get info
:with-date
)
944 (org-export-data (org-export-get-date info
) info
))))
945 ;; There are two types of title blocks depending on the presence
946 ;; of a title to display.
947 (if (string= title
"")
948 ;; Title block without a title. DATE is positioned at the top
949 ;; right of the document, AUTHOR to the top left and EMAIL
952 ((and (org-string-nw-p date
) (org-string-nw-p author
))
955 (make-string (- text-width
(string-width date
) (string-width author
))
958 (when (org-string-nw-p email
) (concat "\n" email
))
960 ((and (org-string-nw-p date
) (org-string-nw-p email
))
963 (make-string (- text-width
(string-width date
) (string-width email
))
966 ((org-string-nw-p date
)
968 (org-ascii--justify-lines date text-width
'right
)
970 ((and (org-string-nw-p author
) (org-string-nw-p email
))
971 (concat author
"\n" email
"\n\n\n"))
972 ((org-string-nw-p author
) (concat author
"\n\n\n"))
973 ((org-string-nw-p email
) (concat email
"\n\n\n")))
974 ;; Title block with a title. Document's TITLE, along with the
975 ;; AUTHOR and its EMAIL are both overlined and an underlined,
976 ;; centered. Date is just below, also centered.
977 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
978 ;; Format TITLE. It may be filled if it is too wide,
979 ;; that is wider than the two thirds of the total width.
980 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
981 (formatted-title (org-ascii--fill-string title title-len info
))
984 (min (+ (max title-len
985 (string-width (or author
""))
986 (string-width (or email
"")))
988 text-width
) (if utf8p ?━ ?_
))))
989 (org-ascii--justify-lines
992 (upcase formatted-title
)
994 ((and (org-string-nw-p author
) (org-string-nw-p email
))
995 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
996 ((org-string-nw-p author
)
997 (concat (if utf8p
"\n\n\n" "\n\n") author
))
998 ((org-string-nw-p email
)
999 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
1001 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
1002 "\n\n\n") text-width
'center
)))))
1004 (defun org-ascii-inner-template (contents info
)
1005 "Return complete document string after ASCII conversion.
1006 CONTENTS is the transcoded contents string. INFO is a plist
1007 holding export options."
1008 (org-element-normalize-string
1009 (let ((global-margin (plist-get info
:ascii-global-margin
)))
1010 (org-ascii--indent-string
1012 ;; 1. Document's body.
1014 ;; 2. Footnote definitions.
1015 (let ((definitions (org-export-collect-footnote-definitions
1016 (plist-get info
:parse-tree
) info
))
1017 ;; Insert full links right inside the footnote definition
1018 ;; as they have no chance to be inserted later.
1019 (info (org-combine-plists info
'(:ascii-links-to-notes nil
))))
1023 (let ((title (org-ascii--translate "Footnotes" info
)))
1027 (string-width title
)
1028 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
1030 (let ((text-width (- (plist-get info
:ascii-text-width
)
1034 (let ((id (format "[%s] " (car ref
))))
1035 ;; Distinguish between inline definitions and
1036 ;; full-fledged definitions.
1038 (let ((def (nth 2 ref
)))
1039 (if (eq (org-element-type def
) 'org-data
)
1040 ;; Full-fledged definition: footnote ID is
1041 ;; inserted inside the first parsed
1042 ;; paragraph (FIRST), if any, to be sure
1043 ;; filling will take it into consideration.
1044 (let ((first (car (org-element-contents def
))))
1045 (if (not (eq (org-element-type first
) 'paragraph
))
1046 (concat id
"\n" (org-export-data def info
))
1047 (push id
(nthcdr 2 first
))
1048 (org-export-data def info
)))
1049 ;; Fill paragraph once footnote ID is inserted
1050 ;; in order to have a correct length for first
1052 (org-ascii--fill-string
1053 (concat id
(org-export-data def info
))
1054 text-width info
))))))
1055 definitions
"\n\n"))))))
1058 (defun org-ascii-template (contents info
)
1059 "Return complete document string after ASCII conversion.
1060 CONTENTS is the transcoded contents string. INFO is a plist
1061 holding export options."
1062 (let ((global-margin (plist-get info
:ascii-global-margin
)))
1064 ;; 1. Build title block.
1065 (org-ascii--indent-string
1066 (concat (org-ascii-template--document-title info
)
1067 ;; 2. Table of contents.
1068 (let ((depth (plist-get info
:with-toc
)))
1071 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
1074 ;; 3. Document's body.
1076 ;; 4. Creator. Ignore `comment' value as there are no comments in
1077 ;; ASCII. Justify it to the bottom right.
1078 (org-ascii--indent-string
1079 (let ((creator-info (plist-get info
:with-creator
))
1080 (text-width (- (plist-get info
:ascii-text-width
) global-margin
)))
1081 (unless (or (not creator-info
) (eq creator-info
'comment
))
1084 (org-ascii--fill-string
1085 (plist-get info
:creator
) text-width info
'right
))))
1088 (defun org-ascii--translate (s info
)
1089 "Translate string S according to specified language and charset.
1090 INFO is a plist used as a communication channel."
1091 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
1092 (org-export-translate s charset info
)))
1096 ;;; Transcode Functions
1100 (defun org-ascii-bold (bold contents info
)
1101 "Transcode BOLD from Org to ASCII.
1102 CONTENTS is the text with bold markup. INFO is a plist holding
1103 contextual information."
1104 (format "*%s*" contents
))
1109 (defun org-ascii-center-block (center-block contents info
)
1110 "Transcode a CENTER-BLOCK element from Org to ASCII.
1111 CONTENTS holds the contents of the block. INFO is a plist
1112 holding contextual information."
1113 ;; Center has already been taken care of at a lower level, so
1114 ;; there's nothing left to do.
1120 (defun org-ascii-clock (clock contents info
)
1121 "Transcode a CLOCK object from Org to ASCII.
1122 CONTENTS is nil. INFO is a plist holding contextual
1124 (org-ascii--justify-element
1125 (concat org-clock-string
" "
1127 (org-element-property :raw-value
1128 (org-element-property :value clock
)))
1129 (let ((time (org-element-property :duration clock
)))
1134 (org-split-string time
":"))))))
1140 (defun org-ascii-code (code contents info
)
1141 "Return a CODE object from Org to ASCII.
1142 CONTENTS is nil. INFO is a plist holding contextual
1144 (format (plist-get info
:ascii-verbatim-format
)
1145 (org-element-property :value code
)))
1150 (defun org-ascii-drawer (drawer contents info
)
1151 "Transcode a DRAWER element from Org to ASCII.
1152 CONTENTS holds the contents of the block. INFO is a plist
1153 holding contextual information."
1154 (let ((name (org-element-property :drawer-name drawer
))
1155 (width (org-ascii--current-text-width drawer info
)))
1156 (funcall (plist-get info
:ascii-format-drawer-function
)
1157 name contents width
)))
1162 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1163 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1164 CONTENTS holds the contents of the block. INFO is a plist
1165 holding contextual information."
1171 (defun org-ascii-entity (entity contents info
)
1172 "Transcode an ENTITY object from Org to ASCII.
1173 CONTENTS are the definition itself. INFO is a plist holding
1174 contextual information."
1175 (org-element-property
1176 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1182 (defun org-ascii-example-block (example-block contents info
)
1183 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1184 CONTENTS is nil. INFO is a plist holding contextual information."
1185 (org-ascii--justify-element
1186 (org-ascii--box-string
1187 (org-export-format-code-default example-block info
) info
)
1188 example-block info
))
1193 (defun org-ascii-export-snippet (export-snippet contents info
)
1194 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1195 CONTENTS is nil. INFO is a plist holding contextual information."
1196 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1197 (org-element-property :value export-snippet
)))
1202 (defun org-ascii-fixed-width (fixed-width contents info
)
1203 "Transcode a FIXED-WIDTH element from Org to ASCII.
1204 CONTENTS is nil. INFO is a plist holding contextual information."
1205 (org-ascii--justify-element
1206 (org-ascii--box-string
1207 (org-remove-indentation
1208 (org-element-property :value fixed-width
)) info
)
1212 ;;;; Footnote Definition
1214 ;; Footnote Definitions are ignored. They are compiled at the end of
1215 ;; the document, by `org-ascii-inner-template'.
1218 ;;;; Footnote Reference
1220 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1221 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1222 CONTENTS is nil. INFO is a plist holding contextual information."
1223 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1228 (defun org-ascii-headline (headline contents info
)
1229 "Transcode a HEADLINE element from Org to ASCII.
1230 CONTENTS holds the contents of the headline. INFO is a plist
1231 holding contextual information."
1232 ;; Don't export footnote section, which will be handled at the end
1234 (unless (org-element-property :footnote-section-p headline
)
1235 (let* ((low-level-rank (org-export-low-level-p headline info
))
1236 (width (org-ascii--current-text-width headline info
))
1237 ;; Blank lines between headline and its contents.
1238 ;; `org-ascii-headline-spacing', when set, overwrites
1239 ;; original buffer's spacing.
1242 (or (car (plist-get info
:ascii-headline-spacing
))
1243 (org-element-property :pre-blank headline
))
1245 ;; Even if HEADLINE has no section, there might be some
1246 ;; links in its title that we shouldn't forget to describe.
1248 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1249 (let ((title (org-element-property :title headline
)))
1251 (org-ascii--describe-links
1252 (org-ascii--unique-links title info
) width info
))))))
1253 ;; Deep subtree: export it as a list item.
1257 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1258 (plist-get info
:ascii-bullets
)))))
1260 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1263 (org-ascii--build-title headline info width
) "\n"
1264 ;; Contents, indented by length of bullet.
1266 (org-ascii--indent-string
1268 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1270 ;; Else: Standard headline.
1272 (org-ascii--build-title headline info width
'underline
)
1274 (concat (when (org-string-nw-p links
) links
) contents
))))))
1277 ;;;; Horizontal Rule
1279 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1280 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1281 CONTENTS is nil. INFO is a plist holding contextual
1283 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1285 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1286 (org-ascii--justify-lines
1287 (make-string (if (and spec-width
(string-match "^[0-9]+$" spec-width
))
1288 (string-to-number spec-width
)
1290 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1291 text-width
'center
)))
1294 ;;;; Inline Src Block
1296 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1297 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1298 CONTENTS holds the contents of the item. INFO is a plist holding
1299 contextual information."
1300 (format (plist-get info
:ascii-verbatim-format
)
1301 (org-element-property :value inline-src-block
)))
1306 (defun org-ascii-format-inlinetask-default
1307 (todo type priority name tags contents width inlinetask info
)
1308 "Format an inline task element for ASCII export.
1309 See `org-ascii-format-inlinetask-function' for a description
1311 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1312 (width (or width
(plist-get info
:ascii-inlinetask-width
))))
1313 (org-ascii--indent-string
1315 ;; Top line, with an additional blank line if not in UTF-8.
1316 (make-string width
(if utf8p ?━ ?_
)) "\n"
1317 (unless utf8p
(concat (make-string width ?
) "\n"))
1318 ;; Add title. Fill it if wider than inlinetask.
1319 (let ((title (org-ascii--build-title inlinetask info width
)))
1320 (if (<= (string-width title
) width
) title
1321 (org-ascii--fill-string title width info
)))
1323 ;; If CONTENTS is not empty, insert it along with
1325 (when (org-string-nw-p contents
)
1326 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1328 (make-string width
(if utf8p ?━ ?_
)))
1329 ;; Flush the inlinetask to the right.
1330 (- (plist-get info
:ascii-text-width
) (plist-get info
:ascii-global-margin
)
1331 (if (not (org-export-get-parent-headline inlinetask
)) 0
1332 (plist-get info
:ascii-inner-margin
))
1333 (org-ascii--current-text-width inlinetask info
)))))
1335 (defun org-ascii-inlinetask (inlinetask contents info
)
1336 "Transcode an INLINETASK element from Org to ASCII.
1337 CONTENTS holds the contents of the block. INFO is a plist
1338 holding contextual information."
1339 (let ((width (org-ascii--current-text-width inlinetask info
)))
1340 (funcall (plist-get info
:ascii-format-inlinetask-function
)
1342 (and (plist-get info
:with-todo-keywords
)
1343 (let ((todo (org-element-property
1344 :todo-keyword inlinetask
)))
1345 (and todo
(org-export-data todo info
))))
1347 (org-element-property :todo-type inlinetask
)
1349 (and (plist-get info
:with-priority
)
1350 (org-element-property :priority inlinetask
))
1352 (org-export-data (org-element-property :title inlinetask
) info
)
1354 (and (plist-get info
:with-tags
)
1355 (org-element-property :tags inlinetask
))
1356 ;; contents and width
1357 contents width inlinetask info
)))
1362 (defun org-ascii-italic (italic contents info
)
1363 "Transcode italic from Org to ASCII.
1364 CONTENTS is the text with italic markup. INFO is a plist holding
1365 contextual information."
1366 (format "/%s/" contents
))
1371 (defun org-ascii-item (item contents info
)
1372 "Transcode an ITEM element from Org to ASCII.
1373 CONTENTS holds the contents of the item. INFO is a plist holding
1374 contextual information."
1375 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1376 (checkbox (org-ascii--checkbox item info
))
1377 (list-type (org-element-property :type
(org-export-get-parent item
)))
1379 ;; First parent of ITEM is always the plain-list. Get
1380 ;; `:type' property from it.
1381 (org-list-bullet-string
1385 (org-export-data (org-element-property :tag item
) info
)
1388 ;; Return correct number for ITEM, paying attention to
1390 (let* ((struct (org-element-property :structure item
))
1391 (bul (org-element-property :bullet item
))
1392 (num (number-to-string
1393 (car (last (org-list-get-item-number
1394 (org-element-property :begin item
)
1396 (org-list-prevs-alist struct
)
1397 (org-list-parents-alist struct
)))))))
1398 (replace-regexp-in-string "[0-9]+" num bul
)))
1399 (t (let ((bul (org-element-property :bullet item
)))
1400 ;; Change bullets into more visible form if UTF-8 is active.
1402 (replace-regexp-in-string
1404 (replace-regexp-in-string
1406 (replace-regexp-in-string "*" "‣" bul
))))))))))
1409 (unless (eq list-type
'descriptive
) checkbox
)
1410 ;; Contents: Pay attention to indentation. Note: check-boxes are
1411 ;; already taken care of at the paragraph level so they don't
1412 ;; interfere with indentation.
1413 (let ((contents (org-ascii--indent-string contents
(string-width bullet
))))
1414 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1416 (concat "\n" contents
))))))
1421 (defun org-ascii-keyword (keyword contents info
)
1422 "Transcode a KEYWORD element from Org to ASCII.
1423 CONTENTS is nil. INFO is a plist holding contextual
1425 (let ((key (org-element-property :key keyword
)))
1427 ((string= key
"ASCII")
1428 (org-ascii--justify-element
1429 (org-element-property :value keyword
) keyword info
))
1430 ((string= key
"TOC")
1431 (org-ascii--justify-element
1432 (let ((value (downcase (org-element-property :value keyword
))))
1434 ((string-match "\\<headlines\\>" value
)
1435 (let ((depth (or (and (string-match "[0-9]+" value
)
1436 (string-to-number (match-string 0 value
)))
1437 (plist-get info
:with-toc
))))
1438 (org-ascii--build-toc
1439 info
(and (wholenump depth
) depth
) keyword
)))
1440 ((string= "tables" value
)
1441 (org-ascii--list-tables keyword info
))
1442 ((string= "listings" value
)
1443 (org-ascii--list-listings keyword info
))))
1447 ;;;; Latex Environment
1449 (defun org-ascii-latex-environment (latex-environment contents info
)
1450 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1451 CONTENTS is nil. INFO is a plist holding contextual
1453 (when (plist-get info
:with-latex
)
1454 (org-ascii--justify-element
1455 (org-remove-indentation (org-element-property :value latex-environment
))
1456 latex-environment info
)))
1461 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1462 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1463 CONTENTS is nil. INFO is a plist holding contextual
1465 (when (plist-get info
:with-latex
)
1466 (org-element-property :value latex-fragment
)))
1471 (defun org-ascii-line-break (line-break contents info
)
1472 "Transcode a LINE-BREAK object from Org to ASCII.
1473 CONTENTS is nil. INFO is a plist holding contextual
1474 information." hard-newline
)
1479 (defun org-ascii-link (link desc info
)
1480 "Transcode a LINK object from Org to ASCII.
1482 DESC is the description part of the link, or the empty string.
1483 INFO is a plist holding contextual information."
1484 (let ((raw-link (org-element-property :raw-link link
))
1485 (type (org-element-property :type link
)))
1487 ((string= type
"coderef")
1488 (let ((ref (org-element-property :path link
)))
1489 (format (org-export-get-coderef-format ref desc
)
1490 (org-export-resolve-coderef ref info
))))
1491 ;; Do not apply a special syntax on radio links. Though, use
1492 ;; transcoded target's contents as output.
1493 ((string= type
"radio") desc
)
1494 ;; Do not apply a special syntax on fuzzy links pointing to
1496 ((string= type
"fuzzy")
1497 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1498 (if (org-string-nw-p desc
) desc
1501 (org-export-get-ordinal
1502 destination info nil
'org-ascii--has-caption-p
)))
1504 (if (atom number
) (number-to-string number
)
1505 (mapconcat 'number-to-string number
"."))))))))
1507 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1508 (concat (format "[%s]" desc
)
1509 (and (not (plist-get info
:ascii-links-to-notes
))
1510 (format " (%s)" raw-link
))))))))
1513 ;;;; Node Properties
1515 (defun org-ascii-node-property (node-property contents info
)
1516 "Transcode a NODE-PROPERTY element from Org to ASCII.
1517 CONTENTS is nil. INFO is a plist holding contextual
1520 (org-element-property :key node-property
)
1521 (let ((value (org-element-property :value node-property
)))
1522 (if value
(concat " " value
) ""))))
1527 (defun org-ascii-paragraph (paragraph contents info
)
1528 "Transcode a PARAGRAPH element from Org to ASCII.
1529 CONTENTS is the contents of the paragraph, as a string. INFO is
1530 the plist used as a communication channel."
1531 (org-ascii--justify-element
1532 (let ((indented-line-width (plist-get info
:ascii-indented-line-width
)))
1533 (if (not (wholenump indented-line-width
)) contents
1535 ;; Do not indent first paragraph in a section.
1536 (unless (and (not (org-export-get-previous-element paragraph info
))
1537 (eq (org-element-type (org-export-get-parent paragraph
))
1539 (make-string indented-line-width ?\s
))
1540 (replace-regexp-in-string "\\`[ \t]+" "" contents
))))
1546 (defun org-ascii-plain-list (plain-list contents info
)
1547 "Transcode a PLAIN-LIST element from Org to ASCII.
1548 CONTENTS is the contents of the list. INFO is a plist holding
1549 contextual information."
1555 (defun org-ascii-plain-text (text info
)
1556 "Transcode a TEXT string from Org to ASCII.
1557 INFO is a plist used as a communication channel."
1558 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1559 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1560 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1561 (if (not (plist-get info
:with-special-strings
)) text
1562 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1563 (if (not utf8p
) text
1564 ;; Usual replacements in utf-8 with proper option set.
1565 (replace-regexp-in-string
1567 (replace-regexp-in-string
1569 (replace-regexp-in-string "---" "—" text
)))))))
1574 (defun org-ascii-planning (planning contents info
)
1575 "Transcode a PLANNING element from Org to ASCII.
1576 CONTENTS is nil. INFO is a plist used as a communication
1578 (org-ascii--justify-element
1582 (list (let ((closed (org-element-property :closed planning
)))
1584 (concat org-closed-string
" "
1586 (org-element-property :raw-value closed
)))))
1587 (let ((deadline (org-element-property :deadline planning
)))
1589 (concat org-deadline-string
" "
1591 (org-element-property :raw-value deadline
)))))
1592 (let ((scheduled (org-element-property :scheduled planning
)))
1594 (concat org-scheduled-string
" "
1596 (org-element-property :raw-value scheduled
)))))))
1601 ;;;; Property Drawer
1603 (defun org-ascii-property-drawer (property-drawer contents info
)
1604 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1605 CONTENTS holds the contents of the drawer. INFO is a plist
1606 holding contextual information."
1607 (and (org-string-nw-p contents
)
1608 (org-ascii--justify-element contents property-drawer info
)))
1613 (defun org-ascii-quote-block (quote-block contents info
)
1614 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1615 CONTENTS holds the contents of the block. INFO is a plist
1616 holding contextual information."
1617 (org-ascii--indent-string contents
(plist-get info
:ascii-quote-margin
)))
1622 (defun org-ascii-radio-target (radio-target contents info
)
1623 "Transcode a RADIO-TARGET object from Org to ASCII.
1624 CONTENTS is the contents of the target. INFO is a plist holding
1625 contextual information."
1631 (defun org-ascii-section (section contents info
)
1632 "Transcode a SECTION element from Org to ASCII.
1633 CONTENTS is the contents of the section. INFO is a plist holding
1634 contextual information."
1635 (org-ascii--indent-string
1638 (when (plist-get info
:ascii-links-to-notes
)
1639 ;; Add list of links at the end of SECTION.
1640 (let ((links (org-ascii--describe-links
1641 (org-ascii--unique-links section info
)
1642 (org-ascii--current-text-width section info
) info
)))
1643 ;; Separate list of links and section contents.
1644 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1645 ;; Do not apply inner margin if parent headline is low level.
1646 (let ((headline (org-export-get-parent-headline section
)))
1647 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1648 (plist-get info
:ascii-inner-margin
)))))
1653 (defun org-ascii-special-block (special-block contents info
)
1654 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1655 CONTENTS holds the contents of the block. INFO is a plist
1656 holding contextual information."
1657 (if (org-export-raw-special-block-p special-block info
)
1658 (org-ascii--justify-element
1659 (org-element-property :raw-value special-block
) special-block info
)
1660 ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
1661 ;; at a lower level. There is no other special block type to
1668 (defun org-ascii-src-block (src-block contents info
)
1669 "Transcode a SRC-BLOCK element from Org to ASCII.
1670 CONTENTS holds the contents of the item. INFO is a plist holding
1671 contextual information."
1672 (let ((caption (org-ascii--build-caption src-block info
))
1673 (caption-above-p (plist-get info
:ascii-caption-above
))
1674 (code (org-export-format-code-default src-block info
)))
1675 (if (equal code
"") ""
1676 (org-ascii--justify-element
1678 (and caption caption-above-p
(concat caption
"\n"))
1679 (org-ascii--box-string code info
)
1680 (and caption
(not caption-above-p
) (concat "\n" caption
)))
1684 ;;;; Statistics Cookie
1686 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1687 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1688 CONTENTS is nil. INFO is a plist holding contextual information."
1689 (org-element-property :value statistics-cookie
))
1694 (defun org-ascii-subscript (subscript contents info
)
1695 "Transcode a SUBSCRIPT object from Org to ASCII.
1696 CONTENTS is the contents of the object. INFO is a plist holding
1697 contextual information."
1698 (if (org-element-property :use-brackets-p subscript
)
1699 (format "_{%s}" contents
)
1700 (format "_%s" contents
)))
1705 (defun org-ascii-superscript (superscript contents info
)
1706 "Transcode a SUPERSCRIPT object from Org to ASCII.
1707 CONTENTS is the contents of the object. INFO is a plist holding
1708 contextual information."
1709 (if (org-element-property :use-brackets-p superscript
)
1710 (format "^{%s}" contents
)
1711 (format "^%s" contents
)))
1716 (defun org-ascii-strike-through (strike-through contents info
)
1717 "Transcode STRIKE-THROUGH from Org to ASCII.
1718 CONTENTS is text with strike-through markup. INFO is a plist
1719 holding contextual information."
1720 (format "+%s+" contents
))
1725 (defun org-ascii-table (table contents info
)
1726 "Transcode a TABLE element from Org to ASCII.
1727 CONTENTS is the contents of the table. INFO is a plist holding
1728 contextual information."
1729 (let ((caption (org-ascii--build-caption table info
))
1730 (caption-above-p (plist-get info
:ascii-caption-above
)))
1731 (org-ascii--justify-element
1733 ;; Possibly add a caption string above.
1734 (and caption caption-above-p
(concat caption
"\n"))
1735 ;; Insert table. Note: "table.el" tables are left unmodified.
1736 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1737 ((and (plist-get info
:ascii-table-use-ascii-art
)
1738 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1739 (require 'ascii-art-to-unicode nil t
))
1741 (insert (org-remove-indentation
1742 (org-element-property :value table
)))
1743 (goto-char (point-min))
1745 (goto-char (point-max))
1746 (skip-chars-backward " \r\t\n")
1747 (buffer-substring (point-min) (point))))
1748 (t (org-remove-indentation (org-element-property :value table
))))
1749 ;; Possible add a caption string below.
1750 (and (not caption-above-p
) caption
))
1756 (defun org-ascii--table-cell-width (table-cell info
)
1757 "Return width of TABLE-CELL.
1759 INFO is a plist used as a communication channel.
1761 Width of a cell is determined either by a width cookie in the
1762 same column as the cell, or by the maximum cell's length in that
1765 When `org-ascii-table-widen-columns' is non-nil, width cookies
1767 (let* ((row (org-export-get-parent table-cell
))
1768 (table (org-export-get-parent row
))
1769 (col (let ((cells (org-element-contents row
)))
1770 (- (length cells
) (length (memq table-cell cells
)))))
1772 (or (plist-get info
:ascii-table-cell-width-cache
)
1773 (plist-get (setq info
1774 (plist-put info
:ascii-table-cell-width-cache
1775 (make-hash-table :test
'equal
)))
1776 :ascii-table-cell-width-cache
)))
1777 (key (cons table col
))
1778 (widenp (plist-get info
:ascii-table-widen-columns
)))
1779 (or (gethash key cache
)
1782 (let ((cookie-width (org-export-table-cell-width table-cell info
)))
1783 (or (and (not widenp
) cookie-width
)
1784 (let ((contents-width
1785 (let ((max-width 0))
1786 (org-element-map table
'table-row
1791 (org-element-contents
1792 (elt (org-element-contents row
) col
))
1797 (cond ((not cookie-width
) contents-width
)
1798 (widenp (max cookie-width contents-width
))
1799 (t cookie-width
)))))
1802 (defun org-ascii-table-cell (table-cell contents info
)
1803 "Transcode a TABLE-CELL object from Org to ASCII.
1804 CONTENTS is the cell contents. INFO is a plist used as
1805 a communication channel."
1806 ;; Determine column width. When `org-ascii-table-widen-columns'
1807 ;; is nil and some width cookie has set it, use that value.
1808 ;; Otherwise, compute the maximum width among transcoded data of
1809 ;; each cell in the column.
1810 (let ((width (org-ascii--table-cell-width table-cell info
)))
1811 ;; When contents are too large, truncate them.
1812 (unless (or (plist-get info
:ascii-table-widen-columns
)
1813 (<= (string-width (or contents
"")) width
))
1814 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1815 ;; Align contents correctly within the cell.
1816 (let* ((indent-tabs-mode nil
)
1819 (org-ascii--justify-lines
1821 (org-export-table-cell-alignment table-cell info
)))))
1824 (make-string (- width
(string-width (or data
""))) ?\s
))))
1826 (concat (format " %s " contents
)
1827 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1828 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1833 (defun org-ascii-table-row (table-row contents info
)
1834 "Transcode a TABLE-ROW element from Org to ASCII.
1835 CONTENTS is the row contents. INFO is a plist used as
1836 a communication channel."
1837 (when (eq (org-element-property :type table-row
) 'standard
)
1840 (lambda (lcorner horiz vert rcorner
)
1844 (org-element-map table-row
'table-cell
1846 (let ((width (org-ascii--table-cell-width cell info
))
1847 (borders (org-export-table-cell-borders cell info
)))
1849 ;; In order to know if CELL starts the row, do
1850 ;; not compare it with the first cell in the
1851 ;; row as there might be a special column.
1852 ;; Instead, compare it with first exportable
1853 ;; cell, obtained with `org-element-map'.
1854 (when (and (memq 'left borders
)
1855 (eq (org-element-map table-row
'table-cell
1859 (make-string (+ 2 width
) (string-to-char horiz
))
1861 ((not (memq 'right borders
)) nil
)
1862 ((eq (car (last (org-element-contents table-row
))) cell
)
1866 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1867 (borders (org-export-table-cell-borders
1868 (org-element-map table-row
'table-cell
'identity info t
)
1871 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1872 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1873 (funcall build-hline
"+" "-" "+" "+")))
1874 ((memq 'above borders
)
1875 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1876 (funcall build-hline
"+" "-" "+" "+"))))
1877 (when (memq 'left borders
) (if utf8p
"│" "|"))
1879 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1880 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1881 (funcall build-hline
"+" "-" "+" "+")))))))
1886 (defun org-ascii-timestamp (timestamp contents info
)
1887 "Transcode a TIMESTAMP object from Org to ASCII.
1888 CONTENTS is nil. INFO is a plist holding contextual information."
1889 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1894 (defun org-ascii-underline (underline contents info
)
1895 "Transcode UNDERLINE from Org to ASCII.
1896 CONTENTS is the text with underline markup. INFO is a plist
1897 holding contextual information."
1898 (format "_%s_" contents
))
1903 (defun org-ascii-verbatim (verbatim contents info
)
1904 "Return a VERBATIM object from Org to ASCII.
1905 CONTENTS is nil. INFO is a plist holding contextual information."
1906 (format (plist-get info
:ascii-verbatim-format
)
1907 (org-element-property :value verbatim
)))
1912 (defun org-ascii-verse-block (verse-block contents info
)
1913 "Transcode a VERSE-BLOCK element from Org to ASCII.
1914 CONTENTS is verse block contents. INFO is a plist holding
1915 contextual information."
1916 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1917 (org-ascii--indent-string
1918 (org-ascii--justify-element contents verse-block info
)
1919 (plist-get info
:ascii-quote-margin
))))
1925 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1926 "Filter controlling number of blank lines after a headline.
1928 HEADLINE is a string representing a transcoded headline.
1929 BACK-END is symbol specifying back-end used for export. INFO is
1930 plist containing the communication channel.
1932 This function only applies to `ascii' back-end. See
1933 `org-ascii-headline-spacing' for information."
1934 (let ((headline-spacing (plist-get info
:ascii-headline-spacing
)))
1935 (if (not headline-spacing
) headline
1936 (let ((blanks (make-string (1+ (cdr headline-spacing
)) ?
\n)))
1937 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
)))))
1939 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1940 "Filter controlling number of blank lines between paragraphs.
1942 TREE is the parse tree. BACK-END is the symbol specifying
1943 back-end used for export. INFO is a plist used as
1944 a communication channel.
1946 See `org-ascii-paragraph-spacing' for information."
1947 (let ((paragraph-spacing (plist-get info
:ascii-paragraph-spacing
)))
1948 (when (wholenump paragraph-spacing
)
1949 (org-element-map tree
'paragraph
1951 (when (eq (org-element-type (org-export-get-next-element p info
))
1953 (org-element-put-property p
:post-blank paragraph-spacing
))))))
1956 (defun org-ascii-filter-comment-spacing (tree backend info
)
1957 "Filter removing blank lines between comments.
1958 TREE is the parse tree. BACK-END is the symbol specifying
1959 back-end used for export. INFO is a plist used as
1960 a communication channel."
1961 (org-element-map tree
'(comment comment-block
)
1963 (when (memq (org-element-type (org-export-get-next-element c info
))
1964 '(comment comment-block
))
1965 (org-element-put-property c
:post-blank
0))))
1970 ;;; End-user functions
1973 (defun org-ascii-export-as-ascii
1974 (&optional async subtreep visible-only body-only ext-plist
)
1975 "Export current buffer to a text buffer.
1977 If narrowing is active in the current buffer, only export its
1980 If a region is active, export that region.
1982 A non-nil optional argument ASYNC means the process should happen
1983 asynchronously. The resulting buffer should be accessible
1984 through the `org-export-stack' interface.
1986 When optional argument SUBTREEP is non-nil, export the sub-tree
1987 at point, extracting information from the headline properties
1990 When optional argument VISIBLE-ONLY is non-nil, don't export
1991 contents of hidden elements.
1993 When optional argument BODY-ONLY is non-nil, strip title and
1994 table of contents from output.
1996 EXT-PLIST, when provided, is a property list with external
1997 parameters overriding Org default settings, but still inferior to
1998 file-local settings.
2000 Export is done in a buffer named \"*Org ASCII Export*\", which
2001 will be displayed when `org-export-show-temporary-export-buffer'
2004 (org-export-to-buffer 'ascii
"*Org ASCII Export*"
2005 async subtreep visible-only body-only ext-plist
(lambda () (text-mode))))
2008 (defun org-ascii-export-to-ascii
2009 (&optional async subtreep visible-only body-only ext-plist
)
2010 "Export current buffer to a text file.
2012 If narrowing is active in the current buffer, only export its
2015 If a region is active, export that region.
2017 A non-nil optional argument ASYNC means the process should happen
2018 asynchronously. The resulting file should be accessible through
2019 the `org-export-stack' interface.
2021 When optional argument SUBTREEP is non-nil, export the sub-tree
2022 at point, extracting information from the headline properties
2025 When optional argument VISIBLE-ONLY is non-nil, don't export
2026 contents of hidden elements.
2028 When optional argument BODY-ONLY is non-nil, strip title and
2029 table of contents from output.
2031 EXT-PLIST, when provided, is a property list with external
2032 parameters overriding Org default settings, but still inferior to
2033 file-local settings.
2035 Return output file's name."
2037 (let ((file (org-export-output-file-name ".txt" subtreep
)))
2038 (org-export-to-file 'ascii file
2039 async subtreep visible-only body-only ext-plist
)))
2042 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
2043 "Publish an Org file to ASCII.
2045 FILENAME is the filename of the Org file to be published. PLIST
2046 is the property list for the given project. PUB-DIR is the
2047 publishing directory.
2049 Return output file name."
2051 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
2054 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
2055 "Publish an Org file to Latin-1.
2057 FILENAME is the filename of the Org file to be published. PLIST
2058 is the property list for the given project. PUB-DIR is the
2059 publishing directory.
2061 Return output file name."
2063 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
2066 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
2067 "Publish an org file to UTF-8.
2069 FILENAME is the filename of the Org file to be published. PLIST
2070 is the property list for the given project. PUB-DIR is the
2071 publishing directory.
2073 Return output file name."
2075 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
2081 ;; generated-autoload-file: "org-loaddefs.el"
2082 ;; coding: utf-8-emacs
2085 ;;; ox-ascii.el ends here