Revert "Merge export and special blocks within back-ends"
[org-mode/org-tableheadings.git] / lisp / ox-ascii.el
blobfeef8607716c4c4a0d795d40b52ec78d4c0d6606
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 (cond
492 ((eq (org-element-type element) 'paragraph)
493 ;; Paragraphs are treated specially as they need to be filled.
494 (org-ascii--fill-string contents text-width info how))
495 ((eq how 'left) contents)
496 (t (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 (plist-get info :ascii-inlinetask-width))
547 (headline
548 (- (plist-get info :ascii-text-width)
549 (let ((low-level-rank (org-export-low-level-p element info)))
550 (if low-level-rank (* low-level-rank 2)
551 (plist-get info :ascii-global-margin)))))
552 ;; Elements with a relative width: store maximum text width in
553 ;; TOTAL-WIDTH.
554 (otherwise
555 (let* ((genealogy (cons element (org-export-get-genealogy element)))
556 ;; Total width is determined by the presence, or not, of an
557 ;; inline task among ELEMENT parents.
558 (total-width
559 (if (loop for parent in genealogy
560 thereis (eq (org-element-type parent) 'inlinetask))
561 (plist-get info :ascii-inlinetask-width)
562 ;; No inlinetask: Remove global margin from text width.
563 (- (plist-get info :ascii-text-width)
564 (plist-get info :ascii-global-margin)
565 (let ((parent (org-export-get-parent-headline element)))
566 ;; Inner margin doesn't apply to text before first
567 ;; headline.
568 (if (not parent) 0
569 (let ((low-level-rank
570 (org-export-low-level-p parent info)))
571 ;; Inner margin doesn't apply to contents of
572 ;; low level headlines, since they've got their
573 ;; own indentation mechanism.
574 (if low-level-rank (* low-level-rank 2)
575 (plist-get info :ascii-inner-margin)))))))))
576 (- total-width
577 ;; Each `quote-block' and `verse-block' above narrows text
578 ;; width by twice the standard margin size.
579 (+ (* (loop for parent in genealogy
580 when (memq (org-element-type parent)
581 '(quote-block verse-block))
582 count parent)
583 2 (plist-get info :ascii-quote-margin))
584 ;; Text width within a plain-list is restricted by
585 ;; indentation of current item. If that's the case,
586 ;; compute it with the help of `:structure' property from
587 ;; parent item, if any.
588 (let ((parent-item
589 (if (eq (org-element-type element) 'item) element
590 (loop for parent in genealogy
591 when (eq (org-element-type parent) 'item)
592 return parent))))
593 (if (not parent-item) 0
594 ;; Compute indentation offset of the current item,
595 ;; that is the sum of the difference between its
596 ;; indentation and the indentation of the top item in
597 ;; the list and current item bullet's length. Also
598 ;; remove checkbox length, and tag length (for
599 ;; description lists) or bullet length.
600 (let ((struct (org-element-property :structure parent-item))
601 (beg-item (org-element-property :begin parent-item)))
602 (+ (- (org-list-get-ind beg-item struct)
603 (org-list-get-ind
604 (org-list-get-top-point struct) struct))
605 (string-width (or (org-ascii--checkbox parent-item info)
606 ""))
607 (string-width
608 (or (org-list-get-tag beg-item struct)
609 (org-list-get-bullet beg-item struct)))))))))))))
611 (defun org-ascii--current-justification (element)
612 "Return expected justification for ELEMENT's contents.
613 Return value is a symbol among `left', `center', `right' and
614 `full'."
615 (let (justification)
616 (while (and (not justification)
617 (setq element (org-element-property :parent element)))
618 (case (org-element-type element)
619 (center-block (setq justification 'center))
620 (special-block
621 (let ((name (org-element-property :type element)))
622 (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
623 ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
624 (or justification 'left)))
626 (defun org-ascii--build-title
627 (element info text-width &optional underline notags toc)
628 "Format ELEMENT title and return it.
630 ELEMENT is either an `headline' or `inlinetask' element. INFO is
631 a plist used as a communication channel. TEXT-WIDTH is an
632 integer representing the maximum length of a line.
634 When optional argument UNDERLINE is non-nil, underline title,
635 without the tags, according to `org-ascii-underline'
636 specifications.
638 If optional argument NOTAGS is non-nil, no tags will be added to
639 the title.
641 When optional argument TOC is non-nil, use optional title if
642 possible. It doesn't apply to `inlinetask' elements."
643 (let* ((headlinep (eq (org-element-type element) 'headline))
644 (numbers
645 ;; Numbering is specific to headlines.
646 (and headlinep (org-export-numbered-headline-p element info)
647 ;; All tests passed: build numbering string.
648 (concat
649 (mapconcat
650 'number-to-string
651 (org-export-get-headline-number element info) ".")
652 " ")))
653 (text
654 (org-trim
655 (org-export-data
656 (if (and toc headlinep) (org-export-get-alt-title element info)
657 (org-element-property :title element))
658 info)))
659 (todo
660 (and (plist-get info :with-todo-keywords)
661 (let ((todo (org-element-property :todo-keyword element)))
662 (and todo (concat (org-export-data todo info) " ")))))
663 (tags (and (not notags)
664 (plist-get info :with-tags)
665 (let ((tag-list (org-export-get-tags element info)))
666 (and tag-list
667 (format ":%s:"
668 (mapconcat 'identity tag-list ":"))))))
669 (priority
670 (and (plist-get info :with-priority)
671 (let ((char (org-element-property :priority element)))
672 (and char (format "(#%c) " char)))))
673 (first-part (concat numbers todo priority text)))
674 (concat
675 first-part
676 ;; Align tags, if any.
677 (when tags
678 (format
679 (format " %%%ds"
680 (max (- text-width (1+ (string-width first-part)))
681 (string-width tags)))
682 tags))
683 ;; Maybe underline text, if ELEMENT type is `headline' and an
684 ;; underline character has been defined.
685 (when (and underline headlinep)
686 (let ((under-char
687 (nth (1- (org-export-get-relative-level element info))
688 (cdr (assq (plist-get info :ascii-charset)
689 (plist-get info :ascii-underline))))))
690 (and under-char
691 (concat "\n"
692 (make-string (/ (string-width first-part)
693 (char-width under-char))
694 under-char))))))))
696 (defun org-ascii--has-caption-p (element info)
697 "Non-nil when ELEMENT has a caption affiliated keyword.
698 INFO is a plist used as a communication channel. This function
699 is meant to be used as a predicate for `org-export-get-ordinal'."
700 (org-element-property :caption element))
702 (defun org-ascii--build-caption (element info)
703 "Return caption string for ELEMENT, if applicable.
705 INFO is a plist used as a communication channel.
707 The caption string contains the sequence number of ELEMENT along
708 with its real caption. Return nil when ELEMENT has no affiliated
709 caption keyword."
710 (let ((caption (org-export-get-caption element)))
711 (when caption
712 ;; Get sequence number of current src-block among every
713 ;; src-block with a caption.
714 (let ((reference
715 (org-export-get-ordinal
716 element info nil 'org-ascii--has-caption-p))
717 (title-fmt (org-ascii--translate
718 (case (org-element-type element)
719 (table "Table %d:")
720 (src-block "Listing %d:"))
721 info)))
722 (org-ascii--fill-string
723 (concat (format title-fmt reference)
725 (org-export-data caption info))
726 (org-ascii--current-text-width element info) info)))))
728 (defun org-ascii--build-toc (info &optional n keyword)
729 "Return a table of contents.
731 INFO is a plist used as a communication channel.
733 Optional argument N, when non-nil, is an integer specifying the
734 depth of the table.
736 Optional argument KEYWORD specifies the TOC keyword, if any, from
737 which the table of contents generation has been initiated."
738 (let ((title (org-ascii--translate "Table of Contents" info)))
739 (concat
740 title "\n"
741 (make-string (string-width title)
742 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
743 "\n\n"
744 (let ((text-width
745 (if keyword (org-ascii--current-text-width keyword info)
746 (- (plist-get info :ascii-text-width)
747 (plist-get info :ascii-global-margin)))))
748 (mapconcat
749 (lambda (headline)
750 (let* ((level (org-export-get-relative-level headline info))
751 (indent (* (1- level) 3)))
752 (concat
753 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
754 (org-ascii--build-title
755 headline info (- text-width indent) nil
756 (or (not (plist-get info :with-tags))
757 (eq (plist-get info :with-tags) 'not-in-toc))
758 'toc))))
759 (org-export-collect-headlines info n) "\n")))))
761 (defun org-ascii--list-listings (keyword info)
762 "Return a list of listings.
764 KEYWORD is the keyword that initiated the list of listings
765 generation. INFO is a plist used as a communication channel."
766 (let ((title (org-ascii--translate "List of Listings" info)))
767 (concat
768 title "\n"
769 (make-string (string-width title)
770 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
771 "\n\n"
772 (let ((text-width
773 (if keyword (org-ascii--current-text-width keyword info)
774 (- (plist-get info :ascii-text-width)
775 (plist-get info :ascii-global-margin))))
776 ;; Use a counter instead of retrieving ordinal of each
777 ;; src-block.
778 (count 0))
779 (mapconcat
780 (lambda (src-block)
781 ;; Store initial text so its length can be computed. This is
782 ;; used to properly align caption right to it in case of
783 ;; filling (like contents of a description list item).
784 (let* ((initial-text
785 (format (org-ascii--translate "Listing %d:" info)
786 (incf count)))
787 (initial-width (string-width initial-text)))
788 (concat
789 initial-text " "
790 (org-trim
791 (org-ascii--indent-string
792 (org-ascii--fill-string
793 ;; Use short name in priority, if available.
794 (let ((caption (or (org-export-get-caption src-block t)
795 (org-export-get-caption src-block))))
796 (org-export-data caption info))
797 (- text-width initial-width) info)
798 initial-width)))))
799 (org-export-collect-listings info) "\n")))))
801 (defun org-ascii--list-tables (keyword info)
802 "Return a list of tables.
804 KEYWORD is the keyword that initiated the list of tables
805 generation. INFO is a plist used as a communication channel."
806 (let ((title (org-ascii--translate "List of Tables" info)))
807 (concat
808 title "\n"
809 (make-string (string-width title)
810 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
811 "\n\n"
812 (let ((text-width
813 (if keyword (org-ascii--current-text-width keyword info)
814 (- (plist-get info :ascii-text-width)
815 (plist-get info :ascii-global-margin))))
816 ;; Use a counter instead of retrieving ordinal of each
817 ;; src-block.
818 (count 0))
819 (mapconcat
820 (lambda (table)
821 ;; Store initial text so its length can be computed. This is
822 ;; used to properly align caption right to it in case of
823 ;; filling (like contents of a description list item).
824 (let* ((initial-text
825 (format (org-ascii--translate "Table %d:" info)
826 (incf count)))
827 (initial-width (string-width initial-text)))
828 (concat
829 initial-text " "
830 (org-trim
831 (org-ascii--indent-string
832 (org-ascii--fill-string
833 ;; Use short name in priority, if available.
834 (let ((caption (or (org-export-get-caption table t)
835 (org-export-get-caption table))))
836 (org-export-data caption info))
837 (- text-width initial-width) info)
838 initial-width)))))
839 (org-export-collect-tables info) "\n")))))
841 (defun org-ascii--unique-links (element info)
842 "Return a list of unique link references in ELEMENT.
843 ELEMENT is either a headline element or a section element. INFO
844 is a plist used as a communication channel."
845 (let* (seen
846 (unique-link-p
847 (function
848 ;; Return LINK if it wasn't referenced so far, or nil.
849 ;; Update SEEN links along the way.
850 (lambda (link)
851 (let ((footprint
852 ;; Normalize description in footprints.
853 (cons (org-element-property :raw-link link)
854 (let ((contents (org-element-contents link)))
855 (and contents
856 (replace-regexp-in-string
857 "[ \r\t\n]+" " "
858 (org-trim
859 (org-element-interpret-data contents))))))))
860 ;; Ignore LINK if it hasn't been translated already.
861 ;; It can happen if it is located in an affiliated
862 ;; keyword that was ignored.
863 (when (and (org-string-nw-p
864 (gethash link (plist-get info :exported-data)))
865 (not (member footprint seen)))
866 (push footprint seen) link)))))
867 ;; If at a section, find parent headline, if any, in order to
868 ;; count links that might be in the title.
869 (headline
870 (if (eq (org-element-type element) 'headline) element
871 (or (org-export-get-parent-headline element) element))))
872 ;; Get all links in HEADLINE.
873 (org-element-map headline 'link
874 (lambda (l) (funcall unique-link-p l)) info nil nil t)))
876 (defun org-ascii--describe-links (links width info)
877 "Return a string describing a list of links.
879 LINKS is a list of link type objects, as returned by
880 `org-ascii--unique-links'. WIDTH is the text width allowed for
881 the output string. INFO is a plist used as a communication
882 channel."
883 (mapconcat
884 (lambda (link)
885 (let ((type (org-element-property :type link))
886 (anchor (let ((desc (org-element-contents link)))
887 (if desc (org-export-data desc info)
888 (org-element-property :raw-link link)))))
889 (cond
890 ;; Coderefs, radio links and fuzzy links are ignored.
891 ((member type '("coderef" "radio" "fuzzy")) nil)
892 ;; Id and custom-id links: Headlines refer to their numbering.
893 ((member type '("custom-id" "id"))
894 (let ((dest (org-export-resolve-id-link link info)))
895 (concat
896 (org-ascii--fill-string
897 (format
898 "[%s] %s"
899 anchor
900 (if (not dest) (org-ascii--translate "Unknown reference" info)
901 (format
902 (org-ascii--translate "See section %s" info)
903 (mapconcat 'number-to-string
904 (org-export-get-headline-number dest info) "."))))
905 width info) "\n\n")))
906 ;; Do not add a link that cannot be resolved and doesn't have
907 ;; any description: destination is already visible in the
908 ;; paragraph.
909 ((not (org-element-contents link)) nil)
911 (concat
912 (org-ascii--fill-string
913 (format "[%s] %s" anchor (org-element-property :raw-link link))
914 width info)
915 "\n\n")))))
916 links ""))
918 (defun org-ascii--checkbox (item info)
919 "Return checkbox string for ITEM or nil.
920 INFO is a plist used as a communication channel."
921 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
922 (case (org-element-property :checkbox item)
923 (on (if utf8p "☑ " "[X] "))
924 (off (if utf8p "☐ " "[ ] "))
925 (trans (if utf8p "☒ " "[-] ")))))
929 ;;; Template
931 (defun org-ascii-template--document-title (info)
932 "Return document title, as a string.
933 INFO is a plist used as a communication channel."
934 (let* ((text-width (plist-get info :ascii-text-width))
935 ;; Links in the title will not be resolved later, so we make
936 ;; sure their path is located right after them.
937 (info (org-combine-plists info '(:ascii-links-to-notes nil)))
938 (title (org-export-data (plist-get info :title) info))
939 (author (and (plist-get info :with-author)
940 (let ((auth (plist-get info :author)))
941 (and auth (org-export-data auth info)))))
942 (email (and (plist-get info :with-email)
943 (org-export-data (plist-get info :email) info)))
944 (date (and (plist-get info :with-date)
945 (org-export-data (org-export-get-date info) info))))
946 ;; There are two types of title blocks depending on the presence
947 ;; of a title to display.
948 (if (string= title "")
949 ;; Title block without a title. DATE is positioned at the top
950 ;; right of the document, AUTHOR to the top left and EMAIL
951 ;; just below.
952 (cond
953 ((and (org-string-nw-p date) (org-string-nw-p author))
954 (concat
955 author
956 (make-string (- text-width (string-width date) (string-width author))
957 ?\s)
958 date
959 (when (org-string-nw-p email) (concat "\n" email))
960 "\n\n\n"))
961 ((and (org-string-nw-p date) (org-string-nw-p email))
962 (concat
963 email
964 (make-string (- text-width (string-width date) (string-width email))
965 ?\s)
966 date "\n\n\n"))
967 ((org-string-nw-p date)
968 (concat
969 (org-ascii--justify-lines date text-width 'right)
970 "\n\n\n"))
971 ((and (org-string-nw-p author) (org-string-nw-p email))
972 (concat author "\n" email "\n\n\n"))
973 ((org-string-nw-p author) (concat author "\n\n\n"))
974 ((org-string-nw-p email) (concat email "\n\n\n")))
975 ;; Title block with a title. Document's TITLE, along with the
976 ;; AUTHOR and its EMAIL are both overlined and an underlined,
977 ;; centered. Date is just below, also centered.
978 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
979 ;; Format TITLE. It may be filled if it is too wide,
980 ;; that is wider than the two thirds of the total width.
981 (title-len (min (length title) (/ (* 2 text-width) 3)))
982 (formatted-title (org-ascii--fill-string title title-len info))
983 (line
984 (make-string
985 (min (+ (max title-len
986 (string-width (or author ""))
987 (string-width (or email "")))
989 text-width) (if utf8p ?━ ?_))))
990 (org-ascii--justify-lines
991 (concat line "\n"
992 (unless utf8p "\n")
993 (upcase formatted-title)
994 (cond
995 ((and (org-string-nw-p author) (org-string-nw-p email))
996 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
997 ((org-string-nw-p author)
998 (concat (if utf8p "\n\n\n" "\n\n") author))
999 ((org-string-nw-p email)
1000 (concat (if utf8p "\n\n\n" "\n\n") email)))
1001 "\n" line
1002 (when (org-string-nw-p date) (concat "\n\n\n" date))
1003 "\n\n\n") text-width 'center)))))
1005 (defun org-ascii-inner-template (contents info)
1006 "Return complete document string after ASCII conversion.
1007 CONTENTS is the transcoded contents string. INFO is a plist
1008 holding export options."
1009 (org-element-normalize-string
1010 (let ((global-margin (plist-get info :ascii-global-margin)))
1011 (org-ascii--indent-string
1012 (concat
1013 ;; 1. Document's body.
1014 contents
1015 ;; 2. Footnote definitions.
1016 (let ((definitions (org-export-collect-footnote-definitions
1017 (plist-get info :parse-tree) info))
1018 ;; Insert full links right inside the footnote definition
1019 ;; as they have no chance to be inserted later.
1020 (info (org-combine-plists info '(:ascii-links-to-notes nil))))
1021 (when definitions
1022 (concat
1023 "\n\n\n"
1024 (let ((title (org-ascii--translate "Footnotes" info)))
1025 (concat
1026 title "\n"
1027 (make-string
1028 (string-width title)
1029 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
1030 "\n\n"
1031 (let ((text-width (- (plist-get info :ascii-text-width)
1032 global-margin)))
1033 (mapconcat
1034 (lambda (ref)
1035 (let ((id (format "[%s] " (car ref))))
1036 ;; Distinguish between inline definitions and
1037 ;; full-fledged definitions.
1038 (org-trim
1039 (let ((def (nth 2 ref)))
1040 (if (eq (org-element-type def) 'org-data)
1041 ;; Full-fledged definition: footnote ID is
1042 ;; inserted inside the first parsed
1043 ;; paragraph (FIRST), if any, to be sure
1044 ;; filling will take it into consideration.
1045 (let ((first (car (org-element-contents def))))
1046 (if (not (eq (org-element-type first) 'paragraph))
1047 (concat id "\n" (org-export-data def info))
1048 (push id (nthcdr 2 first))
1049 (org-export-data def info)))
1050 ;; Fill paragraph once footnote ID is inserted
1051 ;; in order to have a correct length for first
1052 ;; line.
1053 (org-ascii--fill-string
1054 (concat id (org-export-data def info))
1055 text-width info))))))
1056 definitions "\n\n"))))))
1057 global-margin))))
1059 (defun org-ascii-template (contents info)
1060 "Return complete document string after ASCII conversion.
1061 CONTENTS is the transcoded contents string. INFO is a plist
1062 holding export options."
1063 (let ((global-margin (plist-get info :ascii-global-margin)))
1064 (concat
1065 ;; 1. Build title block.
1066 (org-ascii--indent-string
1067 (concat (org-ascii-template--document-title info)
1068 ;; 2. Table of contents.
1069 (let ((depth (plist-get info :with-toc)))
1070 (when depth
1071 (concat
1072 (org-ascii--build-toc info (and (wholenump depth) depth))
1073 "\n\n\n"))))
1074 global-margin)
1075 ;; 3. Document's body.
1076 contents
1077 ;; 4. Creator. Ignore `comment' value as there are no comments in
1078 ;; ASCII. Justify it to the bottom right.
1079 (org-ascii--indent-string
1080 (let ((creator-info (plist-get info :with-creator))
1081 (text-width (- (plist-get info :ascii-text-width) global-margin)))
1082 (unless (or (not creator-info) (eq creator-info 'comment))
1083 (concat
1084 "\n\n\n"
1085 (org-ascii--fill-string
1086 (plist-get info :creator) text-width info 'right))))
1087 global-margin))))
1089 (defun org-ascii--translate (s info)
1090 "Translate string S according to specified language and charset.
1091 INFO is a plist used as a communication channel."
1092 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1093 (org-export-translate s charset info)))
1097 ;;; Transcode Functions
1099 ;;;; Bold
1101 (defun org-ascii-bold (bold contents info)
1102 "Transcode BOLD from Org to ASCII.
1103 CONTENTS is the text with bold markup. INFO is a plist holding
1104 contextual information."
1105 (format "*%s*" contents))
1108 ;;;; Center Block
1110 (defun org-ascii-center-block (center-block contents info)
1111 "Transcode a CENTER-BLOCK element from Org to ASCII.
1112 CONTENTS holds the contents of the block. INFO is a plist
1113 holding contextual information."
1114 ;; Center has already been taken care of at a lower level, so
1115 ;; there's nothing left to do.
1116 contents)
1119 ;;;; Clock
1121 (defun org-ascii-clock (clock contents info)
1122 "Transcode a CLOCK object from Org to ASCII.
1123 CONTENTS is nil. INFO is a plist holding contextual
1124 information."
1125 (org-ascii--justify-element
1126 (concat org-clock-string " "
1127 (org-translate-time
1128 (org-element-property :raw-value
1129 (org-element-property :value clock)))
1130 (let ((time (org-element-property :duration clock)))
1131 (and time
1132 (concat " => "
1133 (apply 'format
1134 "%2s:%02s"
1135 (org-split-string time ":"))))))
1136 clock info))
1139 ;;;; Code
1141 (defun org-ascii-code (code contents info)
1142 "Return a CODE object from Org to ASCII.
1143 CONTENTS is nil. INFO is a plist holding contextual
1144 information."
1145 (format (plist-get info :ascii-verbatim-format)
1146 (org-element-property :value code)))
1149 ;;;; Drawer
1151 (defun org-ascii-drawer (drawer contents info)
1152 "Transcode a DRAWER element from Org to ASCII.
1153 CONTENTS holds the contents of the block. INFO is a plist
1154 holding contextual information."
1155 (let ((name (org-element-property :drawer-name drawer))
1156 (width (org-ascii--current-text-width drawer info)))
1157 (funcall (plist-get info :ascii-format-drawer-function)
1158 name contents width)))
1161 ;;;; Dynamic Block
1163 (defun org-ascii-dynamic-block (dynamic-block contents info)
1164 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1165 CONTENTS holds the contents of the block. INFO is a plist
1166 holding contextual information."
1167 contents)
1170 ;;;; Entity
1172 (defun org-ascii-entity (entity contents info)
1173 "Transcode an ENTITY object from Org to ASCII.
1174 CONTENTS are the definition itself. INFO is a plist holding
1175 contextual information."
1176 (org-element-property
1177 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1178 entity))
1181 ;;;; Example Block
1183 (defun org-ascii-example-block (example-block contents info)
1184 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1185 CONTENTS is nil. INFO is a plist holding contextual information."
1186 (org-ascii--justify-element
1187 (org-ascii--box-string
1188 (org-export-format-code-default example-block info) info)
1189 example-block info))
1192 ;;;; Export Snippet
1194 (defun org-ascii-export-snippet (export-snippet contents info)
1195 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1196 CONTENTS is nil. INFO is a plist holding contextual information."
1197 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1198 (org-element-property :value export-snippet)))
1201 ;;;; Export Block
1203 (defun org-ascii-export-block (export-block contents info)
1204 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1205 CONTENTS is nil. INFO is a plist holding contextual information."
1206 (when (string= (org-element-property :type export-block) "ASCII")
1207 (org-ascii--justify-element
1208 (org-element-property :value export-block) export-block info)))
1211 ;;;; Fixed Width
1213 (defun org-ascii-fixed-width (fixed-width contents info)
1214 "Transcode a FIXED-WIDTH element from Org to ASCII.
1215 CONTENTS is nil. INFO is a plist holding contextual information."
1216 (org-ascii--justify-element
1217 (org-ascii--box-string
1218 (org-remove-indentation
1219 (org-element-property :value fixed-width)) info)
1220 fixed-width info))
1223 ;;;; Footnote Definition
1225 ;; Footnote Definitions are ignored. They are compiled at the end of
1226 ;; the document, by `org-ascii-inner-template'.
1229 ;;;; Footnote Reference
1231 (defun org-ascii-footnote-reference (footnote-reference contents info)
1232 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1233 CONTENTS is nil. INFO is a plist holding contextual information."
1234 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1237 ;;;; Headline
1239 (defun org-ascii-headline (headline contents info)
1240 "Transcode a HEADLINE element from Org to ASCII.
1241 CONTENTS holds the contents of the headline. INFO is a plist
1242 holding contextual information."
1243 ;; Don't export footnote section, which will be handled at the end
1244 ;; of the template.
1245 (unless (org-element-property :footnote-section-p headline)
1246 (let* ((low-level-rank (org-export-low-level-p headline info))
1247 (width (org-ascii--current-text-width headline info))
1248 ;; Blank lines between headline and its contents.
1249 ;; `org-ascii-headline-spacing', when set, overwrites
1250 ;; original buffer's spacing.
1251 (pre-blanks
1252 (make-string
1253 (or (car (plist-get info :ascii-headline-spacing))
1254 (org-element-property :pre-blank headline))
1255 ?\n))
1256 ;; Even if HEADLINE has no section, there might be some
1257 ;; links in its title that we shouldn't forget to describe.
1258 (links
1259 (unless (or (eq (caar (org-element-contents headline)) 'section))
1260 (let ((title (org-element-property :title headline)))
1261 (when (consp title)
1262 (org-ascii--describe-links
1263 (org-ascii--unique-links title info) width info))))))
1264 ;; Deep subtree: export it as a list item.
1265 (if low-level-rank
1266 (concat
1267 ;; Bullet.
1268 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1269 (plist-get info :ascii-bullets)))))
1270 (char-to-string
1271 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1273 ;; Title.
1274 (org-ascii--build-title headline info width) "\n"
1275 ;; Contents, indented by length of bullet.
1276 pre-blanks
1277 (org-ascii--indent-string
1278 (concat contents
1279 (when (org-string-nw-p links) (concat "\n\n" links)))
1281 ;; Else: Standard headline.
1282 (concat
1283 (org-ascii--build-title headline info width 'underline)
1284 "\n" pre-blanks
1285 (concat (when (org-string-nw-p links) links) contents))))))
1288 ;;;; Horizontal Rule
1290 (defun org-ascii-horizontal-rule (horizontal-rule contents info)
1291 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1292 CONTENTS is nil. INFO is a plist holding contextual
1293 information."
1294 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1295 (spec-width
1296 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1297 (org-ascii--justify-lines
1298 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1299 (string-to-number spec-width)
1300 text-width)
1301 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1302 text-width 'center)))
1305 ;;;; Inline Src Block
1307 (defun org-ascii-inline-src-block (inline-src-block contents info)
1308 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1309 CONTENTS holds the contents of the item. INFO is a plist holding
1310 contextual information."
1311 (format (plist-get info :ascii-verbatim-format)
1312 (org-element-property :value inline-src-block)))
1315 ;;;; Inlinetask
1317 (defun org-ascii-format-inlinetask-default
1318 (todo type priority name tags contents width inlinetask info)
1319 "Format an inline task element for ASCII export.
1320 See `org-ascii-format-inlinetask-function' for a description
1321 of the parameters."
1322 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1323 (width (or width (plist-get info :ascii-inlinetask-width))))
1324 (org-ascii--indent-string
1325 (concat
1326 ;; Top line, with an additional blank line if not in UTF-8.
1327 (make-string width (if utf8p ?━ ?_)) "\n"
1328 (unless utf8p (concat (make-string width ? ) "\n"))
1329 ;; Add title. Fill it if wider than inlinetask.
1330 (let ((title (org-ascii--build-title inlinetask info width)))
1331 (if (<= (string-width title) width) title
1332 (org-ascii--fill-string title width info)))
1333 "\n"
1334 ;; If CONTENTS is not empty, insert it along with
1335 ;; a separator.
1336 (when (org-string-nw-p contents)
1337 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1338 ;; Bottom line.
1339 (make-string width (if utf8p ?━ ?_)))
1340 ;; Flush the inlinetask to the right.
1341 (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
1342 (if (not (org-export-get-parent-headline inlinetask)) 0
1343 (plist-get info :ascii-inner-margin))
1344 (org-ascii--current-text-width inlinetask info)))))
1346 (defun org-ascii-inlinetask (inlinetask contents info)
1347 "Transcode an INLINETASK element from Org to ASCII.
1348 CONTENTS holds the contents of the block. INFO is a plist
1349 holding contextual information."
1350 (let ((width (org-ascii--current-text-width inlinetask info)))
1351 (funcall (plist-get info :ascii-format-inlinetask-function)
1352 ;; todo.
1353 (and (plist-get info :with-todo-keywords)
1354 (let ((todo (org-element-property
1355 :todo-keyword inlinetask)))
1356 (and todo (org-export-data todo info))))
1357 ;; todo-type
1358 (org-element-property :todo-type inlinetask)
1359 ;; priority
1360 (and (plist-get info :with-priority)
1361 (org-element-property :priority inlinetask))
1362 ;; title
1363 (org-export-data (org-element-property :title inlinetask) info)
1364 ;; tags
1365 (and (plist-get info :with-tags)
1366 (org-element-property :tags inlinetask))
1367 ;; contents and width
1368 contents width inlinetask info)))
1371 ;;;; Italic
1373 (defun org-ascii-italic (italic contents info)
1374 "Transcode italic from Org to ASCII.
1375 CONTENTS is the text with italic markup. INFO is a plist holding
1376 contextual information."
1377 (format "/%s/" contents))
1380 ;;;; Item
1382 (defun org-ascii-item (item contents info)
1383 "Transcode an ITEM element from Org to ASCII.
1384 CONTENTS holds the contents of the item. INFO is a plist holding
1385 contextual information."
1386 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1387 (checkbox (org-ascii--checkbox item info))
1388 (list-type (org-element-property :type (org-export-get-parent item)))
1389 (bullet
1390 ;; First parent of ITEM is always the plain-list. Get
1391 ;; `:type' property from it.
1392 (org-list-bullet-string
1393 (case list-type
1394 (descriptive
1395 (concat checkbox
1396 (org-export-data (org-element-property :tag item) info)
1397 ": "))
1398 (ordered
1399 ;; Return correct number for ITEM, paying attention to
1400 ;; counters.
1401 (let* ((struct (org-element-property :structure item))
1402 (bul (org-element-property :bullet item))
1403 (num (number-to-string
1404 (car (last (org-list-get-item-number
1405 (org-element-property :begin item)
1406 struct
1407 (org-list-prevs-alist struct)
1408 (org-list-parents-alist struct)))))))
1409 (replace-regexp-in-string "[0-9]+" num bul)))
1410 (t (let ((bul (org-element-property :bullet item)))
1411 ;; Change bullets into more visible form if UTF-8 is active.
1412 (if (not utf8p) bul
1413 (replace-regexp-in-string
1414 "-" "•"
1415 (replace-regexp-in-string
1416 "+" "⁃"
1417 (replace-regexp-in-string "*" "‣" bul))))))))))
1418 (concat
1419 bullet
1420 (unless (eq list-type 'descriptive) checkbox)
1421 ;; Contents: Pay attention to indentation. Note: check-boxes are
1422 ;; already taken care of at the paragraph level so they don't
1423 ;; interfere with indentation.
1424 (let ((contents (org-ascii--indent-string contents (string-width bullet))))
1425 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1426 (org-trim contents)
1427 (concat "\n" contents))))))
1430 ;;;; Keyword
1432 (defun org-ascii-keyword (keyword contents info)
1433 "Transcode a KEYWORD element from Org to ASCII.
1434 CONTENTS is nil. INFO is a plist holding contextual
1435 information."
1436 (let ((key (org-element-property :key keyword)))
1437 (cond
1438 ((string= key "ASCII")
1439 (org-ascii--justify-element
1440 (org-element-property :value keyword) keyword info))
1441 ((string= key "TOC")
1442 (org-ascii--justify-element
1443 (let ((value (downcase (org-element-property :value keyword))))
1444 (cond
1445 ((string-match "\\<headlines\\>" value)
1446 (let ((depth (or (and (string-match "[0-9]+" value)
1447 (string-to-number (match-string 0 value)))
1448 (plist-get info :with-toc))))
1449 (org-ascii--build-toc
1450 info (and (wholenump depth) depth) keyword)))
1451 ((string= "tables" value)
1452 (org-ascii--list-tables keyword info))
1453 ((string= "listings" value)
1454 (org-ascii--list-listings keyword info))))
1455 keyword info)))))
1458 ;;;; Latex Environment
1460 (defun org-ascii-latex-environment (latex-environment contents info)
1461 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1462 CONTENTS is nil. INFO is a plist holding contextual
1463 information."
1464 (when (plist-get info :with-latex)
1465 (org-ascii--justify-element
1466 (org-remove-indentation (org-element-property :value latex-environment))
1467 latex-environment info)))
1470 ;;;; Latex Fragment
1472 (defun org-ascii-latex-fragment (latex-fragment contents info)
1473 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1474 CONTENTS is nil. INFO is a plist holding contextual
1475 information."
1476 (when (plist-get info :with-latex)
1477 (org-element-property :value latex-fragment)))
1480 ;;;; Line Break
1482 (defun org-ascii-line-break (line-break contents info)
1483 "Transcode a LINE-BREAK object from Org to ASCII.
1484 CONTENTS is nil. INFO is a plist holding contextual
1485 information." hard-newline)
1488 ;;;; Link
1490 (defun org-ascii-link (link desc info)
1491 "Transcode a LINK object from Org to ASCII.
1493 DESC is the description part of the link, or the empty string.
1494 INFO is a plist holding contextual information."
1495 (let ((raw-link (org-element-property :raw-link link))
1496 (type (org-element-property :type link)))
1497 (cond
1498 ((string= type "coderef")
1499 (let ((ref (org-element-property :path link)))
1500 (format (org-export-get-coderef-format ref desc)
1501 (org-export-resolve-coderef ref info))))
1502 ;; Do not apply a special syntax on radio links. Though, use
1503 ;; transcoded target's contents as output.
1504 ((string= type "radio") desc)
1505 ;; Do not apply a special syntax on fuzzy links pointing to
1506 ;; targets.
1507 ((string= type "fuzzy")
1508 (let ((destination (org-export-resolve-fuzzy-link link info)))
1509 (if (org-string-nw-p desc) desc
1510 (when destination
1511 (let ((number
1512 (org-export-get-ordinal
1513 destination info nil 'org-ascii--has-caption-p)))
1514 (when number
1515 (if (atom number) (number-to-string number)
1516 (mapconcat 'number-to-string number "."))))))))
1518 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1519 (concat (format "[%s]" desc)
1520 (and (not (plist-get info :ascii-links-to-notes))
1521 (format " (%s)" raw-link))))))))
1524 ;;;; Node Properties
1526 (defun org-ascii-node-property (node-property contents info)
1527 "Transcode a NODE-PROPERTY element from Org to ASCII.
1528 CONTENTS is nil. INFO is a plist holding contextual
1529 information."
1530 (format "%s:%s"
1531 (org-element-property :key node-property)
1532 (let ((value (org-element-property :value node-property)))
1533 (if value (concat " " value) ""))))
1536 ;;;; Paragraph
1538 (defun org-ascii-paragraph (paragraph contents info)
1539 "Transcode a PARAGRAPH element from Org to ASCII.
1540 CONTENTS is the contents of the paragraph, as a string. INFO is
1541 the plist used as a communication channel."
1542 (org-ascii--justify-element
1543 (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
1544 (if (not (wholenump indented-line-width)) contents
1545 (concat
1546 ;; Do not indent first paragraph in a section.
1547 (unless (and (not (org-export-get-previous-element paragraph info))
1548 (eq (org-element-type (org-export-get-parent paragraph))
1549 'section))
1550 (make-string indented-line-width ?\s))
1551 (replace-regexp-in-string "\\`[ \t]+" "" contents))))
1552 paragraph info))
1555 ;;;; Plain List
1557 (defun org-ascii-plain-list (plain-list contents info)
1558 "Transcode a PLAIN-LIST element from Org to ASCII.
1559 CONTENTS is the contents of the list. INFO is a plist holding
1560 contextual information."
1561 contents)
1564 ;;;; Plain Text
1566 (defun org-ascii-plain-text (text info)
1567 "Transcode a TEXT string from Org to ASCII.
1568 INFO is a plist used as a communication channel."
1569 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1570 (when (and utf8p (plist-get info :with-smart-quotes))
1571 (setq text (org-export-activate-smart-quotes text :utf-8 info)))
1572 (if (not (plist-get info :with-special-strings)) text
1573 (setq text (replace-regexp-in-string "\\\\-" "" text))
1574 (if (not utf8p) text
1575 ;; Usual replacements in utf-8 with proper option set.
1576 (replace-regexp-in-string
1577 "\\.\\.\\." "…"
1578 (replace-regexp-in-string
1579 "--" "–"
1580 (replace-regexp-in-string "---" "—" text)))))))
1583 ;;;; Planning
1585 (defun org-ascii-planning (planning contents info)
1586 "Transcode a PLANNING element from Org to ASCII.
1587 CONTENTS is nil. INFO is a plist used as a communication
1588 channel."
1589 (org-ascii--justify-element
1590 (mapconcat
1591 #'identity
1592 (delq nil
1593 (list (let ((closed (org-element-property :closed planning)))
1594 (when closed
1595 (concat org-closed-string " "
1596 (org-translate-time
1597 (org-element-property :raw-value closed)))))
1598 (let ((deadline (org-element-property :deadline planning)))
1599 (when deadline
1600 (concat org-deadline-string " "
1601 (org-translate-time
1602 (org-element-property :raw-value deadline)))))
1603 (let ((scheduled (org-element-property :scheduled planning)))
1604 (when scheduled
1605 (concat org-scheduled-string " "
1606 (org-translate-time
1607 (org-element-property :raw-value scheduled)))))))
1608 " ")
1609 planning info))
1612 ;;;; Property Drawer
1614 (defun org-ascii-property-drawer (property-drawer contents info)
1615 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1616 CONTENTS holds the contents of the drawer. INFO is a plist
1617 holding contextual information."
1618 (and (org-string-nw-p contents)
1619 (org-ascii--justify-element contents property-drawer info)))
1622 ;;;; Quote Block
1624 (defun org-ascii-quote-block (quote-block contents info)
1625 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1626 CONTENTS holds the contents of the block. INFO is a plist
1627 holding contextual information."
1628 (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
1631 ;;;; Radio Target
1633 (defun org-ascii-radio-target (radio-target contents info)
1634 "Transcode a RADIO-TARGET object from Org to ASCII.
1635 CONTENTS is the contents of the target. INFO is a plist holding
1636 contextual information."
1637 contents)
1640 ;;;; Section
1642 (defun org-ascii-section (section contents info)
1643 "Transcode a SECTION element from Org to ASCII.
1644 CONTENTS is the contents of the section. INFO is a plist holding
1645 contextual information."
1646 (org-ascii--indent-string
1647 (concat
1648 contents
1649 (when (plist-get info :ascii-links-to-notes)
1650 ;; Add list of links at the end of SECTION.
1651 (let ((links (org-ascii--describe-links
1652 (org-ascii--unique-links section info)
1653 (org-ascii--current-text-width section info) info)))
1654 ;; Separate list of links and section contents.
1655 (when (org-string-nw-p links) (concat "\n\n" links)))))
1656 ;; Do not apply inner margin if parent headline is low level.
1657 (let ((headline (org-export-get-parent-headline section)))
1658 (if (or (not headline) (org-export-low-level-p headline info)) 0
1659 (plist-get info :ascii-inner-margin)))))
1662 ;;;; Special Block
1664 (defun org-ascii-special-block (special-block contents info)
1665 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1666 CONTENTS holds the contents of the block. INFO is a plist
1667 holding contextual information."
1668 ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
1669 ;; at a lower level. There is no other special block type to
1670 ;; handle.
1671 contents)
1674 ;;;; Src Block
1676 (defun org-ascii-src-block (src-block contents info)
1677 "Transcode a SRC-BLOCK element from Org to ASCII.
1678 CONTENTS holds the contents of the item. INFO is a plist holding
1679 contextual information."
1680 (let ((caption (org-ascii--build-caption src-block info))
1681 (caption-above-p (plist-get info :ascii-caption-above))
1682 (code (org-export-format-code-default src-block info)))
1683 (if (equal code "") ""
1684 (org-ascii--justify-element
1685 (concat
1686 (and caption caption-above-p (concat caption "\n"))
1687 (org-ascii--box-string code info)
1688 (and caption (not caption-above-p) (concat "\n" caption)))
1689 src-block info))))
1692 ;;;; Statistics Cookie
1694 (defun org-ascii-statistics-cookie (statistics-cookie contents info)
1695 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1696 CONTENTS is nil. INFO is a plist holding contextual information."
1697 (org-element-property :value statistics-cookie))
1700 ;;;; Subscript
1702 (defun org-ascii-subscript (subscript contents info)
1703 "Transcode a SUBSCRIPT 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 subscript)
1707 (format "_{%s}" contents)
1708 (format "_%s" contents)))
1711 ;;;; Superscript
1713 (defun org-ascii-superscript (superscript contents info)
1714 "Transcode a SUPERSCRIPT object from Org to ASCII.
1715 CONTENTS is the contents of the object. INFO is a plist holding
1716 contextual information."
1717 (if (org-element-property :use-brackets-p superscript)
1718 (format "^{%s}" contents)
1719 (format "^%s" contents)))
1722 ;;;; Strike-through
1724 (defun org-ascii-strike-through (strike-through contents info)
1725 "Transcode STRIKE-THROUGH from Org to ASCII.
1726 CONTENTS is text with strike-through markup. INFO is a plist
1727 holding contextual information."
1728 (format "+%s+" contents))
1731 ;;;; Table
1733 (defun org-ascii-table (table contents info)
1734 "Transcode a TABLE element from Org to ASCII.
1735 CONTENTS is the contents of the table. INFO is a plist holding
1736 contextual information."
1737 (let ((caption (org-ascii--build-caption table info))
1738 (caption-above-p (plist-get info :ascii-caption-above)))
1739 (org-ascii--justify-element
1740 (concat
1741 ;; Possibly add a caption string above.
1742 (and caption caption-above-p (concat caption "\n"))
1743 ;; Insert table. Note: "table.el" tables are left unmodified.
1744 (cond ((eq (org-element-property :type table) 'org) contents)
1745 ((and (plist-get info :ascii-table-use-ascii-art)
1746 (eq (plist-get info :ascii-charset) 'utf-8)
1747 (require 'ascii-art-to-unicode nil t))
1748 (with-temp-buffer
1749 (insert (org-remove-indentation
1750 (org-element-property :value table)))
1751 (goto-char (point-min))
1752 (aa2u)
1753 (goto-char (point-max))
1754 (skip-chars-backward " \r\t\n")
1755 (buffer-substring (point-min) (point))))
1756 (t (org-remove-indentation (org-element-property :value table))))
1757 ;; Possible add a caption string below.
1758 (and (not caption-above-p) caption))
1759 table info)))
1762 ;;;; Table Cell
1764 (defun org-ascii--table-cell-width (table-cell info)
1765 "Return width of TABLE-CELL.
1767 INFO is a plist used as a communication channel.
1769 Width of a cell is determined either by a width cookie in the
1770 same column as the cell, or by the maximum cell's length in that
1771 column.
1773 When `org-ascii-table-widen-columns' is non-nil, width cookies
1774 are ignored."
1775 (let* ((row (org-export-get-parent table-cell))
1776 (table (org-export-get-parent row))
1777 (col (let ((cells (org-element-contents row)))
1778 (- (length cells) (length (memq table-cell cells)))))
1779 (cache
1780 (or (plist-get info :ascii-table-cell-width-cache)
1781 (plist-get (setq info
1782 (plist-put info :ascii-table-cell-width-cache
1783 (make-hash-table :test 'equal)))
1784 :ascii-table-cell-width-cache)))
1785 (key (cons table col))
1786 (widenp (plist-get info :ascii-table-widen-columns)))
1787 (or (gethash key cache)
1788 (puthash
1790 (let ((cookie-width (org-export-table-cell-width table-cell info)))
1791 (or (and (not widenp) cookie-width)
1792 (let ((contents-width
1793 (let ((max-width 0))
1794 (org-element-map table 'table-row
1795 (lambda (row)
1796 (setq max-width
1797 (max (string-width
1798 (org-export-data
1799 (org-element-contents
1800 (elt (org-element-contents row) col))
1801 info))
1802 max-width)))
1803 info)
1804 max-width)))
1805 (cond ((not cookie-width) contents-width)
1806 (widenp (max cookie-width contents-width))
1807 (t cookie-width)))))
1808 cache))))
1810 (defun org-ascii-table-cell (table-cell contents info)
1811 "Transcode a TABLE-CELL object from Org to ASCII.
1812 CONTENTS is the cell contents. INFO is a plist used as
1813 a communication channel."
1814 ;; Determine column width. When `org-ascii-table-widen-columns'
1815 ;; is nil and some width cookie has set it, use that value.
1816 ;; Otherwise, compute the maximum width among transcoded data of
1817 ;; each cell in the column.
1818 (let ((width (org-ascii--table-cell-width table-cell info)))
1819 ;; When contents are too large, truncate them.
1820 (unless (or (plist-get info :ascii-table-widen-columns)
1821 (<= (string-width (or contents "")) width))
1822 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1823 ;; Align contents correctly within the cell.
1824 (let* ((indent-tabs-mode nil)
1825 (data
1826 (when contents
1827 (org-ascii--justify-lines
1828 contents width
1829 (org-export-table-cell-alignment table-cell info)))))
1830 (setq contents
1831 (concat data
1832 (make-string (- width (string-width (or data ""))) ?\s))))
1833 ;; Return cell.
1834 (concat (format " %s " contents)
1835 (when (memq 'right (org-export-table-cell-borders table-cell info))
1836 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1839 ;;;; Table Row
1841 (defun org-ascii-table-row (table-row contents info)
1842 "Transcode a TABLE-ROW element from Org to ASCII.
1843 CONTENTS is the row contents. INFO is a plist used as
1844 a communication channel."
1845 (when (eq (org-element-property :type table-row) 'standard)
1846 (let ((build-hline
1847 (function
1848 (lambda (lcorner horiz vert rcorner)
1849 (concat
1850 (apply
1851 'concat
1852 (org-element-map table-row 'table-cell
1853 (lambda (cell)
1854 (let ((width (org-ascii--table-cell-width cell info))
1855 (borders (org-export-table-cell-borders cell info)))
1856 (concat
1857 ;; In order to know if CELL starts the row, do
1858 ;; not compare it with the first cell in the
1859 ;; row as there might be a special column.
1860 ;; Instead, compare it with first exportable
1861 ;; cell, obtained with `org-element-map'.
1862 (when (and (memq 'left borders)
1863 (eq (org-element-map table-row 'table-cell
1864 'identity info t)
1865 cell))
1866 lcorner)
1867 (make-string (+ 2 width) (string-to-char horiz))
1868 (cond
1869 ((not (memq 'right borders)) nil)
1870 ((eq (car (last (org-element-contents table-row))) cell)
1871 rcorner)
1872 (t vert)))))
1873 info)) "\n"))))
1874 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1875 (borders (org-export-table-cell-borders
1876 (org-element-map table-row 'table-cell 'identity info t)
1877 info)))
1878 (concat (cond
1879 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1880 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1881 (funcall build-hline "+" "-" "+" "+")))
1882 ((memq 'above borders)
1883 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1884 (funcall build-hline "+" "-" "+" "+"))))
1885 (when (memq 'left borders) (if utf8p "│" "|"))
1886 contents "\n"
1887 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1888 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1889 (funcall build-hline "+" "-" "+" "+")))))))
1892 ;;;; Timestamp
1894 (defun org-ascii-timestamp (timestamp contents info)
1895 "Transcode a TIMESTAMP object from Org to ASCII.
1896 CONTENTS is nil. INFO is a plist holding contextual information."
1897 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1900 ;;;; Underline
1902 (defun org-ascii-underline (underline contents info)
1903 "Transcode UNDERLINE from Org to ASCII.
1904 CONTENTS is the text with underline markup. INFO is a plist
1905 holding contextual information."
1906 (format "_%s_" contents))
1909 ;;;; Verbatim
1911 (defun org-ascii-verbatim (verbatim contents info)
1912 "Return a VERBATIM object from Org to ASCII.
1913 CONTENTS is nil. INFO is a plist holding contextual information."
1914 (format (plist-get info :ascii-verbatim-format)
1915 (org-element-property :value verbatim)))
1918 ;;;; Verse Block
1920 (defun org-ascii-verse-block (verse-block contents info)
1921 "Transcode a VERSE-BLOCK element from Org to ASCII.
1922 CONTENTS is verse block contents. INFO is a plist holding
1923 contextual information."
1924 (let ((verse-width (org-ascii--current-text-width verse-block info)))
1925 (org-ascii--indent-string
1926 (org-ascii--justify-element contents verse-block info)
1927 (plist-get info :ascii-quote-margin))))
1931 ;;; Filters
1933 (defun org-ascii-filter-headline-blank-lines (headline back-end info)
1934 "Filter controlling number of blank lines after a headline.
1936 HEADLINE is a string representing a transcoded headline.
1937 BACK-END is symbol specifying back-end used for export. INFO is
1938 plist containing the communication channel.
1940 This function only applies to `ascii' back-end. See
1941 `org-ascii-headline-spacing' for information."
1942 (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
1943 (if (not headline-spacing) headline
1944 (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
1945 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
1947 (defun org-ascii-filter-paragraph-spacing (tree back-end info)
1948 "Filter controlling number of blank lines between paragraphs.
1950 TREE is the parse tree. BACK-END is the symbol specifying
1951 back-end used for export. INFO is a plist used as
1952 a communication channel.
1954 See `org-ascii-paragraph-spacing' for information."
1955 (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
1956 (when (wholenump paragraph-spacing)
1957 (org-element-map tree 'paragraph
1958 (lambda (p)
1959 (when (eq (org-element-type (org-export-get-next-element p info))
1960 'paragraph)
1961 (org-element-put-property p :post-blank paragraph-spacing))))))
1962 tree)
1964 (defun org-ascii-filter-comment-spacing (tree backend info)
1965 "Filter removing blank lines between comments.
1966 TREE is the parse tree. BACK-END is the symbol specifying
1967 back-end used for export. INFO is a plist used as
1968 a communication channel."
1969 (org-element-map tree '(comment comment-block)
1970 (lambda (c)
1971 (when (memq (org-element-type (org-export-get-next-element c info))
1972 '(comment comment-block))
1973 (org-element-put-property c :post-blank 0))))
1974 tree)
1978 ;;; End-user functions
1980 ;;;###autoload
1981 (defun org-ascii-export-as-ascii
1982 (&optional async subtreep visible-only body-only ext-plist)
1983 "Export current buffer to a text buffer.
1985 If narrowing is active in the current buffer, only export its
1986 narrowed part.
1988 If a region is active, export that region.
1990 A non-nil optional argument ASYNC means the process should happen
1991 asynchronously. The resulting buffer should be accessible
1992 through the `org-export-stack' interface.
1994 When optional argument SUBTREEP is non-nil, export the sub-tree
1995 at point, extracting information from the headline properties
1996 first.
1998 When optional argument VISIBLE-ONLY is non-nil, don't export
1999 contents of hidden elements.
2001 When optional argument BODY-ONLY is non-nil, strip title and
2002 table of contents from output.
2004 EXT-PLIST, when provided, is a property list with external
2005 parameters overriding Org default settings, but still inferior to
2006 file-local settings.
2008 Export is done in a buffer named \"*Org ASCII Export*\", which
2009 will be displayed when `org-export-show-temporary-export-buffer'
2010 is non-nil."
2011 (interactive)
2012 (org-export-to-buffer 'ascii "*Org ASCII Export*"
2013 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
2015 ;;;###autoload
2016 (defun org-ascii-export-to-ascii
2017 (&optional async subtreep visible-only body-only ext-plist)
2018 "Export current buffer to a text file.
2020 If narrowing is active in the current buffer, only export its
2021 narrowed part.
2023 If a region is active, export that region.
2025 A non-nil optional argument ASYNC means the process should happen
2026 asynchronously. The resulting file should be accessible through
2027 the `org-export-stack' interface.
2029 When optional argument SUBTREEP is non-nil, export the sub-tree
2030 at point, extracting information from the headline properties
2031 first.
2033 When optional argument VISIBLE-ONLY is non-nil, don't export
2034 contents of hidden elements.
2036 When optional argument BODY-ONLY is non-nil, strip title and
2037 table of contents from output.
2039 EXT-PLIST, when provided, is a property list with external
2040 parameters overriding Org default settings, but still inferior to
2041 file-local settings.
2043 Return output file's name."
2044 (interactive)
2045 (let ((file (org-export-output-file-name ".txt" subtreep)))
2046 (org-export-to-file 'ascii file
2047 async subtreep visible-only body-only ext-plist)))
2049 ;;;###autoload
2050 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
2051 "Publish an Org file to ASCII.
2053 FILENAME is the filename of the Org file to be published. PLIST
2054 is the property list for the given project. PUB-DIR is the
2055 publishing directory.
2057 Return output file name."
2058 (org-publish-org-to
2059 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
2061 ;;;###autoload
2062 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
2063 "Publish an Org file to Latin-1.
2065 FILENAME is the filename of the Org file to be published. PLIST
2066 is the property list for the given project. PUB-DIR is the
2067 publishing directory.
2069 Return output file name."
2070 (org-publish-org-to
2071 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
2073 ;;;###autoload
2074 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
2075 "Publish an org file to UTF-8.
2077 FILENAME is the filename of the Org file to be published. PLIST
2078 is the property list for the given project. PUB-DIR is the
2079 publishing directory.
2081 Return output file name."
2082 (org-publish-org-to
2083 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2086 (provide 'ox-ascii)
2088 ;; Local variables:
2089 ;; generated-autoload-file: "org-loaddefs.el"
2090 ;; coding: utf-8-emacs
2091 ;; End:
2093 ;;; ox-ascii.el ends here