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