org-export: Add filters for new element type
[org-mode.git] / contrib / lisp / org-export.el
blob520567c84f779168733b3d440c81252359a345db
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 (defmacro org-export-define-backend (backend translators &rest body)
705 "Define a new back-end BACKEND.
707 TRANSLATORS is an alist between object or element types and
708 functions handling them.
710 These functions should return a string without any trailing
711 space, or nil. They must accept three arguments: the object or
712 element itself, its contents or nil when it isn't recursive and
713 the property list used as a communication channel.
715 Contents, when not nil, are stripped from any global indentation
716 \(although the relative one is preserved). They also always end
717 with a single newline character.
719 If, for a given type, no function is found, that element or
720 object type will simply be ignored, along with any blank line or
721 white space at its end. The same will happen if the function
722 returns the nil value. If that function returns the empty
723 string, the type will be ignored, but the blank lines or white
724 spaces will be kept.
726 In addition to element and object types, one function can be
727 associated to the `template' symbol and another one to the
728 `plain-text' symbol.
730 The former returns the final transcoded string, and can be used
731 to add a preamble and a postamble to document's body. It must
732 accept two arguments: the transcoded string and the property list
733 containing export options.
735 The latter, when defined, is to be called on every text not
736 recognized as an element or an object. It must accept two
737 arguments: the text string and the information channel. It is an
738 appropriate place to protect special chars relative to the
739 back-end.
741 BODY can start with pre-defined keyword arguments. The following
742 keywords are understood:
744 :export-block
746 String, or list of strings, representing block names that
747 will not be parsed. This is used to specify blocks that will
748 contain raw code specific to the back-end. These blocks
749 still have to be handled by the relative `export-block' type
750 translator.
752 :filters-alist
754 Alist between filters and function, or list of functions,
755 specific to the back-end. See `org-export-filters-alist' for
756 a list of all allowed filters. Filters defined here
757 shouldn't make a back-end test, as it may prevent back-ends
758 derived from this one to behave properly.
760 :menu-entry
762 Menu entry for the export dispatcher. It should be a list
763 like:
765 \(KEY DESCRIPTION ACTION-OR-MENU)
767 where :
769 KEY is a free character selecting the back-end.
770 DESCRIPTION is a string naming the back-end.
771 ACTION-OR-MENU is either a function or an alist.
773 If it is an action, it will be called with three arguments:
774 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
775 for further explanations.
777 If it is an alist, associations should follow the
778 pattern:
780 \(KEY DESCRIPTION ACTION)
782 where KEY, DESCRIPTION and ACTION are described above.
784 Valid values include:
786 \(?m \"My Special Back-end\" my-special-export-function)
790 \(?l \"Export to LaTeX\"
791 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
792 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
793 \(?p \"PDF file\" org-e-latex-export-to-pdf)
794 \(?o \"PDF file and open\"
795 \(lambda (subtree visible body-only)
796 \(org-open-file
797 \(org-e-latex-export-to-pdf subtree visible body-only))))))
799 :options-alist
801 Alist between back-end specific properties introduced in
802 communication channel and how their value are acquired. See
803 `org-export-options-alist' for more information about
804 structure of the values."
805 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
806 (indent 1))
807 (let (export-block filters menu-entry options)
808 (while (keywordp (car body))
809 (case (pop body)
810 (:export-block (let ((names (pop body)))
811 (setq export-block
812 (if (consp names) (mapcar 'upcase names)
813 (list (upcase names))))))
814 (:filters-alist (setq filters (pop body)))
815 (:menu-entry (setq menu-entry (pop body)))
816 (:options-alist (setq options (pop body)))
817 (t (pop body))))
818 `(progn
819 ;; Define translators.
820 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
821 "Alist between element or object types and translators.")
822 ;; Define options.
823 ,(when options
824 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
825 ,(format "Alist between %s export properties and ways to set them.
826 See `org-export-options-alist' for more information on the
827 structure of the values."
828 backend)))
829 ;; Define filters.
830 ,(when filters
831 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
832 "Alist between filters keywords and back-end specific filters.
833 See `org-export-filters-alist' for more information."))
834 ;; Tell parser to not parse EXPORT-BLOCK blocks.
835 ,(when export-block
836 `(mapc
837 (lambda (name)
838 (add-to-list 'org-element-block-name-alist
839 `(,name . org-element-export-block-parser)))
840 ',export-block))
841 ;; Add an entry for back-end in `org-export-dispatch'.
842 ,(when menu-entry
843 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
844 (unless menu
845 `(push ',menu-entry org-export-dispatch-menu-entries))))
846 ;; Splice in the body, if any.
847 ,@body)))
849 (defmacro org-export-define-derived-backend (child parent &rest body)
850 "Create a new back-end as a variant of an existing one.
852 CHILD is the name of the derived back-end. PARENT is the name of
853 the parent back-end.
855 BODY can start with pre-defined keyword arguments. The following
856 keywords are understood:
858 :export-block
860 String, or list of strings, representing block names that
861 will not be parsed. This is used to specify blocks that will
862 contain raw code specific to the back-end. These blocks
863 still have to be handled by the relative `export-block' type
864 translator.
866 :filters-alist
868 Alist of filters that will overwrite or complete filters
869 defined in PARENT back-end. See `org-export-filters-alist'
870 for a list of allowed filters.
872 :menu-entry
874 Menu entry for the export dispatcher. See
875 `org-export-define-backend' for more information about the
876 expected value.
878 :options-alist
880 Alist of back-end specific properties that will overwrite or
881 complete those defined in PARENT back-end. Refer to
882 `org-export-options-alist' for more information about
883 structure of the values.
885 :sub-menu-entry
887 Append entries to an existing menu in the export dispatcher.
888 The associated value should be a list whose CAR is the
889 character selecting the menu to expand and CDR a list of
890 entries following the pattern:
892 \(KEY DESCRIPTION ACTION)
894 where KEY is a free character triggering the action,
895 DESCRIPTION is a string defining the action, and ACTION is
896 a function that will be called with three arguments:
897 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
898 for further explanations.
900 Valid values include:
902 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
903 \(?O \"As PDF file and open (Beamer)\"
904 \(lambda (s v b)
905 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
907 :translate-alist
909 Alist of element and object types and transcoders that will
910 overwrite or complete transcode table from PARENT back-end.
911 Refer to `org-export-define-backend' for detailed information
912 about transcoders.
914 As an example, here is how one could define \"my-latex\" back-end
915 as a variant of `e-latex' back-end with a custom template
916 function:
918 \(org-export-define-derived-backend my-latex e-latex
919 :translate-alist ((template . my-latex-template-fun)))
921 The back-end could then be called with, for example:
923 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
924 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
925 (indent 2))
926 (let (export-block filters menu-entry options sub-menu-entry translate)
927 (while (keywordp (car body))
928 (case (pop body)
929 (:export-block (let ((names (pop body)))
930 (setq export-block
931 (if (consp names) (mapcar 'upcase names)
932 (list (upcase names))))))
933 (:filters-alist (setq filters (pop body)))
934 (:menu-entry (setq menu-entry (pop body)))
935 (:options-alist (setq options (pop body)))
936 (:sub-menu-entry (setq sub-menu-entry (pop body)))
937 (:translate-alist (setq translate (pop body)))
938 (t (pop body))))
939 `(progn
940 ;; Tell parser to not parse EXPORT-BLOCK blocks.
941 ,(when export-block
942 `(mapc
943 (lambda (name)
944 (add-to-list 'org-element-block-name-alist
945 `(,name . org-element-export-block-parser)))
946 ',export-block))
947 ;; Define filters.
948 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
949 (when (or (boundp parent-filters) filters)
950 `(defconst ,(intern (format "org-%s-filters-alist" child))
951 ',(append filters
952 (and (boundp parent-filters)
953 (copy-sequence (symbol-value parent-filters))))
954 "Alist between filters keywords and back-end specific filters.
955 See `org-export-filters-alist' for more information.")))
956 ;; Define options.
957 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
958 (when (or (boundp parent-options) options)
959 `(defconst ,(intern (format "org-%s-options-alist" child))
960 ',(append options
961 (and (boundp parent-options)
962 (copy-sequence (symbol-value parent-options))))
963 ,(format "Alist between %s export properties and ways to set them.
964 See `org-export-options-alist' for more information on the
965 structure of the values."
966 child))))
967 ;; Define translators.
968 (defvar ,(intern (format "org-%s-translate-alist" child))
969 ',(append translate
970 (copy-sequence
971 (symbol-value
972 (intern (format "org-%s-translate-alist" parent)))))
973 "Alist between element or object types and translators.")
974 ;; Add an entry for back-end in `org-export-dispatch'.
975 ,(when menu-entry
976 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
977 (unless menu
978 `(push ',menu-entry org-export-dispatch-menu-entries))))
979 ,(when sub-menu-entry
980 (let ((menu (nth 2 (assq (car sub-menu-entry)
981 org-export-dispatch-menu-entries))))
982 (when menu `(nconc ',menu
983 ',(org-remove-if (lambda (e) (member e menu))
984 (cdr sub-menu-entry))))))
985 ;; Splice in the body, if any.
986 ,@body)))
990 ;;; The Communication Channel
992 ;; During export process, every function has access to a number of
993 ;; properties. They are of two types:
995 ;; 1. Environment options are collected once at the very beginning of
996 ;; the process, out of the original buffer and configuration.
997 ;; Collecting them is handled by `org-export-get-environment'
998 ;; function.
1000 ;; Most environment options are defined through the
1001 ;; `org-export-options-alist' variable.
1003 ;; 2. Tree properties are extracted directly from the parsed tree,
1004 ;; just before export, by `org-export-collect-tree-properties'.
1006 ;; Here is the full list of properties available during transcode
1007 ;; process, with their category and their value type.
1009 ;; + `:author' :: Author's name.
1010 ;; - category :: option
1011 ;; - type :: string
1013 ;; + `:back-end' :: Current back-end used for transcoding.
1014 ;; - category :: tree
1015 ;; - type :: symbol
1017 ;; + `:creator' :: String to write as creation information.
1018 ;; - category :: option
1019 ;; - type :: string
1021 ;; + `:date' :: String to use as date.
1022 ;; - category :: option
1023 ;; - type :: string
1025 ;; + `:description' :: Description text for the current data.
1026 ;; - category :: option
1027 ;; - type :: string
1029 ;; + `:email' :: Author's email.
1030 ;; - category :: option
1031 ;; - type :: string
1033 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1034 ;; process.
1035 ;; - category :: option
1036 ;; - type :: list of strings
1038 ;; + `:exported-data' :: Hash table used for memoizing
1039 ;; `org-export-data'.
1040 ;; - category :: tree
1041 ;; - type :: hash table
1043 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1044 ;; their definition, as parsed data. Only non-inlined footnotes
1045 ;; are represented in this alist. Also, every definition isn't
1046 ;; guaranteed to be referenced in the parse tree. The purpose of
1047 ;; this property is to preserve definitions from oblivion
1048 ;; (i.e. when the parse tree comes from a part of the original
1049 ;; buffer), it isn't meant for direct use in a back-end. To
1050 ;; retrieve a definition relative to a reference, use
1051 ;; `org-export-get-footnote-definition' instead.
1052 ;; - category :: option
1053 ;; - type :: alist (STRING . LIST)
1055 ;; + `:headline-levels' :: Maximum level being exported as an
1056 ;; headline. Comparison is done with the relative level of
1057 ;; headlines in the parse tree, not necessarily with their
1058 ;; actual level.
1059 ;; - category :: option
1060 ;; - type :: integer
1062 ;; + `:headline-offset' :: Difference between relative and real level
1063 ;; of headlines in the parse tree. For example, a value of -1
1064 ;; means a level 2 headline should be considered as level
1065 ;; 1 (cf. `org-export-get-relative-level').
1066 ;; - category :: tree
1067 ;; - type :: integer
1069 ;; + `:headline-numbering' :: Alist between headlines and their
1070 ;; numbering, as a list of numbers
1071 ;; (cf. `org-export-get-headline-number').
1072 ;; - category :: tree
1073 ;; - type :: alist (INTEGER . LIST)
1075 ;; + `:id-alist' :: Alist between ID strings and destination file's
1076 ;; path, relative to current directory. It is used by
1077 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1078 ;; external file.
1079 ;; - category :: option
1080 ;; - type :: alist (STRING . STRING)
1082 ;; + `:ignore-list' :: List of elements and objects that should be
1083 ;; ignored during export.
1084 ;; - category :: tree
1085 ;; - type :: list of elements and objects
1087 ;; + `:input-file' :: Full path to input file, if any.
1088 ;; - category :: option
1089 ;; - type :: string or nil
1091 ;; + `:keywords' :: List of keywords attached to data.
1092 ;; - category :: option
1093 ;; - type :: string
1095 ;; + `:language' :: Default language used for translations.
1096 ;; - category :: option
1097 ;; - type :: string
1099 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1100 ;; transcoding.
1101 ;; - category :: option
1102 ;; - type :: list (as returned by `org-element-parse-buffer')
1104 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1105 ;; all line breaks.
1106 ;; - category :: option
1107 ;; - type :: symbol (nil, t)
1109 ;; + `:section-numbers' :: Non-nil means transcoding should add
1110 ;; section numbers to headlines.
1111 ;; - category :: option
1112 ;; - type :: symbol (nil, t)
1114 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1115 ;; in transcoding. When such a tag is present, subtrees without
1116 ;; it are de facto excluded from the process. See
1117 ;; `use-select-tags'.
1118 ;; - category :: option
1119 ;; - type :: list of strings
1121 ;; + `:target-list' :: List of targets encountered in the parse tree.
1122 ;; This is used to partly resolve "fuzzy" links
1123 ;; (cf. `org-export-resolve-fuzzy-link').
1124 ;; - category :: tree
1125 ;; - type :: list of strings
1127 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1128 ;; a time stamp in the output.
1129 ;; - category :: option
1130 ;; - type :: symbol (nil, t)
1132 ;; + `:translate-alist' :: Alist between element and object types and
1133 ;; transcoding functions relative to the current back-end.
1134 ;; Special keys `template' and `plain-text' are also possible.
1135 ;; - category :: option
1136 ;; - type :: alist (SYMBOL . FUNCTION)
1138 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1139 ;; also be transcoded. If it is set to the `headline' symbol,
1140 ;; only the archived headline's name is retained.
1141 ;; - category :: option
1142 ;; - type :: symbol (nil, t, `headline')
1144 ;; + `:with-author' :: Non-nil means author's name should be included
1145 ;; in the output.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1150 ;; - category :: option
1151 ;; - type :: symbol (nil, t)
1153 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1154 ;; inserted at the end of the transcoded string. If the value
1155 ;; is `comment', it should be commented.
1156 ;; - category :: option
1157 ;; - type :: symbol (`comment', nil, t)
1159 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1160 ;; its value is a list of names, only drawers with such names
1161 ;; will be transcoded.
1162 ;; - category :: option
1163 ;; - type :: symbol (nil, t) or list of strings
1165 ;; + `:with-email' :: Non-nil means output should contain author's
1166 ;; email.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, t)
1170 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1171 ;; interpreted.
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1176 ;; strings starting with a colon as a fixed-with (verbatim) area.
1177 ;; - category :: option
1178 ;; - type :: symbol (nil, t)
1180 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1181 ;; footnotes.
1182 ;; - category :: option
1183 ;; - type :: symbol (nil, t)
1185 ;; + `:with-plannings' :: Non-nil means transcoding should include
1186 ;; planning info.
1187 ;; - category :: option
1188 ;; - type :: symbol (nil, t)
1190 ;; + `:with-priority' :: Non-nil means transcoding should include
1191 ;; priority cookies.
1192 ;; - category :: option
1193 ;; - type :: symbol (nil, t)
1195 ;; + `:with-special-strings' :: Non-nil means transcoding should
1196 ;; interpret special strings in plain text.
1197 ;; - category :: option
1198 ;; - type :: symbol (nil, t)
1200 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1201 ;; interpret subscript and superscript. With a value of "{}",
1202 ;; only interpret those using curly brackets.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, {}, t)
1206 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1207 ;; tables.
1208 ;; - category :: option
1209 ;; - type :: symbol (nil, t)
1211 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1212 ;; headlines. A `not-in-toc' value will remove them from the
1213 ;; table of contents, if any, nonetheless.
1214 ;; - category :: option
1215 ;; - type :: symbol (nil, t, `not-in-toc')
1217 ;; + `:with-tasks' :: Non-nil means transcoding should include
1218 ;; headlines with a TODO keyword. A `todo' value will only
1219 ;; include headlines with a todo type keyword while a `done'
1220 ;; value will do the contrary. If a list of strings is provided,
1221 ;; only tasks with keywords belonging to that list will be kept.
1222 ;; - category :: option
1223 ;; - type :: symbol (t, todo, done, nil) or list of strings
1225 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1226 ;; time stamps. Special value `active' (resp. `inactive') ask to
1227 ;; export only active (resp. inactive) timestamps. Otherwise,
1228 ;; completely remove them.
1229 ;; - category :: option
1230 ;; - type :: symbol: (`active', `inactive', t, nil)
1232 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1233 ;; added to the output. An integer value limits its depth.
1234 ;; - category :: option
1235 ;; - type :: symbol (nil, t or integer)
1237 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1238 ;; include TODO keywords.
1239 ;; - category :: option
1240 ;; - type :: symbol (nil, t)
1243 ;;;; Environment Options
1245 ;; Environment options encompass all parameters defined outside the
1246 ;; scope of the parsed data. They come from five sources, in
1247 ;; increasing precedence order:
1249 ;; - Global variables,
1250 ;; - Buffer's attributes,
1251 ;; - Options keyword symbols,
1252 ;; - Buffer keywords,
1253 ;; - Subtree properties.
1255 ;; The central internal function with regards to environment options
1256 ;; is `org-export-get-environment'. It updates global variables with
1257 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1258 ;; the different sources.
1260 ;; The internal functions doing the retrieval are:
1261 ;; `org-export--get-global-options',
1262 ;; `org-export--get-buffer-attributes',
1263 ;; `org-export--parse-option-keyword',
1264 ;; `org-export--get-subtree-options' and
1265 ;; `org-export--get-inbuffer-options'
1267 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1268 ;; take care of the part relative to "#+BIND:" keywords.
1270 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1271 "Collect export options from the current buffer.
1273 Optional argument BACKEND is a symbol specifying which back-end
1274 specific options to read, if any.
1276 When optional argument SUBTREEP is non-nil, assume the export is
1277 done against the current sub-tree.
1279 Third optional argument EXT-PLIST is a property list with
1280 external parameters overriding Org default settings, but still
1281 inferior to file-local settings."
1282 ;; First install #+BIND variables.
1283 (org-export--install-letbind-maybe)
1284 ;; Get and prioritize export options...
1285 (org-combine-plists
1286 ;; ... from global variables...
1287 (org-export--get-global-options backend)
1288 ;; ... from buffer's attributes...
1289 (org-export--get-buffer-attributes)
1290 ;; ... from an external property list...
1291 ext-plist
1292 ;; ... from in-buffer settings...
1293 (org-export--get-inbuffer-options
1294 backend
1295 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1296 ;; ... and from subtree, when appropriate.
1297 (and subtreep (org-export--get-subtree-options backend))
1298 ;; Eventually install back-end symbol and its translation table.
1299 `(:back-end
1300 ,backend
1301 :translate-alist
1302 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1303 (when (boundp trans-alist) (symbol-value trans-alist))))))
1305 (defun org-export--parse-option-keyword (options &optional backend)
1306 "Parse an OPTIONS line and return values as a plist.
1307 Optional argument BACKEND is a symbol specifying which back-end
1308 specific items to read, if any."
1309 (let* ((all
1310 (append org-export-options-alist
1311 (and backend
1312 (let ((var (intern
1313 (format "org-%s-options-alist" backend))))
1314 (and (boundp var) (eval var))))))
1315 ;; Build an alist between #+OPTION: item and property-name.
1316 (alist (delq nil
1317 (mapcar (lambda (e)
1318 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1319 (car e))))
1320 all)))
1321 plist)
1322 (mapc (lambda (e)
1323 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1324 (car e)
1325 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1326 options)
1327 (setq plist (plist-put plist
1328 (cdr e)
1329 (car (read-from-string
1330 (match-string 2 options)))))))
1331 alist)
1332 plist))
1334 (defun org-export--get-subtree-options (&optional backend)
1335 "Get export options in subtree at point.
1336 Optional argument BACKEND is a symbol specifying back-end used
1337 for export. Return options as a plist."
1338 ;; For each buffer keyword, create an headline property setting the
1339 ;; same property in communication channel. The name for the property
1340 ;; is the keyword with "EXPORT_" appended to it.
1341 (org-with-wide-buffer
1342 (let (prop plist)
1343 ;; Make sure point is at an heading.
1344 (unless (org-at-heading-p) (org-back-to-heading t))
1345 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1346 ;; title as its fallback value.
1347 (when (setq prop (progn (looking-at org-todo-line-regexp)
1348 (or (save-match-data
1349 (org-entry-get (point) "EXPORT_TITLE"))
1350 (org-match-string-no-properties 3))))
1351 (setq plist
1352 (plist-put
1353 plist :title
1354 (org-element-parse-secondary-string
1355 prop (org-element-restriction 'keyword)))))
1356 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1357 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1358 (setq plist
1359 (nconc plist (org-export--parse-option-keyword prop backend))))
1360 ;; Handle other keywords.
1361 (let ((seen '("TITLE")))
1362 (mapc
1363 (lambda (option)
1364 (let ((property (nth 1 option)))
1365 (when (and property (not (member property seen)))
1366 (let* ((subtree-prop (concat "EXPORT_" property))
1367 ;; Export properties are not case-sensitive.
1368 (value (let ((case-fold-search t))
1369 (org-entry-get (point) subtree-prop))))
1370 (push property seen)
1371 (when value
1372 (setq plist
1373 (plist-put
1374 plist
1375 (car option)
1376 ;; Parse VALUE if required.
1377 (if (member property org-element-parsed-keywords)
1378 (org-element-parse-secondary-string
1379 value (org-element-restriction 'keyword))
1380 value))))))))
1381 ;; Also look for both general keywords and back-end specific
1382 ;; options if BACKEND is provided.
1383 (append (and backend
1384 (let ((var (intern
1385 (format "org-%s-options-alist" backend))))
1386 (and (boundp var) (symbol-value var))))
1387 org-export-options-alist)))
1388 ;; Return value.
1389 plist)))
1391 (defun org-export--get-inbuffer-options (&optional backend files)
1392 "Return current buffer export options, as a plist.
1394 Optional argument BACKEND, when non-nil, is a symbol specifying
1395 which back-end specific options should also be read in the
1396 process.
1398 Optional argument FILES is a list of setup files names read so
1399 far, used to avoid circular dependencies.
1401 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1402 (org-with-wide-buffer
1403 (goto-char (point-min))
1404 (let ((case-fold-search t) plist)
1405 ;; 1. Special keywords, as in `org-export-special-keywords'.
1406 (let ((special-re
1407 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1408 (while (re-search-forward special-re nil t)
1409 (let ((element (org-element-at-point)))
1410 (when (eq (org-element-type element) 'keyword)
1411 (let* ((key (org-element-property :key element))
1412 (val (org-element-property :value element))
1413 (prop
1414 (cond
1415 ((string= key "SETUP_FILE")
1416 (let ((file
1417 (expand-file-name
1418 (org-remove-double-quotes (org-trim val)))))
1419 ;; Avoid circular dependencies.
1420 (unless (member file files)
1421 (with-temp-buffer
1422 (insert (org-file-contents file 'noerror))
1423 (org-mode)
1424 (org-export--get-inbuffer-options
1425 backend (cons file files))))))
1426 ((string= key "OPTIONS")
1427 (org-export--parse-option-keyword val backend)))))
1428 (setq plist (org-combine-plists plist prop)))))))
1429 ;; 2. Standard options, as in `org-export-options-alist'.
1430 (let* ((all (append org-export-options-alist
1431 ;; Also look for back-end specific options
1432 ;; if BACKEND is defined.
1433 (and backend
1434 (let ((var
1435 (intern
1436 (format "org-%s-options-alist" backend))))
1437 (and (boundp var) (eval var))))))
1438 ;; Build ALIST between keyword name and property name.
1439 (alist
1440 (delq nil (mapcar
1441 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1442 all)))
1443 ;; Build regexp matching all keywords associated to export
1444 ;; options. Note: the search is case insensitive.
1445 (opt-re (format "^[ \t]*#\\+%s:"
1446 (regexp-opt
1447 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1448 (goto-char (point-min))
1449 (while (re-search-forward opt-re nil t)
1450 (let ((element (org-element-at-point)))
1451 (when (eq (org-element-type element) 'keyword)
1452 (let* ((key (org-element-property :key element))
1453 (val (org-element-property :value element))
1454 (prop (cdr (assoc key alist)))
1455 (behaviour (nth 4 (assq prop all))))
1456 (setq plist
1457 (plist-put
1458 plist prop
1459 ;; Handle value depending on specified BEHAVIOUR.
1460 (case behaviour
1461 (space
1462 (if (not (plist-get plist prop)) (org-trim val)
1463 (concat (plist-get plist prop) " " (org-trim val))))
1464 (newline
1465 (org-trim
1466 (concat (plist-get plist prop) "\n" (org-trim val))))
1467 (split
1468 `(,@(plist-get plist prop) ,@(org-split-string val)))
1469 ('t val)
1470 (otherwise (if (not (plist-member plist prop)) val
1471 (plist-get plist prop))))))))))
1472 ;; Parse keywords specified in `org-element-parsed-keywords'.
1473 (mapc
1474 (lambda (key)
1475 (let* ((prop (cdr (assoc key alist)))
1476 (value (and prop (plist-get plist prop))))
1477 (when (stringp value)
1478 (setq plist
1479 (plist-put
1480 plist prop
1481 (org-element-parse-secondary-string
1482 value (org-element-restriction 'keyword)))))))
1483 org-element-parsed-keywords))
1484 ;; 3. Return final value.
1485 plist)))
1487 (defun org-export--get-buffer-attributes ()
1488 "Return properties related to buffer attributes, as a plist."
1489 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1490 (list
1491 ;; Store full path of input file name, or nil. For internal use.
1492 :input-file visited-file
1493 :title (or (and visited-file
1494 (file-name-sans-extension
1495 (file-name-nondirectory visited-file)))
1496 (buffer-name (buffer-base-buffer)))
1497 :footnote-definition-alist
1498 ;; Footnotes definitions must be collected in the original
1499 ;; buffer, as there's no insurance that they will still be in the
1500 ;; parse tree, due to possible narrowing.
1501 (let (alist)
1502 (org-with-wide-buffer
1503 (goto-char (point-min))
1504 (while (re-search-forward org-footnote-definition-re nil t)
1505 (let ((def (org-footnote-at-definition-p)))
1506 (when def
1507 (org-skip-whitespace)
1508 (push (cons (car def)
1509 (save-restriction
1510 (narrow-to-region (point) (nth 2 def))
1511 ;; Like `org-element-parse-buffer', but
1512 ;; makes sure the definition doesn't start
1513 ;; with a section element.
1514 (org-element--parse-elements
1515 (point-min) (point-max) nil nil nil nil
1516 (list 'org-data nil))))
1517 alist))))
1518 alist))
1519 :id-alist
1520 ;; Collect id references.
1521 (let (alist)
1522 (org-with-wide-buffer
1523 (goto-char (point-min))
1524 (while (re-search-forward
1525 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1526 (let* ((id (org-match-string-no-properties 1))
1527 (file (org-id-find-id-file id)))
1528 (when file (push (cons id (file-relative-name file)) alist)))))
1529 alist))))
1531 (defun org-export--get-global-options (&optional backend)
1532 "Return global export options as a plist.
1534 Optional argument BACKEND, if non-nil, is a symbol specifying
1535 which back-end specific export options should also be read in the
1536 process."
1537 (let ((all (append org-export-options-alist
1538 (and backend
1539 (let ((var (intern
1540 (format "org-%s-options-alist" backend))))
1541 (and (boundp var) (symbol-value var))))))
1542 ;; Output value.
1543 plist)
1544 (mapc
1545 (lambda (cell)
1546 (setq plist
1547 (plist-put
1548 plist
1549 (car cell)
1550 ;; Eval default value provided. If keyword is a member
1551 ;; of `org-element-parsed-keywords', parse it as
1552 ;; a secondary string before storing it.
1553 (let ((value (eval (nth 3 cell))))
1554 (if (not (stringp value)) value
1555 (let ((keyword (nth 1 cell)))
1556 (if (not (member keyword org-element-parsed-keywords)) value
1557 (org-element-parse-secondary-string
1558 value (org-element-restriction 'keyword)))))))))
1559 all)
1560 ;; Return value.
1561 plist))
1563 (defvar org-export--allow-BIND-local nil)
1564 (defun org-export--confirm-letbind ()
1565 "Can we use #+BIND values during export?
1566 By default this will ask for confirmation by the user, to divert
1567 possible security risks."
1568 (cond
1569 ((not org-export-allow-BIND) nil)
1570 ((eq org-export-allow-BIND t) t)
1571 ((local-variable-p 'org-export--allow-BIND-local)
1572 org-export--allow-BIND-local)
1573 (t (org-set-local 'org-export--allow-BIND-local
1574 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1576 (defun org-export--install-letbind-maybe ()
1577 "Install the values from #+BIND lines as local variables.
1578 Variables must be installed before in-buffer options are
1579 retrieved."
1580 (let ((case-fold-search t) letbind pair)
1581 (org-with-wide-buffer
1582 (goto-char (point-min))
1583 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1584 (let* ((element (org-element-at-point))
1585 (value (org-element-property :value element)))
1586 (when (and (eq (org-element-type element) 'keyword)
1587 (not (equal value ""))
1588 (org-export--confirm-letbind))
1589 (push (read (format "(%s)" value)) letbind)))))
1590 (dolist (pair (nreverse letbind))
1591 (org-set-local (car pair) (nth 1 pair)))))
1594 ;;;; Tree Properties
1596 ;; Tree properties are infromation extracted from parse tree. They
1597 ;; are initialized at the beginning of the transcoding process by
1598 ;; `org-export-collect-tree-properties'.
1600 ;; Dedicated functions focus on computing the value of specific tree
1601 ;; properties during initialization. Thus,
1602 ;; `org-export--populate-ignore-list' lists elements and objects that
1603 ;; should be skipped during export, `org-export--get-min-level' gets
1604 ;; the minimal exportable level, used as a basis to compute relative
1605 ;; level for headlines. Eventually
1606 ;; `org-export--collect-headline-numbering' builds an alist between
1607 ;; headlines and their numbering.
1609 (defun org-export-collect-tree-properties (data info)
1610 "Extract tree properties from parse tree.
1612 DATA is the parse tree from which information is retrieved. INFO
1613 is a list holding export options.
1615 Following tree properties are set or updated:
1617 `:exported-data' Hash table used to memoize results from
1618 `org-export-data'.
1620 `:footnote-definition-alist' List of footnotes definitions in
1621 original buffer and current parse tree.
1623 `:headline-offset' Offset between true level of headlines and
1624 local level. An offset of -1 means an headline
1625 of level 2 should be considered as a level
1626 1 headline in the context.
1628 `:headline-numbering' Alist of all headlines as key an the
1629 associated numbering as value.
1631 `:ignore-list' List of elements that should be ignored during
1632 export.
1634 `:target-list' List of all targets in the parse tree.
1636 Return updated plist."
1637 ;; Install the parse tree in the communication channel, in order to
1638 ;; use `org-export-get-genealogy' and al.
1639 (setq info (plist-put info :parse-tree data))
1640 ;; Get the list of elements and objects to ignore, and put it into
1641 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1642 ;; been done during parse tree filtering.
1643 (setq info
1644 (plist-put info
1645 :ignore-list
1646 (append (org-export--populate-ignore-list data info)
1647 (plist-get info :ignore-list))))
1648 ;; Compute `:headline-offset' in order to be able to use
1649 ;; `org-export-get-relative-level'.
1650 (setq info
1651 (plist-put info
1652 :headline-offset
1653 (- 1 (org-export--get-min-level data info))))
1654 ;; Update footnotes definitions list with definitions in parse tree.
1655 ;; This is required since buffer expansion might have modified
1656 ;; boundaries of footnote definitions contained in the parse tree.
1657 ;; This way, definitions in `footnote-definition-alist' are bound to
1658 ;; match those in the parse tree.
1659 (let ((defs (plist-get info :footnote-definition-alist)))
1660 (org-element-map
1661 data 'footnote-definition
1662 (lambda (fn)
1663 (push (cons (org-element-property :label fn)
1664 `(org-data nil ,@(org-element-contents fn)))
1665 defs)))
1666 (setq info (plist-put info :footnote-definition-alist defs)))
1667 ;; Properties order doesn't matter: get the rest of the tree
1668 ;; properties.
1669 (nconc
1670 `(:target-list
1671 ,(org-element-map
1672 data '(keyword target)
1673 (lambda (blob)
1674 (when (or (eq (org-element-type blob) 'target)
1675 (string= (org-element-property :key blob) "TARGET"))
1676 blob)) info)
1677 :headline-numbering ,(org-export--collect-headline-numbering data info)
1678 :exported-data ,(make-hash-table :test 'eq :size 4001))
1679 info))
1681 (defun org-export--get-min-level (data options)
1682 "Return minimum exportable headline's level in DATA.
1683 DATA is parsed tree as returned by `org-element-parse-buffer'.
1684 OPTIONS is a plist holding export options."
1685 (catch 'exit
1686 (let ((min-level 10000))
1687 (mapc
1688 (lambda (blob)
1689 (when (and (eq (org-element-type blob) 'headline)
1690 (not (memq blob (plist-get options :ignore-list))))
1691 (setq min-level
1692 (min (org-element-property :level blob) min-level)))
1693 (when (= min-level 1) (throw 'exit 1)))
1694 (org-element-contents data))
1695 ;; If no headline was found, for the sake of consistency, set
1696 ;; minimum level to 1 nonetheless.
1697 (if (= min-level 10000) 1 min-level))))
1699 (defun org-export--collect-headline-numbering (data options)
1700 "Return numbering of all exportable headlines in a parse tree.
1702 DATA is the parse tree. OPTIONS is the plist holding export
1703 options.
1705 Return an alist whose key is an headline and value is its
1706 associated numbering \(in the shape of a list of numbers\)."
1707 (let ((numbering (make-vector org-export-max-depth 0)))
1708 (org-element-map
1709 data
1710 'headline
1711 (lambda (headline)
1712 (let ((relative-level
1713 (1- (org-export-get-relative-level headline options))))
1714 (cons
1715 headline
1716 (loop for n across numbering
1717 for idx from 0 to org-export-max-depth
1718 when (< idx relative-level) collect n
1719 when (= idx relative-level) collect (aset numbering idx (1+ n))
1720 when (> idx relative-level) do (aset numbering idx 0)))))
1721 options)))
1723 (defun org-export--populate-ignore-list (data options)
1724 "Return list of elements and objects to ignore during export.
1725 DATA is the parse tree to traverse. OPTIONS is the plist holding
1726 export options."
1727 (let* (ignore
1728 walk-data
1729 ;; First find trees containing a select tag, if any.
1730 (selected (org-export--selected-trees data options))
1731 (walk-data
1732 (lambda (data)
1733 ;; Collect ignored elements or objects into IGNORE-LIST.
1734 (let ((type (org-element-type data)))
1735 (if (org-export--skip-p data options selected) (push data ignore)
1736 (if (and (eq type 'headline)
1737 (eq (plist-get options :with-archived-trees) 'headline)
1738 (org-element-property :archivedp data))
1739 ;; If headline is archived but tree below has
1740 ;; to be skipped, add it to ignore list.
1741 (mapc (lambda (e) (push e ignore))
1742 (org-element-contents data))
1743 ;; Move into secondary string, if any.
1744 (let ((sec-prop
1745 (cdr (assq type org-element-secondary-value-alist))))
1746 (when sec-prop
1747 (mapc walk-data (org-element-property sec-prop data))))
1748 ;; Move into recursive objects/elements.
1749 (mapc walk-data (org-element-contents data))))))))
1750 ;; Main call.
1751 (funcall walk-data data)
1752 ;; Return value.
1753 ignore))
1755 (defun org-export--selected-trees (data info)
1756 "Return list of headlines containing a select tag in their tree.
1757 DATA is parsed data as returned by `org-element-parse-buffer'.
1758 INFO is a plist holding export options."
1759 (let* (selected-trees
1760 walk-data ; for byte-compiler.
1761 (walk-data
1762 (function
1763 (lambda (data genealogy)
1764 (case (org-element-type data)
1765 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1766 (org-element-contents data)))
1767 (headline
1768 (let ((tags (org-element-property :tags data)))
1769 (if (loop for tag in (plist-get info :select-tags)
1770 thereis (member tag tags))
1771 ;; When a select tag is found, mark full
1772 ;; genealogy and every headline within the tree
1773 ;; as acceptable.
1774 (setq selected-trees
1775 (append
1776 genealogy
1777 (org-element-map data 'headline 'identity)
1778 selected-trees))
1779 ;; Else, continue searching in tree, recursively.
1780 (mapc
1781 (lambda (el) (funcall walk-data el (cons data genealogy)))
1782 (org-element-contents data))))))))))
1783 (funcall walk-data data nil) selected-trees))
1785 (defun org-export--skip-p (blob options selected)
1786 "Non-nil when element or object BLOB should be skipped during export.
1787 OPTIONS is the plist holding export options. SELECTED, when
1788 non-nil, is a list of headlines belonging to a tree with a select
1789 tag."
1790 (case (org-element-type blob)
1791 (clock (not (plist-get options :with-clocks)))
1792 (drawer
1793 (or (not (plist-get options :with-drawers))
1794 (and (consp (plist-get options :with-drawers))
1795 (not (member (org-element-property :drawer-name blob)
1796 (plist-get options :with-drawers))))))
1797 (headline
1798 (let ((with-tasks (plist-get options :with-tasks))
1799 (todo (org-element-property :todo-keyword blob))
1800 (todo-type (org-element-property :todo-type blob))
1801 (archived (plist-get options :with-archived-trees))
1802 (tags (org-element-property :tags blob)))
1804 ;; Ignore subtrees with an exclude tag.
1805 (loop for k in (plist-get options :exclude-tags)
1806 thereis (member k tags))
1807 ;; When a select tag is present in the buffer, ignore any tree
1808 ;; without it.
1809 (and selected (not (memq blob selected)))
1810 ;; Ignore commented sub-trees.
1811 (org-element-property :commentedp blob)
1812 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1813 (and (not archived) (org-element-property :archivedp blob))
1814 ;; Ignore tasks, if specified by `:with-tasks' property.
1815 (and todo
1816 (or (not with-tasks)
1817 (and (memq with-tasks '(todo done))
1818 (not (eq todo-type with-tasks)))
1819 (and (consp with-tasks) (not (member todo with-tasks))))))))
1820 (inlinetask (not (plist-get options :with-inlinetasks)))
1821 (planning (not (plist-get options :with-plannings)))
1822 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1823 (table-cell
1824 (and (org-export-table-has-special-column-p
1825 (org-export-get-parent-table blob))
1826 (not (org-export-get-previous-element blob options))))
1827 (table-row (org-export-table-row-is-special-p blob options))
1828 (timestamp
1829 (case (plist-get options :with-timestamps)
1830 ;; No timestamp allowed.
1831 ('nil t)
1832 ;; Only active timestamps allowed and the current one isn't
1833 ;; active.
1834 (active
1835 (not (memq (org-element-property :type blob)
1836 '(active active-range))))
1837 ;; Only inactive timestamps allowed and the current one isn't
1838 ;; inactive.
1839 (inactive
1840 (not (memq (org-element-property :type blob)
1841 '(inactive inactive-range))))))))
1845 ;;; The Transcoder
1847 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1848 ;; `org-element-parse-buffer') and transcodes it into a specified
1849 ;; back-end output. It takes care of filtering out elements or
1850 ;; objects according to export options and organizing the output blank
1851 ;; lines and white space are preserved. The function memoizes its
1852 ;; results, so it is cheap to call it within translators.
1854 ;; Internally, three functions handle the filtering of objects and
1855 ;; elements during the export. In particular,
1856 ;; `org-export-ignore-element' marks an element or object so future
1857 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1858 ;; elements or objects should be seen as real Org syntax and
1859 ;; `org-export-expand' transforms the others back into their original
1860 ;; shape
1862 ;; `org-export-transcoder' is an accessor returning appropriate
1863 ;; translator function for a given element or object.
1865 (defun org-export-transcoder (blob info)
1866 "Return appropriate transcoder for BLOB.
1867 INFO is a plist containing export directives."
1868 (let ((type (org-element-type blob)))
1869 ;; Return contents only for complete parse trees.
1870 (if (eq type 'org-data) (lambda (blob contents info) contents)
1871 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1872 (and (functionp transcoder) transcoder)))))
1874 (defun org-export-data (data info)
1875 "Convert DATA into current back-end format.
1877 DATA is a parse tree, an element or an object or a secondary
1878 string. INFO is a plist holding export options.
1880 Return transcoded string."
1881 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1882 (if (not (eq memo 'no-memo)) memo
1883 (let* ((type (org-element-type data))
1884 (results
1885 (cond
1886 ;; Ignored element/object.
1887 ((memq data (plist-get info :ignore-list)) nil)
1888 ;; Plain text.
1889 ((eq type 'plain-text)
1890 (org-export-filter-apply-functions
1891 (plist-get info :filter-plain-text)
1892 (let ((transcoder (org-export-transcoder data info)))
1893 (if transcoder (funcall transcoder data info) data))
1894 info))
1895 ;; Uninterpreted element/object: change it back to Org
1896 ;; syntax and export again resulting raw string.
1897 ((not (org-export--interpret-p data info))
1898 (org-export-data
1899 (org-export-expand
1900 data
1901 (mapconcat (lambda (blob) (org-export-data blob info))
1902 (org-element-contents data)
1903 ""))
1904 info))
1905 ;; Secondary string.
1906 ((not type)
1907 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1908 ;; Element/Object without contents or, as a special case,
1909 ;; headline with archive tag and archived trees restricted
1910 ;; to title only.
1911 ((or (not (org-element-contents data))
1912 (and (eq type 'headline)
1913 (eq (plist-get info :with-archived-trees) 'headline)
1914 (org-element-property :archivedp data)))
1915 (let ((transcoder (org-export-transcoder data info)))
1916 (and (functionp transcoder)
1917 (funcall transcoder data nil info))))
1918 ;; Element/Object with contents.
1920 (let ((transcoder (org-export-transcoder data info)))
1921 (when transcoder
1922 (let* ((greaterp (memq type org-element-greater-elements))
1923 (objectp
1924 (and (not greaterp)
1925 (memq type org-element-recursive-objects)))
1926 (contents
1927 (mapconcat
1928 (lambda (element) (org-export-data element info))
1929 (org-element-contents
1930 (if (or greaterp objectp) data
1931 ;; Elements directly containing objects
1932 ;; must have their indentation normalized
1933 ;; first.
1934 (org-element-normalize-contents
1935 data
1936 ;; When normalizing contents of the first
1937 ;; paragraph in an item or a footnote
1938 ;; definition, ignore first line's
1939 ;; indentation: there is none and it
1940 ;; might be misleading.
1941 (when (eq type 'paragraph)
1942 (let ((parent (org-export-get-parent data)))
1943 (and
1944 (eq (car (org-element-contents parent))
1945 data)
1946 (memq (org-element-type parent)
1947 '(footnote-definition item))))))))
1948 "")))
1949 (funcall transcoder data
1950 (if (not greaterp) contents
1951 (org-element-normalize-string contents))
1952 info))))))))
1953 ;; Final result will be memoized before being returned.
1954 (puthash
1955 data
1956 (cond
1957 ((not results) nil)
1958 ((memq type '(org-data plain-text nil)) results)
1959 ;; Append the same white space between elements or objects as in
1960 ;; the original buffer, and call appropriate filters.
1962 (let ((results
1963 (org-export-filter-apply-functions
1964 (plist-get info (intern (format ":filter-%s" type)))
1965 (let ((post-blank (or (org-element-property :post-blank data)
1966 0)))
1967 (if (memq type org-element-all-elements)
1968 (concat (org-element-normalize-string results)
1969 (make-string post-blank ?\n))
1970 (concat results (make-string post-blank ? ))))
1971 info)))
1972 results)))
1973 (plist-get info :exported-data))))))
1975 (defun org-export--interpret-p (blob info)
1976 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1977 Check is done according to export options INFO, stored as
1978 a plist."
1979 (case (org-element-type blob)
1980 ;; ... entities...
1981 (entity (plist-get info :with-entities))
1982 ;; ... emphasis...
1983 (emphasis (plist-get info :with-emphasize))
1984 ;; ... fixed-width areas.
1985 (fixed-width (plist-get info :with-fixed-width))
1986 ;; ... footnotes...
1987 ((footnote-definition footnote-reference)
1988 (plist-get info :with-footnotes))
1989 ;; ... sub/superscripts...
1990 ((subscript superscript)
1991 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1992 (if (eq sub/super-p '{})
1993 (org-element-property :use-brackets-p blob)
1994 sub/super-p)))
1995 ;; ... tables...
1996 (table (plist-get info :with-tables))
1997 (otherwise t)))
1999 (defun org-export-expand (blob contents)
2000 "Expand a parsed element or object to its original state.
2001 BLOB is either an element or an object. CONTENTS is its
2002 contents, as a string or nil."
2003 (funcall
2004 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2005 blob contents))
2007 (defun org-export-ignore-element (element info)
2008 "Add ELEMENT to `:ignore-list' in INFO.
2010 Any element in `:ignore-list' will be skipped when using
2011 `org-element-map'. INFO is modified by side effects."
2012 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2016 ;;; The Filter System
2018 ;; Filters allow end-users to tweak easily the transcoded output.
2019 ;; They are the functional counterpart of hooks, as every filter in
2020 ;; a set is applied to the return value of the previous one.
2022 ;; Every set is back-end agnostic. Although, a filter is always
2023 ;; called, in addition to the string it applies to, with the back-end
2024 ;; used as argument, so it's easy for the end-user to add back-end
2025 ;; specific filters in the set. The communication channel, as
2026 ;; a plist, is required as the third argument.
2028 ;; From the developer side, filters sets can be installed in the
2029 ;; process with the help of `org-export-define-backend', which
2030 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2031 ;; association has a key among the following symbols and a function or
2032 ;; a list of functions as value.
2034 ;; - `:filter-parse-tree' applies directly on the complete parsed
2035 ;; tree. It's the only filters set that doesn't apply to a string.
2036 ;; Users can set it through `org-export-filter-parse-tree-functions'
2037 ;; variable.
2039 ;; - `:filter-final-output' applies to the final transcoded string.
2040 ;; Users can set it with `org-export-filter-final-output-functions'
2041 ;; variable
2043 ;; - `:filter-plain-text' applies to any string not recognized as Org
2044 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2045 ;; configure it.
2047 ;; - `:filter-TYPE' applies on the string returned after an element or
2048 ;; object of type TYPE has been transcoded. An user can modify
2049 ;; `org-export-filter-TYPE-functions'
2051 ;; All filters sets are applied with
2052 ;; `org-export-filter-apply-functions' function. Filters in a set are
2053 ;; applied in a LIFO fashion. It allows developers to be sure that
2054 ;; their filters will be applied first.
2056 ;; Filters properties are installed in communication channel with
2057 ;; `org-export-install-filters' function.
2059 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2060 ;; before parsing to allow for heavy structure modifications.
2063 ;;;; Before Parsing Hook
2065 (defvar org-export-before-parsing-hook nil
2066 "Hook run before parsing an export buffer.
2068 This is run after include keywords have been expanded and Babel
2069 code executed, on a copy of original buffer's area being
2070 exported. Visibility is the same as in the original one. Point
2071 is left at the beginning of the new one.
2073 Every function in this hook will be called with one argument: the
2074 back-end currently used, as a symbol.")
2077 ;;;; Special Filters
2079 (defvar org-export-filter-parse-tree-functions nil
2080 "List of functions applied to the parsed tree.
2081 Each filter is called with three arguments: the parse tree, as
2082 returned by `org-element-parse-buffer', the back-end, as
2083 a symbol, and the communication channel, as a plist. It must
2084 return the modified parse tree to transcode.")
2086 (defvar org-export-filter-final-output-functions nil
2087 "List of functions applied to the transcoded string.
2088 Each filter is called with three arguments: the full transcoded
2089 string, the back-end, as a symbol, and the communication channel,
2090 as a plist. It must return a string that will be used as the
2091 final export output.")
2093 (defvar org-export-filter-plain-text-functions nil
2094 "List of functions applied to plain text.
2095 Each filter is called with three arguments: a string which
2096 contains no Org syntax, the back-end, as a symbol, and the
2097 communication channel, as a plist. It must return a string or
2098 nil.")
2101 ;;;; Elements Filters
2103 (defvar org-export-filter-center-block-functions nil
2104 "List of functions applied to a transcoded center block.
2105 Each filter is called with three arguments: the transcoded data,
2106 as a string, the back-end, as a symbol, and the communication
2107 channel, as a plist. It must return a string or nil.")
2109 (defvar org-export-filter-clock-functions nil
2110 "List of functions applied to a transcoded clock.
2111 Each filter is called with three arguments: the transcoded data,
2112 as a string, the back-end, as a symbol, and the communication
2113 channel, as a plist. It must return a string or nil.")
2115 (defvar org-export-filter-drawer-functions nil
2116 "List of functions applied to a transcoded drawer.
2117 Each filter is called with three arguments: the transcoded data,
2118 as a string, the back-end, as a symbol, and the communication
2119 channel, as a plist. It must return a string or nil.")
2121 (defvar org-export-filter-dynamic-block-functions nil
2122 "List of functions applied to a transcoded dynamic-block.
2123 Each filter is called with three arguments: the transcoded data,
2124 as a string, the back-end, as a symbol, and the communication
2125 channel, as a plist. It must return a string or nil.")
2127 (defvar org-export-filter-headline-functions nil
2128 "List of functions applied to a transcoded headline.
2129 Each filter is called with three arguments: the transcoded data,
2130 as a string, the back-end, as a symbol, and the communication
2131 channel, as a plist. It must return a string or nil.")
2133 (defvar org-export-filter-inlinetask-functions nil
2134 "List of functions applied to a transcoded inlinetask.
2135 Each filter is called with three arguments: the transcoded data,
2136 as a string, the back-end, as a symbol, and the communication
2137 channel, as a plist. It must return a string or nil.")
2139 (defvar org-export-filter-plain-list-functions nil
2140 "List of functions applied to a transcoded plain-list.
2141 Each filter is called with three arguments: the transcoded data,
2142 as a string, the back-end, as a symbol, and the communication
2143 channel, as a plist. It must return a string or nil.")
2145 (defvar org-export-filter-item-functions nil
2146 "List of functions applied to a transcoded item.
2147 Each filter is called with three arguments: the transcoded data,
2148 as a string, the back-end, as a symbol, and the communication
2149 channel, as a plist. It must return a string or nil.")
2151 (defvar org-export-filter-comment-functions nil
2152 "List of functions applied to a transcoded comment.
2153 Each filter is called with three arguments: the transcoded data,
2154 as a string, the back-end, as a symbol, and the communication
2155 channel, as a plist. It must return a string or nil.")
2157 (defvar org-export-filter-comment-block-functions nil
2158 "List of functions applied to a transcoded comment-comment.
2159 Each filter is called with three arguments: the transcoded data,
2160 as a string, the back-end, as a symbol, and the communication
2161 channel, as a plist. It must return a string or nil.")
2163 (defvar org-export-filter-example-block-functions nil
2164 "List of functions applied to a transcoded example-block.
2165 Each filter is called with three arguments: the transcoded data,
2166 as a string, the back-end, as a symbol, and the communication
2167 channel, as a plist. It must return a string or nil.")
2169 (defvar org-export-filter-export-block-functions nil
2170 "List of functions applied to a transcoded export-block.
2171 Each filter is called with three arguments: the transcoded data,
2172 as a string, the back-end, as a symbol, and the communication
2173 channel, as a plist. It must return a string or nil.")
2175 (defvar org-export-filter-fixed-width-functions nil
2176 "List of functions applied to a transcoded fixed-width.
2177 Each filter is called with three arguments: the transcoded data,
2178 as a string, the back-end, as a symbol, and the communication
2179 channel, as a plist. It must return a string or nil.")
2181 (defvar org-export-filter-footnote-definition-functions nil
2182 "List of functions applied to a transcoded footnote-definition.
2183 Each filter is called with three arguments: the transcoded data,
2184 as a string, the back-end, as a symbol, and the communication
2185 channel, as a plist. It must return a string or nil.")
2187 (defvar org-export-filter-horizontal-rule-functions nil
2188 "List of functions applied to a transcoded horizontal-rule.
2189 Each filter is called with three arguments: the transcoded data,
2190 as a string, the back-end, as a symbol, and the communication
2191 channel, as a plist. It must return a string or nil.")
2193 (defvar org-export-filter-keyword-functions nil
2194 "List of functions applied to a transcoded keyword.
2195 Each filter is called with three arguments: the transcoded data,
2196 as a string, the back-end, as a symbol, and the communication
2197 channel, as a plist. It must return a string or nil.")
2199 (defvar org-export-filter-latex-environment-functions nil
2200 "List of functions applied to a transcoded latex-environment.
2201 Each filter is called with three arguments: the transcoded data,
2202 as a string, the back-end, as a symbol, and the communication
2203 channel, as a plist. It must return a string or nil.")
2205 (defvar org-export-filter-babel-call-functions nil
2206 "List of functions applied to a transcoded babel-call.
2207 Each filter is called with three arguments: the transcoded data,
2208 as a string, the back-end, as a symbol, and the communication
2209 channel, as a plist. It must return a string or nil.")
2211 (defvar org-export-filter-node-property-functions nil
2212 "List of functions applied to a transcoded node-property.
2213 Each filter is called with three arguments: the transcoded data,
2214 as a string, the back-end, as a symbol, and the communication
2215 channel, as a plist. It must return a string or nil.")
2217 (defvar org-export-filter-paragraph-functions nil
2218 "List of functions applied to a transcoded paragraph.
2219 Each filter is called with three arguments: the transcoded data,
2220 as a string, the back-end, as a symbol, and the communication
2221 channel, as a plist. It must return a string or nil.")
2223 (defvar org-export-filter-planning-functions nil
2224 "List of functions applied to a transcoded planning.
2225 Each filter is called with three arguments: the transcoded data,
2226 as a string, the back-end, as a symbol, and the communication
2227 channel, as a plist. It must return a string or nil.")
2229 (defvar org-export-filter-property-drawer-functions nil
2230 "List of functions applied to a transcoded property-drawer.
2231 Each filter is called with three arguments: the transcoded data,
2232 as a string, the back-end, as a symbol, and the communication
2233 channel, as a plist. It must return a string or nil.")
2235 (defvar org-export-filter-quote-block-functions nil
2236 "List of functions applied to a transcoded quote block.
2237 Each filter is called with three arguments: the transcoded quote
2238 data, as a string, the back-end, as a symbol, and the
2239 communication channel, as a plist. It must return a string or
2240 nil.")
2242 (defvar org-export-filter-quote-section-functions nil
2243 "List of functions applied to a transcoded quote-section.
2244 Each filter is called with three arguments: the transcoded data,
2245 as a string, the back-end, as a symbol, and the communication
2246 channel, as a plist. It must return a string or nil.")
2248 (defvar org-export-filter-section-functions nil
2249 "List of functions applied to a transcoded section.
2250 Each filter is called with three arguments: the transcoded data,
2251 as a string, the back-end, as a symbol, and the communication
2252 channel, as a plist. It must return a string or nil.")
2254 (defvar org-export-filter-special-block-functions nil
2255 "List of functions applied to a transcoded special block.
2256 Each filter is called with three arguments: the transcoded data,
2257 as a string, the back-end, as a symbol, and the communication
2258 channel, as a plist. It must return a string or nil.")
2260 (defvar org-export-filter-src-block-functions nil
2261 "List of functions applied to a transcoded src-block.
2262 Each filter is called with three arguments: the transcoded data,
2263 as a string, the back-end, as a symbol, and the communication
2264 channel, as a plist. It must return a string or nil.")
2266 (defvar org-export-filter-table-functions nil
2267 "List of functions applied to a transcoded table.
2268 Each filter is called with three arguments: the transcoded data,
2269 as a string, the back-end, as a symbol, and the communication
2270 channel, as a plist. It must return a string or nil.")
2272 (defvar org-export-filter-table-cell-functions nil
2273 "List of functions applied to a transcoded table-cell.
2274 Each filter is called with three arguments: the transcoded data,
2275 as a string, the back-end, as a symbol, and the communication
2276 channel, as a plist. It must return a string or nil.")
2278 (defvar org-export-filter-table-row-functions nil
2279 "List of functions applied to a transcoded table-row.
2280 Each filter is called with three arguments: the transcoded data,
2281 as a string, the back-end, as a symbol, and the communication
2282 channel, as a plist. It must return a string or nil.")
2284 (defvar org-export-filter-verse-block-functions nil
2285 "List of functions applied to a transcoded verse block.
2286 Each filter is called with three arguments: the transcoded data,
2287 as a string, the back-end, as a symbol, and the communication
2288 channel, as a plist. It must return a string or nil.")
2291 ;;;; Objects Filters
2293 (defvar org-export-filter-bold-functions nil
2294 "List of functions applied to transcoded bold text.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2299 (defvar org-export-filter-code-functions nil
2300 "List of functions applied to transcoded code text.
2301 Each filter is called with three arguments: the transcoded data,
2302 as a string, the back-end, as a symbol, and the communication
2303 channel, as a plist. It must return a string or nil.")
2305 (defvar org-export-filter-entity-functions nil
2306 "List of functions applied to a transcoded entity.
2307 Each filter is called with three arguments: the transcoded data,
2308 as a string, the back-end, as a symbol, and the communication
2309 channel, as a plist. It must return a string or nil.")
2311 (defvar org-export-filter-export-snippet-functions nil
2312 "List of functions applied to a transcoded export-snippet.
2313 Each filter is called with three arguments: the transcoded data,
2314 as a string, the back-end, as a symbol, and the communication
2315 channel, as a plist. It must return a string or nil.")
2317 (defvar org-export-filter-footnote-reference-functions nil
2318 "List of functions applied to a transcoded footnote-reference.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-inline-babel-call-functions nil
2324 "List of functions applied to a transcoded inline-babel-call.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-inline-src-block-functions nil
2330 "List of functions applied to a transcoded inline-src-block.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-italic-functions nil
2336 "List of functions applied to transcoded italic text.
2337 Each filter is called with three arguments: the transcoded data,
2338 as a string, the back-end, as a symbol, and the communication
2339 channel, as a plist. It must return a string or nil.")
2341 (defvar org-export-filter-latex-fragment-functions nil
2342 "List of functions applied to a transcoded latex-fragment.
2343 Each filter is called with three arguments: the transcoded data,
2344 as a string, the back-end, as a symbol, and the communication
2345 channel, as a plist. It must return a string or nil.")
2347 (defvar org-export-filter-line-break-functions nil
2348 "List of functions applied to a transcoded line-break.
2349 Each filter is called with three arguments: the transcoded data,
2350 as a string, the back-end, as a symbol, and the communication
2351 channel, as a plist. It must return a string or nil.")
2353 (defvar org-export-filter-link-functions nil
2354 "List of functions applied to a transcoded link.
2355 Each filter is called with three arguments: the transcoded data,
2356 as a string, the back-end, as a symbol, and the communication
2357 channel, as a plist. It must return a string or nil.")
2359 (defvar org-export-filter-macro-functions nil
2360 "List of functions applied to a transcoded macro.
2361 Each filter is called with three arguments: the transcoded data,
2362 as a string, the back-end, as a symbol, and the communication
2363 channel, as a plist. It must return a string or nil.")
2365 (defvar org-export-filter-radio-target-functions nil
2366 "List of functions applied to a transcoded radio-target.
2367 Each filter is called with three arguments: the transcoded data,
2368 as a string, the back-end, as a symbol, and the communication
2369 channel, as a plist. It must return a string or nil.")
2371 (defvar org-export-filter-statistics-cookie-functions nil
2372 "List of functions applied to a transcoded statistics-cookie.
2373 Each filter is called with three arguments: the transcoded data,
2374 as a string, the back-end, as a symbol, and the communication
2375 channel, as a plist. It must return a string or nil.")
2377 (defvar org-export-filter-strike-through-functions nil
2378 "List of functions applied to transcoded strike-through text.
2379 Each filter is called with three arguments: the transcoded data,
2380 as a string, the back-end, as a symbol, and the communication
2381 channel, as a plist. It must return a string or nil.")
2383 (defvar org-export-filter-subscript-functions nil
2384 "List of functions applied to a transcoded subscript.
2385 Each filter is called with three arguments: the transcoded data,
2386 as a string, the back-end, as a symbol, and the communication
2387 channel, as a plist. It must return a string or nil.")
2389 (defvar org-export-filter-superscript-functions nil
2390 "List of functions applied to a transcoded superscript.
2391 Each filter is called with three arguments: the transcoded data,
2392 as a string, the back-end, as a symbol, and the communication
2393 channel, as a plist. It must return a string or nil.")
2395 (defvar org-export-filter-target-functions nil
2396 "List of functions applied to a transcoded target.
2397 Each filter is called with three arguments: the transcoded data,
2398 as a string, the back-end, as a symbol, and the communication
2399 channel, as a plist. It must return a string or nil.")
2401 (defvar org-export-filter-timestamp-functions nil
2402 "List of functions applied to a transcoded timestamp.
2403 Each filter is called with three arguments: the transcoded data,
2404 as a string, the back-end, as a symbol, and the communication
2405 channel, as a plist. It must return a string or nil.")
2407 (defvar org-export-filter-underline-functions nil
2408 "List of functions applied to transcoded underline text.
2409 Each filter is called with three arguments: the transcoded data,
2410 as a string, the back-end, as a symbol, and the communication
2411 channel, as a plist. It must return a string or nil.")
2413 (defvar org-export-filter-verbatim-functions nil
2414 "List of functions applied to transcoded verbatim text.
2415 Each filter is called with three arguments: the transcoded data,
2416 as a string, the back-end, as a symbol, and the communication
2417 channel, as a plist. It must return a string or nil.")
2420 ;;;; Filters Tools
2422 ;; Internal function `org-export-install-filters' installs filters
2423 ;; hard-coded in back-ends (developer filters) and filters from global
2424 ;; variables (user filters) in the communication channel.
2426 ;; Internal function `org-export-filter-apply-functions' takes care
2427 ;; about applying each filter in order to a given data. It ignores
2428 ;; filters returning a nil value but stops whenever a filter returns
2429 ;; an empty string.
2431 (defun org-export-filter-apply-functions (filters value info)
2432 "Call every function in FILTERS.
2434 Functions are called with arguments VALUE, current export
2435 back-end and INFO. A function returning a nil value will be
2436 skipped. If it returns the empty string, the process ends and
2437 VALUE is ignored.
2439 Call is done in a LIFO fashion, to be sure that developer
2440 specified filters, if any, are called first."
2441 (catch 'exit
2442 (dolist (filter filters value)
2443 (let ((result (funcall filter value (plist-get info :back-end) info)))
2444 (cond ((not result) value)
2445 ((equal value "") (throw 'exit nil))
2446 (t (setq value result)))))))
2448 (defun org-export-install-filters (info)
2449 "Install filters properties in communication channel.
2451 INFO is a plist containing the current communication channel.
2453 Return the updated communication channel."
2454 (let (plist)
2455 ;; Install user defined filters with `org-export-filters-alist'.
2456 (mapc (lambda (p)
2457 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2458 org-export-filters-alist)
2459 ;; Prepend back-end specific filters to that list.
2460 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2461 (plist-get info :back-end)))))
2462 (when (boundp back-end-filters)
2463 (mapc (lambda (p)
2464 ;; Single values get consed, lists are prepended.
2465 (let ((key (car p)) (value (cdr p)))
2466 (when value
2467 (setq plist
2468 (plist-put
2469 plist key
2470 (if (atom value) (cons value (plist-get plist key))
2471 (append value (plist-get plist key))))))))
2472 (eval back-end-filters))))
2473 ;; Return new communication channel.
2474 (org-combine-plists info plist)))
2478 ;;; Core functions
2480 ;; This is the room for the main function, `org-export-as', along with
2481 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2482 ;; They differ only by the way they output the resulting code.
2484 ;; `org-export-output-file-name' is an auxiliary function meant to be
2485 ;; used with `org-export-to-file'. With a given extension, it tries
2486 ;; to provide a canonical file name to write export output to.
2488 ;; Note that `org-export-as' doesn't really parse the current buffer,
2489 ;; but a copy of it (with the same buffer-local variables and
2490 ;; visibility), where macros and include keywords are expanded and
2491 ;; Babel blocks are executed, if appropriate.
2492 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2494 ;; File inclusion is taken care of by
2495 ;; `org-export-expand-include-keyword' and
2496 ;; `org-export--prepare-file-contents'. Structure wise, including
2497 ;; a whole Org file in a buffer often makes little sense. For
2498 ;; example, if the file contains an headline and the include keyword
2499 ;; was within an item, the item should contain the headline. That's
2500 ;; why file inclusion should be done before any structure can be
2501 ;; associated to the file, that is before parsing.
2503 (defun org-export-as
2504 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2505 "Transcode current Org buffer into BACKEND code.
2507 If narrowing is active in the current buffer, only transcode its
2508 narrowed part.
2510 If a region is active, transcode that region.
2512 When optional argument SUBTREEP is non-nil, transcode the
2513 sub-tree at point, extracting information from the headline
2514 properties first.
2516 When optional argument VISIBLE-ONLY is non-nil, don't export
2517 contents of hidden elements.
2519 When optional argument BODY-ONLY is non-nil, only return body
2520 code, without preamble nor postamble.
2522 Optional argument EXT-PLIST, when provided, is a property list
2523 with external parameters overriding Org default settings, but
2524 still inferior to file-local settings.
2526 Optional argument NOEXPAND, when non-nil, prevents included files
2527 to be expanded and Babel code to be executed.
2529 Return code as a string."
2530 (save-excursion
2531 (save-restriction
2532 ;; Narrow buffer to an appropriate region or subtree for
2533 ;; parsing. If parsing subtree, be sure to remove main headline
2534 ;; too.
2535 (cond ((org-region-active-p)
2536 (narrow-to-region (region-beginning) (region-end)))
2537 (subtreep
2538 (org-narrow-to-subtree)
2539 (goto-char (point-min))
2540 (forward-line)
2541 (narrow-to-region (point) (point-max))))
2542 ;; 1. Get export environment from original buffer. Also install
2543 ;; user's and developer's filters.
2544 (let ((info (org-export-install-filters
2545 (org-export-get-environment backend subtreep ext-plist)))
2546 ;; 2. Get parse tree. Buffer isn't parsed directly.
2547 ;; Instead, a temporary copy is created, where macros
2548 ;; and include keywords are expanded and code blocks
2549 ;; are evaluated.
2550 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2551 (current-buffer))))
2552 (org-export-with-current-buffer-copy
2553 (unless noexpand
2554 (org-macro-replace-all)
2555 (org-export-expand-include-keyword)
2556 ;; TODO: Setting `org-current-export-file' is
2557 ;; required by Org Babel to properly resolve
2558 ;; noweb references. Once "org-exp.el" is
2559 ;; removed, modify
2560 ;; `org-export-blocks-preprocess' so it accepts
2561 ;; the value as an argument instead.
2562 (let ((org-current-export-file buf))
2563 (org-export-blocks-preprocess)))
2564 (goto-char (point-min))
2565 ;; Run hook
2566 ;; `org-export-before-parsing-hook'. with current
2567 ;; back-end as argument.
2568 (run-hook-with-args
2569 'org-export-before-parsing-hook backend)
2570 ;; Eventually parse buffer.
2571 (org-element-parse-buffer nil visible-only)))))
2572 ;; 3. Call parse-tree filters to get the final tree.
2573 (setq tree
2574 (org-export-filter-apply-functions
2575 (plist-get info :filter-parse-tree) tree info))
2576 ;; 4. Now tree is complete, compute its properties and add
2577 ;; them to communication channel.
2578 (setq info
2579 (org-combine-plists
2580 info (org-export-collect-tree-properties tree info)))
2581 ;; 5. Eventually transcode TREE. Wrap the resulting string
2582 ;; into a template, if required. Eventually call
2583 ;; final-output filter.
2584 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2585 (template (cdr (assq 'template
2586 (plist-get info :translate-alist))))
2587 (output (org-export-filter-apply-functions
2588 (plist-get info :filter-final-output)
2589 (if (or (not (functionp template)) body-only) body
2590 (funcall template body info))
2591 info)))
2592 ;; Maybe add final OUTPUT to kill ring, then return it.
2593 (when org-export-copy-to-kill-ring (org-kill-new output))
2594 output)))))
2596 (defun org-export-to-buffer
2597 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2598 "Call `org-export-as' with output to a specified buffer.
2600 BACKEND is the back-end used for transcoding, as a symbol.
2602 BUFFER is the output buffer. If it already exists, it will be
2603 erased first, otherwise, it will be created.
2605 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2606 and NOEXPAND are similar to those used in `org-export-as', which
2607 see.
2609 Return buffer."
2610 (let ((out (org-export-as
2611 backend subtreep visible-only body-only ext-plist noexpand))
2612 (buffer (get-buffer-create buffer)))
2613 (with-current-buffer buffer
2614 (erase-buffer)
2615 (insert out)
2616 (goto-char (point-min)))
2617 buffer))
2619 (defun org-export-to-file
2620 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2621 "Call `org-export-as' with output to a specified file.
2623 BACKEND is the back-end used for transcoding, as a symbol. FILE
2624 is the name of the output file, as a string.
2626 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2627 and NOEXPAND are similar to those used in `org-export-as', which
2628 see.
2630 Return output file's name."
2631 ;; Checks for FILE permissions. `write-file' would do the same, but
2632 ;; we'd rather avoid needless transcoding of parse tree.
2633 (unless (file-writable-p file) (error "Output file not writable"))
2634 ;; Insert contents to a temporary buffer and write it to FILE.
2635 (let ((out (org-export-as
2636 backend subtreep visible-only body-only ext-plist noexpand)))
2637 (with-temp-buffer
2638 (insert out)
2639 (let ((coding-system-for-write org-export-coding-system))
2640 (write-file file))))
2641 ;; Return full path.
2642 file)
2644 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2645 "Return output file's name according to buffer specifications.
2647 EXTENSION is a string representing the output file extension,
2648 with the leading dot.
2650 With a non-nil optional argument SUBTREEP, try to determine
2651 output file's name by looking for \"EXPORT_FILE_NAME\" property
2652 of subtree at point.
2654 When optional argument PUB-DIR is set, use it as the publishing
2655 directory.
2657 When optional argument VISIBLE-ONLY is non-nil, don't export
2658 contents of hidden elements.
2660 Return file name as a string, or nil if it couldn't be
2661 determined."
2662 (let ((base-name
2663 ;; File name may come from EXPORT_FILE_NAME subtree property,
2664 ;; assuming point is at beginning of said sub-tree.
2665 (file-name-sans-extension
2666 (or (and subtreep
2667 (org-entry-get
2668 (save-excursion
2669 (ignore-errors (org-back-to-heading) (point)))
2670 "EXPORT_FILE_NAME" t))
2671 ;; File name may be extracted from buffer's associated
2672 ;; file, if any.
2673 (buffer-file-name (buffer-base-buffer))
2674 ;; Can't determine file name on our own: Ask user.
2675 (let ((read-file-name-function
2676 (and org-completion-use-ido 'ido-read-file-name)))
2677 (read-file-name
2678 "Output file: " pub-dir nil nil nil
2679 (lambda (name)
2680 (string= (file-name-extension name t) extension))))))))
2681 ;; Build file name. Enforce EXTENSION over whatever user may have
2682 ;; come up with. PUB-DIR, if defined, always has precedence over
2683 ;; any provided path.
2684 (cond
2685 (pub-dir
2686 (concat (file-name-as-directory pub-dir)
2687 (file-name-nondirectory base-name)
2688 extension))
2689 ((string= (file-name-nondirectory base-name) base-name)
2690 (concat (file-name-as-directory ".") base-name extension))
2691 (t (concat base-name extension)))))
2693 (defmacro org-export-with-current-buffer-copy (&rest body)
2694 "Apply BODY in a copy of the current buffer.
2696 The copy preserves local variables and visibility of the original
2697 buffer.
2699 Point is at buffer's beginning when BODY is applied."
2700 (org-with-gensyms (original-buffer offset buffer-string overlays)
2701 `(let ((,original-buffer (current-buffer))
2702 (,offset (1- (point-min)))
2703 (,buffer-string (buffer-string))
2704 (,overlays (mapcar
2705 'copy-overlay (overlays-in (point-min) (point-max)))))
2706 (with-temp-buffer
2707 (let ((buffer-invisibility-spec nil))
2708 (org-clone-local-variables
2709 ,original-buffer
2710 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2711 (insert ,buffer-string)
2712 (mapc (lambda (ov)
2713 (move-overlay
2715 (- (overlay-start ov) ,offset)
2716 (- (overlay-end ov) ,offset)
2717 (current-buffer)))
2718 ,overlays)
2719 (goto-char (point-min))
2720 (progn ,@body))))))
2721 (def-edebug-spec org-export-with-current-buffer-copy (body))
2723 (defun org-export-expand-include-keyword (&optional included dir)
2724 "Expand every include keyword in buffer.
2725 Optional argument INCLUDED is a list of included file names along
2726 with their line restriction, when appropriate. It is used to
2727 avoid infinite recursion. Optional argument DIR is the current
2728 working directory. It is used to properly resolve relative
2729 paths."
2730 (let ((case-fold-search t))
2731 (goto-char (point-min))
2732 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2733 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2734 'keyword)
2735 (beginning-of-line)
2736 ;; Extract arguments from keyword's value.
2737 (let* ((value (match-string 1))
2738 (ind (org-get-indentation))
2739 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2740 (prog1 (expand-file-name (match-string 1 value) dir)
2741 (setq value (replace-match "" nil nil value)))))
2742 (lines
2743 (and (string-match
2744 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2745 (prog1 (match-string 1 value)
2746 (setq value (replace-match "" nil nil value)))))
2747 (env (cond ((string-match "\\<example\\>" value) 'example)
2748 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2749 (match-string 1 value))))
2750 ;; Minimal level of included file defaults to the child
2751 ;; level of the current headline, if any, or one. It
2752 ;; only applies is the file is meant to be included as
2753 ;; an Org one.
2754 (minlevel
2755 (and (not env)
2756 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2757 (prog1 (string-to-number (match-string 1 value))
2758 (setq value (replace-match "" nil nil value)))
2759 (let ((cur (org-current-level)))
2760 (if cur (1+ (org-reduced-level cur)) 1))))))
2761 ;; Remove keyword.
2762 (delete-region (point) (progn (forward-line) (point)))
2763 (cond
2764 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2765 ;; Check if files has already been parsed. Look after
2766 ;; inclusion lines too, as different parts of the same file
2767 ;; can be included too.
2768 ((member (list file lines) included)
2769 (error "Recursive file inclusion: %s" file))
2771 (cond
2772 ((eq env 'example)
2773 (insert
2774 (let ((ind-str (make-string ind ? ))
2775 (contents
2776 ;; Protect sensitive contents with commas.
2777 (replace-regexp-in-string
2778 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2779 (org-export--prepare-file-contents file lines)
2780 nil nil 1)))
2781 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2782 ind-str contents ind-str))))
2783 ((stringp env)
2784 (insert
2785 (let ((ind-str (make-string ind ? ))
2786 (contents
2787 ;; Protect sensitive contents with commas.
2788 (replace-regexp-in-string
2789 (if (string= env "org") "\\(^\\)\\(.\\)"
2790 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2791 (org-export--prepare-file-contents file lines)
2792 nil nil 1)))
2793 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2794 ind-str env contents ind-str))))
2796 (insert
2797 (with-temp-buffer
2798 (org-mode)
2799 (insert
2800 (org-export--prepare-file-contents file lines ind minlevel))
2801 (org-export-expand-include-keyword
2802 (cons (list file lines) included)
2803 (file-name-directory file))
2804 (buffer-string))))))))))))
2806 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2807 "Prepare the contents of FILE for inclusion and return them as a string.
2809 When optional argument LINES is a string specifying a range of
2810 lines, include only those lines.
2812 Optional argument IND, when non-nil, is an integer specifying the
2813 global indentation of returned contents. Since its purpose is to
2814 allow an included file to stay in the same environment it was
2815 created \(i.e. a list item), it doesn't apply past the first
2816 headline encountered.
2818 Optional argument MINLEVEL, when non-nil, is an integer
2819 specifying the level that any top-level headline in the included
2820 file should have."
2821 (with-temp-buffer
2822 (insert-file-contents file)
2823 (when lines
2824 (let* ((lines (split-string lines "-"))
2825 (lbeg (string-to-number (car lines)))
2826 (lend (string-to-number (cadr lines)))
2827 (beg (if (zerop lbeg) (point-min)
2828 (goto-char (point-min))
2829 (forward-line (1- lbeg))
2830 (point)))
2831 (end (if (zerop lend) (point-max)
2832 (goto-char (point-min))
2833 (forward-line (1- lend))
2834 (point))))
2835 (narrow-to-region beg end)))
2836 ;; Remove blank lines at beginning and end of contents. The logic
2837 ;; behind that removal is that blank lines around include keyword
2838 ;; override blank lines in included file.
2839 (goto-char (point-min))
2840 (org-skip-whitespace)
2841 (beginning-of-line)
2842 (delete-region (point-min) (point))
2843 (goto-char (point-max))
2844 (skip-chars-backward " \r\t\n")
2845 (forward-line)
2846 (delete-region (point) (point-max))
2847 ;; If IND is set, preserve indentation of include keyword until
2848 ;; the first headline encountered.
2849 (when ind
2850 (unless (eq major-mode 'org-mode) (org-mode))
2851 (goto-char (point-min))
2852 (let ((ind-str (make-string ind ? )))
2853 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2854 ;; Do not move footnote definitions out of column 0.
2855 (unless (and (looking-at org-footnote-definition-re)
2856 (eq (org-element-type (org-element-at-point))
2857 'footnote-definition))
2858 (insert ind-str))
2859 (forward-line))))
2860 ;; When MINLEVEL is specified, compute minimal level for headlines
2861 ;; in the file (CUR-MIN), and remove stars to each headline so
2862 ;; that headlines with minimal level have a level of MINLEVEL.
2863 (when minlevel
2864 (unless (eq major-mode 'org-mode) (org-mode))
2865 (let ((levels (org-map-entries
2866 (lambda () (org-reduced-level (org-current-level))))))
2867 (when levels
2868 (let ((offset (- minlevel (apply 'min levels))))
2869 (unless (zerop offset)
2870 (when org-odd-levels-only (setq offset (* offset 2)))
2871 ;; Only change stars, don't bother moving whole
2872 ;; sections.
2873 (org-map-entries
2874 (lambda () (if (< offset 0) (delete-char (abs offset))
2875 (insert (make-string offset ?*))))))))))
2876 (buffer-string)))
2879 ;;; Tools For Back-Ends
2881 ;; A whole set of tools is available to help build new exporters. Any
2882 ;; function general enough to have its use across many back-ends
2883 ;; should be added here.
2885 ;;;; For Affiliated Keywords
2887 ;; `org-export-read-attribute' reads a property from a given element
2888 ;; as a plist. It can be used to normalize affiliated keywords'
2889 ;; syntax.
2891 ;; Since captions can span over multiple lines and accept dual values,
2892 ;; their internal representation is a bit tricky. Therefore,
2893 ;; `org-export-get-caption' transparently returns a given element's
2894 ;; caption as a secondary string.
2896 (defun org-export-read-attribute (attribute element &optional property)
2897 "Turn ATTRIBUTE property from ELEMENT into a plist.
2899 When optional argument PROPERTY is non-nil, return the value of
2900 that property within attributes.
2902 This function assumes attributes are defined as \":keyword
2903 value\" pairs. It is appropriate for `:attr_html' like
2904 properties."
2905 (let ((attributes
2906 (let ((value (org-element-property attribute element)))
2907 (and value
2908 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2909 (if property (plist-get attributes property) attributes)))
2911 (defun org-export-get-caption (element &optional shortp)
2912 "Return caption from ELEMENT as a secondary string.
2914 When optional argument SHORTP is non-nil, return short caption,
2915 as a secondary string, instead.
2917 Caption lines are separated by a white space."
2918 (let ((full-caption (org-element-property :caption element)) caption)
2919 (dolist (line full-caption (cdr caption))
2920 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2921 (when cap
2922 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2925 ;;;; For Export Snippets
2927 ;; Every export snippet is transmitted to the back-end. Though, the
2928 ;; latter will only retain one type of export-snippet, ignoring
2929 ;; others, based on the former's target back-end. The function
2930 ;; `org-export-snippet-backend' returns that back-end for a given
2931 ;; export-snippet.
2933 (defun org-export-snippet-backend (export-snippet)
2934 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2935 Translation, with `org-export-snippet-translation-alist', is
2936 applied."
2937 (let ((back-end (org-element-property :back-end export-snippet)))
2938 (intern
2939 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2940 back-end))))
2943 ;;;; For Footnotes
2945 ;; `org-export-collect-footnote-definitions' is a tool to list
2946 ;; actually used footnotes definitions in the whole parse tree, or in
2947 ;; an headline, in order to add footnote listings throughout the
2948 ;; transcoded data.
2950 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2951 ;; back-ends, when they need to attach the footnote definition only to
2952 ;; the first occurrence of the corresponding label.
2954 ;; `org-export-get-footnote-definition' and
2955 ;; `org-export-get-footnote-number' provide easier access to
2956 ;; additional information relative to a footnote reference.
2958 (defun org-export-collect-footnote-definitions (data info)
2959 "Return an alist between footnote numbers, labels and definitions.
2961 DATA is the parse tree from which definitions are collected.
2962 INFO is the plist used as a communication channel.
2964 Definitions are sorted by order of references. They either
2965 appear as Org data or as a secondary string for inlined
2966 footnotes. Unreferenced definitions are ignored."
2967 (let* (num-alist
2968 collect-fn ; for byte-compiler.
2969 (collect-fn
2970 (function
2971 (lambda (data)
2972 ;; Collect footnote number, label and definition in DATA.
2973 (org-element-map
2974 data 'footnote-reference
2975 (lambda (fn)
2976 (when (org-export-footnote-first-reference-p fn info)
2977 (let ((def (org-export-get-footnote-definition fn info)))
2978 (push
2979 (list (org-export-get-footnote-number fn info)
2980 (org-element-property :label fn)
2981 def)
2982 num-alist)
2983 ;; Also search in definition for nested footnotes.
2984 (when (eq (org-element-property :type fn) 'standard)
2985 (funcall collect-fn def)))))
2986 ;; Don't enter footnote definitions since it will happen
2987 ;; when their first reference is found.
2988 info nil 'footnote-definition)))))
2989 (funcall collect-fn (plist-get info :parse-tree))
2990 (reverse num-alist)))
2992 (defun org-export-footnote-first-reference-p (footnote-reference info)
2993 "Non-nil when a footnote reference is the first one for its label.
2995 FOOTNOTE-REFERENCE is the footnote reference being considered.
2996 INFO is the plist used as a communication channel."
2997 (let ((label (org-element-property :label footnote-reference)))
2998 ;; Anonymous footnotes are always a first reference.
2999 (if (not label) t
3000 ;; Otherwise, return the first footnote with the same LABEL and
3001 ;; test if it is equal to FOOTNOTE-REFERENCE.
3002 (let* (search-refs ; for byte-compiler.
3003 (search-refs
3004 (function
3005 (lambda (data)
3006 (org-element-map
3007 data 'footnote-reference
3008 (lambda (fn)
3009 (cond
3010 ((string= (org-element-property :label fn) label)
3011 (throw 'exit fn))
3012 ;; If FN isn't inlined, be sure to traverse its
3013 ;; definition before resuming search. See
3014 ;; comments in `org-export-get-footnote-number'
3015 ;; for more information.
3016 ((eq (org-element-property :type fn) 'standard)
3017 (funcall search-refs
3018 (org-export-get-footnote-definition fn info)))))
3019 ;; Don't enter footnote definitions since it will
3020 ;; happen when their first reference is found.
3021 info 'first-match 'footnote-definition)))))
3022 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3023 footnote-reference)))))
3025 (defun org-export-get-footnote-definition (footnote-reference info)
3026 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3027 INFO is the plist used as a communication channel."
3028 (let ((label (org-element-property :label footnote-reference)))
3029 (or (org-element-property :inline-definition footnote-reference)
3030 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3032 (defun org-export-get-footnote-number (footnote info)
3033 "Return number associated to a footnote.
3035 FOOTNOTE is either a footnote reference or a footnote definition.
3036 INFO is the plist used as a communication channel."
3037 (let* ((label (org-element-property :label footnote))
3038 seen-refs
3039 search-ref ; For byte-compiler.
3040 (search-ref
3041 (function
3042 (lambda (data)
3043 ;; Search footnote references through DATA, filling
3044 ;; SEEN-REFS along the way.
3045 (org-element-map
3046 data 'footnote-reference
3047 (lambda (fn)
3048 (let ((fn-lbl (org-element-property :label fn)))
3049 (cond
3050 ;; Anonymous footnote match: return number.
3051 ((and (not fn-lbl) (eq fn footnote))
3052 (throw 'exit (1+ (length seen-refs))))
3053 ;; Labels match: return number.
3054 ((and label (string= label fn-lbl))
3055 (throw 'exit (1+ (length seen-refs))))
3056 ;; Anonymous footnote: it's always a new one. Also,
3057 ;; be sure to return nil from the `cond' so
3058 ;; `first-match' doesn't get us out of the loop.
3059 ((not fn-lbl) (push 'inline seen-refs) nil)
3060 ;; Label not seen so far: add it so SEEN-REFS.
3062 ;; Also search for subsequent references in
3063 ;; footnote definition so numbering follows reading
3064 ;; logic. Note that we don't have to care about
3065 ;; inline definitions, since `org-element-map'
3066 ;; already traverses them at the right time.
3068 ;; Once again, return nil to stay in the loop.
3069 ((not (member fn-lbl seen-refs))
3070 (push fn-lbl seen-refs)
3071 (funcall search-ref
3072 (org-export-get-footnote-definition fn info))
3073 nil))))
3074 ;; Don't enter footnote definitions since it will happen
3075 ;; when their first reference is found.
3076 info 'first-match 'footnote-definition)))))
3077 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3080 ;;;; For Headlines
3082 ;; `org-export-get-relative-level' is a shortcut to get headline
3083 ;; level, relatively to the lower headline level in the parsed tree.
3085 ;; `org-export-get-headline-number' returns the section number of an
3086 ;; headline, while `org-export-number-to-roman' allows to convert it
3087 ;; to roman numbers.
3089 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3090 ;; `org-export-last-sibling-p' are three useful predicates when it
3091 ;; comes to fulfill the `:headline-levels' property.
3093 (defun org-export-get-relative-level (headline info)
3094 "Return HEADLINE relative level within current parsed tree.
3095 INFO is a plist holding contextual information."
3096 (+ (org-element-property :level headline)
3097 (or (plist-get info :headline-offset) 0)))
3099 (defun org-export-low-level-p (headline info)
3100 "Non-nil when HEADLINE is considered as low level.
3102 INFO is a plist used as a communication channel.
3104 A low level headlines has a relative level greater than
3105 `:headline-levels' property value.
3107 Return value is the difference between HEADLINE relative level
3108 and the last level being considered as high enough, or nil."
3109 (let ((limit (plist-get info :headline-levels)))
3110 (when (wholenump limit)
3111 (let ((level (org-export-get-relative-level headline info)))
3112 (and (> level limit) (- level limit))))))
3114 (defun org-export-get-headline-number (headline info)
3115 "Return HEADLINE numbering as a list of numbers.
3116 INFO is a plist holding contextual information."
3117 (cdr (assoc headline (plist-get info :headline-numbering))))
3119 (defun org-export-numbered-headline-p (headline info)
3120 "Return a non-nil value if HEADLINE element should be numbered.
3121 INFO is a plist used as a communication channel."
3122 (let ((sec-num (plist-get info :section-numbers))
3123 (level (org-export-get-relative-level headline info)))
3124 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3126 (defun org-export-number-to-roman (n)
3127 "Convert integer N into a roman numeral."
3128 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3129 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3130 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3131 ( 1 . "I")))
3132 (res ""))
3133 (if (<= n 0)
3134 (number-to-string n)
3135 (while roman
3136 (if (>= n (caar roman))
3137 (setq n (- n (caar roman))
3138 res (concat res (cdar roman)))
3139 (pop roman)))
3140 res)))
3142 (defun org-export-get-tags (element info &optional tags)
3143 "Return list of tags associated to ELEMENT.
3145 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3146 is a plist used as a communication channel.
3148 Select tags (see `org-export-select-tags') and exclude tags (see
3149 `org-export-exclude-tags') are removed from the list.
3151 When non-nil, optional argument TAGS should be a list of strings.
3152 Any tag belonging to this list will also be removed."
3153 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3154 (member tag (plist-get info :exclude-tags))
3155 (member tag tags)))
3156 (org-element-property :tags element)))
3158 (defun org-export-get-node-property (property blob &optional inherited)
3159 "Return node PROPERTY value for BLOB.
3161 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3162 element or object.
3164 If optional argument INHERITED is non-nil, the value can be
3165 inherited from a parent headline.
3167 Return value is a string or nil."
3168 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3169 (org-export-get-parent-headline blob))))
3170 (if (not inherited) (org-element-property property blob)
3171 (let ((parent headline) value)
3172 (catch 'found
3173 (while parent
3174 (when (plist-member (nth 1 parent) property)
3175 (throw 'found (org-element-property property parent)))
3176 (setq parent (org-element-property :parent parent))))))))
3178 (defun org-export-first-sibling-p (headline info)
3179 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3180 INFO is a plist used as a communication channel."
3181 (not (eq (org-element-type (org-export-get-previous-element headline info))
3182 'headline)))
3184 (defun org-export-last-sibling-p (headline info)
3185 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3186 INFO is a plist used as a communication channel."
3187 (not (org-export-get-next-element headline info)))
3190 ;;;; For Links
3192 ;; `org-export-solidify-link-text' turns a string into a safer version
3193 ;; for links, replacing most non-standard characters with hyphens.
3195 ;; `org-export-get-coderef-format' returns an appropriate format
3196 ;; string for coderefs.
3198 ;; `org-export-inline-image-p' returns a non-nil value when the link
3199 ;; provided should be considered as an inline image.
3201 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3202 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3203 ;; returns an appropriate unique identifier when found, or nil.
3205 ;; `org-export-resolve-id-link' returns the first headline with
3206 ;; specified id or custom-id in parse tree, the path to the external
3207 ;; file with the id or nil when neither was found.
3209 ;; `org-export-resolve-coderef' associates a reference to a line
3210 ;; number in the element it belongs, or returns the reference itself
3211 ;; when the element isn't numbered.
3213 (defun org-export-solidify-link-text (s)
3214 "Take link text S and make a safe target out of it."
3215 (save-match-data
3216 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3218 (defun org-export-get-coderef-format (path desc)
3219 "Return format string for code reference link.
3220 PATH is the link path. DESC is its description."
3221 (save-match-data
3222 (cond ((not desc) "%s")
3223 ((string-match (regexp-quote (concat "(" path ")")) desc)
3224 (replace-match "%s" t t desc))
3225 (t desc))))
3227 (defun org-export-inline-image-p (link &optional rules)
3228 "Non-nil if LINK object points to an inline image.
3230 Optional argument is a set of RULES defining inline images. It
3231 is an alist where associations have the following shape:
3233 \(TYPE . REGEXP)
3235 Applying a rule means apply REGEXP against LINK's path when its
3236 type is TYPE. The function will return a non-nil value if any of
3237 the provided rules is non-nil. The default rule is
3238 `org-export-default-inline-image-rule'.
3240 This only applies to links without a description."
3241 (and (not (org-element-contents link))
3242 (let ((case-fold-search t)
3243 (rules (or rules org-export-default-inline-image-rule)))
3244 (catch 'exit
3245 (mapc
3246 (lambda (rule)
3247 (and (string= (org-element-property :type link) (car rule))
3248 (string-match (cdr rule)
3249 (org-element-property :path link))
3250 (throw 'exit t)))
3251 rules)
3252 ;; Return nil if no rule matched.
3253 nil))))
3255 (defun org-export-resolve-coderef (ref info)
3256 "Resolve a code reference REF.
3258 INFO is a plist used as a communication channel.
3260 Return associated line number in source code, or REF itself,
3261 depending on src-block or example element's switches."
3262 (org-element-map
3263 (plist-get info :parse-tree) '(example-block src-block)
3264 (lambda (el)
3265 (with-temp-buffer
3266 (insert (org-trim (org-element-property :value el)))
3267 (let* ((label-fmt (regexp-quote
3268 (or (org-element-property :label-fmt el)
3269 org-coderef-label-format)))
3270 (ref-re
3271 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3272 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3273 ;; Element containing REF is found. Resolve it to either
3274 ;; a label or a line number, as needed.
3275 (when (re-search-backward ref-re nil t)
3276 (cond
3277 ((org-element-property :use-labels el) ref)
3278 ((eq (org-element-property :number-lines el) 'continued)
3279 (+ (org-export-get-loc el info) (line-number-at-pos)))
3280 (t (line-number-at-pos)))))))
3281 info 'first-match))
3283 (defun org-export-resolve-fuzzy-link (link info)
3284 "Return LINK destination.
3286 INFO is a plist holding contextual information.
3288 Return value can be an object, an element, or nil:
3290 - If LINK path matches a target object (i.e. <<path>>) or
3291 element (i.e. \"#+TARGET: path\"), return it.
3293 - If LINK path exactly matches the name affiliated keyword
3294 \(i.e. #+NAME: path) of an element, return that element.
3296 - If LINK path exactly matches any headline name, return that
3297 element. If more than one headline share that name, priority
3298 will be given to the one with the closest common ancestor, if
3299 any, or the first one in the parse tree otherwise.
3301 - Otherwise, return nil.
3303 Assume LINK type is \"fuzzy\"."
3304 (let* ((path (org-element-property :path link))
3305 (match-title-p (eq (aref path 0) ?*)))
3306 (cond
3307 ;; First try to find a matching "<<path>>" unless user specified
3308 ;; he was looking for an headline (path starts with a *
3309 ;; character).
3310 ((and (not match-title-p)
3311 (loop for target in (plist-get info :target-list)
3312 when (string= (org-element-property :value target) path)
3313 return target)))
3314 ;; Then try to find an element with a matching "#+NAME: path"
3315 ;; affiliated keyword.
3316 ((and (not match-title-p)
3317 (org-element-map
3318 (plist-get info :parse-tree) org-element-all-elements
3319 (lambda (el)
3320 (when (string= (org-element-property :name el) path) el))
3321 info 'first-match)))
3322 ;; Last case: link either points to an headline or to
3323 ;; nothingness. Try to find the source, with priority given to
3324 ;; headlines with the closest common ancestor. If such candidate
3325 ;; is found, return it, otherwise return nil.
3327 (let ((find-headline
3328 (function
3329 ;; Return first headline whose `:raw-value' property
3330 ;; is NAME in parse tree DATA, or nil.
3331 (lambda (name data)
3332 (org-element-map
3333 data 'headline
3334 (lambda (headline)
3335 (when (string=
3336 (org-element-property :raw-value headline)
3337 name)
3338 headline))
3339 info 'first-match)))))
3340 ;; Search among headlines sharing an ancestor with link,
3341 ;; from closest to farthest.
3342 (or (catch 'exit
3343 (mapc
3344 (lambda (parent)
3345 (when (eq (org-element-type parent) 'headline)
3346 (let ((foundp (funcall find-headline path parent)))
3347 (when foundp (throw 'exit foundp)))))
3348 (org-export-get-genealogy link)) nil)
3349 ;; No match with a common ancestor: try the full parse-tree.
3350 (funcall find-headline
3351 (if match-title-p (substring path 1) path)
3352 (plist-get info :parse-tree))))))))
3354 (defun org-export-resolve-id-link (link info)
3355 "Return headline referenced as LINK destination.
3357 INFO is a plist used as a communication channel.
3359 Return value can be the headline element matched in current parse
3360 tree, a file name or nil. Assume LINK type is either \"id\" or
3361 \"custom-id\"."
3362 (let ((id (org-element-property :path link)))
3363 ;; First check if id is within the current parse tree.
3364 (or (org-element-map
3365 (plist-get info :parse-tree) 'headline
3366 (lambda (headline)
3367 (when (or (string= (org-element-property :id headline) id)
3368 (string= (org-element-property :custom-id headline) id))
3369 headline))
3370 info 'first-match)
3371 ;; Otherwise, look for external files.
3372 (cdr (assoc id (plist-get info :id-alist))))))
3374 (defun org-export-resolve-radio-link (link info)
3375 "Return radio-target object referenced as LINK destination.
3377 INFO is a plist used as a communication channel.
3379 Return value can be a radio-target object or nil. Assume LINK
3380 has type \"radio\"."
3381 (let ((path (org-element-property :path link)))
3382 (org-element-map
3383 (plist-get info :parse-tree) 'radio-target
3384 (lambda (radio)
3385 (when (equal (org-element-property :value radio) path) radio))
3386 info 'first-match)))
3389 ;;;; For References
3391 ;; `org-export-get-ordinal' associates a sequence number to any object
3392 ;; or element.
3394 (defun org-export-get-ordinal (element info &optional types predicate)
3395 "Return ordinal number of an element or object.
3397 ELEMENT is the element or object considered. INFO is the plist
3398 used as a communication channel.
3400 Optional argument TYPES, when non-nil, is a list of element or
3401 object types, as symbols, that should also be counted in.
3402 Otherwise, only provided element's type is considered.
3404 Optional argument PREDICATE is a function returning a non-nil
3405 value if the current element or object should be counted in. It
3406 accepts two arguments: the element or object being considered and
3407 the plist used as a communication channel. This allows to count
3408 only a certain type of objects (i.e. inline images).
3410 Return value is a list of numbers if ELEMENT is an headline or an
3411 item. It is nil for keywords. It represents the footnote number
3412 for footnote definitions and footnote references. If ELEMENT is
3413 a target, return the same value as if ELEMENT was the closest
3414 table, item or headline containing the target. In any other
3415 case, return the sequence number of ELEMENT among elements or
3416 objects of the same type."
3417 ;; A target keyword, representing an invisible target, never has
3418 ;; a sequence number.
3419 (unless (eq (org-element-type element) 'keyword)
3420 ;; Ordinal of a target object refer to the ordinal of the closest
3421 ;; table, item, or headline containing the object.
3422 (when (eq (org-element-type element) 'target)
3423 (setq element
3424 (loop for parent in (org-export-get-genealogy element)
3425 when
3426 (memq
3427 (org-element-type parent)
3428 '(footnote-definition footnote-reference headline item
3429 table))
3430 return parent)))
3431 (case (org-element-type element)
3432 ;; Special case 1: An headline returns its number as a list.
3433 (headline (org-export-get-headline-number element info))
3434 ;; Special case 2: An item returns its number as a list.
3435 (item (let ((struct (org-element-property :structure element)))
3436 (org-list-get-item-number
3437 (org-element-property :begin element)
3438 struct
3439 (org-list-prevs-alist struct)
3440 (org-list-parents-alist struct))))
3441 ((footnote-definition footnote-reference)
3442 (org-export-get-footnote-number element info))
3443 (otherwise
3444 (let ((counter 0))
3445 ;; Increment counter until ELEMENT is found again.
3446 (org-element-map
3447 (plist-get info :parse-tree) (or types (org-element-type element))
3448 (lambda (el)
3449 (cond
3450 ((eq element el) (1+ counter))
3451 ((not predicate) (incf counter) nil)
3452 ((funcall predicate el info) (incf counter) nil)))
3453 info 'first-match))))))
3456 ;;;; For Src-Blocks
3458 ;; `org-export-get-loc' counts number of code lines accumulated in
3459 ;; src-block or example-block elements with a "+n" switch until
3460 ;; a given element, excluded. Note: "-n" switches reset that count.
3462 ;; `org-export-unravel-code' extracts source code (along with a code
3463 ;; references alist) from an `element-block' or `src-block' type
3464 ;; element.
3466 ;; `org-export-format-code' applies a formatting function to each line
3467 ;; of code, providing relative line number and code reference when
3468 ;; appropriate. Since it doesn't access the original element from
3469 ;; which the source code is coming, it expects from the code calling
3470 ;; it to know if lines should be numbered and if code references
3471 ;; should appear.
3473 ;; Eventually, `org-export-format-code-default' is a higher-level
3474 ;; function (it makes use of the two previous functions) which handles
3475 ;; line numbering and code references inclusion, and returns source
3476 ;; code in a format suitable for plain text or verbatim output.
3478 (defun org-export-get-loc (element info)
3479 "Return accumulated lines of code up to ELEMENT.
3481 INFO is the plist used as a communication channel.
3483 ELEMENT is excluded from count."
3484 (let ((loc 0))
3485 (org-element-map
3486 (plist-get info :parse-tree)
3487 `(src-block example-block ,(org-element-type element))
3488 (lambda (el)
3489 (cond
3490 ;; ELEMENT is reached: Quit the loop.
3491 ((eq el element))
3492 ;; Only count lines from src-block and example-block elements
3493 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3494 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3495 ((let ((linums (org-element-property :number-lines el)))
3496 (when linums
3497 ;; Accumulate locs or reset them.
3498 (let ((lines (org-count-lines
3499 (org-trim (org-element-property :value el)))))
3500 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3501 ;; Return nil to stay in the loop.
3502 nil)))
3503 info 'first-match)
3504 ;; Return value.
3505 loc))
3507 (defun org-export-unravel-code (element)
3508 "Clean source code and extract references out of it.
3510 ELEMENT has either a `src-block' an `example-block' type.
3512 Return a cons cell whose CAR is the source code, cleaned from any
3513 reference and protective comma and CDR is an alist between
3514 relative line number (integer) and name of code reference on that
3515 line (string)."
3516 (let* ((line 0) refs
3517 ;; Get code and clean it. Remove blank lines at its
3518 ;; beginning and end. Also remove protective commas.
3519 (code (let ((c (replace-regexp-in-string
3520 "\\`\\([ \t]*\n\\)+" ""
3521 (replace-regexp-in-string
3522 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3523 (org-element-property :value element)))))
3524 ;; If appropriate, remove global indentation.
3525 (unless (or org-src-preserve-indentation
3526 (org-element-property :preserve-indent element))
3527 (setq c (org-remove-indentation c)))
3528 ;; Free up the protected lines. Note: Org blocks
3529 ;; have commas at the beginning or every line.
3530 (if (string= (org-element-property :language element) "org")
3531 (replace-regexp-in-string "^," "" c)
3532 (replace-regexp-in-string
3533 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3534 ;; Get format used for references.
3535 (label-fmt (regexp-quote
3536 (or (org-element-property :label-fmt element)
3537 org-coderef-label-format)))
3538 ;; Build a regexp matching a loc with a reference.
3539 (with-ref-re
3540 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3541 (replace-regexp-in-string
3542 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3543 ;; Return value.
3544 (cons
3545 ;; Code with references removed.
3546 (org-element-normalize-string
3547 (mapconcat
3548 (lambda (loc)
3549 (incf line)
3550 (if (not (string-match with-ref-re loc)) loc
3551 ;; Ref line: remove ref, and signal its position in REFS.
3552 (push (cons line (match-string 3 loc)) refs)
3553 (replace-match "" nil nil loc 1)))
3554 (org-split-string code "\n") "\n"))
3555 ;; Reference alist.
3556 refs)))
3558 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3559 "Format CODE by applying FUN line-wise and return it.
3561 CODE is a string representing the code to format. FUN is
3562 a function. It must accept three arguments: a line of
3563 code (string), the current line number (integer) or nil and the
3564 reference associated to the current line (string) or nil.
3566 Optional argument NUM-LINES can be an integer representing the
3567 number of code lines accumulated until the current code. Line
3568 numbers passed to FUN will take it into account. If it is nil,
3569 FUN's second argument will always be nil. This number can be
3570 obtained with `org-export-get-loc' function.
3572 Optional argument REF-ALIST can be an alist between relative line
3573 number (i.e. ignoring NUM-LINES) and the name of the code
3574 reference on it. If it is nil, FUN's third argument will always
3575 be nil. It can be obtained through the use of
3576 `org-export-unravel-code' function."
3577 (let ((--locs (org-split-string code "\n"))
3578 (--line 0))
3579 (org-element-normalize-string
3580 (mapconcat
3581 (lambda (--loc)
3582 (incf --line)
3583 (let ((--ref (cdr (assq --line ref-alist))))
3584 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3585 --locs "\n"))))
3587 (defun org-export-format-code-default (element info)
3588 "Return source code from ELEMENT, formatted in a standard way.
3590 ELEMENT is either a `src-block' or `example-block' element. INFO
3591 is a plist used as a communication channel.
3593 This function takes care of line numbering and code references
3594 inclusion. Line numbers, when applicable, appear at the
3595 beginning of the line, separated from the code by two white
3596 spaces. Code references, on the other hand, appear flushed to
3597 the right, separated by six white spaces from the widest line of
3598 code."
3599 ;; Extract code and references.
3600 (let* ((code-info (org-export-unravel-code element))
3601 (code (car code-info))
3602 (code-lines (org-split-string code "\n"))
3603 (refs (and (org-element-property :retain-labels element)
3604 (cdr code-info)))
3605 ;; Handle line numbering.
3606 (num-start (case (org-element-property :number-lines element)
3607 (continued (org-export-get-loc element info))
3608 (new 0)))
3609 (num-fmt
3610 (and num-start
3611 (format "%%%ds "
3612 (length (number-to-string
3613 (+ (length code-lines) num-start))))))
3614 ;; Prepare references display, if required. Any reference
3615 ;; should start six columns after the widest line of code,
3616 ;; wrapped with parenthesis.
3617 (max-width
3618 (+ (apply 'max (mapcar 'length code-lines))
3619 (if (not num-start) 0 (length (format num-fmt num-start))))))
3620 (org-export-format-code
3621 code
3622 (lambda (loc line-num ref)
3623 (let ((number-str (and num-fmt (format num-fmt line-num))))
3624 (concat
3625 number-str
3627 (and ref
3628 (concat (make-string
3629 (- (+ 6 max-width)
3630 (+ (length loc) (length number-str))) ? )
3631 (format "(%s)" ref))))))
3632 num-start refs)))
3635 ;;;; For Tables
3637 ;; `org-export-table-has-special-column-p' and and
3638 ;; `org-export-table-row-is-special-p' are predicates used to look for
3639 ;; meta-information about the table structure.
3641 ;; `org-table-has-header-p' tells when the rows before the first rule
3642 ;; should be considered as table's header.
3644 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3645 ;; and `org-export-table-cell-borders' extract information from
3646 ;; a table-cell element.
3648 ;; `org-export-table-dimensions' gives the number on rows and columns
3649 ;; in the table, ignoring horizontal rules and special columns.
3650 ;; `org-export-table-cell-address', given a table-cell object, returns
3651 ;; the absolute address of a cell. On the other hand,
3652 ;; `org-export-get-table-cell-at' does the contrary.
3654 ;; `org-export-table-cell-starts-colgroup-p',
3655 ;; `org-export-table-cell-ends-colgroup-p',
3656 ;; `org-export-table-row-starts-rowgroup-p',
3657 ;; `org-export-table-row-ends-rowgroup-p',
3658 ;; `org-export-table-row-starts-header-p' and
3659 ;; `org-export-table-row-ends-header-p' indicate position of current
3660 ;; row or cell within the table.
3662 (defun org-export-table-has-special-column-p (table)
3663 "Non-nil when TABLE has a special column.
3664 All special columns will be ignored during export."
3665 ;; The table has a special column when every first cell of every row
3666 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3667 ;; "*" "_" and "^". Though, do not consider a first row containing
3668 ;; only empty cells as special.
3669 (let ((special-column-p 'empty))
3670 (catch 'exit
3671 (mapc
3672 (lambda (row)
3673 (when (eq (org-element-property :type row) 'standard)
3674 (let ((value (org-element-contents
3675 (car (org-element-contents row)))))
3676 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3677 (setq special-column-p 'special))
3678 ((not value))
3679 (t (throw 'exit nil))))))
3680 (org-element-contents table))
3681 (eq special-column-p 'special))))
3683 (defun org-export-table-has-header-p (table info)
3684 "Non-nil when TABLE has an header.
3686 INFO is a plist used as a communication channel.
3688 A table has an header when it contains at least two row groups."
3689 (let ((rowgroup 1) row-flag)
3690 (org-element-map
3691 table 'table-row
3692 (lambda (row)
3693 (cond
3694 ((> rowgroup 1) t)
3695 ((and row-flag (eq (org-element-property :type row) 'rule))
3696 (incf rowgroup) (setq row-flag nil))
3697 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3698 (setq row-flag t) nil)))
3699 info)))
3701 (defun org-export-table-row-is-special-p (table-row info)
3702 "Non-nil if TABLE-ROW is considered special.
3704 INFO is a plist used as the communication channel.
3706 All special rows will be ignored during export."
3707 (when (eq (org-element-property :type table-row) 'standard)
3708 (let ((first-cell (org-element-contents
3709 (car (org-element-contents table-row)))))
3710 ;; A row is special either when...
3712 ;; ... it starts with a field only containing "/",
3713 (equal first-cell '("/"))
3714 ;; ... the table contains a special column and the row start
3715 ;; with a marking character among, "^", "_", "$" or "!",
3716 (and (org-export-table-has-special-column-p
3717 (org-export-get-parent table-row))
3718 (member first-cell '(("^") ("_") ("$") ("!"))))
3719 ;; ... it contains only alignment cookies and empty cells.
3720 (let ((special-row-p 'empty))
3721 (catch 'exit
3722 (mapc
3723 (lambda (cell)
3724 (let ((value (org-element-contents cell)))
3725 ;; Since VALUE is a secondary string, the following
3726 ;; checks avoid expanding it with `org-export-data'.
3727 (cond ((not value))
3728 ((and (not (cdr value))
3729 (stringp (car value))
3730 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3731 (car value)))
3732 (setq special-row-p 'cookie))
3733 (t (throw 'exit nil)))))
3734 (org-element-contents table-row))
3735 (eq special-row-p 'cookie)))))))
3737 (defun org-export-table-row-group (table-row info)
3738 "Return TABLE-ROW's group.
3740 INFO is a plist used as the communication channel.
3742 Return value is the group number, as an integer, or nil special
3743 rows and table rules. Group 1 is also table's header."
3744 (unless (or (eq (org-element-property :type table-row) 'rule)
3745 (org-export-table-row-is-special-p table-row info))
3746 (let ((group 0) row-flag)
3747 (catch 'found
3748 (mapc
3749 (lambda (row)
3750 (cond
3751 ((and (eq (org-element-property :type row) 'standard)
3752 (not (org-export-table-row-is-special-p row info)))
3753 (unless row-flag (incf group) (setq row-flag t)))
3754 ((eq (org-element-property :type row) 'rule)
3755 (setq row-flag nil)))
3756 (when (eq table-row row) (throw 'found group)))
3757 (org-element-contents (org-export-get-parent table-row)))))))
3759 (defun org-export-table-cell-width (table-cell info)
3760 "Return TABLE-CELL contents width.
3762 INFO is a plist used as the communication channel.
3764 Return value is the width given by the last width cookie in the
3765 same column as TABLE-CELL, or nil."
3766 (let* ((row (org-export-get-parent table-cell))
3767 (column (let ((cells (org-element-contents row)))
3768 (- (length cells) (length (memq table-cell cells)))))
3769 (table (org-export-get-parent-table table-cell))
3770 cookie-width)
3771 (mapc
3772 (lambda (row)
3773 (cond
3774 ;; In a special row, try to find a width cookie at COLUMN.
3775 ((org-export-table-row-is-special-p row info)
3776 (let ((value (org-element-contents
3777 (elt (org-element-contents row) column))))
3778 ;; The following checks avoid expanding unnecessarily the
3779 ;; cell with `org-export-data'
3780 (when (and value
3781 (not (cdr value))
3782 (stringp (car value))
3783 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3784 (match-string 1 (car value)))
3785 (setq cookie-width
3786 (string-to-number (match-string 1 (car value)))))))
3787 ;; Ignore table rules.
3788 ((eq (org-element-property :type row) 'rule))))
3789 (org-element-contents table))
3790 ;; Return value.
3791 cookie-width))
3793 (defun org-export-table-cell-alignment (table-cell info)
3794 "Return TABLE-CELL contents alignment.
3796 INFO is a plist used as the communication channel.
3798 Return alignment as specified by the last alignment cookie in the
3799 same column as TABLE-CELL. If no such cookie is found, a default
3800 alignment value will be deduced from fraction of numbers in the
3801 column (see `org-table-number-fraction' for more information).
3802 Possible values are `left', `right' and `center'."
3803 (let* ((row (org-export-get-parent table-cell))
3804 (column (let ((cells (org-element-contents row)))
3805 (- (length cells) (length (memq table-cell cells)))))
3806 (table (org-export-get-parent-table table-cell))
3807 (number-cells 0)
3808 (total-cells 0)
3809 cookie-align)
3810 (mapc
3811 (lambda (row)
3812 (cond
3813 ;; In a special row, try to find an alignment cookie at
3814 ;; COLUMN.
3815 ((org-export-table-row-is-special-p row info)
3816 (let ((value (org-element-contents
3817 (elt (org-element-contents row) column))))
3818 ;; Since VALUE is a secondary string, the following checks
3819 ;; avoid useless expansion through `org-export-data'.
3820 (when (and value
3821 (not (cdr value))
3822 (stringp (car value))
3823 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3824 (car value))
3825 (match-string 1 (car value)))
3826 (setq cookie-align (match-string 1 (car value))))))
3827 ;; Ignore table rules.
3828 ((eq (org-element-property :type row) 'rule))
3829 ;; In a standard row, check if cell's contents are expressing
3830 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3831 ;; Though, don't bother if an alignment cookie has already
3832 ;; defined cell's alignment.
3833 ((not cookie-align)
3834 (let ((value (org-export-data
3835 (org-element-contents
3836 (elt (org-element-contents row) column))
3837 info)))
3838 (incf total-cells)
3839 (when (string-match org-table-number-regexp value)
3840 (incf number-cells))))))
3841 (org-element-contents table))
3842 ;; Return value. Alignment specified by cookies has precedence
3843 ;; over alignment deduced from cells contents.
3844 (cond ((equal cookie-align "l") 'left)
3845 ((equal cookie-align "r") 'right)
3846 ((equal cookie-align "c") 'center)
3847 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3848 'right)
3849 (t 'left))))
3851 (defun org-export-table-cell-borders (table-cell info)
3852 "Return TABLE-CELL borders.
3854 INFO is a plist used as a communication channel.
3856 Return value is a list of symbols, or nil. Possible values are:
3857 `top', `bottom', `above', `below', `left' and `right'. Note:
3858 `top' (resp. `bottom') only happen for a cell in the first
3859 row (resp. last row) of the table, ignoring table rules, if any.
3861 Returned borders ignore special rows."
3862 (let* ((row (org-export-get-parent table-cell))
3863 (table (org-export-get-parent-table table-cell))
3864 borders)
3865 ;; Top/above border? TABLE-CELL has a border above when a rule
3866 ;; used to demarcate row groups can be found above. Hence,
3867 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3868 ;; another regular row has to be found above that rule.
3869 (let (rule-flag)
3870 (catch 'exit
3871 (mapc (lambda (row)
3872 (cond ((eq (org-element-property :type row) 'rule)
3873 (setq rule-flag t))
3874 ((not (org-export-table-row-is-special-p row info))
3875 (if rule-flag (throw 'exit (push 'above borders))
3876 (throw 'exit nil)))))
3877 ;; Look at every row before the current one.
3878 (cdr (memq row (reverse (org-element-contents table)))))
3879 ;; No rule above, or rule found starts the table (ignoring any
3880 ;; special row): TABLE-CELL is at the top of the table.
3881 (when rule-flag (push 'above borders))
3882 (push 'top borders)))
3883 ;; Bottom/below border? TABLE-CELL has a border below when next
3884 ;; non-regular row below is a rule.
3885 (let (rule-flag)
3886 (catch 'exit
3887 (mapc (lambda (row)
3888 (cond ((eq (org-element-property :type row) 'rule)
3889 (setq rule-flag t))
3890 ((not (org-export-table-row-is-special-p row info))
3891 (if rule-flag (throw 'exit (push 'below borders))
3892 (throw 'exit nil)))))
3893 ;; Look at every row after the current one.
3894 (cdr (memq row (org-element-contents table))))
3895 ;; No rule below, or rule found ends the table (modulo some
3896 ;; special row): TABLE-CELL is at the bottom of the table.
3897 (when rule-flag (push 'below borders))
3898 (push 'bottom borders)))
3899 ;; Right/left borders? They can only be specified by column
3900 ;; groups. Column groups are defined in a row starting with "/".
3901 ;; Also a column groups row only contains "<", "<>", ">" or blank
3902 ;; cells.
3903 (catch 'exit
3904 (let ((column (let ((cells (org-element-contents row)))
3905 (- (length cells) (length (memq table-cell cells))))))
3906 (mapc
3907 (lambda (row)
3908 (unless (eq (org-element-property :type row) 'rule)
3909 (when (equal (org-element-contents
3910 (car (org-element-contents row)))
3911 '("/"))
3912 (let ((column-groups
3913 (mapcar
3914 (lambda (cell)
3915 (let ((value (org-element-contents cell)))
3916 (when (member value '(("<") ("<>") (">") nil))
3917 (car value))))
3918 (org-element-contents row))))
3919 ;; There's a left border when previous cell, if
3920 ;; any, ends a group, or current one starts one.
3921 (when (or (and (not (zerop column))
3922 (member (elt column-groups (1- column))
3923 '(">" "<>")))
3924 (member (elt column-groups column) '("<" "<>")))
3925 (push 'left borders))
3926 ;; There's a right border when next cell, if any,
3927 ;; starts a group, or current one ends one.
3928 (when (or (and (/= (1+ column) (length column-groups))
3929 (member (elt column-groups (1+ column))
3930 '("<" "<>")))
3931 (member (elt column-groups column) '(">" "<>")))
3932 (push 'right borders))
3933 (throw 'exit nil)))))
3934 ;; Table rows are read in reverse order so last column groups
3935 ;; row has precedence over any previous one.
3936 (reverse (org-element-contents table)))))
3937 ;; Return value.
3938 borders))
3940 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3941 "Non-nil when TABLE-CELL is at the beginning of a row group.
3942 INFO is a plist used as a communication channel."
3943 ;; A cell starts a column group either when it is at the beginning
3944 ;; of a row (or after the special column, if any) or when it has
3945 ;; a left border.
3946 (or (eq (org-element-map
3947 (org-export-get-parent table-cell)
3948 'table-cell 'identity info 'first-match)
3949 table-cell)
3950 (memq 'left (org-export-table-cell-borders table-cell info))))
3952 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3953 "Non-nil when TABLE-CELL is at the end of a row group.
3954 INFO is a plist used as a communication channel."
3955 ;; A cell ends a column group either when it is at the end of a row
3956 ;; or when it has a right border.
3957 (or (eq (car (last (org-element-contents
3958 (org-export-get-parent table-cell))))
3959 table-cell)
3960 (memq 'right (org-export-table-cell-borders table-cell info))))
3962 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3963 "Non-nil when TABLE-ROW is at the beginning of a column group.
3964 INFO is a plist used as a communication channel."
3965 (unless (or (eq (org-element-property :type table-row) 'rule)
3966 (org-export-table-row-is-special-p table-row info))
3967 (let ((borders (org-export-table-cell-borders
3968 (car (org-element-contents table-row)) info)))
3969 (or (memq 'top borders) (memq 'above borders)))))
3971 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3972 "Non-nil when TABLE-ROW is at the end of a column group.
3973 INFO is a plist used as a communication channel."
3974 (unless (or (eq (org-element-property :type table-row) 'rule)
3975 (org-export-table-row-is-special-p table-row info))
3976 (let ((borders (org-export-table-cell-borders
3977 (car (org-element-contents table-row)) info)))
3978 (or (memq 'bottom borders) (memq 'below borders)))))
3980 (defun org-export-table-row-starts-header-p (table-row info)
3981 "Non-nil when TABLE-ROW is the first table header's row.
3982 INFO is a plist used as a communication channel."
3983 (and (org-export-table-has-header-p
3984 (org-export-get-parent-table table-row) info)
3985 (org-export-table-row-starts-rowgroup-p table-row info)
3986 (= (org-export-table-row-group table-row info) 1)))
3988 (defun org-export-table-row-ends-header-p (table-row info)
3989 "Non-nil when TABLE-ROW is the last table header's row.
3990 INFO is a plist used as a communication channel."
3991 (and (org-export-table-has-header-p
3992 (org-export-get-parent-table table-row) info)
3993 (org-export-table-row-ends-rowgroup-p table-row info)
3994 (= (org-export-table-row-group table-row info) 1)))
3996 (defun org-export-table-dimensions (table info)
3997 "Return TABLE dimensions.
3999 INFO is a plist used as a communication channel.
4001 Return value is a CONS like (ROWS . COLUMNS) where
4002 ROWS (resp. COLUMNS) is the number of exportable
4003 rows (resp. columns)."
4004 (let (first-row (columns 0) (rows 0))
4005 ;; Set number of rows, and extract first one.
4006 (org-element-map
4007 table 'table-row
4008 (lambda (row)
4009 (when (eq (org-element-property :type row) 'standard)
4010 (incf rows)
4011 (unless first-row (setq first-row row)))) info)
4012 ;; Set number of columns.
4013 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4014 ;; Return value.
4015 (cons rows columns)))
4017 (defun org-export-table-cell-address (table-cell info)
4018 "Return address of a regular TABLE-CELL object.
4020 TABLE-CELL is the cell considered. INFO is a plist used as
4021 a communication channel.
4023 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4024 zero-based index. Only exportable cells are considered. The
4025 function returns nil for other cells."
4026 (let* ((table-row (org-export-get-parent table-cell))
4027 (table (org-export-get-parent-table table-cell)))
4028 ;; Ignore cells in special rows or in special column.
4029 (unless (or (org-export-table-row-is-special-p table-row info)
4030 (and (org-export-table-has-special-column-p table)
4031 (eq (car (org-element-contents table-row)) table-cell)))
4032 (cons
4033 ;; Row number.
4034 (let ((row-count 0))
4035 (org-element-map
4036 table 'table-row
4037 (lambda (row)
4038 (cond ((eq (org-element-property :type row) 'rule) nil)
4039 ((eq row table-row) row-count)
4040 (t (incf row-count) nil)))
4041 info 'first-match))
4042 ;; Column number.
4043 (let ((col-count 0))
4044 (org-element-map
4045 table-row 'table-cell
4046 (lambda (cell)
4047 (if (eq cell table-cell) col-count (incf col-count) nil))
4048 info 'first-match))))))
4050 (defun org-export-get-table-cell-at (address table info)
4051 "Return regular table-cell object at ADDRESS in TABLE.
4053 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4054 zero-based index. TABLE is a table type element. INFO is
4055 a plist used as a communication channel.
4057 If no table-cell, among exportable cells, is found at ADDRESS,
4058 return nil."
4059 (let ((column-pos (cdr address)) (column-count 0))
4060 (org-element-map
4061 ;; Row at (car address) or nil.
4062 (let ((row-pos (car address)) (row-count 0))
4063 (org-element-map
4064 table 'table-row
4065 (lambda (row)
4066 (cond ((eq (org-element-property :type row) 'rule) nil)
4067 ((= row-count row-pos) row)
4068 (t (incf row-count) nil)))
4069 info 'first-match))
4070 'table-cell
4071 (lambda (cell)
4072 (if (= column-count column-pos) cell
4073 (incf column-count) nil))
4074 info 'first-match)))
4077 ;;;; For Tables Of Contents
4079 ;; `org-export-collect-headlines' builds a list of all exportable
4080 ;; headline elements, maybe limited to a certain depth. One can then
4081 ;; easily parse it and transcode it.
4083 ;; Building lists of tables, figures or listings is quite similar.
4084 ;; Once the generic function `org-export-collect-elements' is defined,
4085 ;; `org-export-collect-tables', `org-export-collect-figures' and
4086 ;; `org-export-collect-listings' can be derived from it.
4088 (defun org-export-collect-headlines (info &optional n)
4089 "Collect headlines in order to build a table of contents.
4091 INFO is a plist used as a communication channel.
4093 When optional argument N is an integer, it specifies the depth of
4094 the table of contents. Otherwise, it is set to the value of the
4095 last headline level. See `org-export-headline-levels' for more
4096 information.
4098 Return a list of all exportable headlines as parsed elements."
4099 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4100 (org-element-map
4101 (plist-get info :parse-tree)
4102 'headline
4103 (lambda (headline)
4104 ;; Strip contents from HEADLINE.
4105 (let ((relative-level (org-export-get-relative-level headline info)))
4106 (unless (> relative-level n) headline)))
4107 info))
4109 (defun org-export-collect-elements (type info &optional predicate)
4110 "Collect referenceable elements of a determined type.
4112 TYPE can be a symbol or a list of symbols specifying element
4113 types to search. Only elements with a caption are collected.
4115 INFO is a plist used as a communication channel.
4117 When non-nil, optional argument PREDICATE is a function accepting
4118 one argument, an element of type TYPE. It returns a non-nil
4119 value when that element should be collected.
4121 Return a list of all elements found, in order of appearance."
4122 (org-element-map
4123 (plist-get info :parse-tree) type
4124 (lambda (element)
4125 (and (org-element-property :caption element)
4126 (or (not predicate) (funcall predicate element))
4127 element))
4128 info))
4130 (defun org-export-collect-tables (info)
4131 "Build a list of tables.
4132 INFO is a plist used as a communication channel.
4134 Return a list of table elements with a caption."
4135 (org-export-collect-elements 'table info))
4137 (defun org-export-collect-figures (info predicate)
4138 "Build a list of figures.
4140 INFO is a plist used as a communication channel. PREDICATE is
4141 a function which accepts one argument: a paragraph element and
4142 whose return value is non-nil when that element should be
4143 collected.
4145 A figure is a paragraph type element, with a caption, verifying
4146 PREDICATE. The latter has to be provided since a \"figure\" is
4147 a vague concept that may depend on back-end.
4149 Return a list of elements recognized as figures."
4150 (org-export-collect-elements 'paragraph info predicate))
4152 (defun org-export-collect-listings (info)
4153 "Build a list of src blocks.
4155 INFO is a plist used as a communication channel.
4157 Return a list of src-block elements with a caption."
4158 (org-export-collect-elements 'src-block info))
4161 ;;;; Topology
4163 ;; Here are various functions to retrieve information about the
4164 ;; neighbourhood of a given element or object. Neighbours of interest
4165 ;; are direct parent (`org-export-get-parent'), parent headline
4166 ;; (`org-export-get-parent-headline'), first element containing an
4167 ;; object, (`org-export-get-parent-element'), parent table
4168 ;; (`org-export-get-parent-table'), previous element or object
4169 ;; (`org-export-get-previous-element') and next element or object
4170 ;; (`org-export-get-next-element').
4172 ;; `org-export-get-genealogy' returns the full genealogy of a given
4173 ;; element or object, from closest parent to full parse tree.
4175 (defun org-export-get-parent (blob)
4176 "Return BLOB parent or nil.
4177 BLOB is the element or object considered."
4178 (org-element-property :parent blob))
4180 (defun org-export-get-genealogy (blob)
4181 "Return full genealogy relative to a given element or object.
4183 BLOB is the element or object being considered.
4185 Ancestors are returned from closest to farthest, the last one
4186 being the full parse tree."
4187 (let (genealogy (parent blob))
4188 (while (setq parent (org-element-property :parent parent))
4189 (push parent genealogy))
4190 (nreverse genealogy)))
4192 (defun org-export-get-parent-headline (blob)
4193 "Return BLOB parent headline or nil.
4194 BLOB is the element or object being considered."
4195 (let ((parent blob))
4196 (while (and (setq parent (org-element-property :parent parent))
4197 (not (eq (org-element-type parent) 'headline))))
4198 parent))
4200 (defun org-export-get-parent-element (object)
4201 "Return first element containing OBJECT or nil.
4202 OBJECT is the object to consider."
4203 (let ((parent object))
4204 (while (and (setq parent (org-element-property :parent parent))
4205 (memq (org-element-type parent) org-element-all-objects)))
4206 parent))
4208 (defun org-export-get-parent-table (object)
4209 "Return OBJECT parent table or nil.
4210 OBJECT is either a `table-cell' or `table-element' type object."
4211 (let ((parent object))
4212 (while (and (setq parent (org-element-property :parent parent))
4213 (not (eq (org-element-type parent) 'table))))
4214 parent))
4216 (defun org-export-get-previous-element (blob info)
4217 "Return previous element or object.
4218 BLOB is an element or object. INFO is a plist used as
4219 a communication channel. Return previous exportable element or
4220 object, a string, or nil."
4221 (let (prev)
4222 (catch 'exit
4223 (mapc (lambda (obj)
4224 (cond ((eq obj blob) (throw 'exit prev))
4225 ((memq obj (plist-get info :ignore-list)))
4226 (t (setq prev obj))))
4227 (org-element-contents (org-export-get-parent blob))))))
4229 (defun org-export-get-next-element (blob info)
4230 "Return next element or object.
4231 BLOB is an element or object. INFO is a plist used as
4232 a communication channel. Return next exportable element or
4233 object, a string, or nil."
4234 (catch 'found
4235 (mapc (lambda (obj)
4236 (unless (memq obj (plist-get info :ignore-list))
4237 (throw 'found obj)))
4238 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4239 nil))
4242 ;;;; Translation
4244 ;; `org-export-translate' translates a string according to language
4245 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4246 ;; variable and a specified charset. `org-export-dictionary' contains
4247 ;; the dictionary used for the translation.
4249 (defconst org-export-dictionary
4250 '(("Author"
4251 ("ca" :default "Autor")
4252 ("cs" :default "Autor")
4253 ("da" :default "Ophavsmand")
4254 ("de" :default "Autor")
4255 ("eo" :html "A&#365;toro")
4256 ("es" :default "Autor")
4257 ("fi" :html "Tekij&auml;")
4258 ("fr" :default "Auteur")
4259 ("hu" :default "Szerz&otilde;")
4260 ("is" :html "H&ouml;fundur")
4261 ("it" :default "Autore")
4262 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4263 ("nl" :default "Auteur")
4264 ("no" :default "Forfatter")
4265 ("nb" :default "Forfatter")
4266 ("nn" :default "Forfattar")
4267 ("pl" :default "Autor")
4268 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4269 ("sv" :html "F&ouml;rfattare")
4270 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4271 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4272 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4273 ("Date"
4274 ("ca" :default "Data")
4275 ("cs" :default "Datum")
4276 ("da" :default "Dato")
4277 ("de" :default "Datum")
4278 ("eo" :default "Dato")
4279 ("es" :default "Fecha")
4280 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4281 ("hu" :html "D&aacute;tum")
4282 ("is" :default "Dagsetning")
4283 ("it" :default "Data")
4284 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4285 ("nl" :default "Datum")
4286 ("no" :default "Dato")
4287 ("nb" :default "Dato")
4288 ("nn" :default "Dato")
4289 ("pl" :default "Data")
4290 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4291 ("sv" :default "Datum")
4292 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4293 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4294 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4295 ("Equation"
4296 ("fr" :ascii "Equation" :default "Équation"))
4297 ("Figure")
4298 ("Footnotes"
4299 ("ca" :html "Peus de p&agrave;gina")
4300 ("cs" :default "Pozn\xe1mky pod carou")
4301 ("da" :default "Fodnoter")
4302 ("de" :html "Fu&szlig;noten")
4303 ("eo" :default "Piednotoj")
4304 ("es" :html "Pies de p&aacute;gina")
4305 ("fi" :default "Alaviitteet")
4306 ("fr" :default "Notes de bas de page")
4307 ("hu" :html "L&aacute;bjegyzet")
4308 ("is" :html "Aftanm&aacute;lsgreinar")
4309 ("it" :html "Note a pi&egrave; di pagina")
4310 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4311 ("nl" :default "Voetnoten")
4312 ("no" :default "Fotnoter")
4313 ("nb" :default "Fotnoter")
4314 ("nn" :default "Fotnotar")
4315 ("pl" :default "Przypis")
4316 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4317 ("sv" :default "Fotnoter")
4318 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4319 :utf-8 "Примітки")
4320 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4321 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4322 ("List of Listings"
4323 ("fr" :default "Liste des programmes"))
4324 ("List of Tables"
4325 ("fr" :default "Liste des tableaux"))
4326 ("Listing %d:"
4327 ("fr"
4328 :ascii "Programme %d :" :default "Programme nº %d :"
4329 :latin1 "Programme %d :"))
4330 ("Listing %d: %s"
4331 ("fr"
4332 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4333 :latin1 "Programme %d : %s"))
4334 ("See section %s"
4335 ("fr" :default "cf. section %s"))
4336 ("Table %d:"
4337 ("fr"
4338 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4339 ("Table %d: %s"
4340 ("fr"
4341 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4342 :latin1 "Tableau %d : %s"))
4343 ("Table of Contents"
4344 ("ca" :html "&Iacute;ndex")
4345 ("cs" :default "Obsah")
4346 ("da" :default "Indhold")
4347 ("de" :default "Inhaltsverzeichnis")
4348 ("eo" :default "Enhavo")
4349 ("es" :html "&Iacute;ndice")
4350 ("fi" :html "Sis&auml;llysluettelo")
4351 ("fr" :ascii "Sommaire" :default "Table des matières")
4352 ("hu" :html "Tartalomjegyz&eacute;k")
4353 ("is" :default "Efnisyfirlit")
4354 ("it" :default "Indice")
4355 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4356 ("nl" :default "Inhoudsopgave")
4357 ("no" :default "Innhold")
4358 ("nb" :default "Innhold")
4359 ("nn" :default "Innhald")
4360 ("pl" :html "Spis tre&#x015b;ci")
4361 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4362 :utf-8 "Содержание")
4363 ("sv" :html "Inneh&aring;ll")
4364 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4365 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4366 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4367 ("Unknown reference"
4368 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4369 "Dictionary for export engine.
4371 Alist whose CAR is the string to translate and CDR is an alist
4372 whose CAR is the language string and CDR is a plist whose
4373 properties are possible charsets and values translated terms.
4375 It is used as a database for `org-export-translate'. Since this
4376 function returns the string as-is if no translation was found,
4377 the variable only needs to record values different from the
4378 entry.")
4380 (defun org-export-translate (s encoding info)
4381 "Translate string S according to language specification.
4383 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4384 and `:utf-8'. INFO is a plist used as a communication channel.
4386 Translation depends on `:language' property. Return the
4387 translated string. If no translation is found, try to fall back
4388 to `:default' encoding. If it fails, return S."
4389 (let* ((lang (plist-get info :language))
4390 (translations (cdr (assoc lang
4391 (cdr (assoc s org-export-dictionary))))))
4392 (or (plist-get translations encoding)
4393 (plist-get translations :default)
4394 s)))
4398 ;;; The Dispatcher
4400 ;; `org-export-dispatch' is the standard interactive way to start an
4401 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4402 ;; for its interface, which, in turn, delegates response to key
4403 ;; pressed to `org-export-dispatch-action'.
4405 (defvar org-export-dispatch-menu-entries nil
4406 "List of menu entries available for `org-export-dispatch'.
4407 This variable shouldn't be set directly. Set-up :menu-entry
4408 keyword in either `org-export-define-backend' or
4409 `org-export-define-derived-backend' instead.")
4411 ;;;###autoload
4412 (defun org-export-dispatch ()
4413 "Export dispatcher for Org mode.
4415 It provides an access to common export related tasks in a buffer.
4416 Its interface comes in two flavours: standard and expert. While
4417 both share the same set of bindings, only the former displays the
4418 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4419 to switch to one or the other.
4421 Return an error if key pressed has no associated command."
4422 (interactive)
4423 (let* ((input (org-export-dispatch-ui (list org-export-initial-scope)
4425 org-export-dispatch-use-expert-ui))
4426 (action (car input))
4427 (optns (cdr input)))
4428 (case action
4429 ;; First handle special hard-coded actions.
4430 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4431 (publish-current-project
4432 (org-e-publish-current-project (memq 'force optns)))
4433 (publish-choose-project
4434 (org-e-publish (assoc (org-icompleting-read
4435 "Publish project: "
4436 org-e-publish-project-alist nil t)
4437 org-e-publish-project-alist)
4438 (memq 'force optns)))
4439 (publish-all (org-e-publish-all (memq 'force optns)))
4440 (otherwise
4441 (funcall action
4442 (memq 'subtree optns)
4443 (memq 'visible optns)
4444 (memq 'body optns))))))
4446 (defun org-export-dispatch-ui (options first-key expertp)
4447 "Handle interface for `org-export-dispatch'.
4449 OPTIONS is a list containing current interactive options set for
4450 export. It can contain any of the following symbols:
4451 `body' toggles a body-only export
4452 `subtree' restricts export to current subtree
4453 `visible' restricts export to visible part of buffer.
4454 `force' force publishing files.
4456 FIRST-KEY is the key pressed to select the first level menu. It
4457 is nil when this menu hasn't been selected yet.
4459 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4460 buffer is provided, but indications about currently active
4461 options are given in the prompt. Moreover, \[?] allows to switch
4462 back to standard interface."
4463 (let* ((fontify-key
4464 (lambda (key &optional access-key)
4465 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4466 ;; non-nil is the required first-level key to activate
4467 ;; KEY. When its value is t, activate KEY independently
4468 ;; on the first key, if any. A nil value means KEY will
4469 ;; only be activated at first level.
4470 (if (or (eq access-key t) (eq access-key first-key))
4471 (org-add-props key nil 'face 'org-warning)
4472 (org-no-properties key))))
4473 ;; Make sure order of menu doesn't depend on the order in
4474 ;; which back-ends are loaded.
4475 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4476 (lambda (a b) (< (car a) (car b)))))
4477 ;; Compute a list of allowed keys based on the first key
4478 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4479 ;; always available.
4480 (allowed-keys
4481 (nconc (list ?1 ?2 ?3 ?4)
4482 (mapcar 'car
4483 (if (not first-key) backends
4484 (nth 2 (assq first-key backends))))
4485 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4486 ((not first-key) (list ?P)))
4487 (when expertp (list ??))
4488 (list ?q)))
4489 ;; Build the help menu for standard UI.
4490 (help
4491 (unless expertp
4492 (concat
4493 ;; Options are hard-coded.
4494 (format "Options
4495 [%s] Body only: %s [%s] Visible only: %s
4496 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4497 (funcall fontify-key "1" t)
4498 (if (memq 'body options) "On " "Off")
4499 (funcall fontify-key "2" t)
4500 (if (memq 'visible options) "On " "Off")
4501 (funcall fontify-key "3" t)
4502 (if (memq 'subtree options) "Subtree" "Buffer ")
4503 (funcall fontify-key "4" t)
4504 (if (memq 'force options) "On " "Off"))
4505 ;; Display registered back-end entries.
4506 (mapconcat
4507 (lambda (entry)
4508 (let ((top-key (car entry)))
4509 (concat
4510 (format "[%s] %s\n"
4511 (funcall fontify-key (char-to-string top-key))
4512 (nth 1 entry))
4513 (let ((sub-menu (nth 2 entry)))
4514 (unless (functionp sub-menu)
4515 ;; Split sub-menu into two columns.
4516 (let ((index -1))
4517 (concat
4518 (mapconcat
4519 (lambda (sub-entry)
4520 (incf index)
4521 (format (if (zerop (mod index 2)) " [%s] %-24s"
4522 "[%s] %s\n")
4523 (funcall fontify-key
4524 (char-to-string (car sub-entry))
4525 top-key)
4526 (nth 1 sub-entry)))
4527 sub-menu "")
4528 (when (zerop (mod index 2)) "\n"))))))))
4529 backends "\n")
4530 ;; Publishing menu is hard-coded.
4531 (format "\n[%s] Publish
4532 [%s] Current file [%s] Current project
4533 [%s] Choose project [%s] All projects\n\n"
4534 (funcall fontify-key "P")
4535 (funcall fontify-key "f" ?P)
4536 (funcall fontify-key "p" ?P)
4537 (funcall fontify-key "x" ?P)
4538 (funcall fontify-key "a" ?P))
4539 (format "\[%s] %s"
4540 (funcall fontify-key "q" t)
4541 (if first-key "Main menu" "Exit")))))
4542 ;; Build prompts for both standard and expert UI.
4543 (standard-prompt (unless expertp "Export command: "))
4544 (expert-prompt
4545 (when expertp
4546 (format
4547 "Export command (Options: %s%s%s%s) [%s]: "
4548 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4549 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4550 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4551 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4552 (concat allowed-keys)))))
4553 ;; With expert UI, just read key with a fancy prompt. In standard
4554 ;; UI, display an intrusive help buffer.
4555 (if expertp
4556 (org-export-dispatch-action
4557 expert-prompt allowed-keys backends options first-key expertp)
4558 (save-window-excursion
4559 (delete-other-windows)
4560 (unwind-protect
4561 (progn
4562 (with-current-buffer
4563 (get-buffer-create "*Org Export Dispatcher*")
4564 (erase-buffer)
4565 (save-excursion (insert help)))
4566 (org-fit-window-to-buffer
4567 (display-buffer "*Org Export Dispatcher*"))
4568 (org-export-dispatch-action
4569 standard-prompt allowed-keys backends options first-key expertp))
4570 (and (get-buffer "*Org Export Dispatcher*")
4571 (kill-buffer "*Org Export Dispatcher*")))))))
4573 (defun org-export-dispatch-action
4574 (prompt allowed-keys backends options first-key expertp)
4575 "Read a character from command input and act accordingly.
4577 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4578 a list of characters available at a given step in the process.
4579 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4580 EXPERTP are the same as defined in `org-export-dispatch-ui',
4581 which see.
4583 Toggle export options when required. Otherwise, return value is
4584 a list with action as CAR and a list of interactive export
4585 options as CDR."
4586 (let ((key (let ((k (read-char-exclusive prompt)))
4587 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4588 ;; depending on user's key pressed.
4589 (if (< k 27) (+ k 96) k))))
4590 (cond
4591 ;; Ignore non-standard characters (i.e. "M-a") and
4592 ;; undefined associations.
4593 ((not (memq key allowed-keys))
4594 (org-export-dispatch-ui options first-key expertp))
4595 ;; q key at first level aborts export. At second
4596 ;; level, cancel first key instead.
4597 ((eq key ?q) (if (not first-key) (error "Export aborted")
4598 (org-export-dispatch-ui options nil expertp)))
4599 ;; Help key: Switch back to standard interface if
4600 ;; expert UI was active.
4601 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4602 ;; Toggle export options.
4603 ((memq key '(?1 ?2 ?3 ?4))
4604 (org-export-dispatch-ui
4605 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4606 (?4 'force))))
4607 (if (memq option options) (remq option options)
4608 (cons option options)))
4609 first-key expertp))
4610 ;; Action selected: Send key and options back to
4611 ;; `org-export-dispatch'.
4612 ((or first-key
4613 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4614 (functionp (nth 2 (assq key backends))))
4615 (cons (cond
4616 ((not first-key) (nth 2 (assq key backends)))
4617 ;; Publishing actions are hard-coded. Send a special
4618 ;; signal to `org-export-dispatch'.
4619 ((eq first-key ?P)
4620 (case key
4621 (?f 'publish-current-file)
4622 (?p 'publish-current-project)
4623 (?x 'publish-choose-project)
4624 (?a 'publish-all)))
4625 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4626 options))
4627 ;; Otherwise, enter sub-menu.
4628 (t (org-export-dispatch-ui options key expertp)))))
4631 (provide 'org-export)
4632 ;;; org-export.el ends here