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