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