org-tempo.el: New file for expansion of templates
[org-mode.git] / lisp / ox-ascii.el
blob61c231b4461af23766d4608eace398a10facd77b
1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 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 <https://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 (require 'ox)
31 (require 'ox-publish)
32 (require 'cl-lib)
34 (declare-function aa2u "ext:ascii-art-to-unicode" ())
36 ;;; Define Back-End
38 ;; The following setting won't allow modifying preferred charset
39 ;; through a buffer keyword or an option item, but, since the property
40 ;; will appear in communication channel nonetheless, it allows
41 ;; overriding `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 (drawer . org-ascii-drawer)
53 (dynamic-block . org-ascii-dynamic-block)
54 (entity . org-ascii-entity)
55 (example-block . org-ascii-example-block)
56 (export-block . org-ascii-export-block)
57 (export-snippet . org-ascii-export-snippet)
58 (fixed-width . org-ascii-fixed-width)
59 (footnote-reference . org-ascii-footnote-reference)
60 (headline . org-ascii-headline)
61 (horizontal-rule . org-ascii-horizontal-rule)
62 (inline-src-block . org-ascii-inline-src-block)
63 (inlinetask . org-ascii-inlinetask)
64 (inner-template . org-ascii-inner-template)
65 (italic . org-ascii-italic)
66 (item . org-ascii-item)
67 (keyword . org-ascii-keyword)
68 (latex-environment . org-ascii-latex-environment)
69 (latex-fragment . org-ascii-latex-fragment)
70 (line-break . org-ascii-line-break)
71 (link . org-ascii-link)
72 (node-property . org-ascii-node-property)
73 (paragraph . org-ascii-paragraph)
74 (plain-list . org-ascii-plain-list)
75 (plain-text . org-ascii-plain-text)
76 (planning . org-ascii-planning)
77 (property-drawer . org-ascii-property-drawer)
78 (quote-block . org-ascii-quote-block)
79 (radio-target . org-ascii-radio-target)
80 (section . org-ascii-section)
81 (special-block . org-ascii-special-block)
82 (src-block . org-ascii-src-block)
83 (statistics-cookie . org-ascii-statistics-cookie)
84 (strike-through . org-ascii-strike-through)
85 (subscript . org-ascii-subscript)
86 (superscript . org-ascii-superscript)
87 (table . org-ascii-table)
88 (table-cell . org-ascii-table-cell)
89 (table-row . org-ascii-table-row)
90 (target . org-ascii-target)
91 (template . org-ascii-template)
92 (timestamp . org-ascii-timestamp)
93 (underline . org-ascii-underline)
94 (verbatim . org-ascii-verbatim)
95 (verse-block . org-ascii-verse-block))
96 :menu-entry
97 '(?t "Export to Plain Text"
98 ((?A "As ASCII buffer"
99 (lambda (a s v b)
100 (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
101 (?a "As ASCII file"
102 (lambda (a s v b)
103 (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
104 (?L "As Latin1 buffer"
105 (lambda (a s v b)
106 (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
107 (?l "As Latin1 file"
108 (lambda (a s v b)
109 (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
110 (?U "As UTF-8 buffer"
111 (lambda (a s v b)
112 (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
113 (?u "As UTF-8 file"
114 (lambda (a s v b)
115 (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
116 :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
117 (:filter-parse-tree org-ascii-filter-paragraph-spacing
118 org-ascii-filter-comment-spacing)
119 (:filter-section . org-ascii-filter-headline-blank-lines))
120 :options-alist
121 '((:subtitle "SUBTITLE" nil nil parse)
122 (:ascii-bullets nil nil org-ascii-bullets)
123 (:ascii-caption-above nil nil org-ascii-caption-above)
124 (:ascii-charset nil nil org-ascii-charset)
125 (:ascii-global-margin nil nil org-ascii-global-margin)
126 (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function)
127 (:ascii-format-inlinetask-function
128 nil nil org-ascii-format-inlinetask-function)
129 (:ascii-headline-spacing nil nil org-ascii-headline-spacing)
130 (:ascii-indented-line-width nil nil org-ascii-indented-line-width)
131 (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width)
132 (:ascii-inner-margin nil nil org-ascii-inner-margin)
133 (:ascii-links-to-notes nil nil org-ascii-links-to-notes)
134 (:ascii-list-margin nil nil org-ascii-list-margin)
135 (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing)
136 (:ascii-quote-margin nil nil org-ascii-quote-margin)
137 (:ascii-table-keep-all-vertical-lines
138 nil nil org-ascii-table-keep-all-vertical-lines)
139 (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art)
140 (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns)
141 (:ascii-text-width nil nil org-ascii-text-width)
142 (:ascii-underline nil nil org-ascii-underline)
143 (:ascii-verbatim-format nil nil org-ascii-verbatim-format)))
147 ;;; User Configurable Variables
149 (defgroup org-export-ascii nil
150 "Options for exporting Org mode files to ASCII."
151 :tag "Org Export ASCII"
152 :group 'org-export)
154 (defcustom org-ascii-text-width 72
155 "Maximum width of exported text.
156 This number includes margin size, as set in
157 `org-ascii-global-margin'."
158 :group 'org-export-ascii
159 :version "24.4"
160 :package-version '(Org . "8.0")
161 :type 'integer)
163 (defcustom org-ascii-global-margin 0
164 "Width of the left margin, in number of characters."
165 :group 'org-export-ascii
166 :version "24.4"
167 :package-version '(Org . "8.0")
168 :type 'integer)
170 (defcustom org-ascii-inner-margin 2
171 "Width of the inner margin, in number of characters.
172 Inner margin is applied between each headline."
173 :group 'org-export-ascii
174 :version "24.4"
175 :package-version '(Org . "8.0")
176 :type 'integer)
178 (defcustom org-ascii-quote-margin 6
179 "Width of margin used for quoting text, in characters.
180 This margin is applied on both sides of the text. It is also
181 applied on the left side of contents in descriptive lists."
182 :group 'org-export-ascii
183 :version "24.4"
184 :package-version '(Org . "8.0")
185 :type 'integer)
187 (defcustom org-ascii-list-margin 0
188 "Width of margin used for plain lists, in characters.
189 This margin applies to top level list only, not to its
190 sub-lists."
191 :group 'org-export-ascii
192 :version "26.1"
193 :package-version '(Org . "8.3")
194 :type 'integer)
196 (defcustom org-ascii-inlinetask-width 30
197 "Width of inline tasks, in number of characters.
198 This number ignores any margin."
199 :group 'org-export-ascii
200 :version "24.4"
201 :package-version '(Org . "8.0")
202 :type 'integer)
204 (defcustom org-ascii-headline-spacing '(1 . 2)
205 "Number of blank lines inserted around headlines.
207 This variable can be set to a cons cell. In that case, its car
208 represents the number of blank lines present before headline
209 contents whereas its cdr reflects the number of blank lines after
210 contents.
212 A nil value replicates the number of blank lines found in the
213 original Org buffer at the same place."
214 :group 'org-export-ascii
215 :version "24.4"
216 :package-version '(Org . "8.0")
217 :type '(choice
218 (const :tag "Replicate original spacing" nil)
219 (cons :tag "Set a uniform spacing"
220 (integer :tag "Number of blank lines before contents")
221 (integer :tag "Number of blank lines after contents"))))
223 (defcustom org-ascii-indented-line-width 'auto
224 "Additional indentation width for the first line in a paragraph.
225 If the value is an integer, indent the first line of each
226 paragraph by this width, unless it is located at the beginning of
227 a section, in which case indentation is removed from that line.
228 If it is the symbol `auto' preserve indentation from original
229 document."
230 :group 'org-export-ascii
231 :version "24.4"
232 :package-version '(Org . "8.0")
233 :type '(choice
234 (integer :tag "Number of white spaces characters")
235 (const :tag "Preserve original width" auto)))
237 (defcustom org-ascii-paragraph-spacing 'auto
238 "Number of white lines between paragraphs.
239 If the value is an integer, add this number of blank lines
240 between contiguous paragraphs. If is it the symbol `auto', keep
241 the same number of blank lines as in the original document."
242 :group 'org-export-ascii
243 :version "24.4"
244 :package-version '(Org . "8.0")
245 :type '(choice
246 (integer :tag "Number of blank lines")
247 (const :tag "Preserve original spacing" auto)))
249 (defcustom org-ascii-charset 'ascii
250 "The charset allowed to represent various elements and objects.
251 Possible values are:
252 `ascii' Only use plain ASCII characters
253 `latin1' Include Latin-1 characters
254 `utf-8' Use all UTF-8 characters"
255 :group 'org-export-ascii
256 :version "24.4"
257 :package-version '(Org . "8.0")
258 :type '(choice
259 (const :tag "ASCII" ascii)
260 (const :tag "Latin-1" latin1)
261 (const :tag "UTF-8" utf-8)))
263 (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
264 (latin1 ?= ?~ ?-)
265 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
266 "Characters for underlining headings in ASCII export.
268 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
269 and whose value is a list of characters.
271 For each supported charset, this variable associates a sequence
272 of underline characters. In a sequence, the characters will be
273 used in order for headlines level 1, 2, ... If no character is
274 available for a given level, the headline won't be underlined."
275 :group 'org-export-ascii
276 :version "24.4"
277 :package-version '(Org . "8.0")
278 :type '(list
279 (cons :tag "Underline characters sequence"
280 (const :tag "ASCII charset" ascii)
281 (repeat character))
282 (cons :tag "Underline characters sequence"
283 (const :tag "Latin-1 charset" latin1)
284 (repeat character))
285 (cons :tag "Underline characters sequence"
286 (const :tag "UTF-8 charset" utf-8)
287 (repeat character))))
289 (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
290 (latin1 ?§ ?¶)
291 (utf-8 ?◊))
292 "Bullet characters for headlines converted to lists in ASCII export.
294 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
295 and whose value is a list of characters.
297 The first character is used for the first level considered as low
298 level, and so on. If there are more levels than characters given
299 here, the list will be repeated.
301 Note that this variable doesn't affect plain lists
302 representation."
303 :group 'org-export-ascii
304 :version "24.4"
305 :package-version '(Org . "8.0")
306 :type '(list
307 (cons :tag "Bullet characters for low level headlines"
308 (const :tag "ASCII charset" ascii)
309 (repeat character))
310 (cons :tag "Bullet characters for low level headlines"
311 (const :tag "Latin-1 charset" latin1)
312 (repeat character))
313 (cons :tag "Bullet characters for low level headlines"
314 (const :tag "UTF-8 charset" utf-8)
315 (repeat character))))
317 (defcustom org-ascii-links-to-notes t
318 "Non-nil means convert links to notes before the next headline.
319 When nil, the link will be exported in place. If the line
320 becomes long in this way, it will be wrapped."
321 :group 'org-export-ascii
322 :version "24.4"
323 :package-version '(Org . "8.0")
324 :type 'boolean)
326 (defcustom org-ascii-table-keep-all-vertical-lines nil
327 "Non-nil means keep all vertical lines in ASCII tables.
328 When nil, vertical lines will be removed except for those needed
329 for column grouping."
330 :group 'org-export-ascii
331 :version "24.4"
332 :package-version '(Org . "8.0")
333 :type 'boolean)
335 (defcustom org-ascii-table-widen-columns t
336 "Non-nil means widen narrowed columns for export.
337 When nil, narrowed columns will look in ASCII export just like in
338 Org mode, i.e. with \"=>\" as ellipsis."
339 :group 'org-export-ascii
340 :version "24.4"
341 :package-version '(Org . "8.0")
342 :type 'boolean)
344 (defcustom org-ascii-table-use-ascii-art nil
345 "Non-nil means \"table.el\" tables are turned into ASCII art.
346 It only makes sense when export charset is `utf-8'. It is nil by
347 default since it requires \"ascii-art-to-unicode.el\" package,
348 available through, e.g., GNU ELPA."
349 :group 'org-export-ascii
350 :version "24.4"
351 :package-version '(Org . "8.0")
352 :type 'boolean)
354 (defcustom org-ascii-caption-above nil
355 "When non-nil, place caption string before the element.
356 Otherwise, place it right after it."
357 :group 'org-export-ascii
358 :version "24.4"
359 :package-version '(Org . "8.0")
360 :type 'boolean)
362 (defcustom org-ascii-verbatim-format "`%s'"
363 "Format string used for verbatim text and inline code."
364 :group 'org-export-ascii
365 :version "24.4"
366 :package-version '(Org . "8.0")
367 :type 'string)
369 (defcustom org-ascii-format-drawer-function
370 (lambda (_name contents _width) contents)
371 "Function called to format a drawer in ASCII.
373 The function must accept three parameters:
374 NAME the drawer name, like \"LOGBOOK\"
375 CONTENTS the contents of the drawer.
376 WIDTH the text width within the drawer.
378 The function should return either the string to be exported or
379 nil to ignore the drawer.
381 The default value simply returns the value of CONTENTS."
382 :group 'org-export-ascii
383 :version "24.4"
384 :package-version '(Org . "8.0")
385 :type 'function)
387 (defcustom org-ascii-format-inlinetask-function
388 'org-ascii-format-inlinetask-default
389 "Function called to format an inlinetask in ASCII.
391 The function must accept nine parameters:
392 TODO the todo keyword, as a string
393 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
394 PRIORITY the inlinetask priority, as a string
395 NAME the inlinetask name, as a string.
396 TAGS the inlinetask tags, as a list of strings.
397 CONTENTS the contents of the inlinetask, as a string.
398 WIDTH the width of the inlinetask, as a number.
399 INLINETASK the inlinetask itself.
400 INFO the info channel.
402 The function should return either the string to be exported or
403 nil to ignore the inline task."
404 :group 'org-export-ascii
405 :version "26.1"
406 :package-version '(Org . "8.3")
407 :type 'function)
411 ;;; Internal Functions
413 ;; Internal functions fall into three categories.
415 ;; The first one is about text formatting. The core functions are
416 ;; `org-ascii--current-text-width' and
417 ;; `org-ascii--current-justification', which determine, respectively,
418 ;; the current text width allowed to a given element and its expected
419 ;; justification. Once this information is known,
420 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
421 ;; `org-ascii--justify-element' `org-ascii--box-string' and
422 ;; `org-ascii--indent-string' can operate on a given output string.
423 ;; In particular, justification happens at the regular (i.e.,
424 ;; non-greater) element level, which means that when the exporting
425 ;; process reaches a container (e.g., a center block) content are
426 ;; already justified.
428 ;; The second category contains functions handling elements listings,
429 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
430 ;; returns a complete table of contents, `org-ascii--list-listings'
431 ;; returns a list of referenceable src-block elements, and
432 ;; `org-ascii--list-tables' does the same for table elements.
434 ;; The third category includes general helper functions.
435 ;; `org-ascii--build-title' creates the title for a given headline
436 ;; or inlinetask element. `org-ascii--build-caption' returns the
437 ;; caption string associated to a table or a src-block.
438 ;; `org-ascii--describe-links' creates notes about links for
439 ;; insertion at the end of a section. It uses
440 ;; `org-ascii--unique-links' to get the list of links to describe.
441 ;; Eventually, `org-ascii--translate' translates a string according
442 ;; to language and charset specification.
445 (defun org-ascii--fill-string (s text-width info &optional justify)
446 "Fill a string with specified text-width and return it.
448 S is the string being filled. TEXT-WIDTH is an integer
449 specifying maximum length of a line. INFO is the plist used as
450 a communication channel.
452 Optional argument JUSTIFY can specify any type of justification
453 among `left', `center', `right' or `full'. A nil value is
454 equivalent to `left'. For a justification that doesn't also fill
455 string, see `org-ascii--justify-lines' and
456 `org-ascii--justify-block'.
458 Return nil if S isn't a string."
459 (when (stringp s)
460 (let ((double-space-p sentence-end-double-space))
461 (with-temp-buffer
462 (let ((fill-column text-width)
463 (use-hard-newlines t)
464 (sentence-end-double-space double-space-p))
465 (insert (if (plist-get info :preserve-breaks)
466 (replace-regexp-in-string "\n" hard-newline s)
468 (fill-region (point-min) (point-max) justify))
469 (buffer-string)))))
471 (defun org-ascii--justify-lines (s text-width how)
472 "Justify all lines in string S.
473 TEXT-WIDTH is an integer specifying maximum length of a line.
474 HOW determines the type of justification: it can be `left',
475 `right', `full' or `center'."
476 (with-temp-buffer
477 (insert s)
478 (goto-char (point-min))
479 (let ((fill-column text-width)
480 ;; Disable `adaptive-fill-mode' so it doesn't prevent
481 ;; filling lines matching `adaptive-fill-regexp'.
482 (adaptive-fill-mode nil))
483 (while (< (point) (point-max))
484 (justify-current-line how)
485 (forward-line)))
486 (buffer-string)))
488 (defun org-ascii--justify-element (contents element info)
489 "Justify CONTENTS of ELEMENT.
490 INFO is a plist used as a communication channel. Justification
491 is done according to the type of element. More accurately,
492 paragraphs are filled and other elements are justified as blocks,
493 that is according to the widest non blank line in CONTENTS."
494 (if (not (org-string-nw-p contents)) contents
495 (let ((text-width (org-ascii--current-text-width element info))
496 (how (org-ascii--current-justification element)))
497 (cond
498 ((eq (org-element-type element) 'paragraph)
499 ;; Paragraphs are treated specially as they need to be filled.
500 (org-ascii--fill-string contents text-width info how))
501 ((eq how 'left) contents)
502 (t (with-temp-buffer
503 (insert contents)
504 (goto-char (point-min))
505 (catch 'exit
506 (let ((max-width 0))
507 ;; Compute maximum width. Bail out if it is greater
508 ;; than page width, since no justification is
509 ;; possible.
510 (save-excursion
511 (while (not (eobp))
512 (unless (looking-at-p "[ \t]*$")
513 (end-of-line)
514 (let ((column (current-column)))
515 (cond
516 ((>= column text-width) (throw 'exit contents))
517 ((> column max-width) (setq max-width column)))))
518 (forward-line)))
519 ;; Justify every line according to TEXT-WIDTH and
520 ;; MAX-WIDTH.
521 (let ((offset (/ (- text-width max-width)
522 (if (eq how 'right) 1 2))))
523 (if (zerop offset) (throw 'exit contents)
524 (while (not (eobp))
525 (unless (looking-at-p "[ \t]*$")
526 (indent-to-column offset))
527 (forward-line)))))
528 (buffer-string))))))))
530 (defun org-ascii--indent-string (s width)
531 "Indent string S by WIDTH white spaces.
532 Empty lines are not indented."
533 (when (stringp s)
534 (replace-regexp-in-string
535 "\\(^\\)[ \t]*\\S-" (make-string width ?\s) s nil nil 1)))
537 (defun org-ascii--box-string (s info)
538 "Return string S with a partial box to its left.
539 INFO is a plist used as a communication channel."
540 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
541 (format (if utf8p "┌────\n%s\n└────" ",----\n%s\n`----")
542 (replace-regexp-in-string
543 "^" (if utf8p "│ " "| ")
544 ;; Remove last newline character.
545 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
547 (defun org-ascii--current-text-width (element info)
548 "Return maximum text width for ELEMENT's contents.
549 INFO is a plist used as a communication channel."
550 (pcase (org-element-type element)
551 ;; Elements with an absolute width: `headline' and `inlinetask'.
552 (`inlinetask (plist-get info :ascii-inlinetask-width))
553 (`headline
554 (- (plist-get info :ascii-text-width)
555 (let ((low-level-rank (org-export-low-level-p element info)))
556 (if low-level-rank (* low-level-rank 2)
557 (plist-get info :ascii-global-margin)))))
558 ;; Elements with a relative width: store maximum text width in
559 ;; TOTAL-WIDTH.
561 (let* ((genealogy (org-element-lineage element nil t))
562 ;; Total width is determined by the presence, or not, of an
563 ;; inline task among ELEMENT parents.
564 (total-width
565 (if (cl-some (lambda (parent)
566 (eq (org-element-type parent) 'inlinetask))
567 genealogy)
568 (plist-get info :ascii-inlinetask-width)
569 ;; No inlinetask: Remove global margin from text width.
570 (- (plist-get info :ascii-text-width)
571 (plist-get info :ascii-global-margin)
572 (let ((parent (org-export-get-parent-headline element)))
573 ;; Inner margin doesn't apply to text before first
574 ;; headline.
575 (if (not parent) 0
576 (let ((low-level-rank
577 (org-export-low-level-p parent info)))
578 ;; Inner margin doesn't apply to contents of
579 ;; low level headlines, since they've got their
580 ;; own indentation mechanism.
581 (if low-level-rank (* low-level-rank 2)
582 (plist-get info :ascii-inner-margin)))))))))
583 (- total-width
584 ;; Each `quote-block' and `verse-block' above narrows text
585 ;; width by twice the standard margin size.
586 (+ (* (cl-count-if (lambda (parent)
587 (memq (org-element-type parent)
588 '(quote-block verse-block)))
589 genealogy)
591 (plist-get info :ascii-quote-margin))
592 ;; Apply list margin once per "top-level" plain-list
593 ;; containing current line
594 (* (cl-count-if
595 (lambda (e)
596 (and (eq (org-element-type e) 'plain-list)
597 (not (eq (org-element-type (org-export-get-parent e))
598 'item))))
599 genealogy)
600 (plist-get info :ascii-list-margin))
601 ;; Compute indentation offset due to current list. It is
602 ;; `org-ascii-quote-margin' per descriptive item in the
603 ;; genealogy, bullet's length otherwise.
604 (let ((indentation 0))
605 (dolist (e genealogy)
606 (cond
607 ((not (eq 'item (org-element-type e))))
608 ((eq (org-element-property :type (org-export-get-parent e))
609 'descriptive)
610 (cl-incf indentation org-ascii-quote-margin))
612 (cl-incf indentation
613 (+ (string-width
614 (or (org-ascii--checkbox e info) ""))
615 (string-width
616 (org-element-property :bullet e)))))))
617 indentation)))))))
619 (defun org-ascii--current-justification (element)
620 "Return expected justification for ELEMENT's contents.
621 Return value is a symbol among `left', `center', `right' and
622 `full'."
623 (let (justification)
624 (while (and (not justification)
625 (setq element (org-element-property :parent element)))
626 (pcase (org-element-type element)
627 (`center-block (setq justification 'center))
628 (`special-block
629 (let ((name (org-element-property :type element)))
630 (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
631 ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
632 (or justification 'left)))
634 (defun org-ascii--build-title
635 (element info text-width &optional underline notags toc)
636 "Format ELEMENT title and return it.
638 ELEMENT is either an `headline' or `inlinetask' element. INFO is
639 a plist used as a communication channel. TEXT-WIDTH is an
640 integer representing the maximum length of a line.
642 When optional argument UNDERLINE is non-nil, underline title,
643 without the tags, according to `org-ascii-underline'
644 specifications.
646 If optional argument NOTAGS is non-nil, no tags will be added to
647 the title.
649 When optional argument TOC is non-nil, use optional title if
650 possible. It doesn't apply to `inlinetask' elements."
651 (let* ((headlinep (eq (org-element-type element) 'headline))
652 (numbers
653 ;; Numbering is specific to headlines.
654 (and headlinep
655 (org-export-numbered-headline-p element info)
656 (let ((numbering (org-export-get-headline-number element info)))
657 (if toc (format "%d. " (org-last numbering))
658 (concat (mapconcat #'number-to-string numbering ".")
659 " ")))))
660 (text
661 (org-trim
662 (org-export-data
663 (if (and toc headlinep) (org-export-get-alt-title element info)
664 (org-element-property :title element))
665 info)))
666 (todo
667 (and (plist-get info :with-todo-keywords)
668 (let ((todo (org-element-property :todo-keyword element)))
669 (and todo (concat (org-export-data todo info) " ")))))
670 (tags (and (not notags)
671 (plist-get info :with-tags)
672 (let ((tag-list (org-export-get-tags element info)))
673 (and tag-list
674 (format ":%s:"
675 (mapconcat 'identity tag-list ":"))))))
676 (priority
677 (and (plist-get info :with-priority)
678 (let ((char (org-element-property :priority element)))
679 (and char (format "(#%c) " char)))))
680 (first-part (concat numbers todo priority text)))
681 (concat
682 first-part
683 ;; Align tags, if any.
684 (when tags
685 (format
686 (format " %%%ds"
687 (max (- text-width (1+ (string-width first-part)))
688 (string-width tags)))
689 tags))
690 ;; Maybe underline text, if ELEMENT type is `headline' and an
691 ;; underline character has been defined.
692 (when (and underline headlinep)
693 (let ((under-char
694 (nth (1- (org-export-get-relative-level element info))
695 (cdr (assq (plist-get info :ascii-charset)
696 (plist-get info :ascii-underline))))))
697 (and under-char
698 (concat "\n"
699 (make-string (/ (string-width first-part)
700 (char-width under-char))
701 under-char))))))))
703 (defun org-ascii--has-caption-p (element _info)
704 "Non-nil when ELEMENT has a caption affiliated keyword.
705 INFO is a plist used as a communication channel. This function
706 is meant to be used as a predicate for `org-export-get-ordinal'."
707 (org-element-property :caption element))
709 (defun org-ascii--build-caption (element info)
710 "Return caption string for ELEMENT, if applicable.
712 INFO is a plist used as a communication channel.
714 The caption string contains the sequence number of ELEMENT along
715 with its real caption. Return nil when ELEMENT has no affiliated
716 caption keyword."
717 (let ((caption (org-export-get-caption element)))
718 (when caption
719 ;; Get sequence number of current src-block among every
720 ;; src-block with a caption.
721 (let ((reference
722 (org-export-get-ordinal
723 element info nil 'org-ascii--has-caption-p))
724 (title-fmt (org-ascii--translate
725 (pcase (org-element-type element)
726 (`table "Table %d:")
727 (`src-block "Listing %d:"))
728 info)))
729 (org-ascii--fill-string
730 (concat (format title-fmt reference)
732 (org-export-data caption info))
733 (org-ascii--current-text-width element info) info)))))
735 (defun org-ascii--build-toc (info &optional n keyword local)
736 "Return a table of contents.
738 INFO is a plist used as a communication channel.
740 Optional argument N, when non-nil, is an integer specifying the
741 depth of the table.
743 Optional argument KEYWORD specifies the TOC keyword, if any, from
744 which the table of contents generation has been initiated.
746 When optional argument LOCAL is non-nil, build a table of
747 contents according to the current headline."
748 (concat
749 (unless local
750 (let ((title (org-ascii--translate "Table of Contents" info)))
751 (concat title "\n"
752 (make-string
753 (string-width title)
754 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
755 "\n\n")))
756 (let ((text-width
757 (if keyword (org-ascii--current-text-width keyword info)
758 (- (plist-get info :ascii-text-width)
759 (plist-get info :ascii-global-margin)))))
760 (mapconcat
761 (lambda (headline)
762 (let* ((level (org-export-get-relative-level headline info))
763 (indent (* (1- level) 3)))
764 (concat
765 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
766 (org-ascii--build-title
767 headline info (- text-width indent) nil
768 (or (not (plist-get info :with-tags))
769 (eq (plist-get info :with-tags) 'not-in-toc))
770 'toc))))
771 (org-export-collect-headlines info n (and local keyword)) "\n"))))
773 (defun org-ascii--list-listings (keyword info)
774 "Return a list of listings.
776 KEYWORD is the keyword that initiated the list of listings
777 generation. INFO is a plist used as a communication channel."
778 (let ((title (org-ascii--translate "List of Listings" info)))
779 (concat
780 title "\n"
781 (make-string (string-width title)
782 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
783 "\n\n"
784 (let ((text-width
785 (if keyword (org-ascii--current-text-width keyword info)
786 (- (plist-get info :ascii-text-width)
787 (plist-get info :ascii-global-margin))))
788 ;; Use a counter instead of retrieving ordinal of each
789 ;; src-block.
790 (count 0))
791 (mapconcat
792 (lambda (src-block)
793 ;; Store initial text so its length can be computed. This is
794 ;; used to properly align caption right to it in case of
795 ;; filling (like contents of a description list item).
796 (let* ((initial-text
797 (format (org-ascii--translate "Listing %d:" info)
798 (cl-incf count)))
799 (initial-width (string-width initial-text)))
800 (concat
801 initial-text " "
802 (org-trim
803 (org-ascii--indent-string
804 (org-ascii--fill-string
805 ;; Use short name in priority, if available.
806 (let ((caption (or (org-export-get-caption src-block t)
807 (org-export-get-caption src-block))))
808 (org-export-data caption info))
809 (- text-width initial-width) info)
810 initial-width)))))
811 (org-export-collect-listings info) "\n")))))
813 (defun org-ascii--list-tables (keyword info)
814 "Return a list of tables.
816 KEYWORD is the keyword that initiated the list of tables
817 generation. INFO is a plist used as a communication channel."
818 (let ((title (org-ascii--translate "List of Tables" info)))
819 (concat
820 title "\n"
821 (make-string (string-width title)
822 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
823 "\n\n"
824 (let ((text-width
825 (if keyword (org-ascii--current-text-width keyword info)
826 (- (plist-get info :ascii-text-width)
827 (plist-get info :ascii-global-margin))))
828 ;; Use a counter instead of retrieving ordinal of each
829 ;; src-block.
830 (count 0))
831 (mapconcat
832 (lambda (table)
833 ;; Store initial text so its length can be computed. This is
834 ;; used to properly align caption right to it in case of
835 ;; filling (like contents of a description list item).
836 (let* ((initial-text
837 (format (org-ascii--translate "Table %d:" info)
838 (cl-incf count)))
839 (initial-width (string-width initial-text)))
840 (concat
841 initial-text " "
842 (org-trim
843 (org-ascii--indent-string
844 (org-ascii--fill-string
845 ;; Use short name in priority, if available.
846 (let ((caption (or (org-export-get-caption table t)
847 (org-export-get-caption table))))
848 (org-export-data caption info))
849 (- text-width initial-width) info)
850 initial-width)))))
851 (org-export-collect-tables info) "\n")))))
853 (defun org-ascii--unique-links (element info)
854 "Return a list of unique link references in ELEMENT.
855 ELEMENT is either a headline element or a section element. INFO
856 is a plist used as a communication channel."
857 (let* (seen
858 (unique-link-p
859 ;; Return LINK if it wasn't referenced so far, or nil.
860 ;; Update SEEN links along the way.
861 (lambda (link)
862 (let ((footprint
863 ;; Normalize description in footprints.
864 (cons (org-element-property :raw-link link)
865 (let ((contents (org-element-contents link)))
866 (and contents
867 (replace-regexp-in-string
868 "[ \r\t\n]+" " "
869 (org-trim
870 (org-element-interpret-data contents))))))))
871 ;; Ignore LINK if it hasn't been translated already. It
872 ;; can happen if it is located in an affiliated keyword
873 ;; that was ignored.
874 (when (and (org-string-nw-p
875 (gethash link (plist-get info :exported-data)))
876 (not (member footprint seen)))
877 (push footprint seen) link)))))
878 (org-element-map (if (eq (org-element-type element) 'section)
879 element
880 ;; In a headline, only retrieve links in title
881 ;; and relative section, not in children.
882 (list (org-element-property :title element)
883 (car (org-element-contents element))))
884 'link unique-link-p info nil 'headline t)))
886 (defun org-ascii--describe-datum (datum info)
887 "Describe DATUM object or element.
888 If DATUM is a string, consider it to be a file name, per
889 `org-export-resolve-id-link'. INFO is the communication channel,
890 as a plist."
891 (pcase (org-element-type datum)
892 (`plain-text (format "See file %s" datum)) ;External file
893 (`headline
894 (format (org-ascii--translate "See section %s" info)
895 (if (org-export-numbered-headline-p datum info)
896 (mapconcat #'number-to-string
897 (org-export-get-headline-number datum info)
898 ".")
899 (org-export-data (org-element-property :title datum) info))))
901 (let ((number (org-export-get-ordinal
902 datum info nil #'org-ascii--has-caption-p))
903 ;; If destination is a target, make sure we can name the
904 ;; container it refers to.
905 (enumerable
906 (org-element-lineage datum
907 '(headline paragraph src-block table) t)))
908 (pcase (org-element-type enumerable)
909 (`headline
910 (format (org-ascii--translate "See section %s" info)
911 (if (org-export-numbered-headline-p enumerable info)
912 (mapconcat #'number-to-string number ".")
913 (org-export-data
914 (org-element-property :title enumerable) info))))
915 ((guard (not number))
916 (org-ascii--translate "Unknown reference" info))
917 (`paragraph
918 (format (org-ascii--translate "See figure %s" info) number))
919 (`src-block
920 (format (org-ascii--translate "See listing %s" info) number))
921 (`table
922 (format (org-ascii--translate "See table %s" info) number))
923 (_ (org-ascii--translate "Unknown reference" info)))))))
925 (defun org-ascii--describe-links (links width info)
926 "Return a string describing a list of links.
927 LINKS is a list of link type objects, as returned by
928 `org-ascii--unique-links'. WIDTH is the text width allowed for
929 the output string. INFO is a plist used as a communication
930 channel."
931 (mapconcat
932 (lambda (link)
933 (let* ((type (org-element-property :type link))
934 (description (org-element-contents link))
935 (anchor (org-export-data
936 (or description (org-element-property :raw-link link))
937 info)))
938 (cond
939 ((member type '("coderef" "radio")) nil)
940 ((member type '("custom-id" "fuzzy" "id"))
941 ;; Only links with a description need an entry. Other are
942 ;; already handled in `org-ascii-link'.
943 (when description
944 (let ((dest (if (equal type "fuzzy")
945 (org-export-resolve-fuzzy-link link info)
946 (org-export-resolve-id-link link info))))
947 (concat
948 (org-ascii--fill-string
949 (format "[%s] %s" anchor (org-ascii--describe-datum dest info))
950 width info)
951 "\n\n"))))
952 ;; Do not add a link that cannot be resolved and doesn't have
953 ;; any description: destination is already visible in the
954 ;; paragraph.
955 ((not (org-element-contents link)) nil)
956 ;; Do not add a link already handled by custom export
957 ;; functions.
958 ((org-export-custom-protocol-maybe link anchor 'ascii) nil)
960 (concat
961 (org-ascii--fill-string
962 (format "[%s] %s" anchor (org-element-property :raw-link link))
963 width info)
964 "\n\n")))))
965 links ""))
967 (defun org-ascii--checkbox (item info)
968 "Return checkbox string for ITEM or nil.
969 INFO is a plist used as a communication channel."
970 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
971 (pcase (org-element-property :checkbox item)
972 (`on (if utf8p "☑ " "[X] "))
973 (`off (if utf8p "☐ " "[ ] "))
974 (`trans (if utf8p "☒ " "[-] ")))))
978 ;;; Template
980 (defun org-ascii-template--document-title (info)
981 "Return document title, as a string.
982 INFO is a plist used as a communication channel."
983 (let* ((text-width (plist-get info :ascii-text-width))
984 ;; Links in the title will not be resolved later, so we make
985 ;; sure their path is located right after them.
986 (info (org-combine-plists info '(:ascii-links-to-notes nil)))
987 (with-title (plist-get info :with-title))
988 (title (org-export-data
989 (when with-title (plist-get info :title)) info))
990 (subtitle (org-export-data
991 (when with-title (plist-get info :subtitle)) info))
992 (author (and (plist-get info :with-author)
993 (let ((auth (plist-get info :author)))
994 (and auth (org-export-data auth info)))))
995 (email (and (plist-get info :with-email)
996 (org-export-data (plist-get info :email) info)))
997 (date (and (plist-get info :with-date)
998 (org-export-data (org-export-get-date info) info))))
999 ;; There are two types of title blocks depending on the presence
1000 ;; of a title to display.
1001 (if (string= title "")
1002 ;; Title block without a title. DATE is positioned at the top
1003 ;; right of the document, AUTHOR to the top left and EMAIL
1004 ;; just below.
1005 (cond
1006 ((and (org-string-nw-p date) (org-string-nw-p author))
1007 (concat
1008 author
1009 (make-string (- text-width (string-width date) (string-width author))
1010 ?\s)
1011 date
1012 (when (org-string-nw-p email) (concat "\n" email))
1013 "\n\n\n"))
1014 ((and (org-string-nw-p date) (org-string-nw-p email))
1015 (concat
1016 email
1017 (make-string (- text-width (string-width date) (string-width email))
1018 ?\s)
1019 date "\n\n\n"))
1020 ((org-string-nw-p date)
1021 (concat
1022 (org-ascii--justify-lines date text-width 'right)
1023 "\n\n\n"))
1024 ((and (org-string-nw-p author) (org-string-nw-p email))
1025 (concat author "\n" email "\n\n\n"))
1026 ((org-string-nw-p author) (concat author "\n\n\n"))
1027 ((org-string-nw-p email) (concat email "\n\n\n")))
1028 ;; Title block with a title. Document's TITLE, along with the
1029 ;; AUTHOR and its EMAIL are both overlined and an underlined,
1030 ;; centered. Date is just below, also centered.
1031 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1032 ;; Format TITLE. It may be filled if it is too wide,
1033 ;; that is wider than the two thirds of the total width.
1034 (title-len (min (apply #'max
1035 (mapcar #'length
1036 (org-split-string
1037 (concat title "\n" subtitle) "\n")))
1038 (/ (* 2 text-width) 3)))
1039 (formatted-title (org-ascii--fill-string title title-len info))
1040 (formatted-subtitle (when (org-string-nw-p subtitle)
1041 (org-ascii--fill-string subtitle title-len info)))
1042 (line
1043 (make-string
1044 (min (+ (max title-len
1045 (string-width (or author ""))
1046 (string-width (or email "")))
1048 text-width) (if utf8p ?━ ?_))))
1049 (org-ascii--justify-lines
1050 (concat line "\n"
1051 (unless utf8p "\n")
1052 (upcase formatted-title)
1053 (and formatted-subtitle (concat "\n" formatted-subtitle))
1054 (cond
1055 ((and (org-string-nw-p author) (org-string-nw-p email))
1056 (concat "\n\n" author "\n" email))
1057 ((org-string-nw-p author) (concat "\n\n" author))
1058 ((org-string-nw-p email) (concat "\n\n" email)))
1059 "\n" line
1060 (when (org-string-nw-p date) (concat "\n\n\n" date))
1061 "\n\n\n") text-width 'center)))))
1063 (defun org-ascii-inner-template (contents info)
1064 "Return complete document string after ASCII conversion.
1065 CONTENTS is the transcoded contents string. INFO is a plist
1066 holding export options."
1067 (org-element-normalize-string
1068 (let ((global-margin (plist-get info :ascii-global-margin)))
1069 (org-ascii--indent-string
1070 (concat
1071 ;; 1. Document's body.
1072 contents
1073 ;; 2. Footnote definitions.
1074 (let ((definitions (org-export-collect-footnote-definitions info))
1075 ;; Insert full links right inside the footnote definition
1076 ;; as they have no chance to be inserted later.
1077 (info (org-combine-plists info '(:ascii-links-to-notes nil))))
1078 (when definitions
1079 (concat
1080 "\n\n\n"
1081 (let ((title (org-ascii--translate "Footnotes" info)))
1082 (concat
1083 title "\n"
1084 (make-string
1085 (string-width title)
1086 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
1087 "\n\n"
1088 (let ((text-width (- (plist-get info :ascii-text-width)
1089 global-margin)))
1090 (mapconcat
1091 (lambda (ref)
1092 (let ((id (format "[%s] " (car ref))))
1093 ;; Distinguish between inline definitions and
1094 ;; full-fledged definitions.
1095 (org-trim
1096 (let ((def (nth 2 ref)))
1097 (if (org-element-map def org-element-all-elements
1098 #'identity info 'first-match)
1099 ;; Full-fledged definition: footnote ID is
1100 ;; inserted inside the first parsed
1101 ;; paragraph (FIRST), if any, to be sure
1102 ;; filling will take it into consideration.
1103 (let ((first (car (org-element-contents def))))
1104 (if (not (eq (org-element-type first) 'paragraph))
1105 (concat id "\n" (org-export-data def info))
1106 (push id (nthcdr 2 first))
1107 (org-export-data def info)))
1108 ;; Fill paragraph once footnote ID is inserted
1109 ;; in order to have a correct length for first
1110 ;; line.
1111 (org-ascii--fill-string
1112 (concat id (org-export-data def info))
1113 text-width info))))))
1114 definitions "\n\n"))))))
1115 global-margin))))
1117 (defun org-ascii-template (contents info)
1118 "Return complete document string after ASCII conversion.
1119 CONTENTS is the transcoded contents string. INFO is a plist
1120 holding export options."
1121 (let ((global-margin (plist-get info :ascii-global-margin)))
1122 (concat
1123 ;; Build title block.
1124 (org-ascii--indent-string
1125 (concat (org-ascii-template--document-title info)
1126 ;; 2. Table of contents.
1127 (let ((depth (plist-get info :with-toc)))
1128 (when depth
1129 (concat
1130 (org-ascii--build-toc info (and (wholenump depth) depth))
1131 "\n\n\n"))))
1132 global-margin)
1133 ;; Document's body.
1134 contents
1135 ;; Creator. Justify it to the bottom right.
1136 (and (plist-get info :with-creator)
1137 (org-ascii--indent-string
1138 (let ((text-width
1139 (- (plist-get info :ascii-text-width) global-margin)))
1140 (concat
1141 "\n\n\n"
1142 (org-ascii--fill-string
1143 (plist-get info :creator) text-width info 'right)))
1144 global-margin)))))
1146 (defun org-ascii--translate (s info)
1147 "Translate string S according to specified language and charset.
1148 INFO is a plist used as a communication channel."
1149 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1150 (org-export-translate s charset info)))
1154 ;;; Transcode Functions
1156 ;;;; Bold
1158 (defun org-ascii-bold (_bold contents _info)
1159 "Transcode BOLD from Org to ASCII.
1160 CONTENTS is the text with bold markup. INFO is a plist holding
1161 contextual information."
1162 (format "*%s*" contents))
1165 ;;;; Center Block
1167 (defun org-ascii-center-block (_center-block contents _info)
1168 "Transcode a CENTER-BLOCK element from Org to ASCII.
1169 CONTENTS holds the contents of the block. INFO is a plist
1170 holding contextual information."
1171 ;; Center has already been taken care of at a lower level, so
1172 ;; there's nothing left to do.
1173 contents)
1176 ;;;; Clock
1178 (defun org-ascii-clock (clock _contents info)
1179 "Transcode a CLOCK object from Org to ASCII.
1180 CONTENTS is nil. INFO is a plist holding contextual
1181 information."
1182 (org-ascii--justify-element
1183 (concat org-clock-string " "
1184 (org-timestamp-translate (org-element-property :value clock))
1185 (let ((time (org-element-property :duration clock)))
1186 (and time
1187 (concat " => "
1188 (apply 'format
1189 "%2s:%02s"
1190 (org-split-string time ":"))))))
1191 clock info))
1194 ;;;; Code
1196 (defun org-ascii-code (code _contents info)
1197 "Return a CODE object from Org to ASCII.
1198 CONTENTS is nil. INFO is a plist holding contextual
1199 information."
1200 (format (plist-get info :ascii-verbatim-format)
1201 (org-element-property :value code)))
1204 ;;;; Drawer
1206 (defun org-ascii-drawer (drawer contents info)
1207 "Transcode a DRAWER element from Org to ASCII.
1208 CONTENTS holds the contents of the block. INFO is a plist
1209 holding contextual information."
1210 (let ((name (org-element-property :drawer-name drawer))
1211 (width (org-ascii--current-text-width drawer info)))
1212 (funcall (plist-get info :ascii-format-drawer-function)
1213 name contents width)))
1216 ;;;; Dynamic Block
1218 (defun org-ascii-dynamic-block (_dynamic-block contents _info)
1219 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1220 CONTENTS holds the contents of the block. INFO is a plist
1221 holding contextual information."
1222 contents)
1225 ;;;; Entity
1227 (defun org-ascii-entity (entity _contents info)
1228 "Transcode an ENTITY object from Org to ASCII.
1229 CONTENTS are the definition itself. INFO is a plist holding
1230 contextual information."
1231 (org-element-property
1232 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1233 entity))
1236 ;;;; Example Block
1238 (defun org-ascii-example-block (example-block _contents info)
1239 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1240 CONTENTS is nil. INFO is a plist holding contextual information."
1241 (org-ascii--justify-element
1242 (org-ascii--box-string
1243 (org-export-format-code-default example-block info) info)
1244 example-block info))
1247 ;;;; Export Snippet
1249 (defun org-ascii-export-snippet (export-snippet _contents _info)
1250 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1251 CONTENTS is nil. INFO is a plist holding contextual information."
1252 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1253 (org-element-property :value export-snippet)))
1256 ;;;; Export Block
1258 (defun org-ascii-export-block (export-block _contents info)
1259 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1260 CONTENTS is nil. INFO is a plist holding contextual information."
1261 (when (string= (org-element-property :type export-block) "ASCII")
1262 (org-ascii--justify-element
1263 (org-element-property :value export-block) export-block info)))
1266 ;;;; Fixed Width
1268 (defun org-ascii-fixed-width (fixed-width _contents info)
1269 "Transcode a FIXED-WIDTH element from Org to ASCII.
1270 CONTENTS is nil. INFO is a plist holding contextual information."
1271 (org-ascii--justify-element
1272 (org-ascii--box-string
1273 (org-remove-indentation
1274 (org-element-property :value fixed-width)) info)
1275 fixed-width info))
1278 ;;;; Footnote Definition
1280 ;; Footnote Definitions are ignored. They are compiled at the end of
1281 ;; the document, by `org-ascii-inner-template'.
1284 ;;;; Footnote Reference
1286 (defun org-ascii-footnote-reference (footnote-reference _contents info)
1287 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1288 CONTENTS is nil. INFO is a plist holding contextual information."
1289 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1292 ;;;; Headline
1294 (defun org-ascii-headline (headline contents info)
1295 "Transcode a HEADLINE element from Org to ASCII.
1296 CONTENTS holds the contents of the headline. INFO is a plist
1297 holding contextual information."
1298 ;; Don't export footnote section, which will be handled at the end
1299 ;; of the template.
1300 (unless (org-element-property :footnote-section-p headline)
1301 (let* ((low-level (org-export-low-level-p headline info))
1302 (width (org-ascii--current-text-width headline info))
1303 ;; Export title early so that any link in it can be
1304 ;; exported and seen in `org-ascii--unique-links'.
1305 (title (org-ascii--build-title headline info width (not low-level)))
1306 ;; Blank lines between headline and its contents.
1307 ;; `org-ascii-headline-spacing', when set, overwrites
1308 ;; original buffer's spacing.
1309 (pre-blanks
1310 (make-string (or (car (plist-get info :ascii-headline-spacing))
1311 (org-element-property :pre-blank headline)
1313 ?\n))
1314 (links (and (plist-get info :ascii-links-to-notes)
1315 (org-ascii--describe-links
1316 (org-ascii--unique-links headline info) width info)))
1317 ;; Re-build contents, inserting section links at the right
1318 ;; place. The cost is low since build results are cached.
1319 (body
1320 (if (not (org-string-nw-p links)) contents
1321 (let* ((contents (org-element-contents headline))
1322 (section (let ((first (car contents)))
1323 (and (eq (org-element-type first) 'section)
1324 first))))
1325 (concat (and section
1326 (concat (org-element-normalize-string
1327 (org-export-data section info))
1328 "\n\n"))
1329 links
1330 (mapconcat (lambda (e) (org-export-data e info))
1331 (if section (cdr contents) contents)
1332 ""))))))
1333 ;; Deep subtree: export it as a list item.
1334 (if low-level
1335 (let* ((bullets (cdr (assq (plist-get info :ascii-charset)
1336 (plist-get info :ascii-bullets))))
1337 (bullet
1338 (format "%c "
1339 (nth (mod (1- low-level) (length bullets)) bullets))))
1340 (concat bullet title "\n" pre-blanks
1341 ;; Contents, indented by length of bullet.
1342 (org-ascii--indent-string body (length bullet))))
1343 ;; Else: Standard headline.
1344 (concat title "\n" pre-blanks body)))))
1347 ;;;; Horizontal Rule
1349 (defun org-ascii-horizontal-rule (horizontal-rule _contents info)
1350 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1351 CONTENTS is nil. INFO is a plist holding contextual
1352 information."
1353 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1354 (spec-width
1355 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1356 (org-ascii--justify-lines
1357 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1358 (string-to-number spec-width)
1359 text-width)
1360 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1361 text-width 'center)))
1364 ;;;; Inline Src Block
1366 (defun org-ascii-inline-src-block (inline-src-block _contents info)
1367 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1368 CONTENTS holds the contents of the item. INFO is a plist holding
1369 contextual information."
1370 (format (plist-get info :ascii-verbatim-format)
1371 (org-element-property :value inline-src-block)))
1374 ;;;; Inlinetask
1376 (defun org-ascii-format-inlinetask-default
1377 (_todo _type _priority _name _tags contents width inlinetask info)
1378 "Format an inline task element for ASCII export.
1379 See `org-ascii-format-inlinetask-function' for a description
1380 of the parameters."
1381 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1382 (width (or width (plist-get info :ascii-inlinetask-width))))
1383 (org-ascii--indent-string
1384 (concat
1385 ;; Top line, with an additional blank line if not in UTF-8.
1386 (make-string width (if utf8p ?━ ?_)) "\n"
1387 (unless utf8p (concat (make-string width ? ) "\n"))
1388 ;; Add title. Fill it if wider than inlinetask.
1389 (let ((title (org-ascii--build-title inlinetask info width)))
1390 (if (<= (string-width title) width) title
1391 (org-ascii--fill-string title width info)))
1392 "\n"
1393 ;; If CONTENTS is not empty, insert it along with
1394 ;; a separator.
1395 (when (org-string-nw-p contents)
1396 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1397 ;; Bottom line.
1398 (make-string width (if utf8p ?━ ?_)))
1399 ;; Flush the inlinetask to the right.
1400 (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
1401 (if (not (org-export-get-parent-headline inlinetask)) 0
1402 (plist-get info :ascii-inner-margin))
1403 (org-ascii--current-text-width inlinetask info)))))
1405 (defun org-ascii-inlinetask (inlinetask contents info)
1406 "Transcode an INLINETASK element from Org to ASCII.
1407 CONTENTS holds the contents of the block. INFO is a plist
1408 holding contextual information."
1409 (let ((width (org-ascii--current-text-width inlinetask info)))
1410 (funcall (plist-get info :ascii-format-inlinetask-function)
1411 ;; todo.
1412 (and (plist-get info :with-todo-keywords)
1413 (let ((todo (org-element-property
1414 :todo-keyword inlinetask)))
1415 (and todo (org-export-data todo info))))
1416 ;; todo-type
1417 (org-element-property :todo-type inlinetask)
1418 ;; priority
1419 (and (plist-get info :with-priority)
1420 (org-element-property :priority inlinetask))
1421 ;; title
1422 (org-export-data (org-element-property :title inlinetask) info)
1423 ;; tags
1424 (and (plist-get info :with-tags)
1425 (org-element-property :tags inlinetask))
1426 ;; contents and width
1427 contents width inlinetask info)))
1430 ;;;; Italic
1432 (defun org-ascii-italic (_italic contents _info)
1433 "Transcode italic from Org to ASCII.
1434 CONTENTS is the text with italic markup. INFO is a plist holding
1435 contextual information."
1436 (format "/%s/" contents))
1439 ;;;; Item
1441 (defun org-ascii-item (item contents info)
1442 "Transcode an ITEM element from Org to ASCII.
1443 CONTENTS holds the contents of the item. INFO is a plist holding
1444 contextual information."
1445 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1446 (checkbox (org-ascii--checkbox item info))
1447 (list-type (org-element-property :type (org-export-get-parent item)))
1448 (bullet
1449 ;; First parent of ITEM is always the plain-list. Get
1450 ;; `:type' property from it.
1451 (pcase list-type
1452 (`descriptive
1453 (concat checkbox
1454 (org-export-data (org-element-property :tag item)
1455 info)))
1456 (`ordered
1457 ;; Return correct number for ITEM, paying attention to
1458 ;; counters.
1459 (let* ((struct (org-element-property :structure item))
1460 (bul (org-list-bullet-string
1461 (org-element-property :bullet item)))
1462 (num (number-to-string
1463 (car (last (org-list-get-item-number
1464 (org-element-property :begin item)
1465 struct
1466 (org-list-prevs-alist struct)
1467 (org-list-parents-alist struct)))))))
1468 (replace-regexp-in-string "[0-9]+" num bul)))
1469 (_ (let ((bul (org-list-bullet-string
1470 (org-element-property :bullet item))))
1471 ;; Change bullets into more visible form if UTF-8 is active.
1472 (if (not utf8p) bul
1473 (replace-regexp-in-string
1474 "-" "•"
1475 (replace-regexp-in-string
1476 "+" "⁃"
1477 (replace-regexp-in-string "*" "‣" bul))))))))
1478 (indentation (if (eq list-type 'descriptive) org-ascii-quote-margin
1479 (string-width bullet))))
1480 (concat
1481 bullet
1482 checkbox
1483 ;; Contents: Pay attention to indentation. Note: check-boxes are
1484 ;; already taken care of at the paragraph level so they don't
1485 ;; interfere with indentation.
1486 (let ((contents (org-ascii--indent-string contents indentation)))
1487 ;; Determine if contents should follow the bullet or start
1488 ;; a new line. Do the former when the first contributing
1489 ;; element to contents is a paragraph. In descriptive lists
1490 ;; however, contents always start a new line.
1491 (if (and (not (eq list-type 'descriptive))
1492 (org-string-nw-p contents)
1493 (eq 'paragraph
1494 (org-element-type
1495 (cl-some (lambda (e)
1496 (and (org-string-nw-p (org-export-data e info))
1498 (org-element-contents item)))))
1499 (org-trim contents)
1500 (concat "\n" contents))))))
1503 ;;;; Keyword
1505 (defun org-ascii-keyword (keyword _contents info)
1506 "Transcode a KEYWORD element from Org to ASCII.
1507 CONTENTS is nil. INFO is a plist holding contextual
1508 information."
1509 (let ((key (org-element-property :key keyword))
1510 (value (org-element-property :value keyword)))
1511 (cond
1512 ((string= key "ASCII") (org-ascii--justify-element value keyword info))
1513 ((string= key "TOC")
1514 (org-ascii--justify-element
1515 (let ((case-fold-search t))
1516 (cond
1517 ((string-match-p "\\<headlines\\>" value)
1518 (let ((depth (and (string-match "\\<[0-9]+\\>" value)
1519 (string-to-number (match-string 0 value))))
1520 (localp (string-match-p "\\<local\\>" value)))
1521 (org-ascii--build-toc info depth keyword localp)))
1522 ((string-match-p "\\<tables\\>" value)
1523 (org-ascii--list-tables keyword info))
1524 ((string-match-p "\\<listings\\>" value)
1525 (org-ascii--list-listings keyword info))))
1526 keyword info)))))
1529 ;;;; Latex Environment
1531 (defun org-ascii-latex-environment (latex-environment _contents info)
1532 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1533 CONTENTS is nil. INFO is a plist holding contextual
1534 information."
1535 (when (plist-get info :with-latex)
1536 (org-ascii--justify-element
1537 (org-remove-indentation (org-element-property :value latex-environment))
1538 latex-environment info)))
1541 ;;;; Latex Fragment
1543 (defun org-ascii-latex-fragment (latex-fragment _contents info)
1544 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1545 CONTENTS is nil. INFO is a plist holding contextual
1546 information."
1547 (when (plist-get info :with-latex)
1548 (org-element-property :value latex-fragment)))
1551 ;;;; Line Break
1553 (defun org-ascii-line-break (_line-break _contents _info)
1554 "Transcode a LINE-BREAK object from Org to ASCII.
1555 CONTENTS is nil. INFO is a plist holding contextual
1556 information." hard-newline)
1559 ;;;; Link
1561 (defun org-ascii-link (link desc info)
1562 "Transcode a LINK object from Org to ASCII.
1564 DESC is the description part of the link, or the empty string.
1565 INFO is a plist holding contextual information."
1566 (let ((type (org-element-property :type link)))
1567 (cond
1568 ((org-export-custom-protocol-maybe link desc 'ascii))
1569 ((string= type "coderef")
1570 (let ((ref (org-element-property :path link)))
1571 (format (org-export-get-coderef-format ref desc)
1572 (org-export-resolve-coderef ref info))))
1573 ;; Do not apply a special syntax on radio links. Though, use
1574 ;; transcoded target's contents as output.
1575 ((string= type "radio") desc)
1576 ((member type '("custom-id" "fuzzy" "id"))
1577 (let ((destination (if (string= type "fuzzy")
1578 (org-export-resolve-fuzzy-link link info)
1579 (org-export-resolve-id-link link info))))
1580 (pcase (org-element-type destination)
1581 ((guard desc)
1582 (if (plist-get info :ascii-links-to-notes)
1583 (format "[%s]" desc)
1584 (concat desc
1585 (format " (%s)"
1586 (org-ascii--describe-datum destination info)))))
1587 ;; External file.
1588 (`plain-text destination)
1589 (`headline
1590 (if (org-export-numbered-headline-p destination info)
1591 (mapconcat #'number-to-string
1592 (org-export-get-headline-number destination info)
1593 ".")
1594 (org-export-data (org-element-property :title destination) info)))
1595 ;; Handle enumerable elements and targets within them.
1596 ((and (let number (org-export-get-ordinal
1597 destination info nil #'org-ascii--has-caption-p))
1598 (guard number))
1599 (if (atom number) (number-to-string number)
1600 (mapconcat #'number-to-string number ".")))
1601 ;; Don't know what to do. Signal it.
1602 (_ "???"))))
1604 (let ((raw-link (org-element-property :raw-link link)))
1605 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1606 (concat (format "[%s]" desc)
1607 (and (not (plist-get info :ascii-links-to-notes))
1608 (format " (%s)" raw-link)))))))))
1611 ;;;; Node Properties
1613 (defun org-ascii-node-property (node-property _contents _info)
1614 "Transcode a NODE-PROPERTY element from Org to ASCII.
1615 CONTENTS is nil. INFO is a plist holding contextual
1616 information."
1617 (format "%s:%s"
1618 (org-element-property :key node-property)
1619 (let ((value (org-element-property :value node-property)))
1620 (if value (concat " " value) ""))))
1623 ;;;; Paragraph
1625 (defun org-ascii-paragraph (paragraph contents info)
1626 "Transcode a PARAGRAPH element from Org to ASCII.
1627 CONTENTS is the contents of the paragraph, as a string. INFO is
1628 the plist used as a communication channel."
1629 (org-ascii--justify-element
1630 (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
1631 (if (not (wholenump indented-line-width)) contents
1632 (concat
1633 ;; Do not indent first paragraph in a section.
1634 (unless (and (not (org-export-get-previous-element paragraph info))
1635 (eq (org-element-type (org-export-get-parent paragraph))
1636 'section))
1637 (make-string indented-line-width ?\s))
1638 (replace-regexp-in-string "\\`[ \t]+" "" contents))))
1639 paragraph info))
1642 ;;;; Plain List
1644 (defun org-ascii-plain-list (plain-list contents info)
1645 "Transcode a PLAIN-LIST element from Org to ASCII.
1646 CONTENTS is the contents of the list. INFO is a plist holding
1647 contextual information."
1648 (let ((margin (plist-get info :ascii-list-margin)))
1649 (if (or (< margin 1)
1650 (eq (org-element-type (org-export-get-parent plain-list)) 'item))
1651 contents
1652 (org-ascii--indent-string contents margin))))
1655 ;;;; Plain Text
1657 (defun org-ascii-plain-text (text info)
1658 "Transcode a TEXT string from Org to ASCII.
1659 INFO is a plist used as a communication channel."
1660 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1661 (when (and utf8p (plist-get info :with-smart-quotes))
1662 (setq text (org-export-activate-smart-quotes text :utf-8 info)))
1663 (if (not (plist-get info :with-special-strings)) text
1664 (setq text (replace-regexp-in-string "\\\\-" "" text))
1665 (if (not utf8p) text
1666 ;; Usual replacements in utf-8 with proper option set.
1667 (replace-regexp-in-string
1668 "\\.\\.\\." "…"
1669 (replace-regexp-in-string
1670 "--" "–"
1671 (replace-regexp-in-string "---" "—" text)))))))
1674 ;;;; Planning
1676 (defun org-ascii-planning (planning _contents info)
1677 "Transcode a PLANNING element from Org to ASCII.
1678 CONTENTS is nil. INFO is a plist used as a communication
1679 channel."
1680 (org-ascii--justify-element
1681 (mapconcat
1682 #'identity
1683 (delq nil
1684 (list (let ((closed (org-element-property :closed planning)))
1685 (when closed
1686 (concat org-closed-string " "
1687 (org-timestamp-translate closed))))
1688 (let ((deadline (org-element-property :deadline planning)))
1689 (when deadline
1690 (concat org-deadline-string " "
1691 (org-timestamp-translate deadline))))
1692 (let ((scheduled (org-element-property :scheduled planning)))
1693 (when scheduled
1694 (concat org-scheduled-string " "
1695 (org-timestamp-translate scheduled))))))
1696 " ")
1697 planning info))
1700 ;;;; Property Drawer
1702 (defun org-ascii-property-drawer (property-drawer contents info)
1703 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1704 CONTENTS holds the contents of the drawer. INFO is a plist
1705 holding contextual information."
1706 (and (org-string-nw-p contents)
1707 (org-ascii--justify-element contents property-drawer info)))
1710 ;;;; Quote Block
1712 (defun org-ascii-quote-block (_quote-block contents info)
1713 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1714 CONTENTS holds the contents of the block. INFO is a plist
1715 holding contextual information."
1716 (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
1719 ;;;; Radio Target
1721 (defun org-ascii-radio-target (_radio-target contents _info)
1722 "Transcode a RADIO-TARGET object from Org to ASCII.
1723 CONTENTS is the contents of the target. INFO is a plist holding
1724 contextual information."
1725 contents)
1728 ;;;; Section
1730 (defun org-ascii-section (section contents info)
1731 "Transcode a SECTION element from Org to ASCII.
1732 CONTENTS is the contents of the section. INFO is a plist holding
1733 contextual information."
1734 (let ((links
1735 (and (plist-get info :ascii-links-to-notes)
1736 ;; Take care of links in first section of the document.
1737 (not (org-element-lineage section '(headline)))
1738 (org-ascii--describe-links
1739 (org-ascii--unique-links section info)
1740 (org-ascii--current-text-width section info)
1741 info))))
1742 (org-ascii--indent-string
1743 (if (not (org-string-nw-p links)) contents
1744 (concat (org-element-normalize-string contents) "\n\n" links))
1745 ;; Do not apply inner margin if parent headline is low level.
1746 (let ((headline (org-export-get-parent-headline section)))
1747 (if (or (not headline) (org-export-low-level-p headline info)) 0
1748 (plist-get info :ascii-inner-margin))))))
1751 ;;;; Special Block
1753 (defun org-ascii-special-block (_special-block contents _info)
1754 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1755 CONTENTS holds the contents of the block. INFO is a plist
1756 holding contextual information."
1757 ;; "JUSTIFYLEFT" and "JUSTIFYRIGHT" have already been taken care of
1758 ;; at a lower level. There is no other special block type to
1759 ;; handle.
1760 contents)
1763 ;;;; Src Block
1765 (defun org-ascii-src-block (src-block _contents info)
1766 "Transcode a SRC-BLOCK element from Org to ASCII.
1767 CONTENTS holds the contents of the item. INFO is a plist holding
1768 contextual information."
1769 (let ((caption (org-ascii--build-caption src-block info))
1770 (caption-above-p (plist-get info :ascii-caption-above))
1771 (code (org-export-format-code-default src-block info)))
1772 (if (equal code "") ""
1773 (org-ascii--justify-element
1774 (concat
1775 (and caption caption-above-p (concat caption "\n"))
1776 (org-ascii--box-string code info)
1777 (and caption (not caption-above-p) (concat "\n" caption)))
1778 src-block info))))
1781 ;;;; Statistics Cookie
1783 (defun org-ascii-statistics-cookie (statistics-cookie _contents _info)
1784 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1785 CONTENTS is nil. INFO is a plist holding contextual information."
1786 (org-element-property :value statistics-cookie))
1789 ;;;; Subscript
1791 (defun org-ascii-subscript (subscript contents _info)
1792 "Transcode a SUBSCRIPT object from Org to ASCII.
1793 CONTENTS is the contents of the object. INFO is a plist holding
1794 contextual information."
1795 (if (org-element-property :use-brackets-p subscript)
1796 (format "_{%s}" contents)
1797 (format "_%s" contents)))
1800 ;;;; Superscript
1802 (defun org-ascii-superscript (superscript contents _info)
1803 "Transcode a SUPERSCRIPT object from Org to ASCII.
1804 CONTENTS is the contents of the object. INFO is a plist holding
1805 contextual information."
1806 (if (org-element-property :use-brackets-p superscript)
1807 (format "^{%s}" contents)
1808 (format "^%s" contents)))
1811 ;;;; Strike-through
1813 (defun org-ascii-strike-through (_strike-through contents _info)
1814 "Transcode STRIKE-THROUGH from Org to ASCII.
1815 CONTENTS is text with strike-through markup. INFO is a plist
1816 holding contextual information."
1817 (format "+%s+" contents))
1820 ;;;; Table
1822 (defun org-ascii-table (table contents info)
1823 "Transcode a TABLE element from Org to ASCII.
1824 CONTENTS is the contents of the table. INFO is a plist holding
1825 contextual information."
1826 (let ((caption (org-ascii--build-caption table info))
1827 (caption-above-p (plist-get info :ascii-caption-above)))
1828 (org-ascii--justify-element
1829 (concat
1830 ;; Possibly add a caption string above.
1831 (and caption caption-above-p (concat caption "\n"))
1832 ;; Insert table. Note: "table.el" tables are left unmodified.
1833 (cond ((eq (org-element-property :type table) 'org) contents)
1834 ((and (plist-get info :ascii-table-use-ascii-art)
1835 (eq (plist-get info :ascii-charset) 'utf-8)
1836 (require 'ascii-art-to-unicode nil t))
1837 (with-temp-buffer
1838 (insert (org-remove-indentation
1839 (org-element-property :value table)))
1840 (goto-char (point-min))
1841 (aa2u)
1842 (goto-char (point-max))
1843 (skip-chars-backward " \r\t\n")
1844 (buffer-substring (point-min) (point))))
1845 (t (org-remove-indentation (org-element-property :value table))))
1846 ;; Possible add a caption string below.
1847 (and (not caption-above-p) caption))
1848 table info)))
1851 ;;;; Table Cell
1853 (defun org-ascii--table-cell-width (table-cell info)
1854 "Return width of TABLE-CELL.
1856 INFO is a plist used as a communication channel.
1858 Width of a cell is determined either by a width cookie in the
1859 same column as the cell, or by the maximum cell's length in that
1860 column.
1862 When `org-ascii-table-widen-columns' is non-nil, width cookies
1863 are ignored."
1864 (let* ((row (org-export-get-parent table-cell))
1865 (table (org-export-get-parent row))
1866 (col (let ((cells (org-element-contents row)))
1867 (- (length cells) (length (memq table-cell cells)))))
1868 (cache
1869 (or (plist-get info :ascii-table-cell-width-cache)
1870 (plist-get (setq info
1871 (plist-put info :ascii-table-cell-width-cache
1872 (make-hash-table :test 'equal)))
1873 :ascii-table-cell-width-cache)))
1874 (key (cons table col))
1875 (widenp (plist-get info :ascii-table-widen-columns)))
1876 (or (gethash key cache)
1877 (puthash
1879 (let ((cookie-width (org-export-table-cell-width table-cell info)))
1880 (or (and (not widenp) cookie-width)
1881 (let ((contents-width
1882 (let ((max-width 0))
1883 (org-element-map table 'table-row
1884 (lambda (row)
1885 (setq max-width
1886 (max (string-width
1887 (org-export-data
1888 (org-element-contents
1889 (elt (org-element-contents row) col))
1890 info))
1891 max-width)))
1892 info)
1893 max-width)))
1894 (cond ((not cookie-width) contents-width)
1895 (widenp (max cookie-width contents-width))
1896 (t cookie-width)))))
1897 cache))))
1899 (defun org-ascii-table-cell (table-cell contents info)
1900 "Transcode a TABLE-CELL object from Org to ASCII.
1901 CONTENTS is the cell contents. INFO is a plist used as
1902 a communication channel."
1903 ;; Determine column width. When `org-ascii-table-widen-columns'
1904 ;; is nil and some width cookie has set it, use that value.
1905 ;; Otherwise, compute the maximum width among transcoded data of
1906 ;; each cell in the column.
1907 (let ((width (org-ascii--table-cell-width table-cell info)))
1908 ;; When contents are too large, truncate them.
1909 (unless (or (plist-get info :ascii-table-widen-columns)
1910 (<= (string-width (or contents "")) width))
1911 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1912 ;; Align contents correctly within the cell.
1913 (let* ((indent-tabs-mode nil)
1914 (data
1915 (when contents
1916 (org-ascii--justify-lines
1917 contents width
1918 (org-export-table-cell-alignment table-cell info)))))
1919 (setq contents
1920 (concat data
1921 (make-string (- width (string-width (or data ""))) ?\s))))
1922 ;; Return cell.
1923 (concat (format " %s " contents)
1924 (when (memq 'right (org-export-table-cell-borders table-cell info))
1925 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1928 ;;;; Table Row
1930 (defun org-ascii-table-row (table-row contents info)
1931 "Transcode a TABLE-ROW element from Org to ASCII.
1932 CONTENTS is the row contents. INFO is a plist used as
1933 a communication channel."
1934 (when (eq (org-element-property :type table-row) 'standard)
1935 (let ((build-hline
1936 (function
1937 (lambda (lcorner horiz vert rcorner)
1938 (concat
1939 (apply
1940 'concat
1941 (org-element-map table-row 'table-cell
1942 (lambda (cell)
1943 (let ((width (org-ascii--table-cell-width cell info))
1944 (borders (org-export-table-cell-borders cell info)))
1945 (concat
1946 ;; In order to know if CELL starts the row, do
1947 ;; not compare it with the first cell in the
1948 ;; row as there might be a special column.
1949 ;; Instead, compare it with first exportable
1950 ;; cell, obtained with `org-element-map'.
1951 (when (and (memq 'left borders)
1952 (eq (org-element-map table-row 'table-cell
1953 'identity info t)
1954 cell))
1955 lcorner)
1956 (make-string (+ 2 width) (string-to-char horiz))
1957 (cond
1958 ((not (memq 'right borders)) nil)
1959 ((eq (car (last (org-element-contents table-row))) cell)
1960 rcorner)
1961 (t vert)))))
1962 info)) "\n"))))
1963 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1964 (borders (org-export-table-cell-borders
1965 (org-element-map table-row 'table-cell 'identity info t)
1966 info)))
1967 (concat (cond
1968 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1969 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1970 (funcall build-hline "+" "-" "+" "+")))
1971 ((memq 'above borders)
1972 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1973 (funcall build-hline "+" "-" "+" "+"))))
1974 (when (memq 'left borders) (if utf8p "│" "|"))
1975 contents "\n"
1976 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1977 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1978 (funcall build-hline "+" "-" "+" "+")))))))
1981 ;;;; Timestamp
1983 (defun org-ascii-timestamp (timestamp _contents info)
1984 "Transcode a TIMESTAMP object from Org to ASCII.
1985 CONTENTS is nil. INFO is a plist holding contextual information."
1986 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1989 ;;;; Underline
1991 (defun org-ascii-underline (_underline contents _info)
1992 "Transcode UNDERLINE from Org to ASCII.
1993 CONTENTS is the text with underline markup. INFO is a plist
1994 holding contextual information."
1995 (format "_%s_" contents))
1998 ;;;; Verbatim
2000 (defun org-ascii-verbatim (verbatim _contents info)
2001 "Return a VERBATIM object from Org to ASCII.
2002 CONTENTS is nil. INFO is a plist holding contextual information."
2003 (format (plist-get info :ascii-verbatim-format)
2004 (org-element-property :value verbatim)))
2007 ;;;; Verse Block
2009 (defun org-ascii-verse-block (verse-block contents info)
2010 "Transcode a VERSE-BLOCK element from Org to ASCII.
2011 CONTENTS is verse block contents. INFO is a plist holding
2012 contextual information."
2013 (org-ascii--indent-string
2014 (org-ascii--justify-element contents verse-block info)
2015 (plist-get info :ascii-quote-margin)))
2019 ;;; Filters
2021 (defun org-ascii-filter-headline-blank-lines (headline _backend info)
2022 "Filter controlling number of blank lines after a headline.
2024 HEADLINE is a string representing a transcoded headline. BACKEND
2025 is symbol specifying back-end used for export. INFO is plist
2026 containing the communication channel.
2028 This function only applies to `ascii' back-end. See
2029 `org-ascii-headline-spacing' for information."
2030 (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
2031 (if (not headline-spacing) headline
2032 (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
2033 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
2035 (defun org-ascii-filter-paragraph-spacing (tree _backend info)
2036 "Filter controlling number of blank lines between paragraphs.
2038 TREE is the parse tree. BACKEND is the symbol specifying
2039 back-end used for export. INFO is a plist used as
2040 a communication channel.
2042 See `org-ascii-paragraph-spacing' for information."
2043 (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
2044 (when (wholenump paragraph-spacing)
2045 (org-element-map tree 'paragraph
2046 (lambda (p)
2047 (when (eq (org-element-type (org-export-get-next-element p info))
2048 'paragraph)
2049 (org-element-put-property p :post-blank paragraph-spacing))))))
2050 tree)
2052 (defun org-ascii-filter-comment-spacing (tree _backend info)
2053 "Filter removing blank lines between comments.
2054 TREE is the parse tree. BACKEND is the symbol specifying
2055 back-end used for export. INFO is a plist used as
2056 a communication channel."
2057 (org-element-map tree '(comment comment-block)
2058 (lambda (c)
2059 (when (memq (org-element-type (org-export-get-next-element c info))
2060 '(comment comment-block))
2061 (org-element-put-property c :post-blank 0))))
2062 tree)
2066 ;;; End-user functions
2068 ;;;###autoload
2069 (defun org-ascii-export-as-ascii
2070 (&optional async subtreep visible-only body-only ext-plist)
2071 "Export current buffer to a text buffer.
2073 If narrowing is active in the current buffer, only export its
2074 narrowed part.
2076 If a region is active, export that region.
2078 A non-nil optional argument ASYNC means the process should happen
2079 asynchronously. The resulting buffer should be accessible
2080 through the `org-export-stack' interface.
2082 When optional argument SUBTREEP is non-nil, export the sub-tree
2083 at point, extracting information from the headline properties
2084 first.
2086 When optional argument VISIBLE-ONLY is non-nil, don't export
2087 contents of hidden elements.
2089 When optional argument BODY-ONLY is non-nil, strip title and
2090 table of contents from output.
2092 EXT-PLIST, when provided, is a property list with external
2093 parameters overriding Org default settings, but still inferior to
2094 file-local settings.
2096 Export is done in a buffer named \"*Org ASCII Export*\", which
2097 will be displayed when `org-export-show-temporary-export-buffer'
2098 is non-nil."
2099 (interactive)
2100 (org-export-to-buffer 'ascii "*Org ASCII Export*"
2101 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
2103 ;;;###autoload
2104 (defun org-ascii-export-to-ascii
2105 (&optional async subtreep visible-only body-only ext-plist)
2106 "Export current buffer to a text file.
2108 If narrowing is active in the current buffer, only export its
2109 narrowed part.
2111 If a region is active, export that region.
2113 A non-nil optional argument ASYNC means the process should happen
2114 asynchronously. The resulting file should be accessible through
2115 the `org-export-stack' interface.
2117 When optional argument SUBTREEP is non-nil, export the sub-tree
2118 at point, extracting information from the headline properties
2119 first.
2121 When optional argument VISIBLE-ONLY is non-nil, don't export
2122 contents of hidden elements.
2124 When optional argument BODY-ONLY is non-nil, strip title and
2125 table of contents from output.
2127 EXT-PLIST, when provided, is a property list with external
2128 parameters overriding Org default settings, but still inferior to
2129 file-local settings.
2131 Return output file's name."
2132 (interactive)
2133 (let ((file (org-export-output-file-name ".txt" subtreep)))
2134 (org-export-to-file 'ascii file
2135 async subtreep visible-only body-only ext-plist)))
2137 ;;;###autoload
2138 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
2139 "Publish an Org file to ASCII.
2141 FILENAME is the filename of the Org file to be published. PLIST
2142 is the property list for the given project. PUB-DIR is the
2143 publishing directory.
2145 Return output file name."
2146 (org-publish-org-to
2147 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
2149 ;;;###autoload
2150 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
2151 "Publish an Org file to Latin-1.
2153 FILENAME is the filename of the Org file to be published. PLIST
2154 is the property list for the given project. PUB-DIR is the
2155 publishing directory.
2157 Return output file name."
2158 (org-publish-org-to
2159 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
2161 ;;;###autoload
2162 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
2163 "Publish an org file to UTF-8.
2165 FILENAME is the filename of the Org file to be published. PLIST
2166 is the property list for the given project. PUB-DIR is the
2167 publishing directory.
2169 Return output file name."
2170 (org-publish-org-to
2171 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2174 (provide 'ox-ascii)
2176 ;; Local variables:
2177 ;; generated-autoload-file: "org-loaddefs.el"
2178 ;; coding: utf-8
2179 ;; End:
2181 ;;; ox-ascii.el ends here