Merge branch 'maint'
[org-mode.git] / lisp / ox-ascii.el
blobf8192504c44b0985aae000d8b5b06d516ec3813a
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 <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 (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."
181 :group 'org-export-ascii
182 :version "24.4"
183 :package-version '(Org . "8.0")
184 :type 'integer)
186 (defcustom org-ascii-list-margin 0
187 "Width of margin used for plain lists, in characters.
188 This margin applies to top level list only, not to its
189 sub-lists."
190 :group 'org-export-ascii
191 :version "26.1"
192 :package-version '(Org . "8.3")
193 :type 'integer)
195 (defcustom org-ascii-inlinetask-width 30
196 "Width of inline tasks, in number of characters.
197 This number ignores any margin."
198 :group 'org-export-ascii
199 :version "24.4"
200 :package-version '(Org . "8.0")
201 :type 'integer)
203 (defcustom org-ascii-headline-spacing '(1 . 2)
204 "Number of blank lines inserted around headlines.
206 This variable can be set to a cons cell. In that case, its car
207 represents the number of blank lines present before headline
208 contents whereas its cdr reflects the number of blank lines after
209 contents.
211 A nil value replicates the number of blank lines found in the
212 original Org buffer at the same place."
213 :group 'org-export-ascii
214 :version "24.4"
215 :package-version '(Org . "8.0")
216 :type '(choice
217 (const :tag "Replicate original spacing" nil)
218 (cons :tag "Set a uniform spacing"
219 (integer :tag "Number of blank lines before contents")
220 (integer :tag "Number of blank lines after contents"))))
222 (defcustom org-ascii-indented-line-width 'auto
223 "Additional indentation width for the first line in a paragraph.
224 If the value is an integer, indent the first line of each
225 paragraph by this width, unless it is located at the beginning of
226 a section, in which case indentation is removed from that line.
227 If it is the symbol `auto' preserve indentation from original
228 document."
229 :group 'org-export-ascii
230 :version "24.4"
231 :package-version '(Org . "8.0")
232 :type '(choice
233 (integer :tag "Number of white spaces characters")
234 (const :tag "Preserve original width" auto)))
236 (defcustom org-ascii-paragraph-spacing 'auto
237 "Number of white lines between paragraphs.
238 If the value is an integer, add this number of blank lines
239 between contiguous paragraphs. If is it the symbol `auto', keep
240 the same number of blank lines as in the original document."
241 :group 'org-export-ascii
242 :version "24.4"
243 :package-version '(Org . "8.0")
244 :type '(choice
245 (integer :tag "Number of blank lines")
246 (const :tag "Preserve original spacing" auto)))
248 (defcustom org-ascii-charset 'ascii
249 "The charset allowed to represent various elements and objects.
250 Possible values are:
251 `ascii' Only use plain ASCII characters
252 `latin1' Include Latin-1 characters
253 `utf-8' Use all UTF-8 characters"
254 :group 'org-export-ascii
255 :version "24.4"
256 :package-version '(Org . "8.0")
257 :type '(choice
258 (const :tag "ASCII" ascii)
259 (const :tag "Latin-1" latin1)
260 (const :tag "UTF-8" utf-8)))
262 (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
263 (latin1 ?= ?~ ?-)
264 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
265 "Characters for underlining headings in ASCII export.
267 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
268 and whose value is a list of characters.
270 For each supported charset, this variable associates a sequence
271 of underline characters. In a sequence, the characters will be
272 used in order for headlines level 1, 2, ... If no character is
273 available for a given level, the headline won't be underlined."
274 :group 'org-export-ascii
275 :version "24.4"
276 :package-version '(Org . "8.0")
277 :type '(list
278 (cons :tag "Underline characters sequence"
279 (const :tag "ASCII charset" ascii)
280 (repeat character))
281 (cons :tag "Underline characters sequence"
282 (const :tag "Latin-1 charset" latin1)
283 (repeat character))
284 (cons :tag "Underline characters sequence"
285 (const :tag "UTF-8 charset" utf-8)
286 (repeat character))))
288 (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
289 (latin1 ?§ ?¶)
290 (utf-8 ?◊))
291 "Bullet characters for headlines converted to lists in ASCII export.
293 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
294 and whose value is a list of characters.
296 The first character is used for the first level considered as low
297 level, and so on. If there are more levels than characters given
298 here, the list will be repeated.
300 Note that this variable doesn't affect plain lists
301 representation."
302 :group 'org-export-ascii
303 :version "24.4"
304 :package-version '(Org . "8.0")
305 :type '(list
306 (cons :tag "Bullet characters for low level headlines"
307 (const :tag "ASCII charset" ascii)
308 (repeat character))
309 (cons :tag "Bullet characters for low level headlines"
310 (const :tag "Latin-1 charset" latin1)
311 (repeat character))
312 (cons :tag "Bullet characters for low level headlines"
313 (const :tag "UTF-8 charset" utf-8)
314 (repeat character))))
316 (defcustom org-ascii-links-to-notes t
317 "Non-nil means convert links to notes before the next headline.
318 When nil, the link will be exported in place. If the line
319 becomes long in this way, it will be wrapped."
320 :group 'org-export-ascii
321 :version "24.4"
322 :package-version '(Org . "8.0")
323 :type 'boolean)
325 (defcustom org-ascii-table-keep-all-vertical-lines nil
326 "Non-nil means keep all vertical lines in ASCII tables.
327 When nil, vertical lines will be removed except for those needed
328 for column grouping."
329 :group 'org-export-ascii
330 :version "24.4"
331 :package-version '(Org . "8.0")
332 :type 'boolean)
334 (defcustom org-ascii-table-widen-columns t
335 "Non-nil means widen narrowed columns for export.
336 When nil, narrowed columns will look in ASCII export just like in
337 Org mode, i.e. with \"=>\" as ellipsis."
338 :group 'org-export-ascii
339 :version "24.4"
340 :package-version '(Org . "8.0")
341 :type 'boolean)
343 (defcustom org-ascii-table-use-ascii-art nil
344 "Non-nil means \"table.el\" tables are turned into ASCII art.
345 It only makes sense when export charset is `utf-8'. It is nil by
346 default since it requires \"ascii-art-to-unicode.el\" package,
347 available through, e.g., GNU ELPA."
348 :group 'org-export-ascii
349 :version "24.4"
350 :package-version '(Org . "8.0")
351 :type 'boolean)
353 (defcustom org-ascii-caption-above nil
354 "When non-nil, place caption string before the element.
355 Otherwise, place it right after it."
356 :group 'org-export-ascii
357 :version "24.4"
358 :package-version '(Org . "8.0")
359 :type 'boolean)
361 (defcustom org-ascii-verbatim-format "`%s'"
362 "Format string used for verbatim text and inline code."
363 :group 'org-export-ascii
364 :version "24.4"
365 :package-version '(Org . "8.0")
366 :type 'string)
368 (defcustom org-ascii-format-drawer-function
369 (lambda (_name contents _width) contents)
370 "Function called to format a drawer in ASCII.
372 The function must accept three parameters:
373 NAME the drawer name, like \"LOGBOOK\"
374 CONTENTS the contents of the drawer.
375 WIDTH the text width within the drawer.
377 The function should return either the string to be exported or
378 nil to ignore the drawer.
380 The default value simply returns the value of CONTENTS."
381 :group 'org-export-ascii
382 :version "24.4"
383 :package-version '(Org . "8.0")
384 :type 'function)
386 (defcustom org-ascii-format-inlinetask-function
387 'org-ascii-format-inlinetask-default
388 "Function called to format an inlinetask in ASCII.
390 The function must accept nine parameters:
391 TODO the todo keyword, as a string
392 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
393 PRIORITY the inlinetask priority, as a string
394 NAME the inlinetask name, as a string.
395 TAGS the inlinetask tags, as a list of strings.
396 CONTENTS the contents of the inlinetask, as a string.
397 WIDTH the width of the inlinetask, as a number.
398 INLINETASK the inlinetask itself.
399 INFO the info channel.
401 The function should return either the string to be exported or
402 nil to ignore the inline task."
403 :group 'org-export-ascii
404 :version "24.4"
405 :package-version '(Org . "8.3")
406 :type 'function)
410 ;;; Internal Functions
412 ;; Internal functions fall into three categories.
414 ;; The first one is about text formatting. The core functions are
415 ;; `org-ascii--current-text-width' and
416 ;; `org-ascii--current-justification', which determine, respectively,
417 ;; the current text width allowed to a given element and its expected
418 ;; justification. Once this information is known,
419 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
420 ;; `org-ascii--justify-element' `org-ascii--box-string' and
421 ;; `org-ascii--indent-string' can operate on a given output string.
422 ;; In particular, justification happens at the regular (i.e.,
423 ;; non-greater) element level, which means that when the exporting
424 ;; process reaches a container (e.g., a center block) content are
425 ;; already justified.
427 ;; The second category contains functions handling elements listings,
428 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
429 ;; returns a complete table of contents, `org-ascii--list-listings'
430 ;; returns a list of referenceable src-block elements, and
431 ;; `org-ascii--list-tables' does the same for table elements.
433 ;; The third category includes general helper functions.
434 ;; `org-ascii--build-title' creates the title for a given headline
435 ;; or inlinetask element. `org-ascii--build-caption' returns the
436 ;; caption string associated to a table or a src-block.
437 ;; `org-ascii--describe-links' creates notes about links for
438 ;; insertion at the end of a section. It uses
439 ;; `org-ascii--unique-links' to get the list of links to describe.
440 ;; Eventually, `org-ascii--translate' translates a string according
441 ;; to language and charset specification.
444 (defun org-ascii--fill-string (s text-width info &optional justify)
445 "Fill a string with specified text-width and return it.
447 S is the string being filled. TEXT-WIDTH is an integer
448 specifying maximum length of a line. INFO is the plist used as
449 a communication channel.
451 Optional argument JUSTIFY can specify any type of justification
452 among `left', `center', `right' or `full'. A nil value is
453 equivalent to `left'. For a justification that doesn't also fill
454 string, see `org-ascii--justify-lines' and
455 `org-ascii--justify-block'.
457 Return nil if S isn't a string."
458 (when (stringp s)
459 (let ((double-space-p sentence-end-double-space))
460 (with-temp-buffer
461 (let ((fill-column text-width)
462 (use-hard-newlines t)
463 (sentence-end-double-space double-space-p))
464 (insert (if (plist-get info :preserve-breaks)
465 (replace-regexp-in-string "\n" hard-newline s)
467 (fill-region (point-min) (point-max) justify))
468 (buffer-string)))))
470 (defun org-ascii--justify-lines (s text-width how)
471 "Justify all lines in string S.
472 TEXT-WIDTH is an integer specifying maximum length of a line.
473 HOW determines the type of justification: it can be `left',
474 `right', `full' or `center'."
475 (with-temp-buffer
476 (insert s)
477 (goto-char (point-min))
478 (let ((fill-column text-width)
479 ;; Disable `adaptive-fill-mode' so it doesn't prevent
480 ;; filling lines matching `adaptive-fill-regexp'.
481 (adaptive-fill-mode nil))
482 (while (< (point) (point-max))
483 (justify-current-line how)
484 (forward-line)))
485 (buffer-string)))
487 (defun org-ascii--justify-element (contents element info)
488 "Justify CONTENTS of ELEMENT.
489 INFO is a plist used as a communication channel. Justification
490 is done according to the type of element. More accurately,
491 paragraphs are filled and other elements are justified as blocks,
492 that is according to the widest non blank line in CONTENTS."
493 (if (not (org-string-nw-p contents)) contents
494 (let ((text-width (org-ascii--current-text-width element info))
495 (how (org-ascii--current-justification element)))
496 (cond
497 ((eq (org-element-type element) 'paragraph)
498 ;; Paragraphs are treated specially as they need to be filled.
499 (org-ascii--fill-string contents text-width info how))
500 ((eq how 'left) contents)
501 (t (with-temp-buffer
502 (insert contents)
503 (goto-char (point-min))
504 (catch 'exit
505 (let ((max-width 0))
506 ;; Compute maximum width. Bail out if it is greater
507 ;; than page width, since no justification is
508 ;; possible.
509 (save-excursion
510 (while (not (eobp))
511 (unless (looking-at-p "[ \t]*$")
512 (end-of-line)
513 (let ((column (current-column)))
514 (cond
515 ((>= column text-width) (throw 'exit contents))
516 ((> column max-width) (setq max-width column)))))
517 (forward-line)))
518 ;; Justify every line according to TEXT-WIDTH and
519 ;; MAX-WIDTH.
520 (let ((offset (/ (- text-width max-width)
521 (if (eq how 'right) 1 2))))
522 (if (zerop offset) (throw 'exit contents)
523 (while (not (eobp))
524 (unless (looking-at-p "[ \t]*$")
525 (indent-to-column offset))
526 (forward-line)))))
527 (buffer-string))))))))
529 (defun org-ascii--indent-string (s width)
530 "Indent string S by WIDTH white spaces.
531 Empty lines are not indented."
532 (when (stringp s)
533 (replace-regexp-in-string
534 "\\(^\\)[ \t]*\\S-" (make-string width ?\s) s nil nil 1)))
536 (defun org-ascii--box-string (s info)
537 "Return string S with a partial box to its left.
538 INFO is a plist used as a communication channel."
539 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
540 (format (if utf8p "┌────\n%s\n└────" ",----\n%s\n`----")
541 (replace-regexp-in-string
542 "^" (if utf8p "│ " "| ")
543 ;; Remove last newline character.
544 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
546 (defun org-ascii--current-text-width (element info)
547 "Return maximum text width for ELEMENT's contents.
548 INFO is a plist used as a communication channel."
549 (pcase (org-element-type element)
550 ;; Elements with an absolute width: `headline' and `inlinetask'.
551 (`inlinetask (plist-get info :ascii-inlinetask-width))
552 (`headline
553 (- (plist-get info :ascii-text-width)
554 (let ((low-level-rank (org-export-low-level-p element info)))
555 (if low-level-rank (* low-level-rank 2)
556 (plist-get info :ascii-global-margin)))))
557 ;; Elements with a relative width: store maximum text width in
558 ;; TOTAL-WIDTH.
560 (let* ((genealogy (org-element-lineage element nil t))
561 ;; Total width is determined by the presence, or not, of an
562 ;; inline task among ELEMENT parents.
563 (total-width
564 (if (cl-some (lambda (parent)
565 (eq (org-element-type parent) 'inlinetask))
566 genealogy)
567 (plist-get info :ascii-inlinetask-width)
568 ;; No inlinetask: Remove global margin from text width.
569 (- (plist-get info :ascii-text-width)
570 (plist-get info :ascii-global-margin)
571 (let ((parent (org-export-get-parent-headline element)))
572 ;; Inner margin doesn't apply to text before first
573 ;; headline.
574 (if (not parent) 0
575 (let ((low-level-rank
576 (org-export-low-level-p parent info)))
577 ;; Inner margin doesn't apply to contents of
578 ;; low level headlines, since they've got their
579 ;; own indentation mechanism.
580 (if low-level-rank (* low-level-rank 2)
581 (plist-get info :ascii-inner-margin)))))))))
582 (- total-width
583 ;; Each `quote-block' and `verse-block' above narrows text
584 ;; width by twice the standard margin size.
585 (+ (* (cl-count-if (lambda (parent)
586 (memq (org-element-type parent)
587 '(quote-block verse-block)))
588 genealogy)
590 (plist-get info :ascii-quote-margin))
591 ;; Apply list margin once per "top-level" plain-list
592 ;; containing current line
593 (* (cl-count-if
594 (lambda (e)
595 (and (eq (org-element-type e) 'plain-list)
596 (not (eq (org-element-type (org-export-get-parent e))
597 'item))))
598 genealogy)
599 (plist-get info :ascii-list-margin))
600 ;; Text width within a plain-list is restricted by
601 ;; indentation of current item. If that's the case,
602 ;; compute it with the help of `:structure' property from
603 ;; parent item, if any.
604 (let ((item
605 (if (eq (org-element-type element) 'item) element
606 (cl-find-if (lambda (parent)
607 (eq (org-element-type parent) 'item))
608 genealogy))))
609 (if (not item) 0
610 ;; Compute indentation offset of the current item,
611 ;; that is the sum of the difference between its
612 ;; indentation and the indentation of the top item in
613 ;; the list and current item bullet's length. Also
614 ;; remove checkbox length, and tag length (for
615 ;; description lists) or bullet length.
616 (let ((struct (org-element-property :structure item))
617 (beg-item (org-element-property :begin item)))
618 (+ (- (org-list-get-ind beg-item struct)
619 (org-list-get-ind
620 (org-list-get-top-point struct) struct))
621 (string-width (or (org-ascii--checkbox item info)
622 ""))
623 (string-width
624 (let ((tag (org-element-property :tag item)))
625 (if tag (org-export-data tag info)
626 (org-element-property :bullet item))))))))))))))
628 (defun org-ascii--current-justification (element)
629 "Return expected justification for ELEMENT's contents.
630 Return value is a symbol among `left', `center', `right' and
631 `full'."
632 (let (justification)
633 (while (and (not justification)
634 (setq element (org-element-property :parent element)))
635 (pcase (org-element-type element)
636 (`center-block (setq justification 'center))
637 (`special-block
638 (let ((name (org-element-property :type element)))
639 (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
640 ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
641 (or justification 'left)))
643 (defun org-ascii--build-title
644 (element info text-width &optional underline notags toc)
645 "Format ELEMENT title and return it.
647 ELEMENT is either an `headline' or `inlinetask' element. INFO is
648 a plist used as a communication channel. TEXT-WIDTH is an
649 integer representing the maximum length of a line.
651 When optional argument UNDERLINE is non-nil, underline title,
652 without the tags, according to `org-ascii-underline'
653 specifications.
655 If optional argument NOTAGS is non-nil, no tags will be added to
656 the title.
658 When optional argument TOC is non-nil, use optional title if
659 possible. It doesn't apply to `inlinetask' elements."
660 (let* ((headlinep (eq (org-element-type element) 'headline))
661 (numbers
662 ;; Numbering is specific to headlines.
663 (and headlinep (org-export-numbered-headline-p element info)
664 ;; All tests passed: build numbering string.
665 (concat
666 (mapconcat
667 'number-to-string
668 (org-export-get-headline-number element info) ".")
669 " ")))
670 (text
671 (org-trim
672 (org-export-data
673 (if (and toc headlinep) (org-export-get-alt-title element info)
674 (org-element-property :title element))
675 info)))
676 (todo
677 (and (plist-get info :with-todo-keywords)
678 (let ((todo (org-element-property :todo-keyword element)))
679 (and todo (concat (org-export-data todo info) " ")))))
680 (tags (and (not notags)
681 (plist-get info :with-tags)
682 (let ((tag-list (org-export-get-tags element info)))
683 (and tag-list
684 (format ":%s:"
685 (mapconcat 'identity tag-list ":"))))))
686 (priority
687 (and (plist-get info :with-priority)
688 (let ((char (org-element-property :priority element)))
689 (and char (format "(#%c) " char)))))
690 (first-part (concat numbers todo priority text)))
691 (concat
692 first-part
693 ;; Align tags, if any.
694 (when tags
695 (format
696 (format " %%%ds"
697 (max (- text-width (1+ (string-width first-part)))
698 (string-width tags)))
699 tags))
700 ;; Maybe underline text, if ELEMENT type is `headline' and an
701 ;; underline character has been defined.
702 (when (and underline headlinep)
703 (let ((under-char
704 (nth (1- (org-export-get-relative-level element info))
705 (cdr (assq (plist-get info :ascii-charset)
706 (plist-get info :ascii-underline))))))
707 (and under-char
708 (concat "\n"
709 (make-string (/ (string-width first-part)
710 (char-width under-char))
711 under-char))))))))
713 (defun org-ascii--has-caption-p (element _info)
714 "Non-nil when ELEMENT has a caption affiliated keyword.
715 INFO is a plist used as a communication channel. This function
716 is meant to be used as a predicate for `org-export-get-ordinal'."
717 (org-element-property :caption element))
719 (defun org-ascii--build-caption (element info)
720 "Return caption string for ELEMENT, if applicable.
722 INFO is a plist used as a communication channel.
724 The caption string contains the sequence number of ELEMENT along
725 with its real caption. Return nil when ELEMENT has no affiliated
726 caption keyword."
727 (let ((caption (org-export-get-caption element)))
728 (when caption
729 ;; Get sequence number of current src-block among every
730 ;; src-block with a caption.
731 (let ((reference
732 (org-export-get-ordinal
733 element info nil 'org-ascii--has-caption-p))
734 (title-fmt (org-ascii--translate
735 (pcase (org-element-type element)
736 (`table "Table %d:")
737 (`src-block "Listing %d:"))
738 info)))
739 (org-ascii--fill-string
740 (concat (format title-fmt reference)
742 (org-export-data caption info))
743 (org-ascii--current-text-width element info) info)))))
745 (defun org-ascii--build-toc (info &optional n keyword local)
746 "Return a table of contents.
748 INFO is a plist used as a communication channel.
750 Optional argument N, when non-nil, is an integer specifying the
751 depth of the table.
753 Optional argument KEYWORD specifies the TOC keyword, if any, from
754 which the table of contents generation has been initiated.
756 When optional argument LOCAL is non-nil, build a table of
757 contents according to the current headline."
758 (concat
759 (unless local
760 (let ((title (org-ascii--translate "Table of Contents" info)))
761 (concat title "\n"
762 (make-string
763 (string-width title)
764 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
765 "\n\n")))
766 (let ((text-width
767 (if keyword (org-ascii--current-text-width keyword info)
768 (- (plist-get info :ascii-text-width)
769 (plist-get info :ascii-global-margin)))))
770 (mapconcat
771 (lambda (headline)
772 (let* ((level (org-export-get-relative-level headline info))
773 (indent (* (1- level) 3)))
774 (concat
775 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
776 (org-ascii--build-title
777 headline info (- text-width indent) nil
778 (or (not (plist-get info :with-tags))
779 (eq (plist-get info :with-tags) 'not-in-toc))
780 'toc))))
781 (org-export-collect-headlines info n (and local keyword)) "\n"))))
783 (defun org-ascii--list-listings (keyword info)
784 "Return a list of listings.
786 KEYWORD is the keyword that initiated the list of listings
787 generation. INFO is a plist used as a communication channel."
788 (let ((title (org-ascii--translate "List of Listings" info)))
789 (concat
790 title "\n"
791 (make-string (string-width title)
792 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
793 "\n\n"
794 (let ((text-width
795 (if keyword (org-ascii--current-text-width keyword info)
796 (- (plist-get info :ascii-text-width)
797 (plist-get info :ascii-global-margin))))
798 ;; Use a counter instead of retrieving ordinal of each
799 ;; src-block.
800 (count 0))
801 (mapconcat
802 (lambda (src-block)
803 ;; Store initial text so its length can be computed. This is
804 ;; used to properly align caption right to it in case of
805 ;; filling (like contents of a description list item).
806 (let* ((initial-text
807 (format (org-ascii--translate "Listing %d:" info)
808 (cl-incf count)))
809 (initial-width (string-width initial-text)))
810 (concat
811 initial-text " "
812 (org-trim
813 (org-ascii--indent-string
814 (org-ascii--fill-string
815 ;; Use short name in priority, if available.
816 (let ((caption (or (org-export-get-caption src-block t)
817 (org-export-get-caption src-block))))
818 (org-export-data caption info))
819 (- text-width initial-width) info)
820 initial-width)))))
821 (org-export-collect-listings info) "\n")))))
823 (defun org-ascii--list-tables (keyword info)
824 "Return a list of tables.
826 KEYWORD is the keyword that initiated the list of tables
827 generation. INFO is a plist used as a communication channel."
828 (let ((title (org-ascii--translate "List of Tables" info)))
829 (concat
830 title "\n"
831 (make-string (string-width title)
832 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
833 "\n\n"
834 (let ((text-width
835 (if keyword (org-ascii--current-text-width keyword info)
836 (- (plist-get info :ascii-text-width)
837 (plist-get info :ascii-global-margin))))
838 ;; Use a counter instead of retrieving ordinal of each
839 ;; src-block.
840 (count 0))
841 (mapconcat
842 (lambda (table)
843 ;; Store initial text so its length can be computed. This is
844 ;; used to properly align caption right to it in case of
845 ;; filling (like contents of a description list item).
846 (let* ((initial-text
847 (format (org-ascii--translate "Table %d:" info)
848 (cl-incf count)))
849 (initial-width (string-width initial-text)))
850 (concat
851 initial-text " "
852 (org-trim
853 (org-ascii--indent-string
854 (org-ascii--fill-string
855 ;; Use short name in priority, if available.
856 (let ((caption (or (org-export-get-caption table t)
857 (org-export-get-caption table))))
858 (org-export-data caption info))
859 (- text-width initial-width) info)
860 initial-width)))))
861 (org-export-collect-tables info) "\n")))))
863 (defun org-ascii--unique-links (element info)
864 "Return a list of unique link references in ELEMENT.
865 ELEMENT is either a headline element or a section element. INFO
866 is a plist used as a communication channel."
867 (let* (seen
868 (unique-link-p
869 ;; Return LINK if it wasn't referenced so far, or nil.
870 ;; Update SEEN links along the way.
871 (lambda (link)
872 (let ((footprint
873 ;; Normalize description in footprints.
874 (cons (org-element-property :raw-link link)
875 (let ((contents (org-element-contents link)))
876 (and contents
877 (replace-regexp-in-string
878 "[ \r\t\n]+" " "
879 (org-trim
880 (org-element-interpret-data contents))))))))
881 ;; Ignore LINK if it hasn't been translated already. It
882 ;; can happen if it is located in an affiliated keyword
883 ;; that was ignored.
884 (when (and (org-string-nw-p
885 (gethash link (plist-get info :exported-data)))
886 (not (member footprint seen)))
887 (push footprint seen) link)))))
888 (org-element-map (if (eq (org-element-type element) 'section)
889 element
890 ;; In a headline, only retrieve links in title
891 ;; and relative section, not in children.
892 (list (org-element-property :title element)
893 (car (org-element-contents element))))
894 'link unique-link-p info nil 'headline t)))
896 (defun org-ascii--describe-datum (datum info)
897 "Describe DATUM object or element.
898 If DATUM is a string, consider it to be a file name, per
899 `org-export-resolve-id-link'. INFO is the communication channel,
900 as a plist."
901 (pcase (org-element-type datum)
902 (`plain-text (format "See file %s" datum)) ;External file
903 (`headline
904 (format (org-ascii--translate "See section %s" info)
905 (if (org-export-numbered-headline-p datum info)
906 (mapconcat #'number-to-string
907 (org-export-get-headline-number datum info)
908 ".")
909 (org-export-data (org-element-property :title datum) info))))
911 (let ((number (org-export-get-ordinal
912 datum info nil #'org-ascii--has-caption-p))
913 ;; If destination is a target, make sure we can name the
914 ;; container it refers to.
915 (enumerable
916 (org-element-lineage datum
917 '(headline paragraph src-block table) t)))
918 (pcase (org-element-type enumerable)
919 (`headline
920 (format (org-ascii--translate "See section %s" info)
921 (if (org-export-numbered-headline-p enumerable info)
922 (mapconcat #'number-to-string number ".")
923 (org-export-data
924 (org-element-property :title enumerable) info))))
925 ((guard (not number))
926 (org-ascii--translate "Unknown reference" info))
927 (`paragraph
928 (format (org-ascii--translate "See figure %s" info) number))
929 (`src-block
930 (format (org-ascii--translate "See listing %s" info) number))
931 (`table
932 (format (org-ascii--translate "See table %s" info) number))
933 (_ (org-ascii--translate "Unknown reference" info)))))))
935 (defun org-ascii--describe-links (links width info)
936 "Return a string describing a list of links.
937 LINKS is a list of link type objects, as returned by
938 `org-ascii--unique-links'. WIDTH is the text width allowed for
939 the output string. INFO is a plist used as a communication
940 channel."
941 (mapconcat
942 (lambda (link)
943 (let* ((type (org-element-property :type link))
944 (description (org-element-contents link))
945 (anchor (org-export-data
946 (or description (org-element-property :raw-link link))
947 info)))
948 (cond
949 ((member type '("coderef" "radio")) nil)
950 ((member type '("custom-id" "fuzzy" "id"))
951 ;; Only links with a description need an entry. Other are
952 ;; already handled in `org-ascii-link'.
953 (when description
954 (let ((dest (if (equal type "fuzzy")
955 (org-export-resolve-fuzzy-link link info)
956 (org-export-resolve-id-link link info))))
957 (concat
958 (org-ascii--fill-string
959 (format "[%s] %s" anchor (org-ascii--describe-datum dest info))
960 width info)
961 "\n\n"))))
962 ;; Do not add a link that cannot be resolved and doesn't have
963 ;; any description: destination is already visible in the
964 ;; paragraph.
965 ((not (org-element-contents link)) nil)
966 ;; Do not add a link already handled by custom export
967 ;; functions.
968 ((org-export-custom-protocol-maybe link anchor 'ascii) nil)
970 (concat
971 (org-ascii--fill-string
972 (format "[%s] %s" anchor (org-element-property :raw-link link))
973 width info)
974 "\n\n")))))
975 links ""))
977 (defun org-ascii--checkbox (item info)
978 "Return checkbox string for ITEM or nil.
979 INFO is a plist used as a communication channel."
980 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
981 (pcase (org-element-property :checkbox item)
982 (`on (if utf8p "☑ " "[X] "))
983 (`off (if utf8p "☐ " "[ ] "))
984 (`trans (if utf8p "☒ " "[-] ")))))
988 ;;; Template
990 (defun org-ascii-template--document-title (info)
991 "Return document title, as a string.
992 INFO is a plist used as a communication channel."
993 (let* ((text-width (plist-get info :ascii-text-width))
994 ;; Links in the title will not be resolved later, so we make
995 ;; sure their path is located right after them.
996 (info (org-combine-plists info '(:ascii-links-to-notes nil)))
997 (with-title (plist-get info :with-title))
998 (title (org-export-data
999 (when with-title (plist-get info :title)) info))
1000 (subtitle (org-export-data
1001 (when with-title (plist-get info :subtitle)) info))
1002 (author (and (plist-get info :with-author)
1003 (let ((auth (plist-get info :author)))
1004 (and auth (org-export-data auth info)))))
1005 (email (and (plist-get info :with-email)
1006 (org-export-data (plist-get info :email) info)))
1007 (date (and (plist-get info :with-date)
1008 (org-export-data (org-export-get-date info) info))))
1009 ;; There are two types of title blocks depending on the presence
1010 ;; of a title to display.
1011 (if (string= title "")
1012 ;; Title block without a title. DATE is positioned at the top
1013 ;; right of the document, AUTHOR to the top left and EMAIL
1014 ;; just below.
1015 (cond
1016 ((and (org-string-nw-p date) (org-string-nw-p author))
1017 (concat
1018 author
1019 (make-string (- text-width (string-width date) (string-width author))
1020 ?\s)
1021 date
1022 (when (org-string-nw-p email) (concat "\n" email))
1023 "\n\n\n"))
1024 ((and (org-string-nw-p date) (org-string-nw-p email))
1025 (concat
1026 email
1027 (make-string (- text-width (string-width date) (string-width email))
1028 ?\s)
1029 date "\n\n\n"))
1030 ((org-string-nw-p date)
1031 (concat
1032 (org-ascii--justify-lines date text-width 'right)
1033 "\n\n\n"))
1034 ((and (org-string-nw-p author) (org-string-nw-p email))
1035 (concat author "\n" email "\n\n\n"))
1036 ((org-string-nw-p author) (concat author "\n\n\n"))
1037 ((org-string-nw-p email) (concat email "\n\n\n")))
1038 ;; Title block with a title. Document's TITLE, along with the
1039 ;; AUTHOR and its EMAIL are both overlined and an underlined,
1040 ;; centered. Date is just below, also centered.
1041 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1042 ;; Format TITLE. It may be filled if it is too wide,
1043 ;; that is wider than the two thirds of the total width.
1044 (title-len (min (apply #'max
1045 (mapcar #'length
1046 (org-split-string
1047 (concat title "\n" subtitle) "\n")))
1048 (/ (* 2 text-width) 3)))
1049 (formatted-title (org-ascii--fill-string title title-len info))
1050 (formatted-subtitle (when (org-string-nw-p subtitle)
1051 (org-ascii--fill-string subtitle title-len info)))
1052 (line
1053 (make-string
1054 (min (+ (max title-len
1055 (string-width (or author ""))
1056 (string-width (or email "")))
1058 text-width) (if utf8p ?━ ?_))))
1059 (org-ascii--justify-lines
1060 (concat line "\n"
1061 (unless utf8p "\n")
1062 (upcase formatted-title)
1063 (and formatted-subtitle (concat "\n" formatted-subtitle))
1064 (cond
1065 ((and (org-string-nw-p author) (org-string-nw-p email))
1066 (concat "\n\n" author "\n" email))
1067 ((org-string-nw-p author) (concat "\n\n" author))
1068 ((org-string-nw-p email) (concat "\n\n" email)))
1069 "\n" line
1070 (when (org-string-nw-p date) (concat "\n\n\n" date))
1071 "\n\n\n") text-width 'center)))))
1073 (defun org-ascii-inner-template (contents info)
1074 "Return complete document string after ASCII conversion.
1075 CONTENTS is the transcoded contents string. INFO is a plist
1076 holding export options."
1077 (org-element-normalize-string
1078 (let ((global-margin (plist-get info :ascii-global-margin)))
1079 (org-ascii--indent-string
1080 (concat
1081 ;; 1. Document's body.
1082 contents
1083 ;; 2. Footnote definitions.
1084 (let ((definitions (org-export-collect-footnote-definitions info))
1085 ;; Insert full links right inside the footnote definition
1086 ;; as they have no chance to be inserted later.
1087 (info (org-combine-plists info '(:ascii-links-to-notes nil))))
1088 (when definitions
1089 (concat
1090 "\n\n\n"
1091 (let ((title (org-ascii--translate "Footnotes" info)))
1092 (concat
1093 title "\n"
1094 (make-string
1095 (string-width title)
1096 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
1097 "\n\n"
1098 (let ((text-width (- (plist-get info :ascii-text-width)
1099 global-margin)))
1100 (mapconcat
1101 (lambda (ref)
1102 (let ((id (format "[%s] " (car ref))))
1103 ;; Distinguish between inline definitions and
1104 ;; full-fledged definitions.
1105 (org-trim
1106 (let ((def (nth 2 ref)))
1107 (if (org-element-map def org-element-all-elements
1108 #'identity info 'first-match)
1109 ;; Full-fledged definition: footnote ID is
1110 ;; inserted inside the first parsed
1111 ;; paragraph (FIRST), if any, to be sure
1112 ;; filling will take it into consideration.
1113 (let ((first (car (org-element-contents def))))
1114 (if (not (eq (org-element-type first) 'paragraph))
1115 (concat id "\n" (org-export-data def info))
1116 (push id (nthcdr 2 first))
1117 (org-export-data def info)))
1118 ;; Fill paragraph once footnote ID is inserted
1119 ;; in order to have a correct length for first
1120 ;; line.
1121 (org-ascii--fill-string
1122 (concat id (org-export-data def info))
1123 text-width info))))))
1124 definitions "\n\n"))))))
1125 global-margin))))
1127 (defun org-ascii-template (contents info)
1128 "Return complete document string after ASCII conversion.
1129 CONTENTS is the transcoded contents string. INFO is a plist
1130 holding export options."
1131 (let ((global-margin (plist-get info :ascii-global-margin)))
1132 (concat
1133 ;; Build title block.
1134 (org-ascii--indent-string
1135 (concat (org-ascii-template--document-title info)
1136 ;; 2. Table of contents.
1137 (let ((depth (plist-get info :with-toc)))
1138 (when depth
1139 (concat
1140 (org-ascii--build-toc info (and (wholenump depth) depth))
1141 "\n\n\n"))))
1142 global-margin)
1143 ;; Document's body.
1144 contents
1145 ;; Creator. Justify it to the bottom right.
1146 (and (plist-get info :with-creator)
1147 (org-ascii--indent-string
1148 (let ((text-width
1149 (- (plist-get info :ascii-text-width) global-margin)))
1150 (concat
1151 "\n\n\n"
1152 (org-ascii--fill-string
1153 (plist-get info :creator) text-width info 'right)))
1154 global-margin)))))
1156 (defun org-ascii--translate (s info)
1157 "Translate string S according to specified language and charset.
1158 INFO is a plist used as a communication channel."
1159 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1160 (org-export-translate s charset info)))
1164 ;;; Transcode Functions
1166 ;;;; Bold
1168 (defun org-ascii-bold (_bold contents _info)
1169 "Transcode BOLD from Org to ASCII.
1170 CONTENTS is the text with bold markup. INFO is a plist holding
1171 contextual information."
1172 (format "*%s*" contents))
1175 ;;;; Center Block
1177 (defun org-ascii-center-block (_center-block contents _info)
1178 "Transcode a CENTER-BLOCK element from Org to ASCII.
1179 CONTENTS holds the contents of the block. INFO is a plist
1180 holding contextual information."
1181 ;; Center has already been taken care of at a lower level, so
1182 ;; there's nothing left to do.
1183 contents)
1186 ;;;; Clock
1188 (defun org-ascii-clock (clock _contents info)
1189 "Transcode a CLOCK object from Org to ASCII.
1190 CONTENTS is nil. INFO is a plist holding contextual
1191 information."
1192 (org-ascii--justify-element
1193 (concat org-clock-string " "
1194 (org-timestamp-translate (org-element-property :value clock))
1195 (let ((time (org-element-property :duration clock)))
1196 (and time
1197 (concat " => "
1198 (apply 'format
1199 "%2s:%02s"
1200 (org-split-string time ":"))))))
1201 clock info))
1204 ;;;; Code
1206 (defun org-ascii-code (code _contents info)
1207 "Return a CODE object from Org to ASCII.
1208 CONTENTS is nil. INFO is a plist holding contextual
1209 information."
1210 (format (plist-get info :ascii-verbatim-format)
1211 (org-element-property :value code)))
1214 ;;;; Drawer
1216 (defun org-ascii-drawer (drawer contents info)
1217 "Transcode a DRAWER element from Org to ASCII.
1218 CONTENTS holds the contents of the block. INFO is a plist
1219 holding contextual information."
1220 (let ((name (org-element-property :drawer-name drawer))
1221 (width (org-ascii--current-text-width drawer info)))
1222 (funcall (plist-get info :ascii-format-drawer-function)
1223 name contents width)))
1226 ;;;; Dynamic Block
1228 (defun org-ascii-dynamic-block (_dynamic-block contents _info)
1229 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1230 CONTENTS holds the contents of the block. INFO is a plist
1231 holding contextual information."
1232 contents)
1235 ;;;; Entity
1237 (defun org-ascii-entity (entity _contents info)
1238 "Transcode an ENTITY object from Org to ASCII.
1239 CONTENTS are the definition itself. INFO is a plist holding
1240 contextual information."
1241 (org-element-property
1242 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1243 entity))
1246 ;;;; Example Block
1248 (defun org-ascii-example-block (example-block _contents info)
1249 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1250 CONTENTS is nil. INFO is a plist holding contextual information."
1251 (org-ascii--justify-element
1252 (org-ascii--box-string
1253 (org-export-format-code-default example-block info) info)
1254 example-block info))
1257 ;;;; Export Snippet
1259 (defun org-ascii-export-snippet (export-snippet _contents _info)
1260 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1261 CONTENTS is nil. INFO is a plist holding contextual information."
1262 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1263 (org-element-property :value export-snippet)))
1266 ;;;; Export Block
1268 (defun org-ascii-export-block (export-block _contents info)
1269 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1270 CONTENTS is nil. INFO is a plist holding contextual information."
1271 (when (string= (org-element-property :type export-block) "ASCII")
1272 (org-ascii--justify-element
1273 (org-element-property :value export-block) export-block info)))
1276 ;;;; Fixed Width
1278 (defun org-ascii-fixed-width (fixed-width _contents info)
1279 "Transcode a FIXED-WIDTH element from Org to ASCII.
1280 CONTENTS is nil. INFO is a plist holding contextual information."
1281 (org-ascii--justify-element
1282 (org-ascii--box-string
1283 (org-remove-indentation
1284 (org-element-property :value fixed-width)) info)
1285 fixed-width info))
1288 ;;;; Footnote Definition
1290 ;; Footnote Definitions are ignored. They are compiled at the end of
1291 ;; the document, by `org-ascii-inner-template'.
1294 ;;;; Footnote Reference
1296 (defun org-ascii-footnote-reference (footnote-reference _contents info)
1297 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1298 CONTENTS is nil. INFO is a plist holding contextual information."
1299 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1302 ;;;; Headline
1304 (defun org-ascii-headline (headline contents info)
1305 "Transcode a HEADLINE element from Org to ASCII.
1306 CONTENTS holds the contents of the headline. INFO is a plist
1307 holding contextual information."
1308 ;; Don't export footnote section, which will be handled at the end
1309 ;; of the template.
1310 (unless (org-element-property :footnote-section-p headline)
1311 (let* ((low-level (org-export-low-level-p headline info))
1312 (width (org-ascii--current-text-width headline info))
1313 ;; Export title early so that any link in it can be
1314 ;; exported and seen in `org-ascii--unique-links'.
1315 (title (org-ascii--build-title headline info width (not low-level)))
1316 ;; Blank lines between headline and its contents.
1317 ;; `org-ascii-headline-spacing', when set, overwrites
1318 ;; original buffer's spacing.
1319 (pre-blanks
1320 (make-string (or (car (plist-get info :ascii-headline-spacing))
1321 (org-element-property :pre-blank headline)
1323 ?\n))
1324 (links (and (plist-get info :ascii-links-to-notes)
1325 (org-ascii--describe-links
1326 (org-ascii--unique-links headline info) width info)))
1327 ;; Re-build contents, inserting section links at the right
1328 ;; place. The cost is low since build results are cached.
1329 (body
1330 (if (not (org-string-nw-p links)) contents
1331 (let* ((contents (org-element-contents headline))
1332 (section (let ((first (car contents)))
1333 (and (eq (org-element-type first) 'section)
1334 first))))
1335 (concat (and section
1336 (concat (org-element-normalize-string
1337 (org-export-data section info))
1338 "\n\n"))
1339 links
1340 (mapconcat (lambda (e) (org-export-data e info))
1341 (if section (cdr contents) contents)
1342 ""))))))
1343 ;; Deep subtree: export it as a list item.
1344 (if low-level
1345 (let* ((bullets (cdr (assq (plist-get info :ascii-charset)
1346 (plist-get info :ascii-bullets))))
1347 (bullet
1348 (format "%c "
1349 (nth (mod (1- low-level) (length bullets)) bullets))))
1350 (concat bullet title "\n" pre-blanks
1351 ;; Contents, indented by length of bullet.
1352 (org-ascii--indent-string body (length bullet))))
1353 ;; Else: Standard headline.
1354 (concat title "\n" pre-blanks body)))))
1357 ;;;; Horizontal Rule
1359 (defun org-ascii-horizontal-rule (horizontal-rule _contents info)
1360 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1361 CONTENTS is nil. INFO is a plist holding contextual
1362 information."
1363 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1364 (spec-width
1365 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1366 (org-ascii--justify-lines
1367 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1368 (string-to-number spec-width)
1369 text-width)
1370 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1371 text-width 'center)))
1374 ;;;; Inline Src Block
1376 (defun org-ascii-inline-src-block (inline-src-block _contents info)
1377 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1378 CONTENTS holds the contents of the item. INFO is a plist holding
1379 contextual information."
1380 (format (plist-get info :ascii-verbatim-format)
1381 (org-element-property :value inline-src-block)))
1384 ;;;; Inlinetask
1386 (defun org-ascii-format-inlinetask-default
1387 (_todo _type _priority _name _tags contents width inlinetask info)
1388 "Format an inline task element for ASCII export.
1389 See `org-ascii-format-inlinetask-function' for a description
1390 of the parameters."
1391 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1392 (width (or width (plist-get info :ascii-inlinetask-width))))
1393 (org-ascii--indent-string
1394 (concat
1395 ;; Top line, with an additional blank line if not in UTF-8.
1396 (make-string width (if utf8p ?━ ?_)) "\n"
1397 (unless utf8p (concat (make-string width ? ) "\n"))
1398 ;; Add title. Fill it if wider than inlinetask.
1399 (let ((title (org-ascii--build-title inlinetask info width)))
1400 (if (<= (string-width title) width) title
1401 (org-ascii--fill-string title width info)))
1402 "\n"
1403 ;; If CONTENTS is not empty, insert it along with
1404 ;; a separator.
1405 (when (org-string-nw-p contents)
1406 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1407 ;; Bottom line.
1408 (make-string width (if utf8p ?━ ?_)))
1409 ;; Flush the inlinetask to the right.
1410 (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
1411 (if (not (org-export-get-parent-headline inlinetask)) 0
1412 (plist-get info :ascii-inner-margin))
1413 (org-ascii--current-text-width inlinetask info)))))
1415 (defun org-ascii-inlinetask (inlinetask contents info)
1416 "Transcode an INLINETASK element from Org to ASCII.
1417 CONTENTS holds the contents of the block. INFO is a plist
1418 holding contextual information."
1419 (let ((width (org-ascii--current-text-width inlinetask info)))
1420 (funcall (plist-get info :ascii-format-inlinetask-function)
1421 ;; todo.
1422 (and (plist-get info :with-todo-keywords)
1423 (let ((todo (org-element-property
1424 :todo-keyword inlinetask)))
1425 (and todo (org-export-data todo info))))
1426 ;; todo-type
1427 (org-element-property :todo-type inlinetask)
1428 ;; priority
1429 (and (plist-get info :with-priority)
1430 (org-element-property :priority inlinetask))
1431 ;; title
1432 (org-export-data (org-element-property :title inlinetask) info)
1433 ;; tags
1434 (and (plist-get info :with-tags)
1435 (org-element-property :tags inlinetask))
1436 ;; contents and width
1437 contents width inlinetask info)))
1440 ;;;; Italic
1442 (defun org-ascii-italic (_italic contents _info)
1443 "Transcode italic from Org to ASCII.
1444 CONTENTS is the text with italic markup. INFO is a plist holding
1445 contextual information."
1446 (format "/%s/" contents))
1449 ;;;; Item
1451 (defun org-ascii-item (item contents info)
1452 "Transcode an ITEM element from Org to ASCII.
1453 CONTENTS holds the contents of the item. INFO is a plist holding
1454 contextual information."
1455 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1456 (checkbox (org-ascii--checkbox item info))
1457 (list-type (org-element-property :type (org-export-get-parent item)))
1458 (bullet
1459 ;; First parent of ITEM is always the plain-list. Get
1460 ;; `:type' property from it.
1461 (org-list-bullet-string
1462 (pcase list-type
1463 (`descriptive
1464 (concat checkbox
1465 (org-export-data (org-element-property :tag item) info)
1466 ": "))
1467 (`ordered
1468 ;; Return correct number for ITEM, paying attention to
1469 ;; counters.
1470 (let* ((struct (org-element-property :structure item))
1471 (bul (org-element-property :bullet item))
1472 (num (number-to-string
1473 (car (last (org-list-get-item-number
1474 (org-element-property :begin item)
1475 struct
1476 (org-list-prevs-alist struct)
1477 (org-list-parents-alist struct)))))))
1478 (replace-regexp-in-string "[0-9]+" num bul)))
1479 (_ (let ((bul (org-element-property :bullet item)))
1480 ;; Change bullets into more visible form if UTF-8 is active.
1481 (if (not utf8p) bul
1482 (replace-regexp-in-string
1483 "-" "•"
1484 (replace-regexp-in-string
1485 "+" "⁃"
1486 (replace-regexp-in-string "*" "‣" bul))))))))))
1487 (concat
1488 bullet
1489 (unless (eq list-type 'descriptive) checkbox)
1490 ;; Contents: Pay attention to indentation. Note: check-boxes are
1491 ;; already taken care of at the paragraph level so they don't
1492 ;; interfere with indentation.
1493 (let ((contents (org-ascii--indent-string contents (string-width bullet))))
1494 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1495 (org-trim contents)
1496 (concat "\n" contents))))))
1499 ;;;; Keyword
1501 (defun org-ascii-keyword (keyword _contents info)
1502 "Transcode a KEYWORD element from Org to ASCII.
1503 CONTENTS is nil. INFO is a plist holding contextual
1504 information."
1505 (let ((key (org-element-property :key keyword))
1506 (value (org-element-property :value keyword)))
1507 (cond
1508 ((string= key "ASCII") (org-ascii--justify-element value keyword info))
1509 ((string= key "TOC")
1510 (org-ascii--justify-element
1511 (let ((case-fold-search t))
1512 (cond
1513 ((string-match-p "\\<headlines\\>" value)
1514 (let ((depth (and (string-match "\\<[0-9]+\\>" value)
1515 (string-to-number (match-string 0 value))))
1516 (localp (string-match-p "\\<local\\>" value)))
1517 (org-ascii--build-toc info depth keyword localp)))
1518 ((string-match-p "\\<tables\\>" value)
1519 (org-ascii--list-tables keyword info))
1520 ((string-match-p "\\<listings\\>" value)
1521 (org-ascii--list-listings keyword info))))
1522 keyword info)))))
1525 ;;;; Latex Environment
1527 (defun org-ascii-latex-environment (latex-environment _contents info)
1528 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1529 CONTENTS is nil. INFO is a plist holding contextual
1530 information."
1531 (when (plist-get info :with-latex)
1532 (org-ascii--justify-element
1533 (org-remove-indentation (org-element-property :value latex-environment))
1534 latex-environment info)))
1537 ;;;; Latex Fragment
1539 (defun org-ascii-latex-fragment (latex-fragment _contents info)
1540 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1541 CONTENTS is nil. INFO is a plist holding contextual
1542 information."
1543 (when (plist-get info :with-latex)
1544 (org-element-property :value latex-fragment)))
1547 ;;;; Line Break
1549 (defun org-ascii-line-break (_line-break _contents _info)
1550 "Transcode a LINE-BREAK object from Org to ASCII.
1551 CONTENTS is nil. INFO is a plist holding contextual
1552 information." hard-newline)
1555 ;;;; Link
1557 (defun org-ascii-link (link desc info)
1558 "Transcode a LINK object from Org to ASCII.
1560 DESC is the description part of the link, or the empty string.
1561 INFO is a plist holding contextual information."
1562 (let ((type (org-element-property :type link)))
1563 (cond
1564 ((org-export-custom-protocol-maybe link desc 'ascii))
1565 ((string= type "coderef")
1566 (let ((ref (org-element-property :path link)))
1567 (format (org-export-get-coderef-format ref desc)
1568 (org-export-resolve-coderef ref info))))
1569 ;; Do not apply a special syntax on radio links. Though, use
1570 ;; transcoded target's contents as output.
1571 ((string= type "radio") desc)
1572 ((member type '("custom-id" "fuzzy" "id"))
1573 (let ((destination (if (string= type "fuzzy")
1574 (org-export-resolve-fuzzy-link link info)
1575 (org-export-resolve-id-link link info))))
1576 (pcase (org-element-type destination)
1577 ((guard desc)
1578 (if (plist-get info :ascii-links-to-notes)
1579 (format "[%s]" desc)
1580 (concat desc
1581 (format " (%s)"
1582 (org-ascii--describe-datum destination info)))))
1583 ;; External file.
1584 (`plain-text destination)
1585 (`headline
1586 (if (org-export-numbered-headline-p destination info)
1587 (mapconcat #'number-to-string
1588 (org-export-get-headline-number destination info)
1589 ".")
1590 (org-export-data (org-element-property :title destination) info)))
1591 ;; Handle enumerable elements and targets within them.
1592 ((and (let number (org-export-get-ordinal
1593 destination info nil #'org-ascii--has-caption-p))
1594 (guard number))
1595 (if (atom number) (number-to-string number)
1596 (mapconcat #'number-to-string number ".")))
1597 ;; Don't know what to do. Signal it.
1598 (_ "???"))))
1600 (let ((raw-link (org-element-property :raw-link link)))
1601 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1602 (concat (format "[%s]" desc)
1603 (and (not (plist-get info :ascii-links-to-notes))
1604 (format " (%s)" raw-link)))))))))
1607 ;;;; Node Properties
1609 (defun org-ascii-node-property (node-property _contents _info)
1610 "Transcode a NODE-PROPERTY element from Org to ASCII.
1611 CONTENTS is nil. INFO is a plist holding contextual
1612 information."
1613 (format "%s:%s"
1614 (org-element-property :key node-property)
1615 (let ((value (org-element-property :value node-property)))
1616 (if value (concat " " value) ""))))
1619 ;;;; Paragraph
1621 (defun org-ascii-paragraph (paragraph contents info)
1622 "Transcode a PARAGRAPH element from Org to ASCII.
1623 CONTENTS is the contents of the paragraph, as a string. INFO is
1624 the plist used as a communication channel."
1625 (org-ascii--justify-element
1626 (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
1627 (if (not (wholenump indented-line-width)) contents
1628 (concat
1629 ;; Do not indent first paragraph in a section.
1630 (unless (and (not (org-export-get-previous-element paragraph info))
1631 (eq (org-element-type (org-export-get-parent paragraph))
1632 'section))
1633 (make-string indented-line-width ?\s))
1634 (replace-regexp-in-string "\\`[ \t]+" "" contents))))
1635 paragraph info))
1638 ;;;; Plain List
1640 (defun org-ascii-plain-list (plain-list contents info)
1641 "Transcode a PLAIN-LIST element from Org to ASCII.
1642 CONTENTS is the contents of the list. INFO is a plist holding
1643 contextual information."
1644 (let ((margin (plist-get info :ascii-list-margin)))
1645 (if (or (< margin 1)
1646 (eq (org-element-type (org-export-get-parent plain-list)) 'item))
1647 contents
1648 (org-ascii--indent-string contents margin))))
1651 ;;;; Plain Text
1653 (defun org-ascii-plain-text (text info)
1654 "Transcode a TEXT string from Org to ASCII.
1655 INFO is a plist used as a communication channel."
1656 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1657 (when (and utf8p (plist-get info :with-smart-quotes))
1658 (setq text (org-export-activate-smart-quotes text :utf-8 info)))
1659 (if (not (plist-get info :with-special-strings)) text
1660 (setq text (replace-regexp-in-string "\\\\-" "" text))
1661 (if (not utf8p) text
1662 ;; Usual replacements in utf-8 with proper option set.
1663 (replace-regexp-in-string
1664 "\\.\\.\\." "…"
1665 (replace-regexp-in-string
1666 "--" "–"
1667 (replace-regexp-in-string "---" "—" text)))))))
1670 ;;;; Planning
1672 (defun org-ascii-planning (planning _contents info)
1673 "Transcode a PLANNING element from Org to ASCII.
1674 CONTENTS is nil. INFO is a plist used as a communication
1675 channel."
1676 (org-ascii--justify-element
1677 (mapconcat
1678 #'identity
1679 (delq nil
1680 (list (let ((closed (org-element-property :closed planning)))
1681 (when closed
1682 (concat org-closed-string " "
1683 (org-timestamp-translate closed))))
1684 (let ((deadline (org-element-property :deadline planning)))
1685 (when deadline
1686 (concat org-deadline-string " "
1687 (org-timestamp-translate deadline))))
1688 (let ((scheduled (org-element-property :scheduled planning)))
1689 (when scheduled
1690 (concat org-scheduled-string " "
1691 (org-timestamp-translate scheduled))))))
1692 " ")
1693 planning info))
1696 ;;;; Property Drawer
1698 (defun org-ascii-property-drawer (property-drawer contents info)
1699 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1700 CONTENTS holds the contents of the drawer. INFO is a plist
1701 holding contextual information."
1702 (and (org-string-nw-p contents)
1703 (org-ascii--justify-element contents property-drawer info)))
1706 ;;;; Quote Block
1708 (defun org-ascii-quote-block (_quote-block contents info)
1709 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1710 CONTENTS holds the contents of the block. INFO is a plist
1711 holding contextual information."
1712 (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
1715 ;;;; Radio Target
1717 (defun org-ascii-radio-target (_radio-target contents _info)
1718 "Transcode a RADIO-TARGET object from Org to ASCII.
1719 CONTENTS is the contents of the target. INFO is a plist holding
1720 contextual information."
1721 contents)
1724 ;;;; Section
1726 (defun org-ascii-section (section contents info)
1727 "Transcode a SECTION element from Org to ASCII.
1728 CONTENTS is the contents of the section. INFO is a plist holding
1729 contextual information."
1730 (let ((links
1731 (and (plist-get info :ascii-links-to-notes)
1732 ;; Take care of links in first section of the document.
1733 (not (org-element-lineage section '(headline)))
1734 (org-ascii--describe-links
1735 (org-ascii--unique-links section info)
1736 (org-ascii--current-text-width section info)
1737 info))))
1738 (org-ascii--indent-string
1739 (if (not (org-string-nw-p links)) contents
1740 (concat (org-element-normalize-string contents) "\n\n" links))
1741 ;; Do not apply inner margin if parent headline is low level.
1742 (let ((headline (org-export-get-parent-headline section)))
1743 (if (or (not headline) (org-export-low-level-p headline info)) 0
1744 (plist-get info :ascii-inner-margin))))))
1747 ;;;; Special Block
1749 (defun org-ascii-special-block (_special-block contents _info)
1750 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1751 CONTENTS holds the contents of the block. INFO is a plist
1752 holding contextual information."
1753 ;; "JUSTIFYLEFT" and "JUSTIFYRIGHT" have already been taken care of
1754 ;; at a lower level. There is no other special block type to
1755 ;; handle.
1756 contents)
1759 ;;;; Src Block
1761 (defun org-ascii-src-block (src-block _contents info)
1762 "Transcode a SRC-BLOCK element from Org to ASCII.
1763 CONTENTS holds the contents of the item. INFO is a plist holding
1764 contextual information."
1765 (let ((caption (org-ascii--build-caption src-block info))
1766 (caption-above-p (plist-get info :ascii-caption-above))
1767 (code (org-export-format-code-default src-block info)))
1768 (if (equal code "") ""
1769 (org-ascii--justify-element
1770 (concat
1771 (and caption caption-above-p (concat caption "\n"))
1772 (org-ascii--box-string code info)
1773 (and caption (not caption-above-p) (concat "\n" caption)))
1774 src-block info))))
1777 ;;;; Statistics Cookie
1779 (defun org-ascii-statistics-cookie (statistics-cookie _contents _info)
1780 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1781 CONTENTS is nil. INFO is a plist holding contextual information."
1782 (org-element-property :value statistics-cookie))
1785 ;;;; Subscript
1787 (defun org-ascii-subscript (subscript contents _info)
1788 "Transcode a SUBSCRIPT object from Org to ASCII.
1789 CONTENTS is the contents of the object. INFO is a plist holding
1790 contextual information."
1791 (if (org-element-property :use-brackets-p subscript)
1792 (format "_{%s}" contents)
1793 (format "_%s" contents)))
1796 ;;;; Superscript
1798 (defun org-ascii-superscript (superscript contents _info)
1799 "Transcode a SUPERSCRIPT object from Org to ASCII.
1800 CONTENTS is the contents of the object. INFO is a plist holding
1801 contextual information."
1802 (if (org-element-property :use-brackets-p superscript)
1803 (format "^{%s}" contents)
1804 (format "^%s" contents)))
1807 ;;;; Strike-through
1809 (defun org-ascii-strike-through (_strike-through contents _info)
1810 "Transcode STRIKE-THROUGH from Org to ASCII.
1811 CONTENTS is text with strike-through markup. INFO is a plist
1812 holding contextual information."
1813 (format "+%s+" contents))
1816 ;;;; Table
1818 (defun org-ascii-table (table contents info)
1819 "Transcode a TABLE element from Org to ASCII.
1820 CONTENTS is the contents of the table. INFO is a plist holding
1821 contextual information."
1822 (let ((caption (org-ascii--build-caption table info))
1823 (caption-above-p (plist-get info :ascii-caption-above)))
1824 (org-ascii--justify-element
1825 (concat
1826 ;; Possibly add a caption string above.
1827 (and caption caption-above-p (concat caption "\n"))
1828 ;; Insert table. Note: "table.el" tables are left unmodified.
1829 (cond ((eq (org-element-property :type table) 'org) contents)
1830 ((and (plist-get info :ascii-table-use-ascii-art)
1831 (eq (plist-get info :ascii-charset) 'utf-8)
1832 (require 'ascii-art-to-unicode nil t))
1833 (with-temp-buffer
1834 (insert (org-remove-indentation
1835 (org-element-property :value table)))
1836 (goto-char (point-min))
1837 (aa2u)
1838 (goto-char (point-max))
1839 (skip-chars-backward " \r\t\n")
1840 (buffer-substring (point-min) (point))))
1841 (t (org-remove-indentation (org-element-property :value table))))
1842 ;; Possible add a caption string below.
1843 (and (not caption-above-p) caption))
1844 table info)))
1847 ;;;; Table Cell
1849 (defun org-ascii--table-cell-width (table-cell info)
1850 "Return width of TABLE-CELL.
1852 INFO is a plist used as a communication channel.
1854 Width of a cell is determined either by a width cookie in the
1855 same column as the cell, or by the maximum cell's length in that
1856 column.
1858 When `org-ascii-table-widen-columns' is non-nil, width cookies
1859 are ignored."
1860 (let* ((row (org-export-get-parent table-cell))
1861 (table (org-export-get-parent row))
1862 (col (let ((cells (org-element-contents row)))
1863 (- (length cells) (length (memq table-cell cells)))))
1864 (cache
1865 (or (plist-get info :ascii-table-cell-width-cache)
1866 (plist-get (setq info
1867 (plist-put info :ascii-table-cell-width-cache
1868 (make-hash-table :test 'equal)))
1869 :ascii-table-cell-width-cache)))
1870 (key (cons table col))
1871 (widenp (plist-get info :ascii-table-widen-columns)))
1872 (or (gethash key cache)
1873 (puthash
1875 (let ((cookie-width (org-export-table-cell-width table-cell info)))
1876 (or (and (not widenp) cookie-width)
1877 (let ((contents-width
1878 (let ((max-width 0))
1879 (org-element-map table 'table-row
1880 (lambda (row)
1881 (setq max-width
1882 (max (string-width
1883 (org-export-data
1884 (org-element-contents
1885 (elt (org-element-contents row) col))
1886 info))
1887 max-width)))
1888 info)
1889 max-width)))
1890 (cond ((not cookie-width) contents-width)
1891 (widenp (max cookie-width contents-width))
1892 (t cookie-width)))))
1893 cache))))
1895 (defun org-ascii-table-cell (table-cell contents info)
1896 "Transcode a TABLE-CELL object from Org to ASCII.
1897 CONTENTS is the cell contents. INFO is a plist used as
1898 a communication channel."
1899 ;; Determine column width. When `org-ascii-table-widen-columns'
1900 ;; is nil and some width cookie has set it, use that value.
1901 ;; Otherwise, compute the maximum width among transcoded data of
1902 ;; each cell in the column.
1903 (let ((width (org-ascii--table-cell-width table-cell info)))
1904 ;; When contents are too large, truncate them.
1905 (unless (or (plist-get info :ascii-table-widen-columns)
1906 (<= (string-width (or contents "")) width))
1907 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1908 ;; Align contents correctly within the cell.
1909 (let* ((indent-tabs-mode nil)
1910 (data
1911 (when contents
1912 (org-ascii--justify-lines
1913 contents width
1914 (org-export-table-cell-alignment table-cell info)))))
1915 (setq contents
1916 (concat data
1917 (make-string (- width (string-width (or data ""))) ?\s))))
1918 ;; Return cell.
1919 (concat (format " %s " contents)
1920 (when (memq 'right (org-export-table-cell-borders table-cell info))
1921 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1924 ;;;; Table Row
1926 (defun org-ascii-table-row (table-row contents info)
1927 "Transcode a TABLE-ROW element from Org to ASCII.
1928 CONTENTS is the row contents. INFO is a plist used as
1929 a communication channel."
1930 (when (eq (org-element-property :type table-row) 'standard)
1931 (let ((build-hline
1932 (function
1933 (lambda (lcorner horiz vert rcorner)
1934 (concat
1935 (apply
1936 'concat
1937 (org-element-map table-row 'table-cell
1938 (lambda (cell)
1939 (let ((width (org-ascii--table-cell-width cell info))
1940 (borders (org-export-table-cell-borders cell info)))
1941 (concat
1942 ;; In order to know if CELL starts the row, do
1943 ;; not compare it with the first cell in the
1944 ;; row as there might be a special column.
1945 ;; Instead, compare it with first exportable
1946 ;; cell, obtained with `org-element-map'.
1947 (when (and (memq 'left borders)
1948 (eq (org-element-map table-row 'table-cell
1949 'identity info t)
1950 cell))
1951 lcorner)
1952 (make-string (+ 2 width) (string-to-char horiz))
1953 (cond
1954 ((not (memq 'right borders)) nil)
1955 ((eq (car (last (org-element-contents table-row))) cell)
1956 rcorner)
1957 (t vert)))))
1958 info)) "\n"))))
1959 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1960 (borders (org-export-table-cell-borders
1961 (org-element-map table-row 'table-cell 'identity info t)
1962 info)))
1963 (concat (cond
1964 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1965 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1966 (funcall build-hline "+" "-" "+" "+")))
1967 ((memq 'above borders)
1968 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1969 (funcall build-hline "+" "-" "+" "+"))))
1970 (when (memq 'left borders) (if utf8p "│" "|"))
1971 contents "\n"
1972 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1973 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1974 (funcall build-hline "+" "-" "+" "+")))))))
1977 ;;;; Timestamp
1979 (defun org-ascii-timestamp (timestamp _contents info)
1980 "Transcode a TIMESTAMP object from Org to ASCII.
1981 CONTENTS is nil. INFO is a plist holding contextual information."
1982 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1985 ;;;; Underline
1987 (defun org-ascii-underline (_underline contents _info)
1988 "Transcode UNDERLINE from Org to ASCII.
1989 CONTENTS is the text with underline markup. INFO is a plist
1990 holding contextual information."
1991 (format "_%s_" contents))
1994 ;;;; Verbatim
1996 (defun org-ascii-verbatim (verbatim _contents info)
1997 "Return a VERBATIM object from Org to ASCII.
1998 CONTENTS is nil. INFO is a plist holding contextual information."
1999 (format (plist-get info :ascii-verbatim-format)
2000 (org-element-property :value verbatim)))
2003 ;;;; Verse Block
2005 (defun org-ascii-verse-block (verse-block contents info)
2006 "Transcode a VERSE-BLOCK element from Org to ASCII.
2007 CONTENTS is verse block contents. INFO is a plist holding
2008 contextual information."
2009 (org-ascii--indent-string
2010 (org-ascii--justify-element contents verse-block info)
2011 (plist-get info :ascii-quote-margin)))
2015 ;;; Filters
2017 (defun org-ascii-filter-headline-blank-lines (headline _backend info)
2018 "Filter controlling number of blank lines after a headline.
2020 HEADLINE is a string representing a transcoded headline. BACKEND
2021 is symbol specifying back-end used for export. INFO is plist
2022 containing the communication channel.
2024 This function only applies to `ascii' back-end. See
2025 `org-ascii-headline-spacing' for information."
2026 (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
2027 (if (not headline-spacing) headline
2028 (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
2029 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
2031 (defun org-ascii-filter-paragraph-spacing (tree _backend info)
2032 "Filter controlling number of blank lines between paragraphs.
2034 TREE is the parse tree. BACKEND is the symbol specifying
2035 back-end used for export. INFO is a plist used as
2036 a communication channel.
2038 See `org-ascii-paragraph-spacing' for information."
2039 (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
2040 (when (wholenump paragraph-spacing)
2041 (org-element-map tree 'paragraph
2042 (lambda (p)
2043 (when (eq (org-element-type (org-export-get-next-element p info))
2044 'paragraph)
2045 (org-element-put-property p :post-blank paragraph-spacing))))))
2046 tree)
2048 (defun org-ascii-filter-comment-spacing (tree _backend info)
2049 "Filter removing blank lines between comments.
2050 TREE is the parse tree. BACKEND is the symbol specifying
2051 back-end used for export. INFO is a plist used as
2052 a communication channel."
2053 (org-element-map tree '(comment comment-block)
2054 (lambda (c)
2055 (when (memq (org-element-type (org-export-get-next-element c info))
2056 '(comment comment-block))
2057 (org-element-put-property c :post-blank 0))))
2058 tree)
2062 ;;; End-user functions
2064 ;;;###autoload
2065 (defun org-ascii-export-as-ascii
2066 (&optional async subtreep visible-only body-only ext-plist)
2067 "Export current buffer to a text buffer.
2069 If narrowing is active in the current buffer, only export its
2070 narrowed part.
2072 If a region is active, export that region.
2074 A non-nil optional argument ASYNC means the process should happen
2075 asynchronously. The resulting buffer should be accessible
2076 through the `org-export-stack' interface.
2078 When optional argument SUBTREEP is non-nil, export the sub-tree
2079 at point, extracting information from the headline properties
2080 first.
2082 When optional argument VISIBLE-ONLY is non-nil, don't export
2083 contents of hidden elements.
2085 When optional argument BODY-ONLY is non-nil, strip title and
2086 table of contents from output.
2088 EXT-PLIST, when provided, is a property list with external
2089 parameters overriding Org default settings, but still inferior to
2090 file-local settings.
2092 Export is done in a buffer named \"*Org ASCII Export*\", which
2093 will be displayed when `org-export-show-temporary-export-buffer'
2094 is non-nil."
2095 (interactive)
2096 (org-export-to-buffer 'ascii "*Org ASCII Export*"
2097 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
2099 ;;;###autoload
2100 (defun org-ascii-export-to-ascii
2101 (&optional async subtreep visible-only body-only ext-plist)
2102 "Export current buffer to a text file.
2104 If narrowing is active in the current buffer, only export its
2105 narrowed part.
2107 If a region is active, export that region.
2109 A non-nil optional argument ASYNC means the process should happen
2110 asynchronously. The resulting file should be accessible through
2111 the `org-export-stack' interface.
2113 When optional argument SUBTREEP is non-nil, export the sub-tree
2114 at point, extracting information from the headline properties
2115 first.
2117 When optional argument VISIBLE-ONLY is non-nil, don't export
2118 contents of hidden elements.
2120 When optional argument BODY-ONLY is non-nil, strip title and
2121 table of contents from output.
2123 EXT-PLIST, when provided, is a property list with external
2124 parameters overriding Org default settings, but still inferior to
2125 file-local settings.
2127 Return output file's name."
2128 (interactive)
2129 (let ((file (org-export-output-file-name ".txt" subtreep)))
2130 (org-export-to-file 'ascii file
2131 async subtreep visible-only body-only ext-plist)))
2133 ;;;###autoload
2134 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
2135 "Publish an Org file to ASCII.
2137 FILENAME is the filename of the Org file to be published. PLIST
2138 is the property list for the given project. PUB-DIR is the
2139 publishing directory.
2141 Return output file name."
2142 (org-publish-org-to
2143 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
2145 ;;;###autoload
2146 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
2147 "Publish an Org file to Latin-1.
2149 FILENAME is the filename of the Org file to be published. PLIST
2150 is the property list for the given project. PUB-DIR is the
2151 publishing directory.
2153 Return output file name."
2154 (org-publish-org-to
2155 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
2157 ;;;###autoload
2158 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
2159 "Publish an org file to UTF-8.
2161 FILENAME is the filename of the Org file to be published. PLIST
2162 is the property list for the given project. PUB-DIR is the
2163 publishing directory.
2165 Return output file name."
2166 (org-publish-org-to
2167 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2170 (provide 'ox-ascii)
2172 ;; Local variables:
2173 ;; generated-autoload-file: "org-loaddefs.el"
2174 ;; coding: utf-8
2175 ;; End:
2177 ;;; ox-ascii.el ends here