1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 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.
25 ;; It provides two commands for export, depending on the desired
26 ;; output: `org-ascii-export-as-ascii' (temporary buffer) and
27 ;; `org-ascii-export-to-ascii' ("txt" file). Also, three publishing
28 ;; functions are available: `org-ascii-publish-to-ascii',
29 ;; `org-ascii-publish-to-latin1' and `org-ascii-publish-to-utf8'.
31 ;; Output encoding is specified through `org-ascii-charset' variable,
32 ;; among `ascii', `latin1' and `utf-8' symbols.
34 ;; By default, horizontal rules span over the full text with, but with
35 ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
36 ;; they can be shortened and centered.
40 (eval-when-compile (require 'cl
))
44 (declare-function aa2u
"ext:ascii-art-to-unicode" ())
48 ;; The following setting won't allow to modify preferred charset
49 ;; through a buffer keyword or an option item, but, since the property
50 ;; will appear in communication channel nonetheless, it allows to
51 ;; override `org-ascii-charset' variable on the fly by the ext-plist
54 ;; We also install a filter for headlines and sections, in order to
55 ;; control blank lines separating them in output string.
57 (org-export-define-backend ascii
58 ((bold . org-ascii-bold
)
59 (center-block . org-ascii-center-block
)
60 (clock . org-ascii-clock
)
61 (code . org-ascii-code
)
62 (drawer . org-ascii-drawer
)
63 (dynamic-block . org-ascii-dynamic-block
)
64 (entity . org-ascii-entity
)
65 (example-block . org-ascii-example-block
)
66 (export-block . org-ascii-export-block
)
67 (export-snippet . org-ascii-export-snippet
)
68 (fixed-width . org-ascii-fixed-width
)
69 (footnote-definition . org-ascii-footnote-definition
)
70 (footnote-reference . org-ascii-footnote-reference
)
71 (headline . org-ascii-headline
)
72 (horizontal-rule . org-ascii-horizontal-rule
)
73 (inline-src-block . org-ascii-inline-src-block
)
74 (inlinetask . org-ascii-inlinetask
)
75 (italic . org-ascii-italic
)
76 (item . org-ascii-item
)
77 (keyword . org-ascii-keyword
)
78 (latex-environment . org-ascii-latex-environment
)
79 (latex-fragment . org-ascii-latex-fragment
)
80 (line-break . org-ascii-line-break
)
81 (link . org-ascii-link
)
82 (paragraph . org-ascii-paragraph
)
83 (plain-list . org-ascii-plain-list
)
84 (plain-text . org-ascii-plain-text
)
85 (planning . org-ascii-planning
)
86 (quote-block . org-ascii-quote-block
)
87 (quote-section . org-ascii-quote-section
)
88 (radio-target . org-ascii-radio-target
)
89 (section . org-ascii-section
)
90 (special-block . org-ascii-special-block
)
91 (src-block . org-ascii-src-block
)
92 (statistics-cookie . org-ascii-statistics-cookie
)
93 (strike-through . org-ascii-strike-through
)
94 (subscript . org-ascii-subscript
)
95 (superscript . org-ascii-superscript
)
96 (table . org-ascii-table
)
97 (table-cell . org-ascii-table-cell
)
98 (table-row . org-ascii-table-row
)
99 (target . org-ascii-target
)
100 (template . org-ascii-template
)
101 (timestamp . org-ascii-timestamp
)
102 (underline . org-ascii-underline
)
103 (verbatim . org-ascii-verbatim
)
104 (verse-block . org-ascii-verse-block
))
105 :export-block
"ASCII"
107 (?t
"Export to Plain Text"
108 ((?A
"As ASCII buffer"
110 (org-ascii-export-as-ascii a s v b
'(:ascii-charset ascii
))))
113 (org-ascii-export-to-ascii a s v b
'(:ascii-charset ascii
))))
114 (?L
"As Latin1 buffer"
116 (org-ascii-export-as-ascii a s v b
'(:ascii-charset latin1
))))
119 (org-ascii-export-to-ascii a s v b
'(:ascii-charset latin1
))))
120 (?U
"As UTF-8 buffer"
122 (org-ascii-export-as-ascii a s v b
'(:ascii-charset utf-8
))))
125 (org-ascii-export-to-ascii a s v b
'(:ascii-charset utf-8
))))))
126 :filters-alist
((:filter-headline . org-ascii-filter-headline-blank-lines
)
127 (:filter-parse-tree . org-ascii-filter-paragraph-spacing
)
128 (:filter-section . org-ascii-filter-headline-blank-lines
))
129 :options-alist
((:ascii-charset nil nil org-ascii-charset
)))
133 ;;; User Configurable Variables
135 (defgroup org-export-ascii nil
136 "Options for exporting Org mode files to ASCII."
137 :tag
"Org Export ASCII"
140 (defcustom org-ascii-text-width
72
141 "Maximum width of exported text.
142 This number includes margin size, as set in
143 `org-ascii-global-margin'."
144 :group
'org-export-ascii
147 (defcustom org-ascii-global-margin
0
148 "Width of the left margin, in number of characters."
149 :group
'org-export-ascii
152 (defcustom org-ascii-inner-margin
2
153 "Width of the inner margin, in number of characters.
154 Inner margin is applied between each headline."
155 :group
'org-export-ascii
158 (defcustom org-ascii-quote-margin
6
159 "Width of margin used for quoting text, in characters.
160 This margin is applied on both sides of the text."
161 :group
'org-export-ascii
164 (defcustom org-ascii-inlinetask-width
30
165 "Width of inline tasks, in number of characters.
166 This number ignores any margin."
167 :group
'org-export-ascii
170 (defcustom org-ascii-headline-spacing
'(1 .
2)
171 "Number of blank lines inserted around headlines.
173 This variable can be set to a cons cell. In that case, its car
174 represents the number of blank lines present before headline
175 contents whereas its cdr reflects the number of blank lines after
178 A nil value replicates the number of blank lines found in the
179 original Org buffer at the same place."
180 :group
'org-export-ascii
182 (const :tag
"Replicate original spacing" nil
)
183 (cons :tag
"Set an uniform spacing"
184 (integer :tag
"Number of blank lines before contents")
185 (integer :tag
"Number of blank lines after contents"))))
187 (defcustom org-ascii-indented-line-width
'auto
188 "Additional indentation width for the first line in a paragraph.
189 If the value is an integer, indent the first line of each
190 paragraph by this number. If it is the symbol `auto' preserve
191 indentation from original document."
192 :group
'org-export-ascii
194 (integer :tag
"Number of white spaces characters")
195 (const :tag
"Preserve original width" auto
)))
197 (defcustom org-ascii-paragraph-spacing
'auto
198 "Number of white lines between paragraphs.
199 If the value is an integer, add this number of blank lines
200 between contiguous paragraphs. If is it the symbol `auto', keep
201 the same number of blank lines as in the original document."
202 :group
'org-export-ascii
204 (integer :tag
"Number of blank lines")
205 (const :tag
"Preserve original spacing" auto
)))
207 (defcustom org-ascii-charset
'ascii
208 "The charset allowed to represent various elements and objects.
210 `ascii' Only use plain ASCII characters
211 `latin1' Include Latin-1 characters
212 `utf-8' Use all UTF-8 characters"
213 :group
'org-export-ascii
215 (const :tag
"ASCII" ascii
)
216 (const :tag
"Latin-1" latin1
)
217 (const :tag
"UTF-8" utf-8
)))
219 (defcustom org-ascii-underline
'((ascii ?
= ?~ ?-
)
221 (utf-8 ?═ ?─ ?╌ ?┄ ?┈
))
222 "Characters for underlining headings in ASCII export.
224 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
225 and whose value is a list of characters.
227 For each supported charset, this variable associates a sequence
228 of underline characters. In a sequence, the characters will be
229 used in order for headlines level 1, 2, ... If no character is
230 available for a given level, the headline won't be underlined."
231 :group
'org-export-ascii
233 (cons :tag
"Underline characters sequence"
234 (const :tag
"ASCII charset" ascii
)
236 (cons :tag
"Underline characters sequence"
237 (const :tag
"Latin-1 charset" latin1
)
239 (cons :tag
"Underline characters sequence"
240 (const :tag
"UTF-8 charset" utf-8
)
241 (repeat character
))))
243 (defcustom org-ascii-bullets
'((ascii ?
* ?
+ ?-
)
246 "Bullet characters for headlines converted to lists in ASCII export.
248 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
249 and whose value is a list of characters.
251 The first character is used for the first level considered as low
252 level, and so on. If there are more levels than characters given
253 here, the list will be repeated.
255 Note that this variable doesn't affect plain lists
257 :group
'org-export-ascii
259 (cons :tag
"Bullet characters for low level headlines"
260 (const :tag
"ASCII charset" ascii
)
262 (cons :tag
"Bullet characters for low level headlines"
263 (const :tag
"Latin-1 charset" latin1
)
265 (cons :tag
"Bullet characters for low level headlines"
266 (const :tag
"UTF-8 charset" utf-8
)
267 (repeat character
))))
269 (defcustom org-ascii-links-to-notes t
270 "Non-nil means convert links to notes before the next headline.
271 When nil, the link will be exported in place. If the line
272 becomes long in this way, it will be wrapped."
273 :group
'org-export-ascii
276 (defcustom org-ascii-table-keep-all-vertical-lines nil
277 "Non-nil means keep all vertical lines in ASCII tables.
278 When nil, vertical lines will be removed except for those needed
279 for column grouping."
280 :group
'org-export-ascii
283 (defcustom org-ascii-table-widen-columns t
284 "Non-nil means widen narrowed columns for export.
285 When nil, narrowed columns will look in ASCII export just like in
286 Org mode, i.e. with \"=>\" as ellipsis."
287 :group
'org-export-ascii
290 (defcustom org-ascii-table-use-ascii-art nil
291 "Non-nil means table.el tables are turned into ascii-art.
293 It only makes sense when export charset is `utf-8'. It is nil by
294 default since it requires ascii-art-to-unicode.el package. You
295 can download it here:
297 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
299 (defcustom org-ascii-caption-above nil
300 "When non-nil, place caption string before the element.
301 Otherwise, place it right after it."
302 :group
'org-export-ascii
305 (defcustom org-ascii-verbatim-format
"`%s'"
306 "Format string used for verbatim text and inline code."
307 :group
'org-export-ascii
310 (defcustom org-ascii-format-drawer-function nil
311 "Function called to format a drawer in ASCII.
313 The function must accept three parameters:
314 NAME the drawer name, like \"LOGBOOK\"
315 CONTENTS the contents of the drawer.
316 WIDTH the text width within the drawer.
318 The function should return either the string to be exported or
319 nil to ignore the drawer.
321 For example, the variable could be set to the following function
322 in order to mimic default behaviour:
324 \(defun org-ascii-format-drawer-default (name contents width)
325 \"Format a drawer element for ASCII export.\"
327 :group
'org-export-ascii
330 (defcustom org-ascii-format-inlinetask-function nil
331 "Function called to format an inlinetask in ASCII.
333 The function must accept six parameters:
334 TODO the todo keyword, as a string
335 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
336 PRIORITY the inlinetask priority, as a string
337 NAME the inlinetask name, as a string.
338 TAGS the inlinetask tags, as a list of strings.
339 CONTENTS the contents of the inlinetask, as a string.
341 The function should return either the string to be exported or
342 nil to ignore the inline task.
344 For example, the variable could be set to the following function
345 in order to mimic default behaviour:
347 \(defun org-ascii-format-inlinetask-default
348 \(todo type priority name tags contents\)
349 \"Format an inline task element for ASCII export.\"
350 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
351 \(width org-ascii-inlinetask-width\)
352 \(org-ascii--indent-string
354 ;; Top line, with an additional blank line if not in UTF-8.
355 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
356 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
357 ;; Add title. Fill it if wider than inlinetask.
358 \(let \(\(title \(org-ascii--build-title inlinetask info width\)\)\)
359 \(if \(<= \(length title\) width\) title
360 \(org-ascii--fill-string title width info\)\)\)
362 ;; If CONTENTS is not empty, insert it along with
364 \(when \(org-string-nw-p contents\)
365 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
367 \(make-string width \(if utf8p ?━ ?_\)\)\)
368 ;; Flush the inlinetask to the right.
369 \(- \(plist-get info :ascii-width\)
370 \(plist-get info :ascii-margin\)
371 \(plist-get info :ascii-inner-margin\)
372 \(org-ascii--current-text-width inlinetask info\)\)"
373 :group
'org-export-ascii
378 ;;; Internal Functions
380 ;; Internal functions fall into three categories.
382 ;; The first one is about text formatting. The core function is
383 ;; `org-ascii--current-text-width', which determines the current
384 ;; text width allowed to a given element. In other words, it helps
385 ;; keeping each line width within maximum text width defined in
386 ;; `org-ascii-text-width'. Once this information is known,
387 ;; `org-ascii--fill-string', `org-ascii--justify-string',
388 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
389 ;; operate on a given output string.
391 ;; The second category contains functions handling elements listings,
392 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
393 ;; returns a complete table of contents, `org-ascii--list-listings'
394 ;; returns a list of referenceable src-block elements, and
395 ;; `org-ascii--list-tables' does the same for table elements.
397 ;; The third category includes general helper functions.
398 ;; `org-ascii--build-title' creates the title for a given headline
399 ;; or inlinetask element. `org-ascii--build-caption' returns the
400 ;; caption string associated to a table or a src-block.
401 ;; `org-ascii--describe-links' creates notes about links for
402 ;; insertion at the end of a section. It uses
403 ;; `org-ascii--unique-links' to get the list of links to describe.
404 ;; Eventually, `org-ascii--translate' translates a string according
405 ;; to language and charset specification.
408 (defun org-ascii--fill-string (s text-width info
&optional justify
)
409 "Fill a string with specified text-width and return it.
411 S is the string being filled. TEXT-WIDTH is an integer
412 specifying maximum length of a line. INFO is the plist used as
413 a communication channel.
415 Optional argument JUSTIFY can specify any type of justification
416 among `left', `center', `right' or `full'. A nil value is
417 equivalent to `left'. For a justification that doesn't also fill
418 string, see `org-ascii--justify-string'.
420 Return nil if S isn't a string."
421 ;; Don't fill paragraph when break should be preserved.
422 (cond ((not (stringp s
)) nil
)
423 ((plist-get info
:preserve-breaks
) s
)
424 (t (let ((double-space-p sentence-end-double-space
))
426 (let ((fill-column text-width
)
427 (use-hard-newlines t
)
428 (sentence-end-double-space double-space-p
))
430 (fill-region (point-min) (point-max) justify
))
433 (defun org-ascii--justify-string (s text-width how
)
435 TEXT-WIDTH is an integer specifying maximum length of a line.
436 HOW determines the type of justification: it can be `left',
437 `right', `full' or `center'."
440 (goto-char (point-min))
441 (let ((fill-column text-width
)
442 ;; Disable `adaptive-fill-mode' so it doesn't prevent
443 ;; filling lines matching `adaptive-fill-regexp'.
444 (adaptive-fill-mode nil
))
445 (while (< (point) (point-max))
446 (justify-current-line how
)
450 (defun org-ascii--indent-string (s width
)
451 "Indent string S by WIDTH white spaces.
452 Empty lines are not indented."
454 (replace-regexp-in-string
455 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ?
) s nil nil
1)))
457 (defun org-ascii--box-string (s info
)
458 "Return string S with a partial box to its left.
459 INFO is a plist used as a communicaton channel."
460 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
461 (format (if utf8p
"╭────\n%s\n╰────" ",----\n%s\n`----")
462 (replace-regexp-in-string
463 "^" (if utf8p
"│ " "| ")
464 ;; Remove last newline character.
465 (replace-regexp-in-string "\n[ \t]*\\'" "" s
)))))
467 (defun org-ascii--current-text-width (element info
)
468 "Return maximum text width for ELEMENT's contents.
469 INFO is a plist used as a communication channel."
470 (case (org-element-type element
)
471 ;; Elements with an absolute width: `headline' and `inlinetask'.
472 (inlinetask org-ascii-inlinetask-width
)
474 (- org-ascii-text-width
475 (let ((low-level-rank (org-export-low-level-p element info
)))
476 (if low-level-rank
(* low-level-rank
2) org-ascii-global-margin
))))
477 ;; Elements with a relative width: store maximum text width in
480 (let* ((genealogy (cons element
(org-export-get-genealogy element
)))
481 ;; Total width is determined by the presence, or not, of an
482 ;; inline task among ELEMENT parents.
484 (if (loop for parent in genealogy
485 thereis
(eq (org-element-type parent
) 'inlinetask
))
486 org-ascii-inlinetask-width
487 ;; No inlinetask: Remove global margin from text width.
488 (- org-ascii-text-width
489 org-ascii-global-margin
490 (let ((parent (org-export-get-parent-headline element
)))
491 ;; Inner margin doesn't apply to text before first
494 (let ((low-level-rank
495 (org-export-low-level-p parent info
)))
496 ;; Inner margin doesn't apply to contents of
497 ;; low level headlines, since they've got their
498 ;; own indentation mechanism.
499 (if low-level-rank
(* low-level-rank
2)
500 org-ascii-inner-margin
))))))))
502 ;; Each `quote-block', `quote-section' and `verse-block' above
503 ;; narrows text width by twice the standard margin size.
504 (+ (* (loop for parent in genealogy
505 when
(memq (org-element-type parent
)
506 '(quote-block quote-section verse-block
))
508 2 org-ascii-quote-margin
)
509 ;; Text width within a plain-list is restricted by
510 ;; indentation of current item. If that's the case,
511 ;; compute it with the help of `:structure' property from
512 ;; parent item, if any.
514 (if (eq (org-element-type element
) 'item
) element
515 (loop for parent in genealogy
516 when
(eq (org-element-type parent
) 'item
)
518 (if (not parent-item
) 0
519 ;; Compute indentation offset of the current item,
520 ;; that is the sum of the difference between its
521 ;; indentation and the indentation of the top item in
522 ;; the list and current item bullet's length. Also
523 ;; remove checkbox length, and tag length (for
524 ;; description lists) or bullet length.
525 (let ((struct (org-element-property :structure parent-item
))
526 (beg-item (org-element-property :begin parent-item
)))
527 (+ (- (org-list-get-ind beg-item struct
)
529 (org-list-get-top-point struct
) struct
))
530 (length (org-ascii--checkbox parent-item info
))
532 (or (org-list-get-tag beg-item struct
)
533 (org-list-get-bullet beg-item struct
)))))))))))))
535 (defun org-ascii--build-title
536 (element info text-width
&optional underline notags
)
537 "Format ELEMENT title and return it.
539 ELEMENT is either an `headline' or `inlinetask' element. INFO is
540 a plist used as a communication channel. TEXT-WIDTH is an
541 integer representing the maximum length of a line.
543 When optional argument UNDERLINE is non-nil, underline title,
544 without the tags, according to `org-ascii-underline'
547 if optional argument NOTAGS is nil, no tags will be added to the
549 (let* ((headlinep (eq (org-element-type element
) 'headline
))
551 ;; Numbering is specific to headlines.
552 (and headlinep
(org-export-numbered-headline-p element info
)
553 ;; All tests passed: build numbering string.
557 (org-export-get-headline-number element info
) ".")
560 (org-export-data (org-element-property :title element
) info
)))
562 (and (plist-get info
:with-todo-keywords
)
563 (let ((todo (org-element-property :todo-keyword element
)))
564 (and todo
(concat (org-export-data todo info
) " ")))))
565 (tags (and (not notags
)
566 (plist-get info
:with-tags
)
567 (let ((tag-list (org-export-get-tags element info
)))
570 (mapconcat 'identity tag-list
":"))))))
572 (and (plist-get info
:with-priority
)
573 (let ((char (org-element-property :priority element
)))
574 (and char
(format "(#%c) " char
)))))
575 (first-part (concat numbers todo priority text
)))
578 ;; Align tags, if any.
582 (max (- text-width
(1+ (length first-part
))) (length tags
)))
584 ;; Maybe underline text, if ELEMENT type is `headline' and an
585 ;; underline character has been defined.
586 (when (and underline headlinep
)
588 (nth (1- (org-export-get-relative-level element info
))
589 (cdr (assq (plist-get info
:ascii-charset
)
590 org-ascii-underline
)))))
593 (make-string (length first-part
) under-char
))))))))
595 (defun org-ascii--has-caption-p (element info
)
596 "Non-nil when ELEMENT has a caption affiliated keyword.
597 INFO is a plist used as a communication channel. This function
598 is meant to be used as a predicate for `org-export-get-ordinal'."
599 (org-element-property :caption element
))
601 (defun org-ascii--build-caption (element info
)
602 "Return caption string for ELEMENT, if applicable.
604 INFO is a plist used as a communication channel.
606 The caption string contains the sequence number of ELEMENT along
607 with its real caption. Return nil when ELEMENT has no affiliated
609 (let ((caption (org-export-get-caption element
)))
611 ;; Get sequence number of current src-block among every
612 ;; src-block with a caption.
614 (org-export-get-ordinal
615 element info nil
'org-ascii--has-caption-p
))
616 (title-fmt (org-ascii--translate
617 (case (org-element-type element
)
618 (table "Table %d: %s")
619 (src-block "Listing %d: %s"))
621 (org-ascii--fill-string
622 (format title-fmt reference
(org-export-data caption info
))
623 (org-ascii--current-text-width element info
) info
)))))
625 (defun org-ascii--build-toc (info &optional n keyword
)
626 "Return a table of contents.
628 INFO is a plist used as a communication channel.
630 Optional argument N, when non-nil, is an integer specifying the
633 Optional argument KEYWORD specifies the TOC keyword, if any, from
634 which the table of contents generation has been initiated."
635 (let ((title (org-ascii--translate "Table of Contents" info
)))
638 (make-string (length title
)
639 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
642 (if keyword
(org-ascii--current-text-width keyword info
)
643 (- org-ascii-text-width org-ascii-global-margin
))))
646 (let* ((level (org-export-get-relative-level headline info
))
647 (indent (* (1- level
) 3)))
649 (unless (zerop indent
) (concat (make-string (1- indent
) ?.
) " "))
650 (org-ascii--build-title
651 headline info
(- text-width indent
) nil
652 (eq (plist-get info
:with-tags
) 'not-in-toc
)))))
653 (org-export-collect-headlines info n
) "\n")))))
655 (defun org-ascii--list-listings (keyword info
)
656 "Return a list of listings.
658 KEYWORD is the keyword that initiated the list of listings
659 generation. INFO is a plist used as a communication channel."
660 (let ((title (org-ascii--translate "List of Listings" info
)))
663 (make-string (length title
)
664 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
667 (if keyword
(org-ascii--current-text-width keyword info
)
668 (- org-ascii-text-width org-ascii-global-margin
)))
669 ;; Use a counter instead of retreiving ordinal of each
674 ;; Store initial text so its length can be computed. This is
675 ;; used to properly align caption right to it in case of
676 ;; filling (like contents of a description list item).
678 (format (org-ascii--translate "Listing %d:" info
)
683 (org-ascii--indent-string
684 (org-ascii--fill-string
685 ;; Use short name in priority, if available.
686 (let ((caption (or (org-export-get-caption src-block t
)
687 (org-export-get-caption src-block
))))
688 (org-export-data caption info
))
689 (- text-width
(length initial-text
)) info
)
690 (length initial-text
))))))
691 (org-export-collect-listings info
) "\n")))))
693 (defun org-ascii--list-tables (keyword info
)
694 "Return a list of listings.
696 KEYWORD is the keyword that initiated the list of listings
697 generation. INFO is a plist used as a communication channel."
698 (let ((title (org-ascii--translate "List of Tables" info
)))
701 (make-string (length title
)
702 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))
705 (if keyword
(org-ascii--current-text-width keyword info
)
706 (- org-ascii-text-width org-ascii-global-margin
)))
707 ;; Use a counter instead of retreiving ordinal of each
712 ;; Store initial text so its length can be computed. This is
713 ;; used to properly align caption right to it in case of
714 ;; filling (like contents of a description list item).
716 (format (org-ascii--translate "Table %d:" info
)
721 (org-ascii--indent-string
722 (org-ascii--fill-string
723 ;; Use short name in priority, if available.
724 (let ((caption (or (org-export-get-caption table t
)
725 (org-export-get-caption table
))))
726 (org-export-data caption info
))
727 (- text-width
(length initial-text
)) info
)
728 (length initial-text
))))))
729 (org-export-collect-tables info
) "\n")))))
731 (defun org-ascii--unique-links (element info
)
732 "Return a list of unique link references in ELEMENT.
734 ELEMENT is either an headline element or a section element. INFO
735 is a plist used as a communication channel."
739 ;; Return LINK if it wasn't referenced so far, or nil.
740 ;; Update SEEN links along the way.
743 (cons (org-element-property :raw-link link
)
744 (org-element-contents link
))))
745 ;; Ignore LINK if it hasn't been translated already.
746 ;; It can happen if it is located in an affiliated
747 ;; keyword that was ignored.
748 (when (and (org-string-nw-p
749 (gethash link
(plist-get info
:exported-data
)))
750 (not (member footprint seen
)))
751 (push footprint seen
) link
)))))
752 ;; If at a section, find parent headline, if any, in order to
753 ;; count links that might be in the title.
755 (if (eq (org-element-type element
) 'headline
) element
756 (or (org-export-get-parent-headline element
) element
))))
757 ;; Get all links in HEADLINE.
758 (org-element-map headline
'link
759 (lambda (l) (funcall unique-link-p l
)) info nil nil t
)))
761 (defun org-ascii--describe-links (links width info
)
762 "Return a string describing a list of links.
764 LINKS is a list of link type objects, as returned by
765 `org-ascii--unique-links'. WIDTH is the text width allowed for
766 the output string. INFO is a plist used as a communication
770 (let ((type (org-element-property :type link
))
771 (anchor (let ((desc (org-element-contents link
)))
772 (if desc
(org-export-data desc info
)
773 (org-element-property :raw-link link
)))))
775 ;; Coderefs, radio links and fuzzy links are ignored.
776 ((member type
'("coderef" "radio" "fuzzy")) nil
)
777 ;; Id and custom-id links: Headlines refer to their numbering.
778 ((member type
'("custom-id" "id"))
779 (let ((dest (org-export-resolve-id-link link info
)))
781 (org-ascii--fill-string
785 (if (not dest
) (org-ascii--translate "Unknown reference" info
)
787 (org-ascii--translate "See section %s" info
)
788 (mapconcat 'number-to-string
789 (org-export-get-headline-number dest info
) "."))))
790 width info
) "\n\n")))
791 ;; Do not add a link that cannot be resolved and doesn't have
792 ;; any description: destination is already visible in the
794 ((not (org-element-contents link
)) nil
)
797 (org-ascii--fill-string
798 (format "[%s] %s" anchor
(org-element-property :raw-link link
))
803 (defun org-ascii--checkbox (item info
)
804 "Return checkbox string for ITEM or nil.
805 INFO is a plist used as a communication channel."
806 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
807 (case (org-element-property :checkbox item
)
808 (on (if utf8p
"☑ " "[X] "))
809 (off (if utf8p
"☐ " "[ ] "))
810 (trans (if utf8p
"☒ " "[-] ")))))
816 (defun org-ascii-template--document-title (info)
817 "Return document title, as a string.
818 INFO is a plist used as a communication channel."
819 (let* ((text-width org-ascii-text-width
)
820 ;; Links in the title will not be resolved later, so we make
821 ;; sure their path is located right after them.
822 (org-ascii-links-to-notes nil
)
823 (title (org-export-data (plist-get info
:title
) info
))
824 (author (and (plist-get info
:with-author
)
825 (let ((auth (plist-get info
:author
)))
826 (and auth
(org-export-data auth info
)))))
827 (email (and (plist-get info
:with-email
)
828 (org-export-data (plist-get info
:email
) info
)))
829 (date (and (plist-get info
:with-date
)
830 (org-export-data (plist-get info
:date
) info
))))
831 ;; There are two types of title blocks depending on the presence
832 ;; of a title to display.
833 (if (string= title
"")
834 ;; Title block without a title. DATE is positioned at the top
835 ;; right of the document, AUTHOR to the top left and EMAIL
838 ((and (org-string-nw-p date
) (org-string-nw-p author
))
841 (make-string (- text-width
(length date
) (length author
)) ?
)
843 (when (org-string-nw-p email
) (concat "\n" email
))
845 ((and (org-string-nw-p date
) (org-string-nw-p email
))
848 (make-string (- text-width
(length date
) (length email
)) ?
)
850 ((org-string-nw-p date
)
852 (org-ascii--justify-string date text-width
'right
)
854 ((and (org-string-nw-p author
) (org-string-nw-p email
))
855 (concat author
"\n" email
"\n\n\n"))
856 ((org-string-nw-p author
) (concat author
"\n\n\n"))
857 ((org-string-nw-p email
) (concat email
"\n\n\n")))
858 ;; Title block with a title. Document's TITLE, along with the
859 ;; AUTHOR and its EMAIL are both overlined and an underlined,
860 ;; centered. Date is just below, also centered.
861 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
862 ;; Format TITLE. It may be filled if it is too wide,
863 ;; that is wider than the two thirds of the total width.
864 (title-len (min (length title
) (/ (* 2 text-width
) 3)))
865 (formatted-title (org-ascii--fill-string title title-len info
))
868 (min (+ (max title-len
(length author
) (length email
)) 2)
869 text-width
) (if utf8p ?━ ?_
))))
870 (org-ascii--justify-string
873 (upcase formatted-title
)
875 ((and (org-string-nw-p author
) (org-string-nw-p email
))
876 (concat (if utf8p
"\n\n\n" "\n\n") author
"\n" email
))
877 ((org-string-nw-p author
)
878 (concat (if utf8p
"\n\n\n" "\n\n") author
))
879 ((org-string-nw-p email
)
880 (concat (if utf8p
"\n\n\n" "\n\n") email
)))
882 (when (org-string-nw-p date
) (concat "\n\n\n" date
))
883 "\n\n\n") text-width
'center
)))))
885 (defun org-ascii-template (contents info
)
886 "Return complete document string after ASCII conversion.
887 CONTENTS is the transcoded contents string. INFO is a plist
888 holding export options."
889 (org-element-normalize-string
890 (org-ascii--indent-string
891 (let ((text-width (- org-ascii-text-width org-ascii-global-margin
)))
892 ;; 1. Build title block.
894 (org-ascii-template--document-title info
)
895 ;; 2. Table of contents.
896 (let ((depth (plist-get info
:with-toc
)))
899 (org-ascii--build-toc info
(and (wholenump depth
) depth
))
901 ;; 3. Document's body.
903 ;; 4. Footnote definitions.
904 (let ((definitions (org-export-collect-footnote-definitions
905 (plist-get info
:parse-tree
) info
))
906 ;; Insert full links right inside the footnote definition
907 ;; as they have no chance to be inserted later.
908 (org-ascii-links-to-notes nil
))
912 (let ((title (org-ascii--translate "Footnotes" info
)))
917 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?─ ?_
))))
921 (let ((id (format "[%s] " (car ref
))))
922 ;; Distinguish between inline definitions and
923 ;; full-fledged definitions.
925 (let ((def (nth 2 ref
)))
926 (if (eq (org-element-type def
) 'org-data
)
927 ;; Full-fledged definition: footnote ID is
928 ;; inserted inside the first parsed paragraph
929 ;; (FIRST), if any, to be sure filling will
930 ;; take it into consideration.
931 (let ((first (car (org-element-contents def
))))
932 (if (not (eq (org-element-type first
) 'paragraph
))
933 (concat id
"\n" (org-export-data def info
))
934 (push id
(nthcdr 2 first
))
935 (org-export-data def info
)))
936 ;; Fill paragraph once footnote ID is inserted in
937 ;; order to have a correct length for first line.
938 (org-ascii--fill-string
939 (concat id
(org-export-data def info
))
940 text-width info
))))))
941 definitions
"\n\n"))))
942 ;; 5. Creator. Ignore `comment' value as there are no comments in
943 ;; ASCII. Justify it to the bottom right.
944 (let ((creator-info (plist-get info
:with-creator
)))
945 (unless (or (not creator-info
) (eq creator-info
'comment
))
948 (org-ascii--fill-string
949 (plist-get info
:creator
) text-width info
'right
))))))
950 org-ascii-global-margin
)))
952 (defun org-ascii--translate (s info
)
953 "Translate string S according to specified language and charset.
954 INFO is a plist used as a communication channel."
955 (let ((charset (intern (format ":%s" (plist-get info
:ascii-charset
)))))
956 (org-export-translate s charset info
)))
960 ;;; Transcode Functions
964 (defun org-ascii-bold (bold contents info
)
965 "Transcode BOLD from Org to ASCII.
966 CONTENTS is the text with bold markup. INFO is a plist holding
967 contextual information."
968 (format "*%s*" contents
))
973 (defun org-ascii-center-block (center-block contents info
)
974 "Transcode a CENTER-BLOCK element from Org to ASCII.
975 CONTENTS holds the contents of the block. INFO is a plist
976 holding contextual information."
977 (org-ascii--justify-string
978 contents
(org-ascii--current-text-width center-block info
) 'center
))
983 (defun org-ascii-clock (clock contents info
)
984 "Transcode a CLOCK object from Org to ASCII.
985 CONTENTS is nil. INFO is a plist holding contextual
987 (concat org-clock-string
" "
989 (org-element-property :raw-value
990 (org-element-property :value clock
)))
991 (let ((time (org-element-property :duration clock
)))
996 (org-split-string time
":")))))))
1001 (defun org-ascii-code (code contents info
)
1002 "Return a CODE object from Org to ASCII.
1003 CONTENTS is nil. INFO is a plist holding contextual
1005 (format org-ascii-verbatim-format
(org-element-property :value code
)))
1010 (defun org-ascii-drawer (drawer contents info
)
1011 "Transcode a DRAWER element from Org to ASCII.
1012 CONTENTS holds the contents of the block. INFO is a plist
1013 holding contextual information."
1014 (let ((name (org-element-property :drawer-name drawer
))
1015 (width (org-ascii--current-text-width drawer info
)))
1016 (if (functionp org-ascii-format-drawer-function
)
1017 (funcall org-ascii-format-drawer-function name contents width
)
1018 ;; If there's no user defined function: simply
1019 ;; display contents of the drawer.
1025 (defun org-ascii-dynamic-block (dynamic-block contents info
)
1026 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1027 CONTENTS holds the contents of the block. INFO is a plist
1028 holding contextual information."
1034 (defun org-ascii-entity (entity contents info
)
1035 "Transcode an ENTITY object from Org to ASCII.
1036 CONTENTS are the definition itself. INFO is a plist holding
1037 contextual information."
1038 (org-element-property
1039 (intern (concat ":" (symbol-name (plist-get info
:ascii-charset
))))
1045 (defun org-ascii-example-block (example-block contents info
)
1046 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1047 CONTENTS is nil. INFO is a plist holding contextual information."
1048 (org-ascii--box-string
1049 (org-export-format-code-default example-block info
) info
))
1054 (defun org-ascii-export-snippet (export-snippet contents info
)
1055 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1056 CONTENTS is nil. INFO is a plist holding contextual information."
1057 (when (eq (org-export-snippet-backend export-snippet
) 'ascii
)
1058 (org-element-property :value export-snippet
)))
1063 (defun org-ascii-export-block (export-block contents info
)
1064 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1065 CONTENTS is nil. INFO is a plist holding contextual information."
1066 (when (string= (org-element-property :type export-block
) "ASCII")
1067 (org-remove-indentation (org-element-property :value export-block
))))
1072 (defun org-ascii-fixed-width (fixed-width contents info
)
1073 "Transcode a FIXED-WIDTH element from Org to ASCII.
1074 CONTENTS is nil. INFO is a plist holding contextual information."
1075 (org-ascii--box-string
1076 (org-remove-indentation
1077 (org-element-property :value fixed-width
)) info
))
1080 ;;;; Footnote Definition
1082 ;; Footnote Definitions are ignored. They are compiled at the end of
1083 ;; the document, by `org-ascii-template'.
1086 ;;;; Footnote Reference
1088 (defun org-ascii-footnote-reference (footnote-reference contents info
)
1089 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1090 CONTENTS is nil. INFO is a plist holding contextual information."
1091 (format "[%s]" (org-export-get-footnote-number footnote-reference info
)))
1096 (defun org-ascii-headline (headline contents info
)
1097 "Transcode an HEADLINE element from Org to ASCII.
1098 CONTENTS holds the contents of the headline. INFO is a plist
1099 holding contextual information."
1100 ;; Don't export footnote section, which will be handled at the end
1102 (unless (org-element-property :footnote-section-p headline
)
1103 (let* ((low-level-rank (org-export-low-level-p headline info
))
1104 (width (org-ascii--current-text-width headline info
))
1105 ;; Blank lines between headline and its contents.
1106 ;; `org-ascii-headline-spacing', when set, overwrites
1107 ;; original buffer's spacing.
1110 (if org-ascii-headline-spacing
(car org-ascii-headline-spacing
)
1111 (org-element-property :pre-blank headline
)) ?
\n))
1112 ;; Even if HEADLINE has no section, there might be some
1113 ;; links in its title that we shouldn't forget to describe.
1115 (unless (or (eq (caar (org-element-contents headline
)) 'section
))
1116 (let ((title (org-element-property :title headline
)))
1118 (org-ascii--describe-links
1119 (org-ascii--unique-links title info
) width info
))))))
1120 ;; Deep subtree: export it as a list item.
1124 (let ((bullets (cdr (assq (plist-get info
:ascii-charset
)
1125 org-ascii-bullets
))))
1127 (nth (mod (1- low-level-rank
) (length bullets
)) bullets
)))
1130 (org-ascii--build-title headline info width
) "\n"
1131 ;; Contents, indented by length of bullet.
1133 (org-ascii--indent-string
1135 (when (org-string-nw-p links
) (concat "\n\n" links
)))
1137 ;; Else: Standard headline.
1139 (org-ascii--build-title headline info width
'underline
)
1141 (concat (when (org-string-nw-p links
) links
) contents
))))))
1144 ;;;; Horizontal Rule
1146 (defun org-ascii-horizontal-rule (horizontal-rule contents info
)
1147 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1148 CONTENTS is nil. INFO is a plist holding contextual
1150 (let ((text-width (org-ascii--current-text-width horizontal-rule info
))
1152 (org-export-read-attribute :attr_ascii horizontal-rule
:width
)))
1153 (org-ascii--justify-string
1154 (make-string (if (wholenump spec-width
) spec-width text-width
)
1155 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) ?― ?-
))
1156 text-width
'center
)))
1159 ;;;; Inline Src Block
1161 (defun org-ascii-inline-src-block (inline-src-block contents info
)
1162 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1163 CONTENTS holds the contents of the item. INFO is a plist holding
1164 contextual information."
1165 (format org-ascii-verbatim-format
1166 (org-element-property :value inline-src-block
)))
1171 (defun org-ascii-inlinetask (inlinetask contents info
)
1172 "Transcode an INLINETASK element from Org to ASCII.
1173 CONTENTS holds the contents of the block. INFO is a plist
1174 holding contextual information."
1175 (let ((width (org-ascii--current-text-width inlinetask info
)))
1176 ;; If `org-ascii-format-inlinetask-function' is provided, call it
1177 ;; with appropriate arguments.
1178 (if (functionp org-ascii-format-inlinetask-function
)
1179 (funcall org-ascii-format-inlinetask-function
1181 (and (plist-get info
:with-todo-keywords
)
1182 (let ((todo (org-element-property
1183 :todo-keyword inlinetask
)))
1184 (and todo
(org-export-data todo info
))))
1186 (org-element-property :todo-type inlinetask
)
1188 (and (plist-get info
:with-priority
)
1189 (org-element-property :priority inlinetask
))
1191 (org-export-data (org-element-property :title inlinetask
) info
)
1193 (and (plist-get info
:with-tags
)
1194 (org-element-property :tags inlinetask
))
1195 ;; contents and width
1197 ;; Otherwise, use a default template.
1198 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1199 (org-ascii--indent-string
1201 ;; Top line, with an additional blank line if not in UTF-8.
1202 (make-string width
(if utf8p ?━ ?_
)) "\n"
1203 (unless utf8p
(concat (make-string width ?
) "\n"))
1204 ;; Add title. Fill it if wider than inlinetask.
1205 (let ((title (org-ascii--build-title inlinetask info width
)))
1206 (if (<= (length title
) width
) title
1207 (org-ascii--fill-string title width info
)))
1209 ;; If CONTENTS is not empty, insert it along with
1211 (when (org-string-nw-p contents
)
1212 (concat (make-string width
(if utf8p ?─ ?-
)) "\n" contents
))
1214 (make-string width
(if utf8p ?━ ?_
)))
1215 ;; Flush the inlinetask to the right.
1216 (- org-ascii-text-width org-ascii-global-margin
1217 (if (not (org-export-get-parent-headline inlinetask
)) 0
1218 org-ascii-inner-margin
)
1219 (org-ascii--current-text-width inlinetask info
)))))))
1224 (defun org-ascii-italic (italic contents info
)
1225 "Transcode italic from Org to ASCII.
1226 CONTENTS is the text with italic markup. INFO is a plist holding
1227 contextual information."
1228 (format "/%s/" contents
))
1233 (defun org-ascii-item (item contents info
)
1234 "Transcode an ITEM element from Org to ASCII.
1235 CONTENTS holds the contents of the item. INFO is a plist holding
1236 contextual information."
1237 (let* ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1238 (checkbox (org-ascii--checkbox item info
))
1239 (list-type (org-element-property :type
(org-export-get-parent item
)))
1241 ;; First parent of ITEM is always the plain-list. Get
1242 ;; `:type' property from it.
1243 (org-list-bullet-string
1247 (org-export-data (org-element-property :tag item
) info
)
1250 ;; Return correct number for ITEM, paying attention to
1252 (let* ((struct (org-element-property :structure item
))
1253 (bul (org-element-property :bullet item
))
1254 (num (number-to-string
1255 (car (last (org-list-get-item-number
1256 (org-element-property :begin item
)
1258 (org-list-prevs-alist struct
)
1259 (org-list-parents-alist struct
)))))))
1260 (replace-regexp-in-string "[0-9]+" num bul
)))
1261 (t (let ((bul (org-element-property :bullet item
)))
1262 ;; Change bullets into more visible form if UTF-8 is active.
1264 (replace-regexp-in-string
1266 (replace-regexp-in-string
1268 (replace-regexp-in-string "*" "‣" bul
))))))))))
1271 (unless (eq list-type
'descriptive
) checkbox
)
1272 ;; Contents: Pay attention to indentation. Note: check-boxes are
1273 ;; already taken care of at the paragraph level so they don't
1274 ;; interfere with indentation.
1275 (let ((contents (org-ascii--indent-string contents
(length bullet
))))
1276 (if (eq (org-element-type (car (org-element-contents item
))) 'paragraph
)
1278 (concat "\n" contents
))))))
1283 (defun org-ascii-keyword (keyword contents info
)
1284 "Transcode a KEYWORD element from Org to ASCII.
1285 CONTENTS is nil. INFO is a plist holding contextual
1287 (let ((key (org-element-property :key keyword
))
1288 (value (org-element-property :value keyword
)))
1290 ((string= key
"ASCII") value
)
1291 ((string= key
"TOC")
1292 (let ((value (downcase value
)))
1294 ((string-match "\\<headlines\\>" value
)
1295 (let ((depth (or (and (string-match "[0-9]+" value
)
1296 (string-to-number (match-string 0 value
)))
1297 (plist-get info
:with-toc
))))
1298 (org-ascii--build-toc
1299 info
(and (wholenump depth
) depth
) keyword
)))
1300 ((string= "tables" value
)
1301 (org-ascii--list-tables keyword info
))
1302 ((string= "listings" value
)
1303 (org-ascii--list-listings keyword info
))))))))
1306 ;;;; Latex Environment
1308 (defun org-ascii-latex-environment (latex-environment contents info
)
1309 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1310 CONTENTS is nil. INFO is a plist holding contextual
1312 (when (plist-get info
:with-latex
)
1313 (org-remove-indentation (org-element-property :value latex-environment
))))
1318 (defun org-ascii-latex-fragment (latex-fragment contents info
)
1319 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1320 CONTENTS is nil. INFO is a plist holding contextual
1322 (when (plist-get info
:with-latex
)
1323 (org-element-property :value latex-fragment
)))
1328 (defun org-ascii-line-break (line-break contents info
)
1329 "Transcode a LINE-BREAK object from Org to ASCII.
1330 CONTENTS is nil. INFO is a plist holding contextual
1331 information." hard-newline
)
1336 (defun org-ascii-link (link desc info
)
1337 "Transcode a LINK object from Org to ASCII.
1339 DESC is the description part of the link, or the empty string.
1340 INFO is a plist holding contextual information."
1341 (let ((raw-link (org-element-property :raw-link link
))
1342 (type (org-element-property :type link
)))
1344 ((string= type
"coderef")
1345 (let ((ref (org-element-property :path link
)))
1346 (format (org-export-get-coderef-format ref desc
)
1347 (org-export-resolve-coderef ref info
))))
1348 ;; Do not apply a special syntax on radio links. Though, use
1349 ;; transcoded target's contents as output.
1350 ((string= type
"radio")
1351 (let ((destination (org-export-resolve-radio-link link info
)))
1353 (org-export-data (org-element-contents destination
) info
))))
1354 ;; Do not apply a special syntax on fuzzy links pointing to
1356 ((string= type
"fuzzy")
1357 (let ((destination (org-export-resolve-fuzzy-link link info
)))
1358 ;; Ignore invisible "#+TARGET: path".
1359 (unless (eq (org-element-type destination
) 'keyword
)
1360 (if (org-string-nw-p desc
) desc
1363 (org-export-get-ordinal
1364 destination info nil
'org-ascii--has-caption-p
)))
1366 (if (atom number
) (number-to-string number
)
1367 (mapconcat 'number-to-string number
".")))))))))
1369 (if (not (org-string-nw-p desc
)) (format "[%s]" raw-link
)
1371 (format "[%s]" desc
)
1372 (unless org-ascii-links-to-notes
(format " (%s)" raw-link
))))))))
1377 (defun org-ascii-paragraph (paragraph contents info
)
1378 "Transcode a PARAGRAPH element from Org to ASCII.
1379 CONTENTS is the contents of the paragraph, as a string. INFO is
1380 the plist used as a communication channel."
1381 (let ((contents (if (not (wholenump org-ascii-indented-line-width
)) contents
1383 (make-string org-ascii-indented-line-width ?
)
1384 (replace-regexp-in-string "\\`[ \t]+" "" contents
)))))
1385 (org-ascii--fill-string
1386 contents
(org-ascii--current-text-width paragraph info
) info
)))
1391 (defun org-ascii-plain-list (plain-list contents info
)
1392 "Transcode a PLAIN-LIST element from Org to ASCII.
1393 CONTENTS is the contents of the list. INFO is a plist holding
1394 contextual information."
1400 (defun org-ascii-plain-text (text info
)
1401 "Transcode a TEXT string from Org to ASCII.
1402 INFO is a plist used as a communication channel."
1403 (let ((utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
)))
1404 (when (and utf8p
(plist-get info
:with-smart-quotes
))
1405 (setq text
(org-export-activate-smart-quotes text
:utf-8 info
)))
1406 (if (not (plist-get info
:with-special-strings
)) text
1407 (setq text
(replace-regexp-in-string "\\\\-" "" text
))
1408 (if (not utf8p
) text
1409 ;; Usual replacements in utf-8 with proper option set.
1410 (replace-regexp-in-string
1412 (replace-regexp-in-string
1414 (replace-regexp-in-string "---" "—" text
)))))))
1419 (defun org-ascii-planning (planning contents info
)
1420 "Transcode a PLANNING element from Org to ASCII.
1421 CONTENTS is nil. INFO is a plist used as a communication
1426 (list (let ((closed (org-element-property :closed planning
)))
1428 (concat org-closed-string
" "
1430 (org-element-property :raw-value closed
)))))
1431 (let ((deadline (org-element-property :deadline planning
)))
1433 (concat org-deadline-string
" "
1435 (org-element-property :raw-value deadline
)))))
1436 (let ((scheduled (org-element-property :scheduled planning
)))
1438 (concat org-scheduled-string
" "
1440 (org-element-property :raw-value scheduled
)))))))
1446 (defun org-ascii-quote-block (quote-block contents info
)
1447 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1448 CONTENTS holds the contents of the block. INFO is a plist
1449 holding contextual information."
1450 (let ((width (org-ascii--current-text-width quote-block info
)))
1451 (org-ascii--indent-string
1452 (org-remove-indentation
1453 (org-ascii--fill-string contents width info
))
1454 org-ascii-quote-margin
)))
1459 (defun org-ascii-quote-section (quote-section contents info
)
1460 "Transcode a QUOTE-SECTION element from Org to ASCII.
1461 CONTENTS is nil. INFO is a plist holding contextual information."
1462 (let ((width (org-ascii--current-text-width quote-section info
))
1465 (org-remove-indentation (org-element-property :value quote-section
))
1467 (org-ascii--indent-string
1469 (+ org-ascii-quote-margin
1470 ;; Don't apply inner margin if parent headline is low level.
1471 (let ((headline (org-export-get-parent-headline quote-section
)))
1472 (if (org-export-low-level-p headline info
) 0
1473 org-ascii-inner-margin
))))))
1478 (defun org-ascii-radio-target (radio-target contents info
)
1479 "Transcode a RADIO-TARGET object from Org to ASCII.
1480 CONTENTS is the contents of the target. INFO is a plist holding
1481 contextual information."
1487 (defun org-ascii-section (section contents info
)
1488 "Transcode a SECTION element from Org to ASCII.
1489 CONTENTS is the contents of the section. INFO is a plist holding
1490 contextual information."
1491 (org-ascii--indent-string
1494 (when org-ascii-links-to-notes
1495 ;; Add list of links at the end of SECTION.
1496 (let ((links (org-ascii--describe-links
1497 (org-ascii--unique-links section info
)
1498 (org-ascii--current-text-width section info
) info
)))
1499 ;; Separate list of links and section contents.
1500 (when (org-string-nw-p links
) (concat "\n\n" links
)))))
1501 ;; Do not apply inner margin if parent headline is low level.
1502 (let ((headline (org-export-get-parent-headline section
)))
1503 (if (or (not headline
) (org-export-low-level-p headline info
)) 0
1504 org-ascii-inner-margin
))))
1509 (defun org-ascii-special-block (special-block contents info
)
1510 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1511 CONTENTS holds the contents of the block. INFO is a plist
1512 holding contextual information."
1518 (defun org-ascii-src-block (src-block contents info
)
1519 "Transcode a SRC-BLOCK element from Org to ASCII.
1520 CONTENTS holds the contents of the item. INFO is a plist holding
1521 contextual information."
1522 (let ((caption (org-ascii--build-caption src-block info
)))
1524 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1525 (org-ascii--box-string
1526 (org-export-format-code-default src-block info
) info
)
1527 (when (and caption
(not org-ascii-caption-above
))
1528 (concat "\n" caption
)))))
1531 ;;;; Statistics Cookie
1533 (defun org-ascii-statistics-cookie (statistics-cookie contents info
)
1534 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1535 CONTENTS is nil. INFO is a plist holding contextual information."
1536 (org-element-property :value statistics-cookie
))
1541 (defun org-ascii-subscript (subscript contents info
)
1542 "Transcode a SUBSCRIPT object from Org to ASCII.
1543 CONTENTS is the contents of the object. INFO is a plist holding
1544 contextual information."
1545 (if (org-element-property :use-brackets-p subscript
)
1546 (format "_{%s}" contents
)
1547 (format "_%s" contents
)))
1552 (defun org-ascii-superscript (superscript contents info
)
1553 "Transcode a SUPERSCRIPT object from Org to ASCII.
1554 CONTENTS is the contents of the object. INFO is a plist holding
1555 contextual information."
1556 (if (org-element-property :use-brackets-p superscript
)
1557 (format "_{%s}" contents
)
1558 (format "_%s" contents
)))
1563 (defun org-ascii-strike-through (strike-through contents info
)
1564 "Transcode STRIKE-THROUGH from Org to ASCII.
1565 CONTENTS is text with strike-through markup. INFO is a plist
1566 holding contextual information."
1567 (format "+%s+" contents
))
1572 (defun org-ascii-table (table contents info
)
1573 "Transcode a TABLE element from Org to ASCII.
1574 CONTENTS is the contents of the table. INFO is a plist holding
1575 contextual information."
1576 (let ((caption (org-ascii--build-caption table info
)))
1578 ;; Possibly add a caption string above.
1579 (when (and caption org-ascii-caption-above
) (concat caption
"\n"))
1580 ;; Insert table. Note: "table.el" tables are left unmodified.
1581 (cond ((eq (org-element-property :type table
) 'org
) contents
)
1582 ((and org-ascii-table-use-ascii-art
1583 (eq (plist-get info
:ascii-charset
) 'utf-8
)
1584 (require 'ascii-art-to-unicode nil t
))
1586 (insert (org-remove-indentation
1587 (org-element-property :value table
)))
1588 (goto-char (point-min))
1590 (goto-char (point-max))
1591 (skip-chars-backward " \r\t\n")
1592 (buffer-substring (point-min) (point))))
1593 (t (org-remove-indentation (org-element-property :value table
))))
1594 ;; Possible add a caption string below.
1595 (when (and caption
(not org-ascii-caption-above
))
1596 (concat "\n" caption
)))))
1601 (defun org-ascii--table-cell-width (table-cell info
)
1602 "Return width of TABLE-CELL.
1604 INFO is a plist used as a communication channel.
1606 Width of a cell is determined either by a width cookie in the
1607 same column as the cell, or by the maximum cell's length in that
1610 When `org-ascii-table-widen-columns' is non-nil, width cookies
1612 (or (and (not org-ascii-table-widen-columns
)
1613 (org-export-table-cell-width table-cell info
))
1614 (let* ((max-width 0)
1615 (table (org-export-get-parent-table table-cell
))
1616 (specialp (org-export-table-has-special-column-p table
))
1617 (col (cdr (org-export-table-cell-address table-cell info
))))
1618 (org-element-map table
'table-row
1623 (org-element-contents
1624 (elt (if specialp
(cdr (org-element-contents row
))
1625 (org-element-contents row
))
1632 (defun org-ascii-table-cell (table-cell contents info
)
1633 "Transcode a TABLE-CELL object from Org to ASCII.
1634 CONTENTS is the cell contents. INFO is a plist used as
1635 a communication channel."
1636 ;; Determine column width. When `org-ascii-table-widen-columns'
1637 ;; is nil and some width cookie has set it, use that value.
1638 ;; Otherwise, compute the maximum width among transcoded data of
1639 ;; each cell in the column.
1640 (let ((width (org-ascii--table-cell-width table-cell info
)))
1641 ;; When contents are too large, truncate them.
1642 (unless (or org-ascii-table-widen-columns
(<= (length contents
) width
))
1643 (setq contents
(concat (substring contents
0 (- width
2)) "=>")))
1644 ;; Align contents correctly within the cell.
1645 (let* ((indent-tabs-mode nil
)
1648 (org-ascii--justify-string
1650 (org-export-table-cell-alignment table-cell info
)))))
1651 (setq contents
(concat data
(make-string (- width
(length data
)) ?
))))
1653 (concat (format " %s " contents
)
1654 (when (memq 'right
(org-export-table-cell-borders table-cell info
))
1655 (if (eq (plist-get info
:ascii-charset
) 'utf-8
) "│" "|")))))
1660 (defun org-ascii-table-row (table-row contents info
)
1661 "Transcode a TABLE-ROW element from Org to ASCII.
1662 CONTENTS is the row contents. INFO is a plist used as
1663 a communication channel."
1664 (when (eq (org-element-property :type table-row
) 'standard
)
1667 (lambda (lcorner horiz vert rcorner
)
1671 (org-element-map table-row
'table-cell
1673 (let ((width (org-ascii--table-cell-width cell info
))
1674 (borders (org-export-table-cell-borders cell info
)))
1676 ;; In order to know if CELL starts the row, do
1677 ;; not compare it with the first cell in the
1678 ;; row as there might be a special column.
1679 ;; Instead, compare it with first exportable
1680 ;; cell, obtained with `org-element-map'.
1681 (when (and (memq 'left borders
)
1682 (eq (org-element-map table-row
'table-cell
1686 (make-string (+ 2 width
) (string-to-char horiz
))
1688 ((not (memq 'right borders
)) nil
)
1689 ((eq (car (last (org-element-contents table-row
))) cell
)
1693 (utf8p (eq (plist-get info
:ascii-charset
) 'utf-8
))
1694 (borders (org-export-table-cell-borders
1695 (org-element-map table-row
'table-cell
'identity info t
)
1698 ((and (memq 'top borders
) (or utf8p
(memq 'above borders
)))
1699 (if utf8p
(funcall build-hline
"┍" "━" "┯" "┑")
1700 (funcall build-hline
"+" "-" "+" "+")))
1701 ((memq 'above borders
)
1702 (if utf8p
(funcall build-hline
"├" "─" "┼" "┤")
1703 (funcall build-hline
"+" "-" "+" "+"))))
1704 (when (memq 'left borders
) (if utf8p
"│" "|"))
1706 (when (and (memq 'bottom borders
) (or utf8p
(memq 'below borders
)))
1707 (if utf8p
(funcall build-hline
"┕" "━" "┷" "┙")
1708 (funcall build-hline
"+" "-" "+" "+")))))))
1713 (defun org-ascii-timestamp (timestamp contents info
)
1714 "Transcode a TIMESTAMP object from Org to ASCII.
1715 CONTENTS is nil. INFO is a plist holding contextual information."
1716 (org-ascii-plain-text (org-timestamp-translate timestamp
) info
))
1721 (defun org-ascii-underline (underline contents info
)
1722 "Transcode UNDERLINE from Org to ASCII.
1723 CONTENTS is the text with underline markup. INFO is a plist
1724 holding contextual information."
1725 (format "_%s_" contents
))
1730 (defun org-ascii-verbatim (verbatim contents info
)
1731 "Return a VERBATIM object from Org to ASCII.
1732 CONTENTS is nil. INFO is a plist holding contextual information."
1733 (format org-ascii-verbatim-format
1734 (org-element-property :value verbatim
)))
1739 (defun org-ascii-verse-block (verse-block contents info
)
1740 "Transcode a VERSE-BLOCK element from Org to ASCII.
1741 CONTENTS is verse block contents. INFO is a plist holding
1742 contextual information."
1743 (let ((verse-width (org-ascii--current-text-width verse-block info
)))
1744 (org-ascii--indent-string
1745 (org-ascii--justify-string contents verse-width
'left
)
1746 org-ascii-quote-margin
)))
1752 (defun org-ascii-filter-headline-blank-lines (headline back-end info
)
1753 "Filter controlling number of blank lines after an headline.
1755 HEADLINE is a string representing a transcoded headline.
1756 BACK-END is symbol specifying back-end used for export. INFO is
1757 plist containing the communication channel.
1759 This function only applies to `ascii' back-end. See
1760 `org-ascii-headline-spacing' for information."
1761 (if (not org-ascii-headline-spacing
) headline
1762 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing
)) ?
\n)))
1763 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline
))))
1765 (defun org-ascii-filter-paragraph-spacing (tree back-end info
)
1766 "Filter controlling number of blank lines between paragraphs.
1768 TREE is the parse tree. BACK-END is the symbol specifying
1769 back-end used for export. INFO is a plist used as
1770 a communication channel.
1772 This function only applies to `e-ascii' back-end. See
1773 `org-ascii-paragraph-spacing' for information."
1774 (when (wholenump org-ascii-paragraph-spacing
)
1775 (org-element-map tree
'paragraph
1777 (when (eq (org-element-type (org-export-get-next-element p info
))
1779 (org-element-put-property
1780 p
:post-blank org-ascii-paragraph-spacing
)))))
1785 ;;; End-user functions
1788 (defun org-ascii-export-as-ascii
1789 (&optional async subtreep visible-only body-only ext-plist
)
1790 "Export current buffer to a text buffer.
1792 If narrowing is active in the current buffer, only export its
1795 If a region is active, export that region.
1797 A non-nil optional argument ASYNC means the process should happen
1798 asynchronously. The resulting buffer should be accessible
1799 through the `org-export-stack' interface.
1801 When optional argument SUBTREEP is non-nil, export the sub-tree
1802 at point, extracting information from the headline properties
1805 When optional argument VISIBLE-ONLY is non-nil, don't export
1806 contents of hidden elements.
1808 When optional argument BODY-ONLY is non-nil, strip title, table
1809 of contents and footnote definitions from output.
1811 EXT-PLIST, when provided, is a property list with external
1812 parameters overriding Org default settings, but still inferior to
1813 file-local settings.
1815 Export is done in a buffer named \"*Org ASCII Export*\", which
1816 will be displayed when `org-export-show-temporary-export-buffer'
1820 (org-export-async-start
1822 (with-current-buffer (get-buffer-create "*Org ASCII Export*")
1825 (goto-char (point-min))
1827 (org-export-add-to-stack (current-buffer) 'ascii
)))
1828 `(org-export-as 'ascii
,subtreep
,visible-only
,body-only
1830 (let ((outbuf (org-export-to-buffer
1831 'ascii
"*Org ASCII Export*"
1832 subtreep visible-only body-only ext-plist
)))
1833 (with-current-buffer outbuf
(text-mode))
1834 (when org-export-show-temporary-export-buffer
1835 (switch-to-buffer-other-window outbuf
)))))
1838 (defun org-ascii-export-to-ascii
1839 (&optional async subtreep visible-only body-only ext-plist
)
1840 "Export current buffer to a text file.
1842 If narrowing is active in the current buffer, only export its
1845 If a region is active, export that region.
1847 A non-nil optional argument ASYNC means the process should happen
1848 asynchronously. The resulting file should be accessible through
1849 the `org-export-stack' interface.
1851 When optional argument SUBTREEP is non-nil, export the sub-tree
1852 at point, extracting information from the headline properties
1855 When optional argument VISIBLE-ONLY is non-nil, don't export
1856 contents of hidden elements.
1858 When optional argument BODY-ONLY is non-nil, strip title, table
1859 of contents and footnote definitions from output.
1861 EXT-PLIST, when provided, is a property list with external
1862 parameters overriding Org default settings, but still inferior to
1863 file-local settings.
1865 Return output file's name."
1867 (let ((outfile (org-export-output-file-name ".txt" subtreep
)))
1869 (org-export-async-start
1870 (lambda (f) (org-export-add-to-stack f
'ascii
))
1873 'ascii
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
1875 'ascii outfile subtreep visible-only body-only ext-plist
))))
1878 (defun org-ascii-publish-to-ascii (plist filename pub-dir
)
1879 "Publish an Org file to ASCII.
1881 FILENAME is the filename of the Org file to be published. PLIST
1882 is the property list for the given project. PUB-DIR is the
1883 publishing directory.
1885 Return output file name."
1887 'ascii filename
".txt" `(:ascii-charset ascii
,@plist
) pub-dir
))
1890 (defun org-ascii-publish-to-latin1 (plist filename pub-dir
)
1891 "Publish an Org file to Latin-1.
1893 FILENAME is the filename of the Org file to be published. PLIST
1894 is the property list for the given project. PUB-DIR is the
1895 publishing directory.
1897 Return output file name."
1899 'ascii filename
".txt" `(:ascii-charset latin1
,@plist
) pub-dir
))
1902 (defun org-ascii-publish-to-utf8 (plist filename pub-dir
)
1903 "Publish an org file to UTF-8.
1905 FILENAME is the filename of the Org file to be published. PLIST
1906 is the property list for the given project. PUB-DIR is the
1907 publishing directory.
1909 Return output file name."
1911 'ascii filename
".txt" `(:ascii-charset utf-8
,@plist
) pub-dir
))
1917 ;; generated-autoload-file: "org-loaddefs.el"
1920 ;;; ox-ascii.el ends here