org-export: New function to return a node property, even inherited
[org-mode.git] / contrib / lisp / org-export.el
blob886d1bcff07fc416268f0a4f7b6670a8f3d24916
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-paragraph . org-export-filter-paragraph-functions)
219 (:filter-parse-tree . org-export-filter-parse-tree-functions)
220 (:filter-plain-list . org-export-filter-plain-list-functions)
221 (:filter-plain-text . org-export-filter-plain-text-functions)
222 (:filter-planning . org-export-filter-planning-functions)
223 (:filter-property-drawer . org-export-filter-property-drawer-functions)
224 (:filter-quote-block . org-export-filter-quote-block-functions)
225 (:filter-quote-section . org-export-filter-quote-section-functions)
226 (:filter-radio-target . org-export-filter-radio-target-functions)
227 (:filter-section . org-export-filter-section-functions)
228 (:filter-special-block . org-export-filter-special-block-functions)
229 (:filter-src-block . org-export-filter-src-block-functions)
230 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
231 (:filter-strike-through . org-export-filter-strike-through-functions)
232 (:filter-subscript . org-export-filter-subscript-functions)
233 (:filter-superscript . org-export-filter-superscript-functions)
234 (:filter-table . org-export-filter-table-functions)
235 (:filter-table-cell . org-export-filter-table-cell-functions)
236 (:filter-table-row . org-export-filter-table-row-functions)
237 (:filter-target . org-export-filter-target-functions)
238 (:filter-timestamp . org-export-filter-timestamp-functions)
239 (:filter-underline . org-export-filter-underline-functions)
240 (:filter-verbatim . org-export-filter-verbatim-functions)
241 (:filter-verse-block . org-export-filter-verse-block-functions))
242 "Alist between filters properties and initial values.
244 The key of each association is a property name accessible through
245 the communication channel. Its value is a configurable global
246 variable defining initial filters.
248 This list is meant to install user specified filters. Back-end
249 developers may install their own filters using
250 `org-export-define-backend'. Filters defined there will always
251 be prepended to the current list, so they always get applied
252 first.")
254 (defconst org-export-default-inline-image-rule
255 `(("file" .
256 ,(format "\\.%s\\'"
257 (regexp-opt
258 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
259 "xpm" "pbm" "pgm" "ppm") t))))
260 "Default rule for link matching an inline image.
261 This rule applies to links with no description. By default, it
262 will be considered as an inline image if it targets a local file
263 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
264 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
265 See `org-export-inline-image-p' for more information about
266 rules.")
270 ;;; User-configurable Variables
272 ;; Configuration for the masses.
274 ;; They should never be accessed directly, as their value is to be
275 ;; stored in a property list (cf. `org-export-options-alist').
276 ;; Back-ends will read their value from there instead.
278 (defgroup org-export nil
279 "Options for exporting Org mode files."
280 :tag "Org Export"
281 :group 'org)
283 (defgroup org-export-general nil
284 "General options for export engine."
285 :tag "Org Export General"
286 :group 'org-export)
288 (defcustom org-export-with-archived-trees 'headline
289 "Whether sub-trees with the ARCHIVE tag should be exported.
291 This can have three different values:
292 nil Do not export, pretend this tree is not present.
293 t Do export the entire tree.
294 `headline' Only export the headline, but skip the tree below it.
296 This option can also be set with the #+OPTIONS line,
297 e.g. \"arch:nil\"."
298 :group 'org-export-general
299 :type '(choice
300 (const :tag "Not at all" nil)
301 (const :tag "Headline only" 'headline)
302 (const :tag "Entirely" t)))
304 (defcustom org-export-with-author t
305 "Non-nil means insert author name into the exported file.
306 This option can also be set with the #+OPTIONS line,
307 e.g. \"author:nil\"."
308 :group 'org-export-general
309 :type 'boolean)
311 (defcustom org-export-with-clocks nil
312 "Non-nil means export CLOCK keywords.
313 This option can also be set with the #+OPTIONS line,
314 e.g. \"c:t\"."
315 :group 'org-export-general
316 :type 'boolean)
318 (defcustom org-export-with-creator 'comment
319 "Non-nil means the postamble should contain a creator sentence.
321 The sentence can be set in `org-export-creator-string' and
322 defaults to \"Generated by Org mode XX in Emacs XXX.\".
324 If the value is `comment' insert it as a comment."
325 :group 'org-export-general
326 :type '(choice
327 (const :tag "No creator sentence" nil)
328 (const :tag "Sentence as a comment" 'comment)
329 (const :tag "Insert the sentence" t)))
331 (defcustom org-export-creator-string
332 (format "Generated by Org mode %s in Emacs %s."
333 (if (fboundp 'org-version) (org-version) "(Unknown)")
334 emacs-version)
335 "String to insert at the end of the generated document."
336 :group 'org-export-general
337 :type '(string :tag "Creator string"))
339 (defcustom org-export-with-drawers t
340 "Non-nil means export contents of standard drawers.
342 When t, all drawers are exported. This may also be a list of
343 drawer names to export. This variable doesn't apply to
344 properties drawers.
346 This option can also be set with the #+OPTIONS line,
347 e.g. \"d:nil\"."
348 :group 'org-export-general
349 :type '(choice
350 (const :tag "All drawers" t)
351 (const :tag "None" nil)
352 (repeat :tag "Selected drawers"
353 (string :tag "Drawer name"))))
355 (defcustom org-export-with-email nil
356 "Non-nil means insert author email into the exported file.
357 This option can also be set with the #+OPTIONS line,
358 e.g. \"email:t\"."
359 :group 'org-export-general
360 :type 'boolean)
362 (defcustom org-export-with-emphasize t
363 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
365 If the export target supports emphasizing text, the word will be
366 typeset in bold, italic, or underlined, respectively. Not all
367 export backends support this.
369 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
370 :group 'org-export-general
371 :type 'boolean)
373 (defcustom org-export-exclude-tags '("noexport")
374 "Tags that exclude a tree from export.
376 All trees carrying any of these tags will be excluded from
377 export. This is without condition, so even subtrees inside that
378 carry one of the `org-export-select-tags' will be removed.
380 This option can also be set with the #+EXCLUDE_TAGS: keyword."
381 :group 'org-export-general
382 :type '(repeat (string :tag "Tag")))
384 (defcustom org-export-with-fixed-width t
385 "Non-nil means lines starting with \":\" will be in fixed width font.
387 This can be used to have pre-formatted text, fragments of code
388 etc. For example:
389 : ;; Some Lisp examples
390 : (while (defc cnt)
391 : (ding))
392 will be looking just like this in also HTML. See also the QUOTE
393 keyword. Not all export backends support this.
395 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
396 :group 'org-export-translation
397 :type 'boolean)
399 (defcustom org-export-with-footnotes t
400 "Non-nil means Org footnotes should be exported.
401 This option can also be set with the #+OPTIONS line,
402 e.g. \"f:nil\"."
403 :group 'org-export-general
404 :type 'boolean)
406 (defcustom org-export-headline-levels 3
407 "The last level which is still exported as a headline.
409 Inferior levels will produce itemize lists when exported.
411 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
412 :group 'org-export-general
413 :type 'integer)
415 (defcustom org-export-default-language "en"
416 "The default language for export and clocktable translations, as a string.
417 This may have an association in
418 `org-clock-clocktable-language-setup'."
419 :group 'org-export-general
420 :type '(string :tag "Language"))
422 (defcustom org-export-preserve-breaks nil
423 "Non-nil means preserve all line breaks when exporting.
425 Normally, in HTML output paragraphs will be reformatted.
427 This option can also be set with the #+OPTIONS line,
428 e.g. \"\\n:t\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-entities t
433 "Non-nil means interpret entities when exporting.
435 For example, HTML export converts \\alpha to &alpha; and \\AA to
436 &Aring;.
438 For a list of supported names, see the constant `org-entities'
439 and the user option `org-entities-user'.
441 This option can also be set with the #+OPTIONS line,
442 e.g. \"e:nil\"."
443 :group 'org-export-general
444 :type 'boolean)
446 (defcustom org-export-with-inlinetasks t
447 "Non-nil means inlinetasks should be exported.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"inline:nil\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-with-planning nil
454 "Non-nil means include planning info in export.
455 This option can also be set with the #+OPTIONS: line,
456 e.g. \"p:t\"."
457 :group 'org-export-general
458 :type 'boolean)
460 (defcustom org-export-with-priority nil
461 "Non-nil means include priority cookies in export.
462 This option can also be set with the #+OPTIONS line,
463 e.g. \"pri:t\"."
464 :group 'org-export-general
465 :type 'boolean)
467 (defcustom org-export-with-section-numbers t
468 "Non-nil means add section numbers to headlines when exporting.
470 When set to an integer n, numbering will only happen for
471 headlines whose relative level is higher or equal to n.
473 This option can also be set with the #+OPTIONS line,
474 e.g. \"num:t\"."
475 :group 'org-export-general
476 :type 'boolean)
478 (defcustom org-export-select-tags '("export")
479 "Tags that select a tree for export.
481 If any such tag is found in a buffer, all trees that do not carry
482 one of these tags will be ignored during export. Inside trees
483 that are selected like this, you can still deselect a subtree by
484 tagging it with one of the `org-export-exclude-tags'.
486 This option can also be set with the #+SELECT_TAGS: keyword."
487 :group 'org-export-general
488 :type '(repeat (string :tag "Tag")))
490 (defcustom org-export-with-special-strings t
491 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
493 When this option is turned on, these strings will be exported as:
495 Org HTML LaTeX UTF-8
496 -----+----------+--------+-------
497 \\- &shy; \\-
498 -- &ndash; -- –
499 --- &mdash; --- —
500 ... &hellip; \\ldots …
502 This option can also be set with the #+OPTIONS line,
503 e.g. \"-:nil\"."
504 :group 'org-export-general
505 :type 'boolean)
507 (defcustom org-export-with-statistics-cookies t
508 "Non-nil means include statistics cookies in export.
509 This option can also be set with the #+OPTIONS: line,
510 e.g. \"stat:nil\""
511 :group 'org-export-general
512 :type 'boolean)
514 (defcustom org-export-with-sub-superscripts t
515 "Non-nil means interpret \"_\" and \"^\" for export.
517 When this option is turned on, you can use TeX-like syntax for
518 sub- and superscripts. Several characters after \"_\" or \"^\"
519 will be considered as a single item - so grouping with {} is
520 normally not needed. For example, the following things will be
521 parsed as single sub- or superscripts.
523 10^24 or 10^tau several digits will be considered 1 item.
524 10^-12 or 10^-tau a leading sign with digits or a word
525 x^2-y^3 will be read as x^2 - y^3, because items are
526 terminated by almost any nonword/nondigit char.
527 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
529 Still, ambiguity is possible - so when in doubt use {} to enclose
530 the sub/superscript. If you set this variable to the symbol
531 `{}', the braces are *required* in order to trigger
532 interpretations as sub/superscript. This can be helpful in
533 documents that need \"_\" frequently in plain text.
535 This option can also be set with the #+OPTIONS line,
536 e.g. \"^:nil\"."
537 :group 'org-export-general
538 :type '(choice
539 (const :tag "Interpret them" t)
540 (const :tag "Curly brackets only" {})
541 (const :tag "Do not interpret them" nil)))
543 (defcustom org-export-with-toc t
544 "Non-nil means create a table of contents in exported files.
546 The TOC contains headlines with levels up
547 to`org-export-headline-levels'. When an integer, include levels
548 up to N in the toc, this may then be different from
549 `org-export-headline-levels', but it will not be allowed to be
550 larger than the number of headline levels. When nil, no table of
551 contents is made.
553 This option can also be set with the #+OPTIONS line,
554 e.g. \"toc:nil\" or \"toc:3\"."
555 :group 'org-export-general
556 :type '(choice
557 (const :tag "No Table of Contents" nil)
558 (const :tag "Full Table of Contents" t)
559 (integer :tag "TOC to level")))
561 (defcustom org-export-with-tables t
562 "If non-nil, lines starting with \"|\" define a table.
563 For example:
565 | Name | Address | Birthday |
566 |-------------+----------+-----------|
567 | Arthur Dent | England | 29.2.2100 |
569 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
570 :group 'org-export-general
571 :type 'boolean)
573 (defcustom org-export-with-tags t
574 "If nil, do not export tags, just remove them from headlines.
576 If this is the symbol `not-in-toc', tags will be removed from
577 table of contents entries, but still be shown in the headlines of
578 the document.
580 This option can also be set with the #+OPTIONS line,
581 e.g. \"tags:nil\"."
582 :group 'org-export-general
583 :type '(choice
584 (const :tag "Off" nil)
585 (const :tag "Not in TOC" not-in-toc)
586 (const :tag "On" t)))
588 (defcustom org-export-with-tasks t
589 "Non-nil means include TODO items for export.
590 This may have the following values:
591 t include tasks independent of state.
592 todo include only tasks that are not yet done.
593 done include only tasks that are already done.
594 nil remove all tasks before export
595 list of keywords keep only tasks with these keywords"
596 :group 'org-export-general
597 :type '(choice
598 (const :tag "All tasks" t)
599 (const :tag "No tasks" nil)
600 (const :tag "Not-done tasks" todo)
601 (const :tag "Only done tasks" done)
602 (repeat :tag "Specific TODO keywords"
603 (string :tag "Keyword"))))
605 (defcustom org-export-time-stamp-file t
606 "Non-nil means insert a time stamp into the exported file.
607 The time stamp shows when the file was created.
609 This option can also be set with the #+OPTIONS line,
610 e.g. \"timestamp:nil\"."
611 :group 'org-export-general
612 :type 'boolean)
614 (defcustom org-export-with-timestamps t
615 "Non nil means allow timestamps in export.
617 It can be set to `active', `inactive', t or nil, in order to
618 export, respectively, only active timestamps, only inactive ones,
619 all of them or none.
621 This option can also be set with the #+OPTIONS line, e.g.
622 \"<:nil\"."
623 :group 'org-export-general
624 :type '(choice
625 (const :tag "All timestamps" t)
626 (const :tag "Only active timestamps" active)
627 (const :tag "Only inactive timestamps" inactive)
628 (const :tag "No timestamp" nil)))
630 (defcustom org-export-with-todo-keywords t
631 "Non-nil means include TODO keywords in export.
632 When nil, remove all these keywords from the export."
633 :group 'org-export-general
634 :type 'boolean)
636 (defcustom org-export-allow-BIND 'confirm
637 "Non-nil means allow #+BIND to define local variable values for export.
638 This is a potential security risk, which is why the user must
639 confirm the use of these lines."
640 :group 'org-export-general
641 :type '(choice
642 (const :tag "Never" nil)
643 (const :tag "Always" t)
644 (const :tag "Ask a confirmation for each file" confirm)))
646 (defcustom org-export-snippet-translation-alist nil
647 "Alist between export snippets back-ends and exporter back-ends.
649 This variable allows to provide shortcuts for export snippets.
651 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
652 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
653 HTML code while every other back-end will ignore it."
654 :group 'org-export-general
655 :type '(repeat
656 (cons
657 (string :tag "Shortcut")
658 (string :tag "Back-end"))))
660 (defcustom org-export-coding-system nil
661 "Coding system for the exported file."
662 :group 'org-export-general
663 :type 'coding-system)
665 (defcustom org-export-copy-to-kill-ring t
666 "Non-nil means exported stuff will also be pushed onto the kill ring."
667 :group 'org-export-general
668 :type 'boolean)
670 (defcustom org-export-initial-scope 'buffer
671 "The initial scope when exporting with `org-export-dispatch'.
672 This variable can be either set to `buffer' or `subtree'."
673 :group 'org-export-general
674 :type '(choice
675 (const :tag "Export current buffer" 'buffer)
676 (const :tag "Export current subtree" 'subtree)))
678 (defcustom org-export-show-temporary-export-buffer t
679 "Non-nil means show buffer after exporting to temp buffer.
680 When Org exports to a file, the buffer visiting that file is ever
681 shown, but remains buried. However, when exporting to
682 a temporary buffer, that buffer is popped up in a second window.
683 When this variable is nil, the buffer remains buried also in
684 these cases."
685 :group 'org-export-general
686 :type 'boolean)
688 (defcustom org-export-dispatch-use-expert-ui nil
689 "Non-nil means using a non-intrusive `org-export-dispatch'.
690 In that case, no help buffer is displayed. Though, an indicator
691 for current export scope is added to the prompt \(i.e. \"b\" when
692 output is restricted to body only, \"s\" when it is restricted to
693 the current subtree and \"v\" when only visible elements are
694 considered for export\). Also, \[?] allows to switch back to
695 standard mode."
696 :group 'org-export-general
697 :type 'boolean)
701 ;;; Defining New Back-ends
703 (defmacro org-export-define-backend (backend translators &rest body)
704 "Define a new back-end BACKEND.
706 TRANSLATORS is an alist between object or element types and
707 functions handling them.
709 These functions should return a string without any trailing
710 space, or nil. They must accept three arguments: the object or
711 element itself, its contents or nil when it isn't recursive and
712 the property list used as a communication channel.
714 Contents, when not nil, are stripped from any global indentation
715 \(although the relative one is preserved). They also always end
716 with a single newline character.
718 If, for a given type, no function is found, that element or
719 object type will simply be ignored, along with any blank line or
720 white space at its end. The same will happen if the function
721 returns the nil value. If that function returns the empty
722 string, the type will be ignored, but the blank lines or white
723 spaces will be kept.
725 In addition to element and object types, one function can be
726 associated to the `template' symbol and another one to the
727 `plain-text' symbol.
729 The former returns the final transcoded string, and can be used
730 to add a preamble and a postamble to document's body. It must
731 accept two arguments: the transcoded string and the property list
732 containing export options.
734 The latter, when defined, is to be called on every text not
735 recognized as an element or an object. It must accept two
736 arguments: the text string and the information channel. It is an
737 appropriate place to protect special chars relative to the
738 back-end.
740 BODY can start with pre-defined keyword arguments. The following
741 keywords are understood:
743 :export-block
745 String, or list of strings, representing block names that
746 will not be parsed. This is used to specify blocks that will
747 contain raw code specific to the back-end. These blocks
748 still have to be handled by the relative `export-block' type
749 translator.
751 :filters-alist
753 Alist between filters and function, or list of functions,
754 specific to the back-end. See `org-export-filters-alist' for
755 a list of all allowed filters. Filters defined here
756 shouldn't make a back-end test, as it may prevent back-ends
757 derived from this one to behave properly.
759 :menu-entry
761 Menu entry for the export dispatcher. It should be a list
762 like:
764 \(KEY DESCRIPTION ACTION-OR-MENU)
766 where :
768 KEY is a free character selecting the back-end.
769 DESCRIPTION is a string naming the back-end.
770 ACTION-OR-MENU is either a function or an alist.
772 If it is an action, it will be called with three arguments:
773 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
774 for further explanations.
776 If it is an alist, associations should follow the
777 pattern:
779 \(KEY DESCRIPTION ACTION)
781 where KEY, DESCRIPTION and ACTION are described above.
783 Valid values include:
785 \(?m \"My Special Back-end\" my-special-export-function)
789 \(?l \"Export to LaTeX\"
790 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
791 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
792 \(?p \"PDF file\" org-e-latex-export-to-pdf)
793 \(?o \"PDF file and open\"
794 \(lambda (subtree visible body-only)
795 \(org-open-file
796 \(org-e-latex-export-to-pdf subtree visible body-only))))))
798 :options-alist
800 Alist between back-end specific properties introduced in
801 communication channel and how their value are acquired. See
802 `org-export-options-alist' for more information about
803 structure of the values."
804 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
805 (indent 1))
806 (let (export-block filters menu-entry options)
807 (while (keywordp (car body))
808 (case (pop body)
809 (:export-block (let ((names (pop body)))
810 (setq export-block
811 (if (consp names) (mapcar 'upcase names)
812 (list (upcase names))))))
813 (:filters-alist (setq filters (pop body)))
814 (:menu-entry (setq menu-entry (pop body)))
815 (:options-alist (setq options (pop body)))
816 (t (pop body))))
817 `(progn
818 ;; Define translators.
819 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
820 "Alist between element or object types and translators.")
821 ;; Define options.
822 ,(when options
823 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
824 ,(format "Alist between %s export properties and ways to set them.
825 See `org-export-options-alist' for more information on the
826 structure of the values."
827 backend)))
828 ;; Define filters.
829 ,(when filters
830 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
831 "Alist between filters keywords and back-end specific filters.
832 See `org-export-filters-alist' for more information."))
833 ;; Tell parser to not parse EXPORT-BLOCK blocks.
834 ,(when export-block
835 `(mapc
836 (lambda (name)
837 (add-to-list 'org-element-block-name-alist
838 `(,name . org-element-export-block-parser)))
839 ',export-block))
840 ;; Add an entry for back-end in `org-export-dispatch'.
841 ,(when menu-entry
842 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
843 (unless menu
844 `(push ',menu-entry org-export-dispatch-menu-entries))))
845 ;; Splice in the body, if any.
846 ,@body)))
848 (defmacro org-export-define-derived-backend (child parent &rest body)
849 "Create a new back-end as a variant of an existing one.
851 CHILD is the name of the derived back-end. PARENT is the name of
852 the parent back-end.
854 BODY can start with pre-defined keyword arguments. The following
855 keywords are understood:
857 :export-block
859 String, or list of strings, representing block names that
860 will not be parsed. This is used to specify blocks that will
861 contain raw code specific to the back-end. These blocks
862 still have to be handled by the relative `export-block' type
863 translator.
865 :filters-alist
867 Alist of filters that will overwrite or complete filters
868 defined in PARENT back-end. See `org-export-filters-alist'
869 for a list of allowed filters.
871 :menu-entry
873 Menu entry for the export dispatcher. See
874 `org-export-define-backend' for more information about the
875 expected value.
877 :options-alist
879 Alist of back-end specific properties that will overwrite or
880 complete those defined in PARENT back-end. Refer to
881 `org-export-options-alist' for more information about
882 structure of the values.
884 :sub-menu-entry
886 Append entries to an existing menu in the export dispatcher.
887 The associated value should be a list whose CAR is the
888 character selecting the menu to expand and CDR a list of
889 entries following the pattern:
891 \(KEY DESCRIPTION ACTION)
893 where KEY is a free character triggering the action,
894 DESCRIPTION is a string defining the action, and ACTION is
895 a function that will be called with three arguments:
896 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
897 for further explanations.
899 Valid values include:
901 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
902 \(?O \"As PDF file and open (Beamer)\"
903 \(lambda (s v b)
904 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
906 :translate-alist
908 Alist of element and object types and transcoders that will
909 overwrite or complete transcode table from PARENT back-end.
910 Refer to `org-export-define-backend' for detailed information
911 about transcoders.
913 As an example, here is how one could define \"my-latex\" back-end
914 as a variant of `e-latex' back-end with a custom template
915 function:
917 \(org-export-define-derived-backend my-latex e-latex
918 :translate-alist ((template . my-latex-template-fun)))
920 The back-end could then be called with, for example:
922 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
923 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
924 (indent 2))
925 (let (export-block filters menu-entry options sub-menu-entry translate)
926 (while (keywordp (car body))
927 (case (pop body)
928 (:export-block (let ((names (pop body)))
929 (setq export-block
930 (if (consp names) (mapcar 'upcase names)
931 (list (upcase names))))))
932 (:filters-alist (setq filters (pop body)))
933 (:menu-entry (setq menu-entry (pop body)))
934 (:options-alist (setq options (pop body)))
935 (:sub-menu-entry (setq sub-menu-entry (pop body)))
936 (:translate-alist (setq translate (pop body)))
937 (t (pop body))))
938 `(progn
939 ;; Tell parser to not parse EXPORT-BLOCK blocks.
940 ,(when export-block
941 `(mapc
942 (lambda (name)
943 (add-to-list 'org-element-block-name-alist
944 `(,name . org-element-export-block-parser)))
945 ',export-block))
946 ;; Define filters.
947 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
948 (when (or (boundp parent-filters) filters)
949 `(defconst ,(intern (format "org-%s-filters-alist" child))
950 ',(append filters
951 (and (boundp parent-filters)
952 (copy-sequence (symbol-value parent-filters))))
953 "Alist between filters keywords and back-end specific filters.
954 See `org-export-filters-alist' for more information.")))
955 ;; Define options.
956 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
957 (when (or (boundp parent-options) options)
958 `(defconst ,(intern (format "org-%s-options-alist" child))
959 ',(append options
960 (and (boundp parent-options)
961 (copy-sequence (symbol-value parent-options))))
962 ,(format "Alist between %s export properties and ways to set them.
963 See `org-export-options-alist' for more information on the
964 structure of the values."
965 child))))
966 ;; Define translators.
967 (defvar ,(intern (format "org-%s-translate-alist" child))
968 ',(append translate
969 (copy-sequence
970 (symbol-value
971 (intern (format "org-%s-translate-alist" parent)))))
972 "Alist between element or object types and translators.")
973 ;; Add an entry for back-end in `org-export-dispatch'.
974 ,(when menu-entry
975 (let ((menu (assq (car menu-entry) org-export-dispatch-menu-entries)))
976 (unless menu
977 `(push ',menu-entry org-export-dispatch-menu-entries))))
978 ,(when sub-menu-entry
979 (let ((menu (nth 2 (assq (car sub-menu-entry)
980 org-export-dispatch-menu-entries))))
981 (when menu `(nconc ',menu
982 ',(org-remove-if (lambda (e) (member e menu))
983 (cdr sub-menu-entry))))))
984 ;; Splice in the body, if any.
985 ,@body)))
989 ;;; The Communication Channel
991 ;; During export process, every function has access to a number of
992 ;; properties. They are of two types:
994 ;; 1. Environment options are collected once at the very beginning of
995 ;; the process, out of the original buffer and configuration.
996 ;; Collecting them is handled by `org-export-get-environment'
997 ;; function.
999 ;; Most environment options are defined through the
1000 ;; `org-export-options-alist' variable.
1002 ;; 2. Tree properties are extracted directly from the parsed tree,
1003 ;; just before export, by `org-export-collect-tree-properties'.
1005 ;; Here is the full list of properties available during transcode
1006 ;; process, with their category and their value type.
1008 ;; + `:author' :: Author's name.
1009 ;; - category :: option
1010 ;; - type :: string
1012 ;; + `:back-end' :: Current back-end used for transcoding.
1013 ;; - category :: tree
1014 ;; - type :: symbol
1016 ;; + `:creator' :: String to write as creation information.
1017 ;; - category :: option
1018 ;; - type :: string
1020 ;; + `:date' :: String to use as date.
1021 ;; - category :: option
1022 ;; - type :: string
1024 ;; + `:description' :: Description text for the current data.
1025 ;; - category :: option
1026 ;; - type :: string
1028 ;; + `:email' :: Author's email.
1029 ;; - category :: option
1030 ;; - type :: string
1032 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1033 ;; process.
1034 ;; - category :: option
1035 ;; - type :: list of strings
1037 ;; + `:exported-data' :: Hash table used for memoizing
1038 ;; `org-export-data'.
1039 ;; - category :: tree
1040 ;; - type :: hash table
1042 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1043 ;; their definition, as parsed data. Only non-inlined footnotes
1044 ;; are represented in this alist. Also, every definition isn't
1045 ;; guaranteed to be referenced in the parse tree. The purpose of
1046 ;; this property is to preserve definitions from oblivion
1047 ;; (i.e. when the parse tree comes from a part of the original
1048 ;; buffer), it isn't meant for direct use in a back-end. To
1049 ;; retrieve a definition relative to a reference, use
1050 ;; `org-export-get-footnote-definition' instead.
1051 ;; - category :: option
1052 ;; - type :: alist (STRING . LIST)
1054 ;; + `:headline-levels' :: Maximum level being exported as an
1055 ;; headline. Comparison is done with the relative level of
1056 ;; headlines in the parse tree, not necessarily with their
1057 ;; actual level.
1058 ;; - category :: option
1059 ;; - type :: integer
1061 ;; + `:headline-offset' :: Difference between relative and real level
1062 ;; of headlines in the parse tree. For example, a value of -1
1063 ;; means a level 2 headline should be considered as level
1064 ;; 1 (cf. `org-export-get-relative-level').
1065 ;; - category :: tree
1066 ;; - type :: integer
1068 ;; + `:headline-numbering' :: Alist between headlines and their
1069 ;; numbering, as a list of numbers
1070 ;; (cf. `org-export-get-headline-number').
1071 ;; - category :: tree
1072 ;; - type :: alist (INTEGER . LIST)
1074 ;; + `:id-alist' :: Alist between ID strings and destination file's
1075 ;; path, relative to current directory. It is used by
1076 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1077 ;; external file.
1078 ;; - category :: option
1079 ;; - type :: alist (STRING . STRING)
1081 ;; + `:ignore-list' :: List of elements and objects that should be
1082 ;; ignored during export.
1083 ;; - category :: tree
1084 ;; - type :: list of elements and objects
1086 ;; + `:input-file' :: Full path to input file, if any.
1087 ;; - category :: option
1088 ;; - type :: string or nil
1090 ;; + `:keywords' :: List of keywords attached to data.
1091 ;; - category :: option
1092 ;; - type :: string
1094 ;; + `:language' :: Default language used for translations.
1095 ;; - category :: option
1096 ;; - type :: string
1098 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1099 ;; transcoding.
1100 ;; - category :: option
1101 ;; - type :: list (as returned by `org-element-parse-buffer')
1103 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1104 ;; all line breaks.
1105 ;; - category :: option
1106 ;; - type :: symbol (nil, t)
1108 ;; + `:section-numbers' :: Non-nil means transcoding should add
1109 ;; section numbers to headlines.
1110 ;; - category :: option
1111 ;; - type :: symbol (nil, t)
1113 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1114 ;; in transcoding. When such a tag is present, subtrees without
1115 ;; it are de facto excluded from the process. See
1116 ;; `use-select-tags'.
1117 ;; - category :: option
1118 ;; - type :: list of strings
1120 ;; + `:target-list' :: List of targets encountered in the parse tree.
1121 ;; This is used to partly resolve "fuzzy" links
1122 ;; (cf. `org-export-resolve-fuzzy-link').
1123 ;; - category :: tree
1124 ;; - type :: list of strings
1126 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1127 ;; a time stamp in the output.
1128 ;; - category :: option
1129 ;; - type :: symbol (nil, t)
1131 ;; + `:translate-alist' :: Alist between element and object types and
1132 ;; transcoding functions relative to the current back-end.
1133 ;; Special keys `template' and `plain-text' are also possible.
1134 ;; - category :: option
1135 ;; - type :: alist (SYMBOL . FUNCTION)
1137 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1138 ;; also be transcoded. If it is set to the `headline' symbol,
1139 ;; only the archived headline's name is retained.
1140 ;; - category :: option
1141 ;; - type :: symbol (nil, t, `headline')
1143 ;; + `:with-author' :: Non-nil means author's name should be included
1144 ;; in the output.
1145 ;; - category :: option
1146 ;; - type :: symbol (nil, t)
1148 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1149 ;; - category :: option
1150 ;; - type :: symbol (nil, t)
1152 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1153 ;; inserted at the end of the transcoded string. If the value
1154 ;; is `comment', it should be commented.
1155 ;; - category :: option
1156 ;; - type :: symbol (`comment', nil, t)
1158 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1159 ;; its value is a list of names, only drawers with such names
1160 ;; will be transcoded.
1161 ;; - category :: option
1162 ;; - type :: symbol (nil, t) or list of strings
1164 ;; + `:with-email' :: Non-nil means output should contain author's
1165 ;; email.
1166 ;; - category :: option
1167 ;; - type :: symbol (nil, t)
1169 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1170 ;; interpreted.
1171 ;; - category :: option
1172 ;; - type :: symbol (nil, t)
1174 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1175 ;; strings starting with a colon as a fixed-with (verbatim) area.
1176 ;; - category :: option
1177 ;; - type :: symbol (nil, t)
1179 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1180 ;; footnotes.
1181 ;; - category :: option
1182 ;; - type :: symbol (nil, t)
1184 ;; + `:with-plannings' :: Non-nil means transcoding should include
1185 ;; planning info.
1186 ;; - category :: option
1187 ;; - type :: symbol (nil, t)
1189 ;; + `:with-priority' :: Non-nil means transcoding should include
1190 ;; priority cookies.
1191 ;; - category :: option
1192 ;; - type :: symbol (nil, t)
1194 ;; + `:with-special-strings' :: Non-nil means transcoding should
1195 ;; interpret special strings in plain text.
1196 ;; - category :: option
1197 ;; - type :: symbol (nil, t)
1199 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1200 ;; interpret subscript and superscript. With a value of "{}",
1201 ;; only interpret those using curly brackets.
1202 ;; - category :: option
1203 ;; - type :: symbol (nil, {}, t)
1205 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1206 ;; tables.
1207 ;; - category :: option
1208 ;; - type :: symbol (nil, t)
1210 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1211 ;; headlines. A `not-in-toc' value will remove them from the
1212 ;; table of contents, if any, nonetheless.
1213 ;; - category :: option
1214 ;; - type :: symbol (nil, t, `not-in-toc')
1216 ;; + `:with-tasks' :: Non-nil means transcoding should include
1217 ;; headlines with a TODO keyword. A `todo' value will only
1218 ;; include headlines with a todo type keyword while a `done'
1219 ;; value will do the contrary. If a list of strings is provided,
1220 ;; only tasks with keywords belonging to that list will be kept.
1221 ;; - category :: option
1222 ;; - type :: symbol (t, todo, done, nil) or list of strings
1224 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1225 ;; time stamps. Special value `active' (resp. `inactive') ask to
1226 ;; export only active (resp. inactive) timestamps. Otherwise,
1227 ;; completely remove them.
1228 ;; - category :: option
1229 ;; - type :: symbol: (`active', `inactive', t, nil)
1231 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1232 ;; added to the output. An integer value limits its depth.
1233 ;; - category :: option
1234 ;; - type :: symbol (nil, t or integer)
1236 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1237 ;; include TODO keywords.
1238 ;; - category :: option
1239 ;; - type :: symbol (nil, t)
1242 ;;;; Environment Options
1244 ;; Environment options encompass all parameters defined outside the
1245 ;; scope of the parsed data. They come from five sources, in
1246 ;; increasing precedence order:
1248 ;; - Global variables,
1249 ;; - Buffer's attributes,
1250 ;; - Options keyword symbols,
1251 ;; - Buffer keywords,
1252 ;; - Subtree properties.
1254 ;; The central internal function with regards to environment options
1255 ;; is `org-export-get-environment'. It updates global variables with
1256 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1257 ;; the different sources.
1259 ;; The internal functions doing the retrieval are:
1260 ;; `org-export--get-global-options',
1261 ;; `org-export--get-buffer-attributes',
1262 ;; `org-export--parse-option-keyword',
1263 ;; `org-export--get-subtree-options' and
1264 ;; `org-export--get-inbuffer-options'
1266 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1267 ;; take care of the part relative to "#+BIND:" keywords.
1269 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1270 "Collect export options from the current buffer.
1272 Optional argument BACKEND is a symbol specifying which back-end
1273 specific options to read, if any.
1275 When optional argument SUBTREEP is non-nil, assume the export is
1276 done against the current sub-tree.
1278 Third optional argument EXT-PLIST is a property list with
1279 external parameters overriding Org default settings, but still
1280 inferior to file-local settings."
1281 ;; First install #+BIND variables.
1282 (org-export--install-letbind-maybe)
1283 ;; Get and prioritize export options...
1284 (org-combine-plists
1285 ;; ... from global variables...
1286 (org-export--get-global-options backend)
1287 ;; ... from buffer's attributes...
1288 (org-export--get-buffer-attributes)
1289 ;; ... from an external property list...
1290 ext-plist
1291 ;; ... from in-buffer settings...
1292 (org-export--get-inbuffer-options
1293 backend
1294 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1295 ;; ... and from subtree, when appropriate.
1296 (and subtreep (org-export--get-subtree-options backend))
1297 ;; Eventually install back-end symbol and its translation table.
1298 `(:back-end
1299 ,backend
1300 :translate-alist
1301 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1302 (when (boundp trans-alist) (symbol-value trans-alist))))))
1304 (defun org-export--parse-option-keyword (options &optional backend)
1305 "Parse an OPTIONS line and return values as a plist.
1306 Optional argument BACKEND is a symbol specifying which back-end
1307 specific items to read, if any."
1308 (let* ((all
1309 (append org-export-options-alist
1310 (and backend
1311 (let ((var (intern
1312 (format "org-%s-options-alist" backend))))
1313 (and (boundp var) (eval var))))))
1314 ;; Build an alist between #+OPTION: item and property-name.
1315 (alist (delq nil
1316 (mapcar (lambda (e)
1317 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1318 (car e))))
1319 all)))
1320 plist)
1321 (mapc (lambda (e)
1322 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1323 (car e)
1324 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1325 options)
1326 (setq plist (plist-put plist
1327 (cdr e)
1328 (car (read-from-string
1329 (match-string 2 options)))))))
1330 alist)
1331 plist))
1333 (defun org-export--get-subtree-options (&optional backend)
1334 "Get export options in subtree at point.
1335 Optional argument BACKEND is a symbol specifying back-end used
1336 for export. Return options as a plist."
1337 ;; For each buffer keyword, create an headline property setting the
1338 ;; same property in communication channel. The name for the property
1339 ;; is the keyword with "EXPORT_" appended to it.
1340 (org-with-wide-buffer
1341 (let (prop plist)
1342 ;; Make sure point is at an heading.
1343 (unless (org-at-heading-p) (org-back-to-heading t))
1344 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1345 ;; title as its fallback value.
1346 (when (setq prop (progn (looking-at org-todo-line-regexp)
1347 (or (save-match-data
1348 (org-entry-get (point) "EXPORT_TITLE"))
1349 (org-match-string-no-properties 3))))
1350 (setq plist
1351 (plist-put
1352 plist :title
1353 (org-element-parse-secondary-string
1354 prop (org-element-restriction 'keyword)))))
1355 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1356 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1357 (setq plist
1358 (nconc plist (org-export--parse-option-keyword prop backend))))
1359 ;; Handle other keywords.
1360 (let ((seen '("TITLE")))
1361 (mapc
1362 (lambda (option)
1363 (let ((property (nth 1 option)))
1364 (when (and property (not (member property seen)))
1365 (let* ((subtree-prop (concat "EXPORT_" property))
1366 ;; Export properties are not case-sensitive.
1367 (value (let ((case-fold-search t))
1368 (org-entry-get (point) subtree-prop))))
1369 (push property seen)
1370 (when value
1371 (setq plist
1372 (plist-put
1373 plist
1374 (car option)
1375 ;; Parse VALUE if required.
1376 (if (member property org-element-parsed-keywords)
1377 (org-element-parse-secondary-string
1378 value (org-element-restriction 'keyword))
1379 value))))))))
1380 ;; Also look for both general keywords and back-end specific
1381 ;; options if BACKEND is provided.
1382 (append (and backend
1383 (let ((var (intern
1384 (format "org-%s-options-alist" backend))))
1385 (and (boundp var) (symbol-value var))))
1386 org-export-options-alist)))
1387 ;; Return value.
1388 plist)))
1390 (defun org-export--get-inbuffer-options (&optional backend files)
1391 "Return current buffer export options, as a plist.
1393 Optional argument BACKEND, when non-nil, is a symbol specifying
1394 which back-end specific options should also be read in the
1395 process.
1397 Optional argument FILES is a list of setup files names read so
1398 far, used to avoid circular dependencies.
1400 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1401 (org-with-wide-buffer
1402 (goto-char (point-min))
1403 (let ((case-fold-search t) plist)
1404 ;; 1. Special keywords, as in `org-export-special-keywords'.
1405 (let ((special-re
1406 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1407 (while (re-search-forward special-re nil t)
1408 (let ((element (org-element-at-point)))
1409 (when (eq (org-element-type element) 'keyword)
1410 (let* ((key (org-element-property :key element))
1411 (val (org-element-property :value element))
1412 (prop
1413 (cond
1414 ((string= key "SETUP_FILE")
1415 (let ((file
1416 (expand-file-name
1417 (org-remove-double-quotes (org-trim val)))))
1418 ;; Avoid circular dependencies.
1419 (unless (member file files)
1420 (with-temp-buffer
1421 (insert (org-file-contents file 'noerror))
1422 (org-mode)
1423 (org-export--get-inbuffer-options
1424 backend (cons file files))))))
1425 ((string= key "OPTIONS")
1426 (org-export--parse-option-keyword val backend)))))
1427 (setq plist (org-combine-plists plist prop)))))))
1428 ;; 2. Standard options, as in `org-export-options-alist'.
1429 (let* ((all (append org-export-options-alist
1430 ;; Also look for back-end specific options
1431 ;; if BACKEND is defined.
1432 (and backend
1433 (let ((var
1434 (intern
1435 (format "org-%s-options-alist" backend))))
1436 (and (boundp var) (eval var))))))
1437 ;; Build ALIST between keyword name and property name.
1438 (alist
1439 (delq nil (mapcar
1440 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1441 all)))
1442 ;; Build regexp matching all keywords associated to export
1443 ;; options. Note: the search is case insensitive.
1444 (opt-re (format "^[ \t]*#\\+%s:"
1445 (regexp-opt
1446 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1447 (goto-char (point-min))
1448 (while (re-search-forward opt-re nil t)
1449 (let ((element (org-element-at-point)))
1450 (when (eq (org-element-type element) 'keyword)
1451 (let* ((key (org-element-property :key element))
1452 (val (org-element-property :value element))
1453 (prop (cdr (assoc key alist)))
1454 (behaviour (nth 4 (assq prop all))))
1455 (setq plist
1456 (plist-put
1457 plist prop
1458 ;; Handle value depending on specified BEHAVIOUR.
1459 (case behaviour
1460 (space
1461 (if (not (plist-get plist prop)) (org-trim val)
1462 (concat (plist-get plist prop) " " (org-trim val))))
1463 (newline
1464 (org-trim
1465 (concat (plist-get plist prop) "\n" (org-trim val))))
1466 (split
1467 `(,@(plist-get plist prop) ,@(org-split-string val)))
1468 ('t val)
1469 (otherwise (if (not (plist-member plist prop)) val
1470 (plist-get plist prop))))))))))
1471 ;; Parse keywords specified in `org-element-parsed-keywords'.
1472 (mapc
1473 (lambda (key)
1474 (let* ((prop (cdr (assoc key alist)))
1475 (value (and prop (plist-get plist prop))))
1476 (when (stringp value)
1477 (setq plist
1478 (plist-put
1479 plist prop
1480 (org-element-parse-secondary-string
1481 value (org-element-restriction 'keyword)))))))
1482 org-element-parsed-keywords))
1483 ;; 3. Return final value.
1484 plist)))
1486 (defun org-export--get-buffer-attributes ()
1487 "Return properties related to buffer attributes, as a plist."
1488 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1489 (list
1490 ;; Store full path of input file name, or nil. For internal use.
1491 :input-file visited-file
1492 :title (or (and visited-file
1493 (file-name-sans-extension
1494 (file-name-nondirectory visited-file)))
1495 (buffer-name (buffer-base-buffer)))
1496 :footnote-definition-alist
1497 ;; Footnotes definitions must be collected in the original
1498 ;; buffer, as there's no insurance that they will still be in the
1499 ;; parse tree, due to possible narrowing.
1500 (let (alist)
1501 (org-with-wide-buffer
1502 (goto-char (point-min))
1503 (while (re-search-forward org-footnote-definition-re nil t)
1504 (let ((def (org-footnote-at-definition-p)))
1505 (when def
1506 (org-skip-whitespace)
1507 (push (cons (car def)
1508 (save-restriction
1509 (narrow-to-region (point) (nth 2 def))
1510 ;; Like `org-element-parse-buffer', but
1511 ;; makes sure the definition doesn't start
1512 ;; with a section element.
1513 (org-element--parse-elements
1514 (point-min) (point-max) nil nil nil nil
1515 (list 'org-data nil))))
1516 alist))))
1517 alist))
1518 :id-alist
1519 ;; Collect id references.
1520 (let (alist)
1521 (org-with-wide-buffer
1522 (goto-char (point-min))
1523 (while (re-search-forward
1524 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1525 (let* ((id (org-match-string-no-properties 1))
1526 (file (org-id-find-id-file id)))
1527 (when file (push (cons id (file-relative-name file)) alist)))))
1528 alist))))
1530 (defun org-export--get-global-options (&optional backend)
1531 "Return global export options as a plist.
1533 Optional argument BACKEND, if non-nil, is a symbol specifying
1534 which back-end specific export options should also be read in the
1535 process."
1536 (let ((all (append org-export-options-alist
1537 (and backend
1538 (let ((var (intern
1539 (format "org-%s-options-alist" backend))))
1540 (and (boundp var) (symbol-value var))))))
1541 ;; Output value.
1542 plist)
1543 (mapc
1544 (lambda (cell)
1545 (setq plist
1546 (plist-put
1547 plist
1548 (car cell)
1549 ;; Eval default value provided. If keyword is a member
1550 ;; of `org-element-parsed-keywords', parse it as
1551 ;; a secondary string before storing it.
1552 (let ((value (eval (nth 3 cell))))
1553 (if (not (stringp value)) value
1554 (let ((keyword (nth 1 cell)))
1555 (if (not (member keyword org-element-parsed-keywords)) value
1556 (org-element-parse-secondary-string
1557 value (org-element-restriction 'keyword)))))))))
1558 all)
1559 ;; Return value.
1560 plist))
1562 (defvar org-export--allow-BIND-local nil)
1563 (defun org-export--confirm-letbind ()
1564 "Can we use #+BIND values during export?
1565 By default this will ask for confirmation by the user, to divert
1566 possible security risks."
1567 (cond
1568 ((not org-export-allow-BIND) nil)
1569 ((eq org-export-allow-BIND t) t)
1570 ((local-variable-p 'org-export--allow-BIND-local)
1571 org-export--allow-BIND-local)
1572 (t (org-set-local 'org-export--allow-BIND-local
1573 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1575 (defun org-export--install-letbind-maybe ()
1576 "Install the values from #+BIND lines as local variables.
1577 Variables must be installed before in-buffer options are
1578 retrieved."
1579 (let ((case-fold-search t) letbind pair)
1580 (org-with-wide-buffer
1581 (goto-char (point-min))
1582 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1583 (let* ((element (org-element-at-point))
1584 (value (org-element-property :value element)))
1585 (when (and (eq (org-element-type element) 'keyword)
1586 (not (equal value ""))
1587 (org-export--confirm-letbind))
1588 (push (read (format "(%s)" value)) letbind)))))
1589 (dolist (pair (nreverse letbind))
1590 (org-set-local (car pair) (nth 1 pair)))))
1593 ;;;; Tree Properties
1595 ;; Tree properties are infromation extracted from parse tree. They
1596 ;; are initialized at the beginning of the transcoding process by
1597 ;; `org-export-collect-tree-properties'.
1599 ;; Dedicated functions focus on computing the value of specific tree
1600 ;; properties during initialization. Thus,
1601 ;; `org-export--populate-ignore-list' lists elements and objects that
1602 ;; should be skipped during export, `org-export--get-min-level' gets
1603 ;; the minimal exportable level, used as a basis to compute relative
1604 ;; level for headlines. Eventually
1605 ;; `org-export--collect-headline-numbering' builds an alist between
1606 ;; headlines and their numbering.
1608 (defun org-export-collect-tree-properties (data info)
1609 "Extract tree properties from parse tree.
1611 DATA is the parse tree from which information is retrieved. INFO
1612 is a list holding export options.
1614 Following tree properties are set or updated:
1616 `:exported-data' Hash table used to memoize results from
1617 `org-export-data'.
1619 `:footnote-definition-alist' List of footnotes definitions in
1620 original buffer and current parse tree.
1622 `:headline-offset' Offset between true level of headlines and
1623 local level. An offset of -1 means an headline
1624 of level 2 should be considered as a level
1625 1 headline in the context.
1627 `:headline-numbering' Alist of all headlines as key an the
1628 associated numbering as value.
1630 `:ignore-list' List of elements that should be ignored during
1631 export.
1633 `:target-list' List of all targets in the parse tree.
1635 Return updated plist."
1636 ;; Install the parse tree in the communication channel, in order to
1637 ;; use `org-export-get-genealogy' and al.
1638 (setq info (plist-put info :parse-tree data))
1639 ;; Get the list of elements and objects to ignore, and put it into
1640 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1641 ;; been done during parse tree filtering.
1642 (setq info
1643 (plist-put info
1644 :ignore-list
1645 (append (org-export--populate-ignore-list data info)
1646 (plist-get info :ignore-list))))
1647 ;; Compute `:headline-offset' in order to be able to use
1648 ;; `org-export-get-relative-level'.
1649 (setq info
1650 (plist-put info
1651 :headline-offset
1652 (- 1 (org-export--get-min-level data info))))
1653 ;; Update footnotes definitions list with definitions in parse tree.
1654 ;; This is required since buffer expansion might have modified
1655 ;; boundaries of footnote definitions contained in the parse tree.
1656 ;; This way, definitions in `footnote-definition-alist' are bound to
1657 ;; match those in the parse tree.
1658 (let ((defs (plist-get info :footnote-definition-alist)))
1659 (org-element-map
1660 data 'footnote-definition
1661 (lambda (fn)
1662 (push (cons (org-element-property :label fn)
1663 `(org-data nil ,@(org-element-contents fn)))
1664 defs)))
1665 (setq info (plist-put info :footnote-definition-alist defs)))
1666 ;; Properties order doesn't matter: get the rest of the tree
1667 ;; properties.
1668 (nconc
1669 `(:target-list
1670 ,(org-element-map
1671 data '(keyword target)
1672 (lambda (blob)
1673 (when (or (eq (org-element-type blob) 'target)
1674 (string= (org-element-property :key blob) "TARGET"))
1675 blob)) info)
1676 :headline-numbering ,(org-export--collect-headline-numbering data info)
1677 :exported-data ,(make-hash-table :test 'eq :size 4001))
1678 info))
1680 (defun org-export--get-min-level (data options)
1681 "Return minimum exportable headline's level in DATA.
1682 DATA is parsed tree as returned by `org-element-parse-buffer'.
1683 OPTIONS is a plist holding export options."
1684 (catch 'exit
1685 (let ((min-level 10000))
1686 (mapc
1687 (lambda (blob)
1688 (when (and (eq (org-element-type blob) 'headline)
1689 (not (memq blob (plist-get options :ignore-list))))
1690 (setq min-level
1691 (min (org-element-property :level blob) min-level)))
1692 (when (= min-level 1) (throw 'exit 1)))
1693 (org-element-contents data))
1694 ;; If no headline was found, for the sake of consistency, set
1695 ;; minimum level to 1 nonetheless.
1696 (if (= min-level 10000) 1 min-level))))
1698 (defun org-export--collect-headline-numbering (data options)
1699 "Return numbering of all exportable headlines in a parse tree.
1701 DATA is the parse tree. OPTIONS is the plist holding export
1702 options.
1704 Return an alist whose key is an headline and value is its
1705 associated numbering \(in the shape of a list of numbers\)."
1706 (let ((numbering (make-vector org-export-max-depth 0)))
1707 (org-element-map
1708 data
1709 'headline
1710 (lambda (headline)
1711 (let ((relative-level
1712 (1- (org-export-get-relative-level headline options))))
1713 (cons
1714 headline
1715 (loop for n across numbering
1716 for idx from 0 to org-export-max-depth
1717 when (< idx relative-level) collect n
1718 when (= idx relative-level) collect (aset numbering idx (1+ n))
1719 when (> idx relative-level) do (aset numbering idx 0)))))
1720 options)))
1722 (defun org-export--populate-ignore-list (data options)
1723 "Return list of elements and objects to ignore during export.
1724 DATA is the parse tree to traverse. OPTIONS is the plist holding
1725 export options."
1726 (let* (ignore
1727 walk-data
1728 ;; First find trees containing a select tag, if any.
1729 (selected (org-export--selected-trees data options))
1730 (walk-data
1731 (lambda (data)
1732 ;; Collect ignored elements or objects into IGNORE-LIST.
1733 (let ((type (org-element-type data)))
1734 (if (org-export--skip-p data options selected) (push data ignore)
1735 (if (and (eq type 'headline)
1736 (eq (plist-get options :with-archived-trees) 'headline)
1737 (org-element-property :archivedp data))
1738 ;; If headline is archived but tree below has
1739 ;; to be skipped, add it to ignore list.
1740 (mapc (lambda (e) (push e ignore))
1741 (org-element-contents data))
1742 ;; Move into secondary string, if any.
1743 (let ((sec-prop
1744 (cdr (assq type org-element-secondary-value-alist))))
1745 (when sec-prop
1746 (mapc walk-data (org-element-property sec-prop data))))
1747 ;; Move into recursive objects/elements.
1748 (mapc walk-data (org-element-contents data))))))))
1749 ;; Main call.
1750 (funcall walk-data data)
1751 ;; Return value.
1752 ignore))
1754 (defun org-export--selected-trees (data info)
1755 "Return list of headlines containing a select tag in their tree.
1756 DATA is parsed data as returned by `org-element-parse-buffer'.
1757 INFO is a plist holding export options."
1758 (let* (selected-trees
1759 walk-data ; for byte-compiler.
1760 (walk-data
1761 (function
1762 (lambda (data genealogy)
1763 (case (org-element-type data)
1764 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1765 (org-element-contents data)))
1766 (headline
1767 (let ((tags (org-element-property :tags data)))
1768 (if (loop for tag in (plist-get info :select-tags)
1769 thereis (member tag tags))
1770 ;; When a select tag is found, mark full
1771 ;; genealogy and every headline within the tree
1772 ;; as acceptable.
1773 (setq selected-trees
1774 (append
1775 genealogy
1776 (org-element-map data 'headline 'identity)
1777 selected-trees))
1778 ;; Else, continue searching in tree, recursively.
1779 (mapc
1780 (lambda (el) (funcall walk-data el (cons data genealogy)))
1781 (org-element-contents data))))))))))
1782 (funcall walk-data data nil) selected-trees))
1784 (defun org-export--skip-p (blob options selected)
1785 "Non-nil when element or object BLOB should be skipped during export.
1786 OPTIONS is the plist holding export options. SELECTED, when
1787 non-nil, is a list of headlines belonging to a tree with a select
1788 tag."
1789 (case (org-element-type blob)
1790 (clock (not (plist-get options :with-clocks)))
1791 (drawer
1792 (or (not (plist-get options :with-drawers))
1793 (and (consp (plist-get options :with-drawers))
1794 (not (member (org-element-property :drawer-name blob)
1795 (plist-get options :with-drawers))))))
1796 (headline
1797 (let ((with-tasks (plist-get options :with-tasks))
1798 (todo (org-element-property :todo-keyword blob))
1799 (todo-type (org-element-property :todo-type blob))
1800 (archived (plist-get options :with-archived-trees))
1801 (tags (org-element-property :tags blob)))
1803 ;; Ignore subtrees with an exclude tag.
1804 (loop for k in (plist-get options :exclude-tags)
1805 thereis (member k tags))
1806 ;; When a select tag is present in the buffer, ignore any tree
1807 ;; without it.
1808 (and selected (not (memq blob selected)))
1809 ;; Ignore commented sub-trees.
1810 (org-element-property :commentedp blob)
1811 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1812 (and (not archived) (org-element-property :archivedp blob))
1813 ;; Ignore tasks, if specified by `:with-tasks' property.
1814 (and todo
1815 (or (not with-tasks)
1816 (and (memq with-tasks '(todo done))
1817 (not (eq todo-type with-tasks)))
1818 (and (consp with-tasks) (not (member todo with-tasks))))))))
1819 (inlinetask (not (plist-get options :with-inlinetasks)))
1820 (planning (not (plist-get options :with-plannings)))
1821 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1822 (table-cell
1823 (and (org-export-table-has-special-column-p
1824 (org-export-get-parent-table blob))
1825 (not (org-export-get-previous-element blob options))))
1826 (table-row (org-export-table-row-is-special-p blob options))
1827 (timestamp
1828 (case (plist-get options :with-timestamps)
1829 ;; No timestamp allowed.
1830 ('nil t)
1831 ;; Only active timestamps allowed and the current one isn't
1832 ;; active.
1833 (active
1834 (not (memq (org-element-property :type blob)
1835 '(active active-range))))
1836 ;; Only inactive timestamps allowed and the current one isn't
1837 ;; inactive.
1838 (inactive
1839 (not (memq (org-element-property :type blob)
1840 '(inactive inactive-range))))))))
1844 ;;; The Transcoder
1846 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1847 ;; `org-element-parse-buffer') and transcodes it into a specified
1848 ;; back-end output. It takes care of filtering out elements or
1849 ;; objects according to export options and organizing the output blank
1850 ;; lines and white space are preserved. The function memoizes its
1851 ;; results, so it is cheap to call it within translators.
1853 ;; Internally, three functions handle the filtering of objects and
1854 ;; elements during the export. In particular,
1855 ;; `org-export-ignore-element' marks an element or object so future
1856 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1857 ;; elements or objects should be seen as real Org syntax and
1858 ;; `org-export-expand' transforms the others back into their original
1859 ;; shape
1861 ;; `org-export-transcoder' is an accessor returning appropriate
1862 ;; translator function for a given element or object.
1864 (defun org-export-transcoder (blob info)
1865 "Return appropriate transcoder for BLOB.
1866 INFO is a plist containing export directives."
1867 (let ((type (org-element-type blob)))
1868 ;; Return contents only for complete parse trees.
1869 (if (eq type 'org-data) (lambda (blob contents info) contents)
1870 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1871 (and (functionp transcoder) transcoder)))))
1873 (defun org-export-data (data info)
1874 "Convert DATA into current back-end format.
1876 DATA is a parse tree, an element or an object or a secondary
1877 string. INFO is a plist holding export options.
1879 Return transcoded string."
1880 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1881 (if (not (eq memo 'no-memo)) memo
1882 (let* ((type (org-element-type data))
1883 (results
1884 (cond
1885 ;; Ignored element/object.
1886 ((memq data (plist-get info :ignore-list)) nil)
1887 ;; Plain text.
1888 ((eq type 'plain-text)
1889 (org-export-filter-apply-functions
1890 (plist-get info :filter-plain-text)
1891 (let ((transcoder (org-export-transcoder data info)))
1892 (if transcoder (funcall transcoder data info) data))
1893 info))
1894 ;; Uninterpreted element/object: change it back to Org
1895 ;; syntax and export again resulting raw string.
1896 ((not (org-export--interpret-p data info))
1897 (org-export-data
1898 (org-export-expand
1899 data
1900 (mapconcat (lambda (blob) (org-export-data blob info))
1901 (org-element-contents data)
1902 ""))
1903 info))
1904 ;; Secondary string.
1905 ((not type)
1906 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1907 ;; Element/Object without contents or, as a special case,
1908 ;; headline with archive tag and archived trees restricted
1909 ;; to title only.
1910 ((or (not (org-element-contents data))
1911 (and (eq type 'headline)
1912 (eq (plist-get info :with-archived-trees) 'headline)
1913 (org-element-property :archivedp data)))
1914 (let ((transcoder (org-export-transcoder data info)))
1915 (and (functionp transcoder)
1916 (funcall transcoder data nil info))))
1917 ;; Element/Object with contents.
1919 (let ((transcoder (org-export-transcoder data info)))
1920 (when transcoder
1921 (let* ((greaterp (memq type org-element-greater-elements))
1922 (objectp
1923 (and (not greaterp)
1924 (memq type org-element-recursive-objects)))
1925 (contents
1926 (mapconcat
1927 (lambda (element) (org-export-data element info))
1928 (org-element-contents
1929 (if (or greaterp objectp) data
1930 ;; Elements directly containing objects
1931 ;; must have their indentation normalized
1932 ;; first.
1933 (org-element-normalize-contents
1934 data
1935 ;; When normalizing contents of the first
1936 ;; paragraph in an item or a footnote
1937 ;; definition, ignore first line's
1938 ;; indentation: there is none and it
1939 ;; might be misleading.
1940 (when (eq type 'paragraph)
1941 (let ((parent (org-export-get-parent data)))
1942 (and
1943 (eq (car (org-element-contents parent))
1944 data)
1945 (memq (org-element-type parent)
1946 '(footnote-definition item))))))))
1947 "")))
1948 (funcall transcoder data
1949 (if (not greaterp) contents
1950 (org-element-normalize-string contents))
1951 info))))))))
1952 ;; Final result will be memoized before being returned.
1953 (puthash
1954 data
1955 (cond
1956 ((not results) nil)
1957 ((memq type '(org-data plain-text nil)) results)
1958 ;; Append the same white space between elements or objects as in
1959 ;; the original buffer, and call appropriate filters.
1961 (let ((results
1962 (org-export-filter-apply-functions
1963 (plist-get info (intern (format ":filter-%s" type)))
1964 (let ((post-blank (or (org-element-property :post-blank data)
1965 0)))
1966 (if (memq type org-element-all-elements)
1967 (concat (org-element-normalize-string results)
1968 (make-string post-blank ?\n))
1969 (concat results (make-string post-blank ? ))))
1970 info)))
1971 results)))
1972 (plist-get info :exported-data))))))
1974 (defun org-export--interpret-p (blob info)
1975 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1976 Check is done according to export options INFO, stored as
1977 a plist."
1978 (case (org-element-type blob)
1979 ;; ... entities...
1980 (entity (plist-get info :with-entities))
1981 ;; ... emphasis...
1982 (emphasis (plist-get info :with-emphasize))
1983 ;; ... fixed-width areas.
1984 (fixed-width (plist-get info :with-fixed-width))
1985 ;; ... footnotes...
1986 ((footnote-definition footnote-reference)
1987 (plist-get info :with-footnotes))
1988 ;; ... sub/superscripts...
1989 ((subscript superscript)
1990 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1991 (if (eq sub/super-p '{})
1992 (org-element-property :use-brackets-p blob)
1993 sub/super-p)))
1994 ;; ... tables...
1995 (table (plist-get info :with-tables))
1996 (otherwise t)))
1998 (defun org-export-expand (blob contents)
1999 "Expand a parsed element or object to its original state.
2000 BLOB is either an element or an object. CONTENTS is its
2001 contents, as a string or nil."
2002 (funcall
2003 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2004 blob contents))
2006 (defun org-export-ignore-element (element info)
2007 "Add ELEMENT to `:ignore-list' in INFO.
2009 Any element in `:ignore-list' will be skipped when using
2010 `org-element-map'. INFO is modified by side effects."
2011 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2015 ;;; The Filter System
2017 ;; Filters allow end-users to tweak easily the transcoded output.
2018 ;; They are the functional counterpart of hooks, as every filter in
2019 ;; a set is applied to the return value of the previous one.
2021 ;; Every set is back-end agnostic. Although, a filter is always
2022 ;; called, in addition to the string it applies to, with the back-end
2023 ;; used as argument, so it's easy for the end-user to add back-end
2024 ;; specific filters in the set. The communication channel, as
2025 ;; a plist, is required as the third argument.
2027 ;; From the developer side, filters sets can be installed in the
2028 ;; process with the help of `org-export-define-backend', which
2029 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2030 ;; association has a key among the following symbols and a function or
2031 ;; a list of functions as value.
2033 ;; - `:filter-parse-tree' applies directly on the complete parsed
2034 ;; tree. It's the only filters set that doesn't apply to a string.
2035 ;; Users can set it through `org-export-filter-parse-tree-functions'
2036 ;; variable.
2038 ;; - `:filter-final-output' applies to the final transcoded string.
2039 ;; Users can set it with `org-export-filter-final-output-functions'
2040 ;; variable
2042 ;; - `:filter-plain-text' applies to any string not recognized as Org
2043 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2044 ;; configure it.
2046 ;; - `:filter-TYPE' applies on the string returned after an element or
2047 ;; object of type TYPE has been transcoded. An user can modify
2048 ;; `org-export-filter-TYPE-functions'
2050 ;; All filters sets are applied with
2051 ;; `org-export-filter-apply-functions' function. Filters in a set are
2052 ;; applied in a LIFO fashion. It allows developers to be sure that
2053 ;; their filters will be applied first.
2055 ;; Filters properties are installed in communication channel with
2056 ;; `org-export-install-filters' function.
2058 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2059 ;; before parsing to allow for heavy structure modifications.
2062 ;;;; Before Parsing Hook
2064 (defvar org-export-before-parsing-hook nil
2065 "Hook run before parsing an export buffer.
2067 This is run after include keywords have been expanded and Babel
2068 code executed, on a copy of original buffer's area being
2069 exported. Visibility is the same as in the original one. Point
2070 is left at the beginning of the new one.
2072 Every function in this hook will be called with one argument: the
2073 back-end currently used, as a symbol.")
2076 ;;;; Special Filters
2078 (defvar org-export-filter-parse-tree-functions nil
2079 "List of functions applied to the parsed tree.
2080 Each filter is called with three arguments: the parse tree, as
2081 returned by `org-element-parse-buffer', the back-end, as
2082 a symbol, and the communication channel, as a plist. It must
2083 return the modified parse tree to transcode.")
2085 (defvar org-export-filter-final-output-functions nil
2086 "List of functions applied to the transcoded string.
2087 Each filter is called with three arguments: the full transcoded
2088 string, the back-end, as a symbol, and the communication channel,
2089 as a plist. It must return a string that will be used as the
2090 final export output.")
2092 (defvar org-export-filter-plain-text-functions nil
2093 "List of functions applied to plain text.
2094 Each filter is called with three arguments: a string which
2095 contains no Org syntax, the back-end, as a symbol, and the
2096 communication channel, as a plist. It must return a string or
2097 nil.")
2100 ;;;; Elements Filters
2102 (defvar org-export-filter-center-block-functions nil
2103 "List of functions applied to a transcoded center block.
2104 Each filter is called with three arguments: the transcoded data,
2105 as a string, the back-end, as a symbol, and the communication
2106 channel, as a plist. It must return a string or nil.")
2108 (defvar org-export-filter-clock-functions nil
2109 "List of functions applied to a transcoded clock.
2110 Each filter is called with three arguments: the transcoded data,
2111 as a string, the back-end, as a symbol, and the communication
2112 channel, as a plist. It must return a string or nil.")
2114 (defvar org-export-filter-drawer-functions nil
2115 "List of functions applied to a transcoded drawer.
2116 Each filter is called with three arguments: the transcoded data,
2117 as a string, the back-end, as a symbol, and the communication
2118 channel, as a plist. It must return a string or nil.")
2120 (defvar org-export-filter-dynamic-block-functions nil
2121 "List of functions applied to a transcoded dynamic-block.
2122 Each filter is called with three arguments: the transcoded data,
2123 as a string, the back-end, as a symbol, and the communication
2124 channel, as a plist. It must return a string or nil.")
2126 (defvar org-export-filter-headline-functions nil
2127 "List of functions applied to a transcoded headline.
2128 Each filter is called with three arguments: the transcoded data,
2129 as a string, the back-end, as a symbol, and the communication
2130 channel, as a plist. It must return a string or nil.")
2132 (defvar org-export-filter-inlinetask-functions nil
2133 "List of functions applied to a transcoded inlinetask.
2134 Each filter is called with three arguments: the transcoded data,
2135 as a string, the back-end, as a symbol, and the communication
2136 channel, as a plist. It must return a string or nil.")
2138 (defvar org-export-filter-plain-list-functions nil
2139 "List of functions applied to a transcoded plain-list.
2140 Each filter is called with three arguments: the transcoded data,
2141 as a string, the back-end, as a symbol, and the communication
2142 channel, as a plist. It must return a string or nil.")
2144 (defvar org-export-filter-item-functions nil
2145 "List of functions applied to a transcoded item.
2146 Each filter is called with three arguments: the transcoded data,
2147 as a string, the back-end, as a symbol, and the communication
2148 channel, as a plist. It must return a string or nil.")
2150 (defvar org-export-filter-comment-functions nil
2151 "List of functions applied to a transcoded comment.
2152 Each filter is called with three arguments: the transcoded data,
2153 as a string, the back-end, as a symbol, and the communication
2154 channel, as a plist. It must return a string or nil.")
2156 (defvar org-export-filter-comment-block-functions nil
2157 "List of functions applied to a transcoded comment-comment.
2158 Each filter is called with three arguments: the transcoded data,
2159 as a string, the back-end, as a symbol, and the communication
2160 channel, as a plist. It must return a string or nil.")
2162 (defvar org-export-filter-example-block-functions nil
2163 "List of functions applied to a transcoded example-block.
2164 Each filter is called with three arguments: the transcoded data,
2165 as a string, the back-end, as a symbol, and the communication
2166 channel, as a plist. It must return a string or nil.")
2168 (defvar org-export-filter-export-block-functions nil
2169 "List of functions applied to a transcoded export-block.
2170 Each filter is called with three arguments: the transcoded data,
2171 as a string, the back-end, as a symbol, and the communication
2172 channel, as a plist. It must return a string or nil.")
2174 (defvar org-export-filter-fixed-width-functions nil
2175 "List of functions applied to a transcoded fixed-width.
2176 Each filter is called with three arguments: the transcoded data,
2177 as a string, the back-end, as a symbol, and the communication
2178 channel, as a plist. It must return a string or nil.")
2180 (defvar org-export-filter-footnote-definition-functions nil
2181 "List of functions applied to a transcoded footnote-definition.
2182 Each filter is called with three arguments: the transcoded data,
2183 as a string, the back-end, as a symbol, and the communication
2184 channel, as a plist. It must return a string or nil.")
2186 (defvar org-export-filter-horizontal-rule-functions nil
2187 "List of functions applied to a transcoded horizontal-rule.
2188 Each filter is called with three arguments: the transcoded data,
2189 as a string, the back-end, as a symbol, and the communication
2190 channel, as a plist. It must return a string or nil.")
2192 (defvar org-export-filter-keyword-functions nil
2193 "List of functions applied to a transcoded keyword.
2194 Each filter is called with three arguments: the transcoded data,
2195 as a string, the back-end, as a symbol, and the communication
2196 channel, as a plist. It must return a string or nil.")
2198 (defvar org-export-filter-latex-environment-functions nil
2199 "List of functions applied to a transcoded latex-environment.
2200 Each filter is called with three arguments: the transcoded data,
2201 as a string, the back-end, as a symbol, and the communication
2202 channel, as a plist. It must return a string or nil.")
2204 (defvar org-export-filter-babel-call-functions nil
2205 "List of functions applied to a transcoded babel-call.
2206 Each filter is called with three arguments: the transcoded data,
2207 as a string, the back-end, as a symbol, and the communication
2208 channel, as a plist. It must return a string or nil.")
2210 (defvar org-export-filter-paragraph-functions nil
2211 "List of functions applied to a transcoded paragraph.
2212 Each filter is called with three arguments: the transcoded data,
2213 as a string, the back-end, as a symbol, and the communication
2214 channel, as a plist. It must return a string or nil.")
2216 (defvar org-export-filter-planning-functions nil
2217 "List of functions applied to a transcoded planning.
2218 Each filter is called with three arguments: the transcoded data,
2219 as a string, the back-end, as a symbol, and the communication
2220 channel, as a plist. It must return a string or nil.")
2222 (defvar org-export-filter-property-drawer-functions nil
2223 "List of functions applied to a transcoded property-drawer.
2224 Each filter is called with three arguments: the transcoded data,
2225 as a string, the back-end, as a symbol, and the communication
2226 channel, as a plist. It must return a string or nil.")
2228 (defvar org-export-filter-quote-block-functions nil
2229 "List of functions applied to a transcoded quote block.
2230 Each filter is called with three arguments: the transcoded quote
2231 data, as a string, the back-end, as a symbol, and the
2232 communication channel, as a plist. It must return a string or
2233 nil.")
2235 (defvar org-export-filter-quote-section-functions nil
2236 "List of functions applied to a transcoded quote-section.
2237 Each filter is called with three arguments: the transcoded data,
2238 as a string, the back-end, as a symbol, and the communication
2239 channel, as a plist. It must return a string or nil.")
2241 (defvar org-export-filter-section-functions nil
2242 "List of functions applied to a transcoded section.
2243 Each filter is called with three arguments: the transcoded data,
2244 as a string, the back-end, as a symbol, and the communication
2245 channel, as a plist. It must return a string or nil.")
2247 (defvar org-export-filter-special-block-functions nil
2248 "List of functions applied to a transcoded special block.
2249 Each filter is called with three arguments: the transcoded data,
2250 as a string, the back-end, as a symbol, and the communication
2251 channel, as a plist. It must return a string or nil.")
2253 (defvar org-export-filter-src-block-functions nil
2254 "List of functions applied to a transcoded src-block.
2255 Each filter is called with three arguments: the transcoded data,
2256 as a string, the back-end, as a symbol, and the communication
2257 channel, as a plist. It must return a string or nil.")
2259 (defvar org-export-filter-table-functions nil
2260 "List of functions applied to a transcoded table.
2261 Each filter is called with three arguments: the transcoded data,
2262 as a string, the back-end, as a symbol, and the communication
2263 channel, as a plist. It must return a string or nil.")
2265 (defvar org-export-filter-table-cell-functions nil
2266 "List of functions applied to a transcoded table-cell.
2267 Each filter is called with three arguments: the transcoded data,
2268 as a string, the back-end, as a symbol, and the communication
2269 channel, as a plist. It must return a string or nil.")
2271 (defvar org-export-filter-table-row-functions nil
2272 "List of functions applied to a transcoded table-row.
2273 Each filter is called with three arguments: the transcoded data,
2274 as a string, the back-end, as a symbol, and the communication
2275 channel, as a plist. It must return a string or nil.")
2277 (defvar org-export-filter-verse-block-functions nil
2278 "List of functions applied to a transcoded verse block.
2279 Each filter is called with three arguments: the transcoded data,
2280 as a string, the back-end, as a symbol, and the communication
2281 channel, as a plist. It must return a string or nil.")
2284 ;;;; Objects Filters
2286 (defvar org-export-filter-bold-functions nil
2287 "List of functions applied to transcoded bold text.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2292 (defvar org-export-filter-code-functions nil
2293 "List of functions applied to transcoded code text.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2298 (defvar org-export-filter-entity-functions nil
2299 "List of functions applied to a transcoded entity.
2300 Each filter is called with three arguments: the transcoded data,
2301 as a string, the back-end, as a symbol, and the communication
2302 channel, as a plist. It must return a string or nil.")
2304 (defvar org-export-filter-export-snippet-functions nil
2305 "List of functions applied to a transcoded export-snippet.
2306 Each filter is called with three arguments: the transcoded data,
2307 as a string, the back-end, as a symbol, and the communication
2308 channel, as a plist. It must return a string or nil.")
2310 (defvar org-export-filter-footnote-reference-functions nil
2311 "List of functions applied to a transcoded footnote-reference.
2312 Each filter is called with three arguments: the transcoded data,
2313 as a string, the back-end, as a symbol, and the communication
2314 channel, as a plist. It must return a string or nil.")
2316 (defvar org-export-filter-inline-babel-call-functions nil
2317 "List of functions applied to a transcoded inline-babel-call.
2318 Each filter is called with three arguments: the transcoded data,
2319 as a string, the back-end, as a symbol, and the communication
2320 channel, as a plist. It must return a string or nil.")
2322 (defvar org-export-filter-inline-src-block-functions nil
2323 "List of functions applied to a transcoded inline-src-block.
2324 Each filter is called with three arguments: the transcoded data,
2325 as a string, the back-end, as a symbol, and the communication
2326 channel, as a plist. It must return a string or nil.")
2328 (defvar org-export-filter-italic-functions nil
2329 "List of functions applied to transcoded italic text.
2330 Each filter is called with three arguments: the transcoded data,
2331 as a string, the back-end, as a symbol, and the communication
2332 channel, as a plist. It must return a string or nil.")
2334 (defvar org-export-filter-latex-fragment-functions nil
2335 "List of functions applied to a transcoded latex-fragment.
2336 Each filter is called with three arguments: the transcoded data,
2337 as a string, the back-end, as a symbol, and the communication
2338 channel, as a plist. It must return a string or nil.")
2340 (defvar org-export-filter-line-break-functions nil
2341 "List of functions applied to a transcoded line-break.
2342 Each filter is called with three arguments: the transcoded data,
2343 as a string, the back-end, as a symbol, and the communication
2344 channel, as a plist. It must return a string or nil.")
2346 (defvar org-export-filter-link-functions nil
2347 "List of functions applied to a transcoded link.
2348 Each filter is called with three arguments: the transcoded data,
2349 as a string, the back-end, as a symbol, and the communication
2350 channel, as a plist. It must return a string or nil.")
2352 (defvar org-export-filter-macro-functions nil
2353 "List of functions applied to a transcoded macro.
2354 Each filter is called with three arguments: the transcoded data,
2355 as a string, the back-end, as a symbol, and the communication
2356 channel, as a plist. It must return a string or nil.")
2358 (defvar org-export-filter-radio-target-functions nil
2359 "List of functions applied to a transcoded radio-target.
2360 Each filter is called with three arguments: the transcoded data,
2361 as a string, the back-end, as a symbol, and the communication
2362 channel, as a plist. It must return a string or nil.")
2364 (defvar org-export-filter-statistics-cookie-functions nil
2365 "List of functions applied to a transcoded statistics-cookie.
2366 Each filter is called with three arguments: the transcoded data,
2367 as a string, the back-end, as a symbol, and the communication
2368 channel, as a plist. It must return a string or nil.")
2370 (defvar org-export-filter-strike-through-functions nil
2371 "List of functions applied to transcoded strike-through text.
2372 Each filter is called with three arguments: the transcoded data,
2373 as a string, the back-end, as a symbol, and the communication
2374 channel, as a plist. It must return a string or nil.")
2376 (defvar org-export-filter-subscript-functions nil
2377 "List of functions applied to a transcoded subscript.
2378 Each filter is called with three arguments: the transcoded data,
2379 as a string, the back-end, as a symbol, and the communication
2380 channel, as a plist. It must return a string or nil.")
2382 (defvar org-export-filter-superscript-functions nil
2383 "List of functions applied to a transcoded superscript.
2384 Each filter is called with three arguments: the transcoded data,
2385 as a string, the back-end, as a symbol, and the communication
2386 channel, as a plist. It must return a string or nil.")
2388 (defvar org-export-filter-target-functions nil
2389 "List of functions applied to a transcoded target.
2390 Each filter is called with three arguments: the transcoded data,
2391 as a string, the back-end, as a symbol, and the communication
2392 channel, as a plist. It must return a string or nil.")
2394 (defvar org-export-filter-timestamp-functions nil
2395 "List of functions applied to a transcoded timestamp.
2396 Each filter is called with three arguments: the transcoded data,
2397 as a string, the back-end, as a symbol, and the communication
2398 channel, as a plist. It must return a string or nil.")
2400 (defvar org-export-filter-underline-functions nil
2401 "List of functions applied to transcoded underline text.
2402 Each filter is called with three arguments: the transcoded data,
2403 as a string, the back-end, as a symbol, and the communication
2404 channel, as a plist. It must return a string or nil.")
2406 (defvar org-export-filter-verbatim-functions nil
2407 "List of functions applied to transcoded verbatim text.
2408 Each filter is called with three arguments: the transcoded data,
2409 as a string, the back-end, as a symbol, and the communication
2410 channel, as a plist. It must return a string or nil.")
2413 ;;;; Filters Tools
2415 ;; Internal function `org-export-install-filters' installs filters
2416 ;; hard-coded in back-ends (developer filters) and filters from global
2417 ;; variables (user filters) in the communication channel.
2419 ;; Internal function `org-export-filter-apply-functions' takes care
2420 ;; about applying each filter in order to a given data. It ignores
2421 ;; filters returning a nil value but stops whenever a filter returns
2422 ;; an empty string.
2424 (defun org-export-filter-apply-functions (filters value info)
2425 "Call every function in FILTERS.
2427 Functions are called with arguments VALUE, current export
2428 back-end and INFO. A function returning a nil value will be
2429 skipped. If it returns the empty string, the process ends and
2430 VALUE is ignored.
2432 Call is done in a LIFO fashion, to be sure that developer
2433 specified filters, if any, are called first."
2434 (catch 'exit
2435 (dolist (filter filters value)
2436 (let ((result (funcall filter value (plist-get info :back-end) info)))
2437 (cond ((not result) value)
2438 ((equal value "") (throw 'exit nil))
2439 (t (setq value result)))))))
2441 (defun org-export-install-filters (info)
2442 "Install filters properties in communication channel.
2444 INFO is a plist containing the current communication channel.
2446 Return the updated communication channel."
2447 (let (plist)
2448 ;; Install user defined filters with `org-export-filters-alist'.
2449 (mapc (lambda (p)
2450 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2451 org-export-filters-alist)
2452 ;; Prepend back-end specific filters to that list.
2453 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2454 (plist-get info :back-end)))))
2455 (when (boundp back-end-filters)
2456 (mapc (lambda (p)
2457 ;; Single values get consed, lists are prepended.
2458 (let ((key (car p)) (value (cdr p)))
2459 (when value
2460 (setq plist
2461 (plist-put
2462 plist key
2463 (if (atom value) (cons value (plist-get plist key))
2464 (append value (plist-get plist key))))))))
2465 (eval back-end-filters))))
2466 ;; Return new communication channel.
2467 (org-combine-plists info plist)))
2471 ;;; Core functions
2473 ;; This is the room for the main function, `org-export-as', along with
2474 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2475 ;; They differ only by the way they output the resulting code.
2477 ;; `org-export-output-file-name' is an auxiliary function meant to be
2478 ;; used with `org-export-to-file'. With a given extension, it tries
2479 ;; to provide a canonical file name to write export output to.
2481 ;; Note that `org-export-as' doesn't really parse the current buffer,
2482 ;; but a copy of it (with the same buffer-local variables and
2483 ;; visibility), where macros and include keywords are expanded and
2484 ;; Babel blocks are executed, if appropriate.
2485 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2487 ;; File inclusion is taken care of by
2488 ;; `org-export-expand-include-keyword' and
2489 ;; `org-export--prepare-file-contents'. Structure wise, including
2490 ;; a whole Org file in a buffer often makes little sense. For
2491 ;; example, if the file contains an headline and the include keyword
2492 ;; was within an item, the item should contain the headline. That's
2493 ;; why file inclusion should be done before any structure can be
2494 ;; associated to the file, that is before parsing.
2496 (defun org-export-as
2497 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2498 "Transcode current Org buffer into BACKEND code.
2500 If narrowing is active in the current buffer, only transcode its
2501 narrowed part.
2503 If a region is active, transcode that region.
2505 When optional argument SUBTREEP is non-nil, transcode the
2506 sub-tree at point, extracting information from the headline
2507 properties first.
2509 When optional argument VISIBLE-ONLY is non-nil, don't export
2510 contents of hidden elements.
2512 When optional argument BODY-ONLY is non-nil, only return body
2513 code, without preamble nor postamble.
2515 Optional argument EXT-PLIST, when provided, is a property list
2516 with external parameters overriding Org default settings, but
2517 still inferior to file-local settings.
2519 Optional argument NOEXPAND, when non-nil, prevents included files
2520 to be expanded and Babel code to be executed.
2522 Return code as a string."
2523 (save-excursion
2524 (save-restriction
2525 ;; Narrow buffer to an appropriate region or subtree for
2526 ;; parsing. If parsing subtree, be sure to remove main headline
2527 ;; too.
2528 (cond ((org-region-active-p)
2529 (narrow-to-region (region-beginning) (region-end)))
2530 (subtreep
2531 (org-narrow-to-subtree)
2532 (goto-char (point-min))
2533 (forward-line)
2534 (narrow-to-region (point) (point-max))))
2535 ;; 1. Get export environment from original buffer. Also install
2536 ;; user's and developer's filters.
2537 (let ((info (org-export-install-filters
2538 (org-export-get-environment backend subtreep ext-plist)))
2539 ;; 2. Get parse tree. Buffer isn't parsed directly.
2540 ;; Instead, a temporary copy is created, where macros
2541 ;; and include keywords are expanded and code blocks
2542 ;; are evaluated.
2543 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2544 (current-buffer))))
2545 (org-export-with-current-buffer-copy
2546 (unless noexpand
2547 (org-macro-replace-all)
2548 (org-export-expand-include-keyword)
2549 ;; TODO: Setting `org-current-export-file' is
2550 ;; required by Org Babel to properly resolve
2551 ;; noweb references. Once "org-exp.el" is
2552 ;; removed, modify
2553 ;; `org-export-blocks-preprocess' so it accepts
2554 ;; the value as an argument instead.
2555 (let ((org-current-export-file buf))
2556 (org-export-blocks-preprocess)))
2557 (goto-char (point-min))
2558 ;; Run hook
2559 ;; `org-export-before-parsing-hook'. with current
2560 ;; back-end as argument.
2561 (run-hook-with-args
2562 'org-export-before-parsing-hook backend)
2563 ;; Eventually parse buffer.
2564 (org-element-parse-buffer nil visible-only)))))
2565 ;; 3. Call parse-tree filters to get the final tree.
2566 (setq tree
2567 (org-export-filter-apply-functions
2568 (plist-get info :filter-parse-tree) tree info))
2569 ;; 4. Now tree is complete, compute its properties and add
2570 ;; them to communication channel.
2571 (setq info
2572 (org-combine-plists
2573 info (org-export-collect-tree-properties tree info)))
2574 ;; 5. Eventually transcode TREE. Wrap the resulting string
2575 ;; into a template, if required. Eventually call
2576 ;; final-output filter.
2577 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2578 (template (cdr (assq 'template
2579 (plist-get info :translate-alist))))
2580 (output (org-export-filter-apply-functions
2581 (plist-get info :filter-final-output)
2582 (if (or (not (functionp template)) body-only) body
2583 (funcall template body info))
2584 info)))
2585 ;; Maybe add final OUTPUT to kill ring, then return it.
2586 (when org-export-copy-to-kill-ring (org-kill-new output))
2587 output)))))
2589 (defun org-export-to-buffer
2590 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2591 "Call `org-export-as' with output to a specified buffer.
2593 BACKEND is the back-end used for transcoding, as a symbol.
2595 BUFFER is the output buffer. If it already exists, it will be
2596 erased first, otherwise, it will be created.
2598 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2599 and NOEXPAND are similar to those used in `org-export-as', which
2600 see.
2602 Return buffer."
2603 (let ((out (org-export-as
2604 backend subtreep visible-only body-only ext-plist noexpand))
2605 (buffer (get-buffer-create buffer)))
2606 (with-current-buffer buffer
2607 (erase-buffer)
2608 (insert out)
2609 (goto-char (point-min)))
2610 buffer))
2612 (defun org-export-to-file
2613 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2614 "Call `org-export-as' with output to a specified file.
2616 BACKEND is the back-end used for transcoding, as a symbol. FILE
2617 is the name of the output file, as a string.
2619 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2620 and NOEXPAND are similar to those used in `org-export-as', which
2621 see.
2623 Return output file's name."
2624 ;; Checks for FILE permissions. `write-file' would do the same, but
2625 ;; we'd rather avoid needless transcoding of parse tree.
2626 (unless (file-writable-p file) (error "Output file not writable"))
2627 ;; Insert contents to a temporary buffer and write it to FILE.
2628 (let ((out (org-export-as
2629 backend subtreep visible-only body-only ext-plist noexpand)))
2630 (with-temp-buffer
2631 (insert out)
2632 (let ((coding-system-for-write org-export-coding-system))
2633 (write-file file))))
2634 ;; Return full path.
2635 file)
2637 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2638 "Return output file's name according to buffer specifications.
2640 EXTENSION is a string representing the output file extension,
2641 with the leading dot.
2643 With a non-nil optional argument SUBTREEP, try to determine
2644 output file's name by looking for \"EXPORT_FILE_NAME\" property
2645 of subtree at point.
2647 When optional argument PUB-DIR is set, use it as the publishing
2648 directory.
2650 When optional argument VISIBLE-ONLY is non-nil, don't export
2651 contents of hidden elements.
2653 Return file name as a string, or nil if it couldn't be
2654 determined."
2655 (let ((base-name
2656 ;; File name may come from EXPORT_FILE_NAME subtree property,
2657 ;; assuming point is at beginning of said sub-tree.
2658 (file-name-sans-extension
2659 (or (and subtreep
2660 (org-entry-get
2661 (save-excursion
2662 (ignore-errors (org-back-to-heading) (point)))
2663 "EXPORT_FILE_NAME" t))
2664 ;; File name may be extracted from buffer's associated
2665 ;; file, if any.
2666 (buffer-file-name (buffer-base-buffer))
2667 ;; Can't determine file name on our own: Ask user.
2668 (let ((read-file-name-function
2669 (and org-completion-use-ido 'ido-read-file-name)))
2670 (read-file-name
2671 "Output file: " pub-dir nil nil nil
2672 (lambda (name)
2673 (string= (file-name-extension name t) extension))))))))
2674 ;; Build file name. Enforce EXTENSION over whatever user may have
2675 ;; come up with. PUB-DIR, if defined, always has precedence over
2676 ;; any provided path.
2677 (cond
2678 (pub-dir
2679 (concat (file-name-as-directory pub-dir)
2680 (file-name-nondirectory base-name)
2681 extension))
2682 ((string= (file-name-nondirectory base-name) base-name)
2683 (concat (file-name-as-directory ".") base-name extension))
2684 (t (concat base-name extension)))))
2686 (defmacro org-export-with-current-buffer-copy (&rest body)
2687 "Apply BODY in a copy of the current buffer.
2689 The copy preserves local variables and visibility of the original
2690 buffer.
2692 Point is at buffer's beginning when BODY is applied."
2693 (org-with-gensyms (original-buffer offset buffer-string overlays)
2694 `(let ((,original-buffer (current-buffer))
2695 (,offset (1- (point-min)))
2696 (,buffer-string (buffer-string))
2697 (,overlays (mapcar
2698 'copy-overlay (overlays-in (point-min) (point-max)))))
2699 (with-temp-buffer
2700 (let ((buffer-invisibility-spec nil))
2701 (org-clone-local-variables
2702 ,original-buffer
2703 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2704 (insert ,buffer-string)
2705 (mapc (lambda (ov)
2706 (move-overlay
2708 (- (overlay-start ov) ,offset)
2709 (- (overlay-end ov) ,offset)
2710 (current-buffer)))
2711 ,overlays)
2712 (goto-char (point-min))
2713 (progn ,@body))))))
2714 (def-edebug-spec org-export-with-current-buffer-copy (body))
2716 (defun org-export-expand-include-keyword (&optional included dir)
2717 "Expand every include keyword in buffer.
2718 Optional argument INCLUDED is a list of included file names along
2719 with their line restriction, when appropriate. It is used to
2720 avoid infinite recursion. Optional argument DIR is the current
2721 working directory. It is used to properly resolve relative
2722 paths."
2723 (let ((case-fold-search t))
2724 (goto-char (point-min))
2725 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2726 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2727 'keyword)
2728 (beginning-of-line)
2729 ;; Extract arguments from keyword's value.
2730 (let* ((value (match-string 1))
2731 (ind (org-get-indentation))
2732 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2733 (prog1 (expand-file-name (match-string 1 value) dir)
2734 (setq value (replace-match "" nil nil value)))))
2735 (lines
2736 (and (string-match
2737 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2738 (prog1 (match-string 1 value)
2739 (setq value (replace-match "" nil nil value)))))
2740 (env (cond ((string-match "\\<example\\>" value) 'example)
2741 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2742 (match-string 1 value))))
2743 ;; Minimal level of included file defaults to the child
2744 ;; level of the current headline, if any, or one. It
2745 ;; only applies is the file is meant to be included as
2746 ;; an Org one.
2747 (minlevel
2748 (and (not env)
2749 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2750 (prog1 (string-to-number (match-string 1 value))
2751 (setq value (replace-match "" nil nil value)))
2752 (let ((cur (org-current-level)))
2753 (if cur (1+ (org-reduced-level cur)) 1))))))
2754 ;; Remove keyword.
2755 (delete-region (point) (progn (forward-line) (point)))
2756 (cond
2757 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2758 ;; Check if files has already been parsed. Look after
2759 ;; inclusion lines too, as different parts of the same file
2760 ;; can be included too.
2761 ((member (list file lines) included)
2762 (error "Recursive file inclusion: %s" file))
2764 (cond
2765 ((eq env 'example)
2766 (insert
2767 (let ((ind-str (make-string ind ? ))
2768 (contents
2769 ;; Protect sensitive contents with commas.
2770 (replace-regexp-in-string
2771 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2772 (org-export--prepare-file-contents file lines)
2773 nil nil 1)))
2774 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2775 ind-str contents ind-str))))
2776 ((stringp env)
2777 (insert
2778 (let ((ind-str (make-string ind ? ))
2779 (contents
2780 ;; Protect sensitive contents with commas.
2781 (replace-regexp-in-string
2782 (if (string= env "org") "\\(^\\)\\(.\\)"
2783 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2784 (org-export--prepare-file-contents file lines)
2785 nil nil 1)))
2786 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2787 ind-str env contents ind-str))))
2789 (insert
2790 (with-temp-buffer
2791 (org-mode)
2792 (insert
2793 (org-export--prepare-file-contents file lines ind minlevel))
2794 (org-export-expand-include-keyword
2795 (cons (list file lines) included)
2796 (file-name-directory file))
2797 (buffer-string))))))))))))
2799 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2800 "Prepare the contents of FILE for inclusion and return them as a string.
2802 When optional argument LINES is a string specifying a range of
2803 lines, include only those lines.
2805 Optional argument IND, when non-nil, is an integer specifying the
2806 global indentation of returned contents. Since its purpose is to
2807 allow an included file to stay in the same environment it was
2808 created \(i.e. a list item), it doesn't apply past the first
2809 headline encountered.
2811 Optional argument MINLEVEL, when non-nil, is an integer
2812 specifying the level that any top-level headline in the included
2813 file should have."
2814 (with-temp-buffer
2815 (insert-file-contents file)
2816 (when lines
2817 (let* ((lines (split-string lines "-"))
2818 (lbeg (string-to-number (car lines)))
2819 (lend (string-to-number (cadr lines)))
2820 (beg (if (zerop lbeg) (point-min)
2821 (goto-char (point-min))
2822 (forward-line (1- lbeg))
2823 (point)))
2824 (end (if (zerop lend) (point-max)
2825 (goto-char (point-min))
2826 (forward-line (1- lend))
2827 (point))))
2828 (narrow-to-region beg end)))
2829 ;; Remove blank lines at beginning and end of contents. The logic
2830 ;; behind that removal is that blank lines around include keyword
2831 ;; override blank lines in included file.
2832 (goto-char (point-min))
2833 (org-skip-whitespace)
2834 (beginning-of-line)
2835 (delete-region (point-min) (point))
2836 (goto-char (point-max))
2837 (skip-chars-backward " \r\t\n")
2838 (forward-line)
2839 (delete-region (point) (point-max))
2840 ;; If IND is set, preserve indentation of include keyword until
2841 ;; the first headline encountered.
2842 (when ind
2843 (unless (eq major-mode 'org-mode) (org-mode))
2844 (goto-char (point-min))
2845 (let ((ind-str (make-string ind ? )))
2846 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2847 ;; Do not move footnote definitions out of column 0.
2848 (unless (and (looking-at org-footnote-definition-re)
2849 (eq (org-element-type (org-element-at-point))
2850 'footnote-definition))
2851 (insert ind-str))
2852 (forward-line))))
2853 ;; When MINLEVEL is specified, compute minimal level for headlines
2854 ;; in the file (CUR-MIN), and remove stars to each headline so
2855 ;; that headlines with minimal level have a level of MINLEVEL.
2856 (when minlevel
2857 (unless (eq major-mode 'org-mode) (org-mode))
2858 (let ((levels (org-map-entries
2859 (lambda () (org-reduced-level (org-current-level))))))
2860 (when levels
2861 (let ((offset (- minlevel (apply 'min levels))))
2862 (unless (zerop offset)
2863 (when org-odd-levels-only (setq offset (* offset 2)))
2864 ;; Only change stars, don't bother moving whole
2865 ;; sections.
2866 (org-map-entries
2867 (lambda () (if (< offset 0) (delete-char (abs offset))
2868 (insert (make-string offset ?*))))))))))
2869 (buffer-string)))
2872 ;;; Tools For Back-Ends
2874 ;; A whole set of tools is available to help build new exporters. Any
2875 ;; function general enough to have its use across many back-ends
2876 ;; should be added here.
2878 ;;;; For Affiliated Keywords
2880 ;; `org-export-read-attribute' reads a property from a given element
2881 ;; as a plist. It can be used to normalize affiliated keywords'
2882 ;; syntax.
2884 ;; Since captions can span over multiple lines and accept dual values,
2885 ;; their internal representation is a bit tricky. Therefore,
2886 ;; `org-export-get-caption' transparently returns a given element's
2887 ;; caption as a secondary string.
2889 (defun org-export-read-attribute (attribute element &optional property)
2890 "Turn ATTRIBUTE property from ELEMENT into a plist.
2892 When optional argument PROPERTY is non-nil, return the value of
2893 that property within attributes.
2895 This function assumes attributes are defined as \":keyword
2896 value\" pairs. It is appropriate for `:attr_html' like
2897 properties."
2898 (let ((attributes
2899 (let ((value (org-element-property attribute element)))
2900 (and value
2901 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2902 (if property (plist-get attributes property) attributes)))
2904 (defun org-export-get-caption (element &optional shortp)
2905 "Return caption from ELEMENT as a secondary string.
2907 When optional argument SHORTP is non-nil, return short caption,
2908 as a secondary string, instead.
2910 Caption lines are separated by a white space."
2911 (let ((full-caption (org-element-property :caption element)) caption)
2912 (dolist (line full-caption (cdr caption))
2913 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2914 (when cap
2915 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2918 ;;;; For Export Snippets
2920 ;; Every export snippet is transmitted to the back-end. Though, the
2921 ;; latter will only retain one type of export-snippet, ignoring
2922 ;; others, based on the former's target back-end. The function
2923 ;; `org-export-snippet-backend' returns that back-end for a given
2924 ;; export-snippet.
2926 (defun org-export-snippet-backend (export-snippet)
2927 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2928 Translation, with `org-export-snippet-translation-alist', is
2929 applied."
2930 (let ((back-end (org-element-property :back-end export-snippet)))
2931 (intern
2932 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2933 back-end))))
2936 ;;;; For Footnotes
2938 ;; `org-export-collect-footnote-definitions' is a tool to list
2939 ;; actually used footnotes definitions in the whole parse tree, or in
2940 ;; an headline, in order to add footnote listings throughout the
2941 ;; transcoded data.
2943 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2944 ;; back-ends, when they need to attach the footnote definition only to
2945 ;; the first occurrence of the corresponding label.
2947 ;; `org-export-get-footnote-definition' and
2948 ;; `org-export-get-footnote-number' provide easier access to
2949 ;; additional information relative to a footnote reference.
2951 (defun org-export-collect-footnote-definitions (data info)
2952 "Return an alist between footnote numbers, labels and definitions.
2954 DATA is the parse tree from which definitions are collected.
2955 INFO is the plist used as a communication channel.
2957 Definitions are sorted by order of references. They either
2958 appear as Org data or as a secondary string for inlined
2959 footnotes. Unreferenced definitions are ignored."
2960 (let* (num-alist
2961 collect-fn ; for byte-compiler.
2962 (collect-fn
2963 (function
2964 (lambda (data)
2965 ;; Collect footnote number, label and definition in DATA.
2966 (org-element-map
2967 data 'footnote-reference
2968 (lambda (fn)
2969 (when (org-export-footnote-first-reference-p fn info)
2970 (let ((def (org-export-get-footnote-definition fn info)))
2971 (push
2972 (list (org-export-get-footnote-number fn info)
2973 (org-element-property :label fn)
2974 def)
2975 num-alist)
2976 ;; Also search in definition for nested footnotes.
2977 (when (eq (org-element-property :type fn) 'standard)
2978 (funcall collect-fn def)))))
2979 ;; Don't enter footnote definitions since it will happen
2980 ;; when their first reference is found.
2981 info nil 'footnote-definition)))))
2982 (funcall collect-fn (plist-get info :parse-tree))
2983 (reverse num-alist)))
2985 (defun org-export-footnote-first-reference-p (footnote-reference info)
2986 "Non-nil when a footnote reference is the first one for its label.
2988 FOOTNOTE-REFERENCE is the footnote reference being considered.
2989 INFO is the plist used as a communication channel."
2990 (let ((label (org-element-property :label footnote-reference)))
2991 ;; Anonymous footnotes are always a first reference.
2992 (if (not label) t
2993 ;; Otherwise, return the first footnote with the same LABEL and
2994 ;; test if it is equal to FOOTNOTE-REFERENCE.
2995 (let* (search-refs ; for byte-compiler.
2996 (search-refs
2997 (function
2998 (lambda (data)
2999 (org-element-map
3000 data 'footnote-reference
3001 (lambda (fn)
3002 (cond
3003 ((string= (org-element-property :label fn) label)
3004 (throw 'exit fn))
3005 ;; If FN isn't inlined, be sure to traverse its
3006 ;; definition before resuming search. See
3007 ;; comments in `org-export-get-footnote-number'
3008 ;; for more information.
3009 ((eq (org-element-property :type fn) 'standard)
3010 (funcall search-refs
3011 (org-export-get-footnote-definition fn info)))))
3012 ;; Don't enter footnote definitions since it will
3013 ;; happen when their first reference is found.
3014 info 'first-match 'footnote-definition)))))
3015 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3016 footnote-reference)))))
3018 (defun org-export-get-footnote-definition (footnote-reference info)
3019 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3020 INFO is the plist used as a communication channel."
3021 (let ((label (org-element-property :label footnote-reference)))
3022 (or (org-element-property :inline-definition footnote-reference)
3023 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3025 (defun org-export-get-footnote-number (footnote info)
3026 "Return number associated to a footnote.
3028 FOOTNOTE is either a footnote reference or a footnote definition.
3029 INFO is the plist used as a communication channel."
3030 (let* ((label (org-element-property :label footnote))
3031 seen-refs
3032 search-ref ; For byte-compiler.
3033 (search-ref
3034 (function
3035 (lambda (data)
3036 ;; Search footnote references through DATA, filling
3037 ;; SEEN-REFS along the way.
3038 (org-element-map
3039 data 'footnote-reference
3040 (lambda (fn)
3041 (let ((fn-lbl (org-element-property :label fn)))
3042 (cond
3043 ;; Anonymous footnote match: return number.
3044 ((and (not fn-lbl) (eq fn footnote))
3045 (throw 'exit (1+ (length seen-refs))))
3046 ;; Labels match: return number.
3047 ((and label (string= label fn-lbl))
3048 (throw 'exit (1+ (length seen-refs))))
3049 ;; Anonymous footnote: it's always a new one. Also,
3050 ;; be sure to return nil from the `cond' so
3051 ;; `first-match' doesn't get us out of the loop.
3052 ((not fn-lbl) (push 'inline seen-refs) nil)
3053 ;; Label not seen so far: add it so SEEN-REFS.
3055 ;; Also search for subsequent references in
3056 ;; footnote definition so numbering follows reading
3057 ;; logic. Note that we don't have to care about
3058 ;; inline definitions, since `org-element-map'
3059 ;; already traverses them at the right time.
3061 ;; Once again, return nil to stay in the loop.
3062 ((not (member fn-lbl seen-refs))
3063 (push fn-lbl seen-refs)
3064 (funcall search-ref
3065 (org-export-get-footnote-definition fn info))
3066 nil))))
3067 ;; Don't enter footnote definitions since it will happen
3068 ;; when their first reference is found.
3069 info 'first-match 'footnote-definition)))))
3070 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3073 ;;;; For Headlines
3075 ;; `org-export-get-relative-level' is a shortcut to get headline
3076 ;; level, relatively to the lower headline level in the parsed tree.
3078 ;; `org-export-get-headline-number' returns the section number of an
3079 ;; headline, while `org-export-number-to-roman' allows to convert it
3080 ;; to roman numbers.
3082 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3083 ;; `org-export-last-sibling-p' are three useful predicates when it
3084 ;; comes to fulfill the `:headline-levels' property.
3086 (defun org-export-get-relative-level (headline info)
3087 "Return HEADLINE relative level within current parsed tree.
3088 INFO is a plist holding contextual information."
3089 (+ (org-element-property :level headline)
3090 (or (plist-get info :headline-offset) 0)))
3092 (defun org-export-low-level-p (headline info)
3093 "Non-nil when HEADLINE is considered as low level.
3095 INFO is a plist used as a communication channel.
3097 A low level headlines has a relative level greater than
3098 `:headline-levels' property value.
3100 Return value is the difference between HEADLINE relative level
3101 and the last level being considered as high enough, or nil."
3102 (let ((limit (plist-get info :headline-levels)))
3103 (when (wholenump limit)
3104 (let ((level (org-export-get-relative-level headline info)))
3105 (and (> level limit) (- level limit))))))
3107 (defun org-export-get-headline-number (headline info)
3108 "Return HEADLINE numbering as a list of numbers.
3109 INFO is a plist holding contextual information."
3110 (cdr (assoc headline (plist-get info :headline-numbering))))
3112 (defun org-export-numbered-headline-p (headline info)
3113 "Return a non-nil value if HEADLINE element should be numbered.
3114 INFO is a plist used as a communication channel."
3115 (let ((sec-num (plist-get info :section-numbers))
3116 (level (org-export-get-relative-level headline info)))
3117 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3119 (defun org-export-number-to-roman (n)
3120 "Convert integer N into a roman numeral."
3121 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3122 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3123 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3124 ( 1 . "I")))
3125 (res ""))
3126 (if (<= n 0)
3127 (number-to-string n)
3128 (while roman
3129 (if (>= n (caar roman))
3130 (setq n (- n (caar roman))
3131 res (concat res (cdar roman)))
3132 (pop roman)))
3133 res)))
3135 (defun org-export-get-tags (element info &optional tags)
3136 "Return list of tags associated to ELEMENT.
3138 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3139 is a plist used as a communication channel.
3141 Select tags (see `org-export-select-tags') and exclude tags (see
3142 `org-export-exclude-tags') are removed from the list.
3144 When non-nil, optional argument TAGS should be a list of strings.
3145 Any tag belonging to this list will also be removed."
3146 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3147 (member tag (plist-get info :exclude-tags))
3148 (member tag tags)))
3149 (org-element-property :tags element)))
3151 (defun org-export-get-node-property (property blob &optional inherited)
3152 "Return node PROPERTY value for BLOB.
3154 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3155 element or object.
3157 If optional argument INHERITED is non-nil, the value can be
3158 inherited from a parent headline.
3160 Return value is a string or nil."
3161 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3162 (org-export-get-parent-headline blob))))
3163 (if (not inherited) (org-element-property property blob)
3164 (let ((parent headline) value)
3165 (catch 'found
3166 (while parent
3167 (when (plist-member (nth 1 parent) property)
3168 (throw 'found (org-element-property property parent)))
3169 (setq parent (org-element-property :parent parent))))))))
3171 (defun org-export-first-sibling-p (headline info)
3172 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3173 INFO is a plist used as a communication channel."
3174 (not (eq (org-element-type (org-export-get-previous-element headline info))
3175 'headline)))
3177 (defun org-export-last-sibling-p (headline info)
3178 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3179 INFO is a plist used as a communication channel."
3180 (not (org-export-get-next-element headline info)))
3183 ;;;; For Links
3185 ;; `org-export-solidify-link-text' turns a string into a safer version
3186 ;; for links, replacing most non-standard characters with hyphens.
3188 ;; `org-export-get-coderef-format' returns an appropriate format
3189 ;; string for coderefs.
3191 ;; `org-export-inline-image-p' returns a non-nil value when the link
3192 ;; provided should be considered as an inline image.
3194 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3195 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3196 ;; returns an appropriate unique identifier when found, or nil.
3198 ;; `org-export-resolve-id-link' returns the first headline with
3199 ;; specified id or custom-id in parse tree, the path to the external
3200 ;; file with the id or nil when neither was found.
3202 ;; `org-export-resolve-coderef' associates a reference to a line
3203 ;; number in the element it belongs, or returns the reference itself
3204 ;; when the element isn't numbered.
3206 (defun org-export-solidify-link-text (s)
3207 "Take link text S and make a safe target out of it."
3208 (save-match-data
3209 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3211 (defun org-export-get-coderef-format (path desc)
3212 "Return format string for code reference link.
3213 PATH is the link path. DESC is its description."
3214 (save-match-data
3215 (cond ((not desc) "%s")
3216 ((string-match (regexp-quote (concat "(" path ")")) desc)
3217 (replace-match "%s" t t desc))
3218 (t desc))))
3220 (defun org-export-inline-image-p (link &optional rules)
3221 "Non-nil if LINK object points to an inline image.
3223 Optional argument is a set of RULES defining inline images. It
3224 is an alist where associations have the following shape:
3226 \(TYPE . REGEXP)
3228 Applying a rule means apply REGEXP against LINK's path when its
3229 type is TYPE. The function will return a non-nil value if any of
3230 the provided rules is non-nil. The default rule is
3231 `org-export-default-inline-image-rule'.
3233 This only applies to links without a description."
3234 (and (not (org-element-contents link))
3235 (let ((case-fold-search t)
3236 (rules (or rules org-export-default-inline-image-rule)))
3237 (catch 'exit
3238 (mapc
3239 (lambda (rule)
3240 (and (string= (org-element-property :type link) (car rule))
3241 (string-match (cdr rule)
3242 (org-element-property :path link))
3243 (throw 'exit t)))
3244 rules)
3245 ;; Return nil if no rule matched.
3246 nil))))
3248 (defun org-export-resolve-coderef (ref info)
3249 "Resolve a code reference REF.
3251 INFO is a plist used as a communication channel.
3253 Return associated line number in source code, or REF itself,
3254 depending on src-block or example element's switches."
3255 (org-element-map
3256 (plist-get info :parse-tree) '(example-block src-block)
3257 (lambda (el)
3258 (with-temp-buffer
3259 (insert (org-trim (org-element-property :value el)))
3260 (let* ((label-fmt (regexp-quote
3261 (or (org-element-property :label-fmt el)
3262 org-coderef-label-format)))
3263 (ref-re
3264 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3265 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3266 ;; Element containing REF is found. Resolve it to either
3267 ;; a label or a line number, as needed.
3268 (when (re-search-backward ref-re nil t)
3269 (cond
3270 ((org-element-property :use-labels el) ref)
3271 ((eq (org-element-property :number-lines el) 'continued)
3272 (+ (org-export-get-loc el info) (line-number-at-pos)))
3273 (t (line-number-at-pos)))))))
3274 info 'first-match))
3276 (defun org-export-resolve-fuzzy-link (link info)
3277 "Return LINK destination.
3279 INFO is a plist holding contextual information.
3281 Return value can be an object, an element, or nil:
3283 - If LINK path matches a target object (i.e. <<path>>) or
3284 element (i.e. \"#+TARGET: path\"), return it.
3286 - If LINK path exactly matches the name affiliated keyword
3287 \(i.e. #+NAME: path) of an element, return that element.
3289 - If LINK path exactly matches any headline name, return that
3290 element. If more than one headline share that name, priority
3291 will be given to the one with the closest common ancestor, if
3292 any, or the first one in the parse tree otherwise.
3294 - Otherwise, return nil.
3296 Assume LINK type is \"fuzzy\"."
3297 (let* ((path (org-element-property :path link))
3298 (match-title-p (eq (aref path 0) ?*)))
3299 (cond
3300 ;; First try to find a matching "<<path>>" unless user specified
3301 ;; he was looking for an headline (path starts with a *
3302 ;; character).
3303 ((and (not match-title-p)
3304 (loop for target in (plist-get info :target-list)
3305 when (string= (org-element-property :value target) path)
3306 return target)))
3307 ;; Then try to find an element with a matching "#+NAME: path"
3308 ;; affiliated keyword.
3309 ((and (not match-title-p)
3310 (org-element-map
3311 (plist-get info :parse-tree) org-element-all-elements
3312 (lambda (el)
3313 (when (string= (org-element-property :name el) path) el))
3314 info 'first-match)))
3315 ;; Last case: link either points to an headline or to
3316 ;; nothingness. Try to find the source, with priority given to
3317 ;; headlines with the closest common ancestor. If such candidate
3318 ;; is found, return it, otherwise return nil.
3320 (let ((find-headline
3321 (function
3322 ;; Return first headline whose `:raw-value' property
3323 ;; is NAME in parse tree DATA, or nil.
3324 (lambda (name data)
3325 (org-element-map
3326 data 'headline
3327 (lambda (headline)
3328 (when (string=
3329 (org-element-property :raw-value headline)
3330 name)
3331 headline))
3332 info 'first-match)))))
3333 ;; Search among headlines sharing an ancestor with link,
3334 ;; from closest to farthest.
3335 (or (catch 'exit
3336 (mapc
3337 (lambda (parent)
3338 (when (eq (org-element-type parent) 'headline)
3339 (let ((foundp (funcall find-headline path parent)))
3340 (when foundp (throw 'exit foundp)))))
3341 (org-export-get-genealogy link)) nil)
3342 ;; No match with a common ancestor: try the full parse-tree.
3343 (funcall find-headline
3344 (if match-title-p (substring path 1) path)
3345 (plist-get info :parse-tree))))))))
3347 (defun org-export-resolve-id-link (link info)
3348 "Return headline referenced as LINK destination.
3350 INFO is a plist used as a communication channel.
3352 Return value can be the headline element matched in current parse
3353 tree, a file name or nil. Assume LINK type is either \"id\" or
3354 \"custom-id\"."
3355 (let ((id (org-element-property :path link)))
3356 ;; First check if id is within the current parse tree.
3357 (or (org-element-map
3358 (plist-get info :parse-tree) 'headline
3359 (lambda (headline)
3360 (when (or (string= (org-element-property :id headline) id)
3361 (string= (org-element-property :custom-id headline) id))
3362 headline))
3363 info 'first-match)
3364 ;; Otherwise, look for external files.
3365 (cdr (assoc id (plist-get info :id-alist))))))
3367 (defun org-export-resolve-radio-link (link info)
3368 "Return radio-target object referenced as LINK destination.
3370 INFO is a plist used as a communication channel.
3372 Return value can be a radio-target object or nil. Assume LINK
3373 has type \"radio\"."
3374 (let ((path (org-element-property :path link)))
3375 (org-element-map
3376 (plist-get info :parse-tree) 'radio-target
3377 (lambda (radio)
3378 (when (equal (org-element-property :value radio) path) radio))
3379 info 'first-match)))
3382 ;;;; For References
3384 ;; `org-export-get-ordinal' associates a sequence number to any object
3385 ;; or element.
3387 (defun org-export-get-ordinal (element info &optional types predicate)
3388 "Return ordinal number of an element or object.
3390 ELEMENT is the element or object considered. INFO is the plist
3391 used as a communication channel.
3393 Optional argument TYPES, when non-nil, is a list of element or
3394 object types, as symbols, that should also be counted in.
3395 Otherwise, only provided element's type is considered.
3397 Optional argument PREDICATE is a function returning a non-nil
3398 value if the current element or object should be counted in. It
3399 accepts two arguments: the element or object being considered and
3400 the plist used as a communication channel. This allows to count
3401 only a certain type of objects (i.e. inline images).
3403 Return value is a list of numbers if ELEMENT is an headline or an
3404 item. It is nil for keywords. It represents the footnote number
3405 for footnote definitions and footnote references. If ELEMENT is
3406 a target, return the same value as if ELEMENT was the closest
3407 table, item or headline containing the target. In any other
3408 case, return the sequence number of ELEMENT among elements or
3409 objects of the same type."
3410 ;; A target keyword, representing an invisible target, never has
3411 ;; a sequence number.
3412 (unless (eq (org-element-type element) 'keyword)
3413 ;; Ordinal of a target object refer to the ordinal of the closest
3414 ;; table, item, or headline containing the object.
3415 (when (eq (org-element-type element) 'target)
3416 (setq element
3417 (loop for parent in (org-export-get-genealogy element)
3418 when
3419 (memq
3420 (org-element-type parent)
3421 '(footnote-definition footnote-reference headline item
3422 table))
3423 return parent)))
3424 (case (org-element-type element)
3425 ;; Special case 1: An headline returns its number as a list.
3426 (headline (org-export-get-headline-number element info))
3427 ;; Special case 2: An item returns its number as a list.
3428 (item (let ((struct (org-element-property :structure element)))
3429 (org-list-get-item-number
3430 (org-element-property :begin element)
3431 struct
3432 (org-list-prevs-alist struct)
3433 (org-list-parents-alist struct))))
3434 ((footnote-definition footnote-reference)
3435 (org-export-get-footnote-number element info))
3436 (otherwise
3437 (let ((counter 0))
3438 ;; Increment counter until ELEMENT is found again.
3439 (org-element-map
3440 (plist-get info :parse-tree) (or types (org-element-type element))
3441 (lambda (el)
3442 (cond
3443 ((eq element el) (1+ counter))
3444 ((not predicate) (incf counter) nil)
3445 ((funcall predicate el info) (incf counter) nil)))
3446 info 'first-match))))))
3449 ;;;; For Src-Blocks
3451 ;; `org-export-get-loc' counts number of code lines accumulated in
3452 ;; src-block or example-block elements with a "+n" switch until
3453 ;; a given element, excluded. Note: "-n" switches reset that count.
3455 ;; `org-export-unravel-code' extracts source code (along with a code
3456 ;; references alist) from an `element-block' or `src-block' type
3457 ;; element.
3459 ;; `org-export-format-code' applies a formatting function to each line
3460 ;; of code, providing relative line number and code reference when
3461 ;; appropriate. Since it doesn't access the original element from
3462 ;; which the source code is coming, it expects from the code calling
3463 ;; it to know if lines should be numbered and if code references
3464 ;; should appear.
3466 ;; Eventually, `org-export-format-code-default' is a higher-level
3467 ;; function (it makes use of the two previous functions) which handles
3468 ;; line numbering and code references inclusion, and returns source
3469 ;; code in a format suitable for plain text or verbatim output.
3471 (defun org-export-get-loc (element info)
3472 "Return accumulated lines of code up to ELEMENT.
3474 INFO is the plist used as a communication channel.
3476 ELEMENT is excluded from count."
3477 (let ((loc 0))
3478 (org-element-map
3479 (plist-get info :parse-tree)
3480 `(src-block example-block ,(org-element-type element))
3481 (lambda (el)
3482 (cond
3483 ;; ELEMENT is reached: Quit the loop.
3484 ((eq el element))
3485 ;; Only count lines from src-block and example-block elements
3486 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3487 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3488 ((let ((linums (org-element-property :number-lines el)))
3489 (when linums
3490 ;; Accumulate locs or reset them.
3491 (let ((lines (org-count-lines
3492 (org-trim (org-element-property :value el)))))
3493 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3494 ;; Return nil to stay in the loop.
3495 nil)))
3496 info 'first-match)
3497 ;; Return value.
3498 loc))
3500 (defun org-export-unravel-code (element)
3501 "Clean source code and extract references out of it.
3503 ELEMENT has either a `src-block' an `example-block' type.
3505 Return a cons cell whose CAR is the source code, cleaned from any
3506 reference and protective comma and CDR is an alist between
3507 relative line number (integer) and name of code reference on that
3508 line (string)."
3509 (let* ((line 0) refs
3510 ;; Get code and clean it. Remove blank lines at its
3511 ;; beginning and end. Also remove protective commas.
3512 (code (let ((c (replace-regexp-in-string
3513 "\\`\\([ \t]*\n\\)+" ""
3514 (replace-regexp-in-string
3515 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3516 (org-element-property :value element)))))
3517 ;; If appropriate, remove global indentation.
3518 (unless (or org-src-preserve-indentation
3519 (org-element-property :preserve-indent element))
3520 (setq c (org-remove-indentation c)))
3521 ;; Free up the protected lines. Note: Org blocks
3522 ;; have commas at the beginning or every line.
3523 (if (string= (org-element-property :language element) "org")
3524 (replace-regexp-in-string "^," "" c)
3525 (replace-regexp-in-string
3526 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3527 ;; Get format used for references.
3528 (label-fmt (regexp-quote
3529 (or (org-element-property :label-fmt element)
3530 org-coderef-label-format)))
3531 ;; Build a regexp matching a loc with a reference.
3532 (with-ref-re
3533 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3534 (replace-regexp-in-string
3535 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3536 ;; Return value.
3537 (cons
3538 ;; Code with references removed.
3539 (org-element-normalize-string
3540 (mapconcat
3541 (lambda (loc)
3542 (incf line)
3543 (if (not (string-match with-ref-re loc)) loc
3544 ;; Ref line: remove ref, and signal its position in REFS.
3545 (push (cons line (match-string 3 loc)) refs)
3546 (replace-match "" nil nil loc 1)))
3547 (org-split-string code "\n") "\n"))
3548 ;; Reference alist.
3549 refs)))
3551 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3552 "Format CODE by applying FUN line-wise and return it.
3554 CODE is a string representing the code to format. FUN is
3555 a function. It must accept three arguments: a line of
3556 code (string), the current line number (integer) or nil and the
3557 reference associated to the current line (string) or nil.
3559 Optional argument NUM-LINES can be an integer representing the
3560 number of code lines accumulated until the current code. Line
3561 numbers passed to FUN will take it into account. If it is nil,
3562 FUN's second argument will always be nil. This number can be
3563 obtained with `org-export-get-loc' function.
3565 Optional argument REF-ALIST can be an alist between relative line
3566 number (i.e. ignoring NUM-LINES) and the name of the code
3567 reference on it. If it is nil, FUN's third argument will always
3568 be nil. It can be obtained through the use of
3569 `org-export-unravel-code' function."
3570 (let ((--locs (org-split-string code "\n"))
3571 (--line 0))
3572 (org-element-normalize-string
3573 (mapconcat
3574 (lambda (--loc)
3575 (incf --line)
3576 (let ((--ref (cdr (assq --line ref-alist))))
3577 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3578 --locs "\n"))))
3580 (defun org-export-format-code-default (element info)
3581 "Return source code from ELEMENT, formatted in a standard way.
3583 ELEMENT is either a `src-block' or `example-block' element. INFO
3584 is a plist used as a communication channel.
3586 This function takes care of line numbering and code references
3587 inclusion. Line numbers, when applicable, appear at the
3588 beginning of the line, separated from the code by two white
3589 spaces. Code references, on the other hand, appear flushed to
3590 the right, separated by six white spaces from the widest line of
3591 code."
3592 ;; Extract code and references.
3593 (let* ((code-info (org-export-unravel-code element))
3594 (code (car code-info))
3595 (code-lines (org-split-string code "\n"))
3596 (refs (and (org-element-property :retain-labels element)
3597 (cdr code-info)))
3598 ;; Handle line numbering.
3599 (num-start (case (org-element-property :number-lines element)
3600 (continued (org-export-get-loc element info))
3601 (new 0)))
3602 (num-fmt
3603 (and num-start
3604 (format "%%%ds "
3605 (length (number-to-string
3606 (+ (length code-lines) num-start))))))
3607 ;; Prepare references display, if required. Any reference
3608 ;; should start six columns after the widest line of code,
3609 ;; wrapped with parenthesis.
3610 (max-width
3611 (+ (apply 'max (mapcar 'length code-lines))
3612 (if (not num-start) 0 (length (format num-fmt num-start))))))
3613 (org-export-format-code
3614 code
3615 (lambda (loc line-num ref)
3616 (let ((number-str (and num-fmt (format num-fmt line-num))))
3617 (concat
3618 number-str
3620 (and ref
3621 (concat (make-string
3622 (- (+ 6 max-width)
3623 (+ (length loc) (length number-str))) ? )
3624 (format "(%s)" ref))))))
3625 num-start refs)))
3628 ;;;; For Tables
3630 ;; `org-export-table-has-special-column-p' and and
3631 ;; `org-export-table-row-is-special-p' are predicates used to look for
3632 ;; meta-information about the table structure.
3634 ;; `org-table-has-header-p' tells when the rows before the first rule
3635 ;; should be considered as table's header.
3637 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3638 ;; and `org-export-table-cell-borders' extract information from
3639 ;; a table-cell element.
3641 ;; `org-export-table-dimensions' gives the number on rows and columns
3642 ;; in the table, ignoring horizontal rules and special columns.
3643 ;; `org-export-table-cell-address', given a table-cell object, returns
3644 ;; the absolute address of a cell. On the other hand,
3645 ;; `org-export-get-table-cell-at' does the contrary.
3647 ;; `org-export-table-cell-starts-colgroup-p',
3648 ;; `org-export-table-cell-ends-colgroup-p',
3649 ;; `org-export-table-row-starts-rowgroup-p',
3650 ;; `org-export-table-row-ends-rowgroup-p',
3651 ;; `org-export-table-row-starts-header-p' and
3652 ;; `org-export-table-row-ends-header-p' indicate position of current
3653 ;; row or cell within the table.
3655 (defun org-export-table-has-special-column-p (table)
3656 "Non-nil when TABLE has a special column.
3657 All special columns will be ignored during export."
3658 ;; The table has a special column when every first cell of every row
3659 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3660 ;; "*" "_" and "^". Though, do not consider a first row containing
3661 ;; only empty cells as special.
3662 (let ((special-column-p 'empty))
3663 (catch 'exit
3664 (mapc
3665 (lambda (row)
3666 (when (eq (org-element-property :type row) 'standard)
3667 (let ((value (org-element-contents
3668 (car (org-element-contents row)))))
3669 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3670 (setq special-column-p 'special))
3671 ((not value))
3672 (t (throw 'exit nil))))))
3673 (org-element-contents table))
3674 (eq special-column-p 'special))))
3676 (defun org-export-table-has-header-p (table info)
3677 "Non-nil when TABLE has an header.
3679 INFO is a plist used as a communication channel.
3681 A table has an header when it contains at least two row groups."
3682 (let ((rowgroup 1) row-flag)
3683 (org-element-map
3684 table 'table-row
3685 (lambda (row)
3686 (cond
3687 ((> rowgroup 1) t)
3688 ((and row-flag (eq (org-element-property :type row) 'rule))
3689 (incf rowgroup) (setq row-flag nil))
3690 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3691 (setq row-flag t) nil)))
3692 info)))
3694 (defun org-export-table-row-is-special-p (table-row info)
3695 "Non-nil if TABLE-ROW is considered special.
3697 INFO is a plist used as the communication channel.
3699 All special rows will be ignored during export."
3700 (when (eq (org-element-property :type table-row) 'standard)
3701 (let ((first-cell (org-element-contents
3702 (car (org-element-contents table-row)))))
3703 ;; A row is special either when...
3705 ;; ... it starts with a field only containing "/",
3706 (equal first-cell '("/"))
3707 ;; ... the table contains a special column and the row start
3708 ;; with a marking character among, "^", "_", "$" or "!",
3709 (and (org-export-table-has-special-column-p
3710 (org-export-get-parent table-row))
3711 (member first-cell '(("^") ("_") ("$") ("!"))))
3712 ;; ... it contains only alignment cookies and empty cells.
3713 (let ((special-row-p 'empty))
3714 (catch 'exit
3715 (mapc
3716 (lambda (cell)
3717 (let ((value (org-element-contents cell)))
3718 ;; Since VALUE is a secondary string, the following
3719 ;; checks avoid expanding it with `org-export-data'.
3720 (cond ((not value))
3721 ((and (not (cdr value))
3722 (stringp (car value))
3723 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3724 (car value)))
3725 (setq special-row-p 'cookie))
3726 (t (throw 'exit nil)))))
3727 (org-element-contents table-row))
3728 (eq special-row-p 'cookie)))))))
3730 (defun org-export-table-row-group (table-row info)
3731 "Return TABLE-ROW's group.
3733 INFO is a plist used as the communication channel.
3735 Return value is the group number, as an integer, or nil special
3736 rows and table rules. Group 1 is also table's header."
3737 (unless (or (eq (org-element-property :type table-row) 'rule)
3738 (org-export-table-row-is-special-p table-row info))
3739 (let ((group 0) row-flag)
3740 (catch 'found
3741 (mapc
3742 (lambda (row)
3743 (cond
3744 ((and (eq (org-element-property :type row) 'standard)
3745 (not (org-export-table-row-is-special-p row info)))
3746 (unless row-flag (incf group) (setq row-flag t)))
3747 ((eq (org-element-property :type row) 'rule)
3748 (setq row-flag nil)))
3749 (when (eq table-row row) (throw 'found group)))
3750 (org-element-contents (org-export-get-parent table-row)))))))
3752 (defun org-export-table-cell-width (table-cell info)
3753 "Return TABLE-CELL contents width.
3755 INFO is a plist used as the communication channel.
3757 Return value is the width given by the last width cookie in the
3758 same column as TABLE-CELL, or nil."
3759 (let* ((row (org-export-get-parent table-cell))
3760 (column (let ((cells (org-element-contents row)))
3761 (- (length cells) (length (memq table-cell cells)))))
3762 (table (org-export-get-parent-table table-cell))
3763 cookie-width)
3764 (mapc
3765 (lambda (row)
3766 (cond
3767 ;; In a special row, try to find a width cookie at COLUMN.
3768 ((org-export-table-row-is-special-p row info)
3769 (let ((value (org-element-contents
3770 (elt (org-element-contents row) column))))
3771 ;; The following checks avoid expanding unnecessarily the
3772 ;; cell with `org-export-data'
3773 (when (and value
3774 (not (cdr value))
3775 (stringp (car value))
3776 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3777 (match-string 1 (car value)))
3778 (setq cookie-width
3779 (string-to-number (match-string 1 (car value)))))))
3780 ;; Ignore table rules.
3781 ((eq (org-element-property :type row) 'rule))))
3782 (org-element-contents table))
3783 ;; Return value.
3784 cookie-width))
3786 (defun org-export-table-cell-alignment (table-cell info)
3787 "Return TABLE-CELL contents alignment.
3789 INFO is a plist used as the communication channel.
3791 Return alignment as specified by the last alignment cookie in the
3792 same column as TABLE-CELL. If no such cookie is found, a default
3793 alignment value will be deduced from fraction of numbers in the
3794 column (see `org-table-number-fraction' for more information).
3795 Possible values are `left', `right' and `center'."
3796 (let* ((row (org-export-get-parent table-cell))
3797 (column (let ((cells (org-element-contents row)))
3798 (- (length cells) (length (memq table-cell cells)))))
3799 (table (org-export-get-parent-table table-cell))
3800 (number-cells 0)
3801 (total-cells 0)
3802 cookie-align)
3803 (mapc
3804 (lambda (row)
3805 (cond
3806 ;; In a special row, try to find an alignment cookie at
3807 ;; COLUMN.
3808 ((org-export-table-row-is-special-p row info)
3809 (let ((value (org-element-contents
3810 (elt (org-element-contents row) column))))
3811 ;; Since VALUE is a secondary string, the following checks
3812 ;; avoid useless expansion through `org-export-data'.
3813 (when (and value
3814 (not (cdr value))
3815 (stringp (car value))
3816 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3817 (car value))
3818 (match-string 1 (car value)))
3819 (setq cookie-align (match-string 1 (car value))))))
3820 ;; Ignore table rules.
3821 ((eq (org-element-property :type row) 'rule))
3822 ;; In a standard row, check if cell's contents are expressing
3823 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3824 ;; Though, don't bother if an alignment cookie has already
3825 ;; defined cell's alignment.
3826 ((not cookie-align)
3827 (let ((value (org-export-data
3828 (org-element-contents
3829 (elt (org-element-contents row) column))
3830 info)))
3831 (incf total-cells)
3832 (when (string-match org-table-number-regexp value)
3833 (incf number-cells))))))
3834 (org-element-contents table))
3835 ;; Return value. Alignment specified by cookies has precedence
3836 ;; over alignment deduced from cells contents.
3837 (cond ((equal cookie-align "l") 'left)
3838 ((equal cookie-align "r") 'right)
3839 ((equal cookie-align "c") 'center)
3840 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3841 'right)
3842 (t 'left))))
3844 (defun org-export-table-cell-borders (table-cell info)
3845 "Return TABLE-CELL borders.
3847 INFO is a plist used as a communication channel.
3849 Return value is a list of symbols, or nil. Possible values are:
3850 `top', `bottom', `above', `below', `left' and `right'. Note:
3851 `top' (resp. `bottom') only happen for a cell in the first
3852 row (resp. last row) of the table, ignoring table rules, if any.
3854 Returned borders ignore special rows."
3855 (let* ((row (org-export-get-parent table-cell))
3856 (table (org-export-get-parent-table table-cell))
3857 borders)
3858 ;; Top/above border? TABLE-CELL has a border above when a rule
3859 ;; used to demarcate row groups can be found above. Hence,
3860 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3861 ;; another regular row has to be found above that rule.
3862 (let (rule-flag)
3863 (catch 'exit
3864 (mapc (lambda (row)
3865 (cond ((eq (org-element-property :type row) 'rule)
3866 (setq rule-flag t))
3867 ((not (org-export-table-row-is-special-p row info))
3868 (if rule-flag (throw 'exit (push 'above borders))
3869 (throw 'exit nil)))))
3870 ;; Look at every row before the current one.
3871 (cdr (memq row (reverse (org-element-contents table)))))
3872 ;; No rule above, or rule found starts the table (ignoring any
3873 ;; special row): TABLE-CELL is at the top of the table.
3874 (when rule-flag (push 'above borders))
3875 (push 'top borders)))
3876 ;; Bottom/below border? TABLE-CELL has a border below when next
3877 ;; non-regular row below is a rule.
3878 (let (rule-flag)
3879 (catch 'exit
3880 (mapc (lambda (row)
3881 (cond ((eq (org-element-property :type row) 'rule)
3882 (setq rule-flag t))
3883 ((not (org-export-table-row-is-special-p row info))
3884 (if rule-flag (throw 'exit (push 'below borders))
3885 (throw 'exit nil)))))
3886 ;; Look at every row after the current one.
3887 (cdr (memq row (org-element-contents table))))
3888 ;; No rule below, or rule found ends the table (modulo some
3889 ;; special row): TABLE-CELL is at the bottom of the table.
3890 (when rule-flag (push 'below borders))
3891 (push 'bottom borders)))
3892 ;; Right/left borders? They can only be specified by column
3893 ;; groups. Column groups are defined in a row starting with "/".
3894 ;; Also a column groups row only contains "<", "<>", ">" or blank
3895 ;; cells.
3896 (catch 'exit
3897 (let ((column (let ((cells (org-element-contents row)))
3898 (- (length cells) (length (memq table-cell cells))))))
3899 (mapc
3900 (lambda (row)
3901 (unless (eq (org-element-property :type row) 'rule)
3902 (when (equal (org-element-contents
3903 (car (org-element-contents row)))
3904 '("/"))
3905 (let ((column-groups
3906 (mapcar
3907 (lambda (cell)
3908 (let ((value (org-element-contents cell)))
3909 (when (member value '(("<") ("<>") (">") nil))
3910 (car value))))
3911 (org-element-contents row))))
3912 ;; There's a left border when previous cell, if
3913 ;; any, ends a group, or current one starts one.
3914 (when (or (and (not (zerop column))
3915 (member (elt column-groups (1- column))
3916 '(">" "<>")))
3917 (member (elt column-groups column) '("<" "<>")))
3918 (push 'left borders))
3919 ;; There's a right border when next cell, if any,
3920 ;; starts a group, or current one ends one.
3921 (when (or (and (/= (1+ column) (length column-groups))
3922 (member (elt column-groups (1+ column))
3923 '("<" "<>")))
3924 (member (elt column-groups column) '(">" "<>")))
3925 (push 'right borders))
3926 (throw 'exit nil)))))
3927 ;; Table rows are read in reverse order so last column groups
3928 ;; row has precedence over any previous one.
3929 (reverse (org-element-contents table)))))
3930 ;; Return value.
3931 borders))
3933 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3934 "Non-nil when TABLE-CELL is at the beginning of a row group.
3935 INFO is a plist used as a communication channel."
3936 ;; A cell starts a column group either when it is at the beginning
3937 ;; of a row (or after the special column, if any) or when it has
3938 ;; a left border.
3939 (or (eq (org-element-map
3940 (org-export-get-parent table-cell)
3941 'table-cell 'identity info 'first-match)
3942 table-cell)
3943 (memq 'left (org-export-table-cell-borders table-cell info))))
3945 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3946 "Non-nil when TABLE-CELL is at the end of a row group.
3947 INFO is a plist used as a communication channel."
3948 ;; A cell ends a column group either when it is at the end of a row
3949 ;; or when it has a right border.
3950 (or (eq (car (last (org-element-contents
3951 (org-export-get-parent table-cell))))
3952 table-cell)
3953 (memq 'right (org-export-table-cell-borders table-cell info))))
3955 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3956 "Non-nil when TABLE-ROW is at the beginning of a column group.
3957 INFO is a plist used as a communication channel."
3958 (unless (or (eq (org-element-property :type table-row) 'rule)
3959 (org-export-table-row-is-special-p table-row info))
3960 (let ((borders (org-export-table-cell-borders
3961 (car (org-element-contents table-row)) info)))
3962 (or (memq 'top borders) (memq 'above borders)))))
3964 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3965 "Non-nil when TABLE-ROW is at the end of a column group.
3966 INFO is a plist used as a communication channel."
3967 (unless (or (eq (org-element-property :type table-row) 'rule)
3968 (org-export-table-row-is-special-p table-row info))
3969 (let ((borders (org-export-table-cell-borders
3970 (car (org-element-contents table-row)) info)))
3971 (or (memq 'bottom borders) (memq 'below borders)))))
3973 (defun org-export-table-row-starts-header-p (table-row info)
3974 "Non-nil when TABLE-ROW is the first table header's row.
3975 INFO is a plist used as a communication channel."
3976 (and (org-export-table-has-header-p
3977 (org-export-get-parent-table table-row) info)
3978 (org-export-table-row-starts-rowgroup-p table-row info)
3979 (= (org-export-table-row-group table-row info) 1)))
3981 (defun org-export-table-row-ends-header-p (table-row info)
3982 "Non-nil when TABLE-ROW is the last table header's row.
3983 INFO is a plist used as a communication channel."
3984 (and (org-export-table-has-header-p
3985 (org-export-get-parent-table table-row) info)
3986 (org-export-table-row-ends-rowgroup-p table-row info)
3987 (= (org-export-table-row-group table-row info) 1)))
3989 (defun org-export-table-dimensions (table info)
3990 "Return TABLE dimensions.
3992 INFO is a plist used as a communication channel.
3994 Return value is a CONS like (ROWS . COLUMNS) where
3995 ROWS (resp. COLUMNS) is the number of exportable
3996 rows (resp. columns)."
3997 (let (first-row (columns 0) (rows 0))
3998 ;; Set number of rows, and extract first one.
3999 (org-element-map
4000 table 'table-row
4001 (lambda (row)
4002 (when (eq (org-element-property :type row) 'standard)
4003 (incf rows)
4004 (unless first-row (setq first-row row)))) info)
4005 ;; Set number of columns.
4006 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4007 ;; Return value.
4008 (cons rows columns)))
4010 (defun org-export-table-cell-address (table-cell info)
4011 "Return address of a regular TABLE-CELL object.
4013 TABLE-CELL is the cell considered. INFO is a plist used as
4014 a communication channel.
4016 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4017 zero-based index. Only exportable cells are considered. The
4018 function returns nil for other cells."
4019 (let* ((table-row (org-export-get-parent table-cell))
4020 (table (org-export-get-parent-table table-cell)))
4021 ;; Ignore cells in special rows or in special column.
4022 (unless (or (org-export-table-row-is-special-p table-row info)
4023 (and (org-export-table-has-special-column-p table)
4024 (eq (car (org-element-contents table-row)) table-cell)))
4025 (cons
4026 ;; Row number.
4027 (let ((row-count 0))
4028 (org-element-map
4029 table 'table-row
4030 (lambda (row)
4031 (cond ((eq (org-element-property :type row) 'rule) nil)
4032 ((eq row table-row) row-count)
4033 (t (incf row-count) nil)))
4034 info 'first-match))
4035 ;; Column number.
4036 (let ((col-count 0))
4037 (org-element-map
4038 table-row 'table-cell
4039 (lambda (cell)
4040 (if (eq cell table-cell) col-count (incf col-count) nil))
4041 info 'first-match))))))
4043 (defun org-export-get-table-cell-at (address table info)
4044 "Return regular table-cell object at ADDRESS in TABLE.
4046 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4047 zero-based index. TABLE is a table type element. INFO is
4048 a plist used as a communication channel.
4050 If no table-cell, among exportable cells, is found at ADDRESS,
4051 return nil."
4052 (let ((column-pos (cdr address)) (column-count 0))
4053 (org-element-map
4054 ;; Row at (car address) or nil.
4055 (let ((row-pos (car address)) (row-count 0))
4056 (org-element-map
4057 table 'table-row
4058 (lambda (row)
4059 (cond ((eq (org-element-property :type row) 'rule) nil)
4060 ((= row-count row-pos) row)
4061 (t (incf row-count) nil)))
4062 info 'first-match))
4063 'table-cell
4064 (lambda (cell)
4065 (if (= column-count column-pos) cell
4066 (incf column-count) nil))
4067 info 'first-match)))
4070 ;;;; For Tables Of Contents
4072 ;; `org-export-collect-headlines' builds a list of all exportable
4073 ;; headline elements, maybe limited to a certain depth. One can then
4074 ;; easily parse it and transcode it.
4076 ;; Building lists of tables, figures or listings is quite similar.
4077 ;; Once the generic function `org-export-collect-elements' is defined,
4078 ;; `org-export-collect-tables', `org-export-collect-figures' and
4079 ;; `org-export-collect-listings' can be derived from it.
4081 (defun org-export-collect-headlines (info &optional n)
4082 "Collect headlines in order to build a table of contents.
4084 INFO is a plist used as a communication channel.
4086 When optional argument N is an integer, it specifies the depth of
4087 the table of contents. Otherwise, it is set to the value of the
4088 last headline level. See `org-export-headline-levels' for more
4089 information.
4091 Return a list of all exportable headlines as parsed elements."
4092 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4093 (org-element-map
4094 (plist-get info :parse-tree)
4095 'headline
4096 (lambda (headline)
4097 ;; Strip contents from HEADLINE.
4098 (let ((relative-level (org-export-get-relative-level headline info)))
4099 (unless (> relative-level n) headline)))
4100 info))
4102 (defun org-export-collect-elements (type info &optional predicate)
4103 "Collect referenceable elements of a determined type.
4105 TYPE can be a symbol or a list of symbols specifying element
4106 types to search. Only elements with a caption are collected.
4108 INFO is a plist used as a communication channel.
4110 When non-nil, optional argument PREDICATE is a function accepting
4111 one argument, an element of type TYPE. It returns a non-nil
4112 value when that element should be collected.
4114 Return a list of all elements found, in order of appearance."
4115 (org-element-map
4116 (plist-get info :parse-tree) type
4117 (lambda (element)
4118 (and (org-element-property :caption element)
4119 (or (not predicate) (funcall predicate element))
4120 element))
4121 info))
4123 (defun org-export-collect-tables (info)
4124 "Build a list of tables.
4125 INFO is a plist used as a communication channel.
4127 Return a list of table elements with a caption."
4128 (org-export-collect-elements 'table info))
4130 (defun org-export-collect-figures (info predicate)
4131 "Build a list of figures.
4133 INFO is a plist used as a communication channel. PREDICATE is
4134 a function which accepts one argument: a paragraph element and
4135 whose return value is non-nil when that element should be
4136 collected.
4138 A figure is a paragraph type element, with a caption, verifying
4139 PREDICATE. The latter has to be provided since a \"figure\" is
4140 a vague concept that may depend on back-end.
4142 Return a list of elements recognized as figures."
4143 (org-export-collect-elements 'paragraph info predicate))
4145 (defun org-export-collect-listings (info)
4146 "Build a list of src blocks.
4148 INFO is a plist used as a communication channel.
4150 Return a list of src-block elements with a caption."
4151 (org-export-collect-elements 'src-block info))
4154 ;;;; Topology
4156 ;; Here are various functions to retrieve information about the
4157 ;; neighbourhood of a given element or object. Neighbours of interest
4158 ;; are direct parent (`org-export-get-parent'), parent headline
4159 ;; (`org-export-get-parent-headline'), first element containing an
4160 ;; object, (`org-export-get-parent-element'), parent table
4161 ;; (`org-export-get-parent-table'), previous element or object
4162 ;; (`org-export-get-previous-element') and next element or object
4163 ;; (`org-export-get-next-element').
4165 ;; `org-export-get-genealogy' returns the full genealogy of a given
4166 ;; element or object, from closest parent to full parse tree.
4168 (defun org-export-get-parent (blob)
4169 "Return BLOB parent or nil.
4170 BLOB is the element or object considered."
4171 (org-element-property :parent blob))
4173 (defun org-export-get-genealogy (blob)
4174 "Return full genealogy relative to a given element or object.
4176 BLOB is the element or object being considered.
4178 Ancestors are returned from closest to farthest, the last one
4179 being the full parse tree."
4180 (let (genealogy (parent blob))
4181 (while (setq parent (org-element-property :parent parent))
4182 (push parent genealogy))
4183 (nreverse genealogy)))
4185 (defun org-export-get-parent-headline (blob)
4186 "Return BLOB parent headline or nil.
4187 BLOB is the element or object being considered."
4188 (let ((parent blob))
4189 (while (and (setq parent (org-element-property :parent parent))
4190 (not (eq (org-element-type parent) 'headline))))
4191 parent))
4193 (defun org-export-get-parent-element (object)
4194 "Return first element containing OBJECT or nil.
4195 OBJECT is the object to consider."
4196 (let ((parent object))
4197 (while (and (setq parent (org-element-property :parent parent))
4198 (memq (org-element-type parent) org-element-all-objects)))
4199 parent))
4201 (defun org-export-get-parent-table (object)
4202 "Return OBJECT parent table or nil.
4203 OBJECT is either a `table-cell' or `table-element' type object."
4204 (let ((parent object))
4205 (while (and (setq parent (org-element-property :parent parent))
4206 (not (eq (org-element-type parent) 'table))))
4207 parent))
4209 (defun org-export-get-previous-element (blob info)
4210 "Return previous element or object.
4211 BLOB is an element or object. INFO is a plist used as
4212 a communication channel. Return previous exportable element or
4213 object, a string, or nil."
4214 (let (prev)
4215 (catch 'exit
4216 (mapc (lambda (obj)
4217 (cond ((eq obj blob) (throw 'exit prev))
4218 ((memq obj (plist-get info :ignore-list)))
4219 (t (setq prev obj))))
4220 (org-element-contents (org-export-get-parent blob))))))
4222 (defun org-export-get-next-element (blob info)
4223 "Return next element or object.
4224 BLOB is an element or object. INFO is a plist used as
4225 a communication channel. Return next exportable element or
4226 object, a string, or nil."
4227 (catch 'found
4228 (mapc (lambda (obj)
4229 (unless (memq obj (plist-get info :ignore-list))
4230 (throw 'found obj)))
4231 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4232 nil))
4235 ;;;; Translation
4237 ;; `org-export-translate' translates a string according to language
4238 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4239 ;; variable and a specified charset. `org-export-dictionary' contains
4240 ;; the dictionary used for the translation.
4242 (defconst org-export-dictionary
4243 '(("Author"
4244 ("ca" :default "Autor")
4245 ("cs" :default "Autor")
4246 ("da" :default "Ophavsmand")
4247 ("de" :default "Autor")
4248 ("eo" :html "A&#365;toro")
4249 ("es" :default "Autor")
4250 ("fi" :html "Tekij&auml;")
4251 ("fr" :default "Auteur")
4252 ("hu" :default "Szerz&otilde;")
4253 ("is" :html "H&ouml;fundur")
4254 ("it" :default "Autore")
4255 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4256 ("nl" :default "Auteur")
4257 ("no" :default "Forfatter")
4258 ("nb" :default "Forfatter")
4259 ("nn" :default "Forfattar")
4260 ("pl" :default "Autor")
4261 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4262 ("sv" :html "F&ouml;rfattare")
4263 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4264 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4265 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4266 ("Date"
4267 ("ca" :default "Data")
4268 ("cs" :default "Datum")
4269 ("da" :default "Dato")
4270 ("de" :default "Datum")
4271 ("eo" :default "Dato")
4272 ("es" :default "Fecha")
4273 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4274 ("hu" :html "D&aacute;tum")
4275 ("is" :default "Dagsetning")
4276 ("it" :default "Data")
4277 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4278 ("nl" :default "Datum")
4279 ("no" :default "Dato")
4280 ("nb" :default "Dato")
4281 ("nn" :default "Dato")
4282 ("pl" :default "Data")
4283 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4284 ("sv" :default "Datum")
4285 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4286 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4287 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4288 ("Equation"
4289 ("fr" :ascii "Equation" :default "Équation"))
4290 ("Figure")
4291 ("Footnotes"
4292 ("ca" :html "Peus de p&agrave;gina")
4293 ("cs" :default "Pozn\xe1mky pod carou")
4294 ("da" :default "Fodnoter")
4295 ("de" :html "Fu&szlig;noten")
4296 ("eo" :default "Piednotoj")
4297 ("es" :html "Pies de p&aacute;gina")
4298 ("fi" :default "Alaviitteet")
4299 ("fr" :default "Notes de bas de page")
4300 ("hu" :html "L&aacute;bjegyzet")
4301 ("is" :html "Aftanm&aacute;lsgreinar")
4302 ("it" :html "Note a pi&egrave; di pagina")
4303 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4304 ("nl" :default "Voetnoten")
4305 ("no" :default "Fotnoter")
4306 ("nb" :default "Fotnoter")
4307 ("nn" :default "Fotnotar")
4308 ("pl" :default "Przypis")
4309 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4310 ("sv" :default "Fotnoter")
4311 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4312 :utf-8 "Примітки")
4313 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4314 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4315 ("List of Listings"
4316 ("fr" :default "Liste des programmes"))
4317 ("List of Tables"
4318 ("fr" :default "Liste des tableaux"))
4319 ("Listing %d:"
4320 ("fr"
4321 :ascii "Programme %d :" :default "Programme nº %d :"
4322 :latin1 "Programme %d :"))
4323 ("Listing %d: %s"
4324 ("fr"
4325 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4326 :latin1 "Programme %d : %s"))
4327 ("See section %s"
4328 ("fr" :default "cf. section %s"))
4329 ("Table %d:"
4330 ("fr"
4331 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4332 ("Table %d: %s"
4333 ("fr"
4334 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4335 :latin1 "Tableau %d : %s"))
4336 ("Table of Contents"
4337 ("ca" :html "&Iacute;ndex")
4338 ("cs" :default "Obsah")
4339 ("da" :default "Indhold")
4340 ("de" :default "Inhaltsverzeichnis")
4341 ("eo" :default "Enhavo")
4342 ("es" :html "&Iacute;ndice")
4343 ("fi" :html "Sis&auml;llysluettelo")
4344 ("fr" :ascii "Sommaire" :default "Table des matières")
4345 ("hu" :html "Tartalomjegyz&eacute;k")
4346 ("is" :default "Efnisyfirlit")
4347 ("it" :default "Indice")
4348 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4349 ("nl" :default "Inhoudsopgave")
4350 ("no" :default "Innhold")
4351 ("nb" :default "Innhold")
4352 ("nn" :default "Innhald")
4353 ("pl" :html "Spis tre&#x015b;ci")
4354 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4355 :utf-8 "Содержание")
4356 ("sv" :html "Inneh&aring;ll")
4357 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4358 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4359 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4360 ("Unknown reference"
4361 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4362 "Dictionary for export engine.
4364 Alist whose CAR is the string to translate and CDR is an alist
4365 whose CAR is the language string and CDR is a plist whose
4366 properties are possible charsets and values translated terms.
4368 It is used as a database for `org-export-translate'. Since this
4369 function returns the string as-is if no translation was found,
4370 the variable only needs to record values different from the
4371 entry.")
4373 (defun org-export-translate (s encoding info)
4374 "Translate string S according to language specification.
4376 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4377 and `:utf-8'. INFO is a plist used as a communication channel.
4379 Translation depends on `:language' property. Return the
4380 translated string. If no translation is found, try to fall back
4381 to `:default' encoding. If it fails, return S."
4382 (let* ((lang (plist-get info :language))
4383 (translations (cdr (assoc lang
4384 (cdr (assoc s org-export-dictionary))))))
4385 (or (plist-get translations encoding)
4386 (plist-get translations :default)
4387 s)))
4391 ;;; The Dispatcher
4393 ;; `org-export-dispatch' is the standard interactive way to start an
4394 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4395 ;; for its interface, which, in turn, delegates response to key
4396 ;; pressed to `org-export-dispatch-action'.
4398 (defvar org-export-dispatch-menu-entries nil
4399 "List of menu entries available for `org-export-dispatch'.
4400 This variable shouldn't be set directly. Set-up :menu-entry
4401 keyword in either `org-export-define-backend' or
4402 `org-export-define-derived-backend' instead.")
4404 ;;;###autoload
4405 (defun org-export-dispatch ()
4406 "Export dispatcher for Org mode.
4408 It provides an access to common export related tasks in a buffer.
4409 Its interface comes in two flavours: standard and expert. While
4410 both share the same set of bindings, only the former displays the
4411 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4412 to switch to one or the other.
4414 Return an error if key pressed has no associated command."
4415 (interactive)
4416 (let* ((input (org-export-dispatch-ui (list org-export-initial-scope)
4418 org-export-dispatch-use-expert-ui))
4419 (action (car input))
4420 (optns (cdr input)))
4421 (case action
4422 ;; First handle special hard-coded actions.
4423 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4424 (publish-current-project
4425 (org-e-publish-current-project (memq 'force optns)))
4426 (publish-choose-project
4427 (org-e-publish (assoc (org-icompleting-read
4428 "Publish project: "
4429 org-e-publish-project-alist nil t)
4430 org-e-publish-project-alist)
4431 (memq 'force optns)))
4432 (publish-all (org-e-publish-all (memq 'force optns)))
4433 (otherwise
4434 (funcall action
4435 (memq 'subtree optns)
4436 (memq 'visible optns)
4437 (memq 'body optns))))))
4439 (defun org-export-dispatch-ui (options first-key expertp)
4440 "Handle interface for `org-export-dispatch'.
4442 OPTIONS is a list containing current interactive options set for
4443 export. It can contain any of the following symbols:
4444 `body' toggles a body-only export
4445 `subtree' restricts export to current subtree
4446 `visible' restricts export to visible part of buffer.
4447 `force' force publishing files.
4449 FIRST-KEY is the key pressed to select the first level menu. It
4450 is nil when this menu hasn't been selected yet.
4452 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4453 buffer is provided, but indications about currently active
4454 options are given in the prompt. Moreover, \[?] allows to switch
4455 back to standard interface."
4456 (let* ((fontify-key
4457 (lambda (key &optional access-key)
4458 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4459 ;; non-nil is the required first-level key to activate
4460 ;; KEY. When its value is t, activate KEY independently
4461 ;; on the first key, if any. A nil value means KEY will
4462 ;; only be activated at first level.
4463 (if (or (eq access-key t) (eq access-key first-key))
4464 (org-add-props key nil 'face 'org-warning)
4465 (org-no-properties key))))
4466 ;; Make sure order of menu doesn't depend on the order in
4467 ;; which back-ends are loaded.
4468 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4469 (lambda (a b) (< (car a) (car b)))))
4470 ;; Compute a list of allowed keys based on the first key
4471 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4472 ;; always available.
4473 (allowed-keys
4474 (nconc (list ?1 ?2 ?3 ?4)
4475 (mapcar 'car
4476 (if (not first-key) backends
4477 (nth 2 (assq first-key backends))))
4478 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4479 ((not first-key) (list ?P)))
4480 (when expertp (list ??))
4481 (list ?q)))
4482 ;; Build the help menu for standard UI.
4483 (help
4484 (unless expertp
4485 (concat
4486 ;; Options are hard-coded.
4487 (format "Options
4488 [%s] Body only: %s [%s] Visible only: %s
4489 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4490 (funcall fontify-key "1" t)
4491 (if (memq 'body options) "On " "Off")
4492 (funcall fontify-key "2" t)
4493 (if (memq 'visible options) "On " "Off")
4494 (funcall fontify-key "3" t)
4495 (if (memq 'subtree options) "Subtree" "Buffer ")
4496 (funcall fontify-key "4" t)
4497 (if (memq 'force options) "On " "Off"))
4498 ;; Display registered back-end entries.
4499 (mapconcat
4500 (lambda (entry)
4501 (let ((top-key (car entry)))
4502 (concat
4503 (format "[%s] %s\n"
4504 (funcall fontify-key (char-to-string top-key))
4505 (nth 1 entry))
4506 (let ((sub-menu (nth 2 entry)))
4507 (unless (functionp sub-menu)
4508 ;; Split sub-menu into two columns.
4509 (let ((index -1))
4510 (concat
4511 (mapconcat
4512 (lambda (sub-entry)
4513 (incf index)
4514 (format (if (zerop (mod index 2)) " [%s] %-24s"
4515 "[%s] %s\n")
4516 (funcall fontify-key
4517 (char-to-string (car sub-entry))
4518 top-key)
4519 (nth 1 sub-entry)))
4520 sub-menu "")
4521 (when (zerop (mod index 2)) "\n"))))))))
4522 backends "\n")
4523 ;; Publishing menu is hard-coded.
4524 (format "\n[%s] Publish
4525 [%s] Current file [%s] Current project
4526 [%s] Choose project [%s] All projects\n\n"
4527 (funcall fontify-key "P")
4528 (funcall fontify-key "f" ?P)
4529 (funcall fontify-key "p" ?P)
4530 (funcall fontify-key "x" ?P)
4531 (funcall fontify-key "a" ?P))
4532 (format "\[%s] %s"
4533 (funcall fontify-key "q" t)
4534 (if first-key "Main menu" "Exit")))))
4535 ;; Build prompts for both standard and expert UI.
4536 (standard-prompt (unless expertp "Export command: "))
4537 (expert-prompt
4538 (when expertp
4539 (format
4540 "Export command (Options: %s%s%s%s) [%s]: "
4541 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4542 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4543 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4544 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4545 (concat allowed-keys)))))
4546 ;; With expert UI, just read key with a fancy prompt. In standard
4547 ;; UI, display an intrusive help buffer.
4548 (if expertp
4549 (org-export-dispatch-action
4550 expert-prompt allowed-keys backends options first-key expertp)
4551 (save-window-excursion
4552 (delete-other-windows)
4553 (unwind-protect
4554 (progn
4555 (with-current-buffer
4556 (get-buffer-create "*Org Export Dispatcher*")
4557 (erase-buffer)
4558 (save-excursion (insert help)))
4559 (org-fit-window-to-buffer
4560 (display-buffer "*Org Export Dispatcher*"))
4561 (org-export-dispatch-action
4562 standard-prompt allowed-keys backends options first-key expertp))
4563 (and (get-buffer "*Org Export Dispatcher*")
4564 (kill-buffer "*Org Export Dispatcher*")))))))
4566 (defun org-export-dispatch-action
4567 (prompt allowed-keys backends options first-key expertp)
4568 "Read a character from command input and act accordingly.
4570 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4571 a list of characters available at a given step in the process.
4572 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4573 EXPERTP are the same as defined in `org-export-dispatch-ui',
4574 which see.
4576 Toggle export options when required. Otherwise, return value is
4577 a list with action as CAR and a list of interactive export
4578 options as CDR."
4579 (let ((key (let ((k (read-char-exclusive prompt)))
4580 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4581 ;; depending on user's key pressed.
4582 (if (< k 27) (+ k 96) k))))
4583 (cond
4584 ;; Ignore non-standard characters (i.e. "M-a") and
4585 ;; undefined associations.
4586 ((not (memq key allowed-keys))
4587 (org-export-dispatch-ui options first-key expertp))
4588 ;; q key at first level aborts export. At second
4589 ;; level, cancel first key instead.
4590 ((eq key ?q) (if (not first-key) (error "Export aborted")
4591 (org-export-dispatch-ui options nil expertp)))
4592 ;; Help key: Switch back to standard interface if
4593 ;; expert UI was active.
4594 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4595 ;; Toggle export options.
4596 ((memq key '(?1 ?2 ?3 ?4))
4597 (org-export-dispatch-ui
4598 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4599 (?4 'force))))
4600 (if (memq option options) (remq option options)
4601 (cons option options)))
4602 first-key expertp))
4603 ;; Action selected: Send key and options back to
4604 ;; `org-export-dispatch'.
4605 ((or first-key
4606 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4607 (functionp (nth 2 (assq key backends))))
4608 (cons (cond
4609 ((not first-key) (nth 2 (assq key backends)))
4610 ;; Publishing actions are hard-coded. Send a special
4611 ;; signal to `org-export-dispatch'.
4612 ((eq first-key ?P)
4613 (case key
4614 (?f 'publish-current-file)
4615 (?p 'publish-current-project)
4616 (?x 'publish-choose-project)
4617 (?a 'publish-all)))
4618 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4619 options))
4620 ;; Otherwise, enter sub-menu.
4621 (t (org-export-dispatch-ui options key expertp)))))
4624 (provide 'org-export)
4625 ;;; org-export.el ends here