ob-shell: honor the specified shell for :session
[org-mode.git] / lisp / ox-ascii.el
blob8e5b194dc8d5b70501edeaac0781483207f97e98
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/>.
23 ;;; Commentary:
25 ;; This library implements an ASCII back-end for Org generic exporter.
26 ;; See Org manual for more information.
28 ;;; Code:
30 (eval-when-compile (require 'cl))
31 (require 'ox)
32 (require 'ox-publish)
34 (declare-function aa2u "ext:ascii-art-to-unicode" ())
36 ;;; Define Back-End
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
42 ;; mechanism.
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-block . org-ascii-export-block)
59 (export-snippet . org-ascii-export-snippet)
60 (fixed-width . org-ascii-fixed-width)
61 (footnote-reference . org-ascii-footnote-reference)
62 (headline . org-ascii-headline)
63 (horizontal-rule . org-ascii-horizontal-rule)
64 (inline-src-block . org-ascii-inline-src-block)
65 (inlinetask . org-ascii-inlinetask)
66 (inner-template . org-ascii-inner-template)
67 (italic . org-ascii-italic)
68 (item . org-ascii-item)
69 (keyword . org-ascii-keyword)
70 (latex-environment . org-ascii-latex-environment)
71 (latex-fragment . org-ascii-latex-fragment)
72 (line-break . org-ascii-line-break)
73 (link . org-ascii-link)
74 (node-property . org-ascii-node-property)
75 (paragraph . org-ascii-paragraph)
76 (plain-list . org-ascii-plain-list)
77 (plain-text . org-ascii-plain-text)
78 (planning . org-ascii-planning)
79 (property-drawer . org-ascii-property-drawer)
80 (quote-block . org-ascii-quote-block)
81 (radio-target . org-ascii-radio-target)
82 (section . org-ascii-section)
83 (special-block . org-ascii-special-block)
84 (src-block . org-ascii-src-block)
85 (statistics-cookie . org-ascii-statistics-cookie)
86 (strike-through . org-ascii-strike-through)
87 (subscript . org-ascii-subscript)
88 (superscript . org-ascii-superscript)
89 (table . org-ascii-table)
90 (table-cell . org-ascii-table-cell)
91 (table-row . org-ascii-table-row)
92 (target . org-ascii-target)
93 (template . org-ascii-template)
94 (timestamp . org-ascii-timestamp)
95 (underline . org-ascii-underline)
96 (verbatim . org-ascii-verbatim)
97 (verse-block . org-ascii-verse-block))
98 :export-block "ASCII"
99 :menu-entry
100 '(?t "Export to Plain Text"
101 ((?A "As ASCII buffer"
102 (lambda (a s v b)
103 (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
104 (?a "As ASCII file"
105 (lambda (a s v b)
106 (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
107 (?L "As Latin1 buffer"
108 (lambda (a s v b)
109 (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
110 (?l "As Latin1 file"
111 (lambda (a s v b)
112 (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
113 (?U "As UTF-8 buffer"
114 (lambda (a s v b)
115 (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
116 (?u "As UTF-8 file"
117 (lambda (a s v b)
118 (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
119 :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
120 (:filter-parse-tree org-ascii-filter-paragraph-spacing
121 org-ascii-filter-comment-spacing)
122 (:filter-section . org-ascii-filter-headline-blank-lines))
123 :options-alist
124 '((:ascii-bullets nil nil org-ascii-bullets)
125 (:ascii-caption-above nil nil org-ascii-caption-above)
126 (:ascii-charset nil nil org-ascii-charset)
127 (:ascii-global-margin nil nil org-ascii-global-margin)
128 (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function)
129 (:ascii-format-inlinetask-function
130 nil nil org-ascii-format-inlinetask-function)
131 (:ascii-headline-spacing nil nil org-ascii-headline-spacing)
132 (:ascii-indented-line-width nil nil org-ascii-indented-line-width)
133 (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width)
134 (:ascii-inner-margin nil nil org-ascii-inner-margin)
135 (:ascii-links-to-notes nil nil org-ascii-links-to-notes)
136 (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing)
137 (:ascii-quote-margin nil nil org-ascii-quote-margin)
138 (:ascii-table-keep-all-vertical-lines
139 nil nil org-ascii-table-keep-all-vertical-lines)
140 (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art)
141 (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns)
142 (:ascii-text-width nil nil org-ascii-text-width)
143 (:ascii-underline nil nil org-ascii-underline)
144 (:ascii-verbatim-format nil nil org-ascii-verbatim-format)))
148 ;;; User Configurable Variables
150 (defgroup org-export-ascii nil
151 "Options for exporting Org mode files to ASCII."
152 :tag "Org Export ASCII"
153 :group 'org-export)
155 (defcustom org-ascii-text-width 72
156 "Maximum width of exported text.
157 This number includes margin size, as set in
158 `org-ascii-global-margin'."
159 :group 'org-export-ascii
160 :version "24.4"
161 :package-version '(Org . "8.0")
162 :type 'integer)
164 (defcustom org-ascii-global-margin 0
165 "Width of the left margin, in number of characters."
166 :group 'org-export-ascii
167 :version "24.4"
168 :package-version '(Org . "8.0")
169 :type 'integer)
171 (defcustom org-ascii-inner-margin 2
172 "Width of the inner margin, in number of characters.
173 Inner margin is applied between each headline."
174 :group 'org-export-ascii
175 :version "24.4"
176 :package-version '(Org . "8.0")
177 :type 'integer)
179 (defcustom org-ascii-quote-margin 6
180 "Width of margin used for quoting text, in characters.
181 This margin is applied on both sides of the text."
182 :group 'org-export-ascii
183 :version "24.4"
184 :package-version '(Org . "8.0")
185 :type 'integer)
187 (defcustom org-ascii-inlinetask-width 30
188 "Width of inline tasks, in number of characters.
189 This number ignores any margin."
190 :group 'org-export-ascii
191 :version "24.4"
192 :package-version '(Org . "8.0")
193 :type 'integer)
195 (defcustom org-ascii-headline-spacing '(1 . 2)
196 "Number of blank lines inserted around headlines.
198 This variable can be set to a cons cell. In that case, its car
199 represents the number of blank lines present before headline
200 contents whereas its cdr reflects the number of blank lines after
201 contents.
203 A nil value replicates the number of blank lines found in the
204 original Org buffer at the same place."
205 :group 'org-export-ascii
206 :version "24.4"
207 :package-version '(Org . "8.0")
208 :type '(choice
209 (const :tag "Replicate original spacing" nil)
210 (cons :tag "Set an uniform spacing"
211 (integer :tag "Number of blank lines before contents")
212 (integer :tag "Number of blank lines after contents"))))
214 (defcustom org-ascii-indented-line-width 'auto
215 "Additional indentation width for the first line in a paragraph.
216 If the value is an integer, indent the first line of each
217 paragraph by this width, unless it is located at the beginning of
218 a section, in which case indentation is removed from that line.
219 If it is the symbol `auto' preserve indentation from original
220 document."
221 :group 'org-export-ascii
222 :version "24.4"
223 :package-version '(Org . "8.0")
224 :type '(choice
225 (integer :tag "Number of white spaces characters")
226 (const :tag "Preserve original width" auto)))
228 (defcustom org-ascii-paragraph-spacing 'auto
229 "Number of white lines between paragraphs.
230 If the value is an integer, add this number of blank lines
231 between contiguous paragraphs. If is it the symbol `auto', keep
232 the same number of blank lines as in the original document."
233 :group 'org-export-ascii
234 :version "24.4"
235 :package-version '(Org . "8.0")
236 :type '(choice
237 (integer :tag "Number of blank lines")
238 (const :tag "Preserve original spacing" auto)))
240 (defcustom org-ascii-charset 'ascii
241 "The charset allowed to represent various elements and objects.
242 Possible values are:
243 `ascii' Only use plain ASCII characters
244 `latin1' Include Latin-1 characters
245 `utf-8' Use all UTF-8 characters"
246 :group 'org-export-ascii
247 :version "24.4"
248 :package-version '(Org . "8.0")
249 :type '(choice
250 (const :tag "ASCII" ascii)
251 (const :tag "Latin-1" latin1)
252 (const :tag "UTF-8" utf-8)))
254 (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
255 (latin1 ?= ?~ ?-)
256 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
257 "Characters for underlining headings in ASCII export.
259 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
260 and whose value is a list of characters.
262 For each supported charset, this variable associates a sequence
263 of underline characters. In a sequence, the characters will be
264 used in order for headlines level 1, 2, ... If no character is
265 available for a given level, the headline won't be underlined."
266 :group 'org-export-ascii
267 :version "24.4"
268 :package-version '(Org . "8.0")
269 :type '(list
270 (cons :tag "Underline characters sequence"
271 (const :tag "ASCII charset" ascii)
272 (repeat character))
273 (cons :tag "Underline characters sequence"
274 (const :tag "Latin-1 charset" latin1)
275 (repeat character))
276 (cons :tag "Underline characters sequence"
277 (const :tag "UTF-8 charset" utf-8)
278 (repeat character))))
280 (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
281 (latin1 ?§ ?¶)
282 (utf-8 ?◊))
283 "Bullet characters for headlines converted to lists in ASCII export.
285 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
286 and whose value is a list of characters.
288 The first character is used for the first level considered as low
289 level, and so on. If there are more levels than characters given
290 here, the list will be repeated.
292 Note that this variable doesn't affect plain lists
293 representation."
294 :group 'org-export-ascii
295 :version "24.4"
296 :package-version '(Org . "8.0")
297 :type '(list
298 (cons :tag "Bullet characters for low level headlines"
299 (const :tag "ASCII charset" ascii)
300 (repeat character))
301 (cons :tag "Bullet characters for low level headlines"
302 (const :tag "Latin-1 charset" latin1)
303 (repeat character))
304 (cons :tag "Bullet characters for low level headlines"
305 (const :tag "UTF-8 charset" utf-8)
306 (repeat character))))
308 (defcustom org-ascii-links-to-notes t
309 "Non-nil means convert links to notes before the next headline.
310 When nil, the link will be exported in place. If the line
311 becomes long in this way, it will be wrapped."
312 :group 'org-export-ascii
313 :version "24.4"
314 :package-version '(Org . "8.0")
315 :type 'boolean)
317 (defcustom org-ascii-table-keep-all-vertical-lines nil
318 "Non-nil means keep all vertical lines in ASCII tables.
319 When nil, vertical lines will be removed except for those needed
320 for column grouping."
321 :group 'org-export-ascii
322 :version "24.4"
323 :package-version '(Org . "8.0")
324 :type 'boolean)
326 (defcustom org-ascii-table-widen-columns t
327 "Non-nil means widen narrowed columns for export.
328 When nil, narrowed columns will look in ASCII export just like in
329 Org mode, i.e. with \"=>\" as ellipsis."
330 :group 'org-export-ascii
331 :version "24.4"
332 :package-version '(Org . "8.0")
333 :type 'boolean)
335 (defcustom org-ascii-table-use-ascii-art nil
336 "Non-nil means table.el tables are turned into ascii-art.
338 It only makes sense when export charset is `utf-8'. It is nil by
339 default since it requires ascii-art-to-unicode.el package. You
340 can download it here:
342 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
343 :group 'org-export-ascii
344 :version "24.4"
345 :package-version '(Org . "8.0")
346 :type 'boolean)
348 (defcustom org-ascii-caption-above nil
349 "When non-nil, place caption string before the element.
350 Otherwise, place it right after it."
351 :group 'org-export-ascii
352 :version "24.4"
353 :package-version '(Org . "8.0")
354 :type 'boolean)
356 (defcustom org-ascii-verbatim-format "`%s'"
357 "Format string used for verbatim text and inline code."
358 :group 'org-export-ascii
359 :version "24.4"
360 :package-version '(Org . "8.0")
361 :type 'string)
363 (defcustom org-ascii-format-drawer-function
364 (lambda (name contents width) contents)
365 "Function called to format a drawer in ASCII.
367 The function must accept three parameters:
368 NAME the drawer name, like \"LOGBOOK\"
369 CONTENTS the contents of the drawer.
370 WIDTH the text width within the drawer.
372 The function should return either the string to be exported or
373 nil to ignore the drawer.
375 The default value simply returns the value of CONTENTS."
376 :group 'org-export-ascii
377 :version "24.4"
378 :package-version '(Org . "8.0")
379 :type 'function)
381 (defcustom org-ascii-format-inlinetask-function
382 'org-ascii-format-inlinetask-default
383 "Function called to format an inlinetask in ASCII.
385 The function must accept nine parameters:
386 TODO the todo keyword, as a string
387 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
388 PRIORITY the inlinetask priority, as a string
389 NAME the inlinetask name, as a string.
390 TAGS the inlinetask tags, as a list of strings.
391 CONTENTS the contents of the inlinetask, as a string.
392 WIDTH the width of the inlinetask, as a number.
393 INLINETASK the inlinetask itself.
394 INFO the info channel.
396 The function should return either the string to be exported or
397 nil to ignore the inline task."
398 :group 'org-export-ascii
399 :version "24.4"
400 :package-version '(Org . "8.3")
401 :type 'function)
405 ;;; Internal Functions
407 ;; Internal functions fall into three categories.
409 ;; The first one is about text formatting. The core functions are
410 ;; `org-ascii--current-text-width' and
411 ;; `org-ascii--current-justification', which determine, respectively,
412 ;; the current text width allowed to a given element and its expected
413 ;; justification. Once this information is known,
414 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
415 ;; `org-ascii--justify-element' `org-ascii--box-string' and
416 ;; `org-ascii--indent-string' can operate on a given output string.
417 ;; In particular, justification happens at the regular (i.e.,
418 ;; non-greater) element level, which means that when the exporting
419 ;; process reaches a container (e.g., a center block) content are
420 ;; already justified.
422 ;; The second category contains functions handling elements listings,
423 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
424 ;; returns a complete table of contents, `org-ascii--list-listings'
425 ;; returns a list of referenceable src-block elements, and
426 ;; `org-ascii--list-tables' does the same for table elements.
428 ;; The third category includes general helper functions.
429 ;; `org-ascii--build-title' creates the title for a given headline
430 ;; or inlinetask element. `org-ascii--build-caption' returns the
431 ;; caption string associated to a table or a src-block.
432 ;; `org-ascii--describe-links' creates notes about links for
433 ;; insertion at the end of a section. It uses
434 ;; `org-ascii--unique-links' to get the list of links to describe.
435 ;; Eventually, `org-ascii--translate' translates a string according
436 ;; to language and charset specification.
439 (defun org-ascii--fill-string (s text-width info &optional justify)
440 "Fill a string with specified text-width and return it.
442 S is the string being filled. TEXT-WIDTH is an integer
443 specifying maximum length of a line. INFO is the plist used as
444 a communication channel.
446 Optional argument JUSTIFY can specify any type of justification
447 among `left', `center', `right' or `full'. A nil value is
448 equivalent to `left'. For a justification that doesn't also fill
449 string, see `org-ascii--justify-lines' and
450 `org-ascii--justify-block'.
452 Return nil if S isn't a string."
453 ;; Don't fill paragraph when break should be preserved.
454 (cond ((not (stringp s)) nil)
455 ((plist-get info :preserve-breaks) s)
456 (t (let ((double-space-p sentence-end-double-space))
457 (with-temp-buffer
458 (let ((fill-column text-width)
459 (use-hard-newlines t)
460 (sentence-end-double-space double-space-p))
461 (insert s)
462 (fill-region (point-min) (point-max) justify))
463 (buffer-string))))))
465 (defun org-ascii--justify-lines (s text-width how)
466 "Justify all lines in string S.
467 TEXT-WIDTH is an integer specifying maximum length of a line.
468 HOW determines the type of justification: it can be `left',
469 `right', `full' or `center'."
470 (with-temp-buffer
471 (insert s)
472 (goto-char (point-min))
473 (let ((fill-column text-width)
474 ;; Disable `adaptive-fill-mode' so it doesn't prevent
475 ;; filling lines matching `adaptive-fill-regexp'.
476 (adaptive-fill-mode nil))
477 (while (< (point) (point-max))
478 (justify-current-line how)
479 (forward-line)))
480 (buffer-string)))
482 (defun org-ascii--justify-element (contents element info)
483 "Justify CONTENTS of ELEMENT.
484 INFO is a plist used as a communication channel. Justification
485 is done according to the type of element. More accurately,
486 paragraphs are filled and other elements are justified as blocks,
487 that is according to the widest non blank line in CONTENTS."
488 (if (not (org-string-nw-p contents)) contents
489 (let ((text-width (org-ascii--current-text-width element info))
490 (how (org-ascii--current-justification element)))
491 (if (eq how 'left) contents
492 ;; Paragraphs are treated specially as they also need to be
493 ;; filled.
494 (if (eq (org-element-type element) 'paragraph)
495 (org-ascii--fill-string contents text-width info how)
496 (with-temp-buffer
497 (insert contents)
498 (goto-char (point-min))
499 (catch 'exit
500 (let ((max-width 0))
501 ;; Compute maximum width. Bail out if it is greater
502 ;; than page width, since no justification is
503 ;; possible.
504 (save-excursion
505 (while (not (eobp))
506 (unless (org-looking-at-p "[ \t]*$")
507 (end-of-line)
508 (let ((column (current-column)))
509 (cond
510 ((>= column text-width) (throw 'exit contents))
511 ((> column max-width) (setq max-width column)))))
512 (forward-line)))
513 ;; Justify every line according to TEXT-WIDTH and
514 ;; MAX-WIDTH.
515 (let ((offset (/ (- text-width max-width)
516 (if (eq how 'right) 1 2))))
517 (if (zerop offset) (throw 'exit contents)
518 (while (not (eobp))
519 (unless (org-looking-at-p "[ \t]*$")
520 (org-indent-to-column offset))
521 (forward-line)))))
522 (buffer-string))))))))
524 (defun org-ascii--indent-string (s width)
525 "Indent string S by WIDTH white spaces.
526 Empty lines are not indented."
527 (when (stringp s)
528 (replace-regexp-in-string
529 "\\(^\\)[ \t]*\\S-" (make-string width ?\s) s nil nil 1)))
531 (defun org-ascii--box-string (s info)
532 "Return string S with a partial box to its left.
533 INFO is a plist used as a communication channel."
534 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
535 (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
536 (replace-regexp-in-string
537 "^" (if utf8p "│ " "| ")
538 ;; Remove last newline character.
539 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
541 (defun org-ascii--current-text-width (element info)
542 "Return maximum text width for ELEMENT's contents.
543 INFO is a plist used as a communication channel."
544 (case (org-element-type element)
545 ;; Elements with an absolute width: `headline' and `inlinetask'.
546 (inlinetask org-ascii-inlinetask-width)
547 (headline
548 (- org-ascii-text-width
549 (let ((low-level-rank (org-export-low-level-p element info)))
550 (if low-level-rank (* low-level-rank 2) org-ascii-global-margin))))
551 ;; Elements with a relative width: store maximum text width in
552 ;; TOTAL-WIDTH.
553 (otherwise
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.
557 (total-width
558 (if (loop for parent in genealogy
559 thereis (eq (org-element-type parent) 'inlinetask))
560 org-ascii-inlinetask-width
561 ;; No inlinetask: Remove global margin from text width.
562 (- org-ascii-text-width
563 org-ascii-global-margin
564 (let ((parent (org-export-get-parent-headline element)))
565 ;; Inner margin doesn't apply to text before first
566 ;; headline.
567 (if (not parent) 0
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 org-ascii-inner-margin))))))))
575 (- total-width
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))
581 count parent)
582 2 org-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.
587 (let ((parent-item
588 (if (eq (org-element-type element) 'item) element
589 (loop for parent in genealogy
590 when (eq (org-element-type parent) 'item)
591 return parent))))
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)
602 (org-list-get-ind
603 (org-list-get-top-point struct) struct))
604 (string-width (or (org-ascii--checkbox parent-item info)
605 ""))
606 (string-width
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
613 `full'."
614 (let (justification)
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))
619 (special-block
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'
635 specifications.
637 If optional argument NOTAGS is non-nil, no tags will be added to
638 the title.
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))
643 (numbers
644 ;; Numbering is specific to headlines.
645 (and headlinep (org-export-numbered-headline-p element info)
646 ;; All tests passed: build numbering string.
647 (concat
648 (mapconcat
649 'number-to-string
650 (org-export-get-headline-number element info) ".")
651 " ")))
652 (text
653 (org-trim
654 (org-export-data
655 (if (and toc headlinep) (org-export-get-alt-title element info)
656 (org-element-property :title element))
657 info)))
658 (todo
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)))
665 (and tag-list
666 (format ":%s:"
667 (mapconcat 'identity tag-list ":"))))))
668 (priority
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)))
673 (concat
674 first-part
675 ;; Align tags, if any.
676 (when tags
677 (format
678 (format " %%%ds"
679 (max (- text-width (1+ (string-width first-part)))
680 (string-width tags)))
681 tags))
682 ;; Maybe underline text, if ELEMENT type is `headline' and an
683 ;; underline character has been defined.
684 (when (and underline headlinep)
685 (let ((under-char
686 (nth (1- (org-export-get-relative-level element info))
687 (cdr (assq (plist-get info :ascii-charset)
688 org-ascii-underline)))))
689 (and under-char
690 (concat "\n"
691 (make-string (/ (string-width first-part)
692 (char-width under-char))
693 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
708 caption keyword."
709 (let ((caption (org-export-get-caption element)))
710 (when caption
711 ;; Get sequence number of current src-block among every
712 ;; src-block with a caption.
713 (let ((reference
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)
718 (table "Table %d:")
719 (src-block "Listing %d:"))
720 info)))
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
733 depth of the table.
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)))
738 (concat
739 title "\n"
740 (make-string (string-width title)
741 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
742 "\n\n"
743 (let ((text-width
744 (if keyword (org-ascii--current-text-width keyword info)
745 (- org-ascii-text-width org-ascii-global-margin))))
746 (mapconcat
747 (lambda (headline)
748 (let* ((level (org-export-get-relative-level headline info))
749 (indent (* (1- level) 3)))
750 (concat
751 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
752 (org-ascii--build-title
753 headline info (- text-width indent) nil
754 (or (not (plist-get info :with-tags))
755 (eq (plist-get info :with-tags) 'not-in-toc))
756 'toc))))
757 (org-export-collect-headlines info n) "\n")))))
759 (defun org-ascii--list-listings (keyword info)
760 "Return a list of listings.
762 KEYWORD is the keyword that initiated the list of listings
763 generation. INFO is a plist used as a communication channel."
764 (let ((title (org-ascii--translate "List of Listings" info)))
765 (concat
766 title "\n"
767 (make-string (string-width title)
768 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
769 "\n\n"
770 (let ((text-width
771 (if keyword (org-ascii--current-text-width keyword info)
772 (- org-ascii-text-width org-ascii-global-margin)))
773 ;; Use a counter instead of retrieving ordinal of each
774 ;; src-block.
775 (count 0))
776 (mapconcat
777 (lambda (src-block)
778 ;; Store initial text so its length can be computed. This is
779 ;; used to properly align caption right to it in case of
780 ;; filling (like contents of a description list item).
781 (let* ((initial-text
782 (format (org-ascii--translate "Listing %d:" info)
783 (incf count)))
784 (initial-width (string-width initial-text)))
785 (concat
786 initial-text " "
787 (org-trim
788 (org-ascii--indent-string
789 (org-ascii--fill-string
790 ;; Use short name in priority, if available.
791 (let ((caption (or (org-export-get-caption src-block t)
792 (org-export-get-caption src-block))))
793 (org-export-data caption info))
794 (- text-width initial-width) info)
795 initial-width)))))
796 (org-export-collect-listings info) "\n")))))
798 (defun org-ascii--list-tables (keyword info)
799 "Return a list of tables.
801 KEYWORD is the keyword that initiated the list of tables
802 generation. INFO is a plist used as a communication channel."
803 (let ((title (org-ascii--translate "List of Tables" info)))
804 (concat
805 title "\n"
806 (make-string (string-width title)
807 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
808 "\n\n"
809 (let ((text-width
810 (if keyword (org-ascii--current-text-width keyword info)
811 (- org-ascii-text-width org-ascii-global-margin)))
812 ;; Use a counter instead of retrieving ordinal of each
813 ;; src-block.
814 (count 0))
815 (mapconcat
816 (lambda (table)
817 ;; Store initial text so its length can be computed. This is
818 ;; used to properly align caption right to it in case of
819 ;; filling (like contents of a description list item).
820 (let* ((initial-text
821 (format (org-ascii--translate "Table %d:" info)
822 (incf count)))
823 (initial-width (string-width initial-text)))
824 (concat
825 initial-text " "
826 (org-trim
827 (org-ascii--indent-string
828 (org-ascii--fill-string
829 ;; Use short name in priority, if available.
830 (let ((caption (or (org-export-get-caption table t)
831 (org-export-get-caption table))))
832 (org-export-data caption info))
833 (- text-width initial-width) info)
834 initial-width)))))
835 (org-export-collect-tables info) "\n")))))
837 (defun org-ascii--unique-links (element info)
838 "Return a list of unique link references in ELEMENT.
839 ELEMENT is either a headline element or a section element. INFO
840 is a plist used as a communication channel."
841 (let* (seen
842 (unique-link-p
843 (function
844 ;; Return LINK if it wasn't referenced so far, or nil.
845 ;; Update SEEN links along the way.
846 (lambda (link)
847 (let ((footprint
848 ;; Normalize description in footprints.
849 (cons (org-element-property :raw-link link)
850 (let ((contents (org-element-contents link)))
851 (and contents
852 (replace-regexp-in-string
853 "[ \r\t\n]+" " "
854 (org-trim
855 (org-element-interpret-data contents))))))))
856 ;; Ignore LINK if it hasn't been translated already.
857 ;; It can happen if it is located in an affiliated
858 ;; keyword that was ignored.
859 (when (and (org-string-nw-p
860 (gethash link (plist-get info :exported-data)))
861 (not (member footprint seen)))
862 (push footprint seen) link)))))
863 ;; If at a section, find parent headline, if any, in order to
864 ;; count links that might be in the title.
865 (headline
866 (if (eq (org-element-type element) 'headline) element
867 (or (org-export-get-parent-headline element) element))))
868 ;; Get all links in HEADLINE.
869 (org-element-map headline 'link
870 (lambda (l) (funcall unique-link-p l)) info nil nil t)))
872 (defun org-ascii--describe-links (links width info)
873 "Return a string describing a list of links.
875 LINKS is a list of link type objects, as returned by
876 `org-ascii--unique-links'. WIDTH is the text width allowed for
877 the output string. INFO is a plist used as a communication
878 channel."
879 (mapconcat
880 (lambda (link)
881 (let ((type (org-element-property :type link))
882 (anchor (let ((desc (org-element-contents link)))
883 (if desc (org-export-data desc info)
884 (org-element-property :raw-link link)))))
885 (cond
886 ;; Coderefs, radio links and fuzzy links are ignored.
887 ((member type '("coderef" "radio" "fuzzy")) nil)
888 ;; Id and custom-id links: Headlines refer to their numbering.
889 ((member type '("custom-id" "id"))
890 (let ((dest (org-export-resolve-id-link link info)))
891 (concat
892 (org-ascii--fill-string
893 (format
894 "[%s] %s"
895 anchor
896 (if (not dest) (org-ascii--translate "Unknown reference" info)
897 (format
898 (org-ascii--translate "See section %s" info)
899 (mapconcat 'number-to-string
900 (org-export-get-headline-number dest info) "."))))
901 width info) "\n\n")))
902 ;; Do not add a link that cannot be resolved and doesn't have
903 ;; any description: destination is already visible in the
904 ;; paragraph.
905 ((not (org-element-contents link)) nil)
907 (concat
908 (org-ascii--fill-string
909 (format "[%s] %s" anchor (org-element-property :raw-link link))
910 width info)
911 "\n\n")))))
912 links ""))
914 (defun org-ascii--checkbox (item info)
915 "Return checkbox string for ITEM or nil.
916 INFO is a plist used as a communication channel."
917 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
918 (case (org-element-property :checkbox item)
919 (on (if utf8p "☑ " "[X] "))
920 (off (if utf8p "☐ " "[ ] "))
921 (trans (if utf8p "☒ " "[-] ")))))
925 ;;; Template
927 (defun org-ascii-template--document-title (info)
928 "Return document title, as a string.
929 INFO is a plist used as a communication channel."
930 (let* ((text-width org-ascii-text-width)
931 ;; Links in the title will not be resolved later, so we make
932 ;; sure their path is located right after them.
933 (org-ascii-links-to-notes nil)
934 (title (org-export-data (plist-get info :title) info))
935 (author (and (plist-get info :with-author)
936 (let ((auth (plist-get info :author)))
937 (and auth (org-export-data auth info)))))
938 (email (and (plist-get info :with-email)
939 (org-export-data (plist-get info :email) info)))
940 (date (and (plist-get info :with-date)
941 (org-export-data (org-export-get-date info) info))))
942 ;; There are two types of title blocks depending on the presence
943 ;; of a title to display.
944 (if (string= title "")
945 ;; Title block without a title. DATE is positioned at the top
946 ;; right of the document, AUTHOR to the top left and EMAIL
947 ;; just below.
948 (cond
949 ((and (org-string-nw-p date) (org-string-nw-p author))
950 (concat
951 author
952 (make-string (- text-width (string-width date) (string-width author))
953 ?\s)
954 date
955 (when (org-string-nw-p email) (concat "\n" email))
956 "\n\n\n"))
957 ((and (org-string-nw-p date) (org-string-nw-p email))
958 (concat
959 email
960 (make-string (- text-width (string-width date) (string-width email))
961 ?\s)
962 date "\n\n\n"))
963 ((org-string-nw-p date)
964 (concat
965 (org-ascii--justify-lines date text-width 'right)
966 "\n\n\n"))
967 ((and (org-string-nw-p author) (org-string-nw-p email))
968 (concat author "\n" email "\n\n\n"))
969 ((org-string-nw-p author) (concat author "\n\n\n"))
970 ((org-string-nw-p email) (concat email "\n\n\n")))
971 ;; Title block with a title. Document's TITLE, along with the
972 ;; AUTHOR and its EMAIL are both overlined and an underlined,
973 ;; centered. Date is just below, also centered.
974 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
975 ;; Format TITLE. It may be filled if it is too wide,
976 ;; that is wider than the two thirds of the total width.
977 (title-len (min (length title) (/ (* 2 text-width) 3)))
978 (formatted-title (org-ascii--fill-string title title-len info))
979 (line
980 (make-string
981 (min (+ (max title-len
982 (string-width (or author ""))
983 (string-width (or email "")))
985 text-width) (if utf8p ?━ ?_))))
986 (org-ascii--justify-lines
987 (concat line "\n"
988 (unless utf8p "\n")
989 (upcase formatted-title)
990 (cond
991 ((and (org-string-nw-p author) (org-string-nw-p email))
992 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
993 ((org-string-nw-p author)
994 (concat (if utf8p "\n\n\n" "\n\n") author))
995 ((org-string-nw-p email)
996 (concat (if utf8p "\n\n\n" "\n\n") email)))
997 "\n" line
998 (when (org-string-nw-p date) (concat "\n\n\n" date))
999 "\n\n\n") text-width 'center)))))
1001 (defun org-ascii-inner-template (contents info)
1002 "Return complete document string after ASCII conversion.
1003 CONTENTS is the transcoded contents string. INFO is a plist
1004 holding export options."
1005 (org-element-normalize-string
1006 (org-ascii--indent-string
1007 (concat
1008 ;; 1. Document's body.
1009 contents
1010 ;; 2. Footnote definitions.
1011 (let ((definitions (org-export-collect-footnote-definitions
1012 (plist-get info :parse-tree) info))
1013 ;; Insert full links right inside the footnote definition
1014 ;; as they have no chance to be inserted later.
1015 (org-ascii-links-to-notes nil))
1016 (when definitions
1017 (concat
1018 "\n\n\n"
1019 (let ((title (org-ascii--translate "Footnotes" info)))
1020 (concat
1021 title "\n"
1022 (make-string
1023 (string-width title)
1024 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
1025 "\n\n"
1026 (let ((text-width (- org-ascii-text-width org-ascii-global-margin)))
1027 (mapconcat
1028 (lambda (ref)
1029 (let ((id (format "[%s] " (car ref))))
1030 ;; Distinguish between inline definitions and
1031 ;; full-fledged definitions.
1032 (org-trim
1033 (let ((def (nth 2 ref)))
1034 (if (eq (org-element-type def) 'org-data)
1035 ;; Full-fledged definition: footnote ID is
1036 ;; inserted inside the first parsed paragraph
1037 ;; (FIRST), if any, to be sure filling will
1038 ;; take it into consideration.
1039 (let ((first (car (org-element-contents def))))
1040 (if (not (eq (org-element-type first) 'paragraph))
1041 (concat id "\n" (org-export-data def info))
1042 (push id (nthcdr 2 first))
1043 (org-export-data def info)))
1044 ;; Fill paragraph once footnote ID is inserted
1045 ;; in order to have a correct length for first
1046 ;; line.
1047 (org-ascii--fill-string
1048 (concat id (org-export-data def info))
1049 text-width info))))))
1050 definitions "\n\n"))))))
1051 org-ascii-global-margin)))
1053 (defun org-ascii-template (contents info)
1054 "Return complete document string after ASCII conversion.
1055 CONTENTS is the transcoded contents string. INFO is a plist
1056 holding export options."
1057 (concat
1058 ;; 1. Build title block.
1059 (org-ascii--indent-string
1060 (concat (org-ascii-template--document-title info)
1061 ;; 2. Table of contents.
1062 (let ((depth (plist-get info :with-toc)))
1063 (when depth
1064 (concat
1065 (org-ascii--build-toc info (and (wholenump depth) depth))
1066 "\n\n\n"))))
1067 org-ascii-global-margin)
1068 ;; 3. Document's body.
1069 contents
1070 ;; 4. Creator. Ignore `comment' value as there are no comments in
1071 ;; ASCII. Justify it to the bottom right.
1072 (org-ascii--indent-string
1073 (let ((creator-info (plist-get info :with-creator))
1074 (text-width (- org-ascii-text-width org-ascii-global-margin)))
1075 (unless (or (not creator-info) (eq creator-info 'comment))
1076 (concat
1077 "\n\n\n"
1078 (org-ascii--fill-string
1079 (plist-get info :creator) text-width info 'right))))
1080 org-ascii-global-margin)))
1082 (defun org-ascii--translate (s info)
1083 "Translate string S according to specified language and charset.
1084 INFO is a plist used as a communication channel."
1085 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1086 (org-export-translate s charset info)))
1090 ;;; Transcode Functions
1092 ;;;; Bold
1094 (defun org-ascii-bold (bold contents info)
1095 "Transcode BOLD from Org to ASCII.
1096 CONTENTS is the text with bold markup. INFO is a plist holding
1097 contextual information."
1098 (format "*%s*" contents))
1101 ;;;; Center Block
1103 (defun org-ascii-center-block (center-block contents info)
1104 "Transcode a CENTER-BLOCK element from Org to ASCII.
1105 CONTENTS holds the contents of the block. INFO is a plist
1106 holding contextual information."
1107 ;; Center has already been taken care of at a lower level, so
1108 ;; there's nothing left to do.
1109 contents)
1112 ;;;; Clock
1114 (defun org-ascii-clock (clock contents info)
1115 "Transcode a CLOCK object from Org to ASCII.
1116 CONTENTS is nil. INFO is a plist holding contextual
1117 information."
1118 (org-ascii--justify-element
1119 (concat org-clock-string " "
1120 (org-translate-time
1121 (org-element-property :raw-value
1122 (org-element-property :value clock)))
1123 (let ((time (org-element-property :duration clock)))
1124 (and time
1125 (concat " => "
1126 (apply 'format
1127 "%2s:%02s"
1128 (org-split-string time ":"))))))
1129 clock info))
1132 ;;;; Code
1134 (defun org-ascii-code (code contents info)
1135 "Return a CODE object from Org to ASCII.
1136 CONTENTS is nil. INFO is a plist holding contextual
1137 information."
1138 (format org-ascii-verbatim-format (org-element-property :value code)))
1141 ;;;; Drawer
1143 (defun org-ascii-drawer (drawer contents info)
1144 "Transcode a DRAWER element from Org to ASCII.
1145 CONTENTS holds the contents of the block. INFO is a plist
1146 holding contextual information."
1147 (let ((name (org-element-property :drawer-name drawer))
1148 (width (org-ascii--current-text-width drawer info)))
1149 (funcall org-ascii-format-drawer-function name contents width)))
1152 ;;;; Dynamic Block
1154 (defun org-ascii-dynamic-block (dynamic-block contents info)
1155 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1156 CONTENTS holds the contents of the block. INFO is a plist
1157 holding contextual information."
1158 contents)
1161 ;;;; Entity
1163 (defun org-ascii-entity (entity contents info)
1164 "Transcode an ENTITY object from Org to ASCII.
1165 CONTENTS are the definition itself. INFO is a plist holding
1166 contextual information."
1167 (org-element-property
1168 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1169 entity))
1172 ;;;; Example Block
1174 (defun org-ascii-example-block (example-block contents info)
1175 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1176 CONTENTS is nil. INFO is a plist holding contextual information."
1177 (org-ascii--justify-element
1178 (org-ascii--box-string
1179 (org-export-format-code-default example-block info) info)
1180 example-block info))
1183 ;;;; Export Snippet
1185 (defun org-ascii-export-snippet (export-snippet contents info)
1186 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1187 CONTENTS is nil. INFO is a plist holding contextual information."
1188 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1189 (org-element-property :value export-snippet)))
1192 ;;;; Export Block
1194 (defun org-ascii-export-block (export-block contents info)
1195 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1196 CONTENTS is nil. INFO is a plist holding contextual information."
1197 (when (string= (org-element-property :type export-block) "ASCII")
1198 (org-ascii--justify-element
1199 (org-element-property :value export-block) export-block info)))
1202 ;;;; Fixed Width
1204 (defun org-ascii-fixed-width (fixed-width contents info)
1205 "Transcode a FIXED-WIDTH element from Org to ASCII.
1206 CONTENTS is nil. INFO is a plist holding contextual information."
1207 (org-ascii--justify-element
1208 (org-ascii--box-string
1209 (org-remove-indentation
1210 (org-element-property :value fixed-width)) info)
1211 fixed-width info))
1214 ;;;; Footnote Definition
1216 ;; Footnote Definitions are ignored. They are compiled at the end of
1217 ;; the document, by `org-ascii-inner-template'.
1220 ;;;; Footnote Reference
1222 (defun org-ascii-footnote-reference (footnote-reference contents info)
1223 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1224 CONTENTS is nil. INFO is a plist holding contextual information."
1225 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1228 ;;;; Headline
1230 (defun org-ascii-headline (headline contents info)
1231 "Transcode a HEADLINE element from Org to ASCII.
1232 CONTENTS holds the contents of the headline. INFO is a plist
1233 holding contextual information."
1234 ;; Don't export footnote section, which will be handled at the end
1235 ;; of the template.
1236 (unless (org-element-property :footnote-section-p headline)
1237 (let* ((low-level-rank (org-export-low-level-p headline info))
1238 (width (org-ascii--current-text-width headline info))
1239 ;; Blank lines between headline and its contents.
1240 ;; `org-ascii-headline-spacing', when set, overwrites
1241 ;; original buffer's spacing.
1242 (pre-blanks
1243 (make-string
1244 (if org-ascii-headline-spacing (car org-ascii-headline-spacing)
1245 (org-element-property :pre-blank headline)) ?\n))
1246 ;; Even if HEADLINE has no section, there might be some
1247 ;; links in its title that we shouldn't forget to describe.
1248 (links
1249 (unless (or (eq (caar (org-element-contents headline)) 'section))
1250 (let ((title (org-element-property :title headline)))
1251 (when (consp title)
1252 (org-ascii--describe-links
1253 (org-ascii--unique-links title info) width info))))))
1254 ;; Deep subtree: export it as a list item.
1255 (if low-level-rank
1256 (concat
1257 ;; Bullet.
1258 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1259 org-ascii-bullets))))
1260 (char-to-string
1261 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1263 ;; Title.
1264 (org-ascii--build-title headline info width) "\n"
1265 ;; Contents, indented by length of bullet.
1266 pre-blanks
1267 (org-ascii--indent-string
1268 (concat contents
1269 (when (org-string-nw-p links) (concat "\n\n" links)))
1271 ;; Else: Standard headline.
1272 (concat
1273 (org-ascii--build-title headline info width 'underline)
1274 "\n" pre-blanks
1275 (concat (when (org-string-nw-p links) links) contents))))))
1278 ;;;; Horizontal Rule
1280 (defun org-ascii-horizontal-rule (horizontal-rule contents info)
1281 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1282 CONTENTS is nil. INFO is a plist holding contextual
1283 information."
1284 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1285 (spec-width
1286 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1287 (org-ascii--justify-lines
1288 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1289 (string-to-number spec-width)
1290 text-width)
1291 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1292 text-width 'center)))
1295 ;;;; Inline Src Block
1297 (defun org-ascii-inline-src-block (inline-src-block contents info)
1298 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1299 CONTENTS holds the contents of the item. INFO is a plist holding
1300 contextual information."
1301 (format org-ascii-verbatim-format
1302 (org-element-property :value inline-src-block)))
1305 ;;;; Inlinetask
1307 (defun org-ascii-format-inlinetask-default
1308 (todo type priority name tags contents width inlinetask info)
1309 "Format an inline task element for ASCII export.
1310 See `org-ascii-format-inlinetask-function' for a description
1311 of the parameters."
1312 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1313 (width (or width org-ascii-inlinetask-width)))
1314 (org-ascii--indent-string
1315 (concat
1316 ;; Top line, with an additional blank line if not in UTF-8.
1317 (make-string width (if utf8p ?━ ?_)) "\n"
1318 (unless utf8p (concat (make-string width ? ) "\n"))
1319 ;; Add title. Fill it if wider than inlinetask.
1320 (let ((title (org-ascii--build-title inlinetask info width)))
1321 (if (<= (string-width title) width) title
1322 (org-ascii--fill-string title width info)))
1323 "\n"
1324 ;; If CONTENTS is not empty, insert it along with
1325 ;; a separator.
1326 (when (org-string-nw-p contents)
1327 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1328 ;; Bottom line.
1329 (make-string width (if utf8p ?━ ?_)))
1330 ;; Flush the inlinetask to the right.
1331 (- org-ascii-text-width org-ascii-global-margin
1332 (if (not (org-export-get-parent-headline inlinetask)) 0
1333 org-ascii-inner-margin)
1334 (org-ascii--current-text-width inlinetask info)))))
1336 (defun org-ascii-inlinetask (inlinetask contents info)
1337 "Transcode an INLINETASK element from Org to ASCII.
1338 CONTENTS holds the contents of the block. INFO is a plist
1339 holding contextual information."
1340 (let ((width (org-ascii--current-text-width inlinetask info)))
1341 (funcall org-ascii-format-inlinetask-function
1342 ;; todo.
1343 (and (plist-get info :with-todo-keywords)
1344 (let ((todo (org-element-property
1345 :todo-keyword inlinetask)))
1346 (and todo (org-export-data todo info))))
1347 ;; todo-type
1348 (org-element-property :todo-type inlinetask)
1349 ;; priority
1350 (and (plist-get info :with-priority)
1351 (org-element-property :priority inlinetask))
1352 ;; title
1353 (org-export-data (org-element-property :title inlinetask) info)
1354 ;; tags
1355 (and (plist-get info :with-tags)
1356 (org-element-property :tags inlinetask))
1357 ;; contents and width
1358 contents width inlinetask info)))
1361 ;;;; Italic
1363 (defun org-ascii-italic (italic contents info)
1364 "Transcode italic from Org to ASCII.
1365 CONTENTS is the text with italic markup. INFO is a plist holding
1366 contextual information."
1367 (format "/%s/" contents))
1370 ;;;; Item
1372 (defun org-ascii-item (item contents info)
1373 "Transcode an ITEM element from Org to ASCII.
1374 CONTENTS holds the contents of the item. INFO is a plist holding
1375 contextual information."
1376 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1377 (checkbox (org-ascii--checkbox item info))
1378 (list-type (org-element-property :type (org-export-get-parent item)))
1379 (bullet
1380 ;; First parent of ITEM is always the plain-list. Get
1381 ;; `:type' property from it.
1382 (org-list-bullet-string
1383 (case list-type
1384 (descriptive
1385 (concat checkbox
1386 (org-export-data (org-element-property :tag item) info)
1387 ": "))
1388 (ordered
1389 ;; Return correct number for ITEM, paying attention to
1390 ;; counters.
1391 (let* ((struct (org-element-property :structure item))
1392 (bul (org-element-property :bullet item))
1393 (num (number-to-string
1394 (car (last (org-list-get-item-number
1395 (org-element-property :begin item)
1396 struct
1397 (org-list-prevs-alist struct)
1398 (org-list-parents-alist struct)))))))
1399 (replace-regexp-in-string "[0-9]+" num bul)))
1400 (t (let ((bul (org-element-property :bullet item)))
1401 ;; Change bullets into more visible form if UTF-8 is active.
1402 (if (not utf8p) bul
1403 (replace-regexp-in-string
1404 "-" "•"
1405 (replace-regexp-in-string
1406 "+" "⁃"
1407 (replace-regexp-in-string "*" "‣" bul))))))))))
1408 (concat
1409 bullet
1410 (unless (eq list-type 'descriptive) checkbox)
1411 ;; Contents: Pay attention to indentation. Note: check-boxes are
1412 ;; already taken care of at the paragraph level so they don't
1413 ;; interfere with indentation.
1414 (let ((contents (org-ascii--indent-string contents (string-width bullet))))
1415 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1416 (org-trim contents)
1417 (concat "\n" contents))))))
1420 ;;;; Keyword
1422 (defun org-ascii-keyword (keyword contents info)
1423 "Transcode a KEYWORD element from Org to ASCII.
1424 CONTENTS is nil. INFO is a plist holding contextual
1425 information."
1426 (let ((key (org-element-property :key keyword)))
1427 (cond
1428 ((string= key "ASCII")
1429 (org-ascii--justify-element
1430 (org-element-property :value keyword) keyword info))
1431 ((string= key "TOC")
1432 (org-ascii--justify-element
1433 (let ((value (downcase (org-element-property :value keyword))))
1434 (cond
1435 ((string-match "\\<headlines\\>" value)
1436 (let ((depth (or (and (string-match "[0-9]+" value)
1437 (string-to-number (match-string 0 value)))
1438 (plist-get info :with-toc))))
1439 (org-ascii--build-toc
1440 info (and (wholenump depth) depth) keyword)))
1441 ((string= "tables" value)
1442 (org-ascii--list-tables keyword info))
1443 ((string= "listings" value)
1444 (org-ascii--list-listings keyword info))))
1445 keyword info)))))
1448 ;;;; Latex Environment
1450 (defun org-ascii-latex-environment (latex-environment contents info)
1451 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1452 CONTENTS is nil. INFO is a plist holding contextual
1453 information."
1454 (when (plist-get info :with-latex)
1455 (org-ascii--justify-element
1456 (org-remove-indentation (org-element-property :value latex-environment))
1457 latex-environment info)))
1460 ;;;; Latex Fragment
1462 (defun org-ascii-latex-fragment (latex-fragment contents info)
1463 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1464 CONTENTS is nil. INFO is a plist holding contextual
1465 information."
1466 (when (plist-get info :with-latex)
1467 (org-element-property :value latex-fragment)))
1470 ;;;; Line Break
1472 (defun org-ascii-line-break (line-break contents info)
1473 "Transcode a LINE-BREAK object from Org to ASCII.
1474 CONTENTS is nil. INFO is a plist holding contextual
1475 information." hard-newline)
1478 ;;;; Link
1480 (defun org-ascii-link (link desc info)
1481 "Transcode a LINK object from Org to ASCII.
1483 DESC is the description part of the link, or the empty string.
1484 INFO is a plist holding contextual information."
1485 (let ((raw-link (org-element-property :raw-link link))
1486 (type (org-element-property :type link)))
1487 (cond
1488 ((string= type "coderef")
1489 (let ((ref (org-element-property :path link)))
1490 (format (org-export-get-coderef-format ref desc)
1491 (org-export-resolve-coderef ref info))))
1492 ;; Do not apply a special syntax on radio links. Though, use
1493 ;; transcoded target's contents as output.
1494 ((string= type "radio") desc)
1495 ;; Do not apply a special syntax on fuzzy links pointing to
1496 ;; targets.
1497 ((string= type "fuzzy")
1498 (let ((destination (org-export-resolve-fuzzy-link link info)))
1499 (if (org-string-nw-p desc) desc
1500 (when destination
1501 (let ((number
1502 (org-export-get-ordinal
1503 destination info nil 'org-ascii--has-caption-p)))
1504 (when number
1505 (if (atom number) (number-to-string number)
1506 (mapconcat 'number-to-string number "."))))))))
1508 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1509 (concat
1510 (format "[%s]" desc)
1511 (unless org-ascii-links-to-notes (format " (%s)" raw-link))))))))
1514 ;;;; Node Properties
1516 (defun org-ascii-node-property (node-property contents info)
1517 "Transcode a NODE-PROPERTY element from Org to ASCII.
1518 CONTENTS is nil. INFO is a plist holding contextual
1519 information."
1520 (format "%s:%s"
1521 (org-element-property :key node-property)
1522 (let ((value (org-element-property :value node-property)))
1523 (if value (concat " " value) ""))))
1526 ;;;; Paragraph
1528 (defun org-ascii-paragraph (paragraph contents info)
1529 "Transcode a PARAGRAPH element from Org to ASCII.
1530 CONTENTS is the contents of the paragraph, as a string. INFO is
1531 the plist used as a communication channel."
1532 (org-ascii--justify-element
1533 (if (not (wholenump org-ascii-indented-line-width)) contents
1534 (concat
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))
1538 'section))
1539 (make-string org-ascii-indented-line-width ?\s))
1540 (replace-regexp-in-string "\\`[ \t]+" "" contents)))
1541 paragraph info))
1544 ;;;; Plain List
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."
1550 contents)
1553 ;;;; Plain Text
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
1566 "\\.\\.\\." "…"
1567 (replace-regexp-in-string
1568 "--" "–"
1569 (replace-regexp-in-string "---" "—" text)))))))
1572 ;;;; Planning
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
1577 channel."
1578 (org-ascii--justify-element
1579 (mapconcat
1580 #'identity
1581 (delq nil
1582 (list (let ((closed (org-element-property :closed planning)))
1583 (when closed
1584 (concat org-closed-string " "
1585 (org-translate-time
1586 (org-element-property :raw-value closed)))))
1587 (let ((deadline (org-element-property :deadline planning)))
1588 (when deadline
1589 (concat org-deadline-string " "
1590 (org-translate-time
1591 (org-element-property :raw-value deadline)))))
1592 (let ((scheduled (org-element-property :scheduled planning)))
1593 (when scheduled
1594 (concat org-scheduled-string " "
1595 (org-translate-time
1596 (org-element-property :raw-value scheduled)))))))
1597 " ")
1598 planning info))
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)))
1611 ;;;; Quote Block
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 org-ascii-quote-margin))
1620 ;;;; Radio Target
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."
1626 contents)
1629 ;;;; Section
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
1636 (concat
1637 contents
1638 (when org-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 org-ascii-inner-margin))))
1651 ;;;; Special Block
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 ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
1658 ;; at a lower level. There is no other special block type to
1659 ;; handle.
1660 contents)
1663 ;;;; Src Block
1665 (defun org-ascii-src-block (src-block contents info)
1666 "Transcode a SRC-BLOCK element from Org to ASCII.
1667 CONTENTS holds the contents of the item. INFO is a plist holding
1668 contextual information."
1669 (let ((caption (org-ascii--build-caption src-block info))
1670 (code (org-export-format-code-default src-block info)))
1671 (if (equal code "") ""
1672 (org-ascii--justify-element
1673 (concat
1674 (when (and caption org-ascii-caption-above) (concat caption "\n"))
1675 (org-ascii--box-string code info)
1676 (when (and caption (not org-ascii-caption-above))
1677 (concat "\n" caption)))
1678 src-block info))))
1681 ;;;; Statistics Cookie
1683 (defun org-ascii-statistics-cookie (statistics-cookie contents info)
1684 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1685 CONTENTS is nil. INFO is a plist holding contextual information."
1686 (org-element-property :value statistics-cookie))
1689 ;;;; Subscript
1691 (defun org-ascii-subscript (subscript contents info)
1692 "Transcode a SUBSCRIPT object from Org to ASCII.
1693 CONTENTS is the contents of the object. INFO is a plist holding
1694 contextual information."
1695 (if (org-element-property :use-brackets-p subscript)
1696 (format "_{%s}" contents)
1697 (format "_%s" contents)))
1700 ;;;; Superscript
1702 (defun org-ascii-superscript (superscript contents info)
1703 "Transcode a SUPERSCRIPT object from Org to ASCII.
1704 CONTENTS is the contents of the object. INFO is a plist holding
1705 contextual information."
1706 (if (org-element-property :use-brackets-p superscript)
1707 (format "^{%s}" contents)
1708 (format "^%s" contents)))
1711 ;;;; Strike-through
1713 (defun org-ascii-strike-through (strike-through contents info)
1714 "Transcode STRIKE-THROUGH from Org to ASCII.
1715 CONTENTS is text with strike-through markup. INFO is a plist
1716 holding contextual information."
1717 (format "+%s+" contents))
1720 ;;;; Table
1722 (defun org-ascii-table (table contents info)
1723 "Transcode a TABLE element from Org to ASCII.
1724 CONTENTS is the contents of the table. INFO is a plist holding
1725 contextual information."
1726 (let ((caption (org-ascii--build-caption table info)))
1727 (org-ascii--justify-element
1728 (concat
1729 ;; Possibly add a caption string above.
1730 (when (and caption org-ascii-caption-above) (concat caption "\n"))
1731 ;; Insert table. Note: "table.el" tables are left unmodified.
1732 (cond ((eq (org-element-property :type table) 'org) contents)
1733 ((and org-ascii-table-use-ascii-art
1734 (eq (plist-get info :ascii-charset) 'utf-8)
1735 (require 'ascii-art-to-unicode nil t))
1736 (with-temp-buffer
1737 (insert (org-remove-indentation
1738 (org-element-property :value table)))
1739 (goto-char (point-min))
1740 (aa2u)
1741 (goto-char (point-max))
1742 (skip-chars-backward " \r\t\n")
1743 (buffer-substring (point-min) (point))))
1744 (t (org-remove-indentation (org-element-property :value table))))
1745 ;; Possible add a caption string below.
1746 (and (not org-ascii-caption-above) caption))
1747 table info)))
1750 ;;;; Table Cell
1752 (defun org-ascii--table-cell-width (table-cell info)
1753 "Return width of TABLE-CELL.
1755 INFO is a plist used as a communication channel.
1757 Width of a cell is determined either by a width cookie in the
1758 same column as the cell, or by the maximum cell's length in that
1759 column.
1761 When `org-ascii-table-widen-columns' is non-nil, width cookies
1762 are ignored."
1763 (let* ((row (org-export-get-parent table-cell))
1764 (table (org-export-get-parent row))
1765 (col (let ((cells (org-element-contents row)))
1766 (- (length cells) (length (memq table-cell cells)))))
1767 (cache
1768 (or (plist-get info :ascii-table-cell-width-cache)
1769 (plist-get (setq info
1770 (plist-put info :ascii-table-cell-width-cache
1771 (make-hash-table :test 'equal)))
1772 :ascii-table-cell-width-cache)))
1773 (key (cons table col)))
1774 (or (gethash key cache)
1775 (puthash
1777 (or (and (not org-ascii-table-widen-columns)
1778 (org-export-table-cell-width table-cell info))
1779 (let* ((max-width 0))
1780 (org-element-map table 'table-row
1781 (lambda (row)
1782 (setq max-width
1783 (max (string-width
1784 (org-export-data
1785 (org-element-contents
1786 (elt (org-element-contents row) col))
1787 info))
1788 max-width)))
1789 info)
1790 max-width))
1791 cache))))
1793 (defun org-ascii-table-cell (table-cell contents info)
1794 "Transcode a TABLE-CELL object from Org to ASCII.
1795 CONTENTS is the cell contents. INFO is a plist used as
1796 a communication channel."
1797 ;; Determine column width. When `org-ascii-table-widen-columns'
1798 ;; is nil and some width cookie has set it, use that value.
1799 ;; Otherwise, compute the maximum width among transcoded data of
1800 ;; each cell in the column.
1801 (let ((width (org-ascii--table-cell-width table-cell info)))
1802 ;; When contents are too large, truncate them.
1803 (unless (or org-ascii-table-widen-columns
1804 (<= (string-width (or contents "")) width))
1805 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1806 ;; Align contents correctly within the cell.
1807 (let* ((indent-tabs-mode nil)
1808 (data
1809 (when contents
1810 (org-ascii--justify-lines
1811 contents width
1812 (org-export-table-cell-alignment table-cell info)))))
1813 (setq contents
1814 (concat data
1815 (make-string (- width (string-width (or data ""))) ?\s))))
1816 ;; Return cell.
1817 (concat (format " %s " contents)
1818 (when (memq 'right (org-export-table-cell-borders table-cell info))
1819 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1822 ;;;; Table Row
1824 (defun org-ascii-table-row (table-row contents info)
1825 "Transcode a TABLE-ROW element from Org to ASCII.
1826 CONTENTS is the row contents. INFO is a plist used as
1827 a communication channel."
1828 (when (eq (org-element-property :type table-row) 'standard)
1829 (let ((build-hline
1830 (function
1831 (lambda (lcorner horiz vert rcorner)
1832 (concat
1833 (apply
1834 'concat
1835 (org-element-map table-row 'table-cell
1836 (lambda (cell)
1837 (let ((width (org-ascii--table-cell-width cell info))
1838 (borders (org-export-table-cell-borders cell info)))
1839 (concat
1840 ;; In order to know if CELL starts the row, do
1841 ;; not compare it with the first cell in the
1842 ;; row as there might be a special column.
1843 ;; Instead, compare it with first exportable
1844 ;; cell, obtained with `org-element-map'.
1845 (when (and (memq 'left borders)
1846 (eq (org-element-map table-row 'table-cell
1847 'identity info t)
1848 cell))
1849 lcorner)
1850 (make-string (+ 2 width) (string-to-char horiz))
1851 (cond
1852 ((not (memq 'right borders)) nil)
1853 ((eq (car (last (org-element-contents table-row))) cell)
1854 rcorner)
1855 (t vert)))))
1856 info)) "\n"))))
1857 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1858 (borders (org-export-table-cell-borders
1859 (org-element-map table-row 'table-cell 'identity info t)
1860 info)))
1861 (concat (cond
1862 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1863 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1864 (funcall build-hline "+" "-" "+" "+")))
1865 ((memq 'above borders)
1866 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1867 (funcall build-hline "+" "-" "+" "+"))))
1868 (when (memq 'left borders) (if utf8p "│" "|"))
1869 contents "\n"
1870 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1871 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1872 (funcall build-hline "+" "-" "+" "+")))))))
1875 ;;;; Timestamp
1877 (defun org-ascii-timestamp (timestamp contents info)
1878 "Transcode a TIMESTAMP object from Org to ASCII.
1879 CONTENTS is nil. INFO is a plist holding contextual information."
1880 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1883 ;;;; Underline
1885 (defun org-ascii-underline (underline contents info)
1886 "Transcode UNDERLINE from Org to ASCII.
1887 CONTENTS is the text with underline markup. INFO is a plist
1888 holding contextual information."
1889 (format "_%s_" contents))
1892 ;;;; Verbatim
1894 (defun org-ascii-verbatim (verbatim contents info)
1895 "Return a VERBATIM object from Org to ASCII.
1896 CONTENTS is nil. INFO is a plist holding contextual information."
1897 (format org-ascii-verbatim-format
1898 (org-element-property :value verbatim)))
1901 ;;;; Verse Block
1903 (defun org-ascii-verse-block (verse-block contents info)
1904 "Transcode a VERSE-BLOCK element from Org to ASCII.
1905 CONTENTS is verse block contents. INFO is a plist holding
1906 contextual information."
1907 (let ((verse-width (org-ascii--current-text-width verse-block info)))
1908 (org-ascii--indent-string
1909 (org-ascii--justify-element contents verse-block info)
1910 org-ascii-quote-margin)))
1914 ;;; Filters
1916 (defun org-ascii-filter-headline-blank-lines (headline back-end info)
1917 "Filter controlling number of blank lines after a headline.
1919 HEADLINE is a string representing a transcoded headline.
1920 BACK-END is symbol specifying back-end used for export. INFO is
1921 plist containing the communication channel.
1923 This function only applies to `ascii' back-end. See
1924 `org-ascii-headline-spacing' for information."
1925 (if (not org-ascii-headline-spacing) headline
1926 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing)) ?\n)))
1927 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1929 (defun org-ascii-filter-paragraph-spacing (tree back-end info)
1930 "Filter controlling number of blank lines between paragraphs.
1932 TREE is the parse tree. BACK-END is the symbol specifying
1933 back-end used for export. INFO is a plist used as
1934 a communication channel.
1936 See `org-ascii-paragraph-spacing' for information."
1937 (when (wholenump org-ascii-paragraph-spacing)
1938 (org-element-map tree 'paragraph
1939 (lambda (p)
1940 (when (eq (org-element-type (org-export-get-next-element p info))
1941 'paragraph)
1942 (org-element-put-property
1943 p :post-blank org-ascii-paragraph-spacing)))))
1944 tree)
1946 (defun org-ascii-filter-comment-spacing (tree backend info)
1947 "Filter removing blank lines between comments.
1948 TREE is the parse tree. BACK-END is the symbol specifying
1949 back-end used for export. INFO is a plist used as
1950 a communication channel."
1951 (org-element-map tree '(comment comment-block)
1952 (lambda (c)
1953 (when (memq (org-element-type (org-export-get-next-element c info))
1954 '(comment comment-block))
1955 (org-element-put-property c :post-blank 0))))
1956 tree)
1960 ;;; End-user functions
1962 ;;;###autoload
1963 (defun org-ascii-export-as-ascii
1964 (&optional async subtreep visible-only body-only ext-plist)
1965 "Export current buffer to a text buffer.
1967 If narrowing is active in the current buffer, only export its
1968 narrowed part.
1970 If a region is active, export that region.
1972 A non-nil optional argument ASYNC means the process should happen
1973 asynchronously. The resulting buffer should be accessible
1974 through the `org-export-stack' interface.
1976 When optional argument SUBTREEP is non-nil, export the sub-tree
1977 at point, extracting information from the headline properties
1978 first.
1980 When optional argument VISIBLE-ONLY is non-nil, don't export
1981 contents of hidden elements.
1983 When optional argument BODY-ONLY is non-nil, strip title and
1984 table of contents from output.
1986 EXT-PLIST, when provided, is a property list with external
1987 parameters overriding Org default settings, but still inferior to
1988 file-local settings.
1990 Export is done in a buffer named \"*Org ASCII Export*\", which
1991 will be displayed when `org-export-show-temporary-export-buffer'
1992 is non-nil."
1993 (interactive)
1994 (org-export-to-buffer 'ascii "*Org ASCII Export*"
1995 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
1997 ;;;###autoload
1998 (defun org-ascii-export-to-ascii
1999 (&optional async subtreep visible-only body-only ext-plist)
2000 "Export current buffer to a text file.
2002 If narrowing is active in the current buffer, only export its
2003 narrowed part.
2005 If a region is active, export that region.
2007 A non-nil optional argument ASYNC means the process should happen
2008 asynchronously. The resulting file should be accessible through
2009 the `org-export-stack' interface.
2011 When optional argument SUBTREEP is non-nil, export the sub-tree
2012 at point, extracting information from the headline properties
2013 first.
2015 When optional argument VISIBLE-ONLY is non-nil, don't export
2016 contents of hidden elements.
2018 When optional argument BODY-ONLY is non-nil, strip title and
2019 table of contents from output.
2021 EXT-PLIST, when provided, is a property list with external
2022 parameters overriding Org default settings, but still inferior to
2023 file-local settings.
2025 Return output file's name."
2026 (interactive)
2027 (let ((file (org-export-output-file-name ".txt" subtreep)))
2028 (org-export-to-file 'ascii file
2029 async subtreep visible-only body-only ext-plist)))
2031 ;;;###autoload
2032 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
2033 "Publish an Org file to ASCII.
2035 FILENAME is the filename of the Org file to be published. PLIST
2036 is the property list for the given project. PUB-DIR is the
2037 publishing directory.
2039 Return output file name."
2040 (org-publish-org-to
2041 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
2043 ;;;###autoload
2044 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
2045 "Publish an Org file to Latin-1.
2047 FILENAME is the filename of the Org file to be published. PLIST
2048 is the property list for the given project. PUB-DIR is the
2049 publishing directory.
2051 Return output file name."
2052 (org-publish-org-to
2053 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
2055 ;;;###autoload
2056 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
2057 "Publish an org file to UTF-8.
2059 FILENAME is the filename of the Org file to be published. PLIST
2060 is the property list for the given project. PUB-DIR is the
2061 publishing directory.
2063 Return output file name."
2064 (org-publish-org-to
2065 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2068 (provide 'ox-ascii)
2070 ;; Local variables:
2071 ;; generated-autoload-file: "org-loaddefs.el"
2072 ;; coding: utf-8-emacs
2073 ;; End:
2075 ;;; ox-ascii.el ends here