org.texi: Fix typo
[org-mode/org-tableheadings.git] / lisp / ox-ascii.el
blobc4b34cbbf6ebf3a4428e5cb950865de20735f6f7
1 ;;; ox-ascii.el --- ASCII Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements an ASCII back-end for Org generic exporter.
26 ;; See Org manual for more information.
28 ;;; Code:
30 (eval-when-compile (require 'cl))
31 (require 'ox)
32 (require 'ox-publish)
34 (declare-function aa2u "ext:ascii-art-to-unicode" ())
36 ;;; Define Back-End
38 ;; The following setting won't allow to modify preferred charset
39 ;; through a buffer keyword or an option item, but, since the property
40 ;; will appear in communication channel nonetheless, it allows to
41 ;; override `org-ascii-charset' variable on the fly by the ext-plist
42 ;; mechanism.
44 ;; We also install a filter for headlines and sections, in order to
45 ;; control blank lines separating them in output string.
47 (org-export-define-backend 'ascii
48 '((bold . org-ascii-bold)
49 (center-block . org-ascii-center-block)
50 (clock . org-ascii-clock)
51 (code . org-ascii-code)
52 (comment . (lambda (&rest args) ""))
53 (comment-block . (lambda (&rest args) ""))
54 (drawer . org-ascii-drawer)
55 (dynamic-block . org-ascii-dynamic-block)
56 (entity . org-ascii-entity)
57 (example-block . org-ascii-example-block)
58 (export-block . org-ascii-export-block)
59 (export-snippet . org-ascii-export-snippet)
60 (fixed-width . org-ascii-fixed-width)
61 (footnote-reference . org-ascii-footnote-reference)
62 (headline . org-ascii-headline)
63 (horizontal-rule . org-ascii-horizontal-rule)
64 (inline-src-block . org-ascii-inline-src-block)
65 (inlinetask . org-ascii-inlinetask)
66 (inner-template . org-ascii-inner-template)
67 (italic . org-ascii-italic)
68 (item . org-ascii-item)
69 (keyword . org-ascii-keyword)
70 (latex-environment . org-ascii-latex-environment)
71 (latex-fragment . org-ascii-latex-fragment)
72 (line-break . org-ascii-line-break)
73 (link . org-ascii-link)
74 (node-property . org-ascii-node-property)
75 (paragraph . org-ascii-paragraph)
76 (plain-list . org-ascii-plain-list)
77 (plain-text . org-ascii-plain-text)
78 (planning . org-ascii-planning)
79 (property-drawer . org-ascii-property-drawer)
80 (quote-block . org-ascii-quote-block)
81 (radio-target . org-ascii-radio-target)
82 (section . org-ascii-section)
83 (special-block . org-ascii-special-block)
84 (src-block . org-ascii-src-block)
85 (statistics-cookie . org-ascii-statistics-cookie)
86 (strike-through . org-ascii-strike-through)
87 (subscript . org-ascii-subscript)
88 (superscript . org-ascii-superscript)
89 (table . org-ascii-table)
90 (table-cell . org-ascii-table-cell)
91 (table-row . org-ascii-table-row)
92 (target . org-ascii-target)
93 (template . org-ascii-template)
94 (timestamp . org-ascii-timestamp)
95 (underline . org-ascii-underline)
96 (verbatim . org-ascii-verbatim)
97 (verse-block . org-ascii-verse-block))
98 :export-block "ASCII"
99 :menu-entry
100 '(?t "Export to Plain Text"
101 ((?A "As ASCII buffer"
102 (lambda (a s v b)
103 (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
104 (?a "As ASCII file"
105 (lambda (a s v b)
106 (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
107 (?L "As Latin1 buffer"
108 (lambda (a s v b)
109 (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
110 (?l "As Latin1 file"
111 (lambda (a s v b)
112 (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
113 (?U "As UTF-8 buffer"
114 (lambda (a s v b)
115 (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
116 (?u "As UTF-8 file"
117 (lambda (a s v b)
118 (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
119 :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
120 (:filter-parse-tree org-ascii-filter-paragraph-spacing
121 org-ascii-filter-comment-spacing)
122 (:filter-section . org-ascii-filter-headline-blank-lines))
123 :options-alist
124 '((:ascii-bullets nil nil org-ascii-bullets)
125 (:ascii-caption-above nil nil org-ascii-caption-above)
126 (:ascii-charset nil nil org-ascii-charset)
127 (:ascii-global-margin nil nil org-ascii-global-margin)
128 (:ascii-format-drawer-function nil nil org-ascii-format-drawer-function)
129 (:ascii-format-inlinetask-function
130 nil nil org-ascii-format-inlinetask-function)
131 (:ascii-headline-spacing nil nil org-ascii-headline-spacing)
132 (:ascii-indented-line-width nil nil org-ascii-indented-line-width)
133 (:ascii-inlinetask-width nil nil org-ascii-inlinetask-width)
134 (:ascii-inner-margin nil nil org-ascii-inner-margin)
135 (:ascii-links-to-notes nil nil org-ascii-links-to-notes)
136 (:ascii-list-margin nil nil org-ascii-list-margin)
137 (:ascii-paragraph-spacing nil nil org-ascii-paragraph-spacing)
138 (:ascii-quote-margin nil nil org-ascii-quote-margin)
139 (:ascii-table-keep-all-vertical-lines
140 nil nil org-ascii-table-keep-all-vertical-lines)
141 (:ascii-table-use-ascii-art nil nil org-ascii-table-use-ascii-art)
142 (:ascii-table-widen-columns nil nil org-ascii-table-widen-columns)
143 (:ascii-text-width nil nil org-ascii-text-width)
144 (:ascii-underline nil nil org-ascii-underline)
145 (:ascii-verbatim-format nil nil org-ascii-verbatim-format)))
149 ;;; User Configurable Variables
151 (defgroup org-export-ascii nil
152 "Options for exporting Org mode files to ASCII."
153 :tag "Org Export ASCII"
154 :group 'org-export)
156 (defcustom org-ascii-text-width 72
157 "Maximum width of exported text.
158 This number includes margin size, as set in
159 `org-ascii-global-margin'."
160 :group 'org-export-ascii
161 :version "24.4"
162 :package-version '(Org . "8.0")
163 :type 'integer)
165 (defcustom org-ascii-global-margin 0
166 "Width of the left margin, in number of characters."
167 :group 'org-export-ascii
168 :version "24.4"
169 :package-version '(Org . "8.0")
170 :type 'integer)
172 (defcustom org-ascii-inner-margin 2
173 "Width of the inner margin, in number of characters.
174 Inner margin is applied between each headline."
175 :group 'org-export-ascii
176 :version "24.4"
177 :package-version '(Org . "8.0")
178 :type 'integer)
180 (defcustom org-ascii-quote-margin 6
181 "Width of margin used for quoting text, in characters.
182 This margin is applied on both sides of the text."
183 :group 'org-export-ascii
184 :version "24.4"
185 :package-version '(Org . "8.0")
186 :type 'integer)
188 (defcustom org-ascii-list-margin 0
189 "Width of margin used for plain lists, in characters.
190 This margin applies to top level list only, not to its
191 sub-lists."
192 :group 'org-export-ascii
193 :version "25.1"
194 :package-version '(Org . "8.3")
195 :type 'integer)
197 (defcustom org-ascii-inlinetask-width 30
198 "Width of inline tasks, in number of characters.
199 This number ignores any margin."
200 :group 'org-export-ascii
201 :version "24.4"
202 :package-version '(Org . "8.0")
203 :type 'integer)
205 (defcustom org-ascii-headline-spacing '(1 . 2)
206 "Number of blank lines inserted around headlines.
208 This variable can be set to a cons cell. In that case, its car
209 represents the number of blank lines present before headline
210 contents whereas its cdr reflects the number of blank lines after
211 contents.
213 A nil value replicates the number of blank lines found in the
214 original Org buffer at the same place."
215 :group 'org-export-ascii
216 :version "24.4"
217 :package-version '(Org . "8.0")
218 :type '(choice
219 (const :tag "Replicate original spacing" nil)
220 (cons :tag "Set an uniform spacing"
221 (integer :tag "Number of blank lines before contents")
222 (integer :tag "Number of blank lines after contents"))))
224 (defcustom org-ascii-indented-line-width 'auto
225 "Additional indentation width for the first line in a paragraph.
226 If the value is an integer, indent the first line of each
227 paragraph by this width, unless it is located at the beginning of
228 a section, in which case indentation is removed from that line.
229 If it is the symbol `auto' preserve indentation from original
230 document."
231 :group 'org-export-ascii
232 :version "24.4"
233 :package-version '(Org . "8.0")
234 :type '(choice
235 (integer :tag "Number of white spaces characters")
236 (const :tag "Preserve original width" auto)))
238 (defcustom org-ascii-paragraph-spacing 'auto
239 "Number of white lines between paragraphs.
240 If the value is an integer, add this number of blank lines
241 between contiguous paragraphs. If is it the symbol `auto', keep
242 the same number of blank lines as in the original document."
243 :group 'org-export-ascii
244 :version "24.4"
245 :package-version '(Org . "8.0")
246 :type '(choice
247 (integer :tag "Number of blank lines")
248 (const :tag "Preserve original spacing" auto)))
250 (defcustom org-ascii-charset 'ascii
251 "The charset allowed to represent various elements and objects.
252 Possible values are:
253 `ascii' Only use plain ASCII characters
254 `latin1' Include Latin-1 characters
255 `utf-8' Use all UTF-8 characters"
256 :group 'org-export-ascii
257 :version "24.4"
258 :package-version '(Org . "8.0")
259 :type '(choice
260 (const :tag "ASCII" ascii)
261 (const :tag "Latin-1" latin1)
262 (const :tag "UTF-8" utf-8)))
264 (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
265 (latin1 ?= ?~ ?-)
266 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
267 "Characters for underlining headings in ASCII export.
269 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
270 and whose value is a list of characters.
272 For each supported charset, this variable associates a sequence
273 of underline characters. In a sequence, the characters will be
274 used in order for headlines level 1, 2, ... If no character is
275 available for a given level, the headline won't be underlined."
276 :group 'org-export-ascii
277 :version "24.4"
278 :package-version '(Org . "8.0")
279 :type '(list
280 (cons :tag "Underline characters sequence"
281 (const :tag "ASCII charset" ascii)
282 (repeat character))
283 (cons :tag "Underline characters sequence"
284 (const :tag "Latin-1 charset" latin1)
285 (repeat character))
286 (cons :tag "Underline characters sequence"
287 (const :tag "UTF-8 charset" utf-8)
288 (repeat character))))
290 (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
291 (latin1 ?§ ?¶)
292 (utf-8 ?◊))
293 "Bullet characters for headlines converted to lists in ASCII export.
295 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
296 and whose value is a list of characters.
298 The first character is used for the first level considered as low
299 level, and so on. If there are more levels than characters given
300 here, the list will be repeated.
302 Note that this variable doesn't affect plain lists
303 representation."
304 :group 'org-export-ascii
305 :version "24.4"
306 :package-version '(Org . "8.0")
307 :type '(list
308 (cons :tag "Bullet characters for low level headlines"
309 (const :tag "ASCII charset" ascii)
310 (repeat character))
311 (cons :tag "Bullet characters for low level headlines"
312 (const :tag "Latin-1 charset" latin1)
313 (repeat character))
314 (cons :tag "Bullet characters for low level headlines"
315 (const :tag "UTF-8 charset" utf-8)
316 (repeat character))))
318 (defcustom org-ascii-links-to-notes t
319 "Non-nil means convert links to notes before the next headline.
320 When nil, the link will be exported in place. If the line
321 becomes long in this way, it will be wrapped."
322 :group 'org-export-ascii
323 :version "24.4"
324 :package-version '(Org . "8.0")
325 :type 'boolean)
327 (defcustom org-ascii-table-keep-all-vertical-lines nil
328 "Non-nil means keep all vertical lines in ASCII tables.
329 When nil, vertical lines will be removed except for those needed
330 for column grouping."
331 :group 'org-export-ascii
332 :version "24.4"
333 :package-version '(Org . "8.0")
334 :type 'boolean)
336 (defcustom org-ascii-table-widen-columns t
337 "Non-nil means widen narrowed columns for export.
338 When nil, narrowed columns will look in ASCII export just like in
339 Org mode, i.e. with \"=>\" as ellipsis."
340 :group 'org-export-ascii
341 :version "24.4"
342 :package-version '(Org . "8.0")
343 :type 'boolean)
345 (defcustom org-ascii-table-use-ascii-art nil
346 "Non-nil means table.el tables are turned into ascii-art.
348 It only makes sense when export charset is `utf-8'. It is nil by
349 default since it requires ascii-art-to-unicode.el package. You
350 can download it here:
352 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
353 :group 'org-export-ascii
354 :version "24.4"
355 :package-version '(Org . "8.0")
356 :type 'boolean)
358 (defcustom org-ascii-caption-above nil
359 "When non-nil, place caption string before the element.
360 Otherwise, place it right after it."
361 :group 'org-export-ascii
362 :version "24.4"
363 :package-version '(Org . "8.0")
364 :type 'boolean)
366 (defcustom org-ascii-verbatim-format "`%s'"
367 "Format string used for verbatim text and inline code."
368 :group 'org-export-ascii
369 :version "24.4"
370 :package-version '(Org . "8.0")
371 :type 'string)
373 (defcustom org-ascii-format-drawer-function
374 (lambda (name contents width) contents)
375 "Function called to format a drawer in ASCII.
377 The function must accept three parameters:
378 NAME the drawer name, like \"LOGBOOK\"
379 CONTENTS the contents of the drawer.
380 WIDTH the text width within the drawer.
382 The function should return either the string to be exported or
383 nil to ignore the drawer.
385 The default value simply returns the value of CONTENTS."
386 :group 'org-export-ascii
387 :version "24.4"
388 :package-version '(Org . "8.0")
389 :type 'function)
391 (defcustom org-ascii-format-inlinetask-function
392 'org-ascii-format-inlinetask-default
393 "Function called to format an inlinetask in ASCII.
395 The function must accept nine parameters:
396 TODO the todo keyword, as a string
397 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
398 PRIORITY the inlinetask priority, as a string
399 NAME the inlinetask name, as a string.
400 TAGS the inlinetask tags, as a list of strings.
401 CONTENTS the contents of the inlinetask, as a string.
402 WIDTH the width of the inlinetask, as a number.
403 INLINETASK the inlinetask itself.
404 INFO the info channel.
406 The function should return either the string to be exported or
407 nil to ignore the inline task."
408 :group 'org-export-ascii
409 :version "24.4"
410 :package-version '(Org . "8.3")
411 :type 'function)
415 ;;; Internal Functions
417 ;; Internal functions fall into three categories.
419 ;; The first one is about text formatting. The core functions are
420 ;; `org-ascii--current-text-width' and
421 ;; `org-ascii--current-justification', which determine, respectively,
422 ;; the current text width allowed to a given element and its expected
423 ;; justification. Once this information is known,
424 ;; `org-ascii--fill-string', `org-ascii--justify-lines',
425 ;; `org-ascii--justify-element' `org-ascii--box-string' and
426 ;; `org-ascii--indent-string' can operate on a given output string.
427 ;; In particular, justification happens at the regular (i.e.,
428 ;; non-greater) element level, which means that when the exporting
429 ;; process reaches a container (e.g., a center block) content are
430 ;; already justified.
432 ;; The second category contains functions handling elements listings,
433 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
434 ;; returns a complete table of contents, `org-ascii--list-listings'
435 ;; returns a list of referenceable src-block elements, and
436 ;; `org-ascii--list-tables' does the same for table elements.
438 ;; The third category includes general helper functions.
439 ;; `org-ascii--build-title' creates the title for a given headline
440 ;; or inlinetask element. `org-ascii--build-caption' returns the
441 ;; caption string associated to a table or a src-block.
442 ;; `org-ascii--describe-links' creates notes about links for
443 ;; insertion at the end of a section. It uses
444 ;; `org-ascii--unique-links' to get the list of links to describe.
445 ;; Eventually, `org-ascii--translate' translates a string according
446 ;; to language and charset specification.
449 (defun org-ascii--fill-string (s text-width info &optional justify)
450 "Fill a string with specified text-width and return it.
452 S is the string being filled. TEXT-WIDTH is an integer
453 specifying maximum length of a line. INFO is the plist used as
454 a communication channel.
456 Optional argument JUSTIFY can specify any type of justification
457 among `left', `center', `right' or `full'. A nil value is
458 equivalent to `left'. For a justification that doesn't also fill
459 string, see `org-ascii--justify-lines' and
460 `org-ascii--justify-block'.
462 Return nil if S isn't a string."
463 (when (stringp s)
464 (let ((double-space-p sentence-end-double-space))
465 (with-temp-buffer
466 (let ((fill-column text-width)
467 (use-hard-newlines t)
468 (sentence-end-double-space double-space-p))
469 (insert (if (plist-get info :preserve-breaks)
470 (replace-regexp-in-string "\n" hard-newline s)
472 (fill-region (point-min) (point-max) justify))
473 (buffer-string)))))
475 (defun org-ascii--justify-lines (s text-width how)
476 "Justify all lines in string S.
477 TEXT-WIDTH is an integer specifying maximum length of a line.
478 HOW determines the type of justification: it can be `left',
479 `right', `full' or `center'."
480 (with-temp-buffer
481 (insert s)
482 (goto-char (point-min))
483 (let ((fill-column text-width)
484 ;; Disable `adaptive-fill-mode' so it doesn't prevent
485 ;; filling lines matching `adaptive-fill-regexp'.
486 (adaptive-fill-mode nil))
487 (while (< (point) (point-max))
488 (justify-current-line how)
489 (forward-line)))
490 (buffer-string)))
492 (defun org-ascii--justify-element (contents element info)
493 "Justify CONTENTS of ELEMENT.
494 INFO is a plist used as a communication channel. Justification
495 is done according to the type of element. More accurately,
496 paragraphs are filled and other elements are justified as blocks,
497 that is according to the widest non blank line in CONTENTS."
498 (if (not (org-string-nw-p contents)) contents
499 (let ((text-width (org-ascii--current-text-width element info))
500 (how (org-ascii--current-justification element)))
501 (cond
502 ((eq (org-element-type element) 'paragraph)
503 ;; Paragraphs are treated specially as they need to be filled.
504 (org-ascii--fill-string contents text-width info how))
505 ((eq how 'left) contents)
506 (t (with-temp-buffer
507 (insert contents)
508 (goto-char (point-min))
509 (catch 'exit
510 (let ((max-width 0))
511 ;; Compute maximum width. Bail out if it is greater
512 ;; than page width, since no justification is
513 ;; possible.
514 (save-excursion
515 (while (not (eobp))
516 (unless (org-looking-at-p "[ \t]*$")
517 (end-of-line)
518 (let ((column (current-column)))
519 (cond
520 ((>= column text-width) (throw 'exit contents))
521 ((> column max-width) (setq max-width column)))))
522 (forward-line)))
523 ;; Justify every line according to TEXT-WIDTH and
524 ;; MAX-WIDTH.
525 (let ((offset (/ (- text-width max-width)
526 (if (eq how 'right) 1 2))))
527 (if (zerop offset) (throw 'exit contents)
528 (while (not (eobp))
529 (unless (org-looking-at-p "[ \t]*$")
530 (org-indent-to-column offset))
531 (forward-line)))))
532 (buffer-string))))))))
534 (defun org-ascii--indent-string (s width)
535 "Indent string S by WIDTH white spaces.
536 Empty lines are not indented."
537 (when (stringp s)
538 (replace-regexp-in-string
539 "\\(^\\)[ \t]*\\S-" (make-string width ?\s) s nil nil 1)))
541 (defun org-ascii--box-string (s info)
542 "Return string S with a partial box to its left.
543 INFO is a plist used as a communication channel."
544 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
545 (format (if utf8p "┌────\n%s\n└────" ",----\n%s\n`----")
546 (replace-regexp-in-string
547 "^" (if utf8p "│ " "| ")
548 ;; Remove last newline character.
549 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
551 (defun org-ascii--current-text-width (element info)
552 "Return maximum text width for ELEMENT's contents.
553 INFO is a plist used as a communication channel."
554 (case (org-element-type element)
555 ;; Elements with an absolute width: `headline' and `inlinetask'.
556 (inlinetask (plist-get info :ascii-inlinetask-width))
557 (headline
558 (- (plist-get info :ascii-text-width)
559 (let ((low-level-rank (org-export-low-level-p element info)))
560 (if low-level-rank (* low-level-rank 2)
561 (plist-get info :ascii-global-margin)))))
562 ;; Elements with a relative width: store maximum text width in
563 ;; TOTAL-WIDTH.
564 (otherwise
565 (let* ((genealogy (org-element-lineage element nil t))
566 ;; Total width is determined by the presence, or not, of an
567 ;; inline task among ELEMENT parents.
568 (total-width
569 (if (loop for parent in genealogy
570 thereis (eq (org-element-type parent) 'inlinetask))
571 (plist-get info :ascii-inlinetask-width)
572 ;; No inlinetask: Remove global margin from text width.
573 (- (plist-get info :ascii-text-width)
574 (plist-get info :ascii-global-margin)
575 (let ((parent (org-export-get-parent-headline element)))
576 ;; Inner margin doesn't apply to text before first
577 ;; headline.
578 (if (not parent) 0
579 (let ((low-level-rank
580 (org-export-low-level-p parent info)))
581 ;; Inner margin doesn't apply to contents of
582 ;; low level headlines, since they've got their
583 ;; own indentation mechanism.
584 (if low-level-rank (* low-level-rank 2)
585 (plist-get info :ascii-inner-margin)))))))))
586 (- total-width
587 ;; Each `quote-block' and `verse-block' above narrows text
588 ;; width by twice the standard margin size.
589 (+ (* (loop for parent in genealogy
590 when (memq (org-element-type parent)
591 '(quote-block verse-block))
592 count parent)
593 2 (plist-get info :ascii-quote-margin))
594 ;; Apply list margin once per "top-level" plain-list
595 ;; containing current line
596 (* (let ((count 0))
597 (dolist (e genealogy count)
598 (and (eq (org-element-type e) 'plain-list)
599 (not (eq (org-element-type (org-export-get-parent e))
600 'item))
601 (incf count))))
602 (plist-get info :ascii-list-margin))
603 ;; Text width within a plain-list is restricted by
604 ;; indentation of current item. If that's the case,
605 ;; compute it with the help of `:structure' property from
606 ;; parent item, if any.
607 (let ((item
608 (if (eq (org-element-type element) 'item) element
609 (loop for parent in genealogy
610 when (eq (org-element-type parent) 'item)
611 return parent))))
612 (if (not item) 0
613 ;; Compute indentation offset of the current item,
614 ;; that is the sum of the difference between its
615 ;; indentation and the indentation of the top item in
616 ;; the list and current item bullet's length. Also
617 ;; remove checkbox length, and tag length (for
618 ;; description lists) or bullet length.
619 (let ((struct (org-element-property :structure item))
620 (beg-item (org-element-property :begin item)))
621 (+ (- (org-list-get-ind beg-item struct)
622 (org-list-get-ind
623 (org-list-get-top-point struct) struct))
624 (string-width (or (org-ascii--checkbox item info)
625 ""))
626 (string-width
627 (let ((tag (org-element-property :tag item)))
628 (if tag (org-export-data tag info)
629 (org-element-property :bullet item))))))))))))))
631 (defun org-ascii--current-justification (element)
632 "Return expected justification for ELEMENT's contents.
633 Return value is a symbol among `left', `center', `right' and
634 `full'."
635 (let (justification)
636 (while (and (not justification)
637 (setq element (org-element-property :parent element)))
638 (case (org-element-type element)
639 (center-block (setq justification 'center))
640 (special-block
641 (let ((name (org-element-property :type element)))
642 (cond ((string= name "JUSTIFYRIGHT") (setq justification 'right))
643 ((string= name "JUSTIFYLEFT") (setq justification 'left)))))))
644 (or justification 'left)))
646 (defun org-ascii--build-title
647 (element info text-width &optional underline notags toc)
648 "Format ELEMENT title and return it.
650 ELEMENT is either an `headline' or `inlinetask' element. INFO is
651 a plist used as a communication channel. TEXT-WIDTH is an
652 integer representing the maximum length of a line.
654 When optional argument UNDERLINE is non-nil, underline title,
655 without the tags, according to `org-ascii-underline'
656 specifications.
658 If optional argument NOTAGS is non-nil, no tags will be added to
659 the title.
661 When optional argument TOC is non-nil, use optional title if
662 possible. It doesn't apply to `inlinetask' elements."
663 (let* ((headlinep (eq (org-element-type element) 'headline))
664 (numbers
665 ;; Numbering is specific to headlines.
666 (and headlinep (org-export-numbered-headline-p element info)
667 ;; All tests passed: build numbering string.
668 (concat
669 (mapconcat
670 'number-to-string
671 (org-export-get-headline-number element info) ".")
672 " ")))
673 (text
674 (org-trim
675 (org-export-data
676 (if (and toc headlinep) (org-export-get-alt-title element info)
677 (org-element-property :title element))
678 info)))
679 (todo
680 (and (plist-get info :with-todo-keywords)
681 (let ((todo (org-element-property :todo-keyword element)))
682 (and todo (concat (org-export-data todo info) " ")))))
683 (tags (and (not notags)
684 (plist-get info :with-tags)
685 (let ((tag-list (org-export-get-tags element info)))
686 (and tag-list
687 (format ":%s:"
688 (mapconcat 'identity tag-list ":"))))))
689 (priority
690 (and (plist-get info :with-priority)
691 (let ((char (org-element-property :priority element)))
692 (and char (format "(#%c) " char)))))
693 (first-part (concat numbers todo priority text)))
694 (concat
695 first-part
696 ;; Align tags, if any.
697 (when tags
698 (format
699 (format " %%%ds"
700 (max (- text-width (1+ (string-width first-part)))
701 (string-width tags)))
702 tags))
703 ;; Maybe underline text, if ELEMENT type is `headline' and an
704 ;; underline character has been defined.
705 (when (and underline headlinep)
706 (let ((under-char
707 (nth (1- (org-export-get-relative-level element info))
708 (cdr (assq (plist-get info :ascii-charset)
709 (plist-get info :ascii-underline))))))
710 (and under-char
711 (concat "\n"
712 (make-string (/ (string-width first-part)
713 (char-width under-char))
714 under-char))))))))
716 (defun org-ascii--has-caption-p (element info)
717 "Non-nil when ELEMENT has a caption affiliated keyword.
718 INFO is a plist used as a communication channel. This function
719 is meant to be used as a predicate for `org-export-get-ordinal'."
720 (org-element-property :caption element))
722 (defun org-ascii--build-caption (element info)
723 "Return caption string for ELEMENT, if applicable.
725 INFO is a plist used as a communication channel.
727 The caption string contains the sequence number of ELEMENT along
728 with its real caption. Return nil when ELEMENT has no affiliated
729 caption keyword."
730 (let ((caption (org-export-get-caption element)))
731 (when caption
732 ;; Get sequence number of current src-block among every
733 ;; src-block with a caption.
734 (let ((reference
735 (org-export-get-ordinal
736 element info nil 'org-ascii--has-caption-p))
737 (title-fmt (org-ascii--translate
738 (case (org-element-type element)
739 (table "Table %d:")
740 (src-block "Listing %d:"))
741 info)))
742 (org-ascii--fill-string
743 (concat (format title-fmt reference)
745 (org-export-data caption info))
746 (org-ascii--current-text-width element info) info)))))
748 (defun org-ascii--build-toc (info &optional n keyword local)
749 "Return a table of contents.
751 INFO is a plist used as a communication channel.
753 Optional argument N, when non-nil, is an integer specifying the
754 depth of the table.
756 Optional argument KEYWORD specifies the TOC keyword, if any, from
757 which the table of contents generation has been initiated.
759 When optional argument LOCAL is non-nil, build a table of
760 contents according to the current headline."
761 (concat
762 (unless local
763 (let ((title (org-ascii--translate "Table of Contents" info)))
764 (concat title "\n"
765 (make-string
766 (string-width title)
767 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
768 "\n\n")))
769 (let ((text-width
770 (if keyword (org-ascii--current-text-width keyword info)
771 (- (plist-get info :ascii-text-width)
772 (plist-get info :ascii-global-margin)))))
773 (mapconcat
774 (lambda (headline)
775 (let* ((level (org-export-get-relative-level headline info))
776 (indent (* (1- level) 3)))
777 (concat
778 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
779 (org-ascii--build-title
780 headline info (- text-width indent) nil
781 (or (not (plist-get info :with-tags))
782 (eq (plist-get info :with-tags) 'not-in-toc))
783 'toc))))
784 (org-export-collect-headlines info n keyword) "\n"))))
786 (defun org-ascii--list-listings (keyword info)
787 "Return a list of listings.
789 KEYWORD is the keyword that initiated the list of listings
790 generation. INFO is a plist used as a communication channel."
791 (let ((title (org-ascii--translate "List of Listings" info)))
792 (concat
793 title "\n"
794 (make-string (string-width title)
795 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
796 "\n\n"
797 (let ((text-width
798 (if keyword (org-ascii--current-text-width keyword info)
799 (- (plist-get info :ascii-text-width)
800 (plist-get info :ascii-global-margin))))
801 ;; Use a counter instead of retrieving ordinal of each
802 ;; src-block.
803 (count 0))
804 (mapconcat
805 (lambda (src-block)
806 ;; Store initial text so its length can be computed. This is
807 ;; used to properly align caption right to it in case of
808 ;; filling (like contents of a description list item).
809 (let* ((initial-text
810 (format (org-ascii--translate "Listing %d:" info)
811 (incf count)))
812 (initial-width (string-width initial-text)))
813 (concat
814 initial-text " "
815 (org-trim
816 (org-ascii--indent-string
817 (org-ascii--fill-string
818 ;; Use short name in priority, if available.
819 (let ((caption (or (org-export-get-caption src-block t)
820 (org-export-get-caption src-block))))
821 (org-export-data caption info))
822 (- text-width initial-width) info)
823 initial-width)))))
824 (org-export-collect-listings info) "\n")))))
826 (defun org-ascii--list-tables (keyword info)
827 "Return a list of tables.
829 KEYWORD is the keyword that initiated the list of tables
830 generation. INFO is a plist used as a communication channel."
831 (let ((title (org-ascii--translate "List of Tables" info)))
832 (concat
833 title "\n"
834 (make-string (string-width title)
835 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
836 "\n\n"
837 (let ((text-width
838 (if keyword (org-ascii--current-text-width keyword info)
839 (- (plist-get info :ascii-text-width)
840 (plist-get info :ascii-global-margin))))
841 ;; Use a counter instead of retrieving ordinal of each
842 ;; src-block.
843 (count 0))
844 (mapconcat
845 (lambda (table)
846 ;; Store initial text so its length can be computed. This is
847 ;; used to properly align caption right to it in case of
848 ;; filling (like contents of a description list item).
849 (let* ((initial-text
850 (format (org-ascii--translate "Table %d:" info)
851 (incf count)))
852 (initial-width (string-width initial-text)))
853 (concat
854 initial-text " "
855 (org-trim
856 (org-ascii--indent-string
857 (org-ascii--fill-string
858 ;; Use short name in priority, if available.
859 (let ((caption (or (org-export-get-caption table t)
860 (org-export-get-caption table))))
861 (org-export-data caption info))
862 (- text-width initial-width) info)
863 initial-width)))))
864 (org-export-collect-tables info) "\n")))))
866 (defun org-ascii--unique-links (element info)
867 "Return a list of unique link references in ELEMENT.
868 ELEMENT is either a headline element or a section element. INFO
869 is a plist used as a communication channel."
870 (let* (seen
871 (unique-link-p
872 (function
873 ;; Return LINK if it wasn't referenced so far, or nil.
874 ;; Update SEEN links along the way.
875 (lambda (link)
876 (let ((footprint
877 ;; Normalize description in footprints.
878 (cons (org-element-property :raw-link link)
879 (let ((contents (org-element-contents link)))
880 (and contents
881 (replace-regexp-in-string
882 "[ \r\t\n]+" " "
883 (org-trim
884 (org-element-interpret-data contents))))))))
885 ;; Ignore LINK if it hasn't been translated already.
886 ;; It can happen if it is located in an affiliated
887 ;; keyword that was ignored.
888 (when (and (org-string-nw-p
889 (gethash link (plist-get info :exported-data)))
890 (not (member footprint seen)))
891 (push footprint seen) link)))))
892 ;; If at a section, find parent headline, if any, in order to
893 ;; count links that might be in the title.
894 (headline
895 (if (eq (org-element-type element) 'headline) element
896 (or (org-export-get-parent-headline element) element))))
897 ;; Get all links in HEADLINE.
898 (org-element-map headline 'link
899 (lambda (l) (funcall unique-link-p l)) info nil nil t)))
901 (defun org-ascii--describe-links (links width info)
902 "Return a string describing a list of links.
904 LINKS is a list of link type objects, as returned by
905 `org-ascii--unique-links'. WIDTH is the text width allowed for
906 the output string. INFO is a plist used as a communication
907 channel."
908 (mapconcat
909 (lambda (link)
910 (let ((type (org-element-property :type link))
911 (anchor (let ((desc (org-element-contents link)))
912 (if desc (org-export-data desc info)
913 (org-element-property :raw-link link)))))
914 (cond
915 ;; Coderefs, radio links and fuzzy links are ignored.
916 ((member type '("coderef" "radio" "fuzzy")) nil)
917 ;; Id and custom-id links: Headlines refer to their numbering.
918 ((member type '("custom-id" "id"))
919 (let ((dest (org-export-resolve-id-link link info)))
920 (concat
921 (org-ascii--fill-string
922 (format
923 "[%s] %s"
924 anchor
925 (if (not dest) (org-ascii--translate "Unknown reference" info)
926 (format
927 (org-ascii--translate "See section %s" info)
928 (if (org-export-numbered-headline-p dest info)
929 (mapconcat #'number-to-string
930 (org-export-get-headline-number dest info) ".")
931 (org-export-data (org-element-property :title dest) info)))))
932 width info) "\n\n")))
933 ;; Do not add a link that cannot be resolved and doesn't have
934 ;; any description: destination is already visible in the
935 ;; paragraph.
936 ((not (org-element-contents link)) nil)
937 ;; Do not add a link already handled by custom export
938 ;; functions.
939 ((let ((protocol (nth 2 (assoc type org-link-protocols)))
940 (path (org-element-property :path link)))
941 (and (functionp protocol)
942 (funcall protocol (org-link-unescape path) anchor 'ascii)))
943 nil)
945 (concat
946 (org-ascii--fill-string
947 (format "[%s] %s" anchor (org-element-property :raw-link link))
948 width info)
949 "\n\n")))))
950 links ""))
952 (defun org-ascii--checkbox (item info)
953 "Return checkbox string for ITEM or nil.
954 INFO is a plist used as a communication channel."
955 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
956 (case (org-element-property :checkbox item)
957 (on (if utf8p "☑ " "[X] "))
958 (off (if utf8p "☐ " "[ ] "))
959 (trans (if utf8p "☒ " "[-] ")))))
963 ;;; Template
965 (defun org-ascii-template--document-title (info)
966 "Return document title, as a string.
967 INFO is a plist used as a communication channel."
968 (let* ((text-width (plist-get info :ascii-text-width))
969 ;; Links in the title will not be resolved later, so we make
970 ;; sure their path is located right after them.
971 (info (org-combine-plists info '(:ascii-links-to-notes nil)))
972 (title (if (plist-get info :with-title)
973 (org-export-data (plist-get info :title) info)
974 ""))
975 (author (and (plist-get info :with-author)
976 (let ((auth (plist-get info :author)))
977 (and auth (org-export-data auth info)))))
978 (email (and (plist-get info :with-email)
979 (org-export-data (plist-get info :email) info)))
980 (date (and (plist-get info :with-date)
981 (org-export-data (org-export-get-date info) info))))
982 ;; There are two types of title blocks depending on the presence
983 ;; of a title to display.
984 (if (string= title "")
985 ;; Title block without a title. DATE is positioned at the top
986 ;; right of the document, AUTHOR to the top left and EMAIL
987 ;; just below.
988 (cond
989 ((and (org-string-nw-p date) (org-string-nw-p author))
990 (concat
991 author
992 (make-string (- text-width (string-width date) (string-width author))
993 ?\s)
994 date
995 (when (org-string-nw-p email) (concat "\n" email))
996 "\n\n\n"))
997 ((and (org-string-nw-p date) (org-string-nw-p email))
998 (concat
999 email
1000 (make-string (- text-width (string-width date) (string-width email))
1001 ?\s)
1002 date "\n\n\n"))
1003 ((org-string-nw-p date)
1004 (concat
1005 (org-ascii--justify-lines date text-width 'right)
1006 "\n\n\n"))
1007 ((and (org-string-nw-p author) (org-string-nw-p email))
1008 (concat author "\n" email "\n\n\n"))
1009 ((org-string-nw-p author) (concat author "\n\n\n"))
1010 ((org-string-nw-p email) (concat email "\n\n\n")))
1011 ;; Title block with a title. Document's TITLE, along with the
1012 ;; AUTHOR and its EMAIL are both overlined and an underlined,
1013 ;; centered. Date is just below, also centered.
1014 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1015 ;; Format TITLE. It may be filled if it is too wide,
1016 ;; that is wider than the two thirds of the total width.
1017 (title-len (min (length title) (/ (* 2 text-width) 3)))
1018 (formatted-title (org-ascii--fill-string title title-len info))
1019 (line
1020 (make-string
1021 (min (+ (max title-len
1022 (string-width (or author ""))
1023 (string-width (or email "")))
1025 text-width) (if utf8p ?━ ?_))))
1026 (org-ascii--justify-lines
1027 (concat line "\n"
1028 (unless utf8p "\n")
1029 (upcase formatted-title)
1030 (cond
1031 ((and (org-string-nw-p author) (org-string-nw-p email))
1032 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
1033 ((org-string-nw-p author)
1034 (concat (if utf8p "\n\n\n" "\n\n") author))
1035 ((org-string-nw-p email)
1036 (concat (if utf8p "\n\n\n" "\n\n") email)))
1037 "\n" line
1038 (when (org-string-nw-p date) (concat "\n\n\n" date))
1039 "\n\n\n") text-width 'center)))))
1041 (defun org-ascii-inner-template (contents info)
1042 "Return complete document string after ASCII conversion.
1043 CONTENTS is the transcoded contents string. INFO is a plist
1044 holding export options."
1045 (org-element-normalize-string
1046 (let ((global-margin (plist-get info :ascii-global-margin)))
1047 (org-ascii--indent-string
1048 (concat
1049 ;; 1. Document's body.
1050 contents
1051 ;; 2. Footnote definitions.
1052 (let ((definitions (org-export-collect-footnote-definitions
1053 (plist-get info :parse-tree) info))
1054 ;; Insert full links right inside the footnote definition
1055 ;; as they have no chance to be inserted later.
1056 (info (org-combine-plists info '(:ascii-links-to-notes nil))))
1057 (when definitions
1058 (concat
1059 "\n\n\n"
1060 (let ((title (org-ascii--translate "Footnotes" info)))
1061 (concat
1062 title "\n"
1063 (make-string
1064 (string-width title)
1065 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
1066 "\n\n"
1067 (let ((text-width (- (plist-get info :ascii-text-width)
1068 global-margin)))
1069 (mapconcat
1070 (lambda (ref)
1071 (let ((id (format "[%s] " (car ref))))
1072 ;; Distinguish between inline definitions and
1073 ;; full-fledged definitions.
1074 (org-trim
1075 (let ((def (nth 2 ref)))
1076 (if (eq (org-element-type def) 'org-data)
1077 ;; Full-fledged definition: footnote ID is
1078 ;; inserted inside the first parsed
1079 ;; paragraph (FIRST), if any, to be sure
1080 ;; filling will take it into consideration.
1081 (let ((first (car (org-element-contents def))))
1082 (if (not (eq (org-element-type first) 'paragraph))
1083 (concat id "\n" (org-export-data def info))
1084 (push id (nthcdr 2 first))
1085 (org-export-data def info)))
1086 ;; Fill paragraph once footnote ID is inserted
1087 ;; in order to have a correct length for first
1088 ;; line.
1089 (org-ascii--fill-string
1090 (concat id (org-export-data def info))
1091 text-width info))))))
1092 definitions "\n\n"))))))
1093 global-margin))))
1095 (defun org-ascii-template (contents info)
1096 "Return complete document string after ASCII conversion.
1097 CONTENTS is the transcoded contents string. INFO is a plist
1098 holding export options."
1099 (let ((global-margin (plist-get info :ascii-global-margin)))
1100 (concat
1101 ;; 1. Build title block.
1102 (org-ascii--indent-string
1103 (concat (org-ascii-template--document-title info)
1104 ;; 2. Table of contents.
1105 (let ((depth (plist-get info :with-toc)))
1106 (when depth
1107 (concat
1108 (org-ascii--build-toc info (and (wholenump depth) depth))
1109 "\n\n\n"))))
1110 global-margin)
1111 ;; 3. Document's body.
1112 contents
1113 ;; 4. Creator. Ignore `comment' value as there are no comments in
1114 ;; ASCII. Justify it to the bottom right.
1115 (org-ascii--indent-string
1116 (let ((creator-info (plist-get info :with-creator))
1117 (text-width (- (plist-get info :ascii-text-width) global-margin)))
1118 (unless (or (not creator-info) (eq creator-info 'comment))
1119 (concat
1120 "\n\n\n"
1121 (org-ascii--fill-string
1122 (plist-get info :creator) text-width info 'right))))
1123 global-margin))))
1125 (defun org-ascii--translate (s info)
1126 "Translate string S according to specified language and charset.
1127 INFO is a plist used as a communication channel."
1128 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1129 (org-export-translate s charset info)))
1133 ;;; Transcode Functions
1135 ;;;; Bold
1137 (defun org-ascii-bold (bold contents info)
1138 "Transcode BOLD from Org to ASCII.
1139 CONTENTS is the text with bold markup. INFO is a plist holding
1140 contextual information."
1141 (format "*%s*" contents))
1144 ;;;; Center Block
1146 (defun org-ascii-center-block (center-block contents info)
1147 "Transcode a CENTER-BLOCK element from Org to ASCII.
1148 CONTENTS holds the contents of the block. INFO is a plist
1149 holding contextual information."
1150 ;; Center has already been taken care of at a lower level, so
1151 ;; there's nothing left to do.
1152 contents)
1155 ;;;; Clock
1157 (defun org-ascii-clock (clock contents info)
1158 "Transcode a CLOCK object from Org to ASCII.
1159 CONTENTS is nil. INFO is a plist holding contextual
1160 information."
1161 (org-ascii--justify-element
1162 (concat org-clock-string " "
1163 (org-timestamp-translate (org-element-property :value clock))
1164 (let ((time (org-element-property :duration clock)))
1165 (and time
1166 (concat " => "
1167 (apply 'format
1168 "%2s:%02s"
1169 (org-split-string time ":"))))))
1170 clock info))
1173 ;;;; Code
1175 (defun org-ascii-code (code contents info)
1176 "Return a CODE object from Org to ASCII.
1177 CONTENTS is nil. INFO is a plist holding contextual
1178 information."
1179 (format (plist-get info :ascii-verbatim-format)
1180 (org-element-property :value code)))
1183 ;;;; Drawer
1185 (defun org-ascii-drawer (drawer contents info)
1186 "Transcode a DRAWER element from Org to ASCII.
1187 CONTENTS holds the contents of the block. INFO is a plist
1188 holding contextual information."
1189 (let ((name (org-element-property :drawer-name drawer))
1190 (width (org-ascii--current-text-width drawer info)))
1191 (funcall (plist-get info :ascii-format-drawer-function)
1192 name contents width)))
1195 ;;;; Dynamic Block
1197 (defun org-ascii-dynamic-block (dynamic-block contents info)
1198 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1199 CONTENTS holds the contents of the block. INFO is a plist
1200 holding contextual information."
1201 contents)
1204 ;;;; Entity
1206 (defun org-ascii-entity (entity contents info)
1207 "Transcode an ENTITY object from Org to ASCII.
1208 CONTENTS are the definition itself. INFO is a plist holding
1209 contextual information."
1210 (org-element-property
1211 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1212 entity))
1215 ;;;; Example Block
1217 (defun org-ascii-example-block (example-block contents info)
1218 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1219 CONTENTS is nil. INFO is a plist holding contextual information."
1220 (org-ascii--justify-element
1221 (org-ascii--box-string
1222 (org-export-format-code-default example-block info) info)
1223 example-block info))
1226 ;;;; Export Snippet
1228 (defun org-ascii-export-snippet (export-snippet contents info)
1229 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1230 CONTENTS is nil. INFO is a plist holding contextual information."
1231 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1232 (org-element-property :value export-snippet)))
1235 ;;;; Export Block
1237 (defun org-ascii-export-block (export-block contents info)
1238 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1239 CONTENTS is nil. INFO is a plist holding contextual information."
1240 (when (string= (org-element-property :type export-block) "ASCII")
1241 (org-ascii--justify-element
1242 (org-element-property :value export-block) export-block info)))
1245 ;;;; Fixed Width
1247 (defun org-ascii-fixed-width (fixed-width contents info)
1248 "Transcode a FIXED-WIDTH element from Org to ASCII.
1249 CONTENTS is nil. INFO is a plist holding contextual information."
1250 (org-ascii--justify-element
1251 (org-ascii--box-string
1252 (org-remove-indentation
1253 (org-element-property :value fixed-width)) info)
1254 fixed-width info))
1257 ;;;; Footnote Definition
1259 ;; Footnote Definitions are ignored. They are compiled at the end of
1260 ;; the document, by `org-ascii-inner-template'.
1263 ;;;; Footnote Reference
1265 (defun org-ascii-footnote-reference (footnote-reference contents info)
1266 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1267 CONTENTS is nil. INFO is a plist holding contextual information."
1268 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1271 ;;;; Headline
1273 (defun org-ascii-headline (headline contents info)
1274 "Transcode a HEADLINE element from Org to ASCII.
1275 CONTENTS holds the contents of the headline. INFO is a plist
1276 holding contextual information."
1277 ;; Don't export footnote section, which will be handled at the end
1278 ;; of the template.
1279 (unless (org-element-property :footnote-section-p headline)
1280 (let* ((low-level-rank (org-export-low-level-p headline info))
1281 (width (org-ascii--current-text-width headline info))
1282 ;; Blank lines between headline and its contents.
1283 ;; `org-ascii-headline-spacing', when set, overwrites
1284 ;; original buffer's spacing.
1285 (pre-blanks
1286 (make-string
1287 (or (car (plist-get info :ascii-headline-spacing))
1288 (org-element-property :pre-blank headline))
1289 ?\n))
1290 ;; Even if HEADLINE has no section, there might be some
1291 ;; links in its title that we shouldn't forget to describe.
1292 (links
1293 (unless (or (eq (caar (org-element-contents headline)) 'section))
1294 (let ((title (org-element-property :title headline)))
1295 (when (consp title)
1296 (org-ascii--describe-links
1297 (org-ascii--unique-links title info) width info))))))
1298 ;; Deep subtree: export it as a list item.
1299 (if low-level-rank
1300 (concat
1301 ;; Bullet.
1302 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1303 (plist-get info :ascii-bullets)))))
1304 (char-to-string
1305 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1307 ;; Title.
1308 (org-ascii--build-title headline info width) "\n"
1309 ;; Contents, indented by length of bullet.
1310 pre-blanks
1311 (org-ascii--indent-string
1312 (concat contents
1313 (when (org-string-nw-p links) (concat "\n\n" links)))
1315 ;; Else: Standard headline.
1316 (concat
1317 (org-ascii--build-title headline info width 'underline)
1318 "\n" pre-blanks
1319 (concat (when (org-string-nw-p links) links) contents))))))
1322 ;;;; Horizontal Rule
1324 (defun org-ascii-horizontal-rule (horizontal-rule contents info)
1325 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1326 CONTENTS is nil. INFO is a plist holding contextual
1327 information."
1328 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1329 (spec-width
1330 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1331 (org-ascii--justify-lines
1332 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1333 (string-to-number spec-width)
1334 text-width)
1335 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1336 text-width 'center)))
1339 ;;;; Inline Src Block
1341 (defun org-ascii-inline-src-block (inline-src-block contents info)
1342 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1343 CONTENTS holds the contents of the item. INFO is a plist holding
1344 contextual information."
1345 (format (plist-get info :ascii-verbatim-format)
1346 (org-element-property :value inline-src-block)))
1349 ;;;; Inlinetask
1351 (defun org-ascii-format-inlinetask-default
1352 (todo type priority name tags contents width inlinetask info)
1353 "Format an inline task element for ASCII export.
1354 See `org-ascii-format-inlinetask-function' for a description
1355 of the parameters."
1356 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1357 (width (or width (plist-get info :ascii-inlinetask-width))))
1358 (org-ascii--indent-string
1359 (concat
1360 ;; Top line, with an additional blank line if not in UTF-8.
1361 (make-string width (if utf8p ?━ ?_)) "\n"
1362 (unless utf8p (concat (make-string width ? ) "\n"))
1363 ;; Add title. Fill it if wider than inlinetask.
1364 (let ((title (org-ascii--build-title inlinetask info width)))
1365 (if (<= (string-width title) width) title
1366 (org-ascii--fill-string title width info)))
1367 "\n"
1368 ;; If CONTENTS is not empty, insert it along with
1369 ;; a separator.
1370 (when (org-string-nw-p contents)
1371 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1372 ;; Bottom line.
1373 (make-string width (if utf8p ?━ ?_)))
1374 ;; Flush the inlinetask to the right.
1375 (- (plist-get info :ascii-text-width) (plist-get info :ascii-global-margin)
1376 (if (not (org-export-get-parent-headline inlinetask)) 0
1377 (plist-get info :ascii-inner-margin))
1378 (org-ascii--current-text-width inlinetask info)))))
1380 (defun org-ascii-inlinetask (inlinetask contents info)
1381 "Transcode an INLINETASK element from Org to ASCII.
1382 CONTENTS holds the contents of the block. INFO is a plist
1383 holding contextual information."
1384 (let ((width (org-ascii--current-text-width inlinetask info)))
1385 (funcall (plist-get info :ascii-format-inlinetask-function)
1386 ;; todo.
1387 (and (plist-get info :with-todo-keywords)
1388 (let ((todo (org-element-property
1389 :todo-keyword inlinetask)))
1390 (and todo (org-export-data todo info))))
1391 ;; todo-type
1392 (org-element-property :todo-type inlinetask)
1393 ;; priority
1394 (and (plist-get info :with-priority)
1395 (org-element-property :priority inlinetask))
1396 ;; title
1397 (org-export-data (org-element-property :title inlinetask) info)
1398 ;; tags
1399 (and (plist-get info :with-tags)
1400 (org-element-property :tags inlinetask))
1401 ;; contents and width
1402 contents width inlinetask info)))
1405 ;;;; Italic
1407 (defun org-ascii-italic (italic contents info)
1408 "Transcode italic from Org to ASCII.
1409 CONTENTS is the text with italic markup. INFO is a plist holding
1410 contextual information."
1411 (format "/%s/" contents))
1414 ;;;; Item
1416 (defun org-ascii-item (item contents info)
1417 "Transcode an ITEM element from Org to ASCII.
1418 CONTENTS holds the contents of the item. INFO is a plist holding
1419 contextual information."
1420 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1421 (checkbox (org-ascii--checkbox item info))
1422 (list-type (org-element-property :type (org-export-get-parent item)))
1423 (bullet
1424 ;; First parent of ITEM is always the plain-list. Get
1425 ;; `:type' property from it.
1426 (org-list-bullet-string
1427 (case list-type
1428 (descriptive
1429 (concat checkbox
1430 (org-export-data (org-element-property :tag item) info)
1431 ": "))
1432 (ordered
1433 ;; Return correct number for ITEM, paying attention to
1434 ;; counters.
1435 (let* ((struct (org-element-property :structure item))
1436 (bul (org-element-property :bullet item))
1437 (num (number-to-string
1438 (car (last (org-list-get-item-number
1439 (org-element-property :begin item)
1440 struct
1441 (org-list-prevs-alist struct)
1442 (org-list-parents-alist struct)))))))
1443 (replace-regexp-in-string "[0-9]+" num bul)))
1444 (t (let ((bul (org-element-property :bullet item)))
1445 ;; Change bullets into more visible form if UTF-8 is active.
1446 (if (not utf8p) bul
1447 (replace-regexp-in-string
1448 "-" "•"
1449 (replace-regexp-in-string
1450 "+" "⁃"
1451 (replace-regexp-in-string "*" "‣" bul))))))))))
1452 (concat
1453 bullet
1454 (unless (eq list-type 'descriptive) checkbox)
1455 ;; Contents: Pay attention to indentation. Note: check-boxes are
1456 ;; already taken care of at the paragraph level so they don't
1457 ;; interfere with indentation.
1458 (let ((contents (org-ascii--indent-string contents (string-width bullet))))
1459 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1460 (org-trim contents)
1461 (concat "\n" contents))))))
1464 ;;;; Keyword
1466 (defun org-ascii-keyword (keyword contents info)
1467 "Transcode a KEYWORD element from Org to ASCII.
1468 CONTENTS is nil. INFO is a plist holding contextual
1469 information."
1470 (let ((key (org-element-property :key keyword))
1471 (value (org-element-property :value keyword)))
1472 (cond
1473 ((string= key "ASCII") (org-ascii--justify-element value keyword info))
1474 ((string= key "TOC")
1475 (org-ascii--justify-element
1476 (let ((case-fold-search t))
1477 (cond
1478 ((org-string-match-p "\\<headlines\\>" value)
1479 (let ((depth (and (string-match "\\<[0-9]+\\>" value)
1480 (string-to-number (match-string 0 value))))
1481 (localp (org-string-match-p "\\<local\\>" value)))
1482 (org-ascii--build-toc info depth keyword localp)))
1483 ((org-string-match-p "\\<tables\\>" value)
1484 (org-ascii--list-tables keyword info))
1485 ((org-string-match-p "\\<listings\\>" value)
1486 (org-ascii--list-listings keyword info))))
1487 keyword info)))))
1490 ;;;; Latex Environment
1492 (defun org-ascii-latex-environment (latex-environment contents info)
1493 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1494 CONTENTS is nil. INFO is a plist holding contextual
1495 information."
1496 (when (plist-get info :with-latex)
1497 (org-ascii--justify-element
1498 (org-remove-indentation (org-element-property :value latex-environment))
1499 latex-environment info)))
1502 ;;;; Latex Fragment
1504 (defun org-ascii-latex-fragment (latex-fragment contents info)
1505 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1506 CONTENTS is nil. INFO is a plist holding contextual
1507 information."
1508 (when (plist-get info :with-latex)
1509 (org-element-property :value latex-fragment)))
1512 ;;;; Line Break
1514 (defun org-ascii-line-break (line-break contents info)
1515 "Transcode a LINE-BREAK object from Org to ASCII.
1516 CONTENTS is nil. INFO is a plist holding contextual
1517 information." hard-newline)
1520 ;;;; Link
1522 (defun org-ascii-link (link desc info)
1523 "Transcode a LINK object from Org to ASCII.
1525 DESC is the description part of the link, or the empty string.
1526 INFO is a plist holding contextual information."
1527 (let ((type (org-element-property :type link)))
1528 (cond
1529 ((org-export-custom-protocol-maybe link desc info))
1530 ((string= type "coderef")
1531 (let ((ref (org-element-property :path link)))
1532 (format (org-export-get-coderef-format ref desc)
1533 (org-export-resolve-coderef ref info))))
1534 ;; Do not apply a special syntax on radio links. Though, use
1535 ;; transcoded target's contents as output.
1536 ((string= type "radio") desc)
1537 ;; Do not apply a special syntax on fuzzy links pointing to
1538 ;; targets.
1539 ((string= type "fuzzy")
1540 (let ((destination (org-export-resolve-fuzzy-link link info)))
1541 (if (org-string-nw-p desc) desc
1542 (when destination
1543 (let ((number
1544 (org-export-get-ordinal
1545 destination info nil 'org-ascii--has-caption-p)))
1546 (if number
1547 (if (atom number) (number-to-string number)
1548 (mapconcat #'number-to-string number "."))
1549 ;; Unnumbered headline.
1550 (when (eq 'headline (org-element-type destination))
1551 (format "[%s]"
1552 (org-export-data
1553 (org-element-property :title destination)
1554 info)))))))))
1556 (let ((raw-link (org-element-property :raw-link link)))
1557 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1558 (concat (format "[%s]" desc)
1559 (and (not (plist-get info :ascii-links-to-notes))
1560 (format " (%s)" raw-link)))))))))
1563 ;;;; Node Properties
1565 (defun org-ascii-node-property (node-property contents info)
1566 "Transcode a NODE-PROPERTY element from Org to ASCII.
1567 CONTENTS is nil. INFO is a plist holding contextual
1568 information."
1569 (format "%s:%s"
1570 (org-element-property :key node-property)
1571 (let ((value (org-element-property :value node-property)))
1572 (if value (concat " " value) ""))))
1575 ;;;; Paragraph
1577 (defun org-ascii-paragraph (paragraph contents info)
1578 "Transcode a PARAGRAPH element from Org to ASCII.
1579 CONTENTS is the contents of the paragraph, as a string. INFO is
1580 the plist used as a communication channel."
1581 (org-ascii--justify-element
1582 (let ((indented-line-width (plist-get info :ascii-indented-line-width)))
1583 (if (not (wholenump indented-line-width)) contents
1584 (concat
1585 ;; Do not indent first paragraph in a section.
1586 (unless (and (not (org-export-get-previous-element paragraph info))
1587 (eq (org-element-type (org-export-get-parent paragraph))
1588 'section))
1589 (make-string indented-line-width ?\s))
1590 (replace-regexp-in-string "\\`[ \t]+" "" contents))))
1591 paragraph info))
1594 ;;;; Plain List
1596 (defun org-ascii-plain-list (plain-list contents info)
1597 "Transcode a PLAIN-LIST element from Org to ASCII.
1598 CONTENTS is the contents of the list. INFO is a plist holding
1599 contextual information."
1600 (let ((margin (plist-get info :ascii-list-margin)))
1601 (if (or (< margin 1)
1602 (eq (org-element-type (org-export-get-parent plain-list)) 'item))
1603 contents
1604 (org-ascii--indent-string contents margin))))
1607 ;;;; Plain Text
1609 (defun org-ascii-plain-text (text info)
1610 "Transcode a TEXT string from Org to ASCII.
1611 INFO is a plist used as a communication channel."
1612 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1613 (when (and utf8p (plist-get info :with-smart-quotes))
1614 (setq text (org-export-activate-smart-quotes text :utf-8 info)))
1615 (if (not (plist-get info :with-special-strings)) text
1616 (setq text (replace-regexp-in-string "\\\\-" "" text))
1617 (if (not utf8p) text
1618 ;; Usual replacements in utf-8 with proper option set.
1619 (replace-regexp-in-string
1620 "\\.\\.\\." "…"
1621 (replace-regexp-in-string
1622 "--" "–"
1623 (replace-regexp-in-string "---" "—" text)))))))
1626 ;;;; Planning
1628 (defun org-ascii-planning (planning contents info)
1629 "Transcode a PLANNING element from Org to ASCII.
1630 CONTENTS is nil. INFO is a plist used as a communication
1631 channel."
1632 (org-ascii--justify-element
1633 (mapconcat
1634 #'identity
1635 (delq nil
1636 (list (let ((closed (org-element-property :closed planning)))
1637 (when closed
1638 (concat org-closed-string " "
1639 (org-timestamp-translate closed))))
1640 (let ((deadline (org-element-property :deadline planning)))
1641 (when deadline
1642 (concat org-deadline-string " "
1643 (org-timestamp-translate deadline))))
1644 (let ((scheduled (org-element-property :scheduled planning)))
1645 (when scheduled
1646 (concat org-scheduled-string " "
1647 (org-timestamp-translate scheduled))))))
1648 " ")
1649 planning info))
1652 ;;;; Property Drawer
1654 (defun org-ascii-property-drawer (property-drawer contents info)
1655 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1656 CONTENTS holds the contents of the drawer. INFO is a plist
1657 holding contextual information."
1658 (and (org-string-nw-p contents)
1659 (org-ascii--justify-element contents property-drawer info)))
1662 ;;;; Quote Block
1664 (defun org-ascii-quote-block (quote-block contents info)
1665 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1666 CONTENTS holds the contents of the block. INFO is a plist
1667 holding contextual information."
1668 (org-ascii--indent-string contents (plist-get info :ascii-quote-margin)))
1671 ;;;; Radio Target
1673 (defun org-ascii-radio-target (radio-target contents info)
1674 "Transcode a RADIO-TARGET object from Org to ASCII.
1675 CONTENTS is the contents of the target. INFO is a plist holding
1676 contextual information."
1677 contents)
1680 ;;;; Section
1682 (defun org-ascii-section (section contents info)
1683 "Transcode a SECTION element from Org to ASCII.
1684 CONTENTS is the contents of the section. INFO is a plist holding
1685 contextual information."
1686 (org-ascii--indent-string
1687 (concat
1688 contents
1689 (when (plist-get info :ascii-links-to-notes)
1690 ;; Add list of links at the end of SECTION.
1691 (let ((links (org-ascii--describe-links
1692 (org-ascii--unique-links section info)
1693 (org-ascii--current-text-width section info) info)))
1694 ;; Separate list of links and section contents.
1695 (when (org-string-nw-p links) (concat "\n\n" links)))))
1696 ;; Do not apply inner margin if parent headline is low level.
1697 (let ((headline (org-export-get-parent-headline section)))
1698 (if (or (not headline) (org-export-low-level-p headline info)) 0
1699 (plist-get info :ascii-inner-margin)))))
1702 ;;;; Special Block
1704 (defun org-ascii-special-block (special-block contents info)
1705 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1706 CONTENTS holds the contents of the block. INFO is a plist
1707 holding contextual information."
1708 ;; "JUSTIFYLEFT" and "JUSTFYRIGHT" have already been taken care of
1709 ;; at a lower level. There is no other special block type to
1710 ;; handle.
1711 contents)
1714 ;;;; Src Block
1716 (defun org-ascii-src-block (src-block contents info)
1717 "Transcode a SRC-BLOCK element from Org to ASCII.
1718 CONTENTS holds the contents of the item. INFO is a plist holding
1719 contextual information."
1720 (let ((caption (org-ascii--build-caption src-block info))
1721 (caption-above-p (plist-get info :ascii-caption-above))
1722 (code (org-export-format-code-default src-block info)))
1723 (if (equal code "") ""
1724 (org-ascii--justify-element
1725 (concat
1726 (and caption caption-above-p (concat caption "\n"))
1727 (org-ascii--box-string code info)
1728 (and caption (not caption-above-p) (concat "\n" caption)))
1729 src-block info))))
1732 ;;;; Statistics Cookie
1734 (defun org-ascii-statistics-cookie (statistics-cookie contents info)
1735 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1736 CONTENTS is nil. INFO is a plist holding contextual information."
1737 (org-element-property :value statistics-cookie))
1740 ;;;; Subscript
1742 (defun org-ascii-subscript (subscript contents info)
1743 "Transcode a SUBSCRIPT object from Org to ASCII.
1744 CONTENTS is the contents of the object. INFO is a plist holding
1745 contextual information."
1746 (if (org-element-property :use-brackets-p subscript)
1747 (format "_{%s}" contents)
1748 (format "_%s" contents)))
1751 ;;;; Superscript
1753 (defun org-ascii-superscript (superscript contents info)
1754 "Transcode a SUPERSCRIPT object from Org to ASCII.
1755 CONTENTS is the contents of the object. INFO is a plist holding
1756 contextual information."
1757 (if (org-element-property :use-brackets-p superscript)
1758 (format "^{%s}" contents)
1759 (format "^%s" contents)))
1762 ;;;; Strike-through
1764 (defun org-ascii-strike-through (strike-through contents info)
1765 "Transcode STRIKE-THROUGH from Org to ASCII.
1766 CONTENTS is text with strike-through markup. INFO is a plist
1767 holding contextual information."
1768 (format "+%s+" contents))
1771 ;;;; Table
1773 (defun org-ascii-table (table contents info)
1774 "Transcode a TABLE element from Org to ASCII.
1775 CONTENTS is the contents of the table. INFO is a plist holding
1776 contextual information."
1777 (let ((caption (org-ascii--build-caption table info))
1778 (caption-above-p (plist-get info :ascii-caption-above)))
1779 (org-ascii--justify-element
1780 (concat
1781 ;; Possibly add a caption string above.
1782 (and caption caption-above-p (concat caption "\n"))
1783 ;; Insert table. Note: "table.el" tables are left unmodified.
1784 (cond ((eq (org-element-property :type table) 'org) contents)
1785 ((and (plist-get info :ascii-table-use-ascii-art)
1786 (eq (plist-get info :ascii-charset) 'utf-8)
1787 (require 'ascii-art-to-unicode nil t))
1788 (with-temp-buffer
1789 (insert (org-remove-indentation
1790 (org-element-property :value table)))
1791 (goto-char (point-min))
1792 (aa2u)
1793 (goto-char (point-max))
1794 (skip-chars-backward " \r\t\n")
1795 (buffer-substring (point-min) (point))))
1796 (t (org-remove-indentation (org-element-property :value table))))
1797 ;; Possible add a caption string below.
1798 (and (not caption-above-p) caption))
1799 table info)))
1802 ;;;; Table Cell
1804 (defun org-ascii--table-cell-width (table-cell info)
1805 "Return width of TABLE-CELL.
1807 INFO is a plist used as a communication channel.
1809 Width of a cell is determined either by a width cookie in the
1810 same column as the cell, or by the maximum cell's length in that
1811 column.
1813 When `org-ascii-table-widen-columns' is non-nil, width cookies
1814 are ignored."
1815 (let* ((row (org-export-get-parent table-cell))
1816 (table (org-export-get-parent row))
1817 (col (let ((cells (org-element-contents row)))
1818 (- (length cells) (length (memq table-cell cells)))))
1819 (cache
1820 (or (plist-get info :ascii-table-cell-width-cache)
1821 (plist-get (setq info
1822 (plist-put info :ascii-table-cell-width-cache
1823 (make-hash-table :test 'equal)))
1824 :ascii-table-cell-width-cache)))
1825 (key (cons table col))
1826 (widenp (plist-get info :ascii-table-widen-columns)))
1827 (or (gethash key cache)
1828 (puthash
1830 (let ((cookie-width (org-export-table-cell-width table-cell info)))
1831 (or (and (not widenp) cookie-width)
1832 (let ((contents-width
1833 (let ((max-width 0))
1834 (org-element-map table 'table-row
1835 (lambda (row)
1836 (setq max-width
1837 (max (string-width
1838 (org-export-data
1839 (org-element-contents
1840 (elt (org-element-contents row) col))
1841 info))
1842 max-width)))
1843 info)
1844 max-width)))
1845 (cond ((not cookie-width) contents-width)
1846 (widenp (max cookie-width contents-width))
1847 (t cookie-width)))))
1848 cache))))
1850 (defun org-ascii-table-cell (table-cell contents info)
1851 "Transcode a TABLE-CELL object from Org to ASCII.
1852 CONTENTS is the cell contents. INFO is a plist used as
1853 a communication channel."
1854 ;; Determine column width. When `org-ascii-table-widen-columns'
1855 ;; is nil and some width cookie has set it, use that value.
1856 ;; Otherwise, compute the maximum width among transcoded data of
1857 ;; each cell in the column.
1858 (let ((width (org-ascii--table-cell-width table-cell info)))
1859 ;; When contents are too large, truncate them.
1860 (unless (or (plist-get info :ascii-table-widen-columns)
1861 (<= (string-width (or contents "")) width))
1862 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1863 ;; Align contents correctly within the cell.
1864 (let* ((indent-tabs-mode nil)
1865 (data
1866 (when contents
1867 (org-ascii--justify-lines
1868 contents width
1869 (org-export-table-cell-alignment table-cell info)))))
1870 (setq contents
1871 (concat data
1872 (make-string (- width (string-width (or data ""))) ?\s))))
1873 ;; Return cell.
1874 (concat (format " %s " contents)
1875 (when (memq 'right (org-export-table-cell-borders table-cell info))
1876 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1879 ;;;; Table Row
1881 (defun org-ascii-table-row (table-row contents info)
1882 "Transcode a TABLE-ROW element from Org to ASCII.
1883 CONTENTS is the row contents. INFO is a plist used as
1884 a communication channel."
1885 (when (eq (org-element-property :type table-row) 'standard)
1886 (let ((build-hline
1887 (function
1888 (lambda (lcorner horiz vert rcorner)
1889 (concat
1890 (apply
1891 'concat
1892 (org-element-map table-row 'table-cell
1893 (lambda (cell)
1894 (let ((width (org-ascii--table-cell-width cell info))
1895 (borders (org-export-table-cell-borders cell info)))
1896 (concat
1897 ;; In order to know if CELL starts the row, do
1898 ;; not compare it with the first cell in the
1899 ;; row as there might be a special column.
1900 ;; Instead, compare it with first exportable
1901 ;; cell, obtained with `org-element-map'.
1902 (when (and (memq 'left borders)
1903 (eq (org-element-map table-row 'table-cell
1904 'identity info t)
1905 cell))
1906 lcorner)
1907 (make-string (+ 2 width) (string-to-char horiz))
1908 (cond
1909 ((not (memq 'right borders)) nil)
1910 ((eq (car (last (org-element-contents table-row))) cell)
1911 rcorner)
1912 (t vert)))))
1913 info)) "\n"))))
1914 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1915 (borders (org-export-table-cell-borders
1916 (org-element-map table-row 'table-cell 'identity info t)
1917 info)))
1918 (concat (cond
1919 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1920 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1921 (funcall build-hline "+" "-" "+" "+")))
1922 ((memq 'above borders)
1923 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1924 (funcall build-hline "+" "-" "+" "+"))))
1925 (when (memq 'left borders) (if utf8p "│" "|"))
1926 contents "\n"
1927 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1928 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1929 (funcall build-hline "+" "-" "+" "+")))))))
1932 ;;;; Timestamp
1934 (defun org-ascii-timestamp (timestamp contents info)
1935 "Transcode a TIMESTAMP object from Org to ASCII.
1936 CONTENTS is nil. INFO is a plist holding contextual information."
1937 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1940 ;;;; Underline
1942 (defun org-ascii-underline (underline contents info)
1943 "Transcode UNDERLINE from Org to ASCII.
1944 CONTENTS is the text with underline markup. INFO is a plist
1945 holding contextual information."
1946 (format "_%s_" contents))
1949 ;;;; Verbatim
1951 (defun org-ascii-verbatim (verbatim contents info)
1952 "Return a VERBATIM object from Org to ASCII.
1953 CONTENTS is nil. INFO is a plist holding contextual information."
1954 (format (plist-get info :ascii-verbatim-format)
1955 (org-element-property :value verbatim)))
1958 ;;;; Verse Block
1960 (defun org-ascii-verse-block (verse-block contents info)
1961 "Transcode a VERSE-BLOCK element from Org to ASCII.
1962 CONTENTS is verse block contents. INFO is a plist holding
1963 contextual information."
1964 (let ((verse-width (org-ascii--current-text-width verse-block info)))
1965 (org-ascii--indent-string
1966 (org-ascii--justify-element contents verse-block info)
1967 (plist-get info :ascii-quote-margin))))
1971 ;;; Filters
1973 (defun org-ascii-filter-headline-blank-lines (headline back-end info)
1974 "Filter controlling number of blank lines after a headline.
1976 HEADLINE is a string representing a transcoded headline.
1977 BACK-END is symbol specifying back-end used for export. INFO is
1978 plist containing the communication channel.
1980 This function only applies to `ascii' back-end. See
1981 `org-ascii-headline-spacing' for information."
1982 (let ((headline-spacing (plist-get info :ascii-headline-spacing)))
1983 (if (not headline-spacing) headline
1984 (let ((blanks (make-string (1+ (cdr headline-spacing)) ?\n)))
1985 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))))
1987 (defun org-ascii-filter-paragraph-spacing (tree back-end info)
1988 "Filter controlling number of blank lines between paragraphs.
1990 TREE is the parse tree. BACK-END is the symbol specifying
1991 back-end used for export. INFO is a plist used as
1992 a communication channel.
1994 See `org-ascii-paragraph-spacing' for information."
1995 (let ((paragraph-spacing (plist-get info :ascii-paragraph-spacing)))
1996 (when (wholenump paragraph-spacing)
1997 (org-element-map tree 'paragraph
1998 (lambda (p)
1999 (when (eq (org-element-type (org-export-get-next-element p info))
2000 'paragraph)
2001 (org-element-put-property p :post-blank paragraph-spacing))))))
2002 tree)
2004 (defun org-ascii-filter-comment-spacing (tree backend info)
2005 "Filter removing blank lines between comments.
2006 TREE is the parse tree. BACK-END is the symbol specifying
2007 back-end used for export. INFO is a plist used as
2008 a communication channel."
2009 (org-element-map tree '(comment comment-block)
2010 (lambda (c)
2011 (when (memq (org-element-type (org-export-get-next-element c info))
2012 '(comment comment-block))
2013 (org-element-put-property c :post-blank 0))))
2014 tree)
2018 ;;; End-user functions
2020 ;;;###autoload
2021 (defun org-ascii-export-as-ascii
2022 (&optional async subtreep visible-only body-only ext-plist)
2023 "Export current buffer to a text buffer.
2025 If narrowing is active in the current buffer, only export its
2026 narrowed part.
2028 If a region is active, export that region.
2030 A non-nil optional argument ASYNC means the process should happen
2031 asynchronously. The resulting buffer should be accessible
2032 through the `org-export-stack' interface.
2034 When optional argument SUBTREEP is non-nil, export the sub-tree
2035 at point, extracting information from the headline properties
2036 first.
2038 When optional argument VISIBLE-ONLY is non-nil, don't export
2039 contents of hidden elements.
2041 When optional argument BODY-ONLY is non-nil, strip title and
2042 table of contents from output.
2044 EXT-PLIST, when provided, is a property list with external
2045 parameters overriding Org default settings, but still inferior to
2046 file-local settings.
2048 Export is done in a buffer named \"*Org ASCII Export*\", which
2049 will be displayed when `org-export-show-temporary-export-buffer'
2050 is non-nil."
2051 (interactive)
2052 (org-export-to-buffer 'ascii "*Org ASCII Export*"
2053 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
2055 ;;;###autoload
2056 (defun org-ascii-export-to-ascii
2057 (&optional async subtreep visible-only body-only ext-plist)
2058 "Export current buffer to a text file.
2060 If narrowing is active in the current buffer, only export its
2061 narrowed part.
2063 If a region is active, export that region.
2065 A non-nil optional argument ASYNC means the process should happen
2066 asynchronously. The resulting file should be accessible through
2067 the `org-export-stack' interface.
2069 When optional argument SUBTREEP is non-nil, export the sub-tree
2070 at point, extracting information from the headline properties
2071 first.
2073 When optional argument VISIBLE-ONLY is non-nil, don't export
2074 contents of hidden elements.
2076 When optional argument BODY-ONLY is non-nil, strip title and
2077 table of contents from output.
2079 EXT-PLIST, when provided, is a property list with external
2080 parameters overriding Org default settings, but still inferior to
2081 file-local settings.
2083 Return output file's name."
2084 (interactive)
2085 (let ((file (org-export-output-file-name ".txt" subtreep)))
2086 (org-export-to-file 'ascii file
2087 async subtreep visible-only body-only ext-plist)))
2089 ;;;###autoload
2090 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
2091 "Publish an Org file to ASCII.
2093 FILENAME is the filename of the Org file to be published. PLIST
2094 is the property list for the given project. PUB-DIR is the
2095 publishing directory.
2097 Return output file name."
2098 (org-publish-org-to
2099 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
2101 ;;;###autoload
2102 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
2103 "Publish an Org file to Latin-1.
2105 FILENAME is the filename of the Org file to be published. PLIST
2106 is the property list for the given project. PUB-DIR is the
2107 publishing directory.
2109 Return output file name."
2110 (org-publish-org-to
2111 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
2113 ;;;###autoload
2114 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
2115 "Publish an org file to UTF-8.
2117 FILENAME is the filename of the Org file to be published. PLIST
2118 is the property list for the given project. PUB-DIR is the
2119 publishing directory.
2121 Return output file name."
2122 (org-publish-org-to
2123 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2126 (provide 'ox-ascii)
2128 ;; Local variables:
2129 ;; generated-autoload-file: "org-loaddefs.el"
2130 ;; coding: utf-8-emacs
2131 ;; End:
2133 ;;; ox-ascii.el ends here