Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-export.el
blob8c4e00352798b7ded28bafff2557a33ed1b21e31
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. TITLE keyword is excluded as it has
1375 ;; already been handled already.
1376 (let ((seen '("TITLE")))
1377 (mapc
1378 (lambda (option)
1379 (let ((property (nth 1 option)))
1380 (when (and property (not (member property seen)))
1381 (let* ((subtree-prop (concat "EXPORT_" property))
1382 ;; Export properties are not case-sensitive.
1383 (value (let ((case-fold-search t))
1384 (org-entry-get (point) subtree-prop))))
1385 (push property seen)
1386 (when value
1387 (setq plist
1388 (plist-put
1389 plist
1390 (car option)
1391 ;; Parse VALUE if required.
1392 (if (member property org-element-document-properties)
1393 (org-element-parse-secondary-string
1394 value (org-element-restriction 'keyword))
1395 value))))))))
1396 ;; Also look for both general keywords and back-end specific
1397 ;; options if BACKEND is provided.
1398 (append (and backend
1399 (let ((var (intern
1400 (format "org-%s-options-alist" backend))))
1401 (and (boundp var) (symbol-value var))))
1402 org-export-options-alist)))
1403 ;; Return value.
1404 plist)))
1406 (defun org-export--get-inbuffer-options (&optional backend files)
1407 "Return current buffer export options, as a plist.
1409 Optional argument BACKEND, when non-nil, is a symbol specifying
1410 which back-end specific options should also be read in the
1411 process.
1413 Optional argument FILES is a list of setup files names read so
1414 far, used to avoid circular dependencies.
1416 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1417 (org-with-wide-buffer
1418 (goto-char (point-min))
1419 (let ((case-fold-search t) plist)
1420 ;; 1. Special keywords, as in `org-export-special-keywords'.
1421 (let ((special-re
1422 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1423 (while (re-search-forward special-re nil t)
1424 (let ((element (org-element-at-point)))
1425 (when (eq (org-element-type element) 'keyword)
1426 (let* ((key (org-element-property :key element))
1427 (val (org-element-property :value element))
1428 (prop
1429 (cond
1430 ((string= key "SETUP_FILE")
1431 (let ((file
1432 (expand-file-name
1433 (org-remove-double-quotes (org-trim val)))))
1434 ;; Avoid circular dependencies.
1435 (unless (member file files)
1436 (with-temp-buffer
1437 (insert (org-file-contents file 'noerror))
1438 (org-mode)
1439 (org-export--get-inbuffer-options
1440 backend (cons file files))))))
1441 ((string= key "OPTIONS")
1442 (org-export--parse-option-keyword val backend)))))
1443 (setq plist (org-combine-plists plist prop)))))))
1444 ;; 2. Standard options, as in `org-export-options-alist'.
1445 (let* ((all (append org-export-options-alist
1446 ;; Also look for back-end specific options
1447 ;; if BACKEND is defined.
1448 (and backend
1449 (let ((var
1450 (intern
1451 (format "org-%s-options-alist" backend))))
1452 (and (boundp var) (eval var))))))
1453 ;; Build ALIST between keyword name and property name.
1454 (alist
1455 (delq nil (mapcar
1456 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1457 all)))
1458 ;; Build regexp matching all keywords associated to export
1459 ;; options. Note: the search is case insensitive.
1460 (opt-re (format "^[ \t]*#\\+%s:"
1461 (regexp-opt
1462 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1463 (goto-char (point-min))
1464 (while (re-search-forward opt-re nil t)
1465 (let ((element (org-element-at-point)))
1466 (when (eq (org-element-type element) 'keyword)
1467 (let* ((key (org-element-property :key element))
1468 (val (org-element-property :value element))
1469 (prop (cdr (assoc key alist)))
1470 (behaviour (nth 4 (assq prop all))))
1471 (setq plist
1472 (plist-put
1473 plist prop
1474 ;; Handle value depending on specified BEHAVIOUR.
1475 (case behaviour
1476 (space
1477 (if (not (plist-get plist prop)) (org-trim val)
1478 (concat (plist-get plist prop) " " (org-trim val))))
1479 (newline
1480 (org-trim
1481 (concat (plist-get plist prop) "\n" (org-trim val))))
1482 (split
1483 `(,@(plist-get plist prop) ,@(org-split-string val)))
1484 ('t val)
1485 (otherwise (if (not (plist-member plist prop)) val
1486 (plist-get plist prop))))))))))
1487 ;; Parse keywords specified in
1488 ;; `org-element-document-properties'.
1489 (mapc
1490 (lambda (key)
1491 (let* ((prop (cdr (assoc key alist)))
1492 (value (and prop (plist-get plist prop))))
1493 (when (stringp value)
1494 (setq plist
1495 (plist-put
1496 plist prop
1497 (org-element-parse-secondary-string
1498 value (org-element-restriction 'keyword)))))))
1499 org-element-document-properties))
1500 ;; 3. Return final value.
1501 plist)))
1503 (defun org-export--get-buffer-attributes ()
1504 "Return properties related to buffer attributes, as a plist."
1505 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1506 (list
1507 ;; Store full path of input file name, or nil. For internal use.
1508 :input-file visited-file
1509 :title (or (and visited-file
1510 (file-name-sans-extension
1511 (file-name-nondirectory visited-file)))
1512 (buffer-name (buffer-base-buffer)))
1513 :footnote-definition-alist
1514 ;; Footnotes definitions must be collected in the original
1515 ;; buffer, as there's no insurance that they will still be in the
1516 ;; parse tree, due to possible narrowing.
1517 (let (alist)
1518 (org-with-wide-buffer
1519 (goto-char (point-min))
1520 (while (re-search-forward org-footnote-definition-re nil t)
1521 (let ((def (save-match-data (org-element-at-point))))
1522 (when (eq (org-element-type def) 'footnote-definition)
1523 (push
1524 (cons (org-element-property :label def)
1525 (let ((cbeg (org-element-property :contents-begin def)))
1526 (when cbeg
1527 (org-element--parse-elements
1528 cbeg (org-element-property :contents-end def)
1529 nil nil nil nil (list 'org-data nil)))))
1530 alist))))
1531 alist))
1532 :id-alist
1533 ;; Collect id references.
1534 (let (alist)
1535 (org-with-wide-buffer
1536 (goto-char (point-min))
1537 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1538 (let ((link (org-element-context)))
1539 (when (eq (org-element-type link) 'link)
1540 (let* ((id (org-element-property :path link))
1541 (file (org-id-find-id-file id)))
1542 (when file
1543 (push (cons id (file-relative-name file)) alist)))))))
1544 alist))))
1546 (defun org-export--get-global-options (&optional backend)
1547 "Return global export options as a plist.
1549 Optional argument BACKEND, if non-nil, is a symbol specifying
1550 which back-end specific export options should also be read in the
1551 process."
1552 (let ((all (append org-export-options-alist
1553 (and backend
1554 (let ((var (intern
1555 (format "org-%s-options-alist" backend))))
1556 (and (boundp var) (symbol-value var))))))
1557 ;; Output value.
1558 plist)
1559 (mapc
1560 (lambda (cell)
1561 (setq plist
1562 (plist-put
1563 plist
1564 (car cell)
1565 ;; Eval default value provided. If keyword is a member
1566 ;; of `org-element-document-properties', parse it as
1567 ;; a secondary string before storing it.
1568 (let ((value (eval (nth 3 cell))))
1569 (if (not (stringp value)) value
1570 (let ((keyword (nth 1 cell)))
1571 (if (not (member keyword org-element-document-properties))
1572 value
1573 (org-element-parse-secondary-string
1574 value (org-element-restriction 'keyword)))))))))
1575 all)
1576 ;; Return value.
1577 plist))
1579 (defvar org-export--allow-BIND-local nil)
1580 (defun org-export--confirm-letbind ()
1581 "Can we use #+BIND values during export?
1582 By default this will ask for confirmation by the user, to divert
1583 possible security risks."
1584 (cond
1585 ((not org-export-allow-BIND) nil)
1586 ((eq org-export-allow-BIND t) t)
1587 ((local-variable-p 'org-export--allow-BIND-local)
1588 org-export--allow-BIND-local)
1589 (t (org-set-local 'org-export--allow-BIND-local
1590 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1592 (defun org-export--install-letbind-maybe ()
1593 "Install the values from #+BIND lines as local variables.
1594 Variables must be installed before in-buffer options are
1595 retrieved."
1596 (let ((case-fold-search t) letbind pair)
1597 (org-with-wide-buffer
1598 (goto-char (point-min))
1599 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1600 (let* ((element (org-element-at-point))
1601 (value (org-element-property :value element)))
1602 (when (and (eq (org-element-type element) 'keyword)
1603 (not (equal value ""))
1604 (org-export--confirm-letbind))
1605 (push (read (format "(%s)" value)) letbind)))))
1606 (dolist (pair (nreverse letbind))
1607 (org-set-local (car pair) (nth 1 pair)))))
1610 ;;;; Tree Properties
1612 ;; Tree properties are information extracted from parse tree. They
1613 ;; are initialized at the beginning of the transcoding process by
1614 ;; `org-export-collect-tree-properties'.
1616 ;; Dedicated functions focus on computing the value of specific tree
1617 ;; properties during initialization. Thus,
1618 ;; `org-export--populate-ignore-list' lists elements and objects that
1619 ;; should be skipped during export, `org-export--get-min-level' gets
1620 ;; the minimal exportable level, used as a basis to compute relative
1621 ;; level for headlines. Eventually
1622 ;; `org-export--collect-headline-numbering' builds an alist between
1623 ;; headlines and their numbering.
1625 (defun org-export-collect-tree-properties (data info)
1626 "Extract tree properties from parse tree.
1628 DATA is the parse tree from which information is retrieved. INFO
1629 is a list holding export options.
1631 Following tree properties are set or updated:
1633 `:exported-data' Hash table used to memoize results from
1634 `org-export-data'.
1636 `:footnote-definition-alist' List of footnotes definitions in
1637 original buffer and current parse tree.
1639 `:headline-offset' Offset between true level of headlines and
1640 local level. An offset of -1 means an headline
1641 of level 2 should be considered as a level
1642 1 headline in the context.
1644 `:headline-numbering' Alist of all headlines as key an the
1645 associated numbering as value.
1647 `:ignore-list' List of elements that should be ignored during
1648 export.
1650 `:target-list' List of all targets in the parse tree.
1652 Return updated plist."
1653 ;; Install the parse tree in the communication channel, in order to
1654 ;; use `org-export-get-genealogy' and al.
1655 (setq info (plist-put info :parse-tree data))
1656 ;; Get the list of elements and objects to ignore, and put it into
1657 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1658 ;; been done during parse tree filtering.
1659 (setq info
1660 (plist-put info
1661 :ignore-list
1662 (append (org-export--populate-ignore-list data info)
1663 (plist-get info :ignore-list))))
1664 ;; Compute `:headline-offset' in order to be able to use
1665 ;; `org-export-get-relative-level'.
1666 (setq info
1667 (plist-put info
1668 :headline-offset
1669 (- 1 (org-export--get-min-level data info))))
1670 ;; Update footnotes definitions list with definitions in parse tree.
1671 ;; This is required since buffer expansion might have modified
1672 ;; boundaries of footnote definitions contained in the parse tree.
1673 ;; This way, definitions in `footnote-definition-alist' are bound to
1674 ;; match those in the parse tree.
1675 (let ((defs (plist-get info :footnote-definition-alist)))
1676 (org-element-map
1677 data 'footnote-definition
1678 (lambda (fn)
1679 (push (cons (org-element-property :label fn)
1680 `(org-data nil ,@(org-element-contents fn)))
1681 defs)))
1682 (setq info (plist-put info :footnote-definition-alist defs)))
1683 ;; Properties order doesn't matter: get the rest of the tree
1684 ;; properties.
1685 (nconc
1686 `(:target-list
1687 ,(org-element-map
1688 data '(keyword target)
1689 (lambda (blob)
1690 (when (or (eq (org-element-type blob) 'target)
1691 (string= (org-element-property :key blob) "TARGET"))
1692 blob)) info)
1693 :headline-numbering ,(org-export--collect-headline-numbering data info)
1694 :exported-data ,(make-hash-table :test 'eq :size 4001))
1695 info))
1697 (defun org-export--get-min-level (data options)
1698 "Return minimum exportable headline's level in DATA.
1699 DATA is parsed tree as returned by `org-element-parse-buffer'.
1700 OPTIONS is a plist holding export options."
1701 (catch 'exit
1702 (let ((min-level 10000))
1703 (mapc
1704 (lambda (blob)
1705 (when (and (eq (org-element-type blob) 'headline)
1706 (not (memq blob (plist-get options :ignore-list))))
1707 (setq min-level
1708 (min (org-element-property :level blob) min-level)))
1709 (when (= min-level 1) (throw 'exit 1)))
1710 (org-element-contents data))
1711 ;; If no headline was found, for the sake of consistency, set
1712 ;; minimum level to 1 nonetheless.
1713 (if (= min-level 10000) 1 min-level))))
1715 (defun org-export--collect-headline-numbering (data options)
1716 "Return numbering of all exportable headlines in a parse tree.
1718 DATA is the parse tree. OPTIONS is the plist holding export
1719 options.
1721 Return an alist whose key is an headline and value is its
1722 associated numbering \(in the shape of a list of numbers\)."
1723 (let ((numbering (make-vector org-export-max-depth 0)))
1724 (org-element-map
1725 data
1726 'headline
1727 (lambda (headline)
1728 (let ((relative-level
1729 (1- (org-export-get-relative-level headline options))))
1730 (cons
1731 headline
1732 (loop for n across numbering
1733 for idx from 0 to org-export-max-depth
1734 when (< idx relative-level) collect n
1735 when (= idx relative-level) collect (aset numbering idx (1+ n))
1736 when (> idx relative-level) do (aset numbering idx 0)))))
1737 options)))
1739 (defun org-export--populate-ignore-list (data options)
1740 "Return list of elements and objects to ignore during export.
1741 DATA is the parse tree to traverse. OPTIONS is the plist holding
1742 export options."
1743 (let* (ignore
1744 walk-data
1745 ;; First find trees containing a select tag, if any.
1746 (selected (org-export--selected-trees data options))
1747 (walk-data
1748 (lambda (data)
1749 ;; Collect ignored elements or objects into IGNORE-LIST.
1750 (let ((type (org-element-type data)))
1751 (if (org-export--skip-p data options selected) (push data ignore)
1752 (if (and (eq type 'headline)
1753 (eq (plist-get options :with-archived-trees) 'headline)
1754 (org-element-property :archivedp data))
1755 ;; If headline is archived but tree below has
1756 ;; to be skipped, add it to ignore list.
1757 (mapc (lambda (e) (push e ignore))
1758 (org-element-contents data))
1759 ;; Move into secondary string, if any.
1760 (let ((sec-prop
1761 (cdr (assq type org-element-secondary-value-alist))))
1762 (when sec-prop
1763 (mapc walk-data (org-element-property sec-prop data))))
1764 ;; Move into recursive objects/elements.
1765 (mapc walk-data (org-element-contents data))))))))
1766 ;; Main call.
1767 (funcall walk-data data)
1768 ;; Return value.
1769 ignore))
1771 (defun org-export--selected-trees (data info)
1772 "Return list of headlines containing a select tag in their tree.
1773 DATA is parsed data as returned by `org-element-parse-buffer'.
1774 INFO is a plist holding export options."
1775 (let* (selected-trees
1776 walk-data ; for byte-compiler.
1777 (walk-data
1778 (function
1779 (lambda (data genealogy)
1780 (case (org-element-type data)
1781 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1782 (org-element-contents data)))
1783 (headline
1784 (let ((tags (org-element-property :tags data)))
1785 (if (loop for tag in (plist-get info :select-tags)
1786 thereis (member tag tags))
1787 ;; When a select tag is found, mark full
1788 ;; genealogy and every headline within the tree
1789 ;; as acceptable.
1790 (setq selected-trees
1791 (append
1792 genealogy
1793 (org-element-map data 'headline 'identity)
1794 selected-trees))
1795 ;; Else, continue searching in tree, recursively.
1796 (mapc
1797 (lambda (el) (funcall walk-data el (cons data genealogy)))
1798 (org-element-contents data))))))))))
1799 (funcall walk-data data nil) selected-trees))
1801 (defun org-export--skip-p (blob options selected)
1802 "Non-nil when element or object BLOB should be skipped during export.
1803 OPTIONS is the plist holding export options. SELECTED, when
1804 non-nil, is a list of headlines belonging to a tree with a select
1805 tag."
1806 (case (org-element-type blob)
1807 (clock (not (plist-get options :with-clocks)))
1808 (drawer
1809 (or (not (plist-get options :with-drawers))
1810 (and (consp (plist-get options :with-drawers))
1811 (not (member (org-element-property :drawer-name blob)
1812 (plist-get options :with-drawers))))))
1813 (headline
1814 (let ((with-tasks (plist-get options :with-tasks))
1815 (todo (org-element-property :todo-keyword blob))
1816 (todo-type (org-element-property :todo-type blob))
1817 (archived (plist-get options :with-archived-trees))
1818 (tags (org-element-property :tags blob)))
1820 ;; Ignore subtrees with an exclude tag.
1821 (loop for k in (plist-get options :exclude-tags)
1822 thereis (member k tags))
1823 ;; When a select tag is present in the buffer, ignore any tree
1824 ;; without it.
1825 (and selected (not (memq blob selected)))
1826 ;; Ignore commented sub-trees.
1827 (org-element-property :commentedp blob)
1828 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1829 (and (not archived) (org-element-property :archivedp blob))
1830 ;; Ignore tasks, if specified by `:with-tasks' property.
1831 (and todo
1832 (or (not with-tasks)
1833 (and (memq with-tasks '(todo done))
1834 (not (eq todo-type with-tasks)))
1835 (and (consp with-tasks) (not (member todo with-tasks))))))))
1836 (inlinetask (not (plist-get options :with-inlinetasks)))
1837 (planning (not (plist-get options :with-plannings)))
1838 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1839 (table-cell
1840 (and (org-export-table-has-special-column-p
1841 (org-export-get-parent-table blob))
1842 (not (org-export-get-previous-element blob options))))
1843 (table-row (org-export-table-row-is-special-p blob options))
1844 (timestamp
1845 (case (plist-get options :with-timestamps)
1846 ;; No timestamp allowed.
1847 ('nil t)
1848 ;; Only active timestamps allowed and the current one isn't
1849 ;; active.
1850 (active
1851 (not (memq (org-element-property :type blob)
1852 '(active active-range))))
1853 ;; Only inactive timestamps allowed and the current one isn't
1854 ;; inactive.
1855 (inactive
1856 (not (memq (org-element-property :type blob)
1857 '(inactive inactive-range))))))))
1861 ;;; The Transcoder
1863 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1864 ;; `org-element-parse-buffer') and transcodes it into a specified
1865 ;; back-end output. It takes care of filtering out elements or
1866 ;; objects according to export options and organizing the output blank
1867 ;; lines and white space are preserved. The function memoizes its
1868 ;; results, so it is cheap to call it within translators.
1870 ;; Internally, three functions handle the filtering of objects and
1871 ;; elements during the export. In particular,
1872 ;; `org-export-ignore-element' marks an element or object so future
1873 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1874 ;; elements or objects should be seen as real Org syntax and
1875 ;; `org-export-expand' transforms the others back into their original
1876 ;; shape
1878 ;; `org-export-transcoder' is an accessor returning appropriate
1879 ;; translator function for a given element or object.
1881 (defun org-export-transcoder (blob info)
1882 "Return appropriate transcoder for BLOB.
1883 INFO is a plist containing export directives."
1884 (let ((type (org-element-type blob)))
1885 ;; Return contents only for complete parse trees.
1886 (if (eq type 'org-data) (lambda (blob contents info) contents)
1887 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1888 (and (functionp transcoder) transcoder)))))
1890 (defun org-export-data (data info)
1891 "Convert DATA into current back-end format.
1893 DATA is a parse tree, an element or an object or a secondary
1894 string. INFO is a plist holding export options.
1896 Return transcoded string."
1897 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1898 (if (not (eq memo 'no-memo)) memo
1899 (let* ((type (org-element-type data))
1900 (results
1901 (cond
1902 ;; Ignored element/object.
1903 ((memq data (plist-get info :ignore-list)) nil)
1904 ;; Plain text.
1905 ((eq type 'plain-text)
1906 (org-export-filter-apply-functions
1907 (plist-get info :filter-plain-text)
1908 (let ((transcoder (org-export-transcoder data info)))
1909 (if transcoder (funcall transcoder data info) data))
1910 info))
1911 ;; Uninterpreted element/object: change it back to Org
1912 ;; syntax and export again resulting raw string.
1913 ((not (org-export--interpret-p data info))
1914 (org-export-data
1915 (org-export-expand
1916 data
1917 (mapconcat (lambda (blob) (org-export-data blob info))
1918 (org-element-contents data)
1919 ""))
1920 info))
1921 ;; Secondary string.
1922 ((not type)
1923 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1924 ;; Element/Object without contents or, as a special case,
1925 ;; headline with archive tag and archived trees restricted
1926 ;; to title only.
1927 ((or (not (org-element-contents data))
1928 (and (eq type 'headline)
1929 (eq (plist-get info :with-archived-trees) 'headline)
1930 (org-element-property :archivedp data)))
1931 (let ((transcoder (org-export-transcoder data info)))
1932 (and (functionp transcoder)
1933 (funcall transcoder data nil info))))
1934 ;; Element/Object with contents.
1936 (let ((transcoder (org-export-transcoder data info)))
1937 (when transcoder
1938 (let* ((greaterp (memq type org-element-greater-elements))
1939 (objectp
1940 (and (not greaterp)
1941 (memq type org-element-recursive-objects)))
1942 (contents
1943 (mapconcat
1944 (lambda (element) (org-export-data element info))
1945 (org-element-contents
1946 (if (or greaterp objectp) data
1947 ;; Elements directly containing objects
1948 ;; must have their indentation normalized
1949 ;; first.
1950 (org-element-normalize-contents
1951 data
1952 ;; When normalizing contents of the first
1953 ;; paragraph in an item or a footnote
1954 ;; definition, ignore first line's
1955 ;; indentation: there is none and it
1956 ;; might be misleading.
1957 (when (eq type 'paragraph)
1958 (let ((parent (org-export-get-parent data)))
1959 (and
1960 (eq (car (org-element-contents parent))
1961 data)
1962 (memq (org-element-type parent)
1963 '(footnote-definition item))))))))
1964 "")))
1965 (funcall transcoder data
1966 (if (not greaterp) contents
1967 (org-element-normalize-string contents))
1968 info))))))))
1969 ;; Final result will be memoized before being returned.
1970 (puthash
1971 data
1972 (cond
1973 ((not results) nil)
1974 ((memq type '(org-data plain-text nil)) results)
1975 ;; Append the same white space between elements or objects as in
1976 ;; the original buffer, and call appropriate filters.
1978 (let ((results
1979 (org-export-filter-apply-functions
1980 (plist-get info (intern (format ":filter-%s" type)))
1981 (let ((post-blank (or (org-element-property :post-blank data)
1982 0)))
1983 (if (memq type org-element-all-elements)
1984 (concat (org-element-normalize-string results)
1985 (make-string post-blank ?\n))
1986 (concat results (make-string post-blank ? ))))
1987 info)))
1988 results)))
1989 (plist-get info :exported-data))))))
1991 (defun org-export--interpret-p (blob info)
1992 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1993 Check is done according to export options INFO, stored as
1994 a plist."
1995 (case (org-element-type blob)
1996 ;; ... entities...
1997 (entity (plist-get info :with-entities))
1998 ;; ... emphasis...
1999 (emphasis (plist-get info :with-emphasize))
2000 ;; ... fixed-width areas.
2001 (fixed-width (plist-get info :with-fixed-width))
2002 ;; ... footnotes...
2003 ((footnote-definition footnote-reference)
2004 (plist-get info :with-footnotes))
2005 ;; ... sub/superscripts...
2006 ((subscript superscript)
2007 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2008 (if (eq sub/super-p '{})
2009 (org-element-property :use-brackets-p blob)
2010 sub/super-p)))
2011 ;; ... tables...
2012 (table (plist-get info :with-tables))
2013 (otherwise t)))
2015 (defun org-export-expand (blob contents)
2016 "Expand a parsed element or object to its original state.
2017 BLOB is either an element or an object. CONTENTS is its
2018 contents, as a string or nil."
2019 (funcall
2020 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2021 blob contents))
2023 (defun org-export-ignore-element (element info)
2024 "Add ELEMENT to `:ignore-list' in INFO.
2026 Any element in `:ignore-list' will be skipped when using
2027 `org-element-map'. INFO is modified by side effects."
2028 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2032 ;;; The Filter System
2034 ;; Filters allow end-users to tweak easily the transcoded output.
2035 ;; They are the functional counterpart of hooks, as every filter in
2036 ;; a set is applied to the return value of the previous one.
2038 ;; Every set is back-end agnostic. Although, a filter is always
2039 ;; called, in addition to the string it applies to, with the back-end
2040 ;; used as argument, so it's easy for the end-user to add back-end
2041 ;; specific filters in the set. The communication channel, as
2042 ;; a plist, is required as the third argument.
2044 ;; From the developer side, filters sets can be installed in the
2045 ;; process with the help of `org-export-define-backend', which
2046 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2047 ;; association has a key among the following symbols and a function or
2048 ;; a list of functions as value.
2050 ;; - `:filter-parse-tree' applies directly on the complete parsed
2051 ;; tree. It's the only filters set that doesn't apply to a string.
2052 ;; Users can set it through `org-export-filter-parse-tree-functions'
2053 ;; variable.
2055 ;; - `:filter-final-output' applies to the final transcoded string.
2056 ;; Users can set it with `org-export-filter-final-output-functions'
2057 ;; variable
2059 ;; - `:filter-plain-text' applies to any string not recognized as Org
2060 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2061 ;; configure it.
2063 ;; - `:filter-TYPE' applies on the string returned after an element or
2064 ;; object of type TYPE has been transcoded. An user can modify
2065 ;; `org-export-filter-TYPE-functions'
2067 ;; All filters sets are applied with
2068 ;; `org-export-filter-apply-functions' function. Filters in a set are
2069 ;; applied in a LIFO fashion. It allows developers to be sure that
2070 ;; their filters will be applied first.
2072 ;; Filters properties are installed in communication channel with
2073 ;; `org-export-install-filters' function.
2075 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2076 ;; before parsing to allow for heavy structure modifications.
2079 ;;;; Before Parsing Hook
2081 (defvar org-export-before-parsing-hook nil
2082 "Hook run before parsing an export buffer.
2084 This is run after include keywords have been expanded and Babel
2085 code executed, on a copy of original buffer's area being
2086 exported. Visibility is the same as in the original one. Point
2087 is left at the beginning of the new one.
2089 Every function in this hook will be called with one argument: the
2090 back-end currently used, as a symbol.")
2093 ;;;; Special Filters
2095 (defvar org-export-filter-parse-tree-functions nil
2096 "List of functions applied to the parsed tree.
2097 Each filter is called with three arguments: the parse tree, as
2098 returned by `org-element-parse-buffer', the back-end, as
2099 a symbol, and the communication channel, as a plist. It must
2100 return the modified parse tree to transcode.")
2102 (defvar org-export-filter-final-output-functions nil
2103 "List of functions applied to the transcoded string.
2104 Each filter is called with three arguments: the full transcoded
2105 string, the back-end, as a symbol, and the communication channel,
2106 as a plist. It must return a string that will be used as the
2107 final export output.")
2109 (defvar org-export-filter-plain-text-functions nil
2110 "List of functions applied to plain text.
2111 Each filter is called with three arguments: a string which
2112 contains no Org syntax, the back-end, as a symbol, and the
2113 communication channel, as a plist. It must return a string or
2114 nil.")
2117 ;;;; Elements Filters
2119 (defvar org-export-filter-babel-call-functions nil
2120 "List of functions applied to a transcoded babel-call.
2121 Each filter is called with three arguments: the transcoded data,
2122 as a string, the back-end, as a symbol, and the communication
2123 channel, as a plist. It must return a string or nil.")
2125 (defvar org-export-filter-center-block-functions nil
2126 "List of functions applied to a transcoded center block.
2127 Each filter is called with three arguments: the transcoded data,
2128 as a string, the back-end, as a symbol, and the communication
2129 channel, as a plist. It must return a string or nil.")
2131 (defvar org-export-filter-clock-functions nil
2132 "List of functions applied to a transcoded clock.
2133 Each filter is called with three arguments: the transcoded data,
2134 as a string, the back-end, as a symbol, and the communication
2135 channel, as a plist. It must return a string or nil.")
2137 (defvar org-export-filter-comment-functions nil
2138 "List of functions applied to a transcoded comment.
2139 Each filter is called with three arguments: the transcoded data,
2140 as a string, the back-end, as a symbol, and the communication
2141 channel, as a plist. It must return a string or nil.")
2143 (defvar org-export-filter-comment-block-functions nil
2144 "List of functions applied to a transcoded comment-comment.
2145 Each filter is called with three arguments: the transcoded data,
2146 as a string, the back-end, as a symbol, and the communication
2147 channel, as a plist. It must return a string or nil.")
2149 (defvar org-export-filter-drawer-functions nil
2150 "List of functions applied to a transcoded drawer.
2151 Each filter is called with three arguments: the transcoded data,
2152 as a string, the back-end, as a symbol, and the communication
2153 channel, as a plist. It must return a string or nil.")
2155 (defvar org-export-filter-dynamic-block-functions nil
2156 "List of functions applied to a transcoded dynamic-block.
2157 Each filter is called with three arguments: the transcoded data,
2158 as a string, the back-end, as a symbol, and the communication
2159 channel, as a plist. It must return a string or nil.")
2161 (defvar org-export-filter-example-block-functions nil
2162 "List of functions applied to a transcoded example-block.
2163 Each filter is called with three arguments: the transcoded data,
2164 as a string, the back-end, as a symbol, and the communication
2165 channel, as a plist. It must return a string or nil.")
2167 (defvar org-export-filter-export-block-functions nil
2168 "List of functions applied to a transcoded export-block.
2169 Each filter is called with three arguments: the transcoded data,
2170 as a string, the back-end, as a symbol, and the communication
2171 channel, as a plist. It must return a string or nil.")
2173 (defvar org-export-filter-fixed-width-functions nil
2174 "List of functions applied to a transcoded fixed-width.
2175 Each filter is called with three arguments: the transcoded data,
2176 as a string, the back-end, as a symbol, and the communication
2177 channel, as a plist. It must return a string or nil.")
2179 (defvar org-export-filter-footnote-definition-functions nil
2180 "List of functions applied to a transcoded footnote-definition.
2181 Each filter is called with three arguments: the transcoded data,
2182 as a string, the back-end, as a symbol, and the communication
2183 channel, as a plist. It must return a string or nil.")
2185 (defvar org-export-filter-headline-functions nil
2186 "List of functions applied to a transcoded headline.
2187 Each filter is called with three arguments: the transcoded data,
2188 as a string, the back-end, as a symbol, and the communication
2189 channel, as a plist. It must return a string or nil.")
2191 (defvar org-export-filter-horizontal-rule-functions nil
2192 "List of functions applied to a transcoded horizontal-rule.
2193 Each filter is called with three arguments: the transcoded data,
2194 as a string, the back-end, as a symbol, and the communication
2195 channel, as a plist. It must return a string or nil.")
2197 (defvar org-export-filter-inlinetask-functions nil
2198 "List of functions applied to a transcoded inlinetask.
2199 Each filter is called with three arguments: the transcoded data,
2200 as a string, the back-end, as a symbol, and the communication
2201 channel, as a plist. It must return a string or nil.")
2203 (defvar org-export-filter-item-functions nil
2204 "List of functions applied to a transcoded item.
2205 Each filter is called with three arguments: the transcoded data,
2206 as a string, the back-end, as a symbol, and the communication
2207 channel, as a plist. It must return a string or nil.")
2209 (defvar org-export-filter-keyword-functions nil
2210 "List of functions applied to a transcoded keyword.
2211 Each filter is called with three arguments: the transcoded data,
2212 as a string, the back-end, as a symbol, and the communication
2213 channel, as a plist. It must return a string or nil.")
2215 (defvar org-export-filter-latex-environment-functions nil
2216 "List of functions applied to a transcoded latex-environment.
2217 Each filter is called with three arguments: the transcoded data,
2218 as a string, the back-end, as a symbol, and the communication
2219 channel, as a plist. It must return a string or nil.")
2221 (defvar org-export-filter-node-property-functions nil
2222 "List of functions applied to a transcoded node-property.
2223 Each filter is called with three arguments: the transcoded data,
2224 as a string, the back-end, as a symbol, and the communication
2225 channel, as a plist. It must return a string or nil.")
2227 (defvar org-export-filter-paragraph-functions nil
2228 "List of functions applied to a transcoded paragraph.
2229 Each filter is called with three arguments: the transcoded data,
2230 as a string, the back-end, as a symbol, and the communication
2231 channel, as a plist. It must return a string or nil.")
2233 (defvar org-export-filter-plain-list-functions nil
2234 "List of functions applied to a transcoded plain-list.
2235 Each filter is called with three arguments: the transcoded data,
2236 as a string, the back-end, as a symbol, and the communication
2237 channel, as a plist. It must return a string or nil.")
2239 (defvar org-export-filter-planning-functions nil
2240 "List of functions applied to a transcoded planning.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2245 (defvar org-export-filter-property-drawer-functions nil
2246 "List of functions applied to a transcoded property-drawer.
2247 Each filter is called with three arguments: the transcoded data,
2248 as a string, the back-end, as a symbol, and the communication
2249 channel, as a plist. It must return a string or nil.")
2251 (defvar org-export-filter-quote-block-functions nil
2252 "List of functions applied to a transcoded quote block.
2253 Each filter is called with three arguments: the transcoded quote
2254 data, as a string, the back-end, as a symbol, and the
2255 communication channel, as a plist. It must return a string or
2256 nil.")
2258 (defvar org-export-filter-quote-section-functions nil
2259 "List of functions applied to a transcoded quote-section.
2260 Each filter is called with three arguments: the transcoded data,
2261 as a string, the back-end, as a symbol, and the communication
2262 channel, as a plist. It must return a string or nil.")
2264 (defvar org-export-filter-section-functions nil
2265 "List of functions applied to a transcoded section.
2266 Each filter is called with three arguments: the transcoded data,
2267 as a string, the back-end, as a symbol, and the communication
2268 channel, as a plist. It must return a string or nil.")
2270 (defvar org-export-filter-special-block-functions nil
2271 "List of functions applied to a transcoded special block.
2272 Each filter is called with three arguments: the transcoded data,
2273 as a string, the back-end, as a symbol, and the communication
2274 channel, as a plist. It must return a string or nil.")
2276 (defvar org-export-filter-src-block-functions nil
2277 "List of functions applied to a transcoded src-block.
2278 Each filter is called with three arguments: the transcoded data,
2279 as a string, the back-end, as a symbol, and the communication
2280 channel, as a plist. It must return a string or nil.")
2282 (defvar org-export-filter-table-functions nil
2283 "List of functions applied to a transcoded table.
2284 Each filter is called with three arguments: the transcoded data,
2285 as a string, the back-end, as a symbol, and the communication
2286 channel, as a plist. It must return a string or nil.")
2288 (defvar org-export-filter-table-cell-functions nil
2289 "List of functions applied to a transcoded table-cell.
2290 Each filter is called with three arguments: the transcoded data,
2291 as a string, the back-end, as a symbol, and the communication
2292 channel, as a plist. It must return a string or nil.")
2294 (defvar org-export-filter-table-row-functions nil
2295 "List of functions applied to a transcoded table-row.
2296 Each filter is called with three arguments: the transcoded data,
2297 as a string, the back-end, as a symbol, and the communication
2298 channel, as a plist. It must return a string or nil.")
2300 (defvar org-export-filter-verse-block-functions nil
2301 "List of functions applied to a transcoded verse block.
2302 Each filter is called with three arguments: the transcoded data,
2303 as a string, the back-end, as a symbol, and the communication
2304 channel, as a plist. It must return a string or nil.")
2307 ;;;; Objects Filters
2309 (defvar org-export-filter-bold-functions nil
2310 "List of functions applied to transcoded bold text.
2311 Each filter is called with three arguments: the transcoded data,
2312 as a string, the back-end, as a symbol, and the communication
2313 channel, as a plist. It must return a string or nil.")
2315 (defvar org-export-filter-code-functions nil
2316 "List of functions applied to transcoded code text.
2317 Each filter is called with three arguments: the transcoded data,
2318 as a string, the back-end, as a symbol, and the communication
2319 channel, as a plist. It must return a string or nil.")
2321 (defvar org-export-filter-entity-functions nil
2322 "List of functions applied to a transcoded entity.
2323 Each filter is called with three arguments: the transcoded data,
2324 as a string, the back-end, as a symbol, and the communication
2325 channel, as a plist. It must return a string or nil.")
2327 (defvar org-export-filter-export-snippet-functions nil
2328 "List of functions applied to a transcoded export-snippet.
2329 Each filter is called with three arguments: the transcoded data,
2330 as a string, the back-end, as a symbol, and the communication
2331 channel, as a plist. It must return a string or nil.")
2333 (defvar org-export-filter-footnote-reference-functions nil
2334 "List of functions applied to a transcoded footnote-reference.
2335 Each filter is called with three arguments: the transcoded data,
2336 as a string, the back-end, as a symbol, and the communication
2337 channel, as a plist. It must return a string or nil.")
2339 (defvar org-export-filter-inline-babel-call-functions nil
2340 "List of functions applied to a transcoded inline-babel-call.
2341 Each filter is called with three arguments: the transcoded data,
2342 as a string, the back-end, as a symbol, and the communication
2343 channel, as a plist. It must return a string or nil.")
2345 (defvar org-export-filter-inline-src-block-functions nil
2346 "List of functions applied to a transcoded inline-src-block.
2347 Each filter is called with three arguments: the transcoded data,
2348 as a string, the back-end, as a symbol, and the communication
2349 channel, as a plist. It must return a string or nil.")
2351 (defvar org-export-filter-italic-functions nil
2352 "List of functions applied to transcoded italic text.
2353 Each filter is called with three arguments: the transcoded data,
2354 as a string, the back-end, as a symbol, and the communication
2355 channel, as a plist. It must return a string or nil.")
2357 (defvar org-export-filter-latex-fragment-functions nil
2358 "List of functions applied to a transcoded latex-fragment.
2359 Each filter is called with three arguments: the transcoded data,
2360 as a string, the back-end, as a symbol, and the communication
2361 channel, as a plist. It must return a string or nil.")
2363 (defvar org-export-filter-line-break-functions nil
2364 "List of functions applied to a transcoded line-break.
2365 Each filter is called with three arguments: the transcoded data,
2366 as a string, the back-end, as a symbol, and the communication
2367 channel, as a plist. It must return a string or nil.")
2369 (defvar org-export-filter-link-functions nil
2370 "List of functions applied to a transcoded link.
2371 Each filter is called with three arguments: the transcoded data,
2372 as a string, the back-end, as a symbol, and the communication
2373 channel, as a plist. It must return a string or nil.")
2375 (defvar org-export-filter-macro-functions nil
2376 "List of functions applied to a transcoded macro.
2377 Each filter is called with three arguments: the transcoded data,
2378 as a string, the back-end, as a symbol, and the communication
2379 channel, as a plist. It must return a string or nil.")
2381 (defvar org-export-filter-radio-target-functions nil
2382 "List of functions applied to a transcoded radio-target.
2383 Each filter is called with three arguments: the transcoded data,
2384 as a string, the back-end, as a symbol, and the communication
2385 channel, as a plist. It must return a string or nil.")
2387 (defvar org-export-filter-statistics-cookie-functions nil
2388 "List of functions applied to a transcoded statistics-cookie.
2389 Each filter is called with three arguments: the transcoded data,
2390 as a string, the back-end, as a symbol, and the communication
2391 channel, as a plist. It must return a string or nil.")
2393 (defvar org-export-filter-strike-through-functions nil
2394 "List of functions applied to transcoded strike-through text.
2395 Each filter is called with three arguments: the transcoded data,
2396 as a string, the back-end, as a symbol, and the communication
2397 channel, as a plist. It must return a string or nil.")
2399 (defvar org-export-filter-subscript-functions nil
2400 "List of functions applied to a transcoded subscript.
2401 Each filter is called with three arguments: the transcoded data,
2402 as a string, the back-end, as a symbol, and the communication
2403 channel, as a plist. It must return a string or nil.")
2405 (defvar org-export-filter-superscript-functions nil
2406 "List of functions applied to a transcoded superscript.
2407 Each filter is called with three arguments: the transcoded data,
2408 as a string, the back-end, as a symbol, and the communication
2409 channel, as a plist. It must return a string or nil.")
2411 (defvar org-export-filter-target-functions nil
2412 "List of functions applied to a transcoded target.
2413 Each filter is called with three arguments: the transcoded data,
2414 as a string, the back-end, as a symbol, and the communication
2415 channel, as a plist. It must return a string or nil.")
2417 (defvar org-export-filter-timestamp-functions nil
2418 "List of functions applied to a transcoded timestamp.
2419 Each filter is called with three arguments: the transcoded data,
2420 as a string, the back-end, as a symbol, and the communication
2421 channel, as a plist. It must return a string or nil.")
2423 (defvar org-export-filter-underline-functions nil
2424 "List of functions applied to transcoded underline text.
2425 Each filter is called with three arguments: the transcoded data,
2426 as a string, the back-end, as a symbol, and the communication
2427 channel, as a plist. It must return a string or nil.")
2429 (defvar org-export-filter-verbatim-functions nil
2430 "List of functions applied to transcoded verbatim text.
2431 Each filter is called with three arguments: the transcoded data,
2432 as a string, the back-end, as a symbol, and the communication
2433 channel, as a plist. It must return a string or nil.")
2436 ;;;; Filters Tools
2438 ;; Internal function `org-export-install-filters' installs filters
2439 ;; hard-coded in back-ends (developer filters) and filters from global
2440 ;; variables (user filters) in the communication channel.
2442 ;; Internal function `org-export-filter-apply-functions' takes care
2443 ;; about applying each filter in order to a given data. It ignores
2444 ;; filters returning a nil value but stops whenever a filter returns
2445 ;; an empty string.
2447 (defun org-export-filter-apply-functions (filters value info)
2448 "Call every function in FILTERS.
2450 Functions are called with arguments VALUE, current export
2451 back-end and INFO. A function returning a nil value will be
2452 skipped. If it returns the empty string, the process ends and
2453 VALUE is ignored.
2455 Call is done in a LIFO fashion, to be sure that developer
2456 specified filters, if any, are called first."
2457 (catch 'exit
2458 (dolist (filter filters value)
2459 (let ((result (funcall filter value (plist-get info :back-end) info)))
2460 (cond ((not result) value)
2461 ((equal value "") (throw 'exit nil))
2462 (t (setq value result)))))))
2464 (defun org-export-install-filters (info)
2465 "Install filters properties in communication channel.
2467 INFO is a plist containing the current communication channel.
2469 Return the updated communication channel."
2470 (let (plist)
2471 ;; Install user defined filters with `org-export-filters-alist'.
2472 (mapc (lambda (p)
2473 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2474 org-export-filters-alist)
2475 ;; Prepend back-end specific filters to that list.
2476 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2477 (plist-get info :back-end)))))
2478 (when (boundp back-end-filters)
2479 (mapc (lambda (p)
2480 ;; Single values get consed, lists are prepended.
2481 (let ((key (car p)) (value (cdr p)))
2482 (when value
2483 (setq plist
2484 (plist-put
2485 plist key
2486 (if (atom value) (cons value (plist-get plist key))
2487 (append value (plist-get plist key))))))))
2488 (eval back-end-filters))))
2489 ;; Return new communication channel.
2490 (org-combine-plists info plist)))
2494 ;;; Core functions
2496 ;; This is the room for the main function, `org-export-as', along with
2497 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2498 ;; They differ only by the way they output the resulting code.
2500 ;; `org-export-output-file-name' is an auxiliary function meant to be
2501 ;; used with `org-export-to-file'. With a given extension, it tries
2502 ;; to provide a canonical file name to write export output to.
2504 ;; Note that `org-export-as' doesn't really parse the current buffer,
2505 ;; but a copy of it (with the same buffer-local variables and
2506 ;; visibility), where macros and include keywords are expanded and
2507 ;; Babel blocks are executed, if appropriate.
2508 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2510 ;; File inclusion is taken care of by
2511 ;; `org-export-expand-include-keyword' and
2512 ;; `org-export--prepare-file-contents'. Structure wise, including
2513 ;; a whole Org file in a buffer often makes little sense. For
2514 ;; example, if the file contains an headline and the include keyword
2515 ;; was within an item, the item should contain the headline. That's
2516 ;; why file inclusion should be done before any structure can be
2517 ;; associated to the file, that is before parsing.
2519 (defun org-export-as
2520 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2521 "Transcode current Org buffer into BACKEND code.
2523 If narrowing is active in the current buffer, only transcode its
2524 narrowed part.
2526 If a region is active, transcode that region.
2528 When optional argument SUBTREEP is non-nil, transcode the
2529 sub-tree at point, extracting information from the headline
2530 properties first.
2532 When optional argument VISIBLE-ONLY is non-nil, don't export
2533 contents of hidden elements.
2535 When optional argument BODY-ONLY is non-nil, only return body
2536 code, without preamble nor postamble.
2538 Optional argument EXT-PLIST, when provided, is a property list
2539 with external parameters overriding Org default settings, but
2540 still inferior to file-local settings.
2542 Optional argument NOEXPAND, when non-nil, prevents included files
2543 to be expanded and Babel code to be executed.
2545 Return code as a string."
2546 ;; Barf if BACKEND isn't registered.
2547 (org-export-barf-if-invalid-backend backend)
2548 (save-excursion
2549 (save-restriction
2550 ;; Narrow buffer to an appropriate region or subtree for
2551 ;; parsing. If parsing subtree, be sure to remove main headline
2552 ;; too.
2553 (cond ((org-region-active-p)
2554 (narrow-to-region (region-beginning) (region-end)))
2555 (subtreep
2556 (org-narrow-to-subtree)
2557 (goto-char (point-min))
2558 (forward-line)
2559 (narrow-to-region (point) (point-max))))
2560 ;; 1. Get export environment from original buffer. Also install
2561 ;; user's and developer's filters.
2562 (let ((info (org-export-install-filters
2563 (org-export-get-environment backend subtreep ext-plist)))
2564 ;; 2. Get parse tree. Buffer isn't parsed directly.
2565 ;; Instead, a temporary copy is created, where macros
2566 ;; and include keywords are expanded and code blocks
2567 ;; are evaluated.
2568 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2569 (current-buffer))))
2570 (org-export-with-current-buffer-copy
2571 (unless noexpand
2572 (org-macro-replace-all)
2573 (org-export-expand-include-keyword)
2574 ;; TODO: Setting `org-current-export-file' is
2575 ;; required by Org Babel to properly resolve
2576 ;; noweb references. Once "org-exp.el" is
2577 ;; removed, modify
2578 ;; `org-export-blocks-preprocess' so it accepts
2579 ;; the value as an argument instead.
2580 (let ((org-current-export-file buf))
2581 (org-export-blocks-preprocess)))
2582 (goto-char (point-min))
2583 ;; Run hook
2584 ;; `org-export-before-parsing-hook'. with current
2585 ;; back-end as argument.
2586 (run-hook-with-args
2587 'org-export-before-parsing-hook backend)
2588 ;; Eventually parse buffer.
2589 (org-element-parse-buffer nil visible-only)))))
2590 ;; 3. Call parse-tree filters to get the final tree.
2591 (setq tree
2592 (org-export-filter-apply-functions
2593 (plist-get info :filter-parse-tree) tree info))
2594 ;; 4. Now tree is complete, compute its properties and add
2595 ;; them to communication channel.
2596 (setq info
2597 (org-combine-plists
2598 info (org-export-collect-tree-properties tree info)))
2599 ;; 5. Eventually transcode TREE. Wrap the resulting string
2600 ;; into a template, if required. Eventually call
2601 ;; final-output filter.
2602 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2603 (template (cdr (assq 'template
2604 (plist-get info :translate-alist))))
2605 (output (org-export-filter-apply-functions
2606 (plist-get info :filter-final-output)
2607 (if (or (not (functionp template)) body-only) body
2608 (funcall template body info))
2609 info)))
2610 ;; Maybe add final OUTPUT to kill ring, then return it.
2611 (when org-export-copy-to-kill-ring (org-kill-new output))
2612 output)))))
2614 (defun org-export-to-buffer
2615 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2616 "Call `org-export-as' with output to a specified buffer.
2618 BACKEND is the back-end used for transcoding, as a symbol.
2620 BUFFER is the output buffer. If it already exists, it will be
2621 erased first, otherwise, it will be created.
2623 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2624 and NOEXPAND are similar to those used in `org-export-as', which
2625 see.
2627 Return buffer."
2628 (let ((out (org-export-as
2629 backend subtreep visible-only body-only ext-plist noexpand))
2630 (buffer (get-buffer-create buffer)))
2631 (with-current-buffer buffer
2632 (erase-buffer)
2633 (insert out)
2634 (goto-char (point-min)))
2635 buffer))
2637 (defun org-export-to-file
2638 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2639 "Call `org-export-as' with output to a specified file.
2641 BACKEND is the back-end used for transcoding, as a symbol. FILE
2642 is the name of the output file, as a string.
2644 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2645 and NOEXPAND are similar to those used in `org-export-as', which
2646 see.
2648 Return output file's name."
2649 ;; Checks for FILE permissions. `write-file' would do the same, but
2650 ;; we'd rather avoid needless transcoding of parse tree.
2651 (unless (file-writable-p file) (error "Output file not writable"))
2652 ;; Insert contents to a temporary buffer and write it to FILE.
2653 (let ((out (org-export-as
2654 backend subtreep visible-only body-only ext-plist noexpand)))
2655 (with-temp-buffer
2656 (insert out)
2657 (let ((coding-system-for-write org-export-coding-system))
2658 (write-file file))))
2659 ;; Return full path.
2660 file)
2662 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2663 "Return output file's name according to buffer specifications.
2665 EXTENSION is a string representing the output file extension,
2666 with the leading dot.
2668 With a non-nil optional argument SUBTREEP, try to determine
2669 output file's name by looking for \"EXPORT_FILE_NAME\" property
2670 of subtree at point.
2672 When optional argument PUB-DIR is set, use it as the publishing
2673 directory.
2675 When optional argument VISIBLE-ONLY is non-nil, don't export
2676 contents of hidden elements.
2678 Return file name as a string, or nil if it couldn't be
2679 determined."
2680 (let ((base-name
2681 ;; File name may come from EXPORT_FILE_NAME subtree property,
2682 ;; assuming point is at beginning of said sub-tree.
2683 (file-name-sans-extension
2684 (or (and subtreep
2685 (org-entry-get
2686 (save-excursion
2687 (ignore-errors (org-back-to-heading) (point)))
2688 "EXPORT_FILE_NAME" t))
2689 ;; File name may be extracted from buffer's associated
2690 ;; file, if any.
2691 (buffer-file-name (buffer-base-buffer))
2692 ;; Can't determine file name on our own: Ask user.
2693 (let ((read-file-name-function
2694 (and org-completion-use-ido 'ido-read-file-name)))
2695 (read-file-name
2696 "Output file: " pub-dir nil nil nil
2697 (lambda (name)
2698 (string= (file-name-extension name t) extension))))))))
2699 ;; Build file name. Enforce EXTENSION over whatever user may have
2700 ;; come up with. PUB-DIR, if defined, always has precedence over
2701 ;; any provided path.
2702 (cond
2703 (pub-dir
2704 (concat (file-name-as-directory pub-dir)
2705 (file-name-nondirectory base-name)
2706 extension))
2707 ((string= (file-name-nondirectory base-name) base-name)
2708 (concat (file-name-as-directory ".") base-name extension))
2709 (t (concat base-name extension)))))
2711 (defmacro org-export-with-current-buffer-copy (&rest body)
2712 "Apply BODY in a copy of the current buffer.
2714 The copy preserves local variables and visibility of the original
2715 buffer.
2717 Point is at buffer's beginning when BODY is applied."
2718 (org-with-gensyms (original-buffer offset buffer-string overlays)
2719 `(let ((,original-buffer (current-buffer))
2720 (,offset (1- (point-min)))
2721 (,buffer-string (buffer-string))
2722 (,overlays (mapcar
2723 'copy-overlay (overlays-in (point-min) (point-max)))))
2724 (with-temp-buffer
2725 (let ((buffer-invisibility-spec nil))
2726 (org-clone-local-variables
2727 ,original-buffer
2728 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2729 (insert ,buffer-string)
2730 (mapc (lambda (ov)
2731 (move-overlay
2733 (- (overlay-start ov) ,offset)
2734 (- (overlay-end ov) ,offset)
2735 (current-buffer)))
2736 ,overlays)
2737 (goto-char (point-min))
2738 (progn ,@body))))))
2739 (def-edebug-spec org-export-with-current-buffer-copy (body))
2741 (defun org-export-expand-include-keyword (&optional included dir)
2742 "Expand every include keyword in buffer.
2743 Optional argument INCLUDED is a list of included file names along
2744 with their line restriction, when appropriate. It is used to
2745 avoid infinite recursion. Optional argument DIR is the current
2746 working directory. It is used to properly resolve relative
2747 paths."
2748 (let ((case-fold-search t))
2749 (goto-char (point-min))
2750 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2751 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2752 'keyword)
2753 (beginning-of-line)
2754 ;; Extract arguments from keyword's value.
2755 (let* ((value (match-string 1))
2756 (ind (org-get-indentation))
2757 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2758 (prog1 (expand-file-name (match-string 1 value) dir)
2759 (setq value (replace-match "" nil nil value)))))
2760 (lines
2761 (and (string-match
2762 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2763 (prog1 (match-string 1 value)
2764 (setq value (replace-match "" nil nil value)))))
2765 (env (cond ((string-match "\\<example\\>" value) 'example)
2766 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2767 (match-string 1 value))))
2768 ;; Minimal level of included file defaults to the child
2769 ;; level of the current headline, if any, or one. It
2770 ;; only applies is the file is meant to be included as
2771 ;; an Org one.
2772 (minlevel
2773 (and (not env)
2774 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2775 (prog1 (string-to-number (match-string 1 value))
2776 (setq value (replace-match "" nil nil value)))
2777 (let ((cur (org-current-level)))
2778 (if cur (1+ (org-reduced-level cur)) 1))))))
2779 ;; Remove keyword.
2780 (delete-region (point) (progn (forward-line) (point)))
2781 (cond
2782 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2783 ;; Check if files has already been parsed. Look after
2784 ;; inclusion lines too, as different parts of the same file
2785 ;; can be included too.
2786 ((member (list file lines) included)
2787 (error "Recursive file inclusion: %s" file))
2789 (cond
2790 ((eq env 'example)
2791 (insert
2792 (let ((ind-str (make-string ind ? ))
2793 (contents
2794 ;; Protect sensitive contents with commas.
2795 (replace-regexp-in-string
2796 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2797 (org-export--prepare-file-contents file lines)
2798 nil nil 1)))
2799 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2800 ind-str contents ind-str))))
2801 ((stringp env)
2802 (insert
2803 (let ((ind-str (make-string ind ? ))
2804 (contents
2805 ;; Protect sensitive contents with commas.
2806 (replace-regexp-in-string
2807 (if (string= env "org") "\\(^\\)\\(.\\)"
2808 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2809 (org-export--prepare-file-contents file lines)
2810 nil nil 1)))
2811 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2812 ind-str env contents ind-str))))
2814 (insert
2815 (with-temp-buffer
2816 (org-mode)
2817 (insert
2818 (org-export--prepare-file-contents file lines ind minlevel))
2819 (org-export-expand-include-keyword
2820 (cons (list file lines) included)
2821 (file-name-directory file))
2822 (buffer-string))))))))))))
2824 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2825 "Prepare the contents of FILE for inclusion and return them as a string.
2827 When optional argument LINES is a string specifying a range of
2828 lines, include only those lines.
2830 Optional argument IND, when non-nil, is an integer specifying the
2831 global indentation of returned contents. Since its purpose is to
2832 allow an included file to stay in the same environment it was
2833 created \(i.e. a list item), it doesn't apply past the first
2834 headline encountered.
2836 Optional argument MINLEVEL, when non-nil, is an integer
2837 specifying the level that any top-level headline in the included
2838 file should have."
2839 (with-temp-buffer
2840 (insert-file-contents file)
2841 (when lines
2842 (let* ((lines (split-string lines "-"))
2843 (lbeg (string-to-number (car lines)))
2844 (lend (string-to-number (cadr lines)))
2845 (beg (if (zerop lbeg) (point-min)
2846 (goto-char (point-min))
2847 (forward-line (1- lbeg))
2848 (point)))
2849 (end (if (zerop lend) (point-max)
2850 (goto-char (point-min))
2851 (forward-line (1- lend))
2852 (point))))
2853 (narrow-to-region beg end)))
2854 ;; Remove blank lines at beginning and end of contents. The logic
2855 ;; behind that removal is that blank lines around include keyword
2856 ;; override blank lines in included file.
2857 (goto-char (point-min))
2858 (org-skip-whitespace)
2859 (beginning-of-line)
2860 (delete-region (point-min) (point))
2861 (goto-char (point-max))
2862 (skip-chars-backward " \r\t\n")
2863 (forward-line)
2864 (delete-region (point) (point-max))
2865 ;; If IND is set, preserve indentation of include keyword until
2866 ;; the first headline encountered.
2867 (when ind
2868 (unless (eq major-mode 'org-mode) (org-mode))
2869 (goto-char (point-min))
2870 (let ((ind-str (make-string ind ? )))
2871 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2872 ;; Do not move footnote definitions out of column 0.
2873 (unless (and (looking-at org-footnote-definition-re)
2874 (eq (org-element-type (org-element-at-point))
2875 'footnote-definition))
2876 (insert ind-str))
2877 (forward-line))))
2878 ;; When MINLEVEL is specified, compute minimal level for headlines
2879 ;; in the file (CUR-MIN), and remove stars to each headline so
2880 ;; that headlines with minimal level have a level of MINLEVEL.
2881 (when minlevel
2882 (unless (eq major-mode 'org-mode) (org-mode))
2883 (let ((levels (org-map-entries
2884 (lambda () (org-reduced-level (org-current-level))))))
2885 (when levels
2886 (let ((offset (- minlevel (apply 'min levels))))
2887 (unless (zerop offset)
2888 (when org-odd-levels-only (setq offset (* offset 2)))
2889 ;; Only change stars, don't bother moving whole
2890 ;; sections.
2891 (org-map-entries
2892 (lambda () (if (< offset 0) (delete-char (abs offset))
2893 (insert (make-string offset ?*))))))))))
2894 (buffer-string)))
2897 ;;; Tools For Back-Ends
2899 ;; A whole set of tools is available to help build new exporters. Any
2900 ;; function general enough to have its use across many back-ends
2901 ;; should be added here.
2903 ;;;; For Affiliated Keywords
2905 ;; `org-export-read-attribute' reads a property from a given element
2906 ;; as a plist. It can be used to normalize affiliated keywords'
2907 ;; syntax.
2909 ;; Since captions can span over multiple lines and accept dual values,
2910 ;; their internal representation is a bit tricky. Therefore,
2911 ;; `org-export-get-caption' transparently returns a given element's
2912 ;; caption as a secondary string.
2914 (defun org-export-read-attribute (attribute element &optional property)
2915 "Turn ATTRIBUTE property from ELEMENT into a plist.
2917 When optional argument PROPERTY is non-nil, return the value of
2918 that property within attributes.
2920 This function assumes attributes are defined as \":keyword
2921 value\" pairs. It is appropriate for `:attr_html' like
2922 properties."
2923 (let ((attributes
2924 (let ((value (org-element-property attribute element)))
2925 (and value
2926 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2927 (if property (plist-get attributes property) attributes)))
2929 (defun org-export-get-caption (element &optional shortp)
2930 "Return caption from ELEMENT as a secondary string.
2932 When optional argument SHORTP is non-nil, return short caption,
2933 as a secondary string, instead.
2935 Caption lines are separated by a white space."
2936 (let ((full-caption (org-element-property :caption element)) caption)
2937 (dolist (line full-caption (cdr caption))
2938 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2939 (when cap
2940 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2943 ;;;; For Export Snippets
2945 ;; Every export snippet is transmitted to the back-end. Though, the
2946 ;; latter will only retain one type of export-snippet, ignoring
2947 ;; others, based on the former's target back-end. The function
2948 ;; `org-export-snippet-backend' returns that back-end for a given
2949 ;; export-snippet.
2951 (defun org-export-snippet-backend (export-snippet)
2952 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2953 Translation, with `org-export-snippet-translation-alist', is
2954 applied."
2955 (let ((back-end (org-element-property :back-end export-snippet)))
2956 (intern
2957 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2958 back-end))))
2961 ;;;; For Footnotes
2963 ;; `org-export-collect-footnote-definitions' is a tool to list
2964 ;; actually used footnotes definitions in the whole parse tree, or in
2965 ;; an headline, in order to add footnote listings throughout the
2966 ;; transcoded data.
2968 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2969 ;; back-ends, when they need to attach the footnote definition only to
2970 ;; the first occurrence of the corresponding label.
2972 ;; `org-export-get-footnote-definition' and
2973 ;; `org-export-get-footnote-number' provide easier access to
2974 ;; additional information relative to a footnote reference.
2976 (defun org-export-collect-footnote-definitions (data info)
2977 "Return an alist between footnote numbers, labels and definitions.
2979 DATA is the parse tree from which definitions are collected.
2980 INFO is the plist used as a communication channel.
2982 Definitions are sorted by order of references. They either
2983 appear as Org data or as a secondary string for inlined
2984 footnotes. Unreferenced definitions are ignored."
2985 (let* (num-alist
2986 collect-fn ; for byte-compiler.
2987 (collect-fn
2988 (function
2989 (lambda (data)
2990 ;; Collect footnote number, label and definition in DATA.
2991 (org-element-map
2992 data 'footnote-reference
2993 (lambda (fn)
2994 (when (org-export-footnote-first-reference-p fn info)
2995 (let ((def (org-export-get-footnote-definition fn info)))
2996 (push
2997 (list (org-export-get-footnote-number fn info)
2998 (org-element-property :label fn)
2999 def)
3000 num-alist)
3001 ;; Also search in definition for nested footnotes.
3002 (when (eq (org-element-property :type fn) 'standard)
3003 (funcall collect-fn def)))))
3004 ;; Don't enter footnote definitions since it will happen
3005 ;; when their first reference is found.
3006 info nil 'footnote-definition)))))
3007 (funcall collect-fn (plist-get info :parse-tree))
3008 (reverse num-alist)))
3010 (defun org-export-footnote-first-reference-p (footnote-reference info)
3011 "Non-nil when a footnote reference is the first one for its label.
3013 FOOTNOTE-REFERENCE is the footnote reference being considered.
3014 INFO is the plist used as a communication channel."
3015 (let ((label (org-element-property :label footnote-reference)))
3016 ;; Anonymous footnotes are always a first reference.
3017 (if (not label) t
3018 ;; Otherwise, return the first footnote with the same LABEL and
3019 ;; test if it is equal to FOOTNOTE-REFERENCE.
3020 (let* (search-refs ; for byte-compiler.
3021 (search-refs
3022 (function
3023 (lambda (data)
3024 (org-element-map
3025 data 'footnote-reference
3026 (lambda (fn)
3027 (cond
3028 ((string= (org-element-property :label fn) label)
3029 (throw 'exit fn))
3030 ;; If FN isn't inlined, be sure to traverse its
3031 ;; definition before resuming search. See
3032 ;; comments in `org-export-get-footnote-number'
3033 ;; for more information.
3034 ((eq (org-element-property :type fn) 'standard)
3035 (funcall search-refs
3036 (org-export-get-footnote-definition fn info)))))
3037 ;; Don't enter footnote definitions since it will
3038 ;; happen when their first reference is found.
3039 info 'first-match 'footnote-definition)))))
3040 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3041 footnote-reference)))))
3043 (defun org-export-get-footnote-definition (footnote-reference info)
3044 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3045 INFO is the plist used as a communication channel."
3046 (let ((label (org-element-property :label footnote-reference)))
3047 (or (org-element-property :inline-definition footnote-reference)
3048 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3050 (defun org-export-get-footnote-number (footnote info)
3051 "Return number associated to a footnote.
3053 FOOTNOTE is either a footnote reference or a footnote definition.
3054 INFO is the plist used as a communication channel."
3055 (let* ((label (org-element-property :label footnote))
3056 seen-refs
3057 search-ref ; For byte-compiler.
3058 (search-ref
3059 (function
3060 (lambda (data)
3061 ;; Search footnote references through DATA, filling
3062 ;; SEEN-REFS along the way.
3063 (org-element-map
3064 data 'footnote-reference
3065 (lambda (fn)
3066 (let ((fn-lbl (org-element-property :label fn)))
3067 (cond
3068 ;; Anonymous footnote match: return number.
3069 ((and (not fn-lbl) (eq fn footnote))
3070 (throw 'exit (1+ (length seen-refs))))
3071 ;; Labels match: return number.
3072 ((and label (string= label fn-lbl))
3073 (throw 'exit (1+ (length seen-refs))))
3074 ;; Anonymous footnote: it's always a new one. Also,
3075 ;; be sure to return nil from the `cond' so
3076 ;; `first-match' doesn't get us out of the loop.
3077 ((not fn-lbl) (push 'inline seen-refs) nil)
3078 ;; Label not seen so far: add it so SEEN-REFS.
3080 ;; Also search for subsequent references in
3081 ;; footnote definition so numbering follows reading
3082 ;; logic. Note that we don't have to care about
3083 ;; inline definitions, since `org-element-map'
3084 ;; already traverses them at the right time.
3086 ;; Once again, return nil to stay in the loop.
3087 ((not (member fn-lbl seen-refs))
3088 (push fn-lbl seen-refs)
3089 (funcall search-ref
3090 (org-export-get-footnote-definition fn info))
3091 nil))))
3092 ;; Don't enter footnote definitions since it will happen
3093 ;; when their first reference is found.
3094 info 'first-match 'footnote-definition)))))
3095 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3098 ;;;; For Headlines
3100 ;; `org-export-get-relative-level' is a shortcut to get headline
3101 ;; level, relatively to the lower headline level in the parsed tree.
3103 ;; `org-export-get-headline-number' returns the section number of an
3104 ;; headline, while `org-export-number-to-roman' allows to convert it
3105 ;; to roman numbers.
3107 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3108 ;; `org-export-last-sibling-p' are three useful predicates when it
3109 ;; comes to fulfill the `:headline-levels' property.
3111 (defun org-export-get-relative-level (headline info)
3112 "Return HEADLINE relative level within current parsed tree.
3113 INFO is a plist holding contextual information."
3114 (+ (org-element-property :level headline)
3115 (or (plist-get info :headline-offset) 0)))
3117 (defun org-export-low-level-p (headline info)
3118 "Non-nil when HEADLINE is considered as low level.
3120 INFO is a plist used as a communication channel.
3122 A low level headlines has a relative level greater than
3123 `:headline-levels' property value.
3125 Return value is the difference between HEADLINE relative level
3126 and the last level being considered as high enough, or nil."
3127 (let ((limit (plist-get info :headline-levels)))
3128 (when (wholenump limit)
3129 (let ((level (org-export-get-relative-level headline info)))
3130 (and (> level limit) (- level limit))))))
3132 (defun org-export-get-headline-number (headline info)
3133 "Return HEADLINE numbering as a list of numbers.
3134 INFO is a plist holding contextual information."
3135 (cdr (assoc headline (plist-get info :headline-numbering))))
3137 (defun org-export-numbered-headline-p (headline info)
3138 "Return a non-nil value if HEADLINE element should be numbered.
3139 INFO is a plist used as a communication channel."
3140 (let ((sec-num (plist-get info :section-numbers))
3141 (level (org-export-get-relative-level headline info)))
3142 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3144 (defun org-export-number-to-roman (n)
3145 "Convert integer N into a roman numeral."
3146 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3147 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3148 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3149 ( 1 . "I")))
3150 (res ""))
3151 (if (<= n 0)
3152 (number-to-string n)
3153 (while roman
3154 (if (>= n (caar roman))
3155 (setq n (- n (caar roman))
3156 res (concat res (cdar roman)))
3157 (pop roman)))
3158 res)))
3160 (defun org-export-get-tags (element info &optional tags)
3161 "Return list of tags associated to ELEMENT.
3163 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3164 is a plist used as a communication channel.
3166 Select tags (see `org-export-select-tags') and exclude tags (see
3167 `org-export-exclude-tags') are removed from the list.
3169 When non-nil, optional argument TAGS should be a list of strings.
3170 Any tag belonging to this list will also be removed."
3171 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3172 (member tag (plist-get info :exclude-tags))
3173 (member tag tags)))
3174 (org-element-property :tags element)))
3176 (defun org-export-get-node-property (property blob &optional inherited)
3177 "Return node PROPERTY value for BLOB.
3179 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3180 element or object.
3182 If optional argument INHERITED is non-nil, the value can be
3183 inherited from a parent headline.
3185 Return value is a string or nil."
3186 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3187 (org-export-get-parent-headline blob))))
3188 (if (not inherited) (org-element-property property blob)
3189 (let ((parent headline) value)
3190 (catch 'found
3191 (while parent
3192 (when (plist-member (nth 1 parent) property)
3193 (throw 'found (org-element-property property parent)))
3194 (setq parent (org-element-property :parent parent))))))))
3196 (defun org-export-first-sibling-p (headline info)
3197 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3198 INFO is a plist used as a communication channel."
3199 (not (eq (org-element-type (org-export-get-previous-element headline info))
3200 'headline)))
3202 (defun org-export-last-sibling-p (headline info)
3203 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3204 INFO is a plist used as a communication channel."
3205 (not (org-export-get-next-element headline info)))
3208 ;;;; For Links
3210 ;; `org-export-solidify-link-text' turns a string into a safer version
3211 ;; for links, replacing most non-standard characters with hyphens.
3213 ;; `org-export-get-coderef-format' returns an appropriate format
3214 ;; string for coderefs.
3216 ;; `org-export-inline-image-p' returns a non-nil value when the link
3217 ;; provided should be considered as an inline image.
3219 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3220 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3221 ;; returns an appropriate unique identifier when found, or nil.
3223 ;; `org-export-resolve-id-link' returns the first headline with
3224 ;; specified id or custom-id in parse tree, the path to the external
3225 ;; file with the id or nil when neither was found.
3227 ;; `org-export-resolve-coderef' associates a reference to a line
3228 ;; number in the element it belongs, or returns the reference itself
3229 ;; when the element isn't numbered.
3231 (defun org-export-solidify-link-text (s)
3232 "Take link text S and make a safe target out of it."
3233 (save-match-data
3234 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3236 (defun org-export-get-coderef-format (path desc)
3237 "Return format string for code reference link.
3238 PATH is the link path. DESC is its description."
3239 (save-match-data
3240 (cond ((not desc) "%s")
3241 ((string-match (regexp-quote (concat "(" path ")")) desc)
3242 (replace-match "%s" t t desc))
3243 (t desc))))
3245 (defun org-export-inline-image-p (link &optional rules)
3246 "Non-nil if LINK object points to an inline image.
3248 Optional argument is a set of RULES defining inline images. It
3249 is an alist where associations have the following shape:
3251 \(TYPE . REGEXP)
3253 Applying a rule means apply REGEXP against LINK's path when its
3254 type is TYPE. The function will return a non-nil value if any of
3255 the provided rules is non-nil. The default rule is
3256 `org-export-default-inline-image-rule'.
3258 This only applies to links without a description."
3259 (and (not (org-element-contents link))
3260 (let ((case-fold-search t)
3261 (rules (or rules org-export-default-inline-image-rule)))
3262 (catch 'exit
3263 (mapc
3264 (lambda (rule)
3265 (and (string= (org-element-property :type link) (car rule))
3266 (string-match (cdr rule)
3267 (org-element-property :path link))
3268 (throw 'exit t)))
3269 rules)
3270 ;; Return nil if no rule matched.
3271 nil))))
3273 (defun org-export-resolve-coderef (ref info)
3274 "Resolve a code reference REF.
3276 INFO is a plist used as a communication channel.
3278 Return associated line number in source code, or REF itself,
3279 depending on src-block or example element's switches."
3280 (org-element-map
3281 (plist-get info :parse-tree) '(example-block src-block)
3282 (lambda (el)
3283 (with-temp-buffer
3284 (insert (org-trim (org-element-property :value el)))
3285 (let* ((label-fmt (regexp-quote
3286 (or (org-element-property :label-fmt el)
3287 org-coderef-label-format)))
3288 (ref-re
3289 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3290 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3291 ;; Element containing REF is found. Resolve it to either
3292 ;; a label or a line number, as needed.
3293 (when (re-search-backward ref-re nil t)
3294 (cond
3295 ((org-element-property :use-labels el) ref)
3296 ((eq (org-element-property :number-lines el) 'continued)
3297 (+ (org-export-get-loc el info) (line-number-at-pos)))
3298 (t (line-number-at-pos)))))))
3299 info 'first-match))
3301 (defun org-export-resolve-fuzzy-link (link info)
3302 "Return LINK destination.
3304 INFO is a plist holding contextual information.
3306 Return value can be an object, an element, or nil:
3308 - If LINK path matches a target object (i.e. <<path>>) or
3309 element (i.e. \"#+TARGET: path\"), return it.
3311 - If LINK path exactly matches the name affiliated keyword
3312 \(i.e. #+NAME: path) of an element, return that element.
3314 - If LINK path exactly matches any headline name, return that
3315 element. If more than one headline share that name, priority
3316 will be given to the one with the closest common ancestor, if
3317 any, or the first one in the parse tree otherwise.
3319 - Otherwise, return nil.
3321 Assume LINK type is \"fuzzy\"."
3322 (let* ((path (org-element-property :path link))
3323 (match-title-p (eq (aref path 0) ?*)))
3324 (cond
3325 ;; First try to find a matching "<<path>>" unless user specified
3326 ;; he was looking for an headline (path starts with a *
3327 ;; character).
3328 ((and (not match-title-p)
3329 (loop for target in (plist-get info :target-list)
3330 when (string= (org-element-property :value target) path)
3331 return target)))
3332 ;; Then try to find an element with a matching "#+NAME: path"
3333 ;; affiliated keyword.
3334 ((and (not match-title-p)
3335 (org-element-map
3336 (plist-get info :parse-tree) org-element-all-elements
3337 (lambda (el)
3338 (when (string= (org-element-property :name el) path) el))
3339 info 'first-match)))
3340 ;; Last case: link either points to an headline or to
3341 ;; nothingness. Try to find the source, with priority given to
3342 ;; headlines with the closest common ancestor. If such candidate
3343 ;; is found, return it, otherwise return nil.
3345 (let ((find-headline
3346 (function
3347 ;; Return first headline whose `:raw-value' property
3348 ;; is NAME in parse tree DATA, or nil.
3349 (lambda (name data)
3350 (org-element-map
3351 data 'headline
3352 (lambda (headline)
3353 (when (string=
3354 (org-element-property :raw-value headline)
3355 name)
3356 headline))
3357 info 'first-match)))))
3358 ;; Search among headlines sharing an ancestor with link,
3359 ;; from closest to farthest.
3360 (or (catch 'exit
3361 (mapc
3362 (lambda (parent)
3363 (when (eq (org-element-type parent) 'headline)
3364 (let ((foundp (funcall find-headline path parent)))
3365 (when foundp (throw 'exit foundp)))))
3366 (org-export-get-genealogy link)) nil)
3367 ;; No match with a common ancestor: try the full parse-tree.
3368 (funcall find-headline
3369 (if match-title-p (substring path 1) path)
3370 (plist-get info :parse-tree))))))))
3372 (defun org-export-resolve-id-link (link info)
3373 "Return headline referenced as LINK destination.
3375 INFO is a plist used as a communication channel.
3377 Return value can be the headline element matched in current parse
3378 tree, a file name or nil. Assume LINK type is either \"id\" or
3379 \"custom-id\"."
3380 (let ((id (org-element-property :path link)))
3381 ;; First check if id is within the current parse tree.
3382 (or (org-element-map
3383 (plist-get info :parse-tree) 'headline
3384 (lambda (headline)
3385 (when (or (string= (org-element-property :id headline) id)
3386 (string= (org-element-property :custom-id headline) id))
3387 headline))
3388 info 'first-match)
3389 ;; Otherwise, look for external files.
3390 (cdr (assoc id (plist-get info :id-alist))))))
3392 (defun org-export-resolve-radio-link (link info)
3393 "Return radio-target object referenced as LINK destination.
3395 INFO is a plist used as a communication channel.
3397 Return value can be a radio-target object or nil. Assume LINK
3398 has type \"radio\"."
3399 (let ((path (org-element-property :path link)))
3400 (org-element-map
3401 (plist-get info :parse-tree) 'radio-target
3402 (lambda (radio)
3403 (when (equal (org-element-property :value radio) path) radio))
3404 info 'first-match)))
3407 ;;;; For References
3409 ;; `org-export-get-ordinal' associates a sequence number to any object
3410 ;; or element.
3412 (defun org-export-get-ordinal (element info &optional types predicate)
3413 "Return ordinal number of an element or object.
3415 ELEMENT is the element or object considered. INFO is the plist
3416 used as a communication channel.
3418 Optional argument TYPES, when non-nil, is a list of element or
3419 object types, as symbols, that should also be counted in.
3420 Otherwise, only provided element's type is considered.
3422 Optional argument PREDICATE is a function returning a non-nil
3423 value if the current element or object should be counted in. It
3424 accepts two arguments: the element or object being considered and
3425 the plist used as a communication channel. This allows to count
3426 only a certain type of objects (i.e. inline images).
3428 Return value is a list of numbers if ELEMENT is an headline or an
3429 item. It is nil for keywords. It represents the footnote number
3430 for footnote definitions and footnote references. If ELEMENT is
3431 a target, return the same value as if ELEMENT was the closest
3432 table, item or headline containing the target. In any other
3433 case, return the sequence number of ELEMENT among elements or
3434 objects of the same type."
3435 ;; A target keyword, representing an invisible target, never has
3436 ;; a sequence number.
3437 (unless (eq (org-element-type element) 'keyword)
3438 ;; Ordinal of a target object refer to the ordinal of the closest
3439 ;; table, item, or headline containing the object.
3440 (when (eq (org-element-type element) 'target)
3441 (setq element
3442 (loop for parent in (org-export-get-genealogy element)
3443 when
3444 (memq
3445 (org-element-type parent)
3446 '(footnote-definition footnote-reference headline item
3447 table))
3448 return parent)))
3449 (case (org-element-type element)
3450 ;; Special case 1: An headline returns its number as a list.
3451 (headline (org-export-get-headline-number element info))
3452 ;; Special case 2: An item returns its number as a list.
3453 (item (let ((struct (org-element-property :structure element)))
3454 (org-list-get-item-number
3455 (org-element-property :begin element)
3456 struct
3457 (org-list-prevs-alist struct)
3458 (org-list-parents-alist struct))))
3459 ((footnote-definition footnote-reference)
3460 (org-export-get-footnote-number element info))
3461 (otherwise
3462 (let ((counter 0))
3463 ;; Increment counter until ELEMENT is found again.
3464 (org-element-map
3465 (plist-get info :parse-tree) (or types (org-element-type element))
3466 (lambda (el)
3467 (cond
3468 ((eq element el) (1+ counter))
3469 ((not predicate) (incf counter) nil)
3470 ((funcall predicate el info) (incf counter) nil)))
3471 info 'first-match))))))
3474 ;;;; For Src-Blocks
3476 ;; `org-export-get-loc' counts number of code lines accumulated in
3477 ;; src-block or example-block elements with a "+n" switch until
3478 ;; a given element, excluded. Note: "-n" switches reset that count.
3480 ;; `org-export-unravel-code' extracts source code (along with a code
3481 ;; references alist) from an `element-block' or `src-block' type
3482 ;; element.
3484 ;; `org-export-format-code' applies a formatting function to each line
3485 ;; of code, providing relative line number and code reference when
3486 ;; appropriate. Since it doesn't access the original element from
3487 ;; which the source code is coming, it expects from the code calling
3488 ;; it to know if lines should be numbered and if code references
3489 ;; should appear.
3491 ;; Eventually, `org-export-format-code-default' is a higher-level
3492 ;; function (it makes use of the two previous functions) which handles
3493 ;; line numbering and code references inclusion, and returns source
3494 ;; code in a format suitable for plain text or verbatim output.
3496 (defun org-export-get-loc (element info)
3497 "Return accumulated lines of code up to ELEMENT.
3499 INFO is the plist used as a communication channel.
3501 ELEMENT is excluded from count."
3502 (let ((loc 0))
3503 (org-element-map
3504 (plist-get info :parse-tree)
3505 `(src-block example-block ,(org-element-type element))
3506 (lambda (el)
3507 (cond
3508 ;; ELEMENT is reached: Quit the loop.
3509 ((eq el element))
3510 ;; Only count lines from src-block and example-block elements
3511 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3512 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3513 ((let ((linums (org-element-property :number-lines el)))
3514 (when linums
3515 ;; Accumulate locs or reset them.
3516 (let ((lines (org-count-lines
3517 (org-trim (org-element-property :value el)))))
3518 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3519 ;; Return nil to stay in the loop.
3520 nil)))
3521 info 'first-match)
3522 ;; Return value.
3523 loc))
3525 (defun org-export-unravel-code (element)
3526 "Clean source code and extract references out of it.
3528 ELEMENT has either a `src-block' an `example-block' type.
3530 Return a cons cell whose CAR is the source code, cleaned from any
3531 reference and protective comma and CDR is an alist between
3532 relative line number (integer) and name of code reference on that
3533 line (string)."
3534 (let* ((line 0) refs
3535 ;; Get code and clean it. Remove blank lines at its
3536 ;; beginning and end.
3537 (code (let ((c (replace-regexp-in-string
3538 "\\`\\([ \t]*\n\\)+" ""
3539 (replace-regexp-in-string
3540 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3541 (org-element-property :value element)))))
3542 ;; If appropriate, remove global indentation.
3543 (if (or org-src-preserve-indentation
3544 (org-element-property :preserve-indent element))
3546 (org-remove-indentation c))))
3547 ;; Get format used for references.
3548 (label-fmt (regexp-quote
3549 (or (org-element-property :label-fmt element)
3550 org-coderef-label-format)))
3551 ;; Build a regexp matching a loc with a reference.
3552 (with-ref-re
3553 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3554 (replace-regexp-in-string
3555 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3556 ;; Return value.
3557 (cons
3558 ;; Code with references removed.
3559 (org-element-normalize-string
3560 (mapconcat
3561 (lambda (loc)
3562 (incf line)
3563 (if (not (string-match with-ref-re loc)) loc
3564 ;; Ref line: remove ref, and signal its position in REFS.
3565 (push (cons line (match-string 3 loc)) refs)
3566 (replace-match "" nil nil loc 1)))
3567 (org-split-string code "\n") "\n"))
3568 ;; Reference alist.
3569 refs)))
3571 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3572 "Format CODE by applying FUN line-wise and return it.
3574 CODE is a string representing the code to format. FUN is
3575 a function. It must accept three arguments: a line of
3576 code (string), the current line number (integer) or nil and the
3577 reference associated to the current line (string) or nil.
3579 Optional argument NUM-LINES can be an integer representing the
3580 number of code lines accumulated until the current code. Line
3581 numbers passed to FUN will take it into account. If it is nil,
3582 FUN's second argument will always be nil. This number can be
3583 obtained with `org-export-get-loc' function.
3585 Optional argument REF-ALIST can be an alist between relative line
3586 number (i.e. ignoring NUM-LINES) and the name of the code
3587 reference on it. If it is nil, FUN's third argument will always
3588 be nil. It can be obtained through the use of
3589 `org-export-unravel-code' function."
3590 (let ((--locs (org-split-string code "\n"))
3591 (--line 0))
3592 (org-element-normalize-string
3593 (mapconcat
3594 (lambda (--loc)
3595 (incf --line)
3596 (let ((--ref (cdr (assq --line ref-alist))))
3597 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3598 --locs "\n"))))
3600 (defun org-export-format-code-default (element info)
3601 "Return source code from ELEMENT, formatted in a standard way.
3603 ELEMENT is either a `src-block' or `example-block' element. INFO
3604 is a plist used as a communication channel.
3606 This function takes care of line numbering and code references
3607 inclusion. Line numbers, when applicable, appear at the
3608 beginning of the line, separated from the code by two white
3609 spaces. Code references, on the other hand, appear flushed to
3610 the right, separated by six white spaces from the widest line of
3611 code."
3612 ;; Extract code and references.
3613 (let* ((code-info (org-export-unravel-code element))
3614 (code (car code-info))
3615 (code-lines (org-split-string code "\n"))
3616 (refs (and (org-element-property :retain-labels element)
3617 (cdr code-info)))
3618 ;; Handle line numbering.
3619 (num-start (case (org-element-property :number-lines element)
3620 (continued (org-export-get-loc element info))
3621 (new 0)))
3622 (num-fmt
3623 (and num-start
3624 (format "%%%ds "
3625 (length (number-to-string
3626 (+ (length code-lines) num-start))))))
3627 ;; Prepare references display, if required. Any reference
3628 ;; should start six columns after the widest line of code,
3629 ;; wrapped with parenthesis.
3630 (max-width
3631 (+ (apply 'max (mapcar 'length code-lines))
3632 (if (not num-start) 0 (length (format num-fmt num-start))))))
3633 (org-export-format-code
3634 code
3635 (lambda (loc line-num ref)
3636 (let ((number-str (and num-fmt (format num-fmt line-num))))
3637 (concat
3638 number-str
3640 (and ref
3641 (concat (make-string
3642 (- (+ 6 max-width)
3643 (+ (length loc) (length number-str))) ? )
3644 (format "(%s)" ref))))))
3645 num-start refs)))
3648 ;;;; For Tables
3650 ;; `org-export-table-has-special-column-p' and and
3651 ;; `org-export-table-row-is-special-p' are predicates used to look for
3652 ;; meta-information about the table structure.
3654 ;; `org-table-has-header-p' tells when the rows before the first rule
3655 ;; should be considered as table's header.
3657 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3658 ;; and `org-export-table-cell-borders' extract information from
3659 ;; a table-cell element.
3661 ;; `org-export-table-dimensions' gives the number on rows and columns
3662 ;; in the table, ignoring horizontal rules and special columns.
3663 ;; `org-export-table-cell-address', given a table-cell object, returns
3664 ;; the absolute address of a cell. On the other hand,
3665 ;; `org-export-get-table-cell-at' does the contrary.
3667 ;; `org-export-table-cell-starts-colgroup-p',
3668 ;; `org-export-table-cell-ends-colgroup-p',
3669 ;; `org-export-table-row-starts-rowgroup-p',
3670 ;; `org-export-table-row-ends-rowgroup-p',
3671 ;; `org-export-table-row-starts-header-p' and
3672 ;; `org-export-table-row-ends-header-p' indicate position of current
3673 ;; row or cell within the table.
3675 (defun org-export-table-has-special-column-p (table)
3676 "Non-nil when TABLE has a special column.
3677 All special columns will be ignored during export."
3678 ;; The table has a special column when every first cell of every row
3679 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3680 ;; "*" "_" and "^". Though, do not consider a first row containing
3681 ;; only empty cells as special.
3682 (let ((special-column-p 'empty))
3683 (catch 'exit
3684 (mapc
3685 (lambda (row)
3686 (when (eq (org-element-property :type row) 'standard)
3687 (let ((value (org-element-contents
3688 (car (org-element-contents row)))))
3689 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3690 (setq special-column-p 'special))
3691 ((not value))
3692 (t (throw 'exit nil))))))
3693 (org-element-contents table))
3694 (eq special-column-p 'special))))
3696 (defun org-export-table-has-header-p (table info)
3697 "Non-nil when TABLE has an header.
3699 INFO is a plist used as a communication channel.
3701 A table has an header when it contains at least two row groups."
3702 (let ((rowgroup 1) row-flag)
3703 (org-element-map
3704 table 'table-row
3705 (lambda (row)
3706 (cond
3707 ((> rowgroup 1) t)
3708 ((and row-flag (eq (org-element-property :type row) 'rule))
3709 (incf rowgroup) (setq row-flag nil))
3710 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3711 (setq row-flag t) nil)))
3712 info)))
3714 (defun org-export-table-row-is-special-p (table-row info)
3715 "Non-nil if TABLE-ROW is considered special.
3717 INFO is a plist used as the communication channel.
3719 All special rows will be ignored during export."
3720 (when (eq (org-element-property :type table-row) 'standard)
3721 (let ((first-cell (org-element-contents
3722 (car (org-element-contents table-row)))))
3723 ;; A row is special either when...
3725 ;; ... it starts with a field only containing "/",
3726 (equal first-cell '("/"))
3727 ;; ... the table contains a special column and the row start
3728 ;; with a marking character among, "^", "_", "$" or "!",
3729 (and (org-export-table-has-special-column-p
3730 (org-export-get-parent table-row))
3731 (member first-cell '(("^") ("_") ("$") ("!"))))
3732 ;; ... it contains only alignment cookies and empty cells.
3733 (let ((special-row-p 'empty))
3734 (catch 'exit
3735 (mapc
3736 (lambda (cell)
3737 (let ((value (org-element-contents cell)))
3738 ;; Since VALUE is a secondary string, the following
3739 ;; checks avoid expanding it with `org-export-data'.
3740 (cond ((not value))
3741 ((and (not (cdr value))
3742 (stringp (car value))
3743 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3744 (car value)))
3745 (setq special-row-p 'cookie))
3746 (t (throw 'exit nil)))))
3747 (org-element-contents table-row))
3748 (eq special-row-p 'cookie)))))))
3750 (defun org-export-table-row-group (table-row info)
3751 "Return TABLE-ROW's group.
3753 INFO is a plist used as the communication channel.
3755 Return value is the group number, as an integer, or nil special
3756 rows and table rules. Group 1 is also table's header."
3757 (unless (or (eq (org-element-property :type table-row) 'rule)
3758 (org-export-table-row-is-special-p table-row info))
3759 (let ((group 0) row-flag)
3760 (catch 'found
3761 (mapc
3762 (lambda (row)
3763 (cond
3764 ((and (eq (org-element-property :type row) 'standard)
3765 (not (org-export-table-row-is-special-p row info)))
3766 (unless row-flag (incf group) (setq row-flag t)))
3767 ((eq (org-element-property :type row) 'rule)
3768 (setq row-flag nil)))
3769 (when (eq table-row row) (throw 'found group)))
3770 (org-element-contents (org-export-get-parent table-row)))))))
3772 (defun org-export-table-cell-width (table-cell info)
3773 "Return TABLE-CELL contents width.
3775 INFO is a plist used as the communication channel.
3777 Return value is the width given by the last width cookie in the
3778 same column as TABLE-CELL, or nil."
3779 (let* ((row (org-export-get-parent table-cell))
3780 (column (let ((cells (org-element-contents row)))
3781 (- (length cells) (length (memq table-cell cells)))))
3782 (table (org-export-get-parent-table table-cell))
3783 cookie-width)
3784 (mapc
3785 (lambda (row)
3786 (cond
3787 ;; In a special row, try to find a width cookie at COLUMN.
3788 ((org-export-table-row-is-special-p row info)
3789 (let ((value (org-element-contents
3790 (elt (org-element-contents row) column))))
3791 ;; The following checks avoid expanding unnecessarily the
3792 ;; cell with `org-export-data'
3793 (when (and value
3794 (not (cdr value))
3795 (stringp (car value))
3796 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3797 (match-string 1 (car value)))
3798 (setq cookie-width
3799 (string-to-number (match-string 1 (car value)))))))
3800 ;; Ignore table rules.
3801 ((eq (org-element-property :type row) 'rule))))
3802 (org-element-contents table))
3803 ;; Return value.
3804 cookie-width))
3806 (defun org-export-table-cell-alignment (table-cell info)
3807 "Return TABLE-CELL contents alignment.
3809 INFO is a plist used as the communication channel.
3811 Return alignment as specified by the last alignment cookie in the
3812 same column as TABLE-CELL. If no such cookie is found, a default
3813 alignment value will be deduced from fraction of numbers in the
3814 column (see `org-table-number-fraction' for more information).
3815 Possible values are `left', `right' and `center'."
3816 (let* ((row (org-export-get-parent table-cell))
3817 (column (let ((cells (org-element-contents row)))
3818 (- (length cells) (length (memq table-cell cells)))))
3819 (table (org-export-get-parent-table table-cell))
3820 (number-cells 0)
3821 (total-cells 0)
3822 cookie-align)
3823 (mapc
3824 (lambda (row)
3825 (cond
3826 ;; In a special row, try to find an alignment cookie at
3827 ;; COLUMN.
3828 ((org-export-table-row-is-special-p row info)
3829 (let ((value (org-element-contents
3830 (elt (org-element-contents row) column))))
3831 ;; Since VALUE is a secondary string, the following checks
3832 ;; avoid useless expansion through `org-export-data'.
3833 (when (and value
3834 (not (cdr value))
3835 (stringp (car value))
3836 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3837 (car value))
3838 (match-string 1 (car value)))
3839 (setq cookie-align (match-string 1 (car value))))))
3840 ;; Ignore table rules.
3841 ((eq (org-element-property :type row) 'rule))
3842 ;; In a standard row, check if cell's contents are expressing
3843 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3844 ;; Though, don't bother if an alignment cookie has already
3845 ;; defined cell's alignment.
3846 ((not cookie-align)
3847 (let ((value (org-export-data
3848 (org-element-contents
3849 (elt (org-element-contents row) column))
3850 info)))
3851 (incf total-cells)
3852 (when (string-match org-table-number-regexp value)
3853 (incf number-cells))))))
3854 (org-element-contents table))
3855 ;; Return value. Alignment specified by cookies has precedence
3856 ;; over alignment deduced from cells contents.
3857 (cond ((equal cookie-align "l") 'left)
3858 ((equal cookie-align "r") 'right)
3859 ((equal cookie-align "c") 'center)
3860 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3861 'right)
3862 (t 'left))))
3864 (defun org-export-table-cell-borders (table-cell info)
3865 "Return TABLE-CELL borders.
3867 INFO is a plist used as a communication channel.
3869 Return value is a list of symbols, or nil. Possible values are:
3870 `top', `bottom', `above', `below', `left' and `right'. Note:
3871 `top' (resp. `bottom') only happen for a cell in the first
3872 row (resp. last row) of the table, ignoring table rules, if any.
3874 Returned borders ignore special rows."
3875 (let* ((row (org-export-get-parent table-cell))
3876 (table (org-export-get-parent-table table-cell))
3877 borders)
3878 ;; Top/above border? TABLE-CELL has a border above when a rule
3879 ;; used to demarcate row groups can be found above. Hence,
3880 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3881 ;; another regular row has to be found above that rule.
3882 (let (rule-flag)
3883 (catch 'exit
3884 (mapc (lambda (row)
3885 (cond ((eq (org-element-property :type row) 'rule)
3886 (setq rule-flag t))
3887 ((not (org-export-table-row-is-special-p row info))
3888 (if rule-flag (throw 'exit (push 'above borders))
3889 (throw 'exit nil)))))
3890 ;; Look at every row before the current one.
3891 (cdr (memq row (reverse (org-element-contents table)))))
3892 ;; No rule above, or rule found starts the table (ignoring any
3893 ;; special row): TABLE-CELL is at the top of the table.
3894 (when rule-flag (push 'above borders))
3895 (push 'top borders)))
3896 ;; Bottom/below border? TABLE-CELL has a border below when next
3897 ;; non-regular row below is a rule.
3898 (let (rule-flag)
3899 (catch 'exit
3900 (mapc (lambda (row)
3901 (cond ((eq (org-element-property :type row) 'rule)
3902 (setq rule-flag t))
3903 ((not (org-export-table-row-is-special-p row info))
3904 (if rule-flag (throw 'exit (push 'below borders))
3905 (throw 'exit nil)))))
3906 ;; Look at every row after the current one.
3907 (cdr (memq row (org-element-contents table))))
3908 ;; No rule below, or rule found ends the table (modulo some
3909 ;; special row): TABLE-CELL is at the bottom of the table.
3910 (when rule-flag (push 'below borders))
3911 (push 'bottom borders)))
3912 ;; Right/left borders? They can only be specified by column
3913 ;; groups. Column groups are defined in a row starting with "/".
3914 ;; Also a column groups row only contains "<", "<>", ">" or blank
3915 ;; cells.
3916 (catch 'exit
3917 (let ((column (let ((cells (org-element-contents row)))
3918 (- (length cells) (length (memq table-cell cells))))))
3919 (mapc
3920 (lambda (row)
3921 (unless (eq (org-element-property :type row) 'rule)
3922 (when (equal (org-element-contents
3923 (car (org-element-contents row)))
3924 '("/"))
3925 (let ((column-groups
3926 (mapcar
3927 (lambda (cell)
3928 (let ((value (org-element-contents cell)))
3929 (when (member value '(("<") ("<>") (">") nil))
3930 (car value))))
3931 (org-element-contents row))))
3932 ;; There's a left border when previous cell, if
3933 ;; any, ends a group, or current one starts one.
3934 (when (or (and (not (zerop column))
3935 (member (elt column-groups (1- column))
3936 '(">" "<>")))
3937 (member (elt column-groups column) '("<" "<>")))
3938 (push 'left borders))
3939 ;; There's a right border when next cell, if any,
3940 ;; starts a group, or current one ends one.
3941 (when (or (and (/= (1+ column) (length column-groups))
3942 (member (elt column-groups (1+ column))
3943 '("<" "<>")))
3944 (member (elt column-groups column) '(">" "<>")))
3945 (push 'right borders))
3946 (throw 'exit nil)))))
3947 ;; Table rows are read in reverse order so last column groups
3948 ;; row has precedence over any previous one.
3949 (reverse (org-element-contents table)))))
3950 ;; Return value.
3951 borders))
3953 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3954 "Non-nil when TABLE-CELL is at the beginning of a row group.
3955 INFO is a plist used as a communication channel."
3956 ;; A cell starts a column group either when it is at the beginning
3957 ;; of a row (or after the special column, if any) or when it has
3958 ;; a left border.
3959 (or (eq (org-element-map
3960 (org-export-get-parent table-cell)
3961 'table-cell 'identity info 'first-match)
3962 table-cell)
3963 (memq 'left (org-export-table-cell-borders table-cell info))))
3965 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3966 "Non-nil when TABLE-CELL is at the end of a row group.
3967 INFO is a plist used as a communication channel."
3968 ;; A cell ends a column group either when it is at the end of a row
3969 ;; or when it has a right border.
3970 (or (eq (car (last (org-element-contents
3971 (org-export-get-parent table-cell))))
3972 table-cell)
3973 (memq 'right (org-export-table-cell-borders table-cell info))))
3975 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3976 "Non-nil when TABLE-ROW is at the beginning of a column group.
3977 INFO is a plist used as a communication channel."
3978 (unless (or (eq (org-element-property :type table-row) 'rule)
3979 (org-export-table-row-is-special-p table-row info))
3980 (let ((borders (org-export-table-cell-borders
3981 (car (org-element-contents table-row)) info)))
3982 (or (memq 'top borders) (memq 'above borders)))))
3984 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3985 "Non-nil when TABLE-ROW is at the end of a column group.
3986 INFO is a plist used as a communication channel."
3987 (unless (or (eq (org-element-property :type table-row) 'rule)
3988 (org-export-table-row-is-special-p table-row info))
3989 (let ((borders (org-export-table-cell-borders
3990 (car (org-element-contents table-row)) info)))
3991 (or (memq 'bottom borders) (memq 'below borders)))))
3993 (defun org-export-table-row-starts-header-p (table-row info)
3994 "Non-nil when TABLE-ROW is the first table header's row.
3995 INFO is a plist used as a communication channel."
3996 (and (org-export-table-has-header-p
3997 (org-export-get-parent-table table-row) info)
3998 (org-export-table-row-starts-rowgroup-p table-row info)
3999 (= (org-export-table-row-group table-row info) 1)))
4001 (defun org-export-table-row-ends-header-p (table-row info)
4002 "Non-nil when TABLE-ROW is the last table header's row.
4003 INFO is a plist used as a communication channel."
4004 (and (org-export-table-has-header-p
4005 (org-export-get-parent-table table-row) info)
4006 (org-export-table-row-ends-rowgroup-p table-row info)
4007 (= (org-export-table-row-group table-row info) 1)))
4009 (defun org-export-table-dimensions (table info)
4010 "Return TABLE dimensions.
4012 INFO is a plist used as a communication channel.
4014 Return value is a CONS like (ROWS . COLUMNS) where
4015 ROWS (resp. COLUMNS) is the number of exportable
4016 rows (resp. columns)."
4017 (let (first-row (columns 0) (rows 0))
4018 ;; Set number of rows, and extract first one.
4019 (org-element-map
4020 table 'table-row
4021 (lambda (row)
4022 (when (eq (org-element-property :type row) 'standard)
4023 (incf rows)
4024 (unless first-row (setq first-row row)))) info)
4025 ;; Set number of columns.
4026 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4027 ;; Return value.
4028 (cons rows columns)))
4030 (defun org-export-table-cell-address (table-cell info)
4031 "Return address of a regular TABLE-CELL object.
4033 TABLE-CELL is the cell considered. INFO is a plist used as
4034 a communication channel.
4036 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4037 zero-based index. Only exportable cells are considered. The
4038 function returns nil for other cells."
4039 (let* ((table-row (org-export-get-parent table-cell))
4040 (table (org-export-get-parent-table table-cell)))
4041 ;; Ignore cells in special rows or in special column.
4042 (unless (or (org-export-table-row-is-special-p table-row info)
4043 (and (org-export-table-has-special-column-p table)
4044 (eq (car (org-element-contents table-row)) table-cell)))
4045 (cons
4046 ;; Row number.
4047 (let ((row-count 0))
4048 (org-element-map
4049 table 'table-row
4050 (lambda (row)
4051 (cond ((eq (org-element-property :type row) 'rule) nil)
4052 ((eq row table-row) row-count)
4053 (t (incf row-count) nil)))
4054 info 'first-match))
4055 ;; Column number.
4056 (let ((col-count 0))
4057 (org-element-map
4058 table-row 'table-cell
4059 (lambda (cell)
4060 (if (eq cell table-cell) col-count (incf col-count) nil))
4061 info 'first-match))))))
4063 (defun org-export-get-table-cell-at (address table info)
4064 "Return regular table-cell object at ADDRESS in TABLE.
4066 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4067 zero-based index. TABLE is a table type element. INFO is
4068 a plist used as a communication channel.
4070 If no table-cell, among exportable cells, is found at ADDRESS,
4071 return nil."
4072 (let ((column-pos (cdr address)) (column-count 0))
4073 (org-element-map
4074 ;; Row at (car address) or nil.
4075 (let ((row-pos (car address)) (row-count 0))
4076 (org-element-map
4077 table 'table-row
4078 (lambda (row)
4079 (cond ((eq (org-element-property :type row) 'rule) nil)
4080 ((= row-count row-pos) row)
4081 (t (incf row-count) nil)))
4082 info 'first-match))
4083 'table-cell
4084 (lambda (cell)
4085 (if (= column-count column-pos) cell
4086 (incf column-count) nil))
4087 info 'first-match)))
4090 ;;;; For Tables Of Contents
4092 ;; `org-export-collect-headlines' builds a list of all exportable
4093 ;; headline elements, maybe limited to a certain depth. One can then
4094 ;; easily parse it and transcode it.
4096 ;; Building lists of tables, figures or listings is quite similar.
4097 ;; Once the generic function `org-export-collect-elements' is defined,
4098 ;; `org-export-collect-tables', `org-export-collect-figures' and
4099 ;; `org-export-collect-listings' can be derived from it.
4101 (defun org-export-collect-headlines (info &optional n)
4102 "Collect headlines in order to build a table of contents.
4104 INFO is a plist used as a communication channel.
4106 When optional argument N is an integer, it specifies the depth of
4107 the table of contents. Otherwise, it is set to the value of the
4108 last headline level. See `org-export-headline-levels' for more
4109 information.
4111 Return a list of all exportable headlines as parsed elements."
4112 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4113 (org-element-map
4114 (plist-get info :parse-tree)
4115 'headline
4116 (lambda (headline)
4117 ;; Strip contents from HEADLINE.
4118 (let ((relative-level (org-export-get-relative-level headline info)))
4119 (unless (> relative-level n) headline)))
4120 info))
4122 (defun org-export-collect-elements (type info &optional predicate)
4123 "Collect referenceable elements of a determined type.
4125 TYPE can be a symbol or a list of symbols specifying element
4126 types to search. Only elements with a caption are collected.
4128 INFO is a plist used as a communication channel.
4130 When non-nil, optional argument PREDICATE is a function accepting
4131 one argument, an element of type TYPE. It returns a non-nil
4132 value when that element should be collected.
4134 Return a list of all elements found, in order of appearance."
4135 (org-element-map
4136 (plist-get info :parse-tree) type
4137 (lambda (element)
4138 (and (org-element-property :caption element)
4139 (or (not predicate) (funcall predicate element))
4140 element))
4141 info))
4143 (defun org-export-collect-tables (info)
4144 "Build a list of tables.
4145 INFO is a plist used as a communication channel.
4147 Return a list of table elements with a caption."
4148 (org-export-collect-elements 'table info))
4150 (defun org-export-collect-figures (info predicate)
4151 "Build a list of figures.
4153 INFO is a plist used as a communication channel. PREDICATE is
4154 a function which accepts one argument: a paragraph element and
4155 whose return value is non-nil when that element should be
4156 collected.
4158 A figure is a paragraph type element, with a caption, verifying
4159 PREDICATE. The latter has to be provided since a \"figure\" is
4160 a vague concept that may depend on back-end.
4162 Return a list of elements recognized as figures."
4163 (org-export-collect-elements 'paragraph info predicate))
4165 (defun org-export-collect-listings (info)
4166 "Build a list of src blocks.
4168 INFO is a plist used as a communication channel.
4170 Return a list of src-block elements with a caption."
4171 (org-export-collect-elements 'src-block info))
4174 ;;;; Topology
4176 ;; Here are various functions to retrieve information about the
4177 ;; neighbourhood of a given element or object. Neighbours of interest
4178 ;; are direct parent (`org-export-get-parent'), parent headline
4179 ;; (`org-export-get-parent-headline'), first element containing an
4180 ;; object, (`org-export-get-parent-element'), parent table
4181 ;; (`org-export-get-parent-table'), previous element or object
4182 ;; (`org-export-get-previous-element') and next element or object
4183 ;; (`org-export-get-next-element').
4185 ;; `org-export-get-genealogy' returns the full genealogy of a given
4186 ;; element or object, from closest parent to full parse tree.
4188 (defun org-export-get-parent (blob)
4189 "Return BLOB parent or nil.
4190 BLOB is the element or object considered."
4191 (org-element-property :parent blob))
4193 (defun org-export-get-genealogy (blob)
4194 "Return full genealogy relative to a given element or object.
4196 BLOB is the element or object being considered.
4198 Ancestors are returned from closest to farthest, the last one
4199 being the full parse tree."
4200 (let (genealogy (parent blob))
4201 (while (setq parent (org-element-property :parent parent))
4202 (push parent genealogy))
4203 (nreverse genealogy)))
4205 (defun org-export-get-parent-headline (blob)
4206 "Return BLOB parent headline or nil.
4207 BLOB is the element or object being considered."
4208 (let ((parent blob))
4209 (while (and (setq parent (org-element-property :parent parent))
4210 (not (eq (org-element-type parent) 'headline))))
4211 parent))
4213 (defun org-export-get-parent-element (object)
4214 "Return first element containing OBJECT or nil.
4215 OBJECT is the object to consider."
4216 (let ((parent object))
4217 (while (and (setq parent (org-element-property :parent parent))
4218 (memq (org-element-type parent) org-element-all-objects)))
4219 parent))
4221 (defun org-export-get-parent-table (object)
4222 "Return OBJECT parent table or nil.
4223 OBJECT is either a `table-cell' or `table-element' type object."
4224 (let ((parent object))
4225 (while (and (setq parent (org-element-property :parent parent))
4226 (not (eq (org-element-type parent) 'table))))
4227 parent))
4229 (defun org-export-get-previous-element (blob info)
4230 "Return previous element or object.
4231 BLOB is an element or object. INFO is a plist used as
4232 a communication channel. Return previous exportable element or
4233 object, a string, or nil."
4234 (let (prev)
4235 (catch 'exit
4236 (mapc (lambda (obj)
4237 (cond ((eq obj blob) (throw 'exit prev))
4238 ((memq obj (plist-get info :ignore-list)))
4239 (t (setq prev obj))))
4240 (org-element-contents (org-export-get-parent blob))))))
4242 (defun org-export-get-next-element (blob info)
4243 "Return next element or object.
4244 BLOB is an element or object. INFO is a plist used as
4245 a communication channel. Return next exportable element or
4246 object, a string, or nil."
4247 (catch 'found
4248 (mapc (lambda (obj)
4249 (unless (memq obj (plist-get info :ignore-list))
4250 (throw 'found obj)))
4251 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4252 nil))
4255 ;;;; Translation
4257 ;; `org-export-translate' translates a string according to language
4258 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4259 ;; variable and a specified charset. `org-export-dictionary' contains
4260 ;; the dictionary used for the translation.
4262 (defconst org-export-dictionary
4263 '(("Author"
4264 ("ca" :default "Autor")
4265 ("cs" :default "Autor")
4266 ("da" :default "Ophavsmand")
4267 ("de" :default "Autor")
4268 ("eo" :html "A&#365;toro")
4269 ("es" :default "Autor")
4270 ("fi" :html "Tekij&auml;")
4271 ("fr" :default "Auteur")
4272 ("hu" :default "Szerz&otilde;")
4273 ("is" :html "H&ouml;fundur")
4274 ("it" :default "Autore")
4275 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4276 ("nl" :default "Auteur")
4277 ("no" :default "Forfatter")
4278 ("nb" :default "Forfatter")
4279 ("nn" :default "Forfattar")
4280 ("pl" :default "Autor")
4281 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4282 ("sv" :html "F&ouml;rfattare")
4283 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4284 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4285 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4286 ("Date"
4287 ("ca" :default "Data")
4288 ("cs" :default "Datum")
4289 ("da" :default "Dato")
4290 ("de" :default "Datum")
4291 ("eo" :default "Dato")
4292 ("es" :default "Fecha")
4293 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4294 ("hu" :html "D&aacute;tum")
4295 ("is" :default "Dagsetning")
4296 ("it" :default "Data")
4297 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4298 ("nl" :default "Datum")
4299 ("no" :default "Dato")
4300 ("nb" :default "Dato")
4301 ("nn" :default "Dato")
4302 ("pl" :default "Data")
4303 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4304 ("sv" :default "Datum")
4305 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4306 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4307 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4308 ("Equation"
4309 ("fr" :ascii "Equation" :default "Équation"))
4310 ("Figure")
4311 ("Footnotes"
4312 ("ca" :html "Peus de p&agrave;gina")
4313 ("cs" :default "Pozn\xe1mky pod carou")
4314 ("da" :default "Fodnoter")
4315 ("de" :html "Fu&szlig;noten")
4316 ("eo" :default "Piednotoj")
4317 ("es" :html "Pies de p&aacute;gina")
4318 ("fi" :default "Alaviitteet")
4319 ("fr" :default "Notes de bas de page")
4320 ("hu" :html "L&aacute;bjegyzet")
4321 ("is" :html "Aftanm&aacute;lsgreinar")
4322 ("it" :html "Note a pi&egrave; di pagina")
4323 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4324 ("nl" :default "Voetnoten")
4325 ("no" :default "Fotnoter")
4326 ("nb" :default "Fotnoter")
4327 ("nn" :default "Fotnotar")
4328 ("pl" :default "Przypis")
4329 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4330 ("sv" :default "Fotnoter")
4331 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4332 :utf-8 "Примітки")
4333 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4334 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4335 ("List of Listings"
4336 ("fr" :default "Liste des programmes"))
4337 ("List of Tables"
4338 ("fr" :default "Liste des tableaux"))
4339 ("Listing %d:"
4340 ("fr"
4341 :ascii "Programme %d :" :default "Programme nº %d :"
4342 :latin1 "Programme %d :"))
4343 ("Listing %d: %s"
4344 ("fr"
4345 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4346 :latin1 "Programme %d : %s"))
4347 ("See section %s"
4348 ("fr" :default "cf. section %s"))
4349 ("Table %d:"
4350 ("fr"
4351 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4352 ("Table %d: %s"
4353 ("fr"
4354 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4355 :latin1 "Tableau %d : %s"))
4356 ("Table of Contents"
4357 ("ca" :html "&Iacute;ndex")
4358 ("cs" :default "Obsah")
4359 ("da" :default "Indhold")
4360 ("de" :default "Inhaltsverzeichnis")
4361 ("eo" :default "Enhavo")
4362 ("es" :html "&Iacute;ndice")
4363 ("fi" :html "Sis&auml;llysluettelo")
4364 ("fr" :ascii "Sommaire" :default "Table des matières")
4365 ("hu" :html "Tartalomjegyz&eacute;k")
4366 ("is" :default "Efnisyfirlit")
4367 ("it" :default "Indice")
4368 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4369 ("nl" :default "Inhoudsopgave")
4370 ("no" :default "Innhold")
4371 ("nb" :default "Innhold")
4372 ("nn" :default "Innhald")
4373 ("pl" :html "Spis tre&#x015b;ci")
4374 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4375 :utf-8 "Содержание")
4376 ("sv" :html "Inneh&aring;ll")
4377 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4378 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4379 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4380 ("Unknown reference"
4381 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4382 "Dictionary for export engine.
4384 Alist whose CAR is the string to translate and CDR is an alist
4385 whose CAR is the language string and CDR is a plist whose
4386 properties are possible charsets and values translated terms.
4388 It is used as a database for `org-export-translate'. Since this
4389 function returns the string as-is if no translation was found,
4390 the variable only needs to record values different from the
4391 entry.")
4393 (defun org-export-translate (s encoding info)
4394 "Translate string S according to language specification.
4396 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4397 and `:utf-8'. INFO is a plist used as a communication channel.
4399 Translation depends on `:language' property. Return the
4400 translated string. If no translation is found, try to fall back
4401 to `:default' encoding. If it fails, return S."
4402 (let* ((lang (plist-get info :language))
4403 (translations (cdr (assoc lang
4404 (cdr (assoc s org-export-dictionary))))))
4405 (or (plist-get translations encoding)
4406 (plist-get translations :default)
4407 s)))
4411 ;;; The Dispatcher
4413 ;; `org-export-dispatch' is the standard interactive way to start an
4414 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4415 ;; for its interface, which, in turn, delegates response to key
4416 ;; pressed to `org-export-dispatch-action'.
4418 (defvar org-export-dispatch-menu-entries nil
4419 "List of menu entries available for `org-export-dispatch'.
4420 This variable shouldn't be set directly. Set-up :menu-entry
4421 keyword in either `org-export-define-backend' or
4422 `org-export-define-derived-backend' instead.")
4424 ;;;###autoload
4425 (defun org-export-dispatch ()
4426 "Export dispatcher for Org mode.
4428 It provides an access to common export related tasks in a buffer.
4429 Its interface comes in two flavours: standard and expert. While
4430 both share the same set of bindings, only the former displays the
4431 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4432 to switch to one or the other.
4434 Return an error if key pressed has no associated command."
4435 (interactive)
4436 (let* ((input (org-export-dispatch-ui (list org-export-initial-scope)
4438 org-export-dispatch-use-expert-ui))
4439 (action (car input))
4440 (optns (cdr input)))
4441 (case action
4442 ;; First handle special hard-coded actions.
4443 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4444 (publish-current-project
4445 (org-e-publish-current-project (memq 'force optns)))
4446 (publish-choose-project
4447 (org-e-publish (assoc (org-icompleting-read
4448 "Publish project: "
4449 org-e-publish-project-alist nil t)
4450 org-e-publish-project-alist)
4451 (memq 'force optns)))
4452 (publish-all (org-e-publish-all (memq 'force optns)))
4453 (otherwise
4454 (funcall action
4455 (memq 'subtree optns)
4456 (memq 'visible optns)
4457 (memq 'body optns))))))
4459 (defun org-export-dispatch-ui (options first-key expertp)
4460 "Handle interface for `org-export-dispatch'.
4462 OPTIONS is a list containing current interactive options set for
4463 export. It can contain any of the following symbols:
4464 `body' toggles a body-only export
4465 `subtree' restricts export to current subtree
4466 `visible' restricts export to visible part of buffer.
4467 `force' force publishing files.
4469 FIRST-KEY is the key pressed to select the first level menu. It
4470 is nil when this menu hasn't been selected yet.
4472 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4473 buffer is provided, but indications about currently active
4474 options are given in the prompt. Moreover, \[?] allows to switch
4475 back to standard interface."
4476 (let* ((fontify-key
4477 (lambda (key &optional access-key)
4478 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4479 ;; non-nil is the required first-level key to activate
4480 ;; KEY. When its value is t, activate KEY independently
4481 ;; on the first key, if any. A nil value means KEY will
4482 ;; only be activated at first level.
4483 (if (or (eq access-key t) (eq access-key first-key))
4484 (org-add-props key nil 'face 'org-warning)
4485 (org-no-properties key))))
4486 ;; Make sure order of menu doesn't depend on the order in
4487 ;; which back-ends are loaded.
4488 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4489 (lambda (a b) (< (car a) (car b)))))
4490 ;; Compute a list of allowed keys based on the first key
4491 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4492 ;; always available.
4493 (allowed-keys
4494 (nconc (list ?1 ?2 ?3 ?4)
4495 (mapcar 'car
4496 (if (not first-key) backends
4497 (nth 2 (assq first-key backends))))
4498 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4499 ((not first-key) (list ?P)))
4500 (when expertp (list ??))
4501 (list ?q)))
4502 ;; Build the help menu for standard UI.
4503 (help
4504 (unless expertp
4505 (concat
4506 ;; Options are hard-coded.
4507 (format "Options
4508 [%s] Body only: %s [%s] Visible only: %s
4509 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4510 (funcall fontify-key "1" t)
4511 (if (memq 'body options) "On " "Off")
4512 (funcall fontify-key "2" t)
4513 (if (memq 'visible options) "On " "Off")
4514 (funcall fontify-key "3" t)
4515 (if (memq 'subtree options) "Subtree" "Buffer ")
4516 (funcall fontify-key "4" t)
4517 (if (memq 'force options) "On " "Off"))
4518 ;; Display registered back-end entries.
4519 (mapconcat
4520 (lambda (entry)
4521 (let ((top-key (car entry)))
4522 (concat
4523 (format "[%s] %s\n"
4524 (funcall fontify-key (char-to-string top-key))
4525 (nth 1 entry))
4526 (let ((sub-menu (nth 2 entry)))
4527 (unless (functionp sub-menu)
4528 ;; Split sub-menu into two columns.
4529 (let ((index -1))
4530 (concat
4531 (mapconcat
4532 (lambda (sub-entry)
4533 (incf index)
4534 (format (if (zerop (mod index 2)) " [%s] %-24s"
4535 "[%s] %s\n")
4536 (funcall fontify-key
4537 (char-to-string (car sub-entry))
4538 top-key)
4539 (nth 1 sub-entry)))
4540 sub-menu "")
4541 (when (zerop (mod index 2)) "\n"))))))))
4542 backends "\n")
4543 ;; Publishing menu is hard-coded.
4544 (format "\n[%s] Publish
4545 [%s] Current file [%s] Current project
4546 [%s] Choose project [%s] All projects\n\n"
4547 (funcall fontify-key "P")
4548 (funcall fontify-key "f" ?P)
4549 (funcall fontify-key "p" ?P)
4550 (funcall fontify-key "x" ?P)
4551 (funcall fontify-key "a" ?P))
4552 (format "\[%s] %s"
4553 (funcall fontify-key "q" t)
4554 (if first-key "Main menu" "Exit")))))
4555 ;; Build prompts for both standard and expert UI.
4556 (standard-prompt (unless expertp "Export command: "))
4557 (expert-prompt
4558 (when expertp
4559 (format
4560 "Export command (Options: %s%s%s%s) [%s]: "
4561 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4562 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4563 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4564 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4565 (concat allowed-keys)))))
4566 ;; With expert UI, just read key with a fancy prompt. In standard
4567 ;; UI, display an intrusive help buffer.
4568 (if expertp
4569 (org-export-dispatch-action
4570 expert-prompt allowed-keys backends options first-key expertp)
4571 (save-window-excursion
4572 (delete-other-windows)
4573 (unwind-protect
4574 (progn
4575 (with-current-buffer
4576 (get-buffer-create "*Org Export Dispatcher*")
4577 (erase-buffer)
4578 (save-excursion (insert help)))
4579 (org-fit-window-to-buffer
4580 (display-buffer "*Org Export Dispatcher*"))
4581 (org-export-dispatch-action
4582 standard-prompt allowed-keys backends options first-key expertp))
4583 (and (get-buffer "*Org Export Dispatcher*")
4584 (kill-buffer "*Org Export Dispatcher*")))))))
4586 (defun org-export-dispatch-action
4587 (prompt allowed-keys backends options first-key expertp)
4588 "Read a character from command input and act accordingly.
4590 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4591 a list of characters available at a given step in the process.
4592 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4593 EXPERTP are the same as defined in `org-export-dispatch-ui',
4594 which see.
4596 Toggle export options when required. Otherwise, return value is
4597 a list with action as CAR and a list of interactive export
4598 options as CDR."
4599 (let ((key (let ((k (read-char-exclusive prompt)))
4600 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4601 ;; depending on user's key pressed.
4602 (if (< k 27) (+ k 96) k))))
4603 (cond
4604 ;; Ignore non-standard characters (i.e. "M-a") and
4605 ;; undefined associations.
4606 ((not (memq key allowed-keys))
4607 (org-export-dispatch-ui options first-key expertp))
4608 ;; q key at first level aborts export. At second
4609 ;; level, cancel first key instead.
4610 ((eq key ?q) (if (not first-key) (error "Export aborted")
4611 (org-export-dispatch-ui options nil expertp)))
4612 ;; Help key: Switch back to standard interface if
4613 ;; expert UI was active.
4614 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4615 ;; Toggle export options.
4616 ((memq key '(?1 ?2 ?3 ?4))
4617 (org-export-dispatch-ui
4618 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4619 (?4 'force))))
4620 (if (memq option options) (remq option options)
4621 (cons option options)))
4622 first-key expertp))
4623 ;; Action selected: Send key and options back to
4624 ;; `org-export-dispatch'.
4625 ((or first-key
4626 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4627 (functionp (nth 2 (assq key backends))))
4628 (cons (cond
4629 ((not first-key) (nth 2 (assq key backends)))
4630 ;; Publishing actions are hard-coded. Send a special
4631 ;; signal to `org-export-dispatch'.
4632 ((eq first-key ?P)
4633 (case key
4634 (?f 'publish-current-file)
4635 (?p 'publish-current-project)
4636 (?x 'publish-choose-project)
4637 (?a 'publish-all)))
4638 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4639 options))
4640 ;; Otherwise, enter sub-menu.
4641 (t (org-export-dispatch-ui options key expertp)))))
4644 (provide 'org-export)
4645 ;;; org-export.el ends here