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