Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-export.el
blob6f885d18df0c61a214a684935f8a900832a0be37
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)
78 (declare-function org-e-ascii-export-as-ascii "org-e-ascii"
79 (&optional subtreep visible-only body-only ext-plist))
80 (declare-function org-e-ascii-export-to-ascii "org-e-ascii"
81 (&optional subtreep visible-only body-only ext-plist pub-dir))
82 (declare-function org-e-html-export-as-html "org-e-html"
83 (&optional subtreep visible-only body-only ext-plist))
84 (declare-function org-e-html-export-to-html "org-e-html"
85 (&optional subtreep visible-only body-only ext-plist pub-dir))
86 (declare-function org-e-latex-export-as-latex "org-e-latex"
87 (&optional subtreep visible-only body-only ext-plist))
88 (declare-function org-e-latex-export-to-latex "org-e-latex"
89 (&optional subtreep visible-only body-only ext-plist pub-dir))
90 (declare-function org-e-latex-export-to-pdf "org-e-latex"
91 (&optional subtreep visible-only body-only ext-plist pub-dir))
92 (declare-function org-e-odt-export-to-odt "org-e-odt"
93 (&optional subtreep visible-only body-only ext-plist pub-dir))
94 (declare-function org-e-publish "org-e-publish" (project &optional force))
95 (declare-function org-e-publish-all "org-e-publish" (&optional force))
96 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
97 (declare-function org-e-publish-current-project "org-e-publish"
98 (&optional force))
99 (declare-function org-export-blocks-preprocess "org-exp-blocks")
101 (defvar org-e-publish-project-alist)
102 (defvar org-table-number-fraction)
103 (defvar org-table-number-regexp)
107 ;;; Internal Variables
109 ;; Among internal variables, the most important is
110 ;; `org-export-options-alist'. This variable define the global export
111 ;; options, shared between every exporter, and how they are acquired.
113 (defconst org-export-max-depth 19
114 "Maximum nesting depth for headlines, counting from 0.")
116 (defconst org-export-options-alist
117 '((:author "AUTHOR" nil user-full-name t)
118 (:creator "CREATOR" nil org-export-creator-string)
119 (:date "DATE" nil nil t)
120 (:description "DESCRIPTION" nil nil newline)
121 (:email "EMAIL" nil user-mail-address t)
122 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
123 (:headline-levels nil "H" org-export-headline-levels)
124 (:keywords "KEYWORDS" nil nil space)
125 (:language "LANGUAGE" nil org-export-default-language t)
126 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
127 (:section-numbers nil "num" org-export-with-section-numbers)
128 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
129 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
130 (:title "TITLE" nil nil space)
131 (:with-archived-trees nil "arch" org-export-with-archived-trees)
132 (:with-author nil "author" org-export-with-author)
133 (:with-clocks nil "c" org-export-with-clocks)
134 (:with-creator nil "creator" org-export-with-creator)
135 (:with-drawers nil "d" org-export-with-drawers)
136 (:with-email nil "email" org-export-with-email)
137 (:with-emphasize nil "*" org-export-with-emphasize)
138 (:with-entities nil "e" org-export-with-entities)
139 (:with-fixed-width nil ":" org-export-with-fixed-width)
140 (:with-footnotes nil "f" org-export-with-footnotes)
141 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
142 (:with-plannings nil "p" org-export-with-planning)
143 (:with-priority nil "pri" org-export-with-priority)
144 (:with-special-strings nil "-" org-export-with-special-strings)
145 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
146 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
147 (:with-toc nil "toc" org-export-with-toc)
148 (:with-tables nil "|" org-export-with-tables)
149 (:with-tags nil "tags" org-export-with-tags)
150 (:with-tasks nil "tasks" org-export-with-tasks)
151 (:with-timestamps nil "<" org-export-with-timestamps)
152 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
153 "Alist between export properties and ways to set them.
155 The CAR of the alist is the property name, and the CDR is a list
156 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
158 KEYWORD is a string representing a buffer keyword, or nil. Each
159 property defined this way can also be set, during subtree
160 export, through an headline property named after the keyword
161 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
162 property).
163 OPTION is a string that could be found in an #+OPTIONS: line.
164 DEFAULT is the default value for the property.
165 BEHAVIOUR determine how Org should handle multiple keywords for
166 the same property. It is a symbol among:
167 nil Keep old value and discard the new one.
168 t Replace old value with the new one.
169 `space' Concatenate the values, separating them with a space.
170 `newline' Concatenate the values, separating them with
171 a newline.
172 `split' Split values at white spaces, and cons them to the
173 previous list.
175 KEYWORD and OPTION have precedence over DEFAULT.
177 All these properties should be back-end agnostic. Back-end
178 specific properties are set through `org-export-define-backend'.
179 Properties redefined there have precedence over these.")
181 (defconst org-export-special-keywords '("SETUP_FILE" "OPTIONS")
182 "List of in-buffer keywords that require special treatment.
183 These keywords are not directly associated to a property. The
184 way they are handled must be hard-coded into
185 `org-export--get-inbuffer-options' function.")
187 (defconst org-export-filters-alist
188 '((:filter-bold . org-export-filter-bold-functions)
189 (:filter-babel-call . org-export-filter-babel-call-functions)
190 (:filter-center-block . org-export-filter-center-block-functions)
191 (:filter-clock . org-export-filter-clock-functions)
192 (:filter-code . org-export-filter-code-functions)
193 (:filter-comment . org-export-filter-comment-functions)
194 (:filter-comment-block . org-export-filter-comment-block-functions)
195 (:filter-drawer . org-export-filter-drawer-functions)
196 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
197 (:filter-entity . org-export-filter-entity-functions)
198 (:filter-example-block . org-export-filter-example-block-functions)
199 (:filter-export-block . org-export-filter-export-block-functions)
200 (:filter-export-snippet . org-export-filter-export-snippet-functions)
201 (:filter-final-output . org-export-filter-final-output-functions)
202 (:filter-fixed-width . org-export-filter-fixed-width-functions)
203 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
204 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
205 (:filter-headline . org-export-filter-headline-functions)
206 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
207 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
208 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
209 (:filter-inlinetask . org-export-filter-inlinetask-functions)
210 (:filter-italic . org-export-filter-italic-functions)
211 (:filter-item . org-export-filter-item-functions)
212 (:filter-keyword . org-export-filter-keyword-functions)
213 (:filter-latex-environment . org-export-filter-latex-environment-functions)
214 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
215 (:filter-line-break . org-export-filter-line-break-functions)
216 (:filter-link . org-export-filter-link-functions)
217 (:filter-macro . org-export-filter-macro-functions)
218 (:filter-node-property . org-export-filter-node-property-functions)
219 (:filter-paragraph . org-export-filter-paragraph-functions)
220 (:filter-parse-tree . org-export-filter-parse-tree-functions)
221 (:filter-plain-list . org-export-filter-plain-list-functions)
222 (:filter-plain-text . org-export-filter-plain-text-functions)
223 (:filter-planning . org-export-filter-planning-functions)
224 (:filter-property-drawer . org-export-filter-property-drawer-functions)
225 (:filter-quote-block . org-export-filter-quote-block-functions)
226 (:filter-quote-section . org-export-filter-quote-section-functions)
227 (:filter-radio-target . org-export-filter-radio-target-functions)
228 (:filter-section . org-export-filter-section-functions)
229 (:filter-special-block . org-export-filter-special-block-functions)
230 (:filter-src-block . org-export-filter-src-block-functions)
231 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
232 (:filter-strike-through . org-export-filter-strike-through-functions)
233 (:filter-subscript . org-export-filter-subscript-functions)
234 (:filter-superscript . org-export-filter-superscript-functions)
235 (:filter-table . org-export-filter-table-functions)
236 (:filter-table-cell . org-export-filter-table-cell-functions)
237 (:filter-table-row . org-export-filter-table-row-functions)
238 (:filter-target . org-export-filter-target-functions)
239 (:filter-timestamp . org-export-filter-timestamp-functions)
240 (:filter-underline . org-export-filter-underline-functions)
241 (:filter-verbatim . org-export-filter-verbatim-functions)
242 (:filter-verse-block . org-export-filter-verse-block-functions))
243 "Alist between filters properties and initial values.
245 The key of each association is a property name accessible through
246 the communication channel. Its value is a configurable global
247 variable defining initial filters.
249 This list is meant to install user specified filters. Back-end
250 developers may install their own filters using
251 `org-export-define-backend'. Filters defined there will always
252 be prepended to the current list, so they always get applied
253 first.")
255 (defconst org-export-default-inline-image-rule
256 `(("file" .
257 ,(format "\\.%s\\'"
258 (regexp-opt
259 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
260 "xpm" "pbm" "pgm" "ppm") t))))
261 "Default rule for link matching an inline image.
262 This rule applies to links with no description. By default, it
263 will be considered as an inline image if it targets a local file
264 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
265 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
266 See `org-export-inline-image-p' for more information about
267 rules.")
271 ;;; User-configurable Variables
273 ;; Configuration for the masses.
275 ;; They should never be accessed directly, as their value is to be
276 ;; stored in a property list (cf. `org-export-options-alist').
277 ;; Back-ends will read their value from there instead.
279 (defgroup org-export nil
280 "Options for exporting Org mode files."
281 :tag "Org Export"
282 :group 'org)
284 (defgroup org-export-general nil
285 "General options for export engine."
286 :tag "Org Export General"
287 :group 'org-export)
289 (defcustom org-export-with-archived-trees 'headline
290 "Whether sub-trees with the ARCHIVE tag should be exported.
292 This can have three different values:
293 nil Do not export, pretend this tree is not present.
294 t Do export the entire tree.
295 `headline' Only export the headline, but skip the tree below it.
297 This option can also be set with the #+OPTIONS line,
298 e.g. \"arch:nil\"."
299 :group 'org-export-general
300 :type '(choice
301 (const :tag "Not at all" nil)
302 (const :tag "Headline only" 'headline)
303 (const :tag "Entirely" t)))
305 (defcustom org-export-with-author t
306 "Non-nil means insert author name into the exported file.
307 This option can also be set with the #+OPTIONS line,
308 e.g. \"author:nil\"."
309 :group 'org-export-general
310 :type 'boolean)
312 (defcustom org-export-with-clocks nil
313 "Non-nil means export CLOCK keywords.
314 This option can also be set with the #+OPTIONS line,
315 e.g. \"c:t\"."
316 :group 'org-export-general
317 :type 'boolean)
319 (defcustom org-export-with-creator 'comment
320 "Non-nil means the postamble should contain a creator sentence.
322 The sentence can be set in `org-export-creator-string' and
323 defaults to \"Generated by Org mode XX in Emacs XXX.\".
325 If the value is `comment' insert it as a comment."
326 :group 'org-export-general
327 :type '(choice
328 (const :tag "No creator sentence" nil)
329 (const :tag "Sentence as a comment" 'comment)
330 (const :tag "Insert the sentence" t)))
332 (defcustom org-export-creator-string
333 (format "Generated by Org mode %s in Emacs %s."
334 (if (fboundp 'org-version) (org-version) "(Unknown)")
335 emacs-version)
336 "String to insert at the end of the generated document."
337 :group 'org-export-general
338 :type '(string :tag "Creator string"))
340 (defcustom org-export-with-drawers t
341 "Non-nil means export contents of standard drawers.
343 When t, all drawers are exported. This may also be a list of
344 drawer names to export. This variable doesn't apply to
345 properties drawers.
347 This option can also be set with the #+OPTIONS line,
348 e.g. \"d:nil\"."
349 :group 'org-export-general
350 :type '(choice
351 (const :tag "All drawers" t)
352 (const :tag "None" nil)
353 (repeat :tag "Selected drawers"
354 (string :tag "Drawer name"))))
356 (defcustom org-export-with-email nil
357 "Non-nil means insert author email into the exported file.
358 This option can also be set with the #+OPTIONS line,
359 e.g. \"email:t\"."
360 :group 'org-export-general
361 :type 'boolean)
363 (defcustom org-export-with-emphasize t
364 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
366 If the export target supports emphasizing text, the word will be
367 typeset in bold, italic, or underlined, respectively. Not all
368 export backends support this.
370 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
371 :group 'org-export-general
372 :type 'boolean)
374 (defcustom org-export-exclude-tags '("noexport")
375 "Tags that exclude a tree from export.
377 All trees carrying any of these tags will be excluded from
378 export. This is without condition, so even subtrees inside that
379 carry one of the `org-export-select-tags' will be removed.
381 This option can also be set with the #+EXCLUDE_TAGS: keyword."
382 :group 'org-export-general
383 :type '(repeat (string :tag "Tag")))
385 (defcustom org-export-with-fixed-width t
386 "Non-nil means lines starting with \":\" will be in fixed width font.
388 This can be used to have pre-formatted text, fragments of code
389 etc. For example:
390 : ;; Some Lisp examples
391 : (while (defc cnt)
392 : (ding))
393 will be looking just like this in also HTML. See also the QUOTE
394 keyword. Not all export backends support this.
396 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
397 :group 'org-export-translation
398 :type 'boolean)
400 (defcustom org-export-with-footnotes t
401 "Non-nil means Org footnotes should be exported.
402 This option can also be set with the #+OPTIONS line,
403 e.g. \"f:nil\"."
404 :group 'org-export-general
405 :type 'boolean)
407 (defcustom org-export-headline-levels 3
408 "The last level which is still exported as a headline.
410 Inferior levels will produce itemize lists when exported.
412 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
413 :group 'org-export-general
414 :type 'integer)
416 (defcustom org-export-default-language "en"
417 "The default language for export and clocktable translations, as a string.
418 This may have an association in
419 `org-clock-clocktable-language-setup'."
420 :group 'org-export-general
421 :type '(string :tag "Language"))
423 (defcustom org-export-preserve-breaks nil
424 "Non-nil means preserve all line breaks when exporting.
426 Normally, in HTML output paragraphs will be reformatted.
428 This option can also be set with the #+OPTIONS line,
429 e.g. \"\\n:t\"."
430 :group 'org-export-general
431 :type 'boolean)
433 (defcustom org-export-with-entities t
434 "Non-nil means interpret entities when exporting.
436 For example, HTML export converts \\alpha to &alpha; and \\AA to
437 &Aring;.
439 For a list of supported names, see the constant `org-entities'
440 and the user option `org-entities-user'.
442 This option can also be set with the #+OPTIONS line,
443 e.g. \"e:nil\"."
444 :group 'org-export-general
445 :type 'boolean)
447 (defcustom org-export-with-inlinetasks t
448 "Non-nil means inlinetasks should be exported.
449 This option can also be set with the #+OPTIONS line,
450 e.g. \"inline:nil\"."
451 :group 'org-export-general
452 :type 'boolean)
454 (defcustom org-export-with-planning nil
455 "Non-nil means include planning info in export.
456 This option can also be set with the #+OPTIONS: line,
457 e.g. \"p:t\"."
458 :group 'org-export-general
459 :type 'boolean)
461 (defcustom org-export-with-priority nil
462 "Non-nil means include priority cookies in export.
463 This option can also be set with the #+OPTIONS line,
464 e.g. \"pri:t\"."
465 :group 'org-export-general
466 :type 'boolean)
468 (defcustom org-export-with-section-numbers t
469 "Non-nil means add section numbers to headlines when exporting.
471 When set to an integer n, numbering will only happen for
472 headlines whose relative level is higher or equal to n.
474 This option can also be set with the #+OPTIONS line,
475 e.g. \"num:t\"."
476 :group 'org-export-general
477 :type 'boolean)
479 (defcustom org-export-select-tags '("export")
480 "Tags that select a tree for export.
482 If any such tag is found in a buffer, all trees that do not carry
483 one of these tags will be ignored during export. Inside trees
484 that are selected like this, you can still deselect a subtree by
485 tagging it with one of the `org-export-exclude-tags'.
487 This option can also be set with the #+SELECT_TAGS: keyword."
488 :group 'org-export-general
489 :type '(repeat (string :tag "Tag")))
491 (defcustom org-export-with-special-strings t
492 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
494 When this option is turned on, these strings will be exported as:
496 Org HTML LaTeX UTF-8
497 -----+----------+--------+-------
498 \\- &shy; \\-
499 -- &ndash; -- –
500 --- &mdash; --- —
501 ... &hellip; \\ldots …
503 This option can also be set with the #+OPTIONS line,
504 e.g. \"-:nil\"."
505 :group 'org-export-general
506 :type 'boolean)
508 (defcustom org-export-with-statistics-cookies t
509 "Non-nil means include statistics cookies in export.
510 This option can also be set with the #+OPTIONS: line,
511 e.g. \"stat:nil\""
512 :group 'org-export-general
513 :type 'boolean)
515 (defcustom org-export-with-sub-superscripts t
516 "Non-nil means interpret \"_\" and \"^\" for export.
518 When this option is turned on, you can use TeX-like syntax for
519 sub- and superscripts. Several characters after \"_\" or \"^\"
520 will be considered as a single item - so grouping with {} is
521 normally not needed. For example, the following things will be
522 parsed as single sub- or superscripts.
524 10^24 or 10^tau several digits will be considered 1 item.
525 10^-12 or 10^-tau a leading sign with digits or a word
526 x^2-y^3 will be read as x^2 - y^3, because items are
527 terminated by almost any nonword/nondigit char.
528 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
530 Still, ambiguity is possible - so when in doubt use {} to enclose
531 the sub/superscript. If you set this variable to the symbol
532 `{}', the braces are *required* in order to trigger
533 interpretations as sub/superscript. This can be helpful in
534 documents that need \"_\" frequently in plain text.
536 This option can also be set with the #+OPTIONS line,
537 e.g. \"^:nil\"."
538 :group 'org-export-general
539 :type '(choice
540 (const :tag "Interpret them" t)
541 (const :tag "Curly brackets only" {})
542 (const :tag "Do not interpret them" nil)))
544 (defcustom org-export-with-toc t
545 "Non-nil means create a table of contents in exported files.
547 The TOC contains headlines with levels up
548 to`org-export-headline-levels'. When an integer, include levels
549 up to N in the toc, this may then be different from
550 `org-export-headline-levels', but it will not be allowed to be
551 larger than the number of headline levels. When nil, no table of
552 contents is made.
554 This option can also be set with the #+OPTIONS line,
555 e.g. \"toc:nil\" or \"toc:3\"."
556 :group 'org-export-general
557 :type '(choice
558 (const :tag "No Table of Contents" nil)
559 (const :tag "Full Table of Contents" t)
560 (integer :tag "TOC to level")))
562 (defcustom org-export-with-tables t
563 "If non-nil, lines starting with \"|\" define a table.
564 For example:
566 | Name | Address | Birthday |
567 |-------------+----------+-----------|
568 | Arthur Dent | England | 29.2.2100 |
570 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
571 :group 'org-export-general
572 :type 'boolean)
574 (defcustom org-export-with-tags t
575 "If nil, do not export tags, just remove them from headlines.
577 If this is the symbol `not-in-toc', tags will be removed from
578 table of contents entries, but still be shown in the headlines of
579 the document.
581 This option can also be set with the #+OPTIONS line,
582 e.g. \"tags:nil\"."
583 :group 'org-export-general
584 :type '(choice
585 (const :tag "Off" nil)
586 (const :tag "Not in TOC" not-in-toc)
587 (const :tag "On" t)))
589 (defcustom org-export-with-tasks t
590 "Non-nil means include TODO items for export.
591 This may have the following values:
592 t include tasks independent of state.
593 todo include only tasks that are not yet done.
594 done include only tasks that are already done.
595 nil remove all tasks before export
596 list of keywords keep only tasks with these keywords"
597 :group 'org-export-general
598 :type '(choice
599 (const :tag "All tasks" t)
600 (const :tag "No tasks" nil)
601 (const :tag "Not-done tasks" todo)
602 (const :tag "Only done tasks" done)
603 (repeat :tag "Specific TODO keywords"
604 (string :tag "Keyword"))))
606 (defcustom org-export-time-stamp-file t
607 "Non-nil means insert a time stamp into the exported file.
608 The time stamp shows when the file was created.
610 This option can also be set with the #+OPTIONS line,
611 e.g. \"timestamp:nil\"."
612 :group 'org-export-general
613 :type 'boolean)
615 (defcustom org-export-with-timestamps t
616 "Non nil means allow timestamps in export.
618 It can be set to `active', `inactive', t or nil, in order to
619 export, respectively, only active timestamps, only inactive ones,
620 all of them or none.
622 This option can also be set with the #+OPTIONS line, e.g.
623 \"<:nil\"."
624 :group 'org-export-general
625 :type '(choice
626 (const :tag "All timestamps" t)
627 (const :tag "Only active timestamps" active)
628 (const :tag "Only inactive timestamps" inactive)
629 (const :tag "No timestamp" nil)))
631 (defcustom org-export-with-todo-keywords t
632 "Non-nil means include TODO keywords in export.
633 When nil, remove all these keywords from the export."
634 :group 'org-export-general
635 :type 'boolean)
637 (defcustom org-export-allow-BIND 'confirm
638 "Non-nil means allow #+BIND to define local variable values for export.
639 This is a potential security risk, which is why the user must
640 confirm the use of these lines."
641 :group 'org-export-general
642 :type '(choice
643 (const :tag "Never" nil)
644 (const :tag "Always" t)
645 (const :tag "Ask a confirmation for each file" confirm)))
647 (defcustom org-export-snippet-translation-alist nil
648 "Alist between export snippets back-ends and exporter back-ends.
650 This variable allows to provide shortcuts for export snippets.
652 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
653 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
654 HTML code while every other back-end will ignore it."
655 :group 'org-export-general
656 :type '(repeat
657 (cons
658 (string :tag "Shortcut")
659 (string :tag "Back-end"))))
661 (defcustom org-export-coding-system nil
662 "Coding system for the exported file."
663 :group 'org-export-general
664 :type 'coding-system)
666 (defcustom org-export-copy-to-kill-ring t
667 "Non-nil means exported stuff will also be pushed onto the kill ring."
668 :group 'org-export-general
669 :type 'boolean)
671 (defcustom org-export-initial-scope 'buffer
672 "The initial scope when exporting with `org-export-dispatch'.
673 This variable can be either set to `buffer' or `subtree'."
674 :group 'org-export-general
675 :type '(choice
676 (const :tag "Export current buffer" 'buffer)
677 (const :tag "Export current subtree" 'subtree)))
679 (defcustom org-export-show-temporary-export-buffer t
680 "Non-nil means show buffer after exporting to temp buffer.
681 When Org exports to a file, the buffer visiting that file is ever
682 shown, but remains buried. However, when exporting to
683 a temporary buffer, that buffer is popped up in a second window.
684 When this variable is nil, the buffer remains buried also in
685 these cases."
686 :group 'org-export-general
687 :type 'boolean)
689 (defcustom org-export-dispatch-use-expert-ui nil
690 "Non-nil means using a non-intrusive `org-export-dispatch'.
691 In that case, no help buffer is displayed. Though, an indicator
692 for current export scope is added to the prompt \(i.e. \"b\" when
693 output is restricted to body only, \"s\" when it is restricted to
694 the current subtree and \"v\" when only visible elements are
695 considered for export\). Also, \[?] allows to switch back to
696 standard mode."
697 :group 'org-export-general
698 :type 'boolean)
702 ;;; Defining New Back-ends
704 ;; `org-export-define-backend' is the standard way to define an export
705 ;; back-end. It allows to specify translators, filters, buffer
706 ;; options and a menu entry. If the new back-end shares translators
707 ;; with another back-end, `org-export-define-derived-backend' may be
708 ;; used instead.
710 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
711 ;; when a given back-end hasn't been registered yet.
713 (defmacro org-export-define-backend (backend translators &rest body)
714 "Define a new back-end BACKEND.
716 TRANSLATORS is an alist between object or element types and
717 functions handling them.
719 These functions should return a string without any trailing
720 space, or nil. They must accept three arguments: the object or
721 element itself, its contents or nil when it isn't recursive and
722 the property list used as a communication channel.
724 Contents, when not nil, are stripped from any global indentation
725 \(although the relative one is preserved). They also always end
726 with a single newline character.
728 If, for a given type, no function is found, that element or
729 object type will simply be ignored, along with any blank line or
730 white space at its end. The same will happen if the function
731 returns the nil value. If that function returns the empty
732 string, the type will be ignored, but the blank lines or white
733 spaces will be kept.
735 In addition to element and object types, one function can be
736 associated to the `template' symbol and another one to the
737 `plain-text' symbol.
739 The former returns the final transcoded string, and can be used
740 to add a preamble and a postamble to document's body. It must
741 accept two arguments: the transcoded string and the property list
742 containing export options.
744 The latter, when defined, is to be called on every text not
745 recognized as an element or an object. It must accept two
746 arguments: the text string and the information channel. It is an
747 appropriate place to protect special chars relative to the
748 back-end.
750 BODY can start with pre-defined keyword arguments. The following
751 keywords are understood:
753 :export-block
755 String, or list of strings, representing block names that
756 will not be parsed. This is used to specify blocks that will
757 contain raw code specific to the back-end. These blocks
758 still have to be handled by the relative `export-block' type
759 translator.
761 :filters-alist
763 Alist between filters and function, or list of functions,
764 specific to the back-end. See `org-export-filters-alist' for
765 a list of all allowed filters. Filters defined here
766 shouldn't make a back-end test, as it may prevent back-ends
767 derived from this one to behave properly.
769 :menu-entry
771 Menu entry for the export dispatcher. It should be a list
772 like:
774 \(KEY DESCRIPTION ACTION-OR-MENU)
776 where :
778 KEY is a free character selecting the back-end.
779 DESCRIPTION is a string naming the back-end.
780 ACTION-OR-MENU is either a function or an alist.
782 If it is an action, it will be called with three arguments:
783 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
784 for further explanations.
786 If it is an alist, associations should follow the
787 pattern:
789 \(KEY DESCRIPTION ACTION)
791 where KEY, DESCRIPTION and ACTION are described above.
793 Valid values include:
795 \(?m \"My Special Back-end\" my-special-export-function)
799 \(?l \"Export to LaTeX\"
800 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
801 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
802 \(?p \"PDF file\" org-e-latex-export-to-pdf)
803 \(?o \"PDF file and open\"
804 \(lambda (subtree visible body-only)
805 \(org-open-file
806 \(org-e-latex-export-to-pdf subtree visible body-only))))))
808 :options-alist
810 Alist between back-end specific properties introduced in
811 communication channel and how their value are acquired. See
812 `org-export-options-alist' for more information about
813 structure of the values."
814 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
815 (indent 1))
816 (let (export-block filters menu-entry options)
817 (while (keywordp (car body))
818 (case (pop body)
819 (:export-block (let ((names (pop body)))
820 (setq export-block
821 (if (consp names) (mapcar 'upcase names)
822 (list (upcase names))))))
823 (:filters-alist (setq filters (pop body)))
824 (:menu-entry (setq menu-entry (pop body)))
825 (:options-alist (setq options (pop body)))
826 (t (pop body))))
827 `(progn
828 ;; Define translators.
829 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
830 "Alist between element or object types and translators.")
831 ;; Define options.
832 ,(when options
833 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
834 ,(format "Alist between %s export properties and ways to set them.
835 See `org-export-options-alist' for more information on the
836 structure of the values."
837 backend)))
838 ;; Define filters.
839 ,(when filters
840 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
841 "Alist between filters keywords and back-end specific filters.
842 See `org-export-filters-alist' for more information."))
843 ;; Tell parser to not parse EXPORT-BLOCK blocks.
844 ,(when export-block
845 `(mapc
846 (lambda (name)
847 (add-to-list 'org-element-block-name-alist
848 `(,name . org-element-export-block-parser)))
849 ',export-block))
850 ;; Add an entry for back-end in `org-export-dispatch'.
851 ,(when menu-entry
852 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
853 (unless menu
854 `(push ',menu-entry org-export-dispatch-menu-entries))))
855 ;; Splice in the body, if any.
856 ,@body)))
858 (defmacro org-export-define-derived-backend (child parent &rest body)
859 "Create a new back-end as a variant of an existing one.
861 CHILD is the name of the derived back-end. PARENT is the name of
862 the parent back-end.
864 BODY can start with pre-defined keyword arguments. The following
865 keywords are understood:
867 :export-block
869 String, or list of strings, representing block names that
870 will not be parsed. This is used to specify blocks that will
871 contain raw code specific to the back-end. These blocks
872 still have to be handled by the relative `export-block' type
873 translator.
875 :filters-alist
877 Alist of filters that will overwrite or complete filters
878 defined in PARENT back-end. See `org-export-filters-alist'
879 for a list of allowed filters.
881 :menu-entry
883 Menu entry for the export dispatcher. See
884 `org-export-define-backend' for more information about the
885 expected value.
887 :options-alist
889 Alist of back-end specific properties that will overwrite or
890 complete those defined in PARENT back-end. Refer to
891 `org-export-options-alist' for more information about
892 structure of the values.
894 :sub-menu-entry
896 Append entries to an existing menu in the export dispatcher.
897 The associated value should be a list whose CAR is the
898 character selecting the menu to expand and CDR a list of
899 entries following the pattern:
901 \(KEY DESCRIPTION ACTION)
903 where KEY is a free character triggering the action,
904 DESCRIPTION is a string defining the action, and ACTION is
905 a function that will be called with three arguments:
906 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
907 for further explanations.
909 Valid values include:
911 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
912 \(?O \"As PDF file and open (Beamer)\"
913 \(lambda (s v b)
914 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
916 :translate-alist
918 Alist of element and object types and transcoders that will
919 overwrite or complete transcode table from PARENT back-end.
920 Refer to `org-export-define-backend' for detailed information
921 about transcoders.
923 As an example, here is how one could define \"my-latex\" back-end
924 as a variant of `e-latex' back-end with a custom template
925 function:
927 \(org-export-define-derived-backend my-latex e-latex
928 :translate-alist ((template . my-latex-template-fun)))
930 The back-end could then be called with, for example:
932 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
933 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
934 (indent 2))
935 (let (export-block filters menu-entry options sub-menu-entry translate)
936 (while (keywordp (car body))
937 (case (pop body)
938 (:export-block (let ((names (pop body)))
939 (setq export-block
940 (if (consp names) (mapcar 'upcase names)
941 (list (upcase names))))))
942 (:filters-alist (setq filters (pop body)))
943 (:menu-entry (setq menu-entry (pop body)))
944 (:options-alist (setq options (pop body)))
945 (:sub-menu-entry (setq sub-menu-entry (pop body)))
946 (:translate-alist (setq translate (pop body)))
947 (t (pop body))))
948 `(progn
949 ;; Tell parser to not parse EXPORT-BLOCK blocks.
950 ,(when export-block
951 `(mapc
952 (lambda (name)
953 (add-to-list 'org-element-block-name-alist
954 `(,name . org-element-export-block-parser)))
955 ',export-block))
956 ;; Define filters.
957 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
958 (when (or (boundp parent-filters) filters)
959 `(defconst ,(intern (format "org-%s-filters-alist" child))
960 ',(append filters
961 (and (boundp parent-filters)
962 (copy-sequence (symbol-value parent-filters))))
963 "Alist between filters keywords and back-end specific filters.
964 See `org-export-filters-alist' for more information.")))
965 ;; Define options.
966 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
967 (when (or (boundp parent-options) options)
968 `(defconst ,(intern (format "org-%s-options-alist" child))
969 ',(append options
970 (and (boundp parent-options)
971 (copy-sequence (symbol-value parent-options))))
972 ,(format "Alist between %s export properties and ways to set them.
973 See `org-export-options-alist' for more information on the
974 structure of the values."
975 child))))
976 ;; Define translators.
977 (defvar ,(intern (format "org-%s-translate-alist" child))
978 ',(append translate
979 (copy-sequence
980 (symbol-value
981 (intern (format "org-%s-translate-alist" parent)))))
982 "Alist between element or object types and translators.")
983 ;; Add an entry for back-end in `org-export-dispatch'.
984 ,(when menu-entry
985 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
986 (unless menu
987 `(push ',menu-entry org-export-dispatch-menu-entries))))
988 ,(when sub-menu-entry
989 (let ((menu (nth 2 (assq (car sub-menu-entry)
990 org-export-dispatch-menu-entries))))
991 (when menu `(nconc ',menu
992 ',(org-remove-if (lambda (e) (member e menu))
993 (cdr sub-menu-entry))))))
994 ;; Splice in the body, if any.
995 ,@body)))
997 (defun org-export-barf-if-invalid-backend (backend)
998 "Signal an error if BACKEND isn't defined."
999 (unless (boundp (intern (format "org-%s-translate-alist" backend)))
1000 (error "Unknown \"%s\" back-end: Aborting export" backend)))
1004 ;;; The Communication Channel
1006 ;; During export process, every function has access to a number of
1007 ;; properties. They are of two types:
1009 ;; 1. Environment options are collected once at the very beginning of
1010 ;; the process, out of the original buffer and configuration.
1011 ;; Collecting them is handled by `org-export-get-environment'
1012 ;; function.
1014 ;; Most environment options are defined through the
1015 ;; `org-export-options-alist' variable.
1017 ;; 2. Tree properties are extracted directly from the parsed tree,
1018 ;; just before export, by `org-export-collect-tree-properties'.
1020 ;; Here is the full list of properties available during transcode
1021 ;; process, with their category and their value type.
1023 ;; + `:author' :: Author's name.
1024 ;; - category :: option
1025 ;; - type :: string
1027 ;; + `:back-end' :: Current back-end used for transcoding.
1028 ;; - category :: tree
1029 ;; - type :: symbol
1031 ;; + `:creator' :: String to write as creation information.
1032 ;; - category :: option
1033 ;; - type :: string
1035 ;; + `:date' :: String to use as date.
1036 ;; - category :: option
1037 ;; - type :: string
1039 ;; + `:description' :: Description text for the current data.
1040 ;; - category :: option
1041 ;; - type :: string
1043 ;; + `:email' :: Author's email.
1044 ;; - category :: option
1045 ;; - type :: string
1047 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1048 ;; process.
1049 ;; - category :: option
1050 ;; - type :: list of strings
1052 ;; + `:exported-data' :: Hash table used for memoizing
1053 ;; `org-export-data'.
1054 ;; - category :: tree
1055 ;; - type :: hash table
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-special-strings' :: Non-nil means transcoding should
1210 ;; interpret special strings in plain text.
1211 ;; - category :: option
1212 ;; - type :: symbol (nil, t)
1214 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1215 ;; interpret subscript and superscript. With a value of "{}",
1216 ;; only interpret those using curly brackets.
1217 ;; - category :: option
1218 ;; - type :: symbol (nil, {}, t)
1220 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1221 ;; tables.
1222 ;; - category :: option
1223 ;; - type :: symbol (nil, t)
1225 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1226 ;; headlines. A `not-in-toc' value will remove them from the
1227 ;; table of contents, if any, nonetheless.
1228 ;; - category :: option
1229 ;; - type :: symbol (nil, t, `not-in-toc')
1231 ;; + `:with-tasks' :: Non-nil means transcoding should include
1232 ;; headlines with a TODO keyword. A `todo' value will only
1233 ;; include headlines with a todo type keyword while a `done'
1234 ;; value will do the contrary. If a list of strings is provided,
1235 ;; only tasks with keywords belonging to that list will be kept.
1236 ;; - category :: option
1237 ;; - type :: symbol (t, todo, done, nil) or list of strings
1239 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1240 ;; time stamps. Special value `active' (resp. `inactive') ask to
1241 ;; export only active (resp. inactive) timestamps. Otherwise,
1242 ;; completely remove them.
1243 ;; - category :: option
1244 ;; - type :: symbol: (`active', `inactive', t, nil)
1246 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1247 ;; added to the output. An integer value limits its depth.
1248 ;; - category :: option
1249 ;; - type :: symbol (nil, t or integer)
1251 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1252 ;; include TODO keywords.
1253 ;; - category :: option
1254 ;; - type :: symbol (nil, t)
1257 ;;;; Environment Options
1259 ;; Environment options encompass all parameters defined outside the
1260 ;; scope of the parsed data. They come from five sources, in
1261 ;; increasing precedence order:
1263 ;; - Global variables,
1264 ;; - Buffer's attributes,
1265 ;; - Options keyword symbols,
1266 ;; - Buffer keywords,
1267 ;; - Subtree properties.
1269 ;; The central internal function with regards to environment options
1270 ;; is `org-export-get-environment'. It updates global variables with
1271 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1272 ;; the different sources.
1274 ;; The internal functions doing the retrieval are:
1275 ;; `org-export--get-global-options',
1276 ;; `org-export--get-buffer-attributes',
1277 ;; `org-export--parse-option-keyword',
1278 ;; `org-export--get-subtree-options' and
1279 ;; `org-export--get-inbuffer-options'
1281 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1282 ;; take care of the part relative to "#+BIND:" keywords.
1284 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1285 "Collect export options from the current buffer.
1287 Optional argument BACKEND is a symbol specifying which back-end
1288 specific options to read, if any.
1290 When optional argument SUBTREEP is non-nil, assume the export is
1291 done against the current sub-tree.
1293 Third optional argument EXT-PLIST is a property list with
1294 external parameters overriding Org default settings, but still
1295 inferior to file-local settings."
1296 ;; First install #+BIND variables.
1297 (org-export--install-letbind-maybe)
1298 ;; Get and prioritize export options...
1299 (org-combine-plists
1300 ;; ... from global variables...
1301 (org-export--get-global-options backend)
1302 ;; ... from buffer's attributes...
1303 (org-export--get-buffer-attributes)
1304 ;; ... from an external property list...
1305 ext-plist
1306 ;; ... from in-buffer settings...
1307 (org-export--get-inbuffer-options
1308 backend
1309 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1310 ;; ... and from subtree, when appropriate.
1311 (and subtreep (org-export--get-subtree-options backend))
1312 ;; Eventually install back-end symbol and its translation table.
1313 `(:back-end
1314 ,backend
1315 :translate-alist
1316 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1317 (when (boundp trans-alist) (symbol-value trans-alist))))))
1319 (defun org-export--parse-option-keyword (options &optional backend)
1320 "Parse an OPTIONS line and return values as a plist.
1321 Optional argument BACKEND is a symbol specifying which back-end
1322 specific items to read, if any."
1323 (let* ((all
1324 (append org-export-options-alist
1325 (and backend
1326 (let ((var (intern
1327 (format "org-%s-options-alist" backend))))
1328 (and (boundp var) (eval var))))))
1329 ;; Build an alist between #+OPTION: item and property-name.
1330 (alist (delq nil
1331 (mapcar (lambda (e)
1332 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1333 (car e))))
1334 all)))
1335 plist)
1336 (mapc (lambda (e)
1337 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1338 (car e)
1339 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1340 options)
1341 (setq plist (plist-put plist
1342 (cdr e)
1343 (car (read-from-string
1344 (match-string 2 options)))))))
1345 alist)
1346 plist))
1348 (defun org-export--get-subtree-options (&optional backend)
1349 "Get export options in subtree at point.
1350 Optional argument BACKEND is a symbol specifying back-end used
1351 for export. Return options as a plist."
1352 ;; For each buffer keyword, create an headline property setting the
1353 ;; same property in communication channel. The name for the property
1354 ;; is the keyword with "EXPORT_" appended to it.
1355 (org-with-wide-buffer
1356 (let (prop plist)
1357 ;; Make sure point is at an heading.
1358 (unless (org-at-heading-p) (org-back-to-heading t))
1359 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1360 ;; title as its fallback value.
1361 (when (setq prop (progn (looking-at org-todo-line-regexp)
1362 (or (save-match-data
1363 (org-entry-get (point) "EXPORT_TITLE"))
1364 (org-match-string-no-properties 3))))
1365 (setq plist
1366 (plist-put
1367 plist :title
1368 (org-element-parse-secondary-string
1369 prop (org-element-restriction 'keyword)))))
1370 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1371 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1372 (setq plist
1373 (nconc plist (org-export--parse-option-keyword prop backend))))
1374 ;; Handle other keywords.
1375 (let ((seen '("TITLE")))
1376 (mapc
1377 (lambda (option)
1378 (let ((property (nth 1 option)))
1379 (when (and property (not (member property seen)))
1380 (let* ((subtree-prop (concat "EXPORT_" property))
1381 ;; Export properties are not case-sensitive.
1382 (value (let ((case-fold-search t))
1383 (org-entry-get (point) subtree-prop))))
1384 (push property seen)
1385 (when value
1386 (setq plist
1387 (plist-put
1388 plist
1389 (car option)
1390 ;; Parse VALUE if required.
1391 (if (member property org-element-parsed-keywords)
1392 (org-element-parse-secondary-string
1393 value (org-element-restriction 'keyword))
1394 value))))))))
1395 ;; Also look for both general keywords and back-end specific
1396 ;; options if BACKEND is provided.
1397 (append (and backend
1398 (let ((var (intern
1399 (format "org-%s-options-alist" backend))))
1400 (and (boundp var) (symbol-value var))))
1401 org-export-options-alist)))
1402 ;; Return value.
1403 plist)))
1405 (defun org-export--get-inbuffer-options (&optional backend files)
1406 "Return current buffer export options, as a plist.
1408 Optional argument BACKEND, when non-nil, is a symbol specifying
1409 which back-end specific options should also be read in the
1410 process.
1412 Optional argument FILES is a list of setup files names read so
1413 far, used to avoid circular dependencies.
1415 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1416 (org-with-wide-buffer
1417 (goto-char (point-min))
1418 (let ((case-fold-search t) plist)
1419 ;; 1. Special keywords, as in `org-export-special-keywords'.
1420 (let ((special-re
1421 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1422 (while (re-search-forward special-re nil t)
1423 (let ((element (org-element-at-point)))
1424 (when (eq (org-element-type element) 'keyword)
1425 (let* ((key (org-element-property :key element))
1426 (val (org-element-property :value element))
1427 (prop
1428 (cond
1429 ((string= key "SETUP_FILE")
1430 (let ((file
1431 (expand-file-name
1432 (org-remove-double-quotes (org-trim val)))))
1433 ;; Avoid circular dependencies.
1434 (unless (member file files)
1435 (with-temp-buffer
1436 (insert (org-file-contents file 'noerror))
1437 (org-mode)
1438 (org-export--get-inbuffer-options
1439 backend (cons file files))))))
1440 ((string= key "OPTIONS")
1441 (org-export--parse-option-keyword val backend)))))
1442 (setq plist (org-combine-plists plist prop)))))))
1443 ;; 2. Standard options, as in `org-export-options-alist'.
1444 (let* ((all (append org-export-options-alist
1445 ;; Also look for back-end specific options
1446 ;; if BACKEND is defined.
1447 (and backend
1448 (let ((var
1449 (intern
1450 (format "org-%s-options-alist" backend))))
1451 (and (boundp var) (eval var))))))
1452 ;; Build ALIST between keyword name and property name.
1453 (alist
1454 (delq nil (mapcar
1455 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1456 all)))
1457 ;; Build regexp matching all keywords associated to export
1458 ;; options. Note: the search is case insensitive.
1459 (opt-re (format "^[ \t]*#\\+%s:"
1460 (regexp-opt
1461 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1462 (goto-char (point-min))
1463 (while (re-search-forward opt-re nil t)
1464 (let ((element (org-element-at-point)))
1465 (when (eq (org-element-type element) 'keyword)
1466 (let* ((key (org-element-property :key element))
1467 (val (org-element-property :value element))
1468 (prop (cdr (assoc key alist)))
1469 (behaviour (nth 4 (assq prop all))))
1470 (setq plist
1471 (plist-put
1472 plist prop
1473 ;; Handle value depending on specified BEHAVIOUR.
1474 (case behaviour
1475 (space
1476 (if (not (plist-get plist prop)) (org-trim val)
1477 (concat (plist-get plist prop) " " (org-trim val))))
1478 (newline
1479 (org-trim
1480 (concat (plist-get plist prop) "\n" (org-trim val))))
1481 (split
1482 `(,@(plist-get plist prop) ,@(org-split-string val)))
1483 ('t val)
1484 (otherwise (if (not (plist-member plist prop)) val
1485 (plist-get plist prop))))))))))
1486 ;; Parse keywords specified in `org-element-parsed-keywords'.
1487 (mapc
1488 (lambda (key)
1489 (let* ((prop (cdr (assoc key alist)))
1490 (value (and prop (plist-get plist prop))))
1491 (when (stringp value)
1492 (setq plist
1493 (plist-put
1494 plist prop
1495 (org-element-parse-secondary-string
1496 value (org-element-restriction 'keyword)))))))
1497 org-element-parsed-keywords))
1498 ;; 3. Return final value.
1499 plist)))
1501 (defun org-export--get-buffer-attributes ()
1502 "Return properties related to buffer attributes, as a plist."
1503 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1504 (list
1505 ;; Store full path of input file name, or nil. For internal use.
1506 :input-file visited-file
1507 :title (or (and visited-file
1508 (file-name-sans-extension
1509 (file-name-nondirectory visited-file)))
1510 (buffer-name (buffer-base-buffer)))
1511 :footnote-definition-alist
1512 ;; Footnotes definitions must be collected in the original
1513 ;; buffer, as there's no insurance that they will still be in the
1514 ;; parse tree, due to possible narrowing.
1515 (let (alist)
1516 (org-with-wide-buffer
1517 (goto-char (point-min))
1518 (while (re-search-forward org-footnote-definition-re nil t)
1519 (let ((def (org-footnote-at-definition-p)))
1520 (when def
1521 (org-skip-whitespace)
1522 (push (cons (car def)
1523 (save-restriction
1524 (narrow-to-region (point) (nth 2 def))
1525 ;; Like `org-element-parse-buffer', but
1526 ;; makes sure the definition doesn't start
1527 ;; with a section element.
1528 (org-element--parse-elements
1529 (point-min) (point-max) nil nil nil nil
1530 (list 'org-data nil))))
1531 alist))))
1532 alist))
1533 :id-alist
1534 ;; Collect id references.
1535 (let (alist)
1536 (org-with-wide-buffer
1537 (goto-char (point-min))
1538 (while (re-search-forward
1539 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1540 (let* ((id (org-match-string-no-properties 1))
1541 (file (org-id-find-id-file id)))
1542 (when file (push (cons id (file-relative-name file)) alist)))))
1543 alist))))
1545 (defun org-export--get-global-options (&optional backend)
1546 "Return global export options as a plist.
1548 Optional argument BACKEND, if non-nil, is a symbol specifying
1549 which back-end specific export options should also be read in the
1550 process."
1551 (let ((all (append org-export-options-alist
1552 (and backend
1553 (let ((var (intern
1554 (format "org-%s-options-alist" backend))))
1555 (and (boundp var) (symbol-value var))))))
1556 ;; Output value.
1557 plist)
1558 (mapc
1559 (lambda (cell)
1560 (setq plist
1561 (plist-put
1562 plist
1563 (car cell)
1564 ;; Eval default value provided. If keyword is a member
1565 ;; of `org-element-parsed-keywords', parse it as
1566 ;; a secondary string before storing it.
1567 (let ((value (eval (nth 3 cell))))
1568 (if (not (stringp value)) value
1569 (let ((keyword (nth 1 cell)))
1570 (if (not (member keyword org-element-parsed-keywords)) value
1571 (org-element-parse-secondary-string
1572 value (org-element-restriction 'keyword)))))))))
1573 all)
1574 ;; Return value.
1575 plist))
1577 (defvar org-export--allow-BIND-local nil)
1578 (defun org-export--confirm-letbind ()
1579 "Can we use #+BIND values during export?
1580 By default this will ask for confirmation by the user, to divert
1581 possible security risks."
1582 (cond
1583 ((not org-export-allow-BIND) nil)
1584 ((eq org-export-allow-BIND t) t)
1585 ((local-variable-p 'org-export--allow-BIND-local)
1586 org-export--allow-BIND-local)
1587 (t (org-set-local 'org-export--allow-BIND-local
1588 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1590 (defun org-export--install-letbind-maybe ()
1591 "Install the values from #+BIND lines as local variables.
1592 Variables must be installed before in-buffer options are
1593 retrieved."
1594 (let ((case-fold-search t) letbind pair)
1595 (org-with-wide-buffer
1596 (goto-char (point-min))
1597 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1598 (let* ((element (org-element-at-point))
1599 (value (org-element-property :value element)))
1600 (when (and (eq (org-element-type element) 'keyword)
1601 (not (equal value ""))
1602 (org-export--confirm-letbind))
1603 (push (read (format "(%s)" value)) letbind)))))
1604 (dolist (pair (nreverse letbind))
1605 (org-set-local (car pair) (nth 1 pair)))))
1608 ;;;; Tree Properties
1610 ;; Tree properties are infromation extracted from parse tree. They
1611 ;; are initialized at the beginning of the transcoding process by
1612 ;; `org-export-collect-tree-properties'.
1614 ;; Dedicated functions focus on computing the value of specific tree
1615 ;; properties during initialization. Thus,
1616 ;; `org-export--populate-ignore-list' lists elements and objects that
1617 ;; should be skipped during export, `org-export--get-min-level' gets
1618 ;; the minimal exportable level, used as a basis to compute relative
1619 ;; level for headlines. Eventually
1620 ;; `org-export--collect-headline-numbering' builds an alist between
1621 ;; headlines and their numbering.
1623 (defun org-export-collect-tree-properties (data info)
1624 "Extract tree properties from parse tree.
1626 DATA is the parse tree from which information is retrieved. INFO
1627 is a list holding export options.
1629 Following tree properties are set or updated:
1631 `:exported-data' Hash table used to memoize results from
1632 `org-export-data'.
1634 `:footnote-definition-alist' List of footnotes definitions in
1635 original buffer and current parse tree.
1637 `:headline-offset' Offset between true level of headlines and
1638 local level. An offset of -1 means an headline
1639 of level 2 should be considered as a level
1640 1 headline in the context.
1642 `:headline-numbering' Alist of all headlines as key an the
1643 associated numbering as value.
1645 `:ignore-list' List of elements that should be ignored during
1646 export.
1648 `:target-list' List of all targets in the parse tree.
1650 Return updated plist."
1651 ;; Install the parse tree in the communication channel, in order to
1652 ;; use `org-export-get-genealogy' and al.
1653 (setq info (plist-put info :parse-tree data))
1654 ;; Get the list of elements and objects to ignore, and put it into
1655 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1656 ;; been done during parse tree filtering.
1657 (setq info
1658 (plist-put info
1659 :ignore-list
1660 (append (org-export--populate-ignore-list data info)
1661 (plist-get info :ignore-list))))
1662 ;; Compute `:headline-offset' in order to be able to use
1663 ;; `org-export-get-relative-level'.
1664 (setq info
1665 (plist-put info
1666 :headline-offset
1667 (- 1 (org-export--get-min-level data info))))
1668 ;; Update footnotes definitions list with definitions in parse tree.
1669 ;; This is required since buffer expansion might have modified
1670 ;; boundaries of footnote definitions contained in the parse tree.
1671 ;; This way, definitions in `footnote-definition-alist' are bound to
1672 ;; match those in the parse tree.
1673 (let ((defs (plist-get info :footnote-definition-alist)))
1674 (org-element-map
1675 data 'footnote-definition
1676 (lambda (fn)
1677 (push (cons (org-element-property :label fn)
1678 `(org-data nil ,@(org-element-contents fn)))
1679 defs)))
1680 (setq info (plist-put info :footnote-definition-alist defs)))
1681 ;; Properties order doesn't matter: get the rest of the tree
1682 ;; properties.
1683 (nconc
1684 `(:target-list
1685 ,(org-element-map
1686 data '(keyword target)
1687 (lambda (blob)
1688 (when (or (eq (org-element-type blob) 'target)
1689 (string= (org-element-property :key blob) "TARGET"))
1690 blob)) info)
1691 :headline-numbering ,(org-export--collect-headline-numbering data info)
1692 :exported-data ,(make-hash-table :test 'eq :size 4001))
1693 info))
1695 (defun org-export--get-min-level (data options)
1696 "Return minimum exportable headline's level in DATA.
1697 DATA is parsed tree as returned by `org-element-parse-buffer'.
1698 OPTIONS is a plist holding export options."
1699 (catch 'exit
1700 (let ((min-level 10000))
1701 (mapc
1702 (lambda (blob)
1703 (when (and (eq (org-element-type blob) 'headline)
1704 (not (memq blob (plist-get options :ignore-list))))
1705 (setq min-level
1706 (min (org-element-property :level blob) min-level)))
1707 (when (= min-level 1) (throw 'exit 1)))
1708 (org-element-contents data))
1709 ;; If no headline was found, for the sake of consistency, set
1710 ;; minimum level to 1 nonetheless.
1711 (if (= min-level 10000) 1 min-level))))
1713 (defun org-export--collect-headline-numbering (data options)
1714 "Return numbering of all exportable headlines in a parse tree.
1716 DATA is the parse tree. OPTIONS is the plist holding export
1717 options.
1719 Return an alist whose key is an headline and value is its
1720 associated numbering \(in the shape of a list of numbers\)."
1721 (let ((numbering (make-vector org-export-max-depth 0)))
1722 (org-element-map
1723 data
1724 'headline
1725 (lambda (headline)
1726 (let ((relative-level
1727 (1- (org-export-get-relative-level headline options))))
1728 (cons
1729 headline
1730 (loop for n across numbering
1731 for idx from 0 to org-export-max-depth
1732 when (< idx relative-level) collect n
1733 when (= idx relative-level) collect (aset numbering idx (1+ n))
1734 when (> idx relative-level) do (aset numbering idx 0)))))
1735 options)))
1737 (defun org-export--populate-ignore-list (data options)
1738 "Return list of elements and objects to ignore during export.
1739 DATA is the parse tree to traverse. OPTIONS is the plist holding
1740 export options."
1741 (let* (ignore
1742 walk-data
1743 ;; First find trees containing a select tag, if any.
1744 (selected (org-export--selected-trees data options))
1745 (walk-data
1746 (lambda (data)
1747 ;; Collect ignored elements or objects into IGNORE-LIST.
1748 (let ((type (org-element-type data)))
1749 (if (org-export--skip-p data options selected) (push data ignore)
1750 (if (and (eq type 'headline)
1751 (eq (plist-get options :with-archived-trees) 'headline)
1752 (org-element-property :archivedp data))
1753 ;; If headline is archived but tree below has
1754 ;; to be skipped, add it to ignore list.
1755 (mapc (lambda (e) (push e ignore))
1756 (org-element-contents data))
1757 ;; Move into secondary string, if any.
1758 (let ((sec-prop
1759 (cdr (assq type org-element-secondary-value-alist))))
1760 (when sec-prop
1761 (mapc walk-data (org-element-property sec-prop data))))
1762 ;; Move into recursive objects/elements.
1763 (mapc walk-data (org-element-contents data))))))))
1764 ;; Main call.
1765 (funcall walk-data data)
1766 ;; Return value.
1767 ignore))
1769 (defun org-export--selected-trees (data info)
1770 "Return list of headlines containing a select tag in their tree.
1771 DATA is parsed data as returned by `org-element-parse-buffer'.
1772 INFO is a plist holding export options."
1773 (let* (selected-trees
1774 walk-data ; for byte-compiler.
1775 (walk-data
1776 (function
1777 (lambda (data genealogy)
1778 (case (org-element-type data)
1779 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1780 (org-element-contents data)))
1781 (headline
1782 (let ((tags (org-element-property :tags data)))
1783 (if (loop for tag in (plist-get info :select-tags)
1784 thereis (member tag tags))
1785 ;; When a select tag is found, mark full
1786 ;; genealogy and every headline within the tree
1787 ;; as acceptable.
1788 (setq selected-trees
1789 (append
1790 genealogy
1791 (org-element-map data 'headline 'identity)
1792 selected-trees))
1793 ;; Else, continue searching in tree, recursively.
1794 (mapc
1795 (lambda (el) (funcall walk-data el (cons data genealogy)))
1796 (org-element-contents data))))))))))
1797 (funcall walk-data data nil) selected-trees))
1799 (defun org-export--skip-p (blob options selected)
1800 "Non-nil when element or object BLOB should be skipped during export.
1801 OPTIONS is the plist holding export options. SELECTED, when
1802 non-nil, is a list of headlines belonging to a tree with a select
1803 tag."
1804 (case (org-element-type blob)
1805 (clock (not (plist-get options :with-clocks)))
1806 (drawer
1807 (or (not (plist-get options :with-drawers))
1808 (and (consp (plist-get options :with-drawers))
1809 (not (member (org-element-property :drawer-name blob)
1810 (plist-get options :with-drawers))))))
1811 (headline
1812 (let ((with-tasks (plist-get options :with-tasks))
1813 (todo (org-element-property :todo-keyword blob))
1814 (todo-type (org-element-property :todo-type blob))
1815 (archived (plist-get options :with-archived-trees))
1816 (tags (org-element-property :tags blob)))
1818 ;; Ignore subtrees with an exclude tag.
1819 (loop for k in (plist-get options :exclude-tags)
1820 thereis (member k tags))
1821 ;; When a select tag is present in the buffer, ignore any tree
1822 ;; without it.
1823 (and selected (not (memq blob selected)))
1824 ;; Ignore commented sub-trees.
1825 (org-element-property :commentedp blob)
1826 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1827 (and (not archived) (org-element-property :archivedp blob))
1828 ;; Ignore tasks, if specified by `:with-tasks' property.
1829 (and todo
1830 (or (not with-tasks)
1831 (and (memq with-tasks '(todo done))
1832 (not (eq todo-type with-tasks)))
1833 (and (consp with-tasks) (not (member todo with-tasks))))))))
1834 (inlinetask (not (plist-get options :with-inlinetasks)))
1835 (planning (not (plist-get options :with-plannings)))
1836 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1837 (table-cell
1838 (and (org-export-table-has-special-column-p
1839 (org-export-get-parent-table blob))
1840 (not (org-export-get-previous-element blob options))))
1841 (table-row (org-export-table-row-is-special-p blob options))
1842 (timestamp
1843 (case (plist-get options :with-timestamps)
1844 ;; No timestamp allowed.
1845 ('nil t)
1846 ;; Only active timestamps allowed and the current one isn't
1847 ;; active.
1848 (active
1849 (not (memq (org-element-property :type blob)
1850 '(active active-range))))
1851 ;; Only inactive timestamps allowed and the current one isn't
1852 ;; inactive.
1853 (inactive
1854 (not (memq (org-element-property :type blob)
1855 '(inactive inactive-range))))))))
1859 ;;; The Transcoder
1861 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1862 ;; `org-element-parse-buffer') and transcodes it into a specified
1863 ;; back-end output. It takes care of filtering out elements or
1864 ;; objects according to export options and organizing the output blank
1865 ;; lines and white space are preserved. The function memoizes its
1866 ;; results, so it is cheap to call it within translators.
1868 ;; Internally, three functions handle the filtering of objects and
1869 ;; elements during the export. In particular,
1870 ;; `org-export-ignore-element' marks an element or object so future
1871 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1872 ;; elements or objects should be seen as real Org syntax and
1873 ;; `org-export-expand' transforms the others back into their original
1874 ;; shape
1876 ;; `org-export-transcoder' is an accessor returning appropriate
1877 ;; translator function for a given element or object.
1879 (defun org-export-transcoder (blob info)
1880 "Return appropriate transcoder for BLOB.
1881 INFO is a plist containing export directives."
1882 (let ((type (org-element-type blob)))
1883 ;; Return contents only for complete parse trees.
1884 (if (eq type 'org-data) (lambda (blob contents info) contents)
1885 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1886 (and (functionp transcoder) transcoder)))))
1888 (defun org-export-data (data info)
1889 "Convert DATA into current back-end format.
1891 DATA is a parse tree, an element or an object or a secondary
1892 string. INFO is a plist holding export options.
1894 Return transcoded string."
1895 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1896 (if (not (eq memo 'no-memo)) memo
1897 (let* ((type (org-element-type data))
1898 (results
1899 (cond
1900 ;; Ignored element/object.
1901 ((memq data (plist-get info :ignore-list)) nil)
1902 ;; Plain text.
1903 ((eq type 'plain-text)
1904 (org-export-filter-apply-functions
1905 (plist-get info :filter-plain-text)
1906 (let ((transcoder (org-export-transcoder data info)))
1907 (if transcoder (funcall transcoder data info) data))
1908 info))
1909 ;; Uninterpreted element/object: change it back to Org
1910 ;; syntax and export again resulting raw string.
1911 ((not (org-export--interpret-p data info))
1912 (org-export-data
1913 (org-export-expand
1914 data
1915 (mapconcat (lambda (blob) (org-export-data blob info))
1916 (org-element-contents data)
1917 ""))
1918 info))
1919 ;; Secondary string.
1920 ((not type)
1921 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1922 ;; Element/Object without contents or, as a special case,
1923 ;; headline with archive tag and archived trees restricted
1924 ;; to title only.
1925 ((or (not (org-element-contents data))
1926 (and (eq type 'headline)
1927 (eq (plist-get info :with-archived-trees) 'headline)
1928 (org-element-property :archivedp data)))
1929 (let ((transcoder (org-export-transcoder data info)))
1930 (and (functionp transcoder)
1931 (funcall transcoder data nil info))))
1932 ;; Element/Object with contents.
1934 (let ((transcoder (org-export-transcoder data info)))
1935 (when transcoder
1936 (let* ((greaterp (memq type org-element-greater-elements))
1937 (objectp
1938 (and (not greaterp)
1939 (memq type org-element-recursive-objects)))
1940 (contents
1941 (mapconcat
1942 (lambda (element) (org-export-data element info))
1943 (org-element-contents
1944 (if (or greaterp objectp) data
1945 ;; Elements directly containing objects
1946 ;; must have their indentation normalized
1947 ;; first.
1948 (org-element-normalize-contents
1949 data
1950 ;; When normalizing contents of the first
1951 ;; paragraph in an item or a footnote
1952 ;; definition, ignore first line's
1953 ;; indentation: there is none and it
1954 ;; might be misleading.
1955 (when (eq type 'paragraph)
1956 (let ((parent (org-export-get-parent data)))
1957 (and
1958 (eq (car (org-element-contents parent))
1959 data)
1960 (memq (org-element-type parent)
1961 '(footnote-definition item))))))))
1962 "")))
1963 (funcall transcoder data
1964 (if (not greaterp) contents
1965 (org-element-normalize-string contents))
1966 info))))))))
1967 ;; Final result will be memoized before being returned.
1968 (puthash
1969 data
1970 (cond
1971 ((not results) nil)
1972 ((memq type '(org-data plain-text nil)) results)
1973 ;; Append the same white space between elements or objects as in
1974 ;; the original buffer, and call appropriate filters.
1976 (let ((results
1977 (org-export-filter-apply-functions
1978 (plist-get info (intern (format ":filter-%s" type)))
1979 (let ((post-blank (or (org-element-property :post-blank data)
1980 0)))
1981 (if (memq type org-element-all-elements)
1982 (concat (org-element-normalize-string results)
1983 (make-string post-blank ?\n))
1984 (concat results (make-string post-blank ? ))))
1985 info)))
1986 results)))
1987 (plist-get info :exported-data))))))
1989 (defun org-export--interpret-p (blob info)
1990 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1991 Check is done according to export options INFO, stored as
1992 a plist."
1993 (case (org-element-type blob)
1994 ;; ... entities...
1995 (entity (plist-get info :with-entities))
1996 ;; ... emphasis...
1997 (emphasis (plist-get info :with-emphasize))
1998 ;; ... fixed-width areas.
1999 (fixed-width (plist-get info :with-fixed-width))
2000 ;; ... footnotes...
2001 ((footnote-definition footnote-reference)
2002 (plist-get info :with-footnotes))
2003 ;; ... sub/superscripts...
2004 ((subscript superscript)
2005 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2006 (if (eq sub/super-p '{})
2007 (org-element-property :use-brackets-p blob)
2008 sub/super-p)))
2009 ;; ... tables...
2010 (table (plist-get info :with-tables))
2011 (otherwise t)))
2013 (defun org-export-expand (blob contents)
2014 "Expand a parsed element or object to its original state.
2015 BLOB is either an element or an object. CONTENTS is its
2016 contents, as a string or nil."
2017 (funcall
2018 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2019 blob contents))
2021 (defun org-export-ignore-element (element info)
2022 "Add ELEMENT to `:ignore-list' in INFO.
2024 Any element in `:ignore-list' will be skipped when using
2025 `org-element-map'. INFO is modified by side effects."
2026 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2030 ;;; The Filter System
2032 ;; Filters allow end-users to tweak easily the transcoded output.
2033 ;; They are the functional counterpart of hooks, as every filter in
2034 ;; a set is applied to the return value of the previous one.
2036 ;; Every set is back-end agnostic. Although, a filter is always
2037 ;; called, in addition to the string it applies to, with the back-end
2038 ;; used as argument, so it's easy for the end-user to add back-end
2039 ;; specific filters in the set. The communication channel, as
2040 ;; a plist, is required as the third argument.
2042 ;; From the developer side, filters sets can be installed in the
2043 ;; process with the help of `org-export-define-backend', which
2044 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2045 ;; association has a key among the following symbols and a function or
2046 ;; a list of functions as value.
2048 ;; - `:filter-parse-tree' applies directly on the complete parsed
2049 ;; tree. It's the only filters set that doesn't apply to a string.
2050 ;; Users can set it through `org-export-filter-parse-tree-functions'
2051 ;; variable.
2053 ;; - `:filter-final-output' applies to the final transcoded string.
2054 ;; Users can set it with `org-export-filter-final-output-functions'
2055 ;; variable
2057 ;; - `:filter-plain-text' applies to any string not recognized as Org
2058 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2059 ;; configure it.
2061 ;; - `:filter-TYPE' applies on the string returned after an element or
2062 ;; object of type TYPE has been transcoded. An user can modify
2063 ;; `org-export-filter-TYPE-functions'
2065 ;; All filters sets are applied with
2066 ;; `org-export-filter-apply-functions' function. Filters in a set are
2067 ;; applied in a LIFO fashion. It allows developers to be sure that
2068 ;; their filters will be applied first.
2070 ;; Filters properties are installed in communication channel with
2071 ;; `org-export-install-filters' function.
2073 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2074 ;; before parsing to allow for heavy structure modifications.
2077 ;;;; Before Parsing Hook
2079 (defvar org-export-before-parsing-hook nil
2080 "Hook run before parsing an export buffer.
2082 This is run after include keywords have been expanded and Babel
2083 code executed, on a copy of original buffer's area being
2084 exported. Visibility is the same as in the original one. Point
2085 is left at the beginning of the new one.
2087 Every function in this hook will be called with one argument: the
2088 back-end currently used, as a symbol.")
2091 ;;;; Special Filters
2093 (defvar org-export-filter-parse-tree-functions nil
2094 "List of functions applied to the parsed tree.
2095 Each filter is called with three arguments: the parse tree, as
2096 returned by `org-element-parse-buffer', the back-end, as
2097 a symbol, and the communication channel, as a plist. It must
2098 return the modified parse tree to transcode.")
2100 (defvar org-export-filter-final-output-functions nil
2101 "List of functions applied to the transcoded string.
2102 Each filter is called with three arguments: the full transcoded
2103 string, the back-end, as a symbol, and the communication channel,
2104 as a plist. It must return a string that will be used as the
2105 final export output.")
2107 (defvar org-export-filter-plain-text-functions nil
2108 "List of functions applied to plain text.
2109 Each filter is called with three arguments: a string which
2110 contains no Org syntax, the back-end, as a symbol, and the
2111 communication channel, as a plist. It must return a string or
2112 nil.")
2115 ;;;; Elements Filters
2117 (defvar org-export-filter-babel-call-functions nil
2118 "List of functions applied to a transcoded babel-call.
2119 Each filter is called with three arguments: the transcoded data,
2120 as a string, the back-end, as a symbol, and the communication
2121 channel, as a plist. It must return a string or nil.")
2123 (defvar org-export-filter-center-block-functions nil
2124 "List of functions applied to a transcoded center block.
2125 Each filter is called with three arguments: the transcoded data,
2126 as a string, the back-end, as a symbol, and the communication
2127 channel, as a plist. It must return a string or nil.")
2129 (defvar org-export-filter-clock-functions nil
2130 "List of functions applied to a transcoded clock.
2131 Each filter is called with three arguments: the transcoded data,
2132 as a string, the back-end, as a symbol, and the communication
2133 channel, as a plist. It must return a string or nil.")
2135 (defvar org-export-filter-comment-functions nil
2136 "List of functions applied to a transcoded comment.
2137 Each filter is called with three arguments: the transcoded data,
2138 as a string, the back-end, as a symbol, and the communication
2139 channel, as a plist. It must return a string or nil.")
2141 (defvar org-export-filter-comment-block-functions nil
2142 "List of functions applied to a transcoded comment-comment.
2143 Each filter is called with three arguments: the transcoded data,
2144 as a string, the back-end, as a symbol, and the communication
2145 channel, as a plist. It must return a string or nil.")
2147 (defvar org-export-filter-drawer-functions nil
2148 "List of functions applied to a transcoded drawer.
2149 Each filter is called with three arguments: the transcoded data,
2150 as a string, the back-end, as a symbol, and the communication
2151 channel, as a plist. It must return a string or nil.")
2153 (defvar org-export-filter-dynamic-block-functions nil
2154 "List of functions applied to a transcoded dynamic-block.
2155 Each filter is called with three arguments: the transcoded data,
2156 as a string, the back-end, as a symbol, and the communication
2157 channel, as a plist. It must return a string or nil.")
2159 (defvar org-export-filter-example-block-functions nil
2160 "List of functions applied to a transcoded example-block.
2161 Each filter is called with three arguments: the transcoded data,
2162 as a string, the back-end, as a symbol, and the communication
2163 channel, as a plist. It must return a string or nil.")
2165 (defvar org-export-filter-export-block-functions nil
2166 "List of functions applied to a transcoded export-block.
2167 Each filter is called with three arguments: the transcoded data,
2168 as a string, the back-end, as a symbol, and the communication
2169 channel, as a plist. It must return a string or nil.")
2171 (defvar org-export-filter-fixed-width-functions nil
2172 "List of functions applied to a transcoded fixed-width.
2173 Each filter is called with three arguments: the transcoded data,
2174 as a string, the back-end, as a symbol, and the communication
2175 channel, as a plist. It must return a string or nil.")
2177 (defvar org-export-filter-footnote-definition-functions nil
2178 "List of functions applied to a transcoded footnote-definition.
2179 Each filter is called with three arguments: the transcoded data,
2180 as a string, the back-end, as a symbol, and the communication
2181 channel, as a plist. It must return a string or nil.")
2183 (defvar org-export-filter-headline-functions nil
2184 "List of functions applied to a transcoded headline.
2185 Each filter is called with three arguments: the transcoded data,
2186 as a string, the back-end, as a symbol, and the communication
2187 channel, as a plist. It must return a string or nil.")
2189 (defvar org-export-filter-horizontal-rule-functions nil
2190 "List of functions applied to a transcoded horizontal-rule.
2191 Each filter is called with three arguments: the transcoded data,
2192 as a string, the back-end, as a symbol, and the communication
2193 channel, as a plist. It must return a string or nil.")
2195 (defvar org-export-filter-inlinetask-functions nil
2196 "List of functions applied to a transcoded inlinetask.
2197 Each filter is called with three arguments: the transcoded data,
2198 as a string, the back-end, as a symbol, and the communication
2199 channel, as a plist. It must return a string or nil.")
2201 (defvar org-export-filter-item-functions nil
2202 "List of functions applied to a transcoded item.
2203 Each filter is called with three arguments: the transcoded data,
2204 as a string, the back-end, as a symbol, and the communication
2205 channel, as a plist. It must return a string or nil.")
2207 (defvar org-export-filter-keyword-functions nil
2208 "List of functions applied to a transcoded keyword.
2209 Each filter is called with three arguments: the transcoded data,
2210 as a string, the back-end, as a symbol, and the communication
2211 channel, as a plist. It must return a string or nil.")
2213 (defvar org-export-filter-latex-environment-functions nil
2214 "List of functions applied to a transcoded latex-environment.
2215 Each filter is called with three arguments: the transcoded data,
2216 as a string, the back-end, as a symbol, and the communication
2217 channel, as a plist. It must return a string or nil.")
2219 (defvar org-export-filter-node-property-functions nil
2220 "List of functions applied to a transcoded node-property.
2221 Each filter is called with three arguments: the transcoded data,
2222 as a string, the back-end, as a symbol, and the communication
2223 channel, as a plist. It must return a string or nil.")
2225 (defvar org-export-filter-paragraph-functions nil
2226 "List of functions applied to a transcoded paragraph.
2227 Each filter is called with three arguments: the transcoded data,
2228 as a string, the back-end, as a symbol, and the communication
2229 channel, as a plist. It must return a string or nil.")
2231 (defvar org-export-filter-plain-list-functions nil
2232 "List of functions applied to a transcoded plain-list.
2233 Each filter is called with three arguments: the transcoded data,
2234 as a string, the back-end, as a symbol, and the communication
2235 channel, as a plist. It must return a string or nil.")
2237 (defvar org-export-filter-planning-functions nil
2238 "List of functions applied to a transcoded planning.
2239 Each filter is called with three arguments: the transcoded data,
2240 as a string, the back-end, as a symbol, and the communication
2241 channel, as a plist. It must return a string or nil.")
2243 (defvar org-export-filter-property-drawer-functions nil
2244 "List of functions applied to a transcoded property-drawer.
2245 Each filter is called with three arguments: the transcoded data,
2246 as a string, the back-end, as a symbol, and the communication
2247 channel, as a plist. It must return a string or nil.")
2249 (defvar org-export-filter-quote-block-functions nil
2250 "List of functions applied to a transcoded quote block.
2251 Each filter is called with three arguments: the transcoded quote
2252 data, as a string, the back-end, as a symbol, and the
2253 communication channel, as a plist. It must return a string or
2254 nil.")
2256 (defvar org-export-filter-quote-section-functions nil
2257 "List of functions applied to a transcoded quote-section.
2258 Each filter is called with three arguments: the transcoded data,
2259 as a string, the back-end, as a symbol, and the communication
2260 channel, as a plist. It must return a string or nil.")
2262 (defvar org-export-filter-section-functions nil
2263 "List of functions applied to a transcoded section.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-special-block-functions nil
2269 "List of functions applied to a transcoded special block.
2270 Each filter is called with three arguments: the transcoded data,
2271 as a string, the back-end, as a symbol, and the communication
2272 channel, as a plist. It must return a string or nil.")
2274 (defvar org-export-filter-src-block-functions nil
2275 "List of functions applied to a transcoded src-block.
2276 Each filter is called with three arguments: the transcoded data,
2277 as a string, the back-end, as a symbol, and the communication
2278 channel, as a plist. It must return a string or nil.")
2280 (defvar org-export-filter-table-functions nil
2281 "List of functions applied to a transcoded table.
2282 Each filter is called with three arguments: the transcoded data,
2283 as a string, the back-end, as a symbol, and the communication
2284 channel, as a plist. It must return a string or nil.")
2286 (defvar org-export-filter-table-cell-functions nil
2287 "List of functions applied to a transcoded table-cell.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2292 (defvar org-export-filter-table-row-functions nil
2293 "List of functions applied to a transcoded table-row.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2298 (defvar org-export-filter-verse-block-functions nil
2299 "List of functions applied to a transcoded verse block.
2300 Each filter is called with three arguments: the transcoded data,
2301 as a string, the back-end, as a symbol, and the communication
2302 channel, as a plist. It must return a string or nil.")
2305 ;;;; Objects Filters
2307 (defvar org-export-filter-bold-functions nil
2308 "List of functions applied to transcoded bold text.
2309 Each filter is called with three arguments: the transcoded data,
2310 as a string, the back-end, as a symbol, and the communication
2311 channel, as a plist. It must return a string or nil.")
2313 (defvar org-export-filter-code-functions nil
2314 "List of functions applied to transcoded code text.
2315 Each filter is called with three arguments: the transcoded data,
2316 as a string, the back-end, as a symbol, and the communication
2317 channel, as a plist. It must return a string or nil.")
2319 (defvar org-export-filter-entity-functions nil
2320 "List of functions applied to a transcoded entity.
2321 Each filter is called with three arguments: the transcoded data,
2322 as a string, the back-end, as a symbol, and the communication
2323 channel, as a plist. It must return a string or nil.")
2325 (defvar org-export-filter-export-snippet-functions nil
2326 "List of functions applied to a transcoded export-snippet.
2327 Each filter is called with three arguments: the transcoded data,
2328 as a string, the back-end, as a symbol, and the communication
2329 channel, as a plist. It must return a string or nil.")
2331 (defvar org-export-filter-footnote-reference-functions nil
2332 "List of functions applied to a transcoded footnote-reference.
2333 Each filter is called with three arguments: the transcoded data,
2334 as a string, the back-end, as a symbol, and the communication
2335 channel, as a plist. It must return a string or nil.")
2337 (defvar org-export-filter-inline-babel-call-functions nil
2338 "List of functions applied to a transcoded inline-babel-call.
2339 Each filter is called with three arguments: the transcoded data,
2340 as a string, the back-end, as a symbol, and the communication
2341 channel, as a plist. It must return a string or nil.")
2343 (defvar org-export-filter-inline-src-block-functions nil
2344 "List of functions applied to a transcoded inline-src-block.
2345 Each filter is called with three arguments: the transcoded data,
2346 as a string, the back-end, as a symbol, and the communication
2347 channel, as a plist. It must return a string or nil.")
2349 (defvar org-export-filter-italic-functions nil
2350 "List of functions applied to transcoded italic text.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-latex-fragment-functions nil
2356 "List of functions applied to a transcoded latex-fragment.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-line-break-functions nil
2362 "List of functions applied to a transcoded line-break.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-link-functions nil
2368 "List of functions applied to a transcoded link.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-macro-functions nil
2374 "List of functions applied to a transcoded macro.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-radio-target-functions nil
2380 "List of functions applied to a transcoded radio-target.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-statistics-cookie-functions nil
2386 "List of functions applied to a transcoded statistics-cookie.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2391 (defvar org-export-filter-strike-through-functions nil
2392 "List of functions applied to transcoded strike-through text.
2393 Each filter is called with three arguments: the transcoded data,
2394 as a string, the back-end, as a symbol, and the communication
2395 channel, as a plist. It must return a string or nil.")
2397 (defvar org-export-filter-subscript-functions nil
2398 "List of functions applied to a transcoded subscript.
2399 Each filter is called with three arguments: the transcoded data,
2400 as a string, the back-end, as a symbol, and the communication
2401 channel, as a plist. It must return a string or nil.")
2403 (defvar org-export-filter-superscript-functions nil
2404 "List of functions applied to a transcoded superscript.
2405 Each filter is called with three arguments: the transcoded data,
2406 as a string, the back-end, as a symbol, and the communication
2407 channel, as a plist. It must return a string or nil.")
2409 (defvar org-export-filter-target-functions nil
2410 "List of functions applied to a transcoded target.
2411 Each filter is called with three arguments: the transcoded data,
2412 as a string, the back-end, as a symbol, and the communication
2413 channel, as a plist. It must return a string or nil.")
2415 (defvar org-export-filter-timestamp-functions nil
2416 "List of functions applied to a transcoded timestamp.
2417 Each filter is called with three arguments: the transcoded data,
2418 as a string, the back-end, as a symbol, and the communication
2419 channel, as a plist. It must return a string or nil.")
2421 (defvar org-export-filter-underline-functions nil
2422 "List of functions applied to transcoded underline text.
2423 Each filter is called with three arguments: the transcoded data,
2424 as a string, the back-end, as a symbol, and the communication
2425 channel, as a plist. It must return a string or nil.")
2427 (defvar org-export-filter-verbatim-functions nil
2428 "List of functions applied to transcoded verbatim text.
2429 Each filter is called with three arguments: the transcoded data,
2430 as a string, the back-end, as a symbol, and the communication
2431 channel, as a plist. It must return a string or nil.")
2434 ;;;; Filters Tools
2436 ;; Internal function `org-export-install-filters' installs filters
2437 ;; hard-coded in back-ends (developer filters) and filters from global
2438 ;; variables (user filters) in the communication channel.
2440 ;; Internal function `org-export-filter-apply-functions' takes care
2441 ;; about applying each filter in order to a given data. It ignores
2442 ;; filters returning a nil value but stops whenever a filter returns
2443 ;; an empty string.
2445 (defun org-export-filter-apply-functions (filters value info)
2446 "Call every function in FILTERS.
2448 Functions are called with arguments VALUE, current export
2449 back-end and INFO. A function returning a nil value will be
2450 skipped. If it returns the empty string, the process ends and
2451 VALUE is ignored.
2453 Call is done in a LIFO fashion, to be sure that developer
2454 specified filters, if any, are called first."
2455 (catch 'exit
2456 (dolist (filter filters value)
2457 (let ((result (funcall filter value (plist-get info :back-end) info)))
2458 (cond ((not result) value)
2459 ((equal value "") (throw 'exit nil))
2460 (t (setq value result)))))))
2462 (defun org-export-install-filters (info)
2463 "Install filters properties in communication channel.
2465 INFO is a plist containing the current communication channel.
2467 Return the updated communication channel."
2468 (let (plist)
2469 ;; Install user defined filters with `org-export-filters-alist'.
2470 (mapc (lambda (p)
2471 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2472 org-export-filters-alist)
2473 ;; Prepend back-end specific filters to that list.
2474 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2475 (plist-get info :back-end)))))
2476 (when (boundp back-end-filters)
2477 (mapc (lambda (p)
2478 ;; Single values get consed, lists are prepended.
2479 (let ((key (car p)) (value (cdr p)))
2480 (when value
2481 (setq plist
2482 (plist-put
2483 plist key
2484 (if (atom value) (cons value (plist-get plist key))
2485 (append value (plist-get plist key))))))))
2486 (eval back-end-filters))))
2487 ;; Return new communication channel.
2488 (org-combine-plists info plist)))
2492 ;;; Core functions
2494 ;; This is the room for the main function, `org-export-as', along with
2495 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2496 ;; They differ only by the way they output the resulting code.
2498 ;; `org-export-output-file-name' is an auxiliary function meant to be
2499 ;; used with `org-export-to-file'. With a given extension, it tries
2500 ;; to provide a canonical file name to write export output to.
2502 ;; Note that `org-export-as' doesn't really parse the current buffer,
2503 ;; but a copy of it (with the same buffer-local variables and
2504 ;; visibility), where macros and include keywords are expanded and
2505 ;; Babel blocks are executed, if appropriate.
2506 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2508 ;; File inclusion is taken care of by
2509 ;; `org-export-expand-include-keyword' and
2510 ;; `org-export--prepare-file-contents'. Structure wise, including
2511 ;; a whole Org file in a buffer often makes little sense. For
2512 ;; example, if the file contains an headline and the include keyword
2513 ;; was within an item, the item should contain the headline. That's
2514 ;; why file inclusion should be done before any structure can be
2515 ;; associated to the file, that is before parsing.
2517 (defun org-export-as
2518 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2519 "Transcode current Org buffer into BACKEND code.
2521 If narrowing is active in the current buffer, only transcode its
2522 narrowed part.
2524 If a region is active, transcode that region.
2526 When optional argument SUBTREEP is non-nil, transcode the
2527 sub-tree at point, extracting information from the headline
2528 properties first.
2530 When optional argument VISIBLE-ONLY is non-nil, don't export
2531 contents of hidden elements.
2533 When optional argument BODY-ONLY is non-nil, only return body
2534 code, without preamble nor postamble.
2536 Optional argument EXT-PLIST, when provided, is a property list
2537 with external parameters overriding Org default settings, but
2538 still inferior to file-local settings.
2540 Optional argument NOEXPAND, when non-nil, prevents included files
2541 to be expanded and Babel code to be executed.
2543 Return code as a string."
2544 ;; Barf if BACKEND isn't registered.
2545 (org-export-barf-if-invalid-backend backend)
2546 (save-excursion
2547 (save-restriction
2548 ;; Narrow buffer to an appropriate region or subtree for
2549 ;; parsing. If parsing subtree, be sure to remove main headline
2550 ;; too.
2551 (cond ((org-region-active-p)
2552 (narrow-to-region (region-beginning) (region-end)))
2553 (subtreep
2554 (org-narrow-to-subtree)
2555 (goto-char (point-min))
2556 (forward-line)
2557 (narrow-to-region (point) (point-max))))
2558 ;; 1. Get export environment from original buffer. Also install
2559 ;; user's and developer's filters.
2560 (let ((info (org-export-install-filters
2561 (org-export-get-environment backend subtreep ext-plist)))
2562 ;; 2. Get parse tree. Buffer isn't parsed directly.
2563 ;; Instead, a temporary copy is created, where macros
2564 ;; and include keywords are expanded and code blocks
2565 ;; are evaluated.
2566 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2567 (current-buffer))))
2568 (org-export-with-current-buffer-copy
2569 (unless noexpand
2570 (org-macro-replace-all)
2571 (org-export-expand-include-keyword)
2572 ;; TODO: Setting `org-current-export-file' is
2573 ;; required by Org Babel to properly resolve
2574 ;; noweb references. Once "org-exp.el" is
2575 ;; removed, modify
2576 ;; `org-export-blocks-preprocess' so it accepts
2577 ;; the value as an argument instead.
2578 (let ((org-current-export-file buf))
2579 (org-export-blocks-preprocess)))
2580 (goto-char (point-min))
2581 ;; Run hook
2582 ;; `org-export-before-parsing-hook'. with current
2583 ;; back-end as argument.
2584 (run-hook-with-args
2585 'org-export-before-parsing-hook backend)
2586 ;; Eventually parse buffer.
2587 (org-element-parse-buffer nil visible-only)))))
2588 ;; 3. Call parse-tree filters to get the final tree.
2589 (setq tree
2590 (org-export-filter-apply-functions
2591 (plist-get info :filter-parse-tree) tree info))
2592 ;; 4. Now tree is complete, compute its properties and add
2593 ;; them to communication channel.
2594 (setq info
2595 (org-combine-plists
2596 info (org-export-collect-tree-properties tree info)))
2597 ;; 5. Eventually transcode TREE. Wrap the resulting string
2598 ;; into a template, if required. Eventually call
2599 ;; final-output filter.
2600 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2601 (template (cdr (assq 'template
2602 (plist-get info :translate-alist))))
2603 (output (org-export-filter-apply-functions
2604 (plist-get info :filter-final-output)
2605 (if (or (not (functionp template)) body-only) body
2606 (funcall template body info))
2607 info)))
2608 ;; Maybe add final OUTPUT to kill ring, then return it.
2609 (when org-export-copy-to-kill-ring (org-kill-new output))
2610 output)))))
2612 (defun org-export-to-buffer
2613 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2614 "Call `org-export-as' with output to a specified buffer.
2616 BACKEND is the back-end used for transcoding, as a symbol.
2618 BUFFER is the output buffer. If it already exists, it will be
2619 erased first, otherwise, it will be created.
2621 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2622 and NOEXPAND are similar to those used in `org-export-as', which
2623 see.
2625 Return buffer."
2626 (let ((out (org-export-as
2627 backend subtreep visible-only body-only ext-plist noexpand))
2628 (buffer (get-buffer-create buffer)))
2629 (with-current-buffer buffer
2630 (erase-buffer)
2631 (insert out)
2632 (goto-char (point-min)))
2633 buffer))
2635 (defun org-export-to-file
2636 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2637 "Call `org-export-as' with output to a specified file.
2639 BACKEND is the back-end used for transcoding, as a symbol. FILE
2640 is the name of the output file, as a string.
2642 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2643 and NOEXPAND are similar to those used in `org-export-as', which
2644 see.
2646 Return output file's name."
2647 ;; Checks for FILE permissions. `write-file' would do the same, but
2648 ;; we'd rather avoid needless transcoding of parse tree.
2649 (unless (file-writable-p file) (error "Output file not writable"))
2650 ;; Insert contents to a temporary buffer and write it to FILE.
2651 (let ((out (org-export-as
2652 backend subtreep visible-only body-only ext-plist noexpand)))
2653 (with-temp-buffer
2654 (insert out)
2655 (let ((coding-system-for-write org-export-coding-system))
2656 (write-file file))))
2657 ;; Return full path.
2658 file)
2660 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2661 "Return output file's name according to buffer specifications.
2663 EXTENSION is a string representing the output file extension,
2664 with the leading dot.
2666 With a non-nil optional argument SUBTREEP, try to determine
2667 output file's name by looking for \"EXPORT_FILE_NAME\" property
2668 of subtree at point.
2670 When optional argument PUB-DIR is set, use it as the publishing
2671 directory.
2673 When optional argument VISIBLE-ONLY is non-nil, don't export
2674 contents of hidden elements.
2676 Return file name as a string, or nil if it couldn't be
2677 determined."
2678 (let ((base-name
2679 ;; File name may come from EXPORT_FILE_NAME subtree property,
2680 ;; assuming point is at beginning of said sub-tree.
2681 (file-name-sans-extension
2682 (or (and subtreep
2683 (org-entry-get
2684 (save-excursion
2685 (ignore-errors (org-back-to-heading) (point)))
2686 "EXPORT_FILE_NAME" t))
2687 ;; File name may be extracted from buffer's associated
2688 ;; file, if any.
2689 (buffer-file-name (buffer-base-buffer))
2690 ;; Can't determine file name on our own: Ask user.
2691 (let ((read-file-name-function
2692 (and org-completion-use-ido 'ido-read-file-name)))
2693 (read-file-name
2694 "Output file: " pub-dir nil nil nil
2695 (lambda (name)
2696 (string= (file-name-extension name t) extension))))))))
2697 ;; Build file name. Enforce EXTENSION over whatever user may have
2698 ;; come up with. PUB-DIR, if defined, always has precedence over
2699 ;; any provided path.
2700 (cond
2701 (pub-dir
2702 (concat (file-name-as-directory pub-dir)
2703 (file-name-nondirectory base-name)
2704 extension))
2705 ((string= (file-name-nondirectory base-name) base-name)
2706 (concat (file-name-as-directory ".") base-name extension))
2707 (t (concat base-name extension)))))
2709 (defmacro org-export-with-current-buffer-copy (&rest body)
2710 "Apply BODY in a copy of the current buffer.
2712 The copy preserves local variables and visibility of the original
2713 buffer.
2715 Point is at buffer's beginning when BODY is applied."
2716 (org-with-gensyms (original-buffer offset buffer-string overlays)
2717 `(let ((,original-buffer (current-buffer))
2718 (,offset (1- (point-min)))
2719 (,buffer-string (buffer-string))
2720 (,overlays (mapcar
2721 'copy-overlay (overlays-in (point-min) (point-max)))))
2722 (with-temp-buffer
2723 (let ((buffer-invisibility-spec nil))
2724 (org-clone-local-variables
2725 ,original-buffer
2726 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2727 (insert ,buffer-string)
2728 (mapc (lambda (ov)
2729 (move-overlay
2731 (- (overlay-start ov) ,offset)
2732 (- (overlay-end ov) ,offset)
2733 (current-buffer)))
2734 ,overlays)
2735 (goto-char (point-min))
2736 (progn ,@body))))))
2737 (def-edebug-spec org-export-with-current-buffer-copy (body))
2739 (defun org-export-expand-include-keyword (&optional included dir)
2740 "Expand every include keyword in buffer.
2741 Optional argument INCLUDED is a list of included file names along
2742 with their line restriction, when appropriate. It is used to
2743 avoid infinite recursion. Optional argument DIR is the current
2744 working directory. It is used to properly resolve relative
2745 paths."
2746 (let ((case-fold-search t))
2747 (goto-char (point-min))
2748 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2749 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2750 'keyword)
2751 (beginning-of-line)
2752 ;; Extract arguments from keyword's value.
2753 (let* ((value (match-string 1))
2754 (ind (org-get-indentation))
2755 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2756 (prog1 (expand-file-name (match-string 1 value) dir)
2757 (setq value (replace-match "" nil nil value)))))
2758 (lines
2759 (and (string-match
2760 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2761 (prog1 (match-string 1 value)
2762 (setq value (replace-match "" nil nil value)))))
2763 (env (cond ((string-match "\\<example\\>" value) 'example)
2764 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2765 (match-string 1 value))))
2766 ;; Minimal level of included file defaults to the child
2767 ;; level of the current headline, if any, or one. It
2768 ;; only applies is the file is meant to be included as
2769 ;; an Org one.
2770 (minlevel
2771 (and (not env)
2772 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2773 (prog1 (string-to-number (match-string 1 value))
2774 (setq value (replace-match "" nil nil value)))
2775 (let ((cur (org-current-level)))
2776 (if cur (1+ (org-reduced-level cur)) 1))))))
2777 ;; Remove keyword.
2778 (delete-region (point) (progn (forward-line) (point)))
2779 (cond
2780 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2781 ;; Check if files has already been parsed. Look after
2782 ;; inclusion lines too, as different parts of the same file
2783 ;; can be included too.
2784 ((member (list file lines) included)
2785 (error "Recursive file inclusion: %s" file))
2787 (cond
2788 ((eq env 'example)
2789 (insert
2790 (let ((ind-str (make-string ind ? ))
2791 (contents
2792 ;; Protect sensitive contents with commas.
2793 (replace-regexp-in-string
2794 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2795 (org-export--prepare-file-contents file lines)
2796 nil nil 1)))
2797 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2798 ind-str contents ind-str))))
2799 ((stringp env)
2800 (insert
2801 (let ((ind-str (make-string ind ? ))
2802 (contents
2803 ;; Protect sensitive contents with commas.
2804 (replace-regexp-in-string
2805 (if (string= env "org") "\\(^\\)\\(.\\)"
2806 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2807 (org-export--prepare-file-contents file lines)
2808 nil nil 1)))
2809 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2810 ind-str env contents ind-str))))
2812 (insert
2813 (with-temp-buffer
2814 (org-mode)
2815 (insert
2816 (org-export--prepare-file-contents file lines ind minlevel))
2817 (org-export-expand-include-keyword
2818 (cons (list file lines) included)
2819 (file-name-directory file))
2820 (buffer-string))))))))))))
2822 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2823 "Prepare the contents of FILE for inclusion and return them as a string.
2825 When optional argument LINES is a string specifying a range of
2826 lines, include only those lines.
2828 Optional argument IND, when non-nil, is an integer specifying the
2829 global indentation of returned contents. Since its purpose is to
2830 allow an included file to stay in the same environment it was
2831 created \(i.e. a list item), it doesn't apply past the first
2832 headline encountered.
2834 Optional argument MINLEVEL, when non-nil, is an integer
2835 specifying the level that any top-level headline in the included
2836 file should have."
2837 (with-temp-buffer
2838 (insert-file-contents file)
2839 (when lines
2840 (let* ((lines (split-string lines "-"))
2841 (lbeg (string-to-number (car lines)))
2842 (lend (string-to-number (cadr lines)))
2843 (beg (if (zerop lbeg) (point-min)
2844 (goto-char (point-min))
2845 (forward-line (1- lbeg))
2846 (point)))
2847 (end (if (zerop lend) (point-max)
2848 (goto-char (point-min))
2849 (forward-line (1- lend))
2850 (point))))
2851 (narrow-to-region beg end)))
2852 ;; Remove blank lines at beginning and end of contents. The logic
2853 ;; behind that removal is that blank lines around include keyword
2854 ;; override blank lines in included file.
2855 (goto-char (point-min))
2856 (org-skip-whitespace)
2857 (beginning-of-line)
2858 (delete-region (point-min) (point))
2859 (goto-char (point-max))
2860 (skip-chars-backward " \r\t\n")
2861 (forward-line)
2862 (delete-region (point) (point-max))
2863 ;; If IND is set, preserve indentation of include keyword until
2864 ;; the first headline encountered.
2865 (when ind
2866 (unless (eq major-mode 'org-mode) (org-mode))
2867 (goto-char (point-min))
2868 (let ((ind-str (make-string ind ? )))
2869 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2870 ;; Do not move footnote definitions out of column 0.
2871 (unless (and (looking-at org-footnote-definition-re)
2872 (eq (org-element-type (org-element-at-point))
2873 'footnote-definition))
2874 (insert ind-str))
2875 (forward-line))))
2876 ;; When MINLEVEL is specified, compute minimal level for headlines
2877 ;; in the file (CUR-MIN), and remove stars to each headline so
2878 ;; that headlines with minimal level have a level of MINLEVEL.
2879 (when minlevel
2880 (unless (eq major-mode 'org-mode) (org-mode))
2881 (let ((levels (org-map-entries
2882 (lambda () (org-reduced-level (org-current-level))))))
2883 (when levels
2884 (let ((offset (- minlevel (apply 'min levels))))
2885 (unless (zerop offset)
2886 (when org-odd-levels-only (setq offset (* offset 2)))
2887 ;; Only change stars, don't bother moving whole
2888 ;; sections.
2889 (org-map-entries
2890 (lambda () (if (< offset 0) (delete-char (abs offset))
2891 (insert (make-string offset ?*))))))))))
2892 (buffer-string)))
2895 ;;; Tools For Back-Ends
2897 ;; A whole set of tools is available to help build new exporters. Any
2898 ;; function general enough to have its use across many back-ends
2899 ;; should be added here.
2901 ;;;; For Affiliated Keywords
2903 ;; `org-export-read-attribute' reads a property from a given element
2904 ;; as a plist. It can be used to normalize affiliated keywords'
2905 ;; syntax.
2907 ;; Since captions can span over multiple lines and accept dual values,
2908 ;; their internal representation is a bit tricky. Therefore,
2909 ;; `org-export-get-caption' transparently returns a given element's
2910 ;; caption as a secondary string.
2912 (defun org-export-read-attribute (attribute element &optional property)
2913 "Turn ATTRIBUTE property from ELEMENT into a plist.
2915 When optional argument PROPERTY is non-nil, return the value of
2916 that property within attributes.
2918 This function assumes attributes are defined as \":keyword
2919 value\" pairs. It is appropriate for `:attr_html' like
2920 properties."
2921 (let ((attributes
2922 (let ((value (org-element-property attribute element)))
2923 (and value
2924 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2925 (if property (plist-get attributes property) attributes)))
2927 (defun org-export-get-caption (element &optional shortp)
2928 "Return caption from ELEMENT as a secondary string.
2930 When optional argument SHORTP is non-nil, return short caption,
2931 as a secondary string, instead.
2933 Caption lines are separated by a white space."
2934 (let ((full-caption (org-element-property :caption element)) caption)
2935 (dolist (line full-caption (cdr caption))
2936 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2937 (when cap
2938 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2941 ;;;; For Export Snippets
2943 ;; Every export snippet is transmitted to the back-end. Though, the
2944 ;; latter will only retain one type of export-snippet, ignoring
2945 ;; others, based on the former's target back-end. The function
2946 ;; `org-export-snippet-backend' returns that back-end for a given
2947 ;; export-snippet.
2949 (defun org-export-snippet-backend (export-snippet)
2950 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2951 Translation, with `org-export-snippet-translation-alist', is
2952 applied."
2953 (let ((back-end (org-element-property :back-end export-snippet)))
2954 (intern
2955 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2956 back-end))))
2959 ;;;; For Footnotes
2961 ;; `org-export-collect-footnote-definitions' is a tool to list
2962 ;; actually used footnotes definitions in the whole parse tree, or in
2963 ;; an headline, in order to add footnote listings throughout the
2964 ;; transcoded data.
2966 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2967 ;; back-ends, when they need to attach the footnote definition only to
2968 ;; the first occurrence of the corresponding label.
2970 ;; `org-export-get-footnote-definition' and
2971 ;; `org-export-get-footnote-number' provide easier access to
2972 ;; additional information relative to a footnote reference.
2974 (defun org-export-collect-footnote-definitions (data info)
2975 "Return an alist between footnote numbers, labels and definitions.
2977 DATA is the parse tree from which definitions are collected.
2978 INFO is the plist used as a communication channel.
2980 Definitions are sorted by order of references. They either
2981 appear as Org data or as a secondary string for inlined
2982 footnotes. Unreferenced definitions are ignored."
2983 (let* (num-alist
2984 collect-fn ; for byte-compiler.
2985 (collect-fn
2986 (function
2987 (lambda (data)
2988 ;; Collect footnote number, label and definition in DATA.
2989 (org-element-map
2990 data 'footnote-reference
2991 (lambda (fn)
2992 (when (org-export-footnote-first-reference-p fn info)
2993 (let ((def (org-export-get-footnote-definition fn info)))
2994 (push
2995 (list (org-export-get-footnote-number fn info)
2996 (org-element-property :label fn)
2997 def)
2998 num-alist)
2999 ;; Also search in definition for nested footnotes.
3000 (when (eq (org-element-property :type fn) 'standard)
3001 (funcall collect-fn def)))))
3002 ;; Don't enter footnote definitions since it will happen
3003 ;; when their first reference is found.
3004 info nil 'footnote-definition)))))
3005 (funcall collect-fn (plist-get info :parse-tree))
3006 (reverse num-alist)))
3008 (defun org-export-footnote-first-reference-p (footnote-reference info)
3009 "Non-nil when a footnote reference is the first one for its label.
3011 FOOTNOTE-REFERENCE is the footnote reference being considered.
3012 INFO is the plist used as a communication channel."
3013 (let ((label (org-element-property :label footnote-reference)))
3014 ;; Anonymous footnotes are always a first reference.
3015 (if (not label) t
3016 ;; Otherwise, return the first footnote with the same LABEL and
3017 ;; test if it is equal to FOOTNOTE-REFERENCE.
3018 (let* (search-refs ; for byte-compiler.
3019 (search-refs
3020 (function
3021 (lambda (data)
3022 (org-element-map
3023 data 'footnote-reference
3024 (lambda (fn)
3025 (cond
3026 ((string= (org-element-property :label fn) label)
3027 (throw 'exit fn))
3028 ;; If FN isn't inlined, be sure to traverse its
3029 ;; definition before resuming search. See
3030 ;; comments in `org-export-get-footnote-number'
3031 ;; for more information.
3032 ((eq (org-element-property :type fn) 'standard)
3033 (funcall search-refs
3034 (org-export-get-footnote-definition fn info)))))
3035 ;; Don't enter footnote definitions since it will
3036 ;; happen when their first reference is found.
3037 info 'first-match 'footnote-definition)))))
3038 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3039 footnote-reference)))))
3041 (defun org-export-get-footnote-definition (footnote-reference info)
3042 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3043 INFO is the plist used as a communication channel."
3044 (let ((label (org-element-property :label footnote-reference)))
3045 (or (org-element-property :inline-definition footnote-reference)
3046 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3048 (defun org-export-get-footnote-number (footnote info)
3049 "Return number associated to a footnote.
3051 FOOTNOTE is either a footnote reference or a footnote definition.
3052 INFO is the plist used as a communication channel."
3053 (let* ((label (org-element-property :label footnote))
3054 seen-refs
3055 search-ref ; For byte-compiler.
3056 (search-ref
3057 (function
3058 (lambda (data)
3059 ;; Search footnote references through DATA, filling
3060 ;; SEEN-REFS along the way.
3061 (org-element-map
3062 data 'footnote-reference
3063 (lambda (fn)
3064 (let ((fn-lbl (org-element-property :label fn)))
3065 (cond
3066 ;; Anonymous footnote match: return number.
3067 ((and (not fn-lbl) (eq fn footnote))
3068 (throw 'exit (1+ (length seen-refs))))
3069 ;; Labels match: return number.
3070 ((and label (string= label fn-lbl))
3071 (throw 'exit (1+ (length seen-refs))))
3072 ;; Anonymous footnote: it's always a new one. Also,
3073 ;; be sure to return nil from the `cond' so
3074 ;; `first-match' doesn't get us out of the loop.
3075 ((not fn-lbl) (push 'inline seen-refs) nil)
3076 ;; Label not seen so far: add it so SEEN-REFS.
3078 ;; Also search for subsequent references in
3079 ;; footnote definition so numbering follows reading
3080 ;; logic. Note that we don't have to care about
3081 ;; inline definitions, since `org-element-map'
3082 ;; already traverses them at the right time.
3084 ;; Once again, return nil to stay in the loop.
3085 ((not (member fn-lbl seen-refs))
3086 (push fn-lbl seen-refs)
3087 (funcall search-ref
3088 (org-export-get-footnote-definition fn info))
3089 nil))))
3090 ;; Don't enter footnote definitions since it will happen
3091 ;; when their first reference is found.
3092 info 'first-match 'footnote-definition)))))
3093 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3096 ;;;; For Headlines
3098 ;; `org-export-get-relative-level' is a shortcut to get headline
3099 ;; level, relatively to the lower headline level in the parsed tree.
3101 ;; `org-export-get-headline-number' returns the section number of an
3102 ;; headline, while `org-export-number-to-roman' allows to convert it
3103 ;; to roman numbers.
3105 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3106 ;; `org-export-last-sibling-p' are three useful predicates when it
3107 ;; comes to fulfill the `:headline-levels' property.
3109 (defun org-export-get-relative-level (headline info)
3110 "Return HEADLINE relative level within current parsed tree.
3111 INFO is a plist holding contextual information."
3112 (+ (org-element-property :level headline)
3113 (or (plist-get info :headline-offset) 0)))
3115 (defun org-export-low-level-p (headline info)
3116 "Non-nil when HEADLINE is considered as low level.
3118 INFO is a plist used as a communication channel.
3120 A low level headlines has a relative level greater than
3121 `:headline-levels' property value.
3123 Return value is the difference between HEADLINE relative level
3124 and the last level being considered as high enough, or nil."
3125 (let ((limit (plist-get info :headline-levels)))
3126 (when (wholenump limit)
3127 (let ((level (org-export-get-relative-level headline info)))
3128 (and (> level limit) (- level limit))))))
3130 (defun org-export-get-headline-number (headline info)
3131 "Return HEADLINE numbering as a list of numbers.
3132 INFO is a plist holding contextual information."
3133 (cdr (assoc headline (plist-get info :headline-numbering))))
3135 (defun org-export-numbered-headline-p (headline info)
3136 "Return a non-nil value if HEADLINE element should be numbered.
3137 INFO is a plist used as a communication channel."
3138 (let ((sec-num (plist-get info :section-numbers))
3139 (level (org-export-get-relative-level headline info)))
3140 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3142 (defun org-export-number-to-roman (n)
3143 "Convert integer N into a roman numeral."
3144 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3145 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3146 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3147 ( 1 . "I")))
3148 (res ""))
3149 (if (<= n 0)
3150 (number-to-string n)
3151 (while roman
3152 (if (>= n (caar roman))
3153 (setq n (- n (caar roman))
3154 res (concat res (cdar roman)))
3155 (pop roman)))
3156 res)))
3158 (defun org-export-get-tags (element info &optional tags)
3159 "Return list of tags associated to ELEMENT.
3161 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3162 is a plist used as a communication channel.
3164 Select tags (see `org-export-select-tags') and exclude tags (see
3165 `org-export-exclude-tags') are removed from the list.
3167 When non-nil, optional argument TAGS should be a list of strings.
3168 Any tag belonging to this list will also be removed."
3169 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3170 (member tag (plist-get info :exclude-tags))
3171 (member tag tags)))
3172 (org-element-property :tags element)))
3174 (defun org-export-get-node-property (property blob &optional inherited)
3175 "Return node PROPERTY value for BLOB.
3177 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3178 element or object.
3180 If optional argument INHERITED is non-nil, the value can be
3181 inherited from a parent headline.
3183 Return value is a string or nil."
3184 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3185 (org-export-get-parent-headline blob))))
3186 (if (not inherited) (org-element-property property blob)
3187 (let ((parent headline) value)
3188 (catch 'found
3189 (while parent
3190 (when (plist-member (nth 1 parent) property)
3191 (throw 'found (org-element-property property parent)))
3192 (setq parent (org-element-property :parent parent))))))))
3194 (defun org-export-first-sibling-p (headline info)
3195 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3196 INFO is a plist used as a communication channel."
3197 (not (eq (org-element-type (org-export-get-previous-element headline info))
3198 'headline)))
3200 (defun org-export-last-sibling-p (headline info)
3201 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3202 INFO is a plist used as a communication channel."
3203 (not (org-export-get-next-element headline info)))
3206 ;;;; For Links
3208 ;; `org-export-solidify-link-text' turns a string into a safer version
3209 ;; for links, replacing most non-standard characters with hyphens.
3211 ;; `org-export-get-coderef-format' returns an appropriate format
3212 ;; string for coderefs.
3214 ;; `org-export-inline-image-p' returns a non-nil value when the link
3215 ;; provided should be considered as an inline image.
3217 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3218 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3219 ;; returns an appropriate unique identifier when found, or nil.
3221 ;; `org-export-resolve-id-link' returns the first headline with
3222 ;; specified id or custom-id in parse tree, the path to the external
3223 ;; file with the id or nil when neither was found.
3225 ;; `org-export-resolve-coderef' associates a reference to a line
3226 ;; number in the element it belongs, or returns the reference itself
3227 ;; when the element isn't numbered.
3229 (defun org-export-solidify-link-text (s)
3230 "Take link text S and make a safe target out of it."
3231 (save-match-data
3232 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3234 (defun org-export-get-coderef-format (path desc)
3235 "Return format string for code reference link.
3236 PATH is the link path. DESC is its description."
3237 (save-match-data
3238 (cond ((not desc) "%s")
3239 ((string-match (regexp-quote (concat "(" path ")")) desc)
3240 (replace-match "%s" t t desc))
3241 (t desc))))
3243 (defun org-export-inline-image-p (link &optional rules)
3244 "Non-nil if LINK object points to an inline image.
3246 Optional argument is a set of RULES defining inline images. It
3247 is an alist where associations have the following shape:
3249 \(TYPE . REGEXP)
3251 Applying a rule means apply REGEXP against LINK's path when its
3252 type is TYPE. The function will return a non-nil value if any of
3253 the provided rules is non-nil. The default rule is
3254 `org-export-default-inline-image-rule'.
3256 This only applies to links without a description."
3257 (and (not (org-element-contents link))
3258 (let ((case-fold-search t)
3259 (rules (or rules org-export-default-inline-image-rule)))
3260 (catch 'exit
3261 (mapc
3262 (lambda (rule)
3263 (and (string= (org-element-property :type link) (car rule))
3264 (string-match (cdr rule)
3265 (org-element-property :path link))
3266 (throw 'exit t)))
3267 rules)
3268 ;; Return nil if no rule matched.
3269 nil))))
3271 (defun org-export-resolve-coderef (ref info)
3272 "Resolve a code reference REF.
3274 INFO is a plist used as a communication channel.
3276 Return associated line number in source code, or REF itself,
3277 depending on src-block or example element's switches."
3278 (org-element-map
3279 (plist-get info :parse-tree) '(example-block src-block)
3280 (lambda (el)
3281 (with-temp-buffer
3282 (insert (org-trim (org-element-property :value el)))
3283 (let* ((label-fmt (regexp-quote
3284 (or (org-element-property :label-fmt el)
3285 org-coderef-label-format)))
3286 (ref-re
3287 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3288 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3289 ;; Element containing REF is found. Resolve it to either
3290 ;; a label or a line number, as needed.
3291 (when (re-search-backward ref-re nil t)
3292 (cond
3293 ((org-element-property :use-labels el) ref)
3294 ((eq (org-element-property :number-lines el) 'continued)
3295 (+ (org-export-get-loc el info) (line-number-at-pos)))
3296 (t (line-number-at-pos)))))))
3297 info 'first-match))
3299 (defun org-export-resolve-fuzzy-link (link info)
3300 "Return LINK destination.
3302 INFO is a plist holding contextual information.
3304 Return value can be an object, an element, or nil:
3306 - If LINK path matches a target object (i.e. <<path>>) or
3307 element (i.e. \"#+TARGET: path\"), return it.
3309 - If LINK path exactly matches the name affiliated keyword
3310 \(i.e. #+NAME: path) of an element, return that element.
3312 - If LINK path exactly matches any headline name, return that
3313 element. If more than one headline share that name, priority
3314 will be given to the one with the closest common ancestor, if
3315 any, or the first one in the parse tree otherwise.
3317 - Otherwise, return nil.
3319 Assume LINK type is \"fuzzy\"."
3320 (let* ((path (org-element-property :path link))
3321 (match-title-p (eq (aref path 0) ?*)))
3322 (cond
3323 ;; First try to find a matching "<<path>>" unless user specified
3324 ;; he was looking for an headline (path starts with a *
3325 ;; character).
3326 ((and (not match-title-p)
3327 (loop for target in (plist-get info :target-list)
3328 when (string= (org-element-property :value target) path)
3329 return target)))
3330 ;; Then try to find an element with a matching "#+NAME: path"
3331 ;; affiliated keyword.
3332 ((and (not match-title-p)
3333 (org-element-map
3334 (plist-get info :parse-tree) org-element-all-elements
3335 (lambda (el)
3336 (when (string= (org-element-property :name el) path) el))
3337 info 'first-match)))
3338 ;; Last case: link either points to an headline or to
3339 ;; nothingness. Try to find the source, with priority given to
3340 ;; headlines with the closest common ancestor. If such candidate
3341 ;; is found, return it, otherwise return nil.
3343 (let ((find-headline
3344 (function
3345 ;; Return first headline whose `:raw-value' property
3346 ;; is NAME in parse tree DATA, or nil.
3347 (lambda (name data)
3348 (org-element-map
3349 data 'headline
3350 (lambda (headline)
3351 (when (string=
3352 (org-element-property :raw-value headline)
3353 name)
3354 headline))
3355 info 'first-match)))))
3356 ;; Search among headlines sharing an ancestor with link,
3357 ;; from closest to farthest.
3358 (or (catch 'exit
3359 (mapc
3360 (lambda (parent)
3361 (when (eq (org-element-type parent) 'headline)
3362 (let ((foundp (funcall find-headline path parent)))
3363 (when foundp (throw 'exit foundp)))))
3364 (org-export-get-genealogy link)) nil)
3365 ;; No match with a common ancestor: try the full parse-tree.
3366 (funcall find-headline
3367 (if match-title-p (substring path 1) path)
3368 (plist-get info :parse-tree))))))))
3370 (defun org-export-resolve-id-link (link info)
3371 "Return headline referenced as LINK destination.
3373 INFO is a plist used as a communication channel.
3375 Return value can be the headline element matched in current parse
3376 tree, a file name or nil. Assume LINK type is either \"id\" or
3377 \"custom-id\"."
3378 (let ((id (org-element-property :path link)))
3379 ;; First check if id is within the current parse tree.
3380 (or (org-element-map
3381 (plist-get info :parse-tree) 'headline
3382 (lambda (headline)
3383 (when (or (string= (org-element-property :id headline) id)
3384 (string= (org-element-property :custom-id headline) id))
3385 headline))
3386 info 'first-match)
3387 ;; Otherwise, look for external files.
3388 (cdr (assoc id (plist-get info :id-alist))))))
3390 (defun org-export-resolve-radio-link (link info)
3391 "Return radio-target object referenced as LINK destination.
3393 INFO is a plist used as a communication channel.
3395 Return value can be a radio-target object or nil. Assume LINK
3396 has type \"radio\"."
3397 (let ((path (org-element-property :path link)))
3398 (org-element-map
3399 (plist-get info :parse-tree) 'radio-target
3400 (lambda (radio)
3401 (when (equal (org-element-property :value radio) path) radio))
3402 info 'first-match)))
3405 ;;;; For References
3407 ;; `org-export-get-ordinal' associates a sequence number to any object
3408 ;; or element.
3410 (defun org-export-get-ordinal (element info &optional types predicate)
3411 "Return ordinal number of an element or object.
3413 ELEMENT is the element or object considered. INFO is the plist
3414 used as a communication channel.
3416 Optional argument TYPES, when non-nil, is a list of element or
3417 object types, as symbols, that should also be counted in.
3418 Otherwise, only provided element's type is considered.
3420 Optional argument PREDICATE is a function returning a non-nil
3421 value if the current element or object should be counted in. It
3422 accepts two arguments: the element or object being considered and
3423 the plist used as a communication channel. This allows to count
3424 only a certain type of objects (i.e. inline images).
3426 Return value is a list of numbers if ELEMENT is an headline or an
3427 item. It is nil for keywords. It represents the footnote number
3428 for footnote definitions and footnote references. If ELEMENT is
3429 a target, return the same value as if ELEMENT was the closest
3430 table, item or headline containing the target. In any other
3431 case, return the sequence number of ELEMENT among elements or
3432 objects of the same type."
3433 ;; A target keyword, representing an invisible target, never has
3434 ;; a sequence number.
3435 (unless (eq (org-element-type element) 'keyword)
3436 ;; Ordinal of a target object refer to the ordinal of the closest
3437 ;; table, item, or headline containing the object.
3438 (when (eq (org-element-type element) 'target)
3439 (setq element
3440 (loop for parent in (org-export-get-genealogy element)
3441 when
3442 (memq
3443 (org-element-type parent)
3444 '(footnote-definition footnote-reference headline item
3445 table))
3446 return parent)))
3447 (case (org-element-type element)
3448 ;; Special case 1: An headline returns its number as a list.
3449 (headline (org-export-get-headline-number element info))
3450 ;; Special case 2: An item returns its number as a list.
3451 (item (let ((struct (org-element-property :structure element)))
3452 (org-list-get-item-number
3453 (org-element-property :begin element)
3454 struct
3455 (org-list-prevs-alist struct)
3456 (org-list-parents-alist struct))))
3457 ((footnote-definition footnote-reference)
3458 (org-export-get-footnote-number element info))
3459 (otherwise
3460 (let ((counter 0))
3461 ;; Increment counter until ELEMENT is found again.
3462 (org-element-map
3463 (plist-get info :parse-tree) (or types (org-element-type element))
3464 (lambda (el)
3465 (cond
3466 ((eq element el) (1+ counter))
3467 ((not predicate) (incf counter) nil)
3468 ((funcall predicate el info) (incf counter) nil)))
3469 info 'first-match))))))
3472 ;;;; For Src-Blocks
3474 ;; `org-export-get-loc' counts number of code lines accumulated in
3475 ;; src-block or example-block elements with a "+n" switch until
3476 ;; a given element, excluded. Note: "-n" switches reset that count.
3478 ;; `org-export-unravel-code' extracts source code (along with a code
3479 ;; references alist) from an `element-block' or `src-block' type
3480 ;; element.
3482 ;; `org-export-format-code' applies a formatting function to each line
3483 ;; of code, providing relative line number and code reference when
3484 ;; appropriate. Since it doesn't access the original element from
3485 ;; which the source code is coming, it expects from the code calling
3486 ;; it to know if lines should be numbered and if code references
3487 ;; should appear.
3489 ;; Eventually, `org-export-format-code-default' is a higher-level
3490 ;; function (it makes use of the two previous functions) which handles
3491 ;; line numbering and code references inclusion, and returns source
3492 ;; code in a format suitable for plain text or verbatim output.
3494 (defun org-export-get-loc (element info)
3495 "Return accumulated lines of code up to ELEMENT.
3497 INFO is the plist used as a communication channel.
3499 ELEMENT is excluded from count."
3500 (let ((loc 0))
3501 (org-element-map
3502 (plist-get info :parse-tree)
3503 `(src-block example-block ,(org-element-type element))
3504 (lambda (el)
3505 (cond
3506 ;; ELEMENT is reached: Quit the loop.
3507 ((eq el element))
3508 ;; Only count lines from src-block and example-block elements
3509 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3510 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3511 ((let ((linums (org-element-property :number-lines el)))
3512 (when linums
3513 ;; Accumulate locs or reset them.
3514 (let ((lines (org-count-lines
3515 (org-trim (org-element-property :value el)))))
3516 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3517 ;; Return nil to stay in the loop.
3518 nil)))
3519 info 'first-match)
3520 ;; Return value.
3521 loc))
3523 (defun org-export-unravel-code (element)
3524 "Clean source code and extract references out of it.
3526 ELEMENT has either a `src-block' an `example-block' type.
3528 Return a cons cell whose CAR is the source code, cleaned from any
3529 reference and protective comma and CDR is an alist between
3530 relative line number (integer) and name of code reference on that
3531 line (string)."
3532 (let* ((line 0) refs
3533 ;; Get code and clean it. Remove blank lines at its
3534 ;; beginning and end. Also remove protective commas.
3535 (code (let ((c (replace-regexp-in-string
3536 "\\`\\([ \t]*\n\\)+" ""
3537 (replace-regexp-in-string
3538 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3539 (org-element-property :value element)))))
3540 ;; If appropriate, remove global indentation.
3541 (unless (or org-src-preserve-indentation
3542 (org-element-property :preserve-indent element))
3543 (setq c (org-remove-indentation c)))
3544 ;; Free up the protected lines. Note: Org blocks
3545 ;; have commas at the beginning or every line.
3546 (if (string= (org-element-property :language element) "org")
3547 (replace-regexp-in-string "^," "" c)
3548 (replace-regexp-in-string
3549 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3550 ;; Get format used for references.
3551 (label-fmt (regexp-quote
3552 (or (org-element-property :label-fmt element)
3553 org-coderef-label-format)))
3554 ;; Build a regexp matching a loc with a reference.
3555 (with-ref-re
3556 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3557 (replace-regexp-in-string
3558 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3559 ;; Return value.
3560 (cons
3561 ;; Code with references removed.
3562 (org-element-normalize-string
3563 (mapconcat
3564 (lambda (loc)
3565 (incf line)
3566 (if (not (string-match with-ref-re loc)) loc
3567 ;; Ref line: remove ref, and signal its position in REFS.
3568 (push (cons line (match-string 3 loc)) refs)
3569 (replace-match "" nil nil loc 1)))
3570 (org-split-string code "\n") "\n"))
3571 ;; Reference alist.
3572 refs)))
3574 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3575 "Format CODE by applying FUN line-wise and return it.
3577 CODE is a string representing the code to format. FUN is
3578 a function. It must accept three arguments: a line of
3579 code (string), the current line number (integer) or nil and the
3580 reference associated to the current line (string) or nil.
3582 Optional argument NUM-LINES can be an integer representing the
3583 number of code lines accumulated until the current code. Line
3584 numbers passed to FUN will take it into account. If it is nil,
3585 FUN's second argument will always be nil. This number can be
3586 obtained with `org-export-get-loc' function.
3588 Optional argument REF-ALIST can be an alist between relative line
3589 number (i.e. ignoring NUM-LINES) and the name of the code
3590 reference on it. If it is nil, FUN's third argument will always
3591 be nil. It can be obtained through the use of
3592 `org-export-unravel-code' function."
3593 (let ((--locs (org-split-string code "\n"))
3594 (--line 0))
3595 (org-element-normalize-string
3596 (mapconcat
3597 (lambda (--loc)
3598 (incf --line)
3599 (let ((--ref (cdr (assq --line ref-alist))))
3600 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3601 --locs "\n"))))
3603 (defun org-export-format-code-default (element info)
3604 "Return source code from ELEMENT, formatted in a standard way.
3606 ELEMENT is either a `src-block' or `example-block' element. INFO
3607 is a plist used as a communication channel.
3609 This function takes care of line numbering and code references
3610 inclusion. Line numbers, when applicable, appear at the
3611 beginning of the line, separated from the code by two white
3612 spaces. Code references, on the other hand, appear flushed to
3613 the right, separated by six white spaces from the widest line of
3614 code."
3615 ;; Extract code and references.
3616 (let* ((code-info (org-export-unravel-code element))
3617 (code (car code-info))
3618 (code-lines (org-split-string code "\n"))
3619 (refs (and (org-element-property :retain-labels element)
3620 (cdr code-info)))
3621 ;; Handle line numbering.
3622 (num-start (case (org-element-property :number-lines element)
3623 (continued (org-export-get-loc element info))
3624 (new 0)))
3625 (num-fmt
3626 (and num-start
3627 (format "%%%ds "
3628 (length (number-to-string
3629 (+ (length code-lines) num-start))))))
3630 ;; Prepare references display, if required. Any reference
3631 ;; should start six columns after the widest line of code,
3632 ;; wrapped with parenthesis.
3633 (max-width
3634 (+ (apply 'max (mapcar 'length code-lines))
3635 (if (not num-start) 0 (length (format num-fmt num-start))))))
3636 (org-export-format-code
3637 code
3638 (lambda (loc line-num ref)
3639 (let ((number-str (and num-fmt (format num-fmt line-num))))
3640 (concat
3641 number-str
3643 (and ref
3644 (concat (make-string
3645 (- (+ 6 max-width)
3646 (+ (length loc) (length number-str))) ? )
3647 (format "(%s)" ref))))))
3648 num-start refs)))
3651 ;;;; For Tables
3653 ;; `org-export-table-has-special-column-p' and and
3654 ;; `org-export-table-row-is-special-p' are predicates used to look for
3655 ;; meta-information about the table structure.
3657 ;; `org-table-has-header-p' tells when the rows before the first rule
3658 ;; should be considered as table's header.
3660 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3661 ;; and `org-export-table-cell-borders' extract information from
3662 ;; a table-cell element.
3664 ;; `org-export-table-dimensions' gives the number on rows and columns
3665 ;; in the table, ignoring horizontal rules and special columns.
3666 ;; `org-export-table-cell-address', given a table-cell object, returns
3667 ;; the absolute address of a cell. On the other hand,
3668 ;; `org-export-get-table-cell-at' does the contrary.
3670 ;; `org-export-table-cell-starts-colgroup-p',
3671 ;; `org-export-table-cell-ends-colgroup-p',
3672 ;; `org-export-table-row-starts-rowgroup-p',
3673 ;; `org-export-table-row-ends-rowgroup-p',
3674 ;; `org-export-table-row-starts-header-p' and
3675 ;; `org-export-table-row-ends-header-p' indicate position of current
3676 ;; row or cell within the table.
3678 (defun org-export-table-has-special-column-p (table)
3679 "Non-nil when TABLE has a special column.
3680 All special columns will be ignored during export."
3681 ;; The table has a special column when every first cell of every row
3682 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3683 ;; "*" "_" and "^". Though, do not consider a first row containing
3684 ;; only empty cells as special.
3685 (let ((special-column-p 'empty))
3686 (catch 'exit
3687 (mapc
3688 (lambda (row)
3689 (when (eq (org-element-property :type row) 'standard)
3690 (let ((value (org-element-contents
3691 (car (org-element-contents row)))))
3692 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3693 (setq special-column-p 'special))
3694 ((not value))
3695 (t (throw 'exit nil))))))
3696 (org-element-contents table))
3697 (eq special-column-p 'special))))
3699 (defun org-export-table-has-header-p (table info)
3700 "Non-nil when TABLE has an header.
3702 INFO is a plist used as a communication channel.
3704 A table has an header when it contains at least two row groups."
3705 (let ((rowgroup 1) row-flag)
3706 (org-element-map
3707 table 'table-row
3708 (lambda (row)
3709 (cond
3710 ((> rowgroup 1) t)
3711 ((and row-flag (eq (org-element-property :type row) 'rule))
3712 (incf rowgroup) (setq row-flag nil))
3713 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3714 (setq row-flag t) nil)))
3715 info)))
3717 (defun org-export-table-row-is-special-p (table-row info)
3718 "Non-nil if TABLE-ROW is considered special.
3720 INFO is a plist used as the communication channel.
3722 All special rows will be ignored during export."
3723 (when (eq (org-element-property :type table-row) 'standard)
3724 (let ((first-cell (org-element-contents
3725 (car (org-element-contents table-row)))))
3726 ;; A row is special either when...
3728 ;; ... it starts with a field only containing "/",
3729 (equal first-cell '("/"))
3730 ;; ... the table contains a special column and the row start
3731 ;; with a marking character among, "^", "_", "$" or "!",
3732 (and (org-export-table-has-special-column-p
3733 (org-export-get-parent table-row))
3734 (member first-cell '(("^") ("_") ("$") ("!"))))
3735 ;; ... it contains only alignment cookies and empty cells.
3736 (let ((special-row-p 'empty))
3737 (catch 'exit
3738 (mapc
3739 (lambda (cell)
3740 (let ((value (org-element-contents cell)))
3741 ;; Since VALUE is a secondary string, the following
3742 ;; checks avoid expanding it with `org-export-data'.
3743 (cond ((not value))
3744 ((and (not (cdr value))
3745 (stringp (car value))
3746 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3747 (car value)))
3748 (setq special-row-p 'cookie))
3749 (t (throw 'exit nil)))))
3750 (org-element-contents table-row))
3751 (eq special-row-p 'cookie)))))))
3753 (defun org-export-table-row-group (table-row info)
3754 "Return TABLE-ROW's group.
3756 INFO is a plist used as the communication channel.
3758 Return value is the group number, as an integer, or nil special
3759 rows and table rules. Group 1 is also table's header."
3760 (unless (or (eq (org-element-property :type table-row) 'rule)
3761 (org-export-table-row-is-special-p table-row info))
3762 (let ((group 0) row-flag)
3763 (catch 'found
3764 (mapc
3765 (lambda (row)
3766 (cond
3767 ((and (eq (org-element-property :type row) 'standard)
3768 (not (org-export-table-row-is-special-p row info)))
3769 (unless row-flag (incf group) (setq row-flag t)))
3770 ((eq (org-element-property :type row) 'rule)
3771 (setq row-flag nil)))
3772 (when (eq table-row row) (throw 'found group)))
3773 (org-element-contents (org-export-get-parent table-row)))))))
3775 (defun org-export-table-cell-width (table-cell info)
3776 "Return TABLE-CELL contents width.
3778 INFO is a plist used as the communication channel.
3780 Return value is the width given by the last width cookie in the
3781 same column as TABLE-CELL, or nil."
3782 (let* ((row (org-export-get-parent table-cell))
3783 (column (let ((cells (org-element-contents row)))
3784 (- (length cells) (length (memq table-cell cells)))))
3785 (table (org-export-get-parent-table table-cell))
3786 cookie-width)
3787 (mapc
3788 (lambda (row)
3789 (cond
3790 ;; In a special row, try to find a width cookie at COLUMN.
3791 ((org-export-table-row-is-special-p row info)
3792 (let ((value (org-element-contents
3793 (elt (org-element-contents row) column))))
3794 ;; The following checks avoid expanding unnecessarily the
3795 ;; cell with `org-export-data'
3796 (when (and value
3797 (not (cdr value))
3798 (stringp (car value))
3799 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3800 (match-string 1 (car value)))
3801 (setq cookie-width
3802 (string-to-number (match-string 1 (car value)))))))
3803 ;; Ignore table rules.
3804 ((eq (org-element-property :type row) 'rule))))
3805 (org-element-contents table))
3806 ;; Return value.
3807 cookie-width))
3809 (defun org-export-table-cell-alignment (table-cell info)
3810 "Return TABLE-CELL contents alignment.
3812 INFO is a plist used as the communication channel.
3814 Return alignment as specified by the last alignment cookie in the
3815 same column as TABLE-CELL. If no such cookie is found, a default
3816 alignment value will be deduced from fraction of numbers in the
3817 column (see `org-table-number-fraction' for more information).
3818 Possible values are `left', `right' and `center'."
3819 (let* ((row (org-export-get-parent table-cell))
3820 (column (let ((cells (org-element-contents row)))
3821 (- (length cells) (length (memq table-cell cells)))))
3822 (table (org-export-get-parent-table table-cell))
3823 (number-cells 0)
3824 (total-cells 0)
3825 cookie-align)
3826 (mapc
3827 (lambda (row)
3828 (cond
3829 ;; In a special row, try to find an alignment cookie at
3830 ;; COLUMN.
3831 ((org-export-table-row-is-special-p row info)
3832 (let ((value (org-element-contents
3833 (elt (org-element-contents row) column))))
3834 ;; Since VALUE is a secondary string, the following checks
3835 ;; avoid useless expansion through `org-export-data'.
3836 (when (and value
3837 (not (cdr value))
3838 (stringp (car value))
3839 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3840 (car value))
3841 (match-string 1 (car value)))
3842 (setq cookie-align (match-string 1 (car value))))))
3843 ;; Ignore table rules.
3844 ((eq (org-element-property :type row) 'rule))
3845 ;; In a standard row, check if cell's contents are expressing
3846 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3847 ;; Though, don't bother if an alignment cookie has already
3848 ;; defined cell's alignment.
3849 ((not cookie-align)
3850 (let ((value (org-export-data
3851 (org-element-contents
3852 (elt (org-element-contents row) column))
3853 info)))
3854 (incf total-cells)
3855 (when (string-match org-table-number-regexp value)
3856 (incf number-cells))))))
3857 (org-element-contents table))
3858 ;; Return value. Alignment specified by cookies has precedence
3859 ;; over alignment deduced from cells contents.
3860 (cond ((equal cookie-align "l") 'left)
3861 ((equal cookie-align "r") 'right)
3862 ((equal cookie-align "c") 'center)
3863 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3864 'right)
3865 (t 'left))))
3867 (defun org-export-table-cell-borders (table-cell info)
3868 "Return TABLE-CELL borders.
3870 INFO is a plist used as a communication channel.
3872 Return value is a list of symbols, or nil. Possible values are:
3873 `top', `bottom', `above', `below', `left' and `right'. Note:
3874 `top' (resp. `bottom') only happen for a cell in the first
3875 row (resp. last row) of the table, ignoring table rules, if any.
3877 Returned borders ignore special rows."
3878 (let* ((row (org-export-get-parent table-cell))
3879 (table (org-export-get-parent-table table-cell))
3880 borders)
3881 ;; Top/above border? TABLE-CELL has a border above when a rule
3882 ;; used to demarcate row groups can be found above. Hence,
3883 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3884 ;; another regular row has to be found above that rule.
3885 (let (rule-flag)
3886 (catch 'exit
3887 (mapc (lambda (row)
3888 (cond ((eq (org-element-property :type row) 'rule)
3889 (setq rule-flag t))
3890 ((not (org-export-table-row-is-special-p row info))
3891 (if rule-flag (throw 'exit (push 'above borders))
3892 (throw 'exit nil)))))
3893 ;; Look at every row before the current one.
3894 (cdr (memq row (reverse (org-element-contents table)))))
3895 ;; No rule above, or rule found starts the table (ignoring any
3896 ;; special row): TABLE-CELL is at the top of the table.
3897 (when rule-flag (push 'above borders))
3898 (push 'top borders)))
3899 ;; Bottom/below border? TABLE-CELL has a border below when next
3900 ;; non-regular row below is a rule.
3901 (let (rule-flag)
3902 (catch 'exit
3903 (mapc (lambda (row)
3904 (cond ((eq (org-element-property :type row) 'rule)
3905 (setq rule-flag t))
3906 ((not (org-export-table-row-is-special-p row info))
3907 (if rule-flag (throw 'exit (push 'below borders))
3908 (throw 'exit nil)))))
3909 ;; Look at every row after the current one.
3910 (cdr (memq row (org-element-contents table))))
3911 ;; No rule below, or rule found ends the table (modulo some
3912 ;; special row): TABLE-CELL is at the bottom of the table.
3913 (when rule-flag (push 'below borders))
3914 (push 'bottom borders)))
3915 ;; Right/left borders? They can only be specified by column
3916 ;; groups. Column groups are defined in a row starting with "/".
3917 ;; Also a column groups row only contains "<", "<>", ">" or blank
3918 ;; cells.
3919 (catch 'exit
3920 (let ((column (let ((cells (org-element-contents row)))
3921 (- (length cells) (length (memq table-cell cells))))))
3922 (mapc
3923 (lambda (row)
3924 (unless (eq (org-element-property :type row) 'rule)
3925 (when (equal (org-element-contents
3926 (car (org-element-contents row)))
3927 '("/"))
3928 (let ((column-groups
3929 (mapcar
3930 (lambda (cell)
3931 (let ((value (org-element-contents cell)))
3932 (when (member value '(("<") ("<>") (">") nil))
3933 (car value))))
3934 (org-element-contents row))))
3935 ;; There's a left border when previous cell, if
3936 ;; any, ends a group, or current one starts one.
3937 (when (or (and (not (zerop column))
3938 (member (elt column-groups (1- column))
3939 '(">" "<>")))
3940 (member (elt column-groups column) '("<" "<>")))
3941 (push 'left borders))
3942 ;; There's a right border when next cell, if any,
3943 ;; starts a group, or current one ends one.
3944 (when (or (and (/= (1+ column) (length column-groups))
3945 (member (elt column-groups (1+ column))
3946 '("<" "<>")))
3947 (member (elt column-groups column) '(">" "<>")))
3948 (push 'right borders))
3949 (throw 'exit nil)))))
3950 ;; Table rows are read in reverse order so last column groups
3951 ;; row has precedence over any previous one.
3952 (reverse (org-element-contents table)))))
3953 ;; Return value.
3954 borders))
3956 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3957 "Non-nil when TABLE-CELL is at the beginning of a row group.
3958 INFO is a plist used as a communication channel."
3959 ;; A cell starts a column group either when it is at the beginning
3960 ;; of a row (or after the special column, if any) or when it has
3961 ;; a left border.
3962 (or (eq (org-element-map
3963 (org-export-get-parent table-cell)
3964 'table-cell 'identity info 'first-match)
3965 table-cell)
3966 (memq 'left (org-export-table-cell-borders table-cell info))))
3968 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3969 "Non-nil when TABLE-CELL is at the end of a row group.
3970 INFO is a plist used as a communication channel."
3971 ;; A cell ends a column group either when it is at the end of a row
3972 ;; or when it has a right border.
3973 (or (eq (car (last (org-element-contents
3974 (org-export-get-parent table-cell))))
3975 table-cell)
3976 (memq 'right (org-export-table-cell-borders table-cell info))))
3978 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3979 "Non-nil when TABLE-ROW is at the beginning of a column group.
3980 INFO is a plist used as a communication channel."
3981 (unless (or (eq (org-element-property :type table-row) 'rule)
3982 (org-export-table-row-is-special-p table-row info))
3983 (let ((borders (org-export-table-cell-borders
3984 (car (org-element-contents table-row)) info)))
3985 (or (memq 'top borders) (memq 'above borders)))))
3987 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3988 "Non-nil when TABLE-ROW is at the end of a column group.
3989 INFO is a plist used as a communication channel."
3990 (unless (or (eq (org-element-property :type table-row) 'rule)
3991 (org-export-table-row-is-special-p table-row info))
3992 (let ((borders (org-export-table-cell-borders
3993 (car (org-element-contents table-row)) info)))
3994 (or (memq 'bottom borders) (memq 'below borders)))))
3996 (defun org-export-table-row-starts-header-p (table-row info)
3997 "Non-nil when TABLE-ROW is the first table header's row.
3998 INFO is a plist used as a communication channel."
3999 (and (org-export-table-has-header-p
4000 (org-export-get-parent-table table-row) info)
4001 (org-export-table-row-starts-rowgroup-p table-row info)
4002 (= (org-export-table-row-group table-row info) 1)))
4004 (defun org-export-table-row-ends-header-p (table-row info)
4005 "Non-nil when TABLE-ROW is the last table header's row.
4006 INFO is a plist used as a communication channel."
4007 (and (org-export-table-has-header-p
4008 (org-export-get-parent-table table-row) info)
4009 (org-export-table-row-ends-rowgroup-p table-row info)
4010 (= (org-export-table-row-group table-row info) 1)))
4012 (defun org-export-table-dimensions (table info)
4013 "Return TABLE dimensions.
4015 INFO is a plist used as a communication channel.
4017 Return value is a CONS like (ROWS . COLUMNS) where
4018 ROWS (resp. COLUMNS) is the number of exportable
4019 rows (resp. columns)."
4020 (let (first-row (columns 0) (rows 0))
4021 ;; Set number of rows, and extract first one.
4022 (org-element-map
4023 table 'table-row
4024 (lambda (row)
4025 (when (eq (org-element-property :type row) 'standard)
4026 (incf rows)
4027 (unless first-row (setq first-row row)))) info)
4028 ;; Set number of columns.
4029 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4030 ;; Return value.
4031 (cons rows columns)))
4033 (defun org-export-table-cell-address (table-cell info)
4034 "Return address of a regular TABLE-CELL object.
4036 TABLE-CELL is the cell considered. INFO is a plist used as
4037 a communication channel.
4039 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4040 zero-based index. Only exportable cells are considered. The
4041 function returns nil for other cells."
4042 (let* ((table-row (org-export-get-parent table-cell))
4043 (table (org-export-get-parent-table table-cell)))
4044 ;; Ignore cells in special rows or in special column.
4045 (unless (or (org-export-table-row-is-special-p table-row info)
4046 (and (org-export-table-has-special-column-p table)
4047 (eq (car (org-element-contents table-row)) table-cell)))
4048 (cons
4049 ;; Row number.
4050 (let ((row-count 0))
4051 (org-element-map
4052 table 'table-row
4053 (lambda (row)
4054 (cond ((eq (org-element-property :type row) 'rule) nil)
4055 ((eq row table-row) row-count)
4056 (t (incf row-count) nil)))
4057 info 'first-match))
4058 ;; Column number.
4059 (let ((col-count 0))
4060 (org-element-map
4061 table-row 'table-cell
4062 (lambda (cell)
4063 (if (eq cell table-cell) col-count (incf col-count) nil))
4064 info 'first-match))))))
4066 (defun org-export-get-table-cell-at (address table info)
4067 "Return regular table-cell object at ADDRESS in TABLE.
4069 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4070 zero-based index. TABLE is a table type element. INFO is
4071 a plist used as a communication channel.
4073 If no table-cell, among exportable cells, is found at ADDRESS,
4074 return nil."
4075 (let ((column-pos (cdr address)) (column-count 0))
4076 (org-element-map
4077 ;; Row at (car address) or nil.
4078 (let ((row-pos (car address)) (row-count 0))
4079 (org-element-map
4080 table 'table-row
4081 (lambda (row)
4082 (cond ((eq (org-element-property :type row) 'rule) nil)
4083 ((= row-count row-pos) row)
4084 (t (incf row-count) nil)))
4085 info 'first-match))
4086 'table-cell
4087 (lambda (cell)
4088 (if (= column-count column-pos) cell
4089 (incf column-count) nil))
4090 info 'first-match)))
4093 ;;;; For Tables Of Contents
4095 ;; `org-export-collect-headlines' builds a list of all exportable
4096 ;; headline elements, maybe limited to a certain depth. One can then
4097 ;; easily parse it and transcode it.
4099 ;; Building lists of tables, figures or listings is quite similar.
4100 ;; Once the generic function `org-export-collect-elements' is defined,
4101 ;; `org-export-collect-tables', `org-export-collect-figures' and
4102 ;; `org-export-collect-listings' can be derived from it.
4104 (defun org-export-collect-headlines (info &optional n)
4105 "Collect headlines in order to build a table of contents.
4107 INFO is a plist used as a communication channel.
4109 When optional argument N is an integer, it specifies the depth of
4110 the table of contents. Otherwise, it is set to the value of the
4111 last headline level. See `org-export-headline-levels' for more
4112 information.
4114 Return a list of all exportable headlines as parsed elements."
4115 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4116 (org-element-map
4117 (plist-get info :parse-tree)
4118 'headline
4119 (lambda (headline)
4120 ;; Strip contents from HEADLINE.
4121 (let ((relative-level (org-export-get-relative-level headline info)))
4122 (unless (> relative-level n) headline)))
4123 info))
4125 (defun org-export-collect-elements (type info &optional predicate)
4126 "Collect referenceable elements of a determined type.
4128 TYPE can be a symbol or a list of symbols specifying element
4129 types to search. Only elements with a caption are collected.
4131 INFO is a plist used as a communication channel.
4133 When non-nil, optional argument PREDICATE is a function accepting
4134 one argument, an element of type TYPE. It returns a non-nil
4135 value when that element should be collected.
4137 Return a list of all elements found, in order of appearance."
4138 (org-element-map
4139 (plist-get info :parse-tree) type
4140 (lambda (element)
4141 (and (org-element-property :caption element)
4142 (or (not predicate) (funcall predicate element))
4143 element))
4144 info))
4146 (defun org-export-collect-tables (info)
4147 "Build a list of tables.
4148 INFO is a plist used as a communication channel.
4150 Return a list of table elements with a caption."
4151 (org-export-collect-elements 'table info))
4153 (defun org-export-collect-figures (info predicate)
4154 "Build a list of figures.
4156 INFO is a plist used as a communication channel. PREDICATE is
4157 a function which accepts one argument: a paragraph element and
4158 whose return value is non-nil when that element should be
4159 collected.
4161 A figure is a paragraph type element, with a caption, verifying
4162 PREDICATE. The latter has to be provided since a \"figure\" is
4163 a vague concept that may depend on back-end.
4165 Return a list of elements recognized as figures."
4166 (org-export-collect-elements 'paragraph info predicate))
4168 (defun org-export-collect-listings (info)
4169 "Build a list of src blocks.
4171 INFO is a plist used as a communication channel.
4173 Return a list of src-block elements with a caption."
4174 (org-export-collect-elements 'src-block info))
4177 ;;;; Topology
4179 ;; Here are various functions to retrieve information about the
4180 ;; neighbourhood of a given element or object. Neighbours of interest
4181 ;; are direct parent (`org-export-get-parent'), parent headline
4182 ;; (`org-export-get-parent-headline'), first element containing an
4183 ;; object, (`org-export-get-parent-element'), parent table
4184 ;; (`org-export-get-parent-table'), previous element or object
4185 ;; (`org-export-get-previous-element') and next element or object
4186 ;; (`org-export-get-next-element').
4188 ;; `org-export-get-genealogy' returns the full genealogy of a given
4189 ;; element or object, from closest parent to full parse tree.
4191 (defun org-export-get-parent (blob)
4192 "Return BLOB parent or nil.
4193 BLOB is the element or object considered."
4194 (org-element-property :parent blob))
4196 (defun org-export-get-genealogy (blob)
4197 "Return full genealogy relative to a given element or object.
4199 BLOB is the element or object being considered.
4201 Ancestors are returned from closest to farthest, the last one
4202 being the full parse tree."
4203 (let (genealogy (parent blob))
4204 (while (setq parent (org-element-property :parent parent))
4205 (push parent genealogy))
4206 (nreverse genealogy)))
4208 (defun org-export-get-parent-headline (blob)
4209 "Return BLOB parent headline or nil.
4210 BLOB is the element or object being considered."
4211 (let ((parent blob))
4212 (while (and (setq parent (org-element-property :parent parent))
4213 (not (eq (org-element-type parent) 'headline))))
4214 parent))
4216 (defun org-export-get-parent-element (object)
4217 "Return first element containing OBJECT or nil.
4218 OBJECT is the object to consider."
4219 (let ((parent object))
4220 (while (and (setq parent (org-element-property :parent parent))
4221 (memq (org-element-type parent) org-element-all-objects)))
4222 parent))
4224 (defun org-export-get-parent-table (object)
4225 "Return OBJECT parent table or nil.
4226 OBJECT is either a `table-cell' or `table-element' type object."
4227 (let ((parent object))
4228 (while (and (setq parent (org-element-property :parent parent))
4229 (not (eq (org-element-type parent) 'table))))
4230 parent))
4232 (defun org-export-get-previous-element (blob info)
4233 "Return previous element or object.
4234 BLOB is an element or object. INFO is a plist used as
4235 a communication channel. Return previous exportable element or
4236 object, a string, or nil."
4237 (let (prev)
4238 (catch 'exit
4239 (mapc (lambda (obj)
4240 (cond ((eq obj blob) (throw 'exit prev))
4241 ((memq obj (plist-get info :ignore-list)))
4242 (t (setq prev obj))))
4243 (org-element-contents (org-export-get-parent blob))))))
4245 (defun org-export-get-next-element (blob info)
4246 "Return next element or object.
4247 BLOB is an element or object. INFO is a plist used as
4248 a communication channel. Return next exportable element or
4249 object, a string, or nil."
4250 (catch 'found
4251 (mapc (lambda (obj)
4252 (unless (memq obj (plist-get info :ignore-list))
4253 (throw 'found obj)))
4254 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4255 nil))
4258 ;;;; Translation
4260 ;; `org-export-translate' translates a string according to language
4261 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4262 ;; variable and a specified charset. `org-export-dictionary' contains
4263 ;; the dictionary used for the translation.
4265 (defconst org-export-dictionary
4266 '(("Author"
4267 ("ca" :default "Autor")
4268 ("cs" :default "Autor")
4269 ("da" :default "Ophavsmand")
4270 ("de" :default "Autor")
4271 ("eo" :html "A&#365;toro")
4272 ("es" :default "Autor")
4273 ("fi" :html "Tekij&auml;")
4274 ("fr" :default "Auteur")
4275 ("hu" :default "Szerz&otilde;")
4276 ("is" :html "H&ouml;fundur")
4277 ("it" :default "Autore")
4278 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4279 ("nl" :default "Auteur")
4280 ("no" :default "Forfatter")
4281 ("nb" :default "Forfatter")
4282 ("nn" :default "Forfattar")
4283 ("pl" :default "Autor")
4284 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4285 ("sv" :html "F&ouml;rfattare")
4286 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4287 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4288 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4289 ("Date"
4290 ("ca" :default "Data")
4291 ("cs" :default "Datum")
4292 ("da" :default "Dato")
4293 ("de" :default "Datum")
4294 ("eo" :default "Dato")
4295 ("es" :default "Fecha")
4296 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4297 ("hu" :html "D&aacute;tum")
4298 ("is" :default "Dagsetning")
4299 ("it" :default "Data")
4300 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4301 ("nl" :default "Datum")
4302 ("no" :default "Dato")
4303 ("nb" :default "Dato")
4304 ("nn" :default "Dato")
4305 ("pl" :default "Data")
4306 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4307 ("sv" :default "Datum")
4308 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4309 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4310 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4311 ("Equation"
4312 ("fr" :ascii "Equation" :default "Équation"))
4313 ("Figure")
4314 ("Footnotes"
4315 ("ca" :html "Peus de p&agrave;gina")
4316 ("cs" :default "Pozn\xe1mky pod carou")
4317 ("da" :default "Fodnoter")
4318 ("de" :html "Fu&szlig;noten")
4319 ("eo" :default "Piednotoj")
4320 ("es" :html "Pies de p&aacute;gina")
4321 ("fi" :default "Alaviitteet")
4322 ("fr" :default "Notes de bas de page")
4323 ("hu" :html "L&aacute;bjegyzet")
4324 ("is" :html "Aftanm&aacute;lsgreinar")
4325 ("it" :html "Note a pi&egrave; di pagina")
4326 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4327 ("nl" :default "Voetnoten")
4328 ("no" :default "Fotnoter")
4329 ("nb" :default "Fotnoter")
4330 ("nn" :default "Fotnotar")
4331 ("pl" :default "Przypis")
4332 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4333 ("sv" :default "Fotnoter")
4334 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4335 :utf-8 "Примітки")
4336 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4337 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4338 ("List of Listings"
4339 ("fr" :default "Liste des programmes"))
4340 ("List of Tables"
4341 ("fr" :default "Liste des tableaux"))
4342 ("Listing %d:"
4343 ("fr"
4344 :ascii "Programme %d :" :default "Programme nº %d :"
4345 :latin1 "Programme %d :"))
4346 ("Listing %d: %s"
4347 ("fr"
4348 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4349 :latin1 "Programme %d : %s"))
4350 ("See section %s"
4351 ("fr" :default "cf. section %s"))
4352 ("Table %d:"
4353 ("fr"
4354 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4355 ("Table %d: %s"
4356 ("fr"
4357 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4358 :latin1 "Tableau %d : %s"))
4359 ("Table of Contents"
4360 ("ca" :html "&Iacute;ndex")
4361 ("cs" :default "Obsah")
4362 ("da" :default "Indhold")
4363 ("de" :default "Inhaltsverzeichnis")
4364 ("eo" :default "Enhavo")
4365 ("es" :html "&Iacute;ndice")
4366 ("fi" :html "Sis&auml;llysluettelo")
4367 ("fr" :ascii "Sommaire" :default "Table des matières")
4368 ("hu" :html "Tartalomjegyz&eacute;k")
4369 ("is" :default "Efnisyfirlit")
4370 ("it" :default "Indice")
4371 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4372 ("nl" :default "Inhoudsopgave")
4373 ("no" :default "Innhold")
4374 ("nb" :default "Innhold")
4375 ("nn" :default "Innhald")
4376 ("pl" :html "Spis tre&#x015b;ci")
4377 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4378 :utf-8 "Содержание")
4379 ("sv" :html "Inneh&aring;ll")
4380 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4381 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4382 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4383 ("Unknown reference"
4384 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4385 "Dictionary for export engine.
4387 Alist whose CAR is the string to translate and CDR is an alist
4388 whose CAR is the language string and CDR is a plist whose
4389 properties are possible charsets and values translated terms.
4391 It is used as a database for `org-export-translate'. Since this
4392 function returns the string as-is if no translation was found,
4393 the variable only needs to record values different from the
4394 entry.")
4396 (defun org-export-translate (s encoding info)
4397 "Translate string S according to language specification.
4399 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4400 and `:utf-8'. INFO is a plist used as a communication channel.
4402 Translation depends on `:language' property. Return the
4403 translated string. If no translation is found, try to fall back
4404 to `:default' encoding. If it fails, return S."
4405 (let* ((lang (plist-get info :language))
4406 (translations (cdr (assoc lang
4407 (cdr (assoc s org-export-dictionary))))))
4408 (or (plist-get translations encoding)
4409 (plist-get translations :default)
4410 s)))
4414 ;;; The Dispatcher
4416 ;; `org-export-dispatch' is the standard interactive way to start an
4417 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4418 ;; for its interface, which, in turn, delegates response to key
4419 ;; pressed to `org-export-dispatch-action'.
4421 (defvar org-export-dispatch-menu-entries nil
4422 "List of menu entries available for `org-export-dispatch'.
4423 This variable shouldn't be set directly. Set-up :menu-entry
4424 keyword in either `org-export-define-backend' or
4425 `org-export-define-derived-backend' instead.")
4427 ;;;###autoload
4428 (defun org-export-dispatch ()
4429 "Export dispatcher for Org mode.
4431 It provides an access to common export related tasks in a buffer.
4432 Its interface comes in two flavours: standard and expert. While
4433 both share the same set of bindings, only the former displays the
4434 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4435 to switch to one or the other.
4437 Return an error if key pressed has no associated command."
4438 (interactive)
4439 (let* ((input (org-export-dispatch-ui (list org-export-initial-scope)
4441 org-export-dispatch-use-expert-ui))
4442 (action (car input))
4443 (optns (cdr input)))
4444 (case action
4445 ;; First handle special hard-coded actions.
4446 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4447 (publish-current-project
4448 (org-e-publish-current-project (memq 'force optns)))
4449 (publish-choose-project
4450 (org-e-publish (assoc (org-icompleting-read
4451 "Publish project: "
4452 org-e-publish-project-alist nil t)
4453 org-e-publish-project-alist)
4454 (memq 'force optns)))
4455 (publish-all (org-e-publish-all (memq 'force optns)))
4456 (otherwise
4457 (funcall action
4458 (memq 'subtree optns)
4459 (memq 'visible optns)
4460 (memq 'body optns))))))
4462 (defun org-export-dispatch-ui (options first-key expertp)
4463 "Handle interface for `org-export-dispatch'.
4465 OPTIONS is a list containing current interactive options set for
4466 export. It can contain any of the following symbols:
4467 `body' toggles a body-only export
4468 `subtree' restricts export to current subtree
4469 `visible' restricts export to visible part of buffer.
4470 `force' force publishing files.
4472 FIRST-KEY is the key pressed to select the first level menu. It
4473 is nil when this menu hasn't been selected yet.
4475 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4476 buffer is provided, but indications about currently active
4477 options are given in the prompt. Moreover, \[?] allows to switch
4478 back to standard interface."
4479 (let* ((fontify-key
4480 (lambda (key &optional access-key)
4481 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4482 ;; non-nil is the required first-level key to activate
4483 ;; KEY. When its value is t, activate KEY independently
4484 ;; on the first key, if any. A nil value means KEY will
4485 ;; only be activated at first level.
4486 (if (or (eq access-key t) (eq access-key first-key))
4487 (org-add-props key nil 'face 'org-warning)
4488 (org-no-properties key))))
4489 ;; Make sure order of menu doesn't depend on the order in
4490 ;; which back-ends are loaded.
4491 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4492 (lambda (a b) (< (car a) (car b)))))
4493 ;; Compute a list of allowed keys based on the first key
4494 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4495 ;; always available.
4496 (allowed-keys
4497 (nconc (list ?1 ?2 ?3 ?4)
4498 (mapcar 'car
4499 (if (not first-key) backends
4500 (nth 2 (assq first-key backends))))
4501 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4502 ((not first-key) (list ?P)))
4503 (when expertp (list ??))
4504 (list ?q)))
4505 ;; Build the help menu for standard UI.
4506 (help
4507 (unless expertp
4508 (concat
4509 ;; Options are hard-coded.
4510 (format "Options
4511 [%s] Body only: %s [%s] Visible only: %s
4512 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4513 (funcall fontify-key "1" t)
4514 (if (memq 'body options) "On " "Off")
4515 (funcall fontify-key "2" t)
4516 (if (memq 'visible options) "On " "Off")
4517 (funcall fontify-key "3" t)
4518 (if (memq 'subtree options) "Subtree" "Buffer ")
4519 (funcall fontify-key "4" t)
4520 (if (memq 'force options) "On " "Off"))
4521 ;; Display registered back-end entries.
4522 (mapconcat
4523 (lambda (entry)
4524 (let ((top-key (car entry)))
4525 (concat
4526 (format "[%s] %s\n"
4527 (funcall fontify-key (char-to-string top-key))
4528 (nth 1 entry))
4529 (let ((sub-menu (nth 2 entry)))
4530 (unless (functionp sub-menu)
4531 ;; Split sub-menu into two columns.
4532 (let ((index -1))
4533 (concat
4534 (mapconcat
4535 (lambda (sub-entry)
4536 (incf index)
4537 (format (if (zerop (mod index 2)) " [%s] %-24s"
4538 "[%s] %s\n")
4539 (funcall fontify-key
4540 (char-to-string (car sub-entry))
4541 top-key)
4542 (nth 1 sub-entry)))
4543 sub-menu "")
4544 (when (zerop (mod index 2)) "\n"))))))))
4545 backends "\n")
4546 ;; Publishing menu is hard-coded.
4547 (format "\n[%s] Publish
4548 [%s] Current file [%s] Current project
4549 [%s] Choose project [%s] All projects\n\n"
4550 (funcall fontify-key "P")
4551 (funcall fontify-key "f" ?P)
4552 (funcall fontify-key "p" ?P)
4553 (funcall fontify-key "x" ?P)
4554 (funcall fontify-key "a" ?P))
4555 (format "\[%s] %s"
4556 (funcall fontify-key "q" t)
4557 (if first-key "Main menu" "Exit")))))
4558 ;; Build prompts for both standard and expert UI.
4559 (standard-prompt (unless expertp "Export command: "))
4560 (expert-prompt
4561 (when expertp
4562 (format
4563 "Export command (Options: %s%s%s%s) [%s]: "
4564 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4565 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4566 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4567 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4568 (concat allowed-keys)))))
4569 ;; With expert UI, just read key with a fancy prompt. In standard
4570 ;; UI, display an intrusive help buffer.
4571 (if expertp
4572 (org-export-dispatch-action
4573 expert-prompt allowed-keys backends options first-key expertp)
4574 (save-window-excursion
4575 (delete-other-windows)
4576 (unwind-protect
4577 (progn
4578 (with-current-buffer
4579 (get-buffer-create "*Org Export Dispatcher*")
4580 (erase-buffer)
4581 (save-excursion (insert help)))
4582 (org-fit-window-to-buffer
4583 (display-buffer "*Org Export Dispatcher*"))
4584 (org-export-dispatch-action
4585 standard-prompt allowed-keys backends options first-key expertp))
4586 (and (get-buffer "*Org Export Dispatcher*")
4587 (kill-buffer "*Org Export Dispatcher*")))))))
4589 (defun org-export-dispatch-action
4590 (prompt allowed-keys backends options first-key expertp)
4591 "Read a character from command input and act accordingly.
4593 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4594 a list of characters available at a given step in the process.
4595 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4596 EXPERTP are the same as defined in `org-export-dispatch-ui',
4597 which see.
4599 Toggle export options when required. Otherwise, return value is
4600 a list with action as CAR and a list of interactive export
4601 options as CDR."
4602 (let ((key (let ((k (read-char-exclusive prompt)))
4603 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4604 ;; depending on user's key pressed.
4605 (if (< k 27) (+ k 96) k))))
4606 (cond
4607 ;; Ignore non-standard characters (i.e. "M-a") and
4608 ;; undefined associations.
4609 ((not (memq key allowed-keys))
4610 (org-export-dispatch-ui options first-key expertp))
4611 ;; q key at first level aborts export. At second
4612 ;; level, cancel first key instead.
4613 ((eq key ?q) (if (not first-key) (error "Export aborted")
4614 (org-export-dispatch-ui options nil expertp)))
4615 ;; Help key: Switch back to standard interface if
4616 ;; expert UI was active.
4617 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4618 ;; Toggle export options.
4619 ((memq key '(?1 ?2 ?3 ?4))
4620 (org-export-dispatch-ui
4621 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4622 (?4 'force))))
4623 (if (memq option options) (remq option options)
4624 (cons option options)))
4625 first-key expertp))
4626 ;; Action selected: Send key and options back to
4627 ;; `org-export-dispatch'.
4628 ((or first-key
4629 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4630 (functionp (nth 2 (assq key backends))))
4631 (cons (cond
4632 ((not first-key) (nth 2 (assq key backends)))
4633 ;; Publishing actions are hard-coded. Send a special
4634 ;; signal to `org-export-dispatch'.
4635 ((eq first-key ?P)
4636 (case key
4637 (?f 'publish-current-file)
4638 (?p 'publish-current-project)
4639 (?x 'publish-choose-project)
4640 (?a 'publish-all)))
4641 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4642 options))
4643 ;; Otherwise, enter sub-menu.
4644 (t (org-export-dispatch-ui options key expertp)))))
4647 (provide 'org-export)
4648 ;;; org-export.el ends here