org-export: Do not push empty strings to kill ring
[org-mode.git] / contrib / lisp / org-export.el
blob210cdd906e463bc24461448062885faa08e7dc39
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which sets one mandatory variable: his translation table. Its name
52 ;; is always `org-BACKEND-translate-alist' where BACKEND stands for
53 ;; the name chosen for the back-end. Its value is an alist whose keys
54 ;; are elements and objects types and values translator functions.
55 ;; See function's docstring for more information about translators.
57 ;; Optionally, `org-export-define-backend' can also support specific
58 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
59 ;; function documentation for more information.
61 ;; If the new back-end shares most properties with another one,
62 ;; `org-export-define-derived-backend' can be used to simplify the
63 ;; process.
65 ;; Any back-end can define its own variables. Among them, those
66 ;; customizable should belong to the `org-export-BACKEND' group.
68 ;; Tools for common tasks across back-ends are implemented in the
69 ;; penultimate part of this file. A dispatcher for standard back-ends
70 ;; is provided in the last one.
72 ;;; Code:
74 (eval-when-compile (require 'cl))
75 (require 'org-element)
76 (require 'ob-exp)
78 (declare-function org-e-publish "org-e-publish" (project &optional force))
79 (declare-function org-e-publish-all "org-e-publish" (&optional force))
80 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
81 (declare-function org-e-publish-current-project "org-e-publish"
82 (&optional force))
84 (defvar org-e-publish-project-alist)
85 (defvar org-table-number-fraction)
86 (defvar org-table-number-regexp)
90 ;;; Internal Variables
92 ;; Among internal variables, the most important is
93 ;; `org-export-options-alist'. This variable define the global export
94 ;; options, shared between every exporter, and how they are acquired.
96 (defconst org-export-max-depth 19
97 "Maximum nesting depth for headlines, counting from 0.")
99 (defconst org-export-options-alist
100 '((:author "AUTHOR" nil user-full-name t)
101 (:creator "CREATOR" nil org-export-creator-string)
102 (:date "DATE" nil nil t)
103 (:description "DESCRIPTION" nil nil newline)
104 (:email "EMAIL" nil user-mail-address t)
105 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
106 (:headline-levels nil "H" org-export-headline-levels)
107 (:keywords "KEYWORDS" nil nil space)
108 (:language "LANGUAGE" nil org-export-default-language t)
109 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
110 (:section-numbers nil "num" org-export-with-section-numbers)
111 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
112 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
113 (:title "TITLE" nil nil space)
114 (:with-archived-trees nil "arch" org-export-with-archived-trees)
115 (:with-author nil "author" org-export-with-author)
116 (:with-clocks nil "c" org-export-with-clocks)
117 (:with-creator nil "creator" org-export-with-creator)
118 (:with-drawers nil "d" org-export-with-drawers)
119 (:with-email nil "email" org-export-with-email)
120 (:with-emphasize nil "*" org-export-with-emphasize)
121 (:with-entities nil "e" org-export-with-entities)
122 (:with-fixed-width nil ":" org-export-with-fixed-width)
123 (:with-footnotes nil "f" org-export-with-footnotes)
124 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
125 (:with-plannings nil "p" org-export-with-planning)
126 (:with-priority nil "pri" org-export-with-priority)
127 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
128 (:with-special-strings nil "-" org-export-with-special-strings)
129 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
130 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
131 (:with-toc nil "toc" org-export-with-toc)
132 (:with-tables nil "|" org-export-with-tables)
133 (:with-tags nil "tags" org-export-with-tags)
134 (:with-tasks nil "tasks" org-export-with-tasks)
135 (:with-timestamps nil "<" org-export-with-timestamps)
136 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
137 "Alist between export properties and ways to set them.
139 The CAR of the alist is the property name, and the CDR is a list
140 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
142 KEYWORD is a string representing a buffer keyword, or nil. Each
143 property defined this way can also be set, during subtree
144 export, through an headline property named after the keyword
145 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
146 property).
147 OPTION is a string that could be found in an #+OPTIONS: line.
148 DEFAULT is the default value for the property.
149 BEHAVIOUR determine how Org should handle multiple keywords for
150 the same property. It is a symbol among:
151 nil Keep old value and discard the new one.
152 t Replace old value with the new one.
153 `space' Concatenate the values, separating them with a space.
154 `newline' Concatenate the values, separating them with
155 a newline.
156 `split' Split values at white spaces, and cons them to the
157 previous list.
159 KEYWORD and OPTION have precedence over DEFAULT.
161 All these properties should be back-end agnostic. Back-end
162 specific properties are set through `org-export-define-backend'.
163 Properties redefined there have precedence over these.")
165 (defconst org-export-special-keywords '("FILETAGS" "SETUP_FILE" "OPTIONS")
166 "List of in-buffer keywords that require special treatment.
167 These keywords are not directly associated to a property. The
168 way they are handled must be hard-coded into
169 `org-export--get-inbuffer-options' function.")
171 (defconst org-export-filters-alist
172 '((:filter-bold . org-export-filter-bold-functions)
173 (:filter-babel-call . org-export-filter-babel-call-functions)
174 (:filter-center-block . org-export-filter-center-block-functions)
175 (:filter-clock . org-export-filter-clock-functions)
176 (:filter-code . org-export-filter-code-functions)
177 (:filter-comment . org-export-filter-comment-functions)
178 (:filter-comment-block . org-export-filter-comment-block-functions)
179 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
180 (:filter-drawer . org-export-filter-drawer-functions)
181 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
182 (:filter-entity . org-export-filter-entity-functions)
183 (:filter-example-block . org-export-filter-example-block-functions)
184 (:filter-export-block . org-export-filter-export-block-functions)
185 (:filter-export-snippet . org-export-filter-export-snippet-functions)
186 (:filter-final-output . org-export-filter-final-output-functions)
187 (:filter-fixed-width . org-export-filter-fixed-width-functions)
188 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
189 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
190 (:filter-headline . org-export-filter-headline-functions)
191 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
192 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
193 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
194 (:filter-inlinetask . org-export-filter-inlinetask-functions)
195 (:filter-italic . org-export-filter-italic-functions)
196 (:filter-item . org-export-filter-item-functions)
197 (:filter-keyword . org-export-filter-keyword-functions)
198 (:filter-latex-environment . org-export-filter-latex-environment-functions)
199 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
200 (:filter-line-break . org-export-filter-line-break-functions)
201 (:filter-link . org-export-filter-link-functions)
202 (:filter-macro . org-export-filter-macro-functions)
203 (:filter-node-property . org-export-filter-node-property-functions)
204 (:filter-paragraph . org-export-filter-paragraph-functions)
205 (:filter-parse-tree . org-export-filter-parse-tree-functions)
206 (:filter-plain-list . org-export-filter-plain-list-functions)
207 (:filter-plain-text . org-export-filter-plain-text-functions)
208 (:filter-planning . org-export-filter-planning-functions)
209 (:filter-property-drawer . org-export-filter-property-drawer-functions)
210 (:filter-quote-block . org-export-filter-quote-block-functions)
211 (:filter-quote-section . org-export-filter-quote-section-functions)
212 (:filter-radio-target . org-export-filter-radio-target-functions)
213 (:filter-section . org-export-filter-section-functions)
214 (:filter-special-block . org-export-filter-special-block-functions)
215 (:filter-src-block . org-export-filter-src-block-functions)
216 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
217 (:filter-strike-through . org-export-filter-strike-through-functions)
218 (:filter-subscript . org-export-filter-subscript-functions)
219 (:filter-superscript . org-export-filter-superscript-functions)
220 (:filter-table . org-export-filter-table-functions)
221 (:filter-table-cell . org-export-filter-table-cell-functions)
222 (:filter-table-row . org-export-filter-table-row-functions)
223 (:filter-target . org-export-filter-target-functions)
224 (:filter-timestamp . org-export-filter-timestamp-functions)
225 (:filter-underline . org-export-filter-underline-functions)
226 (:filter-verbatim . org-export-filter-verbatim-functions)
227 (:filter-verse-block . org-export-filter-verse-block-functions))
228 "Alist between filters properties and initial values.
230 The key of each association is a property name accessible through
231 the communication channel. Its value is a configurable global
232 variable defining initial filters.
234 This list is meant to install user specified filters. Back-end
235 developers may install their own filters using
236 `org-export-define-backend'. Filters defined there will always
237 be prepended to the current list, so they always get applied
238 first.")
240 (defconst org-export-default-inline-image-rule
241 `(("file" .
242 ,(format "\\.%s\\'"
243 (regexp-opt
244 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
245 "xpm" "pbm" "pgm" "ppm") t))))
246 "Default rule for link matching an inline image.
247 This rule applies to links with no description. By default, it
248 will be considered as an inline image if it targets a local file
249 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
250 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
251 See `org-export-inline-image-p' for more information about
252 rules.")
256 ;;; User-configurable Variables
258 ;; Configuration for the masses.
260 ;; They should never be accessed directly, as their value is to be
261 ;; stored in a property list (cf. `org-export-options-alist').
262 ;; Back-ends will read their value from there instead.
264 (defgroup org-export nil
265 "Options for exporting Org mode files."
266 :tag "Org Export"
267 :group 'org)
269 (defgroup org-export-general nil
270 "General options for export engine."
271 :tag "Org Export General"
272 :group 'org-export)
274 (defcustom org-export-with-archived-trees 'headline
275 "Whether sub-trees with the ARCHIVE tag should be exported.
277 This can have three different values:
278 nil Do not export, pretend this tree is not present.
279 t Do export the entire tree.
280 `headline' Only export the headline, but skip the tree below it.
282 This option can also be set with the #+OPTIONS line,
283 e.g. \"arch:nil\"."
284 :group 'org-export-general
285 :type '(choice
286 (const :tag "Not at all" nil)
287 (const :tag "Headline only" 'headline)
288 (const :tag "Entirely" t)))
290 (defcustom org-export-with-author t
291 "Non-nil means insert author name into the exported file.
292 This option can also be set with the #+OPTIONS line,
293 e.g. \"author:nil\"."
294 :group 'org-export-general
295 :type 'boolean)
297 (defcustom org-export-with-clocks nil
298 "Non-nil means export CLOCK keywords.
299 This option can also be set with the #+OPTIONS line,
300 e.g. \"c:t\"."
301 :group 'org-export-general
302 :type 'boolean)
304 (defcustom org-export-with-creator 'comment
305 "Non-nil means the postamble should contain a creator sentence.
307 The sentence can be set in `org-export-creator-string' and
308 defaults to \"Generated by Org mode XX in Emacs XXX.\".
310 If the value is `comment' insert it as a comment."
311 :group 'org-export-general
312 :type '(choice
313 (const :tag "No creator sentence" nil)
314 (const :tag "Sentence as a comment" 'comment)
315 (const :tag "Insert the sentence" t)))
317 (defcustom org-export-creator-string
318 (format "Generated by Org mode %s in Emacs %s."
319 (if (fboundp 'org-version) (org-version) "(Unknown)")
320 emacs-version)
321 "String to insert at the end of the generated document."
322 :group 'org-export-general
323 :type '(string :tag "Creator string"))
325 (defcustom org-export-with-drawers t
326 "Non-nil means export contents of standard drawers.
328 When t, all drawers are exported. This may also be a list of
329 drawer names to export. This variable doesn't apply to
330 properties drawers.
332 This option can also be set with the #+OPTIONS line,
333 e.g. \"d:nil\"."
334 :group 'org-export-general
335 :type '(choice
336 (const :tag "All drawers" t)
337 (const :tag "None" nil)
338 (repeat :tag "Selected drawers"
339 (string :tag "Drawer name"))))
341 (defcustom org-export-with-email nil
342 "Non-nil means insert author email into the exported file.
343 This option can also be set with the #+OPTIONS line,
344 e.g. \"email:t\"."
345 :group 'org-export-general
346 :type 'boolean)
348 (defcustom org-export-with-emphasize t
349 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
351 If the export target supports emphasizing text, the word will be
352 typeset in bold, italic, or underlined, respectively. Not all
353 export backends support this.
355 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
356 :group 'org-export-general
357 :type 'boolean)
359 (defcustom org-export-exclude-tags '("noexport")
360 "Tags that exclude a tree from export.
362 All trees carrying any of these tags will be excluded from
363 export. This is without condition, so even subtrees inside that
364 carry one of the `org-export-select-tags' will be removed.
366 This option can also be set with the #+EXCLUDE_TAGS: keyword."
367 :group 'org-export-general
368 :type '(repeat (string :tag "Tag")))
370 (defcustom org-export-with-fixed-width t
371 "Non-nil means lines starting with \":\" will be in fixed width font.
373 This can be used to have pre-formatted text, fragments of code
374 etc. For example:
375 : ;; Some Lisp examples
376 : (while (defc cnt)
377 : (ding))
378 will be looking just like this in also HTML. See also the QUOTE
379 keyword. Not all export backends support this.
381 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
382 :group 'org-export-translation
383 :type 'boolean)
385 (defcustom org-export-with-footnotes t
386 "Non-nil means Org footnotes should be exported.
387 This option can also be set with the #+OPTIONS line,
388 e.g. \"f:nil\"."
389 :group 'org-export-general
390 :type 'boolean)
392 (defcustom org-export-headline-levels 3
393 "The last level which is still exported as a headline.
395 Inferior levels will produce itemize lists when exported.
397 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
398 :group 'org-export-general
399 :type 'integer)
401 (defcustom org-export-default-language "en"
402 "The default language for export and clocktable translations, as a string.
403 This may have an association in
404 `org-clock-clocktable-language-setup'."
405 :group 'org-export-general
406 :type '(string :tag "Language"))
408 (defcustom org-export-preserve-breaks nil
409 "Non-nil means preserve all line breaks when exporting.
411 Normally, in HTML output paragraphs will be reformatted.
413 This option can also be set with the #+OPTIONS line,
414 e.g. \"\\n:t\"."
415 :group 'org-export-general
416 :type 'boolean)
418 (defcustom org-export-with-entities t
419 "Non-nil means interpret entities when exporting.
421 For example, HTML export converts \\alpha to &alpha; and \\AA to
422 &Aring;.
424 For a list of supported names, see the constant `org-entities'
425 and the user option `org-entities-user'.
427 This option can also be set with the #+OPTIONS line,
428 e.g. \"e:nil\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-inlinetasks t
433 "Non-nil means inlinetasks should be exported.
434 This option can also be set with the #+OPTIONS line,
435 e.g. \"inline:nil\"."
436 :group 'org-export-general
437 :type 'boolean)
439 (defcustom org-export-with-planning nil
440 "Non-nil means include planning info in export.
441 This option can also be set with the #+OPTIONS: line,
442 e.g. \"p:t\"."
443 :group 'org-export-general
444 :type 'boolean)
446 (defcustom org-export-with-priority nil
447 "Non-nil means include priority cookies in export.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"pri:t\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-with-section-numbers t
454 "Non-nil means add section numbers to headlines when exporting.
456 When set to an integer n, numbering will only happen for
457 headlines whose relative level is higher or equal to n.
459 This option can also be set with the #+OPTIONS line,
460 e.g. \"num:t\"."
461 :group 'org-export-general
462 :type 'boolean)
464 (defcustom org-export-select-tags '("export")
465 "Tags that select a tree for export.
467 If any such tag is found in a buffer, all trees that do not carry
468 one of these tags will be ignored during export. Inside trees
469 that are selected like this, you can still deselect a subtree by
470 tagging it with one of the `org-export-exclude-tags'.
472 This option can also be set with the #+SELECT_TAGS: keyword."
473 :group 'org-export-general
474 :type '(repeat (string :tag "Tag")))
476 (defcustom org-export-with-smart-quotes nil
477 "Non-nil means activate smart quotes during export.
478 This option can also be set with the #+OPTIONS: line,
479 e.g. \"':t\"."
480 :group 'org-export-general
481 :type 'boolean)
483 (defcustom org-export-with-special-strings t
484 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
486 When this option is turned on, these strings will be exported as:
488 Org HTML LaTeX UTF-8
489 -----+----------+--------+-------
490 \\- &shy; \\-
491 -- &ndash; -- –
492 --- &mdash; --- —
493 ... &hellip; \\ldots …
495 This option can also be set with the #+OPTIONS line,
496 e.g. \"-:nil\"."
497 :group 'org-export-general
498 :type 'boolean)
500 (defcustom org-export-with-statistics-cookies t
501 "Non-nil means include statistics cookies in export.
502 This option can also be set with the #+OPTIONS: line,
503 e.g. \"stat:nil\""
504 :group 'org-export-general
505 :type 'boolean)
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
529 e.g. \"^:nil\"."
530 :group 'org-export-general
531 :type '(choice
532 (const :tag "Interpret them" t)
533 (const :tag "Curly brackets only" {})
534 (const :tag "Do not interpret them" nil)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
544 contents is made.
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group 'org-export-general
549 :type '(choice
550 (const :tag "No Table of Contents" nil)
551 (const :tag "Full Table of Contents" t)
552 (integer :tag "TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
556 For example:
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group 'org-export-general
564 :type 'boolean)
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
571 the document.
573 This option can also be set with the #+OPTIONS line,
574 e.g. \"tags:nil\"."
575 :group 'org-export-general
576 :type '(choice
577 (const :tag "Off" nil)
578 (const :tag "Not in TOC" not-in-toc)
579 (const :tag "On" t)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
584 This may have the following values:
585 t include tasks independent of state.
586 `todo' include only tasks that are not yet done.
587 `done' include only tasks that are already done.
588 nil ignore all tasks.
589 list of keywords include tasks with these keywords.
591 This option can also be set with the #+OPTIONS line,
592 e.g. \"tasks:nil\"."
593 :group 'org-export-general
594 :type '(choice
595 (const :tag "All tasks" t)
596 (const :tag "No tasks" nil)
597 (const :tag "Not-done tasks" todo)
598 (const :tag "Only done tasks" done)
599 (repeat :tag "Specific TODO keywords"
600 (string :tag "Keyword"))))
602 (defcustom org-export-time-stamp-file t
603 "Non-nil means insert a time stamp into the exported file.
604 The time stamp shows when the file was created.
606 This option can also be set with the #+OPTIONS line,
607 e.g. \"timestamp:nil\"."
608 :group 'org-export-general
609 :type 'boolean)
611 (defcustom org-export-with-timestamps t
612 "Non nil means allow timestamps in export.
614 It can be set to `active', `inactive', t or nil, in order to
615 export, respectively, only active timestamps, only inactive ones,
616 all of them or none.
618 This option can also be set with the #+OPTIONS line, e.g.
619 \"<:nil\"."
620 :group 'org-export-general
621 :type '(choice
622 (const :tag "All timestamps" t)
623 (const :tag "Only active timestamps" active)
624 (const :tag "Only inactive timestamps" inactive)
625 (const :tag "No timestamp" nil)))
627 (defcustom org-export-with-todo-keywords t
628 "Non-nil means include TODO keywords in export.
629 When nil, remove all these keywords from the export."
630 :group 'org-export-general
631 :type 'boolean)
633 (defcustom org-export-allow-BIND 'confirm
634 "Non-nil means allow #+BIND to define local variable values for export.
635 This is a potential security risk, which is why the user must
636 confirm the use of these lines."
637 :group 'org-export-general
638 :type '(choice
639 (const :tag "Never" nil)
640 (const :tag "Always" t)
641 (const :tag "Ask a confirmation for each file" confirm)))
643 (defcustom org-export-snippet-translation-alist nil
644 "Alist between export snippets back-ends and exporter back-ends.
646 This variable allows to provide shortcuts for export snippets.
648 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
649 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
650 HTML code while every other back-end will ignore it."
651 :group 'org-export-general
652 :type '(repeat
653 (cons
654 (string :tag "Shortcut")
655 (string :tag "Back-end"))))
657 (defcustom org-export-coding-system nil
658 "Coding system for the exported file."
659 :group 'org-export-general
660 :type 'coding-system)
662 (defcustom org-export-copy-to-kill-ring t
663 "Non-nil means exported stuff will also be pushed onto the kill ring."
664 :group 'org-export-general
665 :type 'boolean)
667 (defcustom org-export-initial-scope 'buffer
668 "The initial scope when exporting with `org-export-dispatch'.
669 This variable can be either set to `buffer' or `subtree'."
670 :group 'org-export-general
671 :type '(choice
672 (const :tag "Export current buffer" 'buffer)
673 (const :tag "Export current subtree" 'subtree)))
675 (defcustom org-export-show-temporary-export-buffer t
676 "Non-nil means show buffer after exporting to temp buffer.
677 When Org exports to a file, the buffer visiting that file is ever
678 shown, but remains buried. However, when exporting to
679 a temporary buffer, that buffer is popped up in a second window.
680 When this variable is nil, the buffer remains buried also in
681 these cases."
682 :group 'org-export-general
683 :type 'boolean)
685 (defcustom org-export-dispatch-use-expert-ui nil
686 "Non-nil means using a non-intrusive `org-export-dispatch'.
687 In that case, no help buffer is displayed. Though, an indicator
688 for current export scope is added to the prompt (\"b\" when
689 output is restricted to body only, \"s\" when it is restricted to
690 the current subtree, \"v\" when only visible elements are
691 considered for export and \"f\" when publishing functions should
692 be passed the FORCE argument). Also, \[?] allows to switch back
693 to standard mode."
694 :group 'org-export-general
695 :type 'boolean)
699 ;;; Defining New Back-ends
701 ;; `org-export-define-backend' is the standard way to define an export
702 ;; back-end. It allows to specify translators, filters, buffer
703 ;; options and a menu entry. If the new back-end shares translators
704 ;; with another back-end, `org-export-define-derived-backend' may be
705 ;; used instead.
707 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
708 ;; when a given back-end hasn't been registered yet.
710 (defmacro org-export-define-backend (backend translators &rest body)
711 "Define a new back-end BACKEND.
713 TRANSLATORS is an alist between object or element types and
714 functions handling them.
716 These functions should return a string without any trailing
717 space, or nil. They must accept three arguments: the object or
718 element itself, its contents or nil when it isn't recursive and
719 the property list used as a communication channel.
721 Contents, when not nil, are stripped from any global indentation
722 \(although the relative one is preserved). They also always end
723 with a single newline character.
725 If, for a given type, no function is found, that element or
726 object type will simply be ignored, along with any blank line or
727 white space at its end. The same will happen if the function
728 returns the nil value. If that function returns the empty
729 string, the type will be ignored, but the blank lines or white
730 spaces will be kept.
732 In addition to element and object types, one function can be
733 associated to the `template' symbol and another one to the
734 `plain-text' symbol.
736 The former returns the final transcoded string, and can be used
737 to add a preamble and a postamble to document's body. It must
738 accept two arguments: the transcoded string and the property list
739 containing export options.
741 The latter, when defined, is to be called on every text not
742 recognized as an element or an object. It must accept two
743 arguments: the text string and the information channel. It is an
744 appropriate place to protect special chars relative to the
745 back-end.
747 BODY can start with pre-defined keyword arguments. The following
748 keywords are understood:
750 :export-block
752 String, or list of strings, representing block names that
753 will not be parsed. This is used to specify blocks that will
754 contain raw code specific to the back-end. These blocks
755 still have to be handled by the relative `export-block' type
756 translator.
758 :filters-alist
760 Alist between filters and function, or list of functions,
761 specific to the back-end. See `org-export-filters-alist' for
762 a list of all allowed filters. Filters defined here
763 shouldn't make a back-end test, as it may prevent back-ends
764 derived from this one to behave properly.
766 :menu-entry
768 Menu entry for the export dispatcher. It should be a list
769 like:
771 \(KEY DESCRIPTION ACTION-OR-MENU)
773 where :
775 KEY is a free character selecting the back-end.
776 DESCRIPTION is a string naming the back-end.
777 ACTION-OR-MENU is either a function or an alist.
779 If it is an action, it will be called with three arguments:
780 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
781 for further explanations.
783 If it is an alist, associations should follow the
784 pattern:
786 \(KEY DESCRIPTION ACTION)
788 where KEY, DESCRIPTION and ACTION are described above.
790 Valid values include:
792 \(?m \"My Special Back-end\" my-special-export-function)
796 \(?l \"Export to LaTeX\"
797 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
798 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
799 \(?p \"PDF file\" org-e-latex-export-to-pdf)
800 \(?o \"PDF file and open\"
801 \(lambda (subtree visible body-only)
802 \(org-open-file
803 \(org-e-latex-export-to-pdf subtree visible body-only))))))
805 :options-alist
807 Alist between back-end specific properties introduced in
808 communication channel and how their value are acquired. See
809 `org-export-options-alist' for more information about
810 structure of the values."
811 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
812 (indent 1))
813 (let (export-block filters menu-entry options)
814 (while (keywordp (car body))
815 (case (pop body)
816 (:export-block (let ((names (pop body)))
817 (setq export-block
818 (if (consp names) (mapcar 'upcase names)
819 (list (upcase names))))))
820 (:filters-alist (setq filters (pop body)))
821 (:menu-entry (setq menu-entry (pop body)))
822 (:options-alist (setq options (pop body)))
823 (t (pop body))))
824 `(progn
825 ;; Define translators.
826 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
827 "Alist between element or object types and translators.")
828 ;; Define options.
829 ,(when options
830 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
831 ,(format "Alist between %s export properties and ways to set them.
832 See `org-export-options-alist' for more information on the
833 structure of the values."
834 backend)))
835 ;; Define filters.
836 ,(when filters
837 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
838 "Alist between filters keywords and back-end specific filters.
839 See `org-export-filters-alist' for more information."))
840 ;; Tell parser to not parse EXPORT-BLOCK blocks.
841 ,(when export-block
842 `(mapc
843 (lambda (name)
844 (add-to-list 'org-element-block-name-alist
845 `(,name . org-element-export-block-parser)))
846 ',export-block))
847 ;; Add an entry for back-end in `org-export-dispatch'.
848 ,(when menu-entry
849 `(unless (assq (car ',menu-entry) org-export-dispatch-menu-entries)
850 (add-to-list 'org-export-dispatch-menu-entries ',menu-entry)))
851 ;; Splice in the body, if any.
852 ,@body)))
854 (defmacro org-export-define-derived-backend (child parent &rest body)
855 "Create a new back-end as a variant of an existing one.
857 CHILD is the name of the derived back-end. PARENT is the name of
858 the parent back-end.
860 BODY can start with pre-defined keyword arguments. The following
861 keywords are understood:
863 :export-block
865 String, or list of strings, representing block names that
866 will not be parsed. This is used to specify blocks that will
867 contain raw code specific to the back-end. These blocks
868 still have to be handled by the relative `export-block' type
869 translator.
871 :filters-alist
873 Alist of filters that will overwrite or complete filters
874 defined in PARENT back-end. See `org-export-filters-alist'
875 for a list of allowed filters.
877 :menu-entry
879 Menu entry for the export dispatcher. See
880 `org-export-define-backend' for more information about the
881 expected value.
883 :options-alist
885 Alist of back-end specific properties that will overwrite or
886 complete those defined in PARENT back-end. Refer to
887 `org-export-options-alist' for more information about
888 structure of the values.
890 :sub-menu-entry
892 Append entries to an existing menu in the export dispatcher.
893 The associated value should be a list whose CAR is the
894 character selecting the menu to expand and CDR a list of
895 entries following the pattern:
897 \(KEY DESCRIPTION ACTION)
899 where KEY is a free character triggering the action,
900 DESCRIPTION is a string defining the action, and ACTION is
901 a function that will be called with three arguments:
902 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
903 for further explanations.
905 Valid values include:
907 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
908 \(?O \"As PDF file and open (Beamer)\"
909 \(lambda (s v b)
910 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
912 :translate-alist
914 Alist of element and object types and transcoders that will
915 overwrite or complete transcode table from PARENT back-end.
916 Refer to `org-export-define-backend' for detailed information
917 about transcoders.
919 As an example, here is how one could define \"my-latex\" back-end
920 as a variant of `e-latex' back-end with a custom template
921 function:
923 \(org-export-define-derived-backend my-latex e-latex
924 :translate-alist ((template . my-latex-template-fun)))
926 The back-end could then be called with, for example:
928 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
929 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
930 (indent 2))
931 (let (export-block filters menu-entry options sub-menu-entry translate)
932 (while (keywordp (car body))
933 (case (pop body)
934 (:export-block (let ((names (pop body)))
935 (setq export-block
936 (if (consp names) (mapcar 'upcase names)
937 (list (upcase names))))))
938 (:filters-alist (setq filters (pop body)))
939 (:menu-entry (setq menu-entry (pop body)))
940 (:options-alist (setq options (pop body)))
941 (:sub-menu-entry (setq sub-menu-entry (pop body)))
942 (:translate-alist (setq translate (pop body)))
943 (t (pop body))))
944 `(progn
945 ;; Tell parser to not parse EXPORT-BLOCK blocks.
946 ,(when export-block
947 `(mapc
948 (lambda (name)
949 (add-to-list 'org-element-block-name-alist
950 `(,name . org-element-export-block-parser)))
951 ',export-block))
952 ;; Define filters.
953 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
954 (when (or (boundp parent-filters) filters)
955 `(defconst ,(intern (format "org-%s-filters-alist" child))
956 ',(append filters
957 (and (boundp parent-filters)
958 (copy-sequence (symbol-value parent-filters))))
959 "Alist between filters keywords and back-end specific filters.
960 See `org-export-filters-alist' for more information.")))
961 ;; Define options.
962 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
963 (when (or (boundp parent-options) options)
964 `(defconst ,(intern (format "org-%s-options-alist" child))
965 ',(append options
966 (and (boundp parent-options)
967 (copy-sequence (symbol-value parent-options))))
968 ,(format "Alist between %s export properties and ways to set them.
969 See `org-export-options-alist' for more information on the
970 structure of the values."
971 child))))
972 ;; Define translators.
973 (defvar ,(intern (format "org-%s-translate-alist" child))
974 ',(append translate
975 (copy-sequence
976 (symbol-value
977 (intern (format "org-%s-translate-alist" parent)))))
978 "Alist between element or object types and translators.")
979 ;; Add an entry for back-end in `org-export-dispatch'.
980 ,(when menu-entry
981 `(unless (assq (car ',menu-entry) org-export-dispatch-menu-entries)
982 (add-to-list 'org-export-dispatch-menu-entries ',menu-entry)))
983 ,(when sub-menu-entry
984 (let ((menu (nth 2 (assq (car sub-menu-entry)
985 org-export-dispatch-menu-entries))))
986 (when menu `(nconc ',menu
987 ',(org-remove-if (lambda (e) (member e menu))
988 (cdr sub-menu-entry))))))
989 ;; Splice in the body, if any.
990 ,@body)))
992 (defun org-export-barf-if-invalid-backend (backend)
993 "Signal an error if BACKEND isn't defined."
994 (unless (boundp (intern (format "org-%s-translate-alist" backend)))
995 (error "Unknown \"%s\" back-end: Aborting export" backend)))
999 ;;; The Communication Channel
1001 ;; During export process, every function has access to a number of
1002 ;; properties. They are of two types:
1004 ;; 1. Environment options are collected once at the very beginning of
1005 ;; the process, out of the original buffer and configuration.
1006 ;; Collecting them is handled by `org-export-get-environment'
1007 ;; function.
1009 ;; Most environment options are defined through the
1010 ;; `org-export-options-alist' variable.
1012 ;; 2. Tree properties are extracted directly from the parsed tree,
1013 ;; just before export, by `org-export-collect-tree-properties'.
1015 ;; Here is the full list of properties available during transcode
1016 ;; process, with their category and their value type.
1018 ;; + `:author' :: Author's name.
1019 ;; - category :: option
1020 ;; - type :: string
1022 ;; + `:back-end' :: Current back-end used for transcoding.
1023 ;; - category :: tree
1024 ;; - type :: symbol
1026 ;; + `:creator' :: String to write as creation information.
1027 ;; - category :: option
1028 ;; - type :: string
1030 ;; + `:date' :: String to use as date.
1031 ;; - category :: option
1032 ;; - type :: string
1034 ;; + `:description' :: Description text for the current data.
1035 ;; - category :: option
1036 ;; - type :: string
1038 ;; + `:email' :: Author's email.
1039 ;; - category :: option
1040 ;; - type :: string
1042 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1043 ;; process.
1044 ;; - category :: option
1045 ;; - type :: list of strings
1047 ;; + `:exported-data' :: Hash table used for memoizing
1048 ;; `org-export-data'.
1049 ;; - category :: tree
1050 ;; - type :: hash table
1052 ;; + `:filetags' :: List of global tags for buffer. Used by
1053 ;; `org-export-get-tags' to get tags with inheritance.
1054 ;; - category :: option
1055 ;; - type :: list of strings
1057 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1058 ;; their definition, as parsed data. Only non-inlined footnotes
1059 ;; are represented in this alist. Also, every definition isn't
1060 ;; guaranteed to be referenced in the parse tree. The purpose of
1061 ;; this property is to preserve definitions from oblivion
1062 ;; (i.e. when the parse tree comes from a part of the original
1063 ;; buffer), it isn't meant for direct use in a back-end. To
1064 ;; retrieve a definition relative to a reference, use
1065 ;; `org-export-get-footnote-definition' instead.
1066 ;; - category :: option
1067 ;; - type :: alist (STRING . LIST)
1069 ;; + `:headline-levels' :: Maximum level being exported as an
1070 ;; headline. Comparison is done with the relative level of
1071 ;; headlines in the parse tree, not necessarily with their
1072 ;; actual level.
1073 ;; - category :: option
1074 ;; - type :: integer
1076 ;; + `:headline-offset' :: Difference between relative and real level
1077 ;; of headlines in the parse tree. For example, a value of -1
1078 ;; means a level 2 headline should be considered as level
1079 ;; 1 (cf. `org-export-get-relative-level').
1080 ;; - category :: tree
1081 ;; - type :: integer
1083 ;; + `:headline-numbering' :: Alist between headlines and their
1084 ;; numbering, as a list of numbers
1085 ;; (cf. `org-export-get-headline-number').
1086 ;; - category :: tree
1087 ;; - type :: alist (INTEGER . LIST)
1089 ;; + `:id-alist' :: Alist between ID strings and destination file's
1090 ;; path, relative to current directory. It is used by
1091 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1092 ;; external file.
1093 ;; - category :: option
1094 ;; - type :: alist (STRING . STRING)
1096 ;; + `:ignore-list' :: List of elements and objects that should be
1097 ;; ignored during export.
1098 ;; - category :: tree
1099 ;; - type :: list of elements and objects
1101 ;; + `:input-file' :: Full path to input file, if any.
1102 ;; - category :: option
1103 ;; - type :: string or nil
1105 ;; + `:keywords' :: List of keywords attached to data.
1106 ;; - category :: option
1107 ;; - type :: string
1109 ;; + `:language' :: Default language used for translations.
1110 ;; - category :: option
1111 ;; - type :: string
1113 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1114 ;; transcoding.
1115 ;; - category :: option
1116 ;; - type :: list (as returned by `org-element-parse-buffer')
1118 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1119 ;; all line breaks.
1120 ;; - category :: option
1121 ;; - type :: symbol (nil, t)
1123 ;; + `:section-numbers' :: Non-nil means transcoding should add
1124 ;; section numbers to headlines.
1125 ;; - category :: option
1126 ;; - type :: symbol (nil, t)
1128 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1129 ;; in transcoding. When such a tag is present, subtrees without
1130 ;; it are de facto excluded from the process. See
1131 ;; `use-select-tags'.
1132 ;; - category :: option
1133 ;; - type :: list of strings
1135 ;; + `:target-list' :: List of targets encountered in the parse tree.
1136 ;; This is used to partly resolve "fuzzy" links
1137 ;; (cf. `org-export-resolve-fuzzy-link').
1138 ;; - category :: tree
1139 ;; - type :: list of strings
1141 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1142 ;; a time stamp in the output.
1143 ;; - category :: option
1144 ;; - type :: symbol (nil, t)
1146 ;; + `:translate-alist' :: Alist between element and object types and
1147 ;; transcoding functions relative to the current back-end.
1148 ;; Special keys `template' and `plain-text' are also possible.
1149 ;; - category :: option
1150 ;; - type :: alist (SYMBOL . FUNCTION)
1152 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1153 ;; also be transcoded. If it is set to the `headline' symbol,
1154 ;; only the archived headline's name is retained.
1155 ;; - category :: option
1156 ;; - type :: symbol (nil, t, `headline')
1158 ;; + `:with-author' :: Non-nil means author's name should be included
1159 ;; in the output.
1160 ;; - category :: option
1161 ;; - type :: symbol (nil, t)
1163 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1164 ;; - category :: option
1165 ;; - type :: symbol (nil, t)
1167 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1168 ;; inserted at the end of the transcoded string. If the value
1169 ;; is `comment', it should be commented.
1170 ;; - category :: option
1171 ;; - type :: symbol (`comment', nil, t)
1173 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1174 ;; its value is a list of names, only drawers with such names
1175 ;; will be transcoded.
1176 ;; - category :: option
1177 ;; - type :: symbol (nil, t) or list of strings
1179 ;; + `:with-email' :: Non-nil means output should contain author's
1180 ;; email.
1181 ;; - category :: option
1182 ;; - type :: symbol (nil, t)
1184 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1185 ;; interpreted.
1186 ;; - category :: option
1187 ;; - type :: symbol (nil, t)
1189 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1190 ;; strings starting with a colon as a fixed-with (verbatim) area.
1191 ;; - category :: option
1192 ;; - type :: symbol (nil, t)
1194 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1195 ;; footnotes.
1196 ;; - category :: option
1197 ;; - type :: symbol (nil, t)
1199 ;; + `:with-plannings' :: Non-nil means transcoding should include
1200 ;; planning info.
1201 ;; - category :: option
1202 ;; - type :: symbol (nil, t)
1204 ;; + `:with-priority' :: Non-nil means transcoding should include
1205 ;; priority cookies.
1206 ;; - category :: option
1207 ;; - type :: symbol (nil, t)
1209 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1210 ;; plain text.
1211 ;; - category :: option
1212 ;; - type :: symbol (nil, t)
1214 ;; + `:with-special-strings' :: Non-nil means transcoding should
1215 ;; interpret special strings in plain text.
1216 ;; - category :: option
1217 ;; - type :: symbol (nil, t)
1219 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1220 ;; interpret subscript and superscript. With a value of "{}",
1221 ;; only interpret those using curly brackets.
1222 ;; - category :: option
1223 ;; - type :: symbol (nil, {}, t)
1225 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1226 ;; tables.
1227 ;; - category :: option
1228 ;; - type :: symbol (nil, t)
1230 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1231 ;; headlines. A `not-in-toc' value will remove them from the
1232 ;; table of contents, if any, nonetheless.
1233 ;; - category :: option
1234 ;; - type :: symbol (nil, t, `not-in-toc')
1236 ;; + `:with-tasks' :: Non-nil means transcoding should include
1237 ;; headlines with a TODO keyword. A `todo' value will only
1238 ;; include headlines with a todo type keyword while a `done'
1239 ;; value will do the contrary. If a list of strings is provided,
1240 ;; only tasks with keywords belonging to that list will be kept.
1241 ;; - category :: option
1242 ;; - type :: symbol (t, todo, done, nil) or list of strings
1244 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1245 ;; time stamps. Special value `active' (resp. `inactive') ask to
1246 ;; export only active (resp. inactive) timestamps. Otherwise,
1247 ;; completely remove them.
1248 ;; - category :: option
1249 ;; - type :: symbol: (`active', `inactive', t, nil)
1251 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1252 ;; added to the output. An integer value limits its depth.
1253 ;; - category :: option
1254 ;; - type :: symbol (nil, t or integer)
1256 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1257 ;; include TODO keywords.
1258 ;; - category :: option
1259 ;; - type :: symbol (nil, t)
1262 ;;;; Environment Options
1264 ;; Environment options encompass all parameters defined outside the
1265 ;; scope of the parsed data. They come from five sources, in
1266 ;; increasing precedence order:
1268 ;; - Global variables,
1269 ;; - Buffer's attributes,
1270 ;; - Options keyword symbols,
1271 ;; - Buffer keywords,
1272 ;; - Subtree properties.
1274 ;; The central internal function with regards to environment options
1275 ;; is `org-export-get-environment'. It updates global variables with
1276 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1277 ;; the different sources.
1279 ;; The internal functions doing the retrieval are:
1280 ;; `org-export--get-global-options',
1281 ;; `org-export--get-buffer-attributes',
1282 ;; `org-export--parse-option-keyword',
1283 ;; `org-export--get-subtree-options' and
1284 ;; `org-export--get-inbuffer-options'
1286 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1287 ;; take care of the part relative to "#+BIND:" keywords.
1289 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1290 "Collect export options from the current buffer.
1292 Optional argument BACKEND is a symbol specifying which back-end
1293 specific options to read, if any.
1295 When optional argument SUBTREEP is non-nil, assume the export is
1296 done against the current sub-tree.
1298 Third optional argument EXT-PLIST is a property list with
1299 external parameters overriding Org default settings, but still
1300 inferior to file-local settings."
1301 ;; First install #+BIND variables.
1302 (org-export--install-letbind-maybe)
1303 ;; Get and prioritize export options...
1304 (org-combine-plists
1305 ;; ... from global variables...
1306 (org-export--get-global-options backend)
1307 ;; ... from an external property list...
1308 ext-plist
1309 ;; ... from in-buffer settings...
1310 (org-export--get-inbuffer-options
1311 backend
1312 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1313 ;; ... and from subtree, when appropriate.
1314 (and subtreep (org-export--get-subtree-options backend))
1315 ;; Eventually add misc. properties.
1316 (list
1317 :back-end
1318 backend
1319 :translate-alist
1320 (let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1321 (when (boundp trans-alist) (symbol-value trans-alist)))
1322 :footnote-definition-alist
1323 ;; Footnotes definitions must be collected in the original
1324 ;; buffer, as there's no insurance that they will still be in
1325 ;; the parse tree, due to possible narrowing.
1326 (let (alist)
1327 (org-with-wide-buffer
1328 (goto-char (point-min))
1329 (while (re-search-forward org-footnote-definition-re nil t)
1330 (let ((def (save-match-data (org-element-at-point))))
1331 (when (eq (org-element-type def) 'footnote-definition)
1332 (push
1333 (cons (org-element-property :label def)
1334 (let ((cbeg (org-element-property :contents-begin def)))
1335 (when cbeg
1336 (org-element--parse-elements
1337 cbeg (org-element-property :contents-end def)
1338 nil nil nil nil (list 'org-data nil)))))
1339 alist))))
1340 alist))
1341 :id-alist
1342 ;; Collect id references.
1343 (let (alist)
1344 (org-with-wide-buffer
1345 (goto-char (point-min))
1346 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1347 (let ((link (org-element-context)))
1348 (when (eq (org-element-type link) 'link)
1349 (let* ((id (org-element-property :path link))
1350 (file (org-id-find-id-file id)))
1351 (when file
1352 (push (cons id (file-relative-name file)) alist)))))))
1353 alist))))
1355 (defun org-export--parse-option-keyword (options &optional backend)
1356 "Parse an OPTIONS line and return values as a plist.
1357 Optional argument BACKEND is a symbol specifying which back-end
1358 specific items to read, if any."
1359 (let* ((all
1360 (append org-export-options-alist
1361 (and backend
1362 (let ((var (intern
1363 (format "org-%s-options-alist" backend))))
1364 (and (boundp var) (eval var))))))
1365 ;; Build an alist between #+OPTION: item and property-name.
1366 (alist (delq nil
1367 (mapcar (lambda (e)
1368 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1369 (car e))))
1370 all)))
1371 plist)
1372 (mapc (lambda (e)
1373 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1374 (car e)
1375 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1376 options)
1377 (setq plist (plist-put plist
1378 (cdr e)
1379 (car (read-from-string
1380 (match-string 2 options)))))))
1381 alist)
1382 plist))
1384 (defun org-export--get-subtree-options (&optional backend)
1385 "Get export options in subtree at point.
1386 Optional argument BACKEND is a symbol specifying back-end used
1387 for export. Return options as a plist."
1388 ;; For each buffer keyword, create an headline property setting the
1389 ;; same property in communication channel. The name for the property
1390 ;; is the keyword with "EXPORT_" appended to it.
1391 (org-with-wide-buffer
1392 (let (prop plist)
1393 ;; Make sure point is at an heading.
1394 (unless (org-at-heading-p) (org-back-to-heading t))
1395 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1396 ;; title as its fallback value.
1397 (when (setq prop (progn (looking-at org-todo-line-regexp)
1398 (or (save-match-data
1399 (org-entry-get (point) "EXPORT_TITLE"))
1400 (org-match-string-no-properties 3))))
1401 (setq plist
1402 (plist-put
1403 plist :title
1404 (org-element-parse-secondary-string
1405 prop (org-element-restriction 'keyword)))))
1406 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1407 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1408 (setq plist
1409 (nconc plist (org-export--parse-option-keyword prop backend))))
1410 ;; Handle other keywords. TITLE keyword is excluded as it has
1411 ;; been handled already.
1412 (let ((seen '("TITLE")))
1413 (mapc
1414 (lambda (option)
1415 (let ((property (nth 1 option)))
1416 (when (and property (not (member property seen)))
1417 (let* ((subtree-prop (concat "EXPORT_" property))
1418 ;; Export properties are not case-sensitive.
1419 (value (let ((case-fold-search t))
1420 (org-entry-get (point) subtree-prop))))
1421 (push property seen)
1422 (when value
1423 (setq plist
1424 (plist-put
1425 plist
1426 (car option)
1427 ;; Parse VALUE if required.
1428 (if (member property org-element-document-properties)
1429 (org-element-parse-secondary-string
1430 value (org-element-restriction 'keyword))
1431 value))))))))
1432 ;; Also look for both general keywords and back-end specific
1433 ;; options if BACKEND is provided.
1434 (append (and backend
1435 (let ((var (intern
1436 (format "org-%s-options-alist" backend))))
1437 (and (boundp var) (symbol-value var))))
1438 org-export-options-alist)))
1439 ;; Return value.
1440 plist)))
1442 (defun org-export--get-inbuffer-options (&optional backend files)
1443 "Return current buffer export options, as a plist.
1445 Optional argument BACKEND, when non-nil, is a symbol specifying
1446 which back-end specific options should also be read in the
1447 process.
1449 Optional argument FILES is a list of setup files names read so
1450 far, used to avoid circular dependencies.
1452 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1453 (org-with-wide-buffer
1454 (goto-char (point-min))
1455 (let ((case-fold-search t) plist)
1456 ;; 1. Special keywords, as in `org-export-special-keywords'.
1457 (let ((special-re
1458 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1459 (while (re-search-forward special-re nil t)
1460 (let ((element (org-element-at-point)))
1461 (when (eq (org-element-type element) 'keyword)
1462 (let* ((key (org-element-property :key element))
1463 (val (org-element-property :value element))
1464 (prop
1465 (cond
1466 ((equal key "SETUP_FILE")
1467 (let ((file
1468 (expand-file-name
1469 (org-remove-double-quotes (org-trim val)))))
1470 ;; Avoid circular dependencies.
1471 (unless (member file files)
1472 (with-temp-buffer
1473 (insert (org-file-contents file 'noerror))
1474 (org-mode)
1475 (org-export--get-inbuffer-options
1476 backend (cons file files))))))
1477 ((equal key "OPTIONS")
1478 (org-export--parse-option-keyword val backend))
1479 ((equal key "FILETAGS")
1480 (list :filetags
1481 (org-uniquify
1482 (append (org-split-string val ":")
1483 (plist-get plist :filetags))))))))
1484 (setq plist (org-combine-plists plist prop)))))))
1485 ;; 2. Standard options, as in `org-export-options-alist'.
1486 (let* ((all (append org-export-options-alist
1487 ;; Also look for back-end specific options
1488 ;; if BACKEND is defined.
1489 (and backend
1490 (let ((var
1491 (intern
1492 (format "org-%s-options-alist" backend))))
1493 (and (boundp var) (eval var))))))
1494 ;; Build ALIST between keyword name and property name.
1495 (alist
1496 (delq nil (mapcar
1497 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1498 all)))
1499 ;; Build regexp matching all keywords associated to export
1500 ;; options. Note: the search is case insensitive.
1501 (opt-re (format "^[ \t]*#\\+%s:"
1502 (regexp-opt
1503 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1504 (goto-char (point-min))
1505 (while (re-search-forward opt-re nil t)
1506 (let ((element (org-element-at-point)))
1507 (when (eq (org-element-type element) 'keyword)
1508 (let* ((key (org-element-property :key element))
1509 (val (org-element-property :value element))
1510 (prop (cdr (assoc key alist)))
1511 (behaviour (nth 4 (assq prop all))))
1512 (setq plist
1513 (plist-put
1514 plist prop
1515 ;; Handle value depending on specified BEHAVIOUR.
1516 (case behaviour
1517 (space
1518 (if (not (plist-get plist prop)) (org-trim val)
1519 (concat (plist-get plist prop) " " (org-trim val))))
1520 (newline
1521 (org-trim
1522 (concat (plist-get plist prop) "\n" (org-trim val))))
1523 (split
1524 `(,@(plist-get plist prop) ,@(org-split-string val)))
1525 ('t val)
1526 (otherwise (if (not (plist-member plist prop)) val
1527 (plist-get plist prop))))))))))
1528 ;; Parse keywords specified in
1529 ;; `org-element-document-properties'.
1530 (mapc
1531 (lambda (key)
1532 (let* ((prop (cdr (assoc key alist)))
1533 (value (and prop (plist-get plist prop))))
1534 (when (stringp value)
1535 (setq plist
1536 (plist-put
1537 plist prop
1538 (org-element-parse-secondary-string
1539 value (org-element-restriction 'keyword)))))))
1540 org-element-document-properties))
1541 ;; 3. Return final value.
1542 plist)))
1544 (defun org-export--get-buffer-attributes ()
1545 "Return properties related to buffer attributes, as a plist."
1546 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1547 (list
1548 ;; Store full path of input file name, or nil. For internal use.
1549 :input-file visited-file
1550 :title (or (and visited-file
1551 (file-name-sans-extension
1552 (file-name-nondirectory visited-file)))
1553 (buffer-name (buffer-base-buffer))))))
1555 (defun org-export--get-global-options (&optional backend)
1556 "Return global export options as a plist.
1558 Optional argument BACKEND, if non-nil, is a symbol specifying
1559 which back-end specific export options should also be read in the
1560 process."
1561 (let ((all (append org-export-options-alist
1562 (and backend
1563 (let ((var (intern
1564 (format "org-%s-options-alist" backend))))
1565 (and (boundp var) (symbol-value var))))))
1566 ;; Output value.
1567 plist)
1568 (mapc
1569 (lambda (cell)
1570 (setq plist
1571 (plist-put
1572 plist
1573 (car cell)
1574 ;; Eval default value provided. If keyword is a member
1575 ;; of `org-element-document-properties', parse it as
1576 ;; a secondary string before storing it.
1577 (let ((value (eval (nth 3 cell))))
1578 (if (not (stringp value)) value
1579 (let ((keyword (nth 1 cell)))
1580 (if (not (member keyword org-element-document-properties))
1581 value
1582 (org-element-parse-secondary-string
1583 value (org-element-restriction 'keyword)))))))))
1584 all)
1585 ;; Return value.
1586 plist))
1588 (defvar org-export--allow-BIND-local nil)
1589 (defun org-export--confirm-letbind ()
1590 "Can we use #+BIND values during export?
1591 By default this will ask for confirmation by the user, to divert
1592 possible security risks."
1593 (cond
1594 ((not org-export-allow-BIND) nil)
1595 ((eq org-export-allow-BIND t) t)
1596 ((local-variable-p 'org-export--allow-BIND-local)
1597 org-export--allow-BIND-local)
1598 (t (org-set-local 'org-export--allow-BIND-local
1599 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1601 (defun org-export--install-letbind-maybe ()
1602 "Install the values from #+BIND lines as local variables.
1603 Variables must be installed before in-buffer options are
1604 retrieved."
1605 (let ((case-fold-search t) letbind pair)
1606 (org-with-wide-buffer
1607 (goto-char (point-min))
1608 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1609 (let* ((element (org-element-at-point))
1610 (value (org-element-property :value element)))
1611 (when (and (eq (org-element-type element) 'keyword)
1612 (not (equal value ""))
1613 (org-export--confirm-letbind))
1614 (push (read (format "(%s)" value)) letbind)))))
1615 (dolist (pair (nreverse letbind))
1616 (org-set-local (car pair) (nth 1 pair)))))
1619 ;;;; Tree Properties
1621 ;; Tree properties are information extracted from parse tree. They
1622 ;; are initialized at the beginning of the transcoding process by
1623 ;; `org-export-collect-tree-properties'.
1625 ;; Dedicated functions focus on computing the value of specific tree
1626 ;; properties during initialization. Thus,
1627 ;; `org-export--populate-ignore-list' lists elements and objects that
1628 ;; should be skipped during export, `org-export--get-min-level' gets
1629 ;; the minimal exportable level, used as a basis to compute relative
1630 ;; level for headlines. Eventually
1631 ;; `org-export--collect-headline-numbering' builds an alist between
1632 ;; headlines and their numbering.
1634 (defun org-export-collect-tree-properties (data info)
1635 "Extract tree properties from parse tree.
1637 DATA is the parse tree from which information is retrieved. INFO
1638 is a list holding export options.
1640 Following tree properties are set or updated:
1642 `:exported-data' Hash table used to memoize results from
1643 `org-export-data'.
1645 `:footnote-definition-alist' List of footnotes definitions in
1646 original buffer and current parse tree.
1648 `:headline-offset' Offset between true level of headlines and
1649 local level. An offset of -1 means an headline
1650 of level 2 should be considered as a level
1651 1 headline in the context.
1653 `:headline-numbering' Alist of all headlines as key an the
1654 associated numbering as value.
1656 `:ignore-list' List of elements that should be ignored during
1657 export.
1659 `:target-list' List of all targets in the parse tree.
1661 Return updated plist."
1662 ;; Install the parse tree in the communication channel, in order to
1663 ;; use `org-export-get-genealogy' and al.
1664 (setq info (plist-put info :parse-tree data))
1665 ;; Get the list of elements and objects to ignore, and put it into
1666 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1667 ;; been done during parse tree filtering.
1668 (setq info
1669 (plist-put info
1670 :ignore-list
1671 (append (org-export--populate-ignore-list data info)
1672 (plist-get info :ignore-list))))
1673 ;; Compute `:headline-offset' in order to be able to use
1674 ;; `org-export-get-relative-level'.
1675 (setq info
1676 (plist-put info
1677 :headline-offset
1678 (- 1 (org-export--get-min-level data info))))
1679 ;; Update footnotes definitions list with definitions in parse tree.
1680 ;; This is required since buffer expansion might have modified
1681 ;; boundaries of footnote definitions contained in the parse tree.
1682 ;; This way, definitions in `footnote-definition-alist' are bound to
1683 ;; match those in the parse tree.
1684 (let ((defs (plist-get info :footnote-definition-alist)))
1685 (org-element-map
1686 data 'footnote-definition
1687 (lambda (fn)
1688 (push (cons (org-element-property :label fn)
1689 `(org-data nil ,@(org-element-contents fn)))
1690 defs)))
1691 (setq info (plist-put info :footnote-definition-alist defs)))
1692 ;; Properties order doesn't matter: get the rest of the tree
1693 ;; properties.
1694 (nconc
1695 `(:target-list
1696 ,(org-element-map
1697 data '(keyword target)
1698 (lambda (blob)
1699 (when (or (eq (org-element-type blob) 'target)
1700 (string= (org-element-property :key blob) "TARGET"))
1701 blob)) info)
1702 :headline-numbering ,(org-export--collect-headline-numbering data info)
1703 :exported-data ,(make-hash-table :test 'eq :size 4001))
1704 info))
1706 (defun org-export--get-min-level (data options)
1707 "Return minimum exportable headline's level in DATA.
1708 DATA is parsed tree as returned by `org-element-parse-buffer'.
1709 OPTIONS is a plist holding export options."
1710 (catch 'exit
1711 (let ((min-level 10000))
1712 (mapc
1713 (lambda (blob)
1714 (when (and (eq (org-element-type blob) 'headline)
1715 (not (memq blob (plist-get options :ignore-list))))
1716 (setq min-level
1717 (min (org-element-property :level blob) min-level)))
1718 (when (= min-level 1) (throw 'exit 1)))
1719 (org-element-contents data))
1720 ;; If no headline was found, for the sake of consistency, set
1721 ;; minimum level to 1 nonetheless.
1722 (if (= min-level 10000) 1 min-level))))
1724 (defun org-export--collect-headline-numbering (data options)
1725 "Return numbering of all exportable headlines in a parse tree.
1727 DATA is the parse tree. OPTIONS is the plist holding export
1728 options.
1730 Return an alist whose key is an headline and value is its
1731 associated numbering \(in the shape of a list of numbers\)."
1732 (let ((numbering (make-vector org-export-max-depth 0)))
1733 (org-element-map
1734 data
1735 'headline
1736 (lambda (headline)
1737 (let ((relative-level
1738 (1- (org-export-get-relative-level headline options))))
1739 (cons
1740 headline
1741 (loop for n across numbering
1742 for idx from 0 to org-export-max-depth
1743 when (< idx relative-level) collect n
1744 when (= idx relative-level) collect (aset numbering idx (1+ n))
1745 when (> idx relative-level) do (aset numbering idx 0)))))
1746 options)))
1748 (defun org-export--populate-ignore-list (data options)
1749 "Return list of elements and objects to ignore during export.
1750 DATA is the parse tree to traverse. OPTIONS is the plist holding
1751 export options."
1752 (let* (ignore
1753 walk-data
1754 ;; First find trees containing a select tag, if any.
1755 (selected (org-export--selected-trees data options))
1756 (walk-data
1757 (lambda (data)
1758 ;; Collect ignored elements or objects into IGNORE-LIST.
1759 (let ((type (org-element-type data)))
1760 (if (org-export--skip-p data options selected) (push data ignore)
1761 (if (and (eq type 'headline)
1762 (eq (plist-get options :with-archived-trees) 'headline)
1763 (org-element-property :archivedp data))
1764 ;; If headline is archived but tree below has
1765 ;; to be skipped, add it to ignore list.
1766 (mapc (lambda (e) (push e ignore))
1767 (org-element-contents data))
1768 ;; Move into secondary string, if any.
1769 (let ((sec-prop
1770 (cdr (assq type org-element-secondary-value-alist))))
1771 (when sec-prop
1772 (mapc walk-data (org-element-property sec-prop data))))
1773 ;; Move into recursive objects/elements.
1774 (mapc walk-data (org-element-contents data))))))))
1775 ;; Main call.
1776 (funcall walk-data data)
1777 ;; Return value.
1778 ignore))
1780 (defun org-export--selected-trees (data info)
1781 "Return list of headlines containing a select tag in their tree.
1782 DATA is parsed data as returned by `org-element-parse-buffer'.
1783 INFO is a plist holding export options."
1784 (let* (selected-trees
1785 walk-data ; for byte-compiler.
1786 (walk-data
1787 (function
1788 (lambda (data genealogy)
1789 (case (org-element-type data)
1790 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1791 (org-element-contents data)))
1792 (headline
1793 (let ((tags (org-element-property :tags data)))
1794 (if (loop for tag in (plist-get info :select-tags)
1795 thereis (member tag tags))
1796 ;; When a select tag is found, mark full
1797 ;; genealogy and every headline within the tree
1798 ;; as acceptable.
1799 (setq selected-trees
1800 (append
1801 genealogy
1802 (org-element-map data 'headline 'identity)
1803 selected-trees))
1804 ;; Else, continue searching in tree, recursively.
1805 (mapc
1806 (lambda (el) (funcall walk-data el (cons data genealogy)))
1807 (org-element-contents data))))))))))
1808 (funcall walk-data data nil) selected-trees))
1810 (defun org-export--skip-p (blob options selected)
1811 "Non-nil when element or object BLOB should be skipped during export.
1812 OPTIONS is the plist holding export options. SELECTED, when
1813 non-nil, is a list of headlines belonging to a tree with a select
1814 tag."
1815 (case (org-element-type blob)
1816 (clock (not (plist-get options :with-clocks)))
1817 (drawer
1818 (or (not (plist-get options :with-drawers))
1819 (and (consp (plist-get options :with-drawers))
1820 (not (member (org-element-property :drawer-name blob)
1821 (plist-get options :with-drawers))))))
1822 (headline
1823 (let ((with-tasks (plist-get options :with-tasks))
1824 (todo (org-element-property :todo-keyword blob))
1825 (todo-type (org-element-property :todo-type blob))
1826 (archived (plist-get options :with-archived-trees))
1827 (tags (org-element-property :tags blob)))
1829 ;; Ignore subtrees with an exclude tag.
1830 (loop for k in (plist-get options :exclude-tags)
1831 thereis (member k tags))
1832 ;; When a select tag is present in the buffer, ignore any tree
1833 ;; without it.
1834 (and selected (not (memq blob selected)))
1835 ;; Ignore commented sub-trees.
1836 (org-element-property :commentedp blob)
1837 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1838 (and (not archived) (org-element-property :archivedp blob))
1839 ;; Ignore tasks, if specified by `:with-tasks' property.
1840 (and todo
1841 (or (not with-tasks)
1842 (and (memq with-tasks '(todo done))
1843 (not (eq todo-type with-tasks)))
1844 (and (consp with-tasks) (not (member todo with-tasks))))))))
1845 (inlinetask (not (plist-get options :with-inlinetasks)))
1846 (planning (not (plist-get options :with-plannings)))
1847 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1848 (table-cell
1849 (and (org-export-table-has-special-column-p
1850 (org-export-get-parent-table blob))
1851 (not (org-export-get-previous-element blob options))))
1852 (table-row (org-export-table-row-is-special-p blob options))
1853 (timestamp
1854 (case (plist-get options :with-timestamps)
1855 ;; No timestamp allowed.
1856 ('nil t)
1857 ;; Only active timestamps allowed and the current one isn't
1858 ;; active.
1859 (active
1860 (not (memq (org-element-property :type blob)
1861 '(active active-range))))
1862 ;; Only inactive timestamps allowed and the current one isn't
1863 ;; inactive.
1864 (inactive
1865 (not (memq (org-element-property :type blob)
1866 '(inactive inactive-range))))))))
1870 ;;; The Transcoder
1872 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1873 ;; `org-element-parse-buffer') and transcodes it into a specified
1874 ;; back-end output. It takes care of filtering out elements or
1875 ;; objects according to export options and organizing the output blank
1876 ;; lines and white space are preserved. The function memoizes its
1877 ;; results, so it is cheap to call it within translators.
1879 ;; Internally, three functions handle the filtering of objects and
1880 ;; elements during the export. In particular,
1881 ;; `org-export-ignore-element' marks an element or object so future
1882 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1883 ;; elements or objects should be seen as real Org syntax and
1884 ;; `org-export-expand' transforms the others back into their original
1885 ;; shape
1887 ;; `org-export-transcoder' is an accessor returning appropriate
1888 ;; translator function for a given element or object.
1890 (defun org-export-transcoder (blob info)
1891 "Return appropriate transcoder for BLOB.
1892 INFO is a plist containing export directives."
1893 (let ((type (org-element-type blob)))
1894 ;; Return contents only for complete parse trees.
1895 (if (eq type 'org-data) (lambda (blob contents info) contents)
1896 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1897 (and (functionp transcoder) transcoder)))))
1899 (defun org-export-data (data info)
1900 "Convert DATA into current back-end format.
1902 DATA is a parse tree, an element or an object or a secondary
1903 string. INFO is a plist holding export options.
1905 Return transcoded string."
1906 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1907 (if (not (eq memo 'no-memo)) memo
1908 (let* ((type (org-element-type data))
1909 (results
1910 (cond
1911 ;; Ignored element/object.
1912 ((memq data (plist-get info :ignore-list)) nil)
1913 ;; Plain text. All residual text properties from parse
1914 ;; tree (i.e. `:parent' property) are removed.
1915 ((eq type 'plain-text)
1916 (org-no-properties
1917 (org-export-filter-apply-functions
1918 (plist-get info :filter-plain-text)
1919 (let ((transcoder (org-export-transcoder data info)))
1920 (if transcoder (funcall transcoder data info) data))
1921 info)))
1922 ;; Uninterpreted element/object: change it back to Org
1923 ;; syntax and export again resulting raw string.
1924 ((not (org-export--interpret-p data info))
1925 (org-export-data
1926 (org-export-expand
1927 data
1928 (mapconcat (lambda (blob) (org-export-data blob info))
1929 (org-element-contents data)
1930 ""))
1931 info))
1932 ;; Secondary string.
1933 ((not type)
1934 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1935 ;; Element/Object without contents or, as a special case,
1936 ;; headline with archive tag and archived trees restricted
1937 ;; to title only.
1938 ((or (not (org-element-contents data))
1939 (and (eq type 'headline)
1940 (eq (plist-get info :with-archived-trees) 'headline)
1941 (org-element-property :archivedp data)))
1942 (let ((transcoder (org-export-transcoder data info)))
1943 (and (functionp transcoder)
1944 (funcall transcoder data nil info))))
1945 ;; Element/Object with contents.
1947 (let ((transcoder (org-export-transcoder data info)))
1948 (when transcoder
1949 (let* ((greaterp (memq type org-element-greater-elements))
1950 (objectp
1951 (and (not greaterp)
1952 (memq type org-element-recursive-objects)))
1953 (contents
1954 (mapconcat
1955 (lambda (element) (org-export-data element info))
1956 (org-element-contents
1957 (if (or greaterp objectp) data
1958 ;; Elements directly containing objects
1959 ;; must have their indentation normalized
1960 ;; first.
1961 (org-element-normalize-contents
1962 data
1963 ;; When normalizing contents of the first
1964 ;; paragraph in an item or a footnote
1965 ;; definition, ignore first line's
1966 ;; indentation: there is none and it
1967 ;; might be misleading.
1968 (when (eq type 'paragraph)
1969 (let ((parent (org-export-get-parent data)))
1970 (and
1971 (eq (car (org-element-contents parent))
1972 data)
1973 (memq (org-element-type parent)
1974 '(footnote-definition item))))))))
1975 "")))
1976 (funcall transcoder data
1977 (if (not greaterp) contents
1978 (org-element-normalize-string contents))
1979 info))))))))
1980 ;; Final result will be memoized before being returned.
1981 (puthash
1982 data
1983 (cond
1984 ((not results) nil)
1985 ((memq type '(org-data plain-text nil)) results)
1986 ;; Append the same white space between elements or objects as in
1987 ;; the original buffer, and call appropriate filters.
1989 (let ((results
1990 (org-export-filter-apply-functions
1991 (plist-get info (intern (format ":filter-%s" type)))
1992 (let ((post-blank (or (org-element-property :post-blank data)
1993 0)))
1994 (if (memq type org-element-all-elements)
1995 (concat (org-element-normalize-string results)
1996 (make-string post-blank ?\n))
1997 (concat results (make-string post-blank ? ))))
1998 info)))
1999 results)))
2000 (plist-get info :exported-data))))))
2002 (defun org-export--interpret-p (blob info)
2003 "Non-nil if element or object BLOB should be interpreted as Org syntax.
2004 Check is done according to export options INFO, stored as
2005 a plist."
2006 (case (org-element-type blob)
2007 ;; ... entities...
2008 (entity (plist-get info :with-entities))
2009 ;; ... emphasis...
2010 (emphasis (plist-get info :with-emphasize))
2011 ;; ... fixed-width areas.
2012 (fixed-width (plist-get info :with-fixed-width))
2013 ;; ... footnotes...
2014 ((footnote-definition footnote-reference)
2015 (plist-get info :with-footnotes))
2016 ;; ... sub/superscripts...
2017 ((subscript superscript)
2018 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2019 (if (eq sub/super-p '{})
2020 (org-element-property :use-brackets-p blob)
2021 sub/super-p)))
2022 ;; ... tables...
2023 (table (plist-get info :with-tables))
2024 (otherwise t)))
2026 (defun org-export-expand (blob contents)
2027 "Expand a parsed element or object to its original state.
2028 BLOB is either an element or an object. CONTENTS is its
2029 contents, as a string or nil."
2030 (funcall
2031 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2032 blob contents))
2034 (defun org-export-ignore-element (element info)
2035 "Add ELEMENT to `:ignore-list' in INFO.
2037 Any element in `:ignore-list' will be skipped when using
2038 `org-element-map'. INFO is modified by side effects."
2039 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2043 ;;; The Filter System
2045 ;; Filters allow end-users to tweak easily the transcoded output.
2046 ;; They are the functional counterpart of hooks, as every filter in
2047 ;; a set is applied to the return value of the previous one.
2049 ;; Every set is back-end agnostic. Although, a filter is always
2050 ;; called, in addition to the string it applies to, with the back-end
2051 ;; used as argument, so it's easy for the end-user to add back-end
2052 ;; specific filters in the set. The communication channel, as
2053 ;; a plist, is required as the third argument.
2055 ;; From the developer side, filters sets can be installed in the
2056 ;; process with the help of `org-export-define-backend', which
2057 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2058 ;; association has a key among the following symbols and a function or
2059 ;; a list of functions as value.
2061 ;; - `:filter-parse-tree' applies directly on the complete parsed
2062 ;; tree. It's the only filters set that doesn't apply to a string.
2063 ;; Users can set it through `org-export-filter-parse-tree-functions'
2064 ;; variable.
2066 ;; - `:filter-final-output' applies to the final transcoded string.
2067 ;; Users can set it with `org-export-filter-final-output-functions'
2068 ;; variable
2070 ;; - `:filter-plain-text' applies to any string not recognized as Org
2071 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2072 ;; configure it.
2074 ;; - `:filter-TYPE' applies on the string returned after an element or
2075 ;; object of type TYPE has been transcoded. An user can modify
2076 ;; `org-export-filter-TYPE-functions'
2078 ;; All filters sets are applied with
2079 ;; `org-export-filter-apply-functions' function. Filters in a set are
2080 ;; applied in a LIFO fashion. It allows developers to be sure that
2081 ;; their filters will be applied first.
2083 ;; Filters properties are installed in communication channel with
2084 ;; `org-export-install-filters' function.
2086 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2087 ;; `org-export-before-parsing-hook') are run at the beginning of the
2088 ;; export process and just before parsing to allow for heavy structure
2089 ;; modifications.
2092 ;;;; Hooks
2094 (defvar org-export-before-processing-hook nil
2095 "Hook run at the beginning of the export process.
2097 This is run before include keywords and macros are expanded and
2098 Babel code blocks executed, on a copy of the original buffer
2099 being exported. Visibility and narrowing are preserved. Point
2100 is at the beginning of the buffer.
2102 Every function in this hook will be called with one argument: the
2103 back-end currently used, as a symbol.")
2105 (defvar org-export-before-parsing-hook nil
2106 "Hook run before parsing an export buffer.
2108 This is run after include keywords and macros have been expanded
2109 and Babel code blocks executed, on a copy of the original buffer
2110 being exported. Visibility and narrowing are preserved. Point
2111 is at the beginning of the buffer.
2113 Every function in this hook will be called with one argument: the
2114 back-end currently used, as a symbol.")
2117 ;;;; Special Filters
2119 (defvar org-export-filter-parse-tree-functions nil
2120 "List of functions applied to the parsed tree.
2121 Each filter is called with three arguments: the parse tree, as
2122 returned by `org-element-parse-buffer', the back-end, as
2123 a symbol, and the communication channel, as a plist. It must
2124 return the modified parse tree to transcode.")
2126 (defvar org-export-filter-final-output-functions nil
2127 "List of functions applied to the transcoded string.
2128 Each filter is called with three arguments: the full transcoded
2129 string, the back-end, as a symbol, and the communication channel,
2130 as a plist. It must return a string that will be used as the
2131 final export output.")
2133 (defvar org-export-filter-plain-text-functions nil
2134 "List of functions applied to plain text.
2135 Each filter is called with three arguments: a string which
2136 contains no Org syntax, the back-end, as a symbol, and the
2137 communication channel, as a plist. It must return a string or
2138 nil.")
2141 ;;;; Elements Filters
2143 (defvar org-export-filter-babel-call-functions nil
2144 "List of functions applied to a transcoded babel-call.
2145 Each filter is called with three arguments: the transcoded data,
2146 as a string, the back-end, as a symbol, and the communication
2147 channel, as a plist. It must return a string or nil.")
2149 (defvar org-export-filter-center-block-functions nil
2150 "List of functions applied to a transcoded center block.
2151 Each filter is called with three arguments: the transcoded data,
2152 as a string, the back-end, as a symbol, and the communication
2153 channel, as a plist. It must return a string or nil.")
2155 (defvar org-export-filter-clock-functions nil
2156 "List of functions applied to a transcoded clock.
2157 Each filter is called with three arguments: the transcoded data,
2158 as a string, the back-end, as a symbol, and the communication
2159 channel, as a plist. It must return a string or nil.")
2161 (defvar org-export-filter-comment-functions nil
2162 "List of functions applied to a transcoded comment.
2163 Each filter is called with three arguments: the transcoded data,
2164 as a string, the back-end, as a symbol, and the communication
2165 channel, as a plist. It must return a string or nil.")
2167 (defvar org-export-filter-comment-block-functions nil
2168 "List of functions applied to a transcoded comment-block.
2169 Each filter is called with three arguments: the transcoded data,
2170 as a string, the back-end, as a symbol, and the communication
2171 channel, as a plist. It must return a string or nil.")
2173 (defvar org-export-filter-diary-sexp-functions nil
2174 "List of functions applied to a transcoded diary-sexp.
2175 Each filter is called with three arguments: the transcoded data,
2176 as a string, the back-end, as a symbol, and the communication
2177 channel, as a plist. It must return a string or nil.")
2179 (defvar org-export-filter-drawer-functions nil
2180 "List of functions applied to a transcoded drawer.
2181 Each filter is called with three arguments: the transcoded data,
2182 as a string, the back-end, as a symbol, and the communication
2183 channel, as a plist. It must return a string or nil.")
2185 (defvar org-export-filter-dynamic-block-functions nil
2186 "List of functions applied to a transcoded dynamic-block.
2187 Each filter is called with three arguments: the transcoded data,
2188 as a string, the back-end, as a symbol, and the communication
2189 channel, as a plist. It must return a string or nil.")
2191 (defvar org-export-filter-example-block-functions nil
2192 "List of functions applied to a transcoded example-block.
2193 Each filter is called with three arguments: the transcoded data,
2194 as a string, the back-end, as a symbol, and the communication
2195 channel, as a plist. It must return a string or nil.")
2197 (defvar org-export-filter-export-block-functions nil
2198 "List of functions applied to a transcoded export-block.
2199 Each filter is called with three arguments: the transcoded data,
2200 as a string, the back-end, as a symbol, and the communication
2201 channel, as a plist. It must return a string or nil.")
2203 (defvar org-export-filter-fixed-width-functions nil
2204 "List of functions applied to a transcoded fixed-width.
2205 Each filter is called with three arguments: the transcoded data,
2206 as a string, the back-end, as a symbol, and the communication
2207 channel, as a plist. It must return a string or nil.")
2209 (defvar org-export-filter-footnote-definition-functions nil
2210 "List of functions applied to a transcoded footnote-definition.
2211 Each filter is called with three arguments: the transcoded data,
2212 as a string, the back-end, as a symbol, and the communication
2213 channel, as a plist. It must return a string or nil.")
2215 (defvar org-export-filter-headline-functions nil
2216 "List of functions applied to a transcoded headline.
2217 Each filter is called with three arguments: the transcoded data,
2218 as a string, the back-end, as a symbol, and the communication
2219 channel, as a plist. It must return a string or nil.")
2221 (defvar org-export-filter-horizontal-rule-functions nil
2222 "List of functions applied to a transcoded horizontal-rule.
2223 Each filter is called with three arguments: the transcoded data,
2224 as a string, the back-end, as a symbol, and the communication
2225 channel, as a plist. It must return a string or nil.")
2227 (defvar org-export-filter-inlinetask-functions nil
2228 "List of functions applied to a transcoded inlinetask.
2229 Each filter is called with three arguments: the transcoded data,
2230 as a string, the back-end, as a symbol, and the communication
2231 channel, as a plist. It must return a string or nil.")
2233 (defvar org-export-filter-item-functions nil
2234 "List of functions applied to a transcoded item.
2235 Each filter is called with three arguments: the transcoded data,
2236 as a string, the back-end, as a symbol, and the communication
2237 channel, as a plist. It must return a string or nil.")
2239 (defvar org-export-filter-keyword-functions nil
2240 "List of functions applied to a transcoded keyword.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2245 (defvar org-export-filter-latex-environment-functions nil
2246 "List of functions applied to a transcoded latex-environment.
2247 Each filter is called with three arguments: the transcoded data,
2248 as a string, the back-end, as a symbol, and the communication
2249 channel, as a plist. It must return a string or nil.")
2251 (defvar org-export-filter-node-property-functions nil
2252 "List of functions applied to a transcoded node-property.
2253 Each filter is called with three arguments: the transcoded data,
2254 as a string, the back-end, as a symbol, and the communication
2255 channel, as a plist. It must return a string or nil.")
2257 (defvar org-export-filter-paragraph-functions nil
2258 "List of functions applied to a transcoded paragraph.
2259 Each filter is called with three arguments: the transcoded data,
2260 as a string, the back-end, as a symbol, and the communication
2261 channel, as a plist. It must return a string or nil.")
2263 (defvar org-export-filter-plain-list-functions nil
2264 "List of functions applied to a transcoded plain-list.
2265 Each filter is called with three arguments: the transcoded data,
2266 as a string, the back-end, as a symbol, and the communication
2267 channel, as a plist. It must return a string or nil.")
2269 (defvar org-export-filter-planning-functions nil
2270 "List of functions applied to a transcoded planning.
2271 Each filter is called with three arguments: the transcoded data,
2272 as a string, the back-end, as a symbol, and the communication
2273 channel, as a plist. It must return a string or nil.")
2275 (defvar org-export-filter-property-drawer-functions nil
2276 "List of functions applied to a transcoded property-drawer.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-quote-block-functions nil
2282 "List of functions applied to a transcoded quote block.
2283 Each filter is called with three arguments: the transcoded quote
2284 data, as a string, the back-end, as a symbol, and the
2285 communication channel, as a plist. It must return a string or
2286 nil.")
2288 (defvar org-export-filter-quote-section-functions nil
2289 "List of functions applied to a transcoded quote-section.
2290 Each filter is called with three arguments: the transcoded data,
2291 as a string, the back-end, as a symbol, and the communication
2292 channel, as a plist. It must return a string or nil.")
2294 (defvar org-export-filter-section-functions nil
2295 "List of functions applied to a transcoded section.
2296 Each filter is called with three arguments: the transcoded data,
2297 as a string, the back-end, as a symbol, and the communication
2298 channel, as a plist. It must return a string or nil.")
2300 (defvar org-export-filter-special-block-functions nil
2301 "List of functions applied to a transcoded special block.
2302 Each filter is called with three arguments: the transcoded data,
2303 as a string, the back-end, as a symbol, and the communication
2304 channel, as a plist. It must return a string or nil.")
2306 (defvar org-export-filter-src-block-functions nil
2307 "List of functions applied to a transcoded src-block.
2308 Each filter is called with three arguments: the transcoded data,
2309 as a string, the back-end, as a symbol, and the communication
2310 channel, as a plist. It must return a string or nil.")
2312 (defvar org-export-filter-table-functions nil
2313 "List of functions applied to a transcoded table.
2314 Each filter is called with three arguments: the transcoded data,
2315 as a string, the back-end, as a symbol, and the communication
2316 channel, as a plist. It must return a string or nil.")
2318 (defvar org-export-filter-table-cell-functions nil
2319 "List of functions applied to a transcoded table-cell.
2320 Each filter is called with three arguments: the transcoded data,
2321 as a string, the back-end, as a symbol, and the communication
2322 channel, as a plist. It must return a string or nil.")
2324 (defvar org-export-filter-table-row-functions nil
2325 "List of functions applied to a transcoded table-row.
2326 Each filter is called with three arguments: the transcoded data,
2327 as a string, the back-end, as a symbol, and the communication
2328 channel, as a plist. It must return a string or nil.")
2330 (defvar org-export-filter-verse-block-functions nil
2331 "List of functions applied to a transcoded verse block.
2332 Each filter is called with three arguments: the transcoded data,
2333 as a string, the back-end, as a symbol, and the communication
2334 channel, as a plist. It must return a string or nil.")
2337 ;;;; Objects Filters
2339 (defvar org-export-filter-bold-functions nil
2340 "List of functions applied to transcoded bold text.
2341 Each filter is called with three arguments: the transcoded data,
2342 as a string, the back-end, as a symbol, and the communication
2343 channel, as a plist. It must return a string or nil.")
2345 (defvar org-export-filter-code-functions nil
2346 "List of functions applied to transcoded code text.
2347 Each filter is called with three arguments: the transcoded data,
2348 as a string, the back-end, as a symbol, and the communication
2349 channel, as a plist. It must return a string or nil.")
2351 (defvar org-export-filter-entity-functions nil
2352 "List of functions applied to a transcoded entity.
2353 Each filter is called with three arguments: the transcoded data,
2354 as a string, the back-end, as a symbol, and the communication
2355 channel, as a plist. It must return a string or nil.")
2357 (defvar org-export-filter-export-snippet-functions nil
2358 "List of functions applied to a transcoded export-snippet.
2359 Each filter is called with three arguments: the transcoded data,
2360 as a string, the back-end, as a symbol, and the communication
2361 channel, as a plist. It must return a string or nil.")
2363 (defvar org-export-filter-footnote-reference-functions nil
2364 "List of functions applied to a transcoded footnote-reference.
2365 Each filter is called with three arguments: the transcoded data,
2366 as a string, the back-end, as a symbol, and the communication
2367 channel, as a plist. It must return a string or nil.")
2369 (defvar org-export-filter-inline-babel-call-functions nil
2370 "List of functions applied to a transcoded inline-babel-call.
2371 Each filter is called with three arguments: the transcoded data,
2372 as a string, the back-end, as a symbol, and the communication
2373 channel, as a plist. It must return a string or nil.")
2375 (defvar org-export-filter-inline-src-block-functions nil
2376 "List of functions applied to a transcoded inline-src-block.
2377 Each filter is called with three arguments: the transcoded data,
2378 as a string, the back-end, as a symbol, and the communication
2379 channel, as a plist. It must return a string or nil.")
2381 (defvar org-export-filter-italic-functions nil
2382 "List of functions applied to transcoded italic text.
2383 Each filter is called with three arguments: the transcoded data,
2384 as a string, the back-end, as a symbol, and the communication
2385 channel, as a plist. It must return a string or nil.")
2387 (defvar org-export-filter-latex-fragment-functions nil
2388 "List of functions applied to a transcoded latex-fragment.
2389 Each filter is called with three arguments: the transcoded data,
2390 as a string, the back-end, as a symbol, and the communication
2391 channel, as a plist. It must return a string or nil.")
2393 (defvar org-export-filter-line-break-functions nil
2394 "List of functions applied to a transcoded line-break.
2395 Each filter is called with three arguments: the transcoded data,
2396 as a string, the back-end, as a symbol, and the communication
2397 channel, as a plist. It must return a string or nil.")
2399 (defvar org-export-filter-link-functions nil
2400 "List of functions applied to a transcoded link.
2401 Each filter is called with three arguments: the transcoded data,
2402 as a string, the back-end, as a symbol, and the communication
2403 channel, as a plist. It must return a string or nil.")
2405 (defvar org-export-filter-macro-functions nil
2406 "List of functions applied to a transcoded macro.
2407 Each filter is called with three arguments: the transcoded data,
2408 as a string, the back-end, as a symbol, and the communication
2409 channel, as a plist. It must return a string or nil.")
2411 (defvar org-export-filter-radio-target-functions nil
2412 "List of functions applied to a transcoded radio-target.
2413 Each filter is called with three arguments: the transcoded data,
2414 as a string, the back-end, as a symbol, and the communication
2415 channel, as a plist. It must return a string or nil.")
2417 (defvar org-export-filter-statistics-cookie-functions nil
2418 "List of functions applied to a transcoded statistics-cookie.
2419 Each filter is called with three arguments: the transcoded data,
2420 as a string, the back-end, as a symbol, and the communication
2421 channel, as a plist. It must return a string or nil.")
2423 (defvar org-export-filter-strike-through-functions nil
2424 "List of functions applied to transcoded strike-through text.
2425 Each filter is called with three arguments: the transcoded data,
2426 as a string, the back-end, as a symbol, and the communication
2427 channel, as a plist. It must return a string or nil.")
2429 (defvar org-export-filter-subscript-functions nil
2430 "List of functions applied to a transcoded subscript.
2431 Each filter is called with three arguments: the transcoded data,
2432 as a string, the back-end, as a symbol, and the communication
2433 channel, as a plist. It must return a string or nil.")
2435 (defvar org-export-filter-superscript-functions nil
2436 "List of functions applied to a transcoded superscript.
2437 Each filter is called with three arguments: the transcoded data,
2438 as a string, the back-end, as a symbol, and the communication
2439 channel, as a plist. It must return a string or nil.")
2441 (defvar org-export-filter-target-functions nil
2442 "List of functions applied to a transcoded target.
2443 Each filter is called with three arguments: the transcoded data,
2444 as a string, the back-end, as a symbol, and the communication
2445 channel, as a plist. It must return a string or nil.")
2447 (defvar org-export-filter-timestamp-functions nil
2448 "List of functions applied to a transcoded timestamp.
2449 Each filter is called with three arguments: the transcoded data,
2450 as a string, the back-end, as a symbol, and the communication
2451 channel, as a plist. It must return a string or nil.")
2453 (defvar org-export-filter-underline-functions nil
2454 "List of functions applied to transcoded underline text.
2455 Each filter is called with three arguments: the transcoded data,
2456 as a string, the back-end, as a symbol, and the communication
2457 channel, as a plist. It must return a string or nil.")
2459 (defvar org-export-filter-verbatim-functions nil
2460 "List of functions applied to transcoded verbatim text.
2461 Each filter is called with three arguments: the transcoded data,
2462 as a string, the back-end, as a symbol, and the communication
2463 channel, as a plist. It must return a string or nil.")
2466 ;;;; Filters Tools
2468 ;; Internal function `org-export-install-filters' installs filters
2469 ;; hard-coded in back-ends (developer filters) and filters from global
2470 ;; variables (user filters) in the communication channel.
2472 ;; Internal function `org-export-filter-apply-functions' takes care
2473 ;; about applying each filter in order to a given data. It ignores
2474 ;; filters returning a nil value but stops whenever a filter returns
2475 ;; an empty string.
2477 (defun org-export-filter-apply-functions (filters value info)
2478 "Call every function in FILTERS.
2480 Functions are called with arguments VALUE, current export
2481 back-end and INFO. A function returning a nil value will be
2482 skipped. If it returns the empty string, the process ends and
2483 VALUE is ignored.
2485 Call is done in a LIFO fashion, to be sure that developer
2486 specified filters, if any, are called first."
2487 (catch 'exit
2488 (dolist (filter filters value)
2489 (let ((result (funcall filter value (plist-get info :back-end) info)))
2490 (cond ((not result) value)
2491 ((equal value "") (throw 'exit nil))
2492 (t (setq value result)))))))
2494 (defun org-export-install-filters (info)
2495 "Install filters properties in communication channel.
2497 INFO is a plist containing the current communication channel.
2499 Return the updated communication channel."
2500 (let (plist)
2501 ;; Install user defined filters with `org-export-filters-alist'.
2502 (mapc (lambda (p)
2503 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2504 org-export-filters-alist)
2505 ;; Prepend back-end specific filters to that list.
2506 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2507 (plist-get info :back-end)))))
2508 (when (boundp back-end-filters)
2509 (mapc (lambda (p)
2510 ;; Single values get consed, lists are prepended.
2511 (let ((key (car p)) (value (cdr p)))
2512 (when value
2513 (setq plist
2514 (plist-put
2515 plist key
2516 (if (atom value) (cons value (plist-get plist key))
2517 (append value (plist-get plist key))))))))
2518 (eval back-end-filters))))
2519 ;; Return new communication channel.
2520 (org-combine-plists info plist)))
2524 ;;; Core functions
2526 ;; This is the room for the main function, `org-export-as', along with
2527 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2528 ;; They differ only by the way they output the resulting code.
2530 ;; `org-export-output-file-name' is an auxiliary function meant to be
2531 ;; used with `org-export-to-file'. With a given extension, it tries
2532 ;; to provide a canonical file name to write export output to.
2534 ;; Note that `org-export-as' doesn't really parse the current buffer,
2535 ;; but a copy of it (with the same buffer-local variables and
2536 ;; visibility), where macros and include keywords are expanded and
2537 ;; Babel blocks are executed, if appropriate.
2538 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2540 ;; File inclusion is taken care of by
2541 ;; `org-export-expand-include-keyword' and
2542 ;; `org-export--prepare-file-contents'. Structure wise, including
2543 ;; a whole Org file in a buffer often makes little sense. For
2544 ;; example, if the file contains an headline and the include keyword
2545 ;; was within an item, the item should contain the headline. That's
2546 ;; why file inclusion should be done before any structure can be
2547 ;; associated to the file, that is before parsing.
2549 ;; Macro are expanded with `org-export-expand-macro'.
2551 (defun org-export-as
2552 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2553 "Transcode current Org buffer into BACKEND code.
2555 If narrowing is active in the current buffer, only transcode its
2556 narrowed part.
2558 If a region is active, transcode that region.
2560 When optional argument SUBTREEP is non-nil, transcode the
2561 sub-tree at point, extracting information from the headline
2562 properties first.
2564 When optional argument VISIBLE-ONLY is non-nil, don't export
2565 contents of hidden elements.
2567 When optional argument BODY-ONLY is non-nil, only return body
2568 code, without preamble nor postamble.
2570 Optional argument EXT-PLIST, when provided, is a property list
2571 with external parameters overriding Org default settings, but
2572 still inferior to file-local settings.
2574 Optional argument NOEXPAND, when non-nil, prevents included files
2575 to be expanded and Babel code to be executed.
2577 Return code as a string."
2578 ;; Barf if BACKEND isn't registered.
2579 (org-export-barf-if-invalid-backend backend)
2580 (save-excursion
2581 (save-restriction
2582 ;; Narrow buffer to an appropriate region or subtree for
2583 ;; parsing. If parsing subtree, be sure to remove main headline
2584 ;; too.
2585 (cond ((org-region-active-p)
2586 (narrow-to-region (region-beginning) (region-end)))
2587 (subtreep
2588 (org-narrow-to-subtree)
2589 (goto-char (point-min))
2590 (forward-line)
2591 (narrow-to-region (point) (point-max))))
2592 ;; Initialize communication channel with original buffer
2593 ;; attributes, unavailable in its copy.
2594 (let ((info (org-export--get-buffer-attributes)) tree)
2595 (org-export-with-current-buffer-copy
2596 ;; Run first hook with current back-end as argument.
2597 (run-hook-with-args 'org-export-before-processing-hook backend)
2598 ;; Update communication channel and get parse tree. Buffer
2599 ;; isn't parsed directly. Instead, a temporary copy is
2600 ;; created, where include keywords, macros are expanded and
2601 ;; code blocks are evaluated.
2602 (unless noexpand
2603 (org-export-expand-include-keyword)
2604 ;; Update macro templates since #+INCLUDE keywords might
2605 ;; have added some new ones.
2606 (org-macro-initialize-templates)
2607 (org-macro-replace-all org-macro-templates)
2608 ;; TODO: Setting `org-current-export-file' is required by
2609 ;; Org Babel to properly resolve noweb references. Once
2610 ;; "org-exp.el" is removed, modify
2611 ;; `org-export-blocks-preprocess' so it accepts the value
2612 ;; as an argument instead.
2613 (let ((org-current-export-file (current-buffer)))
2614 (org-export-blocks-preprocess)))
2615 ;; Update radio targets since keyword inclusion might have
2616 ;; added some more.
2617 (org-update-radio-target-regexp)
2618 ;; Run last hook with current back-end as argument.
2619 (goto-char (point-min))
2620 (run-hook-with-args 'org-export-before-parsing-hook backend)
2621 ;; Update communication channel with environment. Also
2622 ;; install user's and developer's filters.
2623 (setq info
2624 (org-export-install-filters
2625 (org-combine-plists
2626 info (org-export-get-environment backend subtreep ext-plist))))
2627 ;; Expand export-specific set of macros: {{{author}}},
2628 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2629 ;; once regular macros have been expanded, since document
2630 ;; keywords may contain one of them.
2631 (unless noexpand
2632 (org-macro-replace-all
2633 (list (cons "author"
2634 (org-element-interpret-data (plist-get info :author)))
2635 (cons "date"
2636 (org-element-interpret-data (plist-get info :date)))
2637 ;; EMAIL is not a parsed keyword: store it as-is.
2638 (cons "email" (or (plist-get info :email) ""))
2639 (cons "title"
2640 (org-element-interpret-data (plist-get info :title))))))
2641 ;; Eventually parse buffer. Call parse-tree filters to get
2642 ;; the final tree.
2643 (setq tree
2644 (org-export-filter-apply-functions
2645 (plist-get info :filter-parse-tree)
2646 (org-element-parse-buffer nil visible-only) info)))
2647 ;; Now tree is complete, compute its properties and add them
2648 ;; to communication channel.
2649 (setq info
2650 (org-combine-plists
2651 info (org-export-collect-tree-properties tree info)))
2652 ;; Eventually transcode TREE. Wrap the resulting string into
2653 ;; a template, if required. Finally call final-output filter.
2654 (let* ((body (org-element-normalize-string
2655 (or (org-export-data tree info) "")))
2656 (template (cdr (assq 'template
2657 (plist-get info :translate-alist))))
2658 (output (org-export-filter-apply-functions
2659 (plist-get info :filter-final-output)
2660 (if (or (not (functionp template)) body-only) body
2661 (funcall template body info))
2662 info)))
2663 ;; Maybe add final OUTPUT to kill ring, then return it.
2664 (when (and org-export-copy-to-kill-ring (org-string-nw-p output))
2665 (org-kill-new output))
2666 output)))))
2668 (defun org-export-to-buffer
2669 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2670 "Call `org-export-as' with output to a specified buffer.
2672 BACKEND is the back-end used for transcoding, as a symbol.
2674 BUFFER is the output buffer. If it already exists, it will be
2675 erased first, otherwise, it will be created.
2677 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2678 and NOEXPAND are similar to those used in `org-export-as', which
2679 see.
2681 Return buffer."
2682 (let ((out (org-export-as
2683 backend subtreep visible-only body-only ext-plist noexpand))
2684 (buffer (get-buffer-create buffer)))
2685 (with-current-buffer buffer
2686 (erase-buffer)
2687 (insert out)
2688 (goto-char (point-min)))
2689 buffer))
2691 (defun org-export-to-file
2692 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2693 "Call `org-export-as' with output to a specified file.
2695 BACKEND is the back-end used for transcoding, as a symbol. FILE
2696 is the name of the output file, as a string.
2698 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2699 and NOEXPAND are similar to those used in `org-export-as', which
2700 see.
2702 Return output file's name."
2703 ;; Checks for FILE permissions. `write-file' would do the same, but
2704 ;; we'd rather avoid needless transcoding of parse tree.
2705 (unless (file-writable-p file) (error "Output file not writable"))
2706 ;; Insert contents to a temporary buffer and write it to FILE.
2707 (let ((out (org-export-as
2708 backend subtreep visible-only body-only ext-plist noexpand)))
2709 (with-temp-buffer
2710 (insert out)
2711 (let ((coding-system-for-write org-export-coding-system))
2712 (write-file file))))
2713 ;; Return full path.
2714 file)
2716 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2717 "Return output file's name according to buffer specifications.
2719 EXTENSION is a string representing the output file extension,
2720 with the leading dot.
2722 With a non-nil optional argument SUBTREEP, try to determine
2723 output file's name by looking for \"EXPORT_FILE_NAME\" property
2724 of subtree at point.
2726 When optional argument PUB-DIR is set, use it as the publishing
2727 directory.
2729 When optional argument VISIBLE-ONLY is non-nil, don't export
2730 contents of hidden elements.
2732 Return file name as a string, or nil if it couldn't be
2733 determined."
2734 (let ((base-name
2735 ;; File name may come from EXPORT_FILE_NAME subtree property,
2736 ;; assuming point is at beginning of said sub-tree.
2737 (file-name-sans-extension
2738 (or (and subtreep
2739 (org-entry-get
2740 (save-excursion
2741 (ignore-errors (org-back-to-heading) (point)))
2742 "EXPORT_FILE_NAME" t))
2743 ;; File name may be extracted from buffer's associated
2744 ;; file, if any.
2745 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2746 (and visited-file (file-name-nondirectory visited-file)))
2747 ;; Can't determine file name on our own: Ask user.
2748 (let ((read-file-name-function
2749 (and org-completion-use-ido 'ido-read-file-name)))
2750 (read-file-name
2751 "Output file: " pub-dir nil nil nil
2752 (lambda (name)
2753 (string= (file-name-extension name t) extension))))))))
2754 ;; Build file name. Enforce EXTENSION over whatever user may have
2755 ;; come up with. PUB-DIR, if defined, always has precedence over
2756 ;; any provided path.
2757 (cond
2758 (pub-dir
2759 (concat (file-name-as-directory pub-dir)
2760 (file-name-nondirectory base-name)
2761 extension))
2762 ((file-name-absolute-p base-name) (concat base-name extension))
2763 (t (concat (file-name-as-directory ".") base-name extension)))))
2765 (defmacro org-export-with-current-buffer-copy (&rest body)
2766 "Apply BODY in a copy of the current buffer.
2768 The copy preserves local variables and visibility of the original
2769 buffer.
2771 Point is at buffer's beginning when BODY is applied."
2772 (declare (debug (body)))
2773 (org-with-gensyms (original-buffer offset buffer-string overlays region)
2774 `(let* ((,original-buffer (current-buffer))
2775 (,region (list (point-min) (point-max)))
2776 (,buffer-string (org-with-wide-buffer (buffer-string)))
2777 (,overlays (mapcar 'copy-overlay (apply 'overlays-in ,region))))
2778 (with-temp-buffer
2779 (let ((buffer-invisibility-spec nil))
2780 (org-clone-local-variables
2781 ,original-buffer
2782 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2783 (insert ,buffer-string)
2784 (apply 'narrow-to-region ,region)
2785 (mapc (lambda (ov)
2786 (move-overlay
2787 ov (overlay-start ov) (overlay-end ov) (current-buffer)))
2788 ,overlays)
2789 (goto-char (point-min))
2790 (progn ,@body))))))
2792 (defun org-export-expand-include-keyword (&optional included dir)
2793 "Expand every include keyword in buffer.
2794 Optional argument INCLUDED is a list of included file names along
2795 with their line restriction, when appropriate. It is used to
2796 avoid infinite recursion. Optional argument DIR is the current
2797 working directory. It is used to properly resolve relative
2798 paths."
2799 (let ((case-fold-search t))
2800 (goto-char (point-min))
2801 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
2802 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2803 'keyword)
2804 (beginning-of-line)
2805 ;; Extract arguments from keyword's value.
2806 (let* ((value (match-string 1))
2807 (ind (org-get-indentation))
2808 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2809 (prog1 (expand-file-name (match-string 1 value) dir)
2810 (setq value (replace-match "" nil nil value)))))
2811 (lines
2812 (and (string-match
2813 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2814 (prog1 (match-string 1 value)
2815 (setq value (replace-match "" nil nil value)))))
2816 (env (cond ((string-match "\\<example\\>" value) 'example)
2817 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2818 (match-string 1 value))))
2819 ;; Minimal level of included file defaults to the child
2820 ;; level of the current headline, if any, or one. It
2821 ;; only applies is the file is meant to be included as
2822 ;; an Org one.
2823 (minlevel
2824 (and (not env)
2825 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2826 (prog1 (string-to-number (match-string 1 value))
2827 (setq value (replace-match "" nil nil value)))
2828 (let ((cur (org-current-level)))
2829 (if cur (1+ (org-reduced-level cur)) 1))))))
2830 ;; Remove keyword.
2831 (delete-region (point) (progn (forward-line) (point)))
2832 (cond
2833 ((not file) (error "Invalid syntax in INCLUDE keyword"))
2834 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2835 ;; Check if files has already been parsed. Look after
2836 ;; inclusion lines too, as different parts of the same file
2837 ;; can be included too.
2838 ((member (list file lines) included)
2839 (error "Recursive file inclusion: %s" file))
2841 (cond
2842 ((eq env 'example)
2843 (insert
2844 (let ((ind-str (make-string ind ? ))
2845 (contents
2846 (org-escape-code-in-string
2847 (org-export--prepare-file-contents file lines))))
2848 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2849 ind-str contents ind-str))))
2850 ((stringp env)
2851 (insert
2852 (let ((ind-str (make-string ind ? ))
2853 (contents
2854 (org-escape-code-in-string
2855 (org-export--prepare-file-contents file lines))))
2856 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2857 ind-str env contents ind-str))))
2859 (insert
2860 (with-temp-buffer
2861 (org-mode)
2862 (insert
2863 (org-export--prepare-file-contents file lines ind minlevel))
2864 (org-export-expand-include-keyword
2865 (cons (list file lines) included)
2866 (file-name-directory file))
2867 (buffer-string))))))))))))
2869 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2870 "Prepare the contents of FILE for inclusion and return them as a string.
2872 When optional argument LINES is a string specifying a range of
2873 lines, include only those lines.
2875 Optional argument IND, when non-nil, is an integer specifying the
2876 global indentation of returned contents. Since its purpose is to
2877 allow an included file to stay in the same environment it was
2878 created \(i.e. a list item), it doesn't apply past the first
2879 headline encountered.
2881 Optional argument MINLEVEL, when non-nil, is an integer
2882 specifying the level that any top-level headline in the included
2883 file should have."
2884 (with-temp-buffer
2885 (insert-file-contents file)
2886 (when lines
2887 (let* ((lines (split-string lines "-"))
2888 (lbeg (string-to-number (car lines)))
2889 (lend (string-to-number (cadr lines)))
2890 (beg (if (zerop lbeg) (point-min)
2891 (goto-char (point-min))
2892 (forward-line (1- lbeg))
2893 (point)))
2894 (end (if (zerop lend) (point-max)
2895 (goto-char (point-min))
2896 (forward-line (1- lend))
2897 (point))))
2898 (narrow-to-region beg end)))
2899 ;; Remove blank lines at beginning and end of contents. The logic
2900 ;; behind that removal is that blank lines around include keyword
2901 ;; override blank lines in included file.
2902 (goto-char (point-min))
2903 (org-skip-whitespace)
2904 (beginning-of-line)
2905 (delete-region (point-min) (point))
2906 (goto-char (point-max))
2907 (skip-chars-backward " \r\t\n")
2908 (forward-line)
2909 (delete-region (point) (point-max))
2910 ;; If IND is set, preserve indentation of include keyword until
2911 ;; the first headline encountered.
2912 (when ind
2913 (unless (eq major-mode 'org-mode) (org-mode))
2914 (goto-char (point-min))
2915 (let ((ind-str (make-string ind ? )))
2916 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2917 ;; Do not move footnote definitions out of column 0.
2918 (unless (and (looking-at org-footnote-definition-re)
2919 (eq (org-element-type (org-element-at-point))
2920 'footnote-definition))
2921 (insert ind-str))
2922 (forward-line))))
2923 ;; When MINLEVEL is specified, compute minimal level for headlines
2924 ;; in the file (CUR-MIN), and remove stars to each headline so
2925 ;; that headlines with minimal level have a level of MINLEVEL.
2926 (when minlevel
2927 (unless (eq major-mode 'org-mode) (org-mode))
2928 (org-with-limited-levels
2929 (let ((levels (org-map-entries
2930 (lambda () (org-reduced-level (org-current-level))))))
2931 (when levels
2932 (let ((offset (- minlevel (apply 'min levels))))
2933 (unless (zerop offset)
2934 (when org-odd-levels-only (setq offset (* offset 2)))
2935 ;; Only change stars, don't bother moving whole
2936 ;; sections.
2937 (org-map-entries
2938 (lambda () (if (< offset 0) (delete-char (abs offset))
2939 (insert (make-string offset ?*)))))))))))
2940 (org-element-normalize-string (buffer-string))))
2943 ;;; Tools For Back-Ends
2945 ;; A whole set of tools is available to help build new exporters. Any
2946 ;; function general enough to have its use across many back-ends
2947 ;; should be added here.
2949 ;;;; For Affiliated Keywords
2951 ;; `org-export-read-attribute' reads a property from a given element
2952 ;; as a plist. It can be used to normalize affiliated keywords'
2953 ;; syntax.
2955 ;; Since captions can span over multiple lines and accept dual values,
2956 ;; their internal representation is a bit tricky. Therefore,
2957 ;; `org-export-get-caption' transparently returns a given element's
2958 ;; caption as a secondary string.
2960 (defun org-export-read-attribute (attribute element &optional property)
2961 "Turn ATTRIBUTE property from ELEMENT into a plist.
2963 When optional argument PROPERTY is non-nil, return the value of
2964 that property within attributes.
2966 This function assumes attributes are defined as \":keyword
2967 value\" pairs. It is appropriate for `:attr_html' like
2968 properties."
2969 (let ((attributes
2970 (let ((value (org-element-property attribute element)))
2971 (and value
2972 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2973 (if property (plist-get attributes property) attributes)))
2975 (defun org-export-get-caption (element &optional shortp)
2976 "Return caption from ELEMENT as a secondary string.
2978 When optional argument SHORTP is non-nil, return short caption,
2979 as a secondary string, instead.
2981 Caption lines are separated by a white space."
2982 (let ((full-caption (org-element-property :caption element)) caption)
2983 (dolist (line full-caption (cdr caption))
2984 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2985 (when cap
2986 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2989 ;;;; For Export Snippets
2991 ;; Every export snippet is transmitted to the back-end. Though, the
2992 ;; latter will only retain one type of export-snippet, ignoring
2993 ;; others, based on the former's target back-end. The function
2994 ;; `org-export-snippet-backend' returns that back-end for a given
2995 ;; export-snippet.
2997 (defun org-export-snippet-backend (export-snippet)
2998 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2999 Translation, with `org-export-snippet-translation-alist', is
3000 applied."
3001 (let ((back-end (org-element-property :back-end export-snippet)))
3002 (intern
3003 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3004 back-end))))
3007 ;;;; For Footnotes
3009 ;; `org-export-collect-footnote-definitions' is a tool to list
3010 ;; actually used footnotes definitions in the whole parse tree, or in
3011 ;; an headline, in order to add footnote listings throughout the
3012 ;; transcoded data.
3014 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3015 ;; back-ends, when they need to attach the footnote definition only to
3016 ;; the first occurrence of the corresponding label.
3018 ;; `org-export-get-footnote-definition' and
3019 ;; `org-export-get-footnote-number' provide easier access to
3020 ;; additional information relative to a footnote reference.
3022 (defun org-export-collect-footnote-definitions (data info)
3023 "Return an alist between footnote numbers, labels and definitions.
3025 DATA is the parse tree from which definitions are collected.
3026 INFO is the plist used as a communication channel.
3028 Definitions are sorted by order of references. They either
3029 appear as Org data or as a secondary string for inlined
3030 footnotes. Unreferenced definitions are ignored."
3031 (let* (num-alist
3032 collect-fn ; for byte-compiler.
3033 (collect-fn
3034 (function
3035 (lambda (data)
3036 ;; Collect footnote number, label and definition in DATA.
3037 (org-element-map
3038 data 'footnote-reference
3039 (lambda (fn)
3040 (when (org-export-footnote-first-reference-p fn info)
3041 (let ((def (org-export-get-footnote-definition fn info)))
3042 (push
3043 (list (org-export-get-footnote-number fn info)
3044 (org-element-property :label fn)
3045 def)
3046 num-alist)
3047 ;; Also search in definition for nested footnotes.
3048 (when (eq (org-element-property :type fn) 'standard)
3049 (funcall collect-fn def)))))
3050 ;; Don't enter footnote definitions since it will happen
3051 ;; when their first reference is found.
3052 info nil 'footnote-definition)))))
3053 (funcall collect-fn (plist-get info :parse-tree))
3054 (reverse num-alist)))
3056 (defun org-export-footnote-first-reference-p (footnote-reference info)
3057 "Non-nil when a footnote reference is the first one for its label.
3059 FOOTNOTE-REFERENCE is the footnote reference being considered.
3060 INFO is the plist used as a communication channel."
3061 (let ((label (org-element-property :label footnote-reference)))
3062 ;; Anonymous footnotes are always a first reference.
3063 (if (not label) t
3064 ;; Otherwise, return the first footnote with the same LABEL and
3065 ;; test if it is equal to FOOTNOTE-REFERENCE.
3066 (let* (search-refs ; for byte-compiler.
3067 (search-refs
3068 (function
3069 (lambda (data)
3070 (org-element-map
3071 data 'footnote-reference
3072 (lambda (fn)
3073 (cond
3074 ((string= (org-element-property :label fn) label)
3075 (throw 'exit fn))
3076 ;; If FN isn't inlined, be sure to traverse its
3077 ;; definition before resuming search. See
3078 ;; comments in `org-export-get-footnote-number'
3079 ;; for more information.
3080 ((eq (org-element-property :type fn) 'standard)
3081 (funcall search-refs
3082 (org-export-get-footnote-definition fn info)))))
3083 ;; Don't enter footnote definitions since it will
3084 ;; happen when their first reference is found.
3085 info 'first-match 'footnote-definition)))))
3086 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3087 footnote-reference)))))
3089 (defun org-export-get-footnote-definition (footnote-reference info)
3090 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3091 INFO is the plist used as a communication channel."
3092 (let ((label (org-element-property :label footnote-reference)))
3093 (or (org-element-property :inline-definition footnote-reference)
3094 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3096 (defun org-export-get-footnote-number (footnote info)
3097 "Return number associated to a footnote.
3099 FOOTNOTE is either a footnote reference or a footnote definition.
3100 INFO is the plist used as a communication channel."
3101 (let* ((label (org-element-property :label footnote))
3102 seen-refs
3103 search-ref ; For byte-compiler.
3104 (search-ref
3105 (function
3106 (lambda (data)
3107 ;; Search footnote references through DATA, filling
3108 ;; SEEN-REFS along the way.
3109 (org-element-map
3110 data 'footnote-reference
3111 (lambda (fn)
3112 (let ((fn-lbl (org-element-property :label fn)))
3113 (cond
3114 ;; Anonymous footnote match: return number.
3115 ((and (not fn-lbl) (eq fn footnote))
3116 (throw 'exit (1+ (length seen-refs))))
3117 ;; Labels match: return number.
3118 ((and label (string= label fn-lbl))
3119 (throw 'exit (1+ (length seen-refs))))
3120 ;; Anonymous footnote: it's always a new one. Also,
3121 ;; be sure to return nil from the `cond' so
3122 ;; `first-match' doesn't get us out of the loop.
3123 ((not fn-lbl) (push 'inline seen-refs) nil)
3124 ;; Label not seen so far: add it so SEEN-REFS.
3126 ;; Also search for subsequent references in
3127 ;; footnote definition so numbering follows reading
3128 ;; logic. Note that we don't have to care about
3129 ;; inline definitions, since `org-element-map'
3130 ;; already traverses them at the right time.
3132 ;; Once again, return nil to stay in the loop.
3133 ((not (member fn-lbl seen-refs))
3134 (push fn-lbl seen-refs)
3135 (funcall search-ref
3136 (org-export-get-footnote-definition fn info))
3137 nil))))
3138 ;; Don't enter footnote definitions since it will happen
3139 ;; when their first reference is found.
3140 info 'first-match 'footnote-definition)))))
3141 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3144 ;;;; For Headlines
3146 ;; `org-export-get-relative-level' is a shortcut to get headline
3147 ;; level, relatively to the lower headline level in the parsed tree.
3149 ;; `org-export-get-headline-number' returns the section number of an
3150 ;; headline, while `org-export-number-to-roman' allows to convert it
3151 ;; to roman numbers.
3153 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3154 ;; `org-export-last-sibling-p' are three useful predicates when it
3155 ;; comes to fulfill the `:headline-levels' property.
3157 (defun org-export-get-relative-level (headline info)
3158 "Return HEADLINE relative level within current parsed tree.
3159 INFO is a plist holding contextual information."
3160 (+ (org-element-property :level headline)
3161 (or (plist-get info :headline-offset) 0)))
3163 (defun org-export-low-level-p (headline info)
3164 "Non-nil when HEADLINE is considered as low level.
3166 INFO is a plist used as a communication channel.
3168 A low level headlines has a relative level greater than
3169 `:headline-levels' property value.
3171 Return value is the difference between HEADLINE relative level
3172 and the last level being considered as high enough, or nil."
3173 (let ((limit (plist-get info :headline-levels)))
3174 (when (wholenump limit)
3175 (let ((level (org-export-get-relative-level headline info)))
3176 (and (> level limit) (- level limit))))))
3178 (defun org-export-get-headline-number (headline info)
3179 "Return HEADLINE numbering as a list of numbers.
3180 INFO is a plist holding contextual information."
3181 (cdr (assoc headline (plist-get info :headline-numbering))))
3183 (defun org-export-numbered-headline-p (headline info)
3184 "Return a non-nil value if HEADLINE element should be numbered.
3185 INFO is a plist used as a communication channel."
3186 (let ((sec-num (plist-get info :section-numbers))
3187 (level (org-export-get-relative-level headline info)))
3188 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3190 (defun org-export-number-to-roman (n)
3191 "Convert integer N into a roman numeral."
3192 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3193 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3194 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3195 ( 1 . "I")))
3196 (res ""))
3197 (if (<= n 0)
3198 (number-to-string n)
3199 (while roman
3200 (if (>= n (caar roman))
3201 (setq n (- n (caar roman))
3202 res (concat res (cdar roman)))
3203 (pop roman)))
3204 res)))
3206 (defun org-export-get-tags (element info &optional tags inherited)
3207 "Return list of tags associated to ELEMENT.
3209 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3210 is a plist used as a communication channel.
3212 Select tags (see `org-export-select-tags') and exclude tags (see
3213 `org-export-exclude-tags') are removed from the list.
3215 When non-nil, optional argument TAGS should be a list of strings.
3216 Any tag belonging to this list will also be removed.
3218 When optional argument INHERITED is non-nil, tags can also be
3219 inherited from parent headlines and FILETAGS keywords."
3220 (org-remove-if
3221 (lambda (tag) (or (member tag (plist-get info :select-tags))
3222 (member tag (plist-get info :exclude-tags))
3223 (member tag tags)))
3224 (if (not inherited) (org-element-property :tags element)
3225 ;; Build complete list of inherited tags.
3226 (let ((current-tag-list (org-element-property :tags element)))
3227 (mapc
3228 (lambda (parent)
3229 (mapc
3230 (lambda (tag)
3231 (when (and (memq (org-element-type parent) '(headline inlinetask))
3232 (not (member tag current-tag-list)))
3233 (push tag current-tag-list)))
3234 (org-element-property :tags parent)))
3235 (org-export-get-genealogy element))
3236 ;; Add FILETAGS keywords and return results.
3237 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3239 (defun org-export-get-node-property (property blob &optional inherited)
3240 "Return node PROPERTY value for BLOB.
3242 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3243 element or object.
3245 If optional argument INHERITED is non-nil, the value can be
3246 inherited from a parent headline.
3248 Return value is a string or nil."
3249 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3250 (org-export-get-parent-headline blob))))
3251 (if (not inherited) (org-element-property property blob)
3252 (let ((parent headline) value)
3253 (catch 'found
3254 (while parent
3255 (when (plist-member (nth 1 parent) property)
3256 (throw 'found (org-element-property property parent)))
3257 (setq parent (org-element-property :parent parent))))))))
3259 (defun org-export-get-category (blob info)
3260 "Return category for element or object BLOB.
3262 INFO is a plist used as a communication channel.
3264 CATEGORY is automatically inherited from a parent headline, from
3265 #+CATEGORY: keyword or created out of original file name. If all
3266 fail, the fall-back value is \"???\"."
3267 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3268 (org-export-get-parent-headline blob))))
3269 ;; Almost like `org-export-node-property', but we cannot trust
3270 ;; `plist-member' as every headline has a `:category'
3271 ;; property, even if nil.
3272 (let ((parent headline) value)
3273 (catch 'found
3274 (while parent
3275 (let ((category (org-element-property :category parent)))
3276 (and category (throw 'found category)))
3277 (setq parent (org-element-property :parent parent))))))
3278 (org-element-map
3279 (plist-get info :parse-tree) 'keyword
3280 (lambda (kwd)
3281 (when (equal (org-element-property :key kwd) "CATEGORY")
3282 (org-element-property :value kwd)))
3283 info 'first-match)
3284 (let ((file (plist-get info :input-file)))
3285 (and file (file-name-sans-extension (file-name-nondirectory file))))
3286 "???"))
3288 (defun org-export-first-sibling-p (headline info)
3289 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3290 INFO is a plist used as a communication channel."
3291 (not (eq (org-element-type (org-export-get-previous-element headline info))
3292 'headline)))
3294 (defun org-export-last-sibling-p (headline info)
3295 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3296 INFO is a plist used as a communication channel."
3297 (not (org-export-get-next-element headline info)))
3300 ;;;; For Links
3302 ;; `org-export-solidify-link-text' turns a string into a safer version
3303 ;; for links, replacing most non-standard characters with hyphens.
3305 ;; `org-export-get-coderef-format' returns an appropriate format
3306 ;; string for coderefs.
3308 ;; `org-export-inline-image-p' returns a non-nil value when the link
3309 ;; provided should be considered as an inline image.
3311 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3312 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3313 ;; returns an appropriate unique identifier when found, or nil.
3315 ;; `org-export-resolve-id-link' returns the first headline with
3316 ;; specified id or custom-id in parse tree, the path to the external
3317 ;; file with the id or nil when neither was found.
3319 ;; `org-export-resolve-coderef' associates a reference to a line
3320 ;; number in the element it belongs, or returns the reference itself
3321 ;; when the element isn't numbered.
3323 (defun org-export-solidify-link-text (s)
3324 "Take link text S and make a safe target out of it."
3325 (save-match-data
3326 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3328 (defun org-export-get-coderef-format (path desc)
3329 "Return format string for code reference link.
3330 PATH is the link path. DESC is its description."
3331 (save-match-data
3332 (cond ((not desc) "%s")
3333 ((string-match (regexp-quote (concat "(" path ")")) desc)
3334 (replace-match "%s" t t desc))
3335 (t desc))))
3337 (defun org-export-inline-image-p (link &optional rules)
3338 "Non-nil if LINK object points to an inline image.
3340 Optional argument is a set of RULES defining inline images. It
3341 is an alist where associations have the following shape:
3343 \(TYPE . REGEXP)
3345 Applying a rule means apply REGEXP against LINK's path when its
3346 type is TYPE. The function will return a non-nil value if any of
3347 the provided rules is non-nil. The default rule is
3348 `org-export-default-inline-image-rule'.
3350 This only applies to links without a description."
3351 (and (not (org-element-contents link))
3352 (let ((case-fold-search t)
3353 (rules (or rules org-export-default-inline-image-rule)))
3354 (catch 'exit
3355 (mapc
3356 (lambda (rule)
3357 (and (string= (org-element-property :type link) (car rule))
3358 (string-match (cdr rule)
3359 (org-element-property :path link))
3360 (throw 'exit t)))
3361 rules)
3362 ;; Return nil if no rule matched.
3363 nil))))
3365 (defun org-export-resolve-coderef (ref info)
3366 "Resolve a code reference REF.
3368 INFO is a plist used as a communication channel.
3370 Return associated line number in source code, or REF itself,
3371 depending on src-block or example element's switches."
3372 (org-element-map
3373 (plist-get info :parse-tree) '(example-block src-block)
3374 (lambda (el)
3375 (with-temp-buffer
3376 (insert (org-trim (org-element-property :value el)))
3377 (let* ((label-fmt (regexp-quote
3378 (or (org-element-property :label-fmt el)
3379 org-coderef-label-format)))
3380 (ref-re
3381 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3382 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3383 ;; Element containing REF is found. Resolve it to either
3384 ;; a label or a line number, as needed.
3385 (when (re-search-backward ref-re nil t)
3386 (cond
3387 ((org-element-property :use-labels el) ref)
3388 ((eq (org-element-property :number-lines el) 'continued)
3389 (+ (org-export-get-loc el info) (line-number-at-pos)))
3390 (t (line-number-at-pos)))))))
3391 info 'first-match))
3393 (defun org-export-resolve-fuzzy-link (link info)
3394 "Return LINK destination.
3396 INFO is a plist holding contextual information.
3398 Return value can be an object, an element, or nil:
3400 - If LINK path matches a target object (i.e. <<path>>) or
3401 element (i.e. \"#+TARGET: path\"), return it.
3403 - If LINK path exactly matches the name affiliated keyword
3404 \(i.e. #+NAME: path) of an element, return that element.
3406 - If LINK path exactly matches any headline name, return that
3407 element. If more than one headline share that name, priority
3408 will be given to the one with the closest common ancestor, if
3409 any, or the first one in the parse tree otherwise.
3411 - Otherwise, return nil.
3413 Assume LINK type is \"fuzzy\"."
3414 (let* ((path (org-element-property :path link))
3415 (match-title-p (eq (aref path 0) ?*)))
3416 (cond
3417 ;; First try to find a matching "<<path>>" unless user specified
3418 ;; he was looking for an headline (path starts with a *
3419 ;; character).
3420 ((and (not match-title-p)
3421 (loop for target in (plist-get info :target-list)
3422 when (string= (org-element-property :value target) path)
3423 return target)))
3424 ;; Then try to find an element with a matching "#+NAME: path"
3425 ;; affiliated keyword.
3426 ((and (not match-title-p)
3427 (org-element-map
3428 (plist-get info :parse-tree) org-element-all-elements
3429 (lambda (el)
3430 (when (string= (org-element-property :name el) path) el))
3431 info 'first-match)))
3432 ;; Last case: link either points to an headline or to
3433 ;; nothingness. Try to find the source, with priority given to
3434 ;; headlines with the closest common ancestor. If such candidate
3435 ;; is found, return it, otherwise return nil.
3437 (let ((find-headline
3438 (function
3439 ;; Return first headline whose `:raw-value' property
3440 ;; is NAME in parse tree DATA, or nil.
3441 (lambda (name data)
3442 (org-element-map
3443 data 'headline
3444 (lambda (headline)
3445 (when (string=
3446 (org-element-property :raw-value headline)
3447 name)
3448 headline))
3449 info 'first-match)))))
3450 ;; Search among headlines sharing an ancestor with link,
3451 ;; from closest to farthest.
3452 (or (catch 'exit
3453 (mapc
3454 (lambda (parent)
3455 (when (eq (org-element-type parent) 'headline)
3456 (let ((foundp (funcall find-headline path parent)))
3457 (when foundp (throw 'exit foundp)))))
3458 (org-export-get-genealogy link)) nil)
3459 ;; No match with a common ancestor: try the full parse-tree.
3460 (funcall find-headline
3461 (if match-title-p (substring path 1) path)
3462 (plist-get info :parse-tree))))))))
3464 (defun org-export-resolve-id-link (link info)
3465 "Return headline referenced as LINK destination.
3467 INFO is a plist used as a communication channel.
3469 Return value can be the headline element matched in current parse
3470 tree, a file name or nil. Assume LINK type is either \"id\" or
3471 \"custom-id\"."
3472 (let ((id (org-element-property :path link)))
3473 ;; First check if id is within the current parse tree.
3474 (or (org-element-map
3475 (plist-get info :parse-tree) 'headline
3476 (lambda (headline)
3477 (when (or (string= (org-element-property :id headline) id)
3478 (string= (org-element-property :custom-id headline) id))
3479 headline))
3480 info 'first-match)
3481 ;; Otherwise, look for external files.
3482 (cdr (assoc id (plist-get info :id-alist))))))
3484 (defun org-export-resolve-radio-link (link info)
3485 "Return radio-target object referenced as LINK destination.
3487 INFO is a plist used as a communication channel.
3489 Return value can be a radio-target object or nil. Assume LINK
3490 has type \"radio\"."
3491 (let ((path (org-element-property :path link)))
3492 (org-element-map
3493 (plist-get info :parse-tree) 'radio-target
3494 (lambda (radio)
3495 (when (equal (org-element-property :value radio) path) radio))
3496 info 'first-match)))
3499 ;;;; For References
3501 ;; `org-export-get-ordinal' associates a sequence number to any object
3502 ;; or element.
3504 (defun org-export-get-ordinal (element info &optional types predicate)
3505 "Return ordinal number of an element or object.
3507 ELEMENT is the element or object considered. INFO is the plist
3508 used as a communication channel.
3510 Optional argument TYPES, when non-nil, is a list of element or
3511 object types, as symbols, that should also be counted in.
3512 Otherwise, only provided element's type is considered.
3514 Optional argument PREDICATE is a function returning a non-nil
3515 value if the current element or object should be counted in. It
3516 accepts two arguments: the element or object being considered and
3517 the plist used as a communication channel. This allows to count
3518 only a certain type of objects (i.e. inline images).
3520 Return value is a list of numbers if ELEMENT is an headline or an
3521 item. It is nil for keywords. It represents the footnote number
3522 for footnote definitions and footnote references. If ELEMENT is
3523 a target, return the same value as if ELEMENT was the closest
3524 table, item or headline containing the target. In any other
3525 case, return the sequence number of ELEMENT among elements or
3526 objects of the same type."
3527 ;; A target keyword, representing an invisible target, never has
3528 ;; a sequence number.
3529 (unless (eq (org-element-type element) 'keyword)
3530 ;; Ordinal of a target object refer to the ordinal of the closest
3531 ;; table, item, or headline containing the object.
3532 (when (eq (org-element-type element) 'target)
3533 (setq element
3534 (loop for parent in (org-export-get-genealogy element)
3535 when
3536 (memq
3537 (org-element-type parent)
3538 '(footnote-definition footnote-reference headline item
3539 table))
3540 return parent)))
3541 (case (org-element-type element)
3542 ;; Special case 1: An headline returns its number as a list.
3543 (headline (org-export-get-headline-number element info))
3544 ;; Special case 2: An item returns its number as a list.
3545 (item (let ((struct (org-element-property :structure element)))
3546 (org-list-get-item-number
3547 (org-element-property :begin element)
3548 struct
3549 (org-list-prevs-alist struct)
3550 (org-list-parents-alist struct))))
3551 ((footnote-definition footnote-reference)
3552 (org-export-get-footnote-number element info))
3553 (otherwise
3554 (let ((counter 0))
3555 ;; Increment counter until ELEMENT is found again.
3556 (org-element-map
3557 (plist-get info :parse-tree) (or types (org-element-type element))
3558 (lambda (el)
3559 (cond
3560 ((eq element el) (1+ counter))
3561 ((not predicate) (incf counter) nil)
3562 ((funcall predicate el info) (incf counter) nil)))
3563 info 'first-match))))))
3566 ;;;; For Src-Blocks
3568 ;; `org-export-get-loc' counts number of code lines accumulated in
3569 ;; src-block or example-block elements with a "+n" switch until
3570 ;; a given element, excluded. Note: "-n" switches reset that count.
3572 ;; `org-export-unravel-code' extracts source code (along with a code
3573 ;; references alist) from an `element-block' or `src-block' type
3574 ;; element.
3576 ;; `org-export-format-code' applies a formatting function to each line
3577 ;; of code, providing relative line number and code reference when
3578 ;; appropriate. Since it doesn't access the original element from
3579 ;; which the source code is coming, it expects from the code calling
3580 ;; it to know if lines should be numbered and if code references
3581 ;; should appear.
3583 ;; Eventually, `org-export-format-code-default' is a higher-level
3584 ;; function (it makes use of the two previous functions) which handles
3585 ;; line numbering and code references inclusion, and returns source
3586 ;; code in a format suitable for plain text or verbatim output.
3588 (defun org-export-get-loc (element info)
3589 "Return accumulated lines of code up to ELEMENT.
3591 INFO is the plist used as a communication channel.
3593 ELEMENT is excluded from count."
3594 (let ((loc 0))
3595 (org-element-map
3596 (plist-get info :parse-tree)
3597 `(src-block example-block ,(org-element-type element))
3598 (lambda (el)
3599 (cond
3600 ;; ELEMENT is reached: Quit the loop.
3601 ((eq el element))
3602 ;; Only count lines from src-block and example-block elements
3603 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3604 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3605 ((let ((linums (org-element-property :number-lines el)))
3606 (when linums
3607 ;; Accumulate locs or reset them.
3608 (let ((lines (org-count-lines
3609 (org-trim (org-element-property :value el)))))
3610 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3611 ;; Return nil to stay in the loop.
3612 nil)))
3613 info 'first-match)
3614 ;; Return value.
3615 loc))
3617 (defun org-export-unravel-code (element)
3618 "Clean source code and extract references out of it.
3620 ELEMENT has either a `src-block' an `example-block' type.
3622 Return a cons cell whose CAR is the source code, cleaned from any
3623 reference and protective comma and CDR is an alist between
3624 relative line number (integer) and name of code reference on that
3625 line (string)."
3626 (let* ((line 0) refs
3627 ;; Get code and clean it. Remove blank lines at its
3628 ;; beginning and end.
3629 (code (let ((c (replace-regexp-in-string
3630 "\\`\\([ \t]*\n\\)+" ""
3631 (replace-regexp-in-string
3632 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3633 (org-element-property :value element)))))
3634 ;; If appropriate, remove global indentation.
3635 (if (or org-src-preserve-indentation
3636 (org-element-property :preserve-indent element))
3638 (org-remove-indentation c))))
3639 ;; Get format used for references.
3640 (label-fmt (regexp-quote
3641 (or (org-element-property :label-fmt element)
3642 org-coderef-label-format)))
3643 ;; Build a regexp matching a loc with a reference.
3644 (with-ref-re
3645 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3646 (replace-regexp-in-string
3647 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3648 ;; Return value.
3649 (cons
3650 ;; Code with references removed.
3651 (org-element-normalize-string
3652 (mapconcat
3653 (lambda (loc)
3654 (incf line)
3655 (if (not (string-match with-ref-re loc)) loc
3656 ;; Ref line: remove ref, and signal its position in REFS.
3657 (push (cons line (match-string 3 loc)) refs)
3658 (replace-match "" nil nil loc 1)))
3659 (org-split-string code "\n") "\n"))
3660 ;; Reference alist.
3661 refs)))
3663 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3664 "Format CODE by applying FUN line-wise and return it.
3666 CODE is a string representing the code to format. FUN is
3667 a function. It must accept three arguments: a line of
3668 code (string), the current line number (integer) or nil and the
3669 reference associated to the current line (string) or nil.
3671 Optional argument NUM-LINES can be an integer representing the
3672 number of code lines accumulated until the current code. Line
3673 numbers passed to FUN will take it into account. If it is nil,
3674 FUN's second argument will always be nil. This number can be
3675 obtained with `org-export-get-loc' function.
3677 Optional argument REF-ALIST can be an alist between relative line
3678 number (i.e. ignoring NUM-LINES) and the name of the code
3679 reference on it. If it is nil, FUN's third argument will always
3680 be nil. It can be obtained through the use of
3681 `org-export-unravel-code' function."
3682 (let ((--locs (org-split-string code "\n"))
3683 (--line 0))
3684 (org-element-normalize-string
3685 (mapconcat
3686 (lambda (--loc)
3687 (incf --line)
3688 (let ((--ref (cdr (assq --line ref-alist))))
3689 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3690 --locs "\n"))))
3692 (defun org-export-format-code-default (element info)
3693 "Return source code from ELEMENT, formatted in a standard way.
3695 ELEMENT is either a `src-block' or `example-block' element. INFO
3696 is a plist used as a communication channel.
3698 This function takes care of line numbering and code references
3699 inclusion. Line numbers, when applicable, appear at the
3700 beginning of the line, separated from the code by two white
3701 spaces. Code references, on the other hand, appear flushed to
3702 the right, separated by six white spaces from the widest line of
3703 code."
3704 ;; Extract code and references.
3705 (let* ((code-info (org-export-unravel-code element))
3706 (code (car code-info))
3707 (code-lines (org-split-string code "\n"))
3708 (refs (and (org-element-property :retain-labels element)
3709 (cdr code-info)))
3710 ;; Handle line numbering.
3711 (num-start (case (org-element-property :number-lines element)
3712 (continued (org-export-get-loc element info))
3713 (new 0)))
3714 (num-fmt
3715 (and num-start
3716 (format "%%%ds "
3717 (length (number-to-string
3718 (+ (length code-lines) num-start))))))
3719 ;; Prepare references display, if required. Any reference
3720 ;; should start six columns after the widest line of code,
3721 ;; wrapped with parenthesis.
3722 (max-width
3723 (+ (apply 'max (mapcar 'length code-lines))
3724 (if (not num-start) 0 (length (format num-fmt num-start))))))
3725 (org-export-format-code
3726 code
3727 (lambda (loc line-num ref)
3728 (let ((number-str (and num-fmt (format num-fmt line-num))))
3729 (concat
3730 number-str
3732 (and ref
3733 (concat (make-string
3734 (- (+ 6 max-width)
3735 (+ (length loc) (length number-str))) ? )
3736 (format "(%s)" ref))))))
3737 num-start refs)))
3740 ;;;; For Tables
3742 ;; `org-export-table-has-special-column-p' and and
3743 ;; `org-export-table-row-is-special-p' are predicates used to look for
3744 ;; meta-information about the table structure.
3746 ;; `org-table-has-header-p' tells when the rows before the first rule
3747 ;; should be considered as table's header.
3749 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3750 ;; and `org-export-table-cell-borders' extract information from
3751 ;; a table-cell element.
3753 ;; `org-export-table-dimensions' gives the number on rows and columns
3754 ;; in the table, ignoring horizontal rules and special columns.
3755 ;; `org-export-table-cell-address', given a table-cell object, returns
3756 ;; the absolute address of a cell. On the other hand,
3757 ;; `org-export-get-table-cell-at' does the contrary.
3759 ;; `org-export-table-cell-starts-colgroup-p',
3760 ;; `org-export-table-cell-ends-colgroup-p',
3761 ;; `org-export-table-row-starts-rowgroup-p',
3762 ;; `org-export-table-row-ends-rowgroup-p',
3763 ;; `org-export-table-row-starts-header-p' and
3764 ;; `org-export-table-row-ends-header-p' indicate position of current
3765 ;; row or cell within the table.
3767 (defun org-export-table-has-special-column-p (table)
3768 "Non-nil when TABLE has a special column.
3769 All special columns will be ignored during export."
3770 ;; The table has a special column when every first cell of every row
3771 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3772 ;; "*" "_" and "^". Though, do not consider a first row containing
3773 ;; only empty cells as special.
3774 (let ((special-column-p 'empty))
3775 (catch 'exit
3776 (mapc
3777 (lambda (row)
3778 (when (eq (org-element-property :type row) 'standard)
3779 (let ((value (org-element-contents
3780 (car (org-element-contents row)))))
3781 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3782 (setq special-column-p 'special))
3783 ((not value))
3784 (t (throw 'exit nil))))))
3785 (org-element-contents table))
3786 (eq special-column-p 'special))))
3788 (defun org-export-table-has-header-p (table info)
3789 "Non-nil when TABLE has an header.
3791 INFO is a plist used as a communication channel.
3793 A table has an header when it contains at least two row groups."
3794 (let ((rowgroup 1) row-flag)
3795 (org-element-map
3796 table 'table-row
3797 (lambda (row)
3798 (cond
3799 ((> rowgroup 1) t)
3800 ((and row-flag (eq (org-element-property :type row) 'rule))
3801 (incf rowgroup) (setq row-flag nil))
3802 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3803 (setq row-flag t) nil)))
3804 info)))
3806 (defun org-export-table-row-is-special-p (table-row info)
3807 "Non-nil if TABLE-ROW is considered special.
3809 INFO is a plist used as the communication channel.
3811 All special rows will be ignored during export."
3812 (when (eq (org-element-property :type table-row) 'standard)
3813 (let ((first-cell (org-element-contents
3814 (car (org-element-contents table-row)))))
3815 ;; A row is special either when...
3817 ;; ... it starts with a field only containing "/",
3818 (equal first-cell '("/"))
3819 ;; ... the table contains a special column and the row start
3820 ;; with a marking character among, "^", "_", "$" or "!",
3821 (and (org-export-table-has-special-column-p
3822 (org-export-get-parent table-row))
3823 (member first-cell '(("^") ("_") ("$") ("!"))))
3824 ;; ... it contains only alignment cookies and empty cells.
3825 (let ((special-row-p 'empty))
3826 (catch 'exit
3827 (mapc
3828 (lambda (cell)
3829 (let ((value (org-element-contents cell)))
3830 ;; Since VALUE is a secondary string, the following
3831 ;; checks avoid expanding it with `org-export-data'.
3832 (cond ((not value))
3833 ((and (not (cdr value))
3834 (stringp (car value))
3835 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3836 (car value)))
3837 (setq special-row-p 'cookie))
3838 (t (throw 'exit nil)))))
3839 (org-element-contents table-row))
3840 (eq special-row-p 'cookie)))))))
3842 (defun org-export-table-row-group (table-row info)
3843 "Return TABLE-ROW's group.
3845 INFO is a plist used as the communication channel.
3847 Return value is the group number, as an integer, or nil special
3848 rows and table rules. Group 1 is also table's header."
3849 (unless (or (eq (org-element-property :type table-row) 'rule)
3850 (org-export-table-row-is-special-p table-row info))
3851 (let ((group 0) row-flag)
3852 (catch 'found
3853 (mapc
3854 (lambda (row)
3855 (cond
3856 ((and (eq (org-element-property :type row) 'standard)
3857 (not (org-export-table-row-is-special-p row info)))
3858 (unless row-flag (incf group) (setq row-flag t)))
3859 ((eq (org-element-property :type row) 'rule)
3860 (setq row-flag nil)))
3861 (when (eq table-row row) (throw 'found group)))
3862 (org-element-contents (org-export-get-parent table-row)))))))
3864 (defun org-export-table-cell-width (table-cell info)
3865 "Return TABLE-CELL contents width.
3867 INFO is a plist used as the communication channel.
3869 Return value is the width given by the last width cookie in the
3870 same column as TABLE-CELL, or nil."
3871 (let* ((row (org-export-get-parent table-cell))
3872 (column (let ((cells (org-element-contents row)))
3873 (- (length cells) (length (memq table-cell cells)))))
3874 (table (org-export-get-parent-table table-cell))
3875 cookie-width)
3876 (mapc
3877 (lambda (row)
3878 (cond
3879 ;; In a special row, try to find a width cookie at COLUMN.
3880 ((org-export-table-row-is-special-p row info)
3881 (let ((value (org-element-contents
3882 (elt (org-element-contents row) column))))
3883 ;; The following checks avoid expanding unnecessarily the
3884 ;; cell with `org-export-data'
3885 (when (and value
3886 (not (cdr value))
3887 (stringp (car value))
3888 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3889 (match-string 1 (car value)))
3890 (setq cookie-width
3891 (string-to-number (match-string 1 (car value)))))))
3892 ;; Ignore table rules.
3893 ((eq (org-element-property :type row) 'rule))))
3894 (org-element-contents table))
3895 ;; Return value.
3896 cookie-width))
3898 (defun org-export-table-cell-alignment (table-cell info)
3899 "Return TABLE-CELL contents alignment.
3901 INFO is a plist used as the communication channel.
3903 Return alignment as specified by the last alignment cookie in the
3904 same column as TABLE-CELL. If no such cookie is found, a default
3905 alignment value will be deduced from fraction of numbers in the
3906 column (see `org-table-number-fraction' for more information).
3907 Possible values are `left', `right' and `center'."
3908 (let* ((row (org-export-get-parent table-cell))
3909 (column (let ((cells (org-element-contents row)))
3910 (- (length cells) (length (memq table-cell cells)))))
3911 (table (org-export-get-parent-table table-cell))
3912 (number-cells 0)
3913 (total-cells 0)
3914 cookie-align)
3915 (mapc
3916 (lambda (row)
3917 (cond
3918 ;; In a special row, try to find an alignment cookie at
3919 ;; COLUMN.
3920 ((org-export-table-row-is-special-p row info)
3921 (let ((value (org-element-contents
3922 (elt (org-element-contents row) column))))
3923 ;; Since VALUE is a secondary string, the following checks
3924 ;; avoid useless expansion through `org-export-data'.
3925 (when (and value
3926 (not (cdr value))
3927 (stringp (car value))
3928 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3929 (car value))
3930 (match-string 1 (car value)))
3931 (setq cookie-align (match-string 1 (car value))))))
3932 ;; Ignore table rules.
3933 ((eq (org-element-property :type row) 'rule))
3934 ;; In a standard row, check if cell's contents are expressing
3935 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3936 ;; Though, don't bother if an alignment cookie has already
3937 ;; defined cell's alignment.
3938 ((not cookie-align)
3939 (let ((value (org-export-data
3940 (org-element-contents
3941 (elt (org-element-contents row) column))
3942 info)))
3943 (incf total-cells)
3944 (when (string-match org-table-number-regexp value)
3945 (incf number-cells))))))
3946 (org-element-contents table))
3947 ;; Return value. Alignment specified by cookies has precedence
3948 ;; over alignment deduced from cells contents.
3949 (cond ((equal cookie-align "l") 'left)
3950 ((equal cookie-align "r") 'right)
3951 ((equal cookie-align "c") 'center)
3952 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3953 'right)
3954 (t 'left))))
3956 (defun org-export-table-cell-borders (table-cell info)
3957 "Return TABLE-CELL borders.
3959 INFO is a plist used as a communication channel.
3961 Return value is a list of symbols, or nil. Possible values are:
3962 `top', `bottom', `above', `below', `left' and `right'. Note:
3963 `top' (resp. `bottom') only happen for a cell in the first
3964 row (resp. last row) of the table, ignoring table rules, if any.
3966 Returned borders ignore special rows."
3967 (let* ((row (org-export-get-parent table-cell))
3968 (table (org-export-get-parent-table table-cell))
3969 borders)
3970 ;; Top/above border? TABLE-CELL has a border above when a rule
3971 ;; used to demarcate row groups can be found above. Hence,
3972 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3973 ;; another regular row has to be found above that rule.
3974 (let (rule-flag)
3975 (catch 'exit
3976 (mapc (lambda (row)
3977 (cond ((eq (org-element-property :type row) 'rule)
3978 (setq rule-flag t))
3979 ((not (org-export-table-row-is-special-p row info))
3980 (if rule-flag (throw 'exit (push 'above borders))
3981 (throw 'exit nil)))))
3982 ;; Look at every row before the current one.
3983 (cdr (memq row (reverse (org-element-contents table)))))
3984 ;; No rule above, or rule found starts the table (ignoring any
3985 ;; special row): TABLE-CELL is at the top of the table.
3986 (when rule-flag (push 'above borders))
3987 (push 'top borders)))
3988 ;; Bottom/below border? TABLE-CELL has a border below when next
3989 ;; non-regular row below is a rule.
3990 (let (rule-flag)
3991 (catch 'exit
3992 (mapc (lambda (row)
3993 (cond ((eq (org-element-property :type row) 'rule)
3994 (setq rule-flag t))
3995 ((not (org-export-table-row-is-special-p row info))
3996 (if rule-flag (throw 'exit (push 'below borders))
3997 (throw 'exit nil)))))
3998 ;; Look at every row after the current one.
3999 (cdr (memq row (org-element-contents table))))
4000 ;; No rule below, or rule found ends the table (modulo some
4001 ;; special row): TABLE-CELL is at the bottom of the table.
4002 (when rule-flag (push 'below borders))
4003 (push 'bottom borders)))
4004 ;; Right/left borders? They can only be specified by column
4005 ;; groups. Column groups are defined in a row starting with "/".
4006 ;; Also a column groups row only contains "<", "<>", ">" or blank
4007 ;; cells.
4008 (catch 'exit
4009 (let ((column (let ((cells (org-element-contents row)))
4010 (- (length cells) (length (memq table-cell cells))))))
4011 (mapc
4012 (lambda (row)
4013 (unless (eq (org-element-property :type row) 'rule)
4014 (when (equal (org-element-contents
4015 (car (org-element-contents row)))
4016 '("/"))
4017 (let ((column-groups
4018 (mapcar
4019 (lambda (cell)
4020 (let ((value (org-element-contents cell)))
4021 (when (member value '(("<") ("<>") (">") nil))
4022 (car value))))
4023 (org-element-contents row))))
4024 ;; There's a left border when previous cell, if
4025 ;; any, ends a group, or current one starts one.
4026 (when (or (and (not (zerop column))
4027 (member (elt column-groups (1- column))
4028 '(">" "<>")))
4029 (member (elt column-groups column) '("<" "<>")))
4030 (push 'left borders))
4031 ;; There's a right border when next cell, if any,
4032 ;; starts a group, or current one ends one.
4033 (when (or (and (/= (1+ column) (length column-groups))
4034 (member (elt column-groups (1+ column))
4035 '("<" "<>")))
4036 (member (elt column-groups column) '(">" "<>")))
4037 (push 'right borders))
4038 (throw 'exit nil)))))
4039 ;; Table rows are read in reverse order so last column groups
4040 ;; row has precedence over any previous one.
4041 (reverse (org-element-contents table)))))
4042 ;; Return value.
4043 borders))
4045 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4046 "Non-nil when TABLE-CELL is at the beginning of a row group.
4047 INFO is a plist used as a communication channel."
4048 ;; A cell starts a column group either when it is at the beginning
4049 ;; of a row (or after the special column, if any) or when it has
4050 ;; a left border.
4051 (or (eq (org-element-map
4052 (org-export-get-parent table-cell)
4053 'table-cell 'identity info 'first-match)
4054 table-cell)
4055 (memq 'left (org-export-table-cell-borders table-cell info))))
4057 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4058 "Non-nil when TABLE-CELL is at the end of a row group.
4059 INFO is a plist used as a communication channel."
4060 ;; A cell ends a column group either when it is at the end of a row
4061 ;; or when it has a right border.
4062 (or (eq (car (last (org-element-contents
4063 (org-export-get-parent table-cell))))
4064 table-cell)
4065 (memq 'right (org-export-table-cell-borders table-cell info))))
4067 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4068 "Non-nil when TABLE-ROW is at the beginning of a column group.
4069 INFO is a plist used as a communication channel."
4070 (unless (or (eq (org-element-property :type table-row) 'rule)
4071 (org-export-table-row-is-special-p table-row info))
4072 (let ((borders (org-export-table-cell-borders
4073 (car (org-element-contents table-row)) info)))
4074 (or (memq 'top borders) (memq 'above borders)))))
4076 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4077 "Non-nil when TABLE-ROW is at the end of a column group.
4078 INFO is a plist used as a communication channel."
4079 (unless (or (eq (org-element-property :type table-row) 'rule)
4080 (org-export-table-row-is-special-p table-row info))
4081 (let ((borders (org-export-table-cell-borders
4082 (car (org-element-contents table-row)) info)))
4083 (or (memq 'bottom borders) (memq 'below borders)))))
4085 (defun org-export-table-row-starts-header-p (table-row info)
4086 "Non-nil when TABLE-ROW is the first table header's row.
4087 INFO is a plist used as a communication channel."
4088 (and (org-export-table-has-header-p
4089 (org-export-get-parent-table table-row) info)
4090 (org-export-table-row-starts-rowgroup-p table-row info)
4091 (= (org-export-table-row-group table-row info) 1)))
4093 (defun org-export-table-row-ends-header-p (table-row info)
4094 "Non-nil when TABLE-ROW is the last table header's row.
4095 INFO is a plist used as a communication channel."
4096 (and (org-export-table-has-header-p
4097 (org-export-get-parent-table table-row) info)
4098 (org-export-table-row-ends-rowgroup-p table-row info)
4099 (= (org-export-table-row-group table-row info) 1)))
4101 (defun org-export-table-dimensions (table info)
4102 "Return TABLE dimensions.
4104 INFO is a plist used as a communication channel.
4106 Return value is a CONS like (ROWS . COLUMNS) where
4107 ROWS (resp. COLUMNS) is the number of exportable
4108 rows (resp. columns)."
4109 (let (first-row (columns 0) (rows 0))
4110 ;; Set number of rows, and extract first one.
4111 (org-element-map
4112 table 'table-row
4113 (lambda (row)
4114 (when (eq (org-element-property :type row) 'standard)
4115 (incf rows)
4116 (unless first-row (setq first-row row)))) info)
4117 ;; Set number of columns.
4118 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4119 ;; Return value.
4120 (cons rows columns)))
4122 (defun org-export-table-cell-address (table-cell info)
4123 "Return address of a regular TABLE-CELL object.
4125 TABLE-CELL is the cell considered. INFO is a plist used as
4126 a communication channel.
4128 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4129 zero-based index. Only exportable cells are considered. The
4130 function returns nil for other cells."
4131 (let* ((table-row (org-export-get-parent table-cell))
4132 (table (org-export-get-parent-table table-cell)))
4133 ;; Ignore cells in special rows or in special column.
4134 (unless (or (org-export-table-row-is-special-p table-row info)
4135 (and (org-export-table-has-special-column-p table)
4136 (eq (car (org-element-contents table-row)) table-cell)))
4137 (cons
4138 ;; Row number.
4139 (let ((row-count 0))
4140 (org-element-map
4141 table 'table-row
4142 (lambda (row)
4143 (cond ((eq (org-element-property :type row) 'rule) nil)
4144 ((eq row table-row) row-count)
4145 (t (incf row-count) nil)))
4146 info 'first-match))
4147 ;; Column number.
4148 (let ((col-count 0))
4149 (org-element-map
4150 table-row 'table-cell
4151 (lambda (cell)
4152 (if (eq cell table-cell) col-count (incf col-count) nil))
4153 info 'first-match))))))
4155 (defun org-export-get-table-cell-at (address table info)
4156 "Return regular table-cell object at ADDRESS in TABLE.
4158 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4159 zero-based index. TABLE is a table type element. INFO is
4160 a plist used as a communication channel.
4162 If no table-cell, among exportable cells, is found at ADDRESS,
4163 return nil."
4164 (let ((column-pos (cdr address)) (column-count 0))
4165 (org-element-map
4166 ;; Row at (car address) or nil.
4167 (let ((row-pos (car address)) (row-count 0))
4168 (org-element-map
4169 table 'table-row
4170 (lambda (row)
4171 (cond ((eq (org-element-property :type row) 'rule) nil)
4172 ((= row-count row-pos) row)
4173 (t (incf row-count) nil)))
4174 info 'first-match))
4175 'table-cell
4176 (lambda (cell)
4177 (if (= column-count column-pos) cell
4178 (incf column-count) nil))
4179 info 'first-match)))
4182 ;;;; For Tables Of Contents
4184 ;; `org-export-collect-headlines' builds a list of all exportable
4185 ;; headline elements, maybe limited to a certain depth. One can then
4186 ;; easily parse it and transcode it.
4188 ;; Building lists of tables, figures or listings is quite similar.
4189 ;; Once the generic function `org-export-collect-elements' is defined,
4190 ;; `org-export-collect-tables', `org-export-collect-figures' and
4191 ;; `org-export-collect-listings' can be derived from it.
4193 (defun org-export-collect-headlines (info &optional n)
4194 "Collect headlines in order to build a table of contents.
4196 INFO is a plist used as a communication channel.
4198 When optional argument N is an integer, it specifies the depth of
4199 the table of contents. Otherwise, it is set to the value of the
4200 last headline level. See `org-export-headline-levels' for more
4201 information.
4203 Return a list of all exportable headlines as parsed elements."
4204 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4205 (org-element-map
4206 (plist-get info :parse-tree)
4207 'headline
4208 (lambda (headline)
4209 ;; Strip contents from HEADLINE.
4210 (let ((relative-level (org-export-get-relative-level headline info)))
4211 (unless (> relative-level n) headline)))
4212 info))
4214 (defun org-export-collect-elements (type info &optional predicate)
4215 "Collect referenceable elements of a determined type.
4217 TYPE can be a symbol or a list of symbols specifying element
4218 types to search. Only elements with a caption are collected.
4220 INFO is a plist used as a communication channel.
4222 When non-nil, optional argument PREDICATE is a function accepting
4223 one argument, an element of type TYPE. It returns a non-nil
4224 value when that element should be collected.
4226 Return a list of all elements found, in order of appearance."
4227 (org-element-map
4228 (plist-get info :parse-tree) type
4229 (lambda (element)
4230 (and (org-element-property :caption element)
4231 (or (not predicate) (funcall predicate element))
4232 element))
4233 info))
4235 (defun org-export-collect-tables (info)
4236 "Build a list of tables.
4237 INFO is a plist used as a communication channel.
4239 Return a list of table elements with a caption."
4240 (org-export-collect-elements 'table info))
4242 (defun org-export-collect-figures (info predicate)
4243 "Build a list of figures.
4245 INFO is a plist used as a communication channel. PREDICATE is
4246 a function which accepts one argument: a paragraph element and
4247 whose return value is non-nil when that element should be
4248 collected.
4250 A figure is a paragraph type element, with a caption, verifying
4251 PREDICATE. The latter has to be provided since a \"figure\" is
4252 a vague concept that may depend on back-end.
4254 Return a list of elements recognized as figures."
4255 (org-export-collect-elements 'paragraph info predicate))
4257 (defun org-export-collect-listings (info)
4258 "Build a list of src blocks.
4260 INFO is a plist used as a communication channel.
4262 Return a list of src-block elements with a caption."
4263 (org-export-collect-elements 'src-block info))
4266 ;;;; Smart Quotes
4268 (defconst org-export-smart-quotes-alist
4269 '(("de"
4270 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4271 :texinfo "@quotedblbase{}")
4272 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4273 :texinfo "@quotedblleft{}")
4274 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4275 :texinfo "@quotesinglbase{}")
4276 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4277 :texinfo "@quoteleft{}")
4278 (apostrophe :utf-8 "’" :html "&rsquo;"))
4279 ("en"
4280 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4281 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4282 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4283 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4284 (apostrophe :utf-8 "’" :html "&rsquo;"))
4285 ("es"
4286 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4287 :texinfo "@guillemetleft{}")
4288 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4289 :texinfo "@guillemetright{}")
4290 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4291 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4292 (apostrophe :utf-8 "’" :html "&rsquo;"))
4293 ("fr"
4294 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4295 :texinfo "@guillemetleft{}@tie{}")
4296 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4297 :texinfo "@tie{}@guillemetright{}")
4298 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4299 :texinfo "@guillemetleft{}@tie{}")
4300 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4301 :texinfo "@tie{}@guillemetright{}")
4302 (apostrophe :utf-8 "’" :html "&rsquo;")))
4303 "Smart quotes translations.
4305 Alist whose CAR is a language string and CDR is an alist with
4306 quote type as key and a plist associating various encodings to
4307 their translation as value.
4309 A quote type can be any symbol among `opening-double-quote',
4310 `closing-double-quote', `opening-single-quote',
4311 `closing-single-quote' and `apostrophe'.
4313 Valid encodings include `:utf-8', `:html', `:latex' and
4314 `:texinfo'.
4316 If no translation is found, the quote character is left as-is.")
4318 (defconst org-export-smart-quotes-regexps
4319 (list
4320 ;; Possible opening quote at beginning of string.
4321 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4322 ;; Possible closing quote at beginning of string.
4323 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4324 ;; Possible apostrophe at beginning of string.
4325 "\\`\\('\\)\\S-"
4326 ;; Opening single and double quotes.
4327 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4328 ;; Closing single and double quotes.
4329 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4330 ;; Apostrophe.
4331 "\\S-\\('\\)\\S-"
4332 ;; Possible opening quote at end of string.
4333 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4334 ;; Possible closing quote at end of string.
4335 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4336 ;; Possible apostrophe at end of string.
4337 "\\S-\\('\\)\\'")
4338 "List of regexps matching a quote or an apostrophe.
4339 In every regexp, quote or apostrophe matched is put in group 1.")
4341 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4342 "Replace regular quotes with \"smart\" quotes in string S.
4344 ENCODING is a symbol among `:html', `:latex' and `:utf-8'. INFO
4345 is a plist used as a communication channel.
4347 The function has to retrieve information about string
4348 surroundings in parse tree. It can only happen with an
4349 unmodified string. Thus, if S has already been through another
4350 process, a non-nil ORIGINAL optional argument will provide that
4351 original string.
4353 Return the new string."
4354 (if (equal s "") ""
4355 (let* ((prev (org-export-get-previous-element (or original s) info))
4356 (pre-blank (and prev (org-element-property :post-blank prev)))
4357 (next (org-export-get-next-element (or original s) info))
4358 (get-smart-quote
4359 (lambda (q type)
4360 ;; Return smart quote associated to a give quote Q, as
4361 ;; a string. TYPE is a symbol among `open', `close' and
4362 ;; `apostrophe'.
4363 (let ((key (case type
4364 (apostrophe 'apostrophe)
4365 (open (if (equal "'" q) 'opening-single-quote
4366 'opening-double-quote))
4367 (otherwise (if (equal "'" q) 'closing-single-quote
4368 'closing-double-quote)))))
4369 (or (plist-get
4370 (cdr (assq key
4371 (cdr (assoc (plist-get info :language)
4372 org-export-smart-quotes-alist))))
4373 encoding)
4374 q)))))
4375 (if (or (equal "\"" s) (equal "'" s))
4376 ;; Only a quote: no regexp can match. We have to check both
4377 ;; sides and decide what to do.
4378 (cond ((and (not prev) (not next)) s)
4379 ((not prev) (funcall get-smart-quote s 'open))
4380 ((and (not next) (zerop pre-blank))
4381 (funcall get-smart-quote s 'close))
4382 ((not next) s)
4383 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
4384 (t (funcall get-smart-quote 'open)))
4385 ;; 1. Replace quote character at the beginning of S.
4386 (cond
4387 ;; Apostrophe?
4388 ((and prev (zerop pre-blank)
4389 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4390 (setq s (replace-match
4391 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4392 nil t s 1)))
4393 ;; Closing quote?
4394 ((and prev (zerop pre-blank)
4395 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4396 (setq s (replace-match
4397 (funcall get-smart-quote (match-string 1 s) 'close)
4398 nil t s 1)))
4399 ;; Opening quote?
4400 ((and (or (not prev) (> pre-blank 0))
4401 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4402 (setq s (replace-match
4403 (funcall get-smart-quote (match-string 1 s) 'open)
4404 nil t s 1))))
4405 ;; 2. Replace quotes in the middle of the string.
4406 (setq s (replace-regexp-in-string
4407 ;; Opening quotes.
4408 (nth 3 org-export-smart-quotes-regexps)
4409 (lambda (text)
4410 (funcall get-smart-quote (match-string 1 text) 'open))
4411 s nil t 1))
4412 (setq s (replace-regexp-in-string
4413 ;; Closing quotes.
4414 (nth 4 org-export-smart-quotes-regexps)
4415 (lambda (text)
4416 (funcall get-smart-quote (match-string 1 text) 'close))
4417 s nil t 1))
4418 (setq s (replace-regexp-in-string
4419 ;; Apostrophes.
4420 (nth 5 org-export-smart-quotes-regexps)
4421 (lambda (text)
4422 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
4423 s nil t 1))
4424 ;; 3. Replace quote character at the end of S.
4425 (cond
4426 ;; Apostrophe?
4427 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4428 (setq s (replace-match
4429 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4430 nil t s 1)))
4431 ;; Closing quote?
4432 ((and (not next)
4433 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4434 (setq s (replace-match
4435 (funcall get-smart-quote (match-string 1 s) 'close)
4436 nil t s 1)))
4437 ;; Opening quote?
4438 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4439 (setq s (replace-match
4440 (funcall get-smart-quote (match-string 1 s) 'open)
4441 nil t s 1))))
4442 ;; Return string with smart quotes.
4443 s))))
4445 ;;;; Topology
4447 ;; Here are various functions to retrieve information about the
4448 ;; neighbourhood of a given element or object. Neighbours of interest
4449 ;; are direct parent (`org-export-get-parent'), parent headline
4450 ;; (`org-export-get-parent-headline'), first element containing an
4451 ;; object, (`org-export-get-parent-element'), parent table
4452 ;; (`org-export-get-parent-table'), previous element or object
4453 ;; (`org-export-get-previous-element') and next element or object
4454 ;; (`org-export-get-next-element').
4456 ;; `org-export-get-genealogy' returns the full genealogy of a given
4457 ;; element or object, from closest parent to full parse tree.
4459 (defun org-export-get-parent (blob)
4460 "Return BLOB parent or nil.
4461 BLOB is the element or object considered."
4462 (org-element-property :parent blob))
4464 (defun org-export-get-genealogy (blob)
4465 "Return full genealogy relative to a given element or object.
4467 BLOB is the element or object being considered.
4469 Ancestors are returned from closest to farthest, the last one
4470 being the full parse tree."
4471 (let (genealogy (parent blob))
4472 (while (setq parent (org-element-property :parent parent))
4473 (push parent genealogy))
4474 (nreverse genealogy)))
4476 (defun org-export-get-parent-headline (blob)
4477 "Return BLOB parent headline or nil.
4478 BLOB is the element or object being considered."
4479 (let ((parent blob))
4480 (while (and (setq parent (org-element-property :parent parent))
4481 (not (eq (org-element-type parent) 'headline))))
4482 parent))
4484 (defun org-export-get-parent-element (object)
4485 "Return first element containing OBJECT or nil.
4486 OBJECT is the object to consider."
4487 (let ((parent object))
4488 (while (and (setq parent (org-element-property :parent parent))
4489 (memq (org-element-type parent) org-element-all-objects)))
4490 parent))
4492 (defun org-export-get-parent-table (object)
4493 "Return OBJECT parent table or nil.
4494 OBJECT is either a `table-cell' or `table-element' type object."
4495 (let ((parent object))
4496 (while (and (setq parent (org-element-property :parent parent))
4497 (not (eq (org-element-type parent) 'table))))
4498 parent))
4500 (defun org-export-get-previous-element (blob info)
4501 "Return previous element or object.
4502 BLOB is an element or object. INFO is a plist used as
4503 a communication channel. Return previous exportable element or
4504 object, a string, or nil."
4505 (let (prev)
4506 (catch 'exit
4507 (mapc (lambda (obj)
4508 (cond ((eq obj blob) (throw 'exit prev))
4509 ((memq obj (plist-get info :ignore-list)))
4510 (t (setq prev obj))))
4511 ;; An object can belong to the contents of its parent or
4512 ;; to a secondary string. We check the latter option
4513 ;; first.
4514 (let ((parent (org-export-get-parent blob)))
4515 (or (and (not (memq (org-element-type blob)
4516 org-element-all-elements))
4517 (let ((sec-value
4518 (org-element-property
4519 (cdr (assq (org-element-type parent)
4520 org-element-secondary-value-alist))
4521 parent)))
4522 (and (memq blob sec-value) sec-value)))
4523 (org-element-contents parent)))))))
4525 (defun org-export-get-next-element (blob info)
4526 "Return next element or object.
4527 BLOB is an element or object. INFO is a plist used as
4528 a communication channel. Return next exportable element or
4529 object, a string, or nil."
4530 (catch 'found
4531 (mapc (lambda (obj)
4532 (unless (memq obj (plist-get info :ignore-list))
4533 (throw 'found obj)))
4534 ;; An object can belong to the contents of its parent or to
4535 ;; a secondary string. We check the latter option first.
4536 (let ((parent (org-export-get-parent blob)))
4537 (or (and (not (memq (org-element-type blob)
4538 org-element-all-objects))
4539 (let ((sec-value
4540 (org-element-property
4541 (cdr (assq (org-element-type parent)
4542 org-element-secondary-value-alist))
4543 parent)))
4544 (cdr (memq blob sec-value))))
4545 (cdr (memq blob (org-element-contents parent))))))
4546 nil))
4549 ;;;; Translation
4551 ;; `org-export-translate' translates a string according to language
4552 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4553 ;; variable and a specified charset. `org-export-dictionary' contains
4554 ;; the dictionary used for the translation.
4556 (defconst org-export-dictionary
4557 '(("Author"
4558 ("ca" :default "Autor")
4559 ("cs" :default "Autor")
4560 ("da" :default "Ophavsmand")
4561 ("de" :default "Autor")
4562 ("eo" :html "A&#365;toro")
4563 ("es" :default "Autor")
4564 ("fi" :html "Tekij&auml;")
4565 ("fr" :default "Auteur")
4566 ("hu" :default "Szerz&otilde;")
4567 ("is" :html "H&ouml;fundur")
4568 ("it" :default "Autore")
4569 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4570 ("nl" :default "Auteur")
4571 ("no" :default "Forfatter")
4572 ("nb" :default "Forfatter")
4573 ("nn" :default "Forfattar")
4574 ("pl" :default "Autor")
4575 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4576 ("sv" :html "F&ouml;rfattare")
4577 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4578 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4579 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4580 ("Date"
4581 ("ca" :default "Data")
4582 ("cs" :default "Datum")
4583 ("da" :default "Dato")
4584 ("de" :default "Datum")
4585 ("eo" :default "Dato")
4586 ("es" :default "Fecha")
4587 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4588 ("hu" :html "D&aacute;tum")
4589 ("is" :default "Dagsetning")
4590 ("it" :default "Data")
4591 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4592 ("nl" :default "Datum")
4593 ("no" :default "Dato")
4594 ("nb" :default "Dato")
4595 ("nn" :default "Dato")
4596 ("pl" :default "Data")
4597 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4598 ("sv" :default "Datum")
4599 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4600 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4601 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4602 ("Equation"
4603 ("fr" :ascii "Equation" :default "Équation"))
4604 ("Figure")
4605 ("Footnotes"
4606 ("ca" :html "Peus de p&agrave;gina")
4607 ("cs" :default "Pozn\xe1mky pod carou")
4608 ("da" :default "Fodnoter")
4609 ("de" :html "Fu&szlig;noten")
4610 ("eo" :default "Piednotoj")
4611 ("es" :html "Pies de p&aacute;gina")
4612 ("fi" :default "Alaviitteet")
4613 ("fr" :default "Notes de bas de page")
4614 ("hu" :html "L&aacute;bjegyzet")
4615 ("is" :html "Aftanm&aacute;lsgreinar")
4616 ("it" :html "Note a pi&egrave; di pagina")
4617 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4618 ("nl" :default "Voetnoten")
4619 ("no" :default "Fotnoter")
4620 ("nb" :default "Fotnoter")
4621 ("nn" :default "Fotnotar")
4622 ("pl" :default "Przypis")
4623 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4624 ("sv" :default "Fotnoter")
4625 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4626 :utf-8 "Примітки")
4627 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4628 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4629 ("List of Listings"
4630 ("fr" :default "Liste des programmes"))
4631 ("List of Tables"
4632 ("fr" :default "Liste des tableaux"))
4633 ("Listing %d:"
4634 ("fr"
4635 :ascii "Programme %d :" :default "Programme nº %d :"
4636 :latin1 "Programme %d :"))
4637 ("Listing %d: %s"
4638 ("fr"
4639 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4640 :latin1 "Programme %d : %s"))
4641 ("See section %s"
4642 ("fr" :default "cf. section %s"))
4643 ("Table %d:"
4644 ("fr"
4645 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4646 ("Table %d: %s"
4647 ("fr"
4648 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4649 :latin1 "Tableau %d : %s"))
4650 ("Table of Contents"
4651 ("ca" :html "&Iacute;ndex")
4652 ("cs" :default "Obsah")
4653 ("da" :default "Indhold")
4654 ("de" :default "Inhaltsverzeichnis")
4655 ("eo" :default "Enhavo")
4656 ("es" :html "&Iacute;ndice")
4657 ("fi" :html "Sis&auml;llysluettelo")
4658 ("fr" :ascii "Sommaire" :default "Table des matières")
4659 ("hu" :html "Tartalomjegyz&eacute;k")
4660 ("is" :default "Efnisyfirlit")
4661 ("it" :default "Indice")
4662 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4663 ("nl" :default "Inhoudsopgave")
4664 ("no" :default "Innhold")
4665 ("nb" :default "Innhold")
4666 ("nn" :default "Innhald")
4667 ("pl" :html "Spis tre&#x015b;ci")
4668 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4669 :utf-8 "Содержание")
4670 ("sv" :html "Inneh&aring;ll")
4671 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4672 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4673 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4674 ("Unknown reference"
4675 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4676 "Dictionary for export engine.
4678 Alist whose CAR is the string to translate and CDR is an alist
4679 whose CAR is the language string and CDR is a plist whose
4680 properties are possible charsets and values translated terms.
4682 It is used as a database for `org-export-translate'. Since this
4683 function returns the string as-is if no translation was found,
4684 the variable only needs to record values different from the
4685 entry.")
4687 (defun org-export-translate (s encoding info)
4688 "Translate string S according to language specification.
4690 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4691 and `:utf-8'. INFO is a plist used as a communication channel.
4693 Translation depends on `:language' property. Return the
4694 translated string. If no translation is found, try to fall back
4695 to `:default' encoding. If it fails, return S."
4696 (let* ((lang (plist-get info :language))
4697 (translations (cdr (assoc lang
4698 (cdr (assoc s org-export-dictionary))))))
4699 (or (plist-get translations encoding)
4700 (plist-get translations :default)
4701 s)))
4705 ;;; The Dispatcher
4707 ;; `org-export-dispatch' is the standard interactive way to start an
4708 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4709 ;; for its interface, which, in turn, delegates response to key
4710 ;; pressed to `org-export-dispatch-action'.
4712 (defvar org-export-dispatch-menu-entries nil
4713 "List of menu entries available for `org-export-dispatch'.
4714 This variable shouldn't be set directly. Set-up :menu-entry
4715 keyword in either `org-export-define-backend' or
4716 `org-export-define-derived-backend' instead.")
4718 ;;;###autoload
4719 (defun org-export-dispatch ()
4720 "Export dispatcher for Org mode.
4722 It provides an access to common export related tasks in a buffer.
4723 Its interface comes in two flavours: standard and expert. While
4724 both share the same set of bindings, only the former displays the
4725 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4726 to switch to one or the other."
4727 (interactive)
4728 (let* ((input (save-window-excursion
4729 (unwind-protect
4730 (org-export-dispatch-ui (list org-export-initial-scope)
4732 org-export-dispatch-use-expert-ui)
4733 (and (get-buffer "*Org Export Dispatcher*")
4734 (kill-buffer "*Org Export Dispatcher*")))))
4735 (action (car input))
4736 (optns (cdr input)))
4737 (case action
4738 ;; First handle special hard-coded actions.
4739 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4740 (publish-current-project
4741 (org-e-publish-current-project (memq 'force optns)))
4742 (publish-choose-project
4743 (org-e-publish (assoc (org-icompleting-read
4744 "Publish project: "
4745 org-e-publish-project-alist nil t)
4746 org-e-publish-project-alist)
4747 (memq 'force optns)))
4748 (publish-all (org-e-publish-all (memq 'force optns)))
4749 (otherwise
4750 (funcall action
4751 (memq 'subtree optns)
4752 (memq 'visible optns)
4753 (memq 'body optns))))))
4755 (defun org-export-dispatch-ui (options first-key expertp)
4756 "Handle interface for `org-export-dispatch'.
4758 OPTIONS is a list containing current interactive options set for
4759 export. It can contain any of the following symbols:
4760 `body' toggles a body-only export
4761 `subtree' restricts export to current subtree
4762 `visible' restricts export to visible part of buffer.
4763 `force' force publishing files.
4765 FIRST-KEY is the key pressed to select the first level menu. It
4766 is nil when this menu hasn't been selected yet.
4768 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4769 buffer is provided, but indications about currently active
4770 options are given in the prompt. Moreover, \[?] allows to switch
4771 back to standard interface."
4772 (let* ((fontify-key
4773 (lambda (key &optional access-key)
4774 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4775 ;; non-nil is the required first-level key to activate
4776 ;; KEY. When its value is t, activate KEY independently
4777 ;; on the first key, if any. A nil value means KEY will
4778 ;; only be activated at first level.
4779 (if (or (eq access-key t) (eq access-key first-key))
4780 (org-add-props key nil 'face 'org-warning)
4781 (org-no-properties key))))
4782 ;; Make sure order of menu doesn't depend on the order in
4783 ;; which back-ends are loaded.
4784 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4785 (lambda (a b) (< (car a) (car b)))))
4786 ;; Compute a list of allowed keys based on the first key
4787 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4788 ;; always available.
4789 (allowed-keys
4790 (nconc (list ?1 ?2 ?3 ?4)
4791 (mapcar 'car
4792 (if (not first-key) backends
4793 (nth 2 (assq first-key backends))))
4794 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4795 ((not first-key) (list ?P)))
4796 (when expertp (list ??))
4797 (list ?q)))
4798 ;; Build the help menu for standard UI.
4799 (help
4800 (unless expertp
4801 (concat
4802 ;; Options are hard-coded.
4803 (format "Options
4804 [%s] Body only: %s [%s] Visible only: %s
4805 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4806 (funcall fontify-key "1" t)
4807 (if (memq 'body options) "On " "Off")
4808 (funcall fontify-key "2" t)
4809 (if (memq 'visible options) "On " "Off")
4810 (funcall fontify-key "3" t)
4811 (if (memq 'subtree options) "Subtree" "Buffer ")
4812 (funcall fontify-key "4" t)
4813 (if (memq 'force options) "On " "Off"))
4814 ;; Display registered back-end entries.
4815 (mapconcat
4816 (lambda (entry)
4817 (let ((top-key (car entry)))
4818 (concat
4819 (format "[%s] %s\n"
4820 (funcall fontify-key (char-to-string top-key))
4821 (nth 1 entry))
4822 (let ((sub-menu (nth 2 entry)))
4823 (unless (functionp sub-menu)
4824 ;; Split sub-menu into two columns.
4825 (let ((index -1))
4826 (concat
4827 (mapconcat
4828 (lambda (sub-entry)
4829 (incf index)
4830 (format (if (zerop (mod index 2)) " [%s] %-24s"
4831 "[%s] %s\n")
4832 (funcall fontify-key
4833 (char-to-string (car sub-entry))
4834 top-key)
4835 (nth 1 sub-entry)))
4836 sub-menu "")
4837 (when (zerop (mod index 2)) "\n"))))))))
4838 backends "\n")
4839 ;; Publishing menu is hard-coded.
4840 (format "\n[%s] Publish
4841 [%s] Current file [%s] Current project
4842 [%s] Choose project [%s] All projects\n\n"
4843 (funcall fontify-key "P")
4844 (funcall fontify-key "f" ?P)
4845 (funcall fontify-key "p" ?P)
4846 (funcall fontify-key "x" ?P)
4847 (funcall fontify-key "a" ?P))
4848 (format "\[%s] %s"
4849 (funcall fontify-key "q" t)
4850 (if first-key "Main menu" "Exit")))))
4851 ;; Build prompts for both standard and expert UI.
4852 (standard-prompt (unless expertp "Export command: "))
4853 (expert-prompt
4854 (when expertp
4855 (format
4856 "Export command (Options: %s%s%s%s) [%s]: "
4857 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4858 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4859 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4860 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4861 (concat allowed-keys)))))
4862 ;; With expert UI, just read key with a fancy prompt. In standard
4863 ;; UI, display an intrusive help buffer.
4864 (if expertp
4865 (org-export-dispatch-action
4866 expert-prompt allowed-keys backends options first-key expertp)
4867 ;; At first call, create frame layout in order to display menu.
4868 (unless (get-buffer "*Org Export Dispatcher*")
4869 (delete-other-windows)
4870 (org-switch-to-buffer-other-window
4871 (get-buffer-create "*Org Export Dispatcher*"))
4872 (setq cursor-type nil))
4873 ;; At this point, the buffer containing the menu exists and is
4874 ;; visible in the current window. So, refresh it.
4875 (with-current-buffer "*Org Export Dispatcher*"
4876 (erase-buffer)
4877 (insert help))
4878 (org-fit-window-to-buffer)
4879 (org-export-dispatch-action
4880 standard-prompt allowed-keys backends options first-key expertp))))
4882 (defun org-export-dispatch-action
4883 (prompt allowed-keys backends options first-key expertp)
4884 "Read a character from command input and act accordingly.
4886 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4887 a list of characters available at a given step in the process.
4888 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4889 EXPERTP are the same as defined in `org-export-dispatch-ui',
4890 which see.
4892 Toggle export options when required. Otherwise, return value is
4893 a list with action as CAR and a list of interactive export
4894 options as CDR."
4895 (let ((key (let ((k (read-char-exclusive prompt)))
4896 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4897 ;; depending on user's key pressed.
4898 (if (< k 27) (+ k 96) k))))
4899 (cond
4900 ;; Ignore non-standard characters (i.e. "M-a") and
4901 ;; undefined associations.
4902 ((not (memq key allowed-keys))
4903 (ding)
4904 (unless expertp (message "Invalid key") (sit-for 1))
4905 (org-export-dispatch-ui options first-key expertp))
4906 ;; q key at first level aborts export. At second
4907 ;; level, cancel first key instead.
4908 ((eq key ?q) (if (not first-key) (error "Export aborted")
4909 (org-export-dispatch-ui options nil expertp)))
4910 ;; Help key: Switch back to standard interface if
4911 ;; expert UI was active.
4912 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4913 ;; Toggle export options.
4914 ((memq key '(?1 ?2 ?3 ?4))
4915 (org-export-dispatch-ui
4916 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4917 (?4 'force))))
4918 (if (memq option options) (remq option options)
4919 (cons option options)))
4920 first-key expertp))
4921 ;; Action selected: Send key and options back to
4922 ;; `org-export-dispatch'.
4923 ((or first-key
4924 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4925 (functionp (nth 2 (assq key backends))))
4926 (cons (cond
4927 ((not first-key) (nth 2 (assq key backends)))
4928 ;; Publishing actions are hard-coded. Send a special
4929 ;; signal to `org-export-dispatch'.
4930 ((eq first-key ?P)
4931 (case key
4932 (?f 'publish-current-file)
4933 (?p 'publish-current-project)
4934 (?x 'publish-choose-project)
4935 (?a 'publish-all)))
4936 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4937 options))
4938 ;; Otherwise, enter sub-menu.
4939 (t (org-export-dispatch-ui options key expertp)))))
4942 (provide 'org-export)
4943 ;;; org-export.el ends here