org-export: Objects in secondary strings can also be ignored
[org-mode.git] / contrib / lisp / org-export.el
bloba1e3366572f9d7b3d86596ee0af7429a05e3a30a
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which sets one mandatory variable: his translation table. Its name
52 ;; is always `org-BACKEND-translate-alist' where BACKEND stands for
53 ;; the name chosen for the back-end. Its value is an alist whose keys
54 ;; are elements and objects types and values translator functions.
55 ;; See function's docstring for more information about translators.
57 ;; Optionally, `org-export-define-backend' can also support specific
58 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
59 ;; function documentation for more information.
61 ;; If the new back-end shares most properties with another one,
62 ;; `org-export-define-derived-backend' can be used to simplify the
63 ;; process.
65 ;; Any back-end can define its own variables. Among them, those
66 ;; customizable should belong to the `org-export-BACKEND' group.
68 ;; Tools for common tasks across back-ends are implemented in the
69 ;; penultimate part of this file. A dispatcher for standard back-ends
70 ;; is provided in the last one.
72 ;;; Code:
74 (eval-when-compile (require 'cl))
75 (require 'org-element)
78 (declare-function org-e-ascii-export-as-ascii "org-e-ascii"
79 (&optional subtreep visible-only body-only ext-plist))
80 (declare-function org-e-ascii-export-to-ascii "org-e-ascii"
81 (&optional subtreep visible-only body-only ext-plist pub-dir))
82 (declare-function org-e-html-export-as-html "org-e-html"
83 (&optional subtreep visible-only body-only ext-plist))
84 (declare-function org-e-html-export-to-html "org-e-html"
85 (&optional subtreep visible-only body-only ext-plist pub-dir))
86 (declare-function org-e-latex-export-as-latex "org-e-latex"
87 (&optional subtreep visible-only body-only ext-plist))
88 (declare-function org-e-latex-export-to-latex "org-e-latex"
89 (&optional subtreep visible-only body-only ext-plist pub-dir))
90 (declare-function org-e-latex-export-to-pdf "org-e-latex"
91 (&optional subtreep visible-only body-only ext-plist pub-dir))
92 (declare-function org-e-odt-export-to-odt "org-e-odt"
93 (&optional subtreep visible-only body-only ext-plist pub-dir))
94 (declare-function org-e-publish "org-e-publish" (project &optional force))
95 (declare-function org-e-publish-all "org-e-publish" (&optional force))
96 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
97 (declare-function org-e-publish-current-project "org-e-publish"
98 (&optional force))
99 (declare-function org-export-blocks-preprocess "org-exp-blocks")
101 (defvar org-e-publish-project-alist)
102 (defvar org-table-number-fraction)
103 (defvar org-table-number-regexp)
107 ;;; Internal Variables
109 ;; Among internal variables, the most important is
110 ;; `org-export-options-alist'. This variable define the global export
111 ;; options, shared between every exporter, and how they are acquired.
113 (defconst org-export-max-depth 19
114 "Maximum nesting depth for headlines, counting from 0.")
116 (defconst org-export-options-alist
117 '((:author "AUTHOR" nil user-full-name t)
118 (:creator "CREATOR" nil org-export-creator-string)
119 (:date "DATE" nil nil t)
120 (:description "DESCRIPTION" nil nil newline)
121 (:email "EMAIL" nil user-mail-address t)
122 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
123 (:headline-levels nil "H" org-export-headline-levels)
124 (:keywords "KEYWORDS" nil nil space)
125 (:language "LANGUAGE" nil org-export-default-language t)
126 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
127 (:section-numbers nil "num" org-export-with-section-numbers)
128 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
129 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
130 (:title "TITLE" nil nil space)
131 (:with-archived-trees nil "arch" org-export-with-archived-trees)
132 (:with-author nil "author" org-export-with-author)
133 (:with-clocks nil "c" org-export-with-clocks)
134 (:with-creator nil "creator" org-export-with-creator)
135 (:with-drawers nil "d" org-export-with-drawers)
136 (:with-email nil "email" org-export-with-email)
137 (:with-emphasize nil "*" org-export-with-emphasize)
138 (:with-entities nil "e" org-export-with-entities)
139 (:with-fixed-width nil ":" org-export-with-fixed-width)
140 (:with-footnotes nil "f" org-export-with-footnotes)
141 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
142 (:with-plannings nil "p" org-export-with-planning)
143 (:with-priority nil "pri" org-export-with-priority)
144 (:with-special-strings nil "-" org-export-with-special-strings)
145 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
146 (:with-toc nil "toc" org-export-with-toc)
147 (:with-tables nil "|" org-export-with-tables)
148 (:with-tags nil "tags" org-export-with-tags)
149 (:with-tasks nil "tasks" org-export-with-tasks)
150 (:with-timestamps nil "<" org-export-with-timestamps)
151 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
152 "Alist between export properties and ways to set them.
154 The CAR of the alist is the property name, and the CDR is a list
155 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
157 KEYWORD is a string representing a buffer keyword, or nil. Each
158 property defined this way can also be set, during subtree
159 export, through an headline property named after the keyword
160 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
161 property).
162 OPTION is a string that could be found in an #+OPTIONS: line.
163 DEFAULT is the default value for the property.
164 BEHAVIOUR determine how Org should handle multiple keywords for
165 the same property. It is a symbol among:
166 nil Keep old value and discard the new one.
167 t Replace old value with the new one.
168 `space' Concatenate the values, separating them with a space.
169 `newline' Concatenate the values, separating them with
170 a newline.
171 `split' Split values at white spaces, and cons them to the
172 previous list.
174 KEYWORD and OPTION have precedence over DEFAULT.
176 All these properties should be back-end agnostic. Back-end
177 specific properties are set through `org-export-define-backend'.
178 Properties redefined there have precedence over these.")
180 (defconst org-export-special-keywords
181 '("SETUP_FILE" "OPTIONS" "MACRO")
182 "List of in-buffer keywords that require special treatment.
183 These keywords are not directly associated to a property. The
184 way they are handled must be hard-coded into
185 `org-export--get-inbuffer-options' function.")
187 (defconst org-export-filters-alist
188 '((:filter-bold . org-export-filter-bold-functions)
189 (:filter-babel-call . org-export-filter-babel-call-functions)
190 (:filter-center-block . org-export-filter-center-block-functions)
191 (:filter-clock . org-export-filter-clock-functions)
192 (:filter-code . org-export-filter-code-functions)
193 (:filter-comment . org-export-filter-comment-functions)
194 (:filter-comment-block . org-export-filter-comment-block-functions)
195 (:filter-drawer . org-export-filter-drawer-functions)
196 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
197 (:filter-entity . org-export-filter-entity-functions)
198 (:filter-example-block . org-export-filter-example-block-functions)
199 (:filter-export-block . org-export-filter-export-block-functions)
200 (:filter-export-snippet . org-export-filter-export-snippet-functions)
201 (:filter-final-output . org-export-filter-final-output-functions)
202 (:filter-fixed-width . org-export-filter-fixed-width-functions)
203 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
204 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
205 (:filter-headline . org-export-filter-headline-functions)
206 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
207 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
208 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
209 (:filter-inlinetask . org-export-filter-inlinetask-functions)
210 (:filter-italic . org-export-filter-italic-functions)
211 (:filter-item . org-export-filter-item-functions)
212 (:filter-keyword . org-export-filter-keyword-functions)
213 (:filter-latex-environment . org-export-filter-latex-environment-functions)
214 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
215 (:filter-line-break . org-export-filter-line-break-functions)
216 (:filter-link . org-export-filter-link-functions)
217 (:filter-macro . org-export-filter-macro-functions)
218 (:filter-paragraph . org-export-filter-paragraph-functions)
219 (:filter-parse-tree . org-export-filter-parse-tree-functions)
220 (:filter-plain-list . org-export-filter-plain-list-functions)
221 (:filter-plain-text . org-export-filter-plain-text-functions)
222 (:filter-planning . org-export-filter-planning-functions)
223 (:filter-property-drawer . org-export-filter-property-drawer-functions)
224 (:filter-quote-block . org-export-filter-quote-block-functions)
225 (:filter-quote-section . org-export-filter-quote-section-functions)
226 (:filter-radio-target . org-export-filter-radio-target-functions)
227 (:filter-section . org-export-filter-section-functions)
228 (:filter-special-block . org-export-filter-special-block-functions)
229 (:filter-src-block . org-export-filter-src-block-functions)
230 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
231 (:filter-strike-through . org-export-filter-strike-through-functions)
232 (:filter-subscript . org-export-filter-subscript-functions)
233 (:filter-superscript . org-export-filter-superscript-functions)
234 (:filter-table . org-export-filter-table-functions)
235 (:filter-table-cell . org-export-filter-table-cell-functions)
236 (:filter-table-row . org-export-filter-table-row-functions)
237 (:filter-target . org-export-filter-target-functions)
238 (:filter-timestamp . org-export-filter-timestamp-functions)
239 (:filter-underline . org-export-filter-underline-functions)
240 (:filter-verbatim . org-export-filter-verbatim-functions)
241 (:filter-verse-block . org-export-filter-verse-block-functions))
242 "Alist between filters properties and initial values.
244 The key of each association is a property name accessible through
245 the communication channel. Its value is a configurable global
246 variable defining initial filters.
248 This list is meant to install user specified filters. Back-end
249 developers may install their own filters using
250 `org-export-define-backend'. Filters defined there will always
251 be prepended to the current list, so they always get applied
252 first.")
254 (defconst org-export-default-inline-image-rule
255 `(("file" .
256 ,(format "\\.%s\\'"
257 (regexp-opt
258 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
259 "xpm" "pbm" "pgm" "ppm") t))))
260 "Default rule for link matching an inline image.
261 This rule applies to links with no description. By default, it
262 will be considered as an inline image if it targets a local file
263 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
264 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
265 See `org-export-inline-image-p' for more information about
266 rules.")
270 ;;; User-configurable Variables
272 ;; Configuration for the masses.
274 ;; They should never be accessed directly, as their value is to be
275 ;; stored in a property list (cf. `org-export-options-alist').
276 ;; Back-ends will read their value from there instead.
278 (defgroup org-export nil
279 "Options for exporting Org mode files."
280 :tag "Org Export"
281 :group 'org)
283 (defgroup org-export-general nil
284 "General options for export engine."
285 :tag "Org Export General"
286 :group 'org-export)
288 (defcustom org-export-with-archived-trees 'headline
289 "Whether sub-trees with the ARCHIVE tag should be exported.
291 This can have three different values:
292 nil Do not export, pretend this tree is not present.
293 t Do export the entire tree.
294 `headline' Only export the headline, but skip the tree below it.
296 This option can also be set with the #+OPTIONS line,
297 e.g. \"arch:nil\"."
298 :group 'org-export-general
299 :type '(choice
300 (const :tag "Not at all" nil)
301 (const :tag "Headline only" 'headline)
302 (const :tag "Entirely" t)))
304 (defcustom org-export-with-author t
305 "Non-nil means insert author name into the exported file.
306 This option can also be set with the #+OPTIONS line,
307 e.g. \"author:nil\"."
308 :group 'org-export-general
309 :type 'boolean)
311 (defcustom org-export-with-clocks nil
312 "Non-nil means export CLOCK keywords.
313 This option can also be set with the #+OPTIONS line,
314 e.g. \"c:t\"."
315 :group 'org-export-general
316 :type 'boolean)
318 (defcustom org-export-with-creator 'comment
319 "Non-nil means the postamble should contain a creator sentence.
321 The sentence can be set in `org-export-creator-string' and
322 defaults to \"Generated by Org mode XX in Emacs XXX.\".
324 If the value is `comment' insert it as a comment."
325 :group 'org-export-general
326 :type '(choice
327 (const :tag "No creator sentence" nil)
328 (const :tag "Sentence as a comment" 'comment)
329 (const :tag "Insert the sentence" t)))
331 (defcustom org-export-creator-string
332 (format "Generated by Org mode %s in Emacs %s."
333 (if (fboundp 'org-version) (org-version) "(Unknown)")
334 emacs-version)
335 "String to insert at the end of the generated document."
336 :group 'org-export-general
337 :type '(string :tag "Creator string"))
339 (defcustom org-export-with-drawers t
340 "Non-nil means export contents of standard drawers.
342 When t, all drawers are exported. This may also be a list of
343 drawer names to export. This variable doesn't apply to
344 properties drawers.
346 This option can also be set with the #+OPTIONS line,
347 e.g. \"d:nil\"."
348 :group 'org-export-general
349 :type '(choice
350 (const :tag "All drawers" t)
351 (const :tag "None" nil)
352 (repeat :tag "Selected drawers"
353 (string :tag "Drawer name"))))
355 (defcustom org-export-with-email nil
356 "Non-nil means insert author email into the exported file.
357 This option can also be set with the #+OPTIONS line,
358 e.g. \"email:t\"."
359 :group 'org-export-general
360 :type 'boolean)
362 (defcustom org-export-with-emphasize t
363 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
365 If the export target supports emphasizing text, the word will be
366 typeset in bold, italic, or underlined, respectively. Not all
367 export backends support this.
369 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
370 :group 'org-export-general
371 :type 'boolean)
373 (defcustom org-export-exclude-tags '("noexport")
374 "Tags that exclude a tree from export.
376 All trees carrying any of these tags will be excluded from
377 export. This is without condition, so even subtrees inside that
378 carry one of the `org-export-select-tags' will be removed.
380 This option can also be set with the #+EXCLUDE_TAGS: keyword."
381 :group 'org-export-general
382 :type '(repeat (string :tag "Tag")))
384 (defcustom org-export-with-fixed-width t
385 "Non-nil means lines starting with \":\" will be in fixed width font.
387 This can be used to have pre-formatted text, fragments of code
388 etc. For example:
389 : ;; Some Lisp examples
390 : (while (defc cnt)
391 : (ding))
392 will be looking just like this in also HTML. See also the QUOTE
393 keyword. Not all export backends support this.
395 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
396 :group 'org-export-translation
397 :type 'boolean)
399 (defcustom org-export-with-footnotes t
400 "Non-nil means Org footnotes should be exported.
401 This option can also be set with the #+OPTIONS line,
402 e.g. \"f:nil\"."
403 :group 'org-export-general
404 :type 'boolean)
406 (defcustom org-export-headline-levels 3
407 "The last level which is still exported as a headline.
409 Inferior levels will produce itemize lists when exported.
411 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
412 :group 'org-export-general
413 :type 'integer)
415 (defcustom org-export-default-language "en"
416 "The default language for export and clocktable translations, as a string.
417 This may have an association in
418 `org-clock-clocktable-language-setup'."
419 :group 'org-export-general
420 :type '(string :tag "Language"))
422 (defcustom org-export-preserve-breaks nil
423 "Non-nil means preserve all line breaks when exporting.
425 Normally, in HTML output paragraphs will be reformatted.
427 This option can also be set with the #+OPTIONS line,
428 e.g. \"\\n:t\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-entities t
433 "Non-nil means interpret entities when exporting.
435 For example, HTML export converts \\alpha to &alpha; and \\AA to
436 &Aring;.
438 For a list of supported names, see the constant `org-entities'
439 and the user option `org-entities-user'.
441 This option can also be set with the #+OPTIONS line,
442 e.g. \"e:nil\"."
443 :group 'org-export-general
444 :type 'boolean)
446 (defcustom org-export-with-inlinetasks t
447 "Non-nil means inlinetasks should be exported.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"inline:nil\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-with-planning nil
454 "Non-nil means include planning info in export.
455 This option can also be set with the #+OPTIONS: line,
456 e.g. \"p:t\"."
457 :group 'org-export-general
458 :type 'boolean)
460 (defcustom org-export-with-priority nil
461 "Non-nil means include priority cookies in export.
462 This option can also be set with the #+OPTIONS line,
463 e.g. \"pri:t\"."
464 :group 'org-export-general
465 :type 'boolean)
467 (defcustom org-export-with-section-numbers t
468 "Non-nil means add section numbers to headlines when exporting.
470 When set to an integer n, numbering will only happen for
471 headlines whose relative level is higher or equal to n.
473 This option can also be set with the #+OPTIONS line,
474 e.g. \"num:t\"."
475 :group 'org-export-general
476 :type 'boolean)
478 (defcustom org-export-select-tags '("export")
479 "Tags that select a tree for export.
481 If any such tag is found in a buffer, all trees that do not carry
482 one of these tags will be ignored during export. Inside trees
483 that are selected like this, you can still deselect a subtree by
484 tagging it with one of the `org-export-exclude-tags'.
486 This option can also be set with the #+SELECT_TAGS: keyword."
487 :group 'org-export-general
488 :type '(repeat (string :tag "Tag")))
490 (defcustom org-export-with-special-strings t
491 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
493 When this option is turned on, these strings will be exported as:
495 Org HTML LaTeX UTF-8
496 -----+----------+--------+-------
497 \\- &shy; \\-
498 -- &ndash; -- –
499 --- &mdash; --- —
500 ... &hellip; \\ldots …
502 This option can also be set with the #+OPTIONS line,
503 e.g. \"-:nil\"."
504 :group 'org-export-general
505 :type 'boolean)
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
529 e.g. \"^:nil\"."
530 :group 'org-export-general
531 :type '(choice
532 (const :tag "Interpret them" t)
533 (const :tag "Curly brackets only" {})
534 (const :tag "Do not interpret them" nil)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
544 contents is made.
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group 'org-export-general
549 :type '(choice
550 (const :tag "No Table of Contents" nil)
551 (const :tag "Full Table of Contents" t)
552 (integer :tag "TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
556 For example:
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group 'org-export-general
564 :type 'boolean)
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
571 the document.
573 This option can also be set with the #+OPTIONS line,
574 e.g. \"tags:nil\"."
575 :group 'org-export-general
576 :type '(choice
577 (const :tag "Off" nil)
578 (const :tag "Not in TOC" not-in-toc)
579 (const :tag "On" t)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
583 This may have the following values:
584 t include tasks independent of state.
585 todo include only tasks that are not yet done.
586 done include only tasks that are already done.
587 nil remove all tasks before export
588 list of keywords keep only tasks with these keywords"
589 :group 'org-export-general
590 :type '(choice
591 (const :tag "All tasks" t)
592 (const :tag "No tasks" nil)
593 (const :tag "Not-done tasks" todo)
594 (const :tag "Only done tasks" done)
595 (repeat :tag "Specific TODO keywords"
596 (string :tag "Keyword"))))
598 (defcustom org-export-time-stamp-file t
599 "Non-nil means insert a time stamp into the exported file.
600 The time stamp shows when the file was created.
602 This option can also be set with the #+OPTIONS line,
603 e.g. \"timestamp:nil\"."
604 :group 'org-export-general
605 :type 'boolean)
607 (defcustom org-export-with-timestamps t
608 "Non nil means allow timestamps in export.
610 It can be set to `active', `inactive', t or nil, in order to
611 export, respectively, only active timestamps, only inactive ones,
612 all of them or none.
614 This option can also be set with the #+OPTIONS line, e.g.
615 \"<:nil\"."
616 :group 'org-export-general
617 :type '(choice
618 (const :tag "All timestamps" t)
619 (const :tag "Only active timestamps" active)
620 (const :tag "Only inactive timestamps" inactive)
621 (const :tag "No timestamp" nil)))
623 (defcustom org-export-with-todo-keywords t
624 "Non-nil means include TODO keywords in export.
625 When nil, remove all these keywords from the export."
626 :group 'org-export-general
627 :type 'boolean)
629 (defcustom org-export-allow-BIND 'confirm
630 "Non-nil means allow #+BIND to define local variable values for export.
631 This is a potential security risk, which is why the user must
632 confirm the use of these lines."
633 :group 'org-export-general
634 :type '(choice
635 (const :tag "Never" nil)
636 (const :tag "Always" t)
637 (const :tag "Ask a confirmation for each file" confirm)))
639 (defcustom org-export-snippet-translation-alist nil
640 "Alist between export snippets back-ends and exporter back-ends.
642 This variable allows to provide shortcuts for export snippets.
644 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
645 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
646 HTML code while every other back-end will ignore it."
647 :group 'org-export-general
648 :type '(repeat
649 (cons
650 (string :tag "Shortcut")
651 (string :tag "Back-end"))))
653 (defcustom org-export-coding-system nil
654 "Coding system for the exported file."
655 :group 'org-export-general
656 :type 'coding-system)
658 (defcustom org-export-copy-to-kill-ring t
659 "Non-nil means exported stuff will also be pushed onto the kill ring."
660 :group 'org-export-general
661 :type 'boolean)
663 (defcustom org-export-initial-scope 'buffer
664 "The initial scope when exporting with `org-export-dispatch'.
665 This variable can be either set to `buffer' or `subtree'."
666 :group 'org-export-general
667 :type '(choice
668 (const :tag "Export current buffer" 'buffer)
669 (const :tag "Export current subtree" 'subtree)))
671 (defcustom org-export-show-temporary-export-buffer t
672 "Non-nil means show buffer after exporting to temp buffer.
673 When Org exports to a file, the buffer visiting that file is ever
674 shown, but remains buried. However, when exporting to
675 a temporary buffer, that buffer is popped up in a second window.
676 When this variable is nil, the buffer remains buried also in
677 these cases."
678 :group 'org-export-general
679 :type 'boolean)
681 (defcustom org-export-dispatch-use-expert-ui nil
682 "Non-nil means using a non-intrusive `org-export-dispatch'.
683 In that case, no help buffer is displayed. Though, an indicator
684 for current export scope is added to the prompt \(i.e. \"b\" when
685 output is restricted to body only, \"s\" when it is restricted to
686 the current subtree and \"v\" when only visible elements are
687 considered for export\). Also, \[?] allows to switch back to
688 standard mode."
689 :group 'org-export-general
690 :type 'boolean)
694 ;;; Defining New Back-ends
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 :options-alist
754 Alist between back-end specific properties introduced in
755 communication channel and how their value are acquired. See
756 `org-export-options-alist' for more information about
757 structure of the values.
759 As an example, here is how the `e-ascii' back-end is defined:
761 \(org-export-define-backend e-ascii
762 \((bold . org-e-ascii-bold)
763 \(center-block . org-e-ascii-center-block)
764 \(clock . org-e-ascii-clock)
765 \(code . org-e-ascii-code)
766 \(drawer . org-e-ascii-drawer)
767 \(dynamic-block . org-e-ascii-dynamic-block)
768 \(entity . org-e-ascii-entity)
769 \(example-block . org-e-ascii-example-block)
770 \(export-block . org-e-ascii-export-block)
771 \(export-snippet . org-e-ascii-export-snippet)
772 \(fixed-width . org-e-ascii-fixed-width)
773 \(footnote-definition . org-e-ascii-footnote-definition)
774 \(footnote-reference . org-e-ascii-footnote-reference)
775 \(headline . org-e-ascii-headline)
776 \(horizontal-rule . org-e-ascii-horizontal-rule)
777 \(inline-src-block . org-e-ascii-inline-src-block)
778 \(inlinetask . org-e-ascii-inlinetask)
779 \(italic . org-e-ascii-italic)
780 \(item . org-e-ascii-item)
781 \(keyword . org-e-ascii-keyword)
782 \(latex-environment . org-e-ascii-latex-environment)
783 \(latex-fragment . org-e-ascii-latex-fragment)
784 \(line-break . org-e-ascii-line-break)
785 \(link . org-e-ascii-link)
786 \(macro . org-e-ascii-macro)
787 \(paragraph . org-e-ascii-paragraph)
788 \(plain-list . org-e-ascii-plain-list)
789 \(plain-text . org-e-ascii-plain-text)
790 \(planning . org-e-ascii-planning)
791 \(property-drawer . org-e-ascii-property-drawer)
792 \(quote-block . org-e-ascii-quote-block)
793 \(quote-section . org-e-ascii-quote-section)
794 \(radio-target . org-e-ascii-radio-target)
795 \(section . org-e-ascii-section)
796 \(special-block . org-e-ascii-special-block)
797 \(src-block . org-e-ascii-src-block)
798 \(statistics-cookie . org-e-ascii-statistics-cookie)
799 \(strike-through . org-e-ascii-strike-through)
800 \(subscript . org-e-ascii-subscript)
801 \(superscript . org-e-ascii-superscript)
802 \(table . org-e-ascii-table)
803 \(table-cell . org-e-ascii-table-cell)
804 \(table-row . org-e-ascii-table-row)
805 \(target . org-e-ascii-target)
806 \(template . org-e-ascii-template)
807 \(timestamp . org-e-ascii-timestamp)
808 \(underline . org-e-ascii-underline)
809 \(verbatim . org-e-ascii-verbatim)
810 \(verse-block . org-e-ascii-verse-block))
811 :export-block \"ASCII\"
812 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
813 \(:filter-section . org-e-ascii-filter-headline-blank-lines))
814 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))"
815 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
816 (indent 1))
817 (let (filters options export-block)
818 (while (keywordp (car body))
819 (case (pop body)
820 (:export-block (let ((names (pop body)))
821 (setq export-block
822 (if (consp names) (mapcar 'upcase names)
823 (list (upcase names))))))
824 (:filters-alist (setq filters (pop body)))
825 (:options-alist (setq options (pop body)))
826 (t (pop body))))
827 `(progn
828 ;; Define translators.
829 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
830 "Alist between element or object types and translators.")
831 ;; Define options.
832 ,(when options
833 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
834 ,(format "Alist between %s export properties and ways to set them.
835 See `org-export-options-alist' for more information on the
836 structure of the values."
837 backend)))
838 ;; Define filters.
839 ,(when filters
840 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
841 "Alist between filters keywords and back-end specific filters.
842 See `org-export-filters-alist' for more information."))
843 ;; Tell parser to not parse EXPORT-BLOCK blocks.
844 ,(when export-block
845 `(mapc
846 (lambda (name)
847 (add-to-list 'org-element-block-name-alist
848 `(,name . org-element-export-block-parser)))
849 ',export-block))
850 ;; Splice in the body, if any.
851 ,@body)))
853 (defmacro org-export-define-derived-backend (child parent &rest body)
854 "Create a new back-end as a variant of an existing one.
856 CHILD is the name of the derived back-end. PARENT is the name of
857 the parent back-end.
859 BODY can start with pre-defined keyword arguments. The following
860 keywords are understood:
862 :export-block
864 String, or list of strings, representing block names that
865 will not be parsed. This is used to specify blocks that will
866 contain raw code specific to the back-end. These blocks
867 still have to be handled by the relative `export-block' type
868 translator.
870 :filters-alist
872 Alist of filters that will overwrite or complete filters
873 defined in PARENT back-end. See `org-export-filters-alist'
874 for more a list of allowed filters.
876 :options-alist
878 Alist of back-end specific properties that will overwrite or
879 complete those defined in PARENT back-end. Refer to
880 `org-export-options-alist' for more information about
881 structure of the values.
883 :translate-alist
885 Alist of element and object types and transcoders that will
886 overwrite or complete transcode table from PARENT back-end.
887 Refer to `org-export-define-backend' for detailed information
888 about transcoders.
890 As an example, here is how one could define \"my-latex\" back-end
891 as a variant of `e-latex' back-end with a custom template
892 function:
894 \(org-export-define-derived-backend my-latex e-latex
895 :translate-alist ((template . my-latex-template-fun)))
897 The back-end could then be called with, for example:
899 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
900 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
901 (indent 2))
902 (let (filters options translate export-block)
903 (while (keywordp (car body))
904 (case (pop body)
905 (:export-block (let ((names (pop body)))
906 (setq export-block
907 (if (consp names) (mapcar 'upcase names)
908 (list (upcase names))))))
909 (:filters-alist (setq filters (pop body)))
910 (:options-alist (setq options (pop body)))
911 (:translate-alist (setq translate (pop body)))
912 (t (pop body))))
913 `(progn
914 ;; Tell parser to not parse EXPORT-BLOCK blocks.
915 ,(when export-block
916 `(mapc
917 (lambda (name)
918 (add-to-list 'org-element-block-name-alist
919 `(,name . org-element-export-block-parser)))
920 ',export-block))
921 ;; Define filters.
922 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
923 (when (or (boundp parent-filters) filters)
924 `(defconst ,(intern (format "org-%s-filters-alist" child))
925 ',(append filters
926 (and (boundp parent-filters)
927 (copy-sequence (symbol-value parent-filters))))
928 "Alist between filters keywords and back-end specific filters.
929 See `org-export-filters-alist' for more information.")))
930 ;; Define options.
931 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
932 (when (or (boundp parent-options) options)
933 `(defconst ,(intern (format "org-%s-options-alist" child))
934 ',(append options
935 (and (boundp parent-options)
936 (copy-sequence (symbol-value parent-options))))
937 ,(format "Alist between %s export properties and ways to set them.
938 See `org-export-options-alist' for more information on the
939 structure of the values."
940 child))))
941 ;; Define translators.
942 (defvar ,(intern (format "org-%s-translate-alist" child))
943 ',(append translate
944 (copy-sequence
945 (symbol-value
946 (intern (format "org-%s-translate-alist" parent)))))
947 "Alist between element or object types and translators.")
948 ;; Splice in the body, if any.
949 ,@body)))
953 ;;; The Communication Channel
955 ;; During export process, every function has access to a number of
956 ;; properties. They are of two types:
958 ;; 1. Environment options are collected once at the very beginning of
959 ;; the process, out of the original buffer and configuration.
960 ;; Collecting them is handled by `org-export-get-environment'
961 ;; function.
963 ;; Most environment options are defined through the
964 ;; `org-export-options-alist' variable.
966 ;; 2. Tree properties are extracted directly from the parsed tree,
967 ;; just before export, by `org-export-collect-tree-properties'.
969 ;; Here is the full list of properties available during transcode
970 ;; process, with their category (option, tree or local) and their
971 ;; value type.
973 ;; + `:author' :: Author's name.
974 ;; - category :: option
975 ;; - type :: string
977 ;; + `:back-end' :: Current back-end used for transcoding.
978 ;; - category :: tree
979 ;; - type :: symbol
981 ;; + `:creator' :: String to write as creation information.
982 ;; - category :: option
983 ;; - type :: string
985 ;; + `:date' :: String to use as date.
986 ;; - category :: option
987 ;; - type :: string
989 ;; + `:description' :: Description text for the current data.
990 ;; - category :: option
991 ;; - type :: string
993 ;; + `:email' :: Author's email.
994 ;; - category :: option
995 ;; - type :: string
997 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
998 ;; process.
999 ;; - category :: option
1000 ;; - type :: list of strings
1002 ;; + `:exported-data' :: Hash table used for memoizing
1003 ;; `org-export-data'.
1004 ;; - category :: tree
1005 ;; - type :: hash table
1007 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1008 ;; their definition, as parsed data. Only non-inlined footnotes
1009 ;; are represented in this alist. Also, every definition isn't
1010 ;; guaranteed to be referenced in the parse tree. The purpose of
1011 ;; this property is to preserve definitions from oblivion
1012 ;; (i.e. when the parse tree comes from a part of the original
1013 ;; buffer), it isn't meant for direct use in a back-end. To
1014 ;; retrieve a definition relative to a reference, use
1015 ;; `org-export-get-footnote-definition' instead.
1016 ;; - category :: option
1017 ;; - type :: alist (STRING . LIST)
1019 ;; + `:headline-levels' :: Maximum level being exported as an
1020 ;; headline. Comparison is done with the relative level of
1021 ;; headlines in the parse tree, not necessarily with their
1022 ;; actual level.
1023 ;; - category :: option
1024 ;; - type :: integer
1026 ;; + `:headline-offset' :: Difference between relative and real level
1027 ;; of headlines in the parse tree. For example, a value of -1
1028 ;; means a level 2 headline should be considered as level
1029 ;; 1 (cf. `org-export-get-relative-level').
1030 ;; - category :: tree
1031 ;; - type :: integer
1033 ;; + `:headline-numbering' :: Alist between headlines and their
1034 ;; numbering, as a list of numbers
1035 ;; (cf. `org-export-get-headline-number').
1036 ;; - category :: tree
1037 ;; - type :: alist (INTEGER . LIST)
1039 ;; + `:id-alist' :: Alist between ID strings and destination file's
1040 ;; path, relative to current directory. It is used by
1041 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1042 ;; external file.
1043 ;; - category :: option
1044 ;; - type :: alist (STRING . STRING)
1046 ;; + `:ignore-list' :: List of elements and objects that should be
1047 ;; ignored during export.
1048 ;; - category :: tree
1049 ;; - type :: list of elements and objects
1051 ;; + `:input-file' :: Full path to input file, if any.
1052 ;; - category :: option
1053 ;; - type :: string or nil
1055 ;; + `:keywords' :: List of keywords attached to data.
1056 ;; - category :: option
1057 ;; - type :: string
1059 ;; + `:language' :: Default language used for translations.
1060 ;; - category :: option
1061 ;; - type :: string
1063 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1064 ;; transcoding.
1065 ;; - category :: option
1066 ;; - type :: list (as returned by `org-element-parse-buffer')
1068 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1069 ;; all line breaks.
1070 ;; - category :: option
1071 ;; - type :: symbol (nil, t)
1073 ;; + `:section-numbers' :: Non-nil means transcoding should add
1074 ;; section numbers to headlines.
1075 ;; - category :: option
1076 ;; - type :: symbol (nil, t)
1078 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1079 ;; in transcoding. When such a tag is present, subtrees without
1080 ;; it are de facto excluded from the process. See
1081 ;; `use-select-tags'.
1082 ;; - category :: option
1083 ;; - type :: list of strings
1085 ;; + `:target-list' :: List of targets encountered in the parse tree.
1086 ;; This is used to partly resolve "fuzzy" links
1087 ;; (cf. `org-export-resolve-fuzzy-link').
1088 ;; - category :: tree
1089 ;; - type :: list of strings
1091 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1092 ;; a time stamp in the output.
1093 ;; - category :: option
1094 ;; - type :: symbol (nil, t)
1096 ;; + `:translate-alist' :: Alist between element and object types and
1097 ;; transcoding functions relative to the current back-end.
1098 ;; Special keys `template' and `plain-text' are also possible.
1099 ;; - category :: option
1100 ;; - type :: alist (SYMBOL . FUNCTION)
1102 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1103 ;; also be transcoded. If it is set to the `headline' symbol,
1104 ;; only the archived headline's name is retained.
1105 ;; - category :: option
1106 ;; - type :: symbol (nil, t, `headline')
1108 ;; + `:with-author' :: Non-nil means author's name should be included
1109 ;; in the output.
1110 ;; - category :: option
1111 ;; - type :: symbol (nil, t)
1113 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1114 ;; - category :: option
1115 ;; - type :: symbol (nil, t)
1117 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1118 ;; inserted at the end of the transcoded string. If the value
1119 ;; is `comment', it should be commented.
1120 ;; - category :: option
1121 ;; - type :: symbol (`comment', nil, t)
1123 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1124 ;; its value is a list of names, only drawers with such names
1125 ;; will be transcoded.
1126 ;; - category :: option
1127 ;; - type :: symbol (nil, t) or list of strings
1129 ;; + `:with-email' :: Non-nil means output should contain author's
1130 ;; email.
1131 ;; - category :: option
1132 ;; - type :: symbol (nil, t)
1134 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1135 ;; interpreted.
1136 ;; - category :: option
1137 ;; - type :: symbol (nil, t)
1139 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1140 ;; strings starting with a colon as a fixed-with (verbatim) area.
1141 ;; - category :: option
1142 ;; - type :: symbol (nil, t)
1144 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1145 ;; footnotes.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:with-plannings' :: Non-nil means transcoding should include
1150 ;; planning info.
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-priority' :: Non-nil means transcoding should include
1155 ;; priority cookies.
1156 ;; - category :: option
1157 ;; - type :: symbol (nil, t)
1159 ;; + `:with-special-strings' :: Non-nil means transcoding should
1160 ;; interpret special strings in plain text.
1161 ;; - category :: option
1162 ;; - type :: symbol (nil, t)
1164 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1165 ;; interpret subscript and superscript. With a value of "{}",
1166 ;; only interpret those using curly brackets.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, {}, t)
1170 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1171 ;; tables.
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1176 ;; headlines. A `not-in-toc' value will remove them from the
1177 ;; table of contents, if any, nonetheless.
1178 ;; - category :: option
1179 ;; - type :: symbol (nil, t, `not-in-toc')
1181 ;; + `:with-tasks' :: Non-nil means transcoding should include
1182 ;; headlines with a TODO keyword. A `todo' value will only
1183 ;; include headlines with a todo type keyword while a `done'
1184 ;; value will do the contrary. If a list of strings is provided,
1185 ;; only tasks with keywords belonging to that list will be kept.
1186 ;; - category :: option
1187 ;; - type :: symbol (t, todo, done, nil) or list of strings
1189 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1190 ;; time stamps. Special value `active' (resp. `inactive') ask to
1191 ;; export only active (resp. inactive) timestamps. Otherwise,
1192 ;; completely remove them.
1193 ;; - category :: option
1194 ;; - type :: symbol: (`active', `inactive', t, nil)
1196 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1197 ;; added to the output. An integer value limits its depth.
1198 ;; - category :: option
1199 ;; - type :: symbol (nil, t or integer)
1201 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1202 ;; include TODO keywords.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, t)
1207 ;;;; Environment Options
1209 ;; Environment options encompass all parameters defined outside the
1210 ;; scope of the parsed data. They come from five sources, in
1211 ;; increasing precedence order:
1213 ;; - Global variables,
1214 ;; - Buffer's attributes,
1215 ;; - Options keyword symbols,
1216 ;; - Buffer keywords,
1217 ;; - Subtree properties.
1219 ;; The central internal function with regards to environment options
1220 ;; is `org-export-get-environment'. It updates global variables with
1221 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1222 ;; the different sources.
1224 ;; The internal functions doing the retrieval are:
1225 ;; `org-export--get-global-options',
1226 ;; `org-export--get-buffer-attributes',
1227 ;; `org-export--parse-option-keyword',
1228 ;; `org-export--get-subtree-options' and
1229 ;; `org-export--get-inbuffer-options'
1231 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1232 ;; take care of the part relative to "#+BIND:" keywords.
1234 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1235 "Collect export options from the current buffer.
1237 Optional argument BACKEND is a symbol specifying which back-end
1238 specific options to read, if any.
1240 When optional argument SUBTREEP is non-nil, assume the export is
1241 done against the current sub-tree.
1243 Third optional argument EXT-PLIST is a property list with
1244 external parameters overriding Org default settings, but still
1245 inferior to file-local settings."
1246 ;; First install #+BIND variables.
1247 (org-export--install-letbind-maybe)
1248 ;; Get and prioritize export options...
1249 (org-combine-plists
1250 ;; ... from global variables...
1251 (org-export--get-global-options backend)
1252 ;; ... from buffer's attributes...
1253 (org-export--get-buffer-attributes)
1254 ;; ... from an external property list...
1255 ext-plist
1256 ;; ... from in-buffer settings...
1257 (org-export--get-inbuffer-options
1258 backend
1259 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1260 ;; ... and from subtree, when appropriate.
1261 (and subtreep (org-export--get-subtree-options backend))
1262 ;; Eventually install back-end symbol and its translation table.
1263 `(:back-end
1264 ,backend
1265 :translate-alist
1266 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1267 (when (boundp trans-alist) (symbol-value trans-alist))))))
1269 (defun org-export--parse-option-keyword (options &optional backend)
1270 "Parse an OPTIONS line and return values as a plist.
1271 Optional argument BACKEND is a symbol specifying which back-end
1272 specific items to read, if any."
1273 (let* ((all
1274 (append org-export-options-alist
1275 (and backend
1276 (let ((var (intern
1277 (format "org-%s-options-alist" backend))))
1278 (and (boundp var) (eval var))))))
1279 ;; Build an alist between #+OPTION: item and property-name.
1280 (alist (delq nil
1281 (mapcar (lambda (e)
1282 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1283 (car e))))
1284 all)))
1285 plist)
1286 (mapc (lambda (e)
1287 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1288 (car e)
1289 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1290 options)
1291 (setq plist (plist-put plist
1292 (cdr e)
1293 (car (read-from-string
1294 (match-string 2 options)))))))
1295 alist)
1296 plist))
1298 (defun org-export--get-subtree-options (&optional backend)
1299 "Get export options in subtree at point.
1300 Optional argument BACKEND is a symbol specifying back-end used
1301 for export. Return options as a plist."
1302 ;; For each buffer keyword, create an headline property setting the
1303 ;; same property in communication channel. The name for the property
1304 ;; is the keyword with "EXPORT_" appended to it.
1305 (org-with-wide-buffer
1306 (let (prop plist)
1307 ;; Make sure point is at an heading.
1308 (unless (org-at-heading-p) (org-back-to-heading t))
1309 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1310 ;; title as its fallback value.
1311 (when (setq prop (progn (looking-at org-todo-line-regexp)
1312 (or (save-match-data
1313 (org-entry-get (point) "EXPORT_TITLE"))
1314 (org-match-string-no-properties 3))))
1315 (setq plist
1316 (plist-put
1317 plist :title
1318 (org-element-parse-secondary-string
1319 prop (org-element-restriction 'keyword)))))
1320 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1321 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1322 (setq plist
1323 (nconc plist (org-export--parse-option-keyword prop backend))))
1324 ;; Handle other keywords.
1325 (let ((seen '("TITLE")))
1326 (mapc
1327 (lambda (option)
1328 (let ((property (nth 1 option)))
1329 (when (and property (not (member property seen)))
1330 (let* ((subtree-prop (concat "EXPORT_" property))
1331 ;; Export properties are not case-sensitive.
1332 (value (let ((case-fold-search t))
1333 (org-entry-get (point) subtree-prop))))
1334 (push property seen)
1335 (when value
1336 (setq plist
1337 (plist-put
1338 plist
1339 (car option)
1340 ;; Parse VALUE if required.
1341 (if (member property org-element-parsed-keywords)
1342 (org-element-parse-secondary-string
1343 value (org-element-restriction 'keyword))
1344 value))))))))
1345 ;; Also look for both general keywords and back-end specific
1346 ;; options if BACKEND is provided.
1347 (append (and backend
1348 (let ((var (intern
1349 (format "org-%s-options-alist" backend))))
1350 (and (boundp var) (symbol-value var))))
1351 org-export-options-alist)))
1352 ;; Return value.
1353 plist)))
1355 (defun org-export--get-inbuffer-options (&optional backend files)
1356 "Return current buffer export options, as a plist.
1358 Optional argument BACKEND, when non-nil, is a symbol specifying
1359 which back-end specific options should also be read in the
1360 process.
1362 Optional argument FILES is a list of setup files names read so
1363 far, used to avoid circular dependencies.
1365 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1366 (org-with-wide-buffer
1367 (goto-char (point-min))
1368 (let ((case-fold-search t) plist)
1369 ;; 1. Special keywords, as in `org-export-special-keywords'.
1370 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1371 (while (re-search-forward special-re nil t)
1372 (let ((element (org-element-at-point)))
1373 (when (eq (org-element-type element) 'keyword)
1374 (let* ((key (org-element-property :key element))
1375 (val (org-element-property :value element))
1376 (prop
1377 (cond
1378 ((string= key "SETUP_FILE")
1379 (let ((file
1380 (expand-file-name
1381 (org-remove-double-quotes (org-trim val)))))
1382 ;; Avoid circular dependencies.
1383 (unless (member file files)
1384 (with-temp-buffer
1385 (insert (org-file-contents file 'noerror))
1386 (org-mode)
1387 (org-export--get-inbuffer-options
1388 backend (cons file files))))))
1389 ((string= key "OPTIONS")
1390 (org-export--parse-option-keyword val backend))
1391 ((string= key "MACRO")
1392 (when (string-match
1393 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1394 val)
1395 (let ((key
1396 (intern
1397 (concat ":macro-"
1398 (downcase (match-string 1 val)))))
1399 (value (org-match-string-no-properties 2 val)))
1400 (cond
1401 ((not value) nil)
1402 ;; Value will be evaled: do not parse it.
1403 ((string-match "\\`(eval\\>" value)
1404 (list key (list value)))
1405 ;; Value has to be parsed for nested
1406 ;; macros.
1408 (list
1410 (let ((restr (org-element-restriction 'macro)))
1411 (org-element-parse-secondary-string
1412 ;; If user explicitly asks for
1413 ;; a newline, be sure to preserve it
1414 ;; from further filling with
1415 ;; `hard-newline'. Also replace
1416 ;; "\\n" with "\n", "\\\n" with "\\n"
1417 ;; and so on...
1418 (replace-regexp-in-string
1419 "\\(\\\\\\\\\\)n" "\\\\"
1420 (replace-regexp-in-string
1421 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1422 hard-newline value nil nil 1)
1423 nil nil 1)
1424 restr)))))))))))
1425 (setq plist (org-combine-plists plist prop)))))))
1426 ;; 2. Standard options, as in `org-export-options-alist'.
1427 (let* ((all (append org-export-options-alist
1428 ;; Also look for back-end specific options
1429 ;; if BACKEND is defined.
1430 (and backend
1431 (let ((var
1432 (intern
1433 (format "org-%s-options-alist" backend))))
1434 (and (boundp var) (eval var))))))
1435 ;; Build alist between keyword name and property name.
1436 (alist
1437 (delq nil (mapcar
1438 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1439 all)))
1440 ;; Build regexp matching all keywords associated to export
1441 ;; options. Note: the search is case insensitive.
1442 (opt-re (org-make-options-regexp
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 `org-element-parsed-keywords'.
1469 (mapc
1470 (lambda (key)
1471 (let* ((prop (cdr (assoc key alist)))
1472 (value (and prop (plist-get plist prop))))
1473 (when (stringp value)
1474 (setq plist
1475 (plist-put
1476 plist prop
1477 (org-element-parse-secondary-string
1478 value (org-element-restriction 'keyword)))))))
1479 org-element-parsed-keywords))
1480 ;; 3. Return final value.
1481 plist)))
1483 (defun org-export--get-buffer-attributes ()
1484 "Return properties related to buffer attributes, as a plist."
1485 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1486 (list
1487 ;; Store full path of input file name, or nil. For internal use.
1488 :input-file visited-file
1489 :title (or (and visited-file
1490 (file-name-sans-extension
1491 (file-name-nondirectory visited-file)))
1492 (buffer-name (buffer-base-buffer)))
1493 :footnote-definition-alist
1494 ;; Footnotes definitions must be collected in the original
1495 ;; buffer, as there's no insurance that they will still be in the
1496 ;; parse tree, due to possible narrowing.
1497 (let (alist)
1498 (org-with-wide-buffer
1499 (goto-char (point-min))
1500 (while (re-search-forward org-footnote-definition-re nil t)
1501 (let ((def (org-footnote-at-definition-p)))
1502 (when def
1503 (org-skip-whitespace)
1504 (push (cons (car def)
1505 (save-restriction
1506 (narrow-to-region (point) (nth 2 def))
1507 ;; Like `org-element-parse-buffer', but
1508 ;; makes sure the definition doesn't start
1509 ;; with a section element.
1510 (org-element--parse-elements
1511 (point-min) (point-max) nil nil nil nil
1512 (list 'org-data nil))))
1513 alist))))
1514 alist))
1515 :id-alist
1516 ;; Collect id references.
1517 (let (alist)
1518 (org-with-wide-buffer
1519 (goto-char (point-min))
1520 (while (re-search-forward
1521 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1522 (let* ((id (org-match-string-no-properties 1))
1523 (file (org-id-find-id-file id)))
1524 (when file (push (cons id (file-relative-name file)) alist)))))
1525 alist)
1526 :macro-modification-time
1527 (and visited-file
1528 (file-exists-p visited-file)
1529 (concat "(eval (format-time-string \"$1\" '"
1530 (prin1-to-string (nth 5 (file-attributes visited-file)))
1531 "))"))
1532 ;; Store input file name as a macro.
1533 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1534 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1535 ;; well be initialized as tree properties, since they don't
1536 ;; depend on buffer properties. Though, it may be more logical
1537 ;; to keep them close to other ":macro-" properties.
1538 :macro-date "(eval (format-time-string \"$1\"))"
1539 :macro-time "(eval (format-time-string \"$1\"))"
1540 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
1542 (defun org-export--get-global-options (&optional backend)
1543 "Return global export options as a plist.
1545 Optional argument BACKEND, if non-nil, is a symbol specifying
1546 which back-end specific export options should also be read in the
1547 process."
1548 (let ((all (append org-export-options-alist
1549 (and backend
1550 (let ((var (intern
1551 (format "org-%s-options-alist" backend))))
1552 (and (boundp var) (symbol-value var))))))
1553 ;; Output value.
1554 plist)
1555 (mapc
1556 (lambda (cell)
1557 (setq plist
1558 (plist-put
1559 plist
1560 (car cell)
1561 ;; Eval default value provided. If keyword is a member
1562 ;; of `org-element-parsed-keywords', parse it as
1563 ;; a secondary string before storing it.
1564 (let ((value (eval (nth 3 cell))))
1565 (if (not (stringp value)) value
1566 (let ((keyword (nth 1 cell)))
1567 (if (not (member keyword org-element-parsed-keywords)) value
1568 (org-element-parse-secondary-string
1569 value (org-element-restriction 'keyword)))))))))
1570 all)
1571 ;; Return value.
1572 plist))
1574 (defvar org-export--allow-BIND-local nil)
1575 (defun org-export--confirm-letbind ()
1576 "Can we use #+BIND values during export?
1577 By default this will ask for confirmation by the user, to divert
1578 possible security risks."
1579 (cond
1580 ((not org-export-allow-BIND) nil)
1581 ((eq org-export-allow-BIND t) t)
1582 ((local-variable-p 'org-export--allow-BIND-local)
1583 org-export--allow-BIND-local)
1584 (t (org-set-local 'org-export--allow-BIND-local
1585 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1587 (defun org-export--install-letbind-maybe ()
1588 "Install the values from #+BIND lines as local variables.
1589 Variables must be installed before in-buffer options are
1590 retrieved."
1591 (let (letbind pair)
1592 (org-with-wide-buffer
1593 (goto-char (point-min))
1594 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1595 (when (org-export-confirm-letbind)
1596 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1597 letbind))))
1598 (while (setq pair (pop letbind))
1599 (org-set-local (car pair) (nth 1 pair)))))
1602 ;;;; Tree Properties
1604 ;; Tree properties are infromation extracted from parse tree. They
1605 ;; are initialized at the beginning of the transcoding process by
1606 ;; `org-export-collect-tree-properties'.
1608 ;; Dedicated functions focus on computing the value of specific tree
1609 ;; properties during initialization. Thus,
1610 ;; `org-export--populate-ignore-list' lists elements and objects that
1611 ;; should be skipped during export, `org-export--get-min-level' gets
1612 ;; the minimal exportable level, used as a basis to compute relative
1613 ;; level for headlines. Eventually
1614 ;; `org-export--collect-headline-numbering' builds an alist between
1615 ;; headlines and their numbering.
1617 (defun org-export-collect-tree-properties (data info)
1618 "Extract tree properties from parse tree.
1620 DATA is the parse tree from which information is retrieved. INFO
1621 is a list holding export options.
1623 Following tree properties are set or updated:
1625 `:exported-data' Hash table used to memoize results from
1626 `org-export-data'.
1628 `:footnote-definition-alist' List of footnotes definitions in
1629 original buffer and current parse tree.
1631 `:headline-offset' Offset between true level of headlines and
1632 local level. An offset of -1 means an headline
1633 of level 2 should be considered as a level
1634 1 headline in the context.
1636 `:headline-numbering' Alist of all headlines as key an the
1637 associated numbering as value.
1639 `:ignore-list' List of elements that should be ignored during
1640 export.
1642 `:target-list' List of all targets in the parse tree.
1644 Return updated plist."
1645 ;; Install the parse tree in the communication channel, in order to
1646 ;; use `org-export-get-genealogy' and al.
1647 (setq info (plist-put info :parse-tree data))
1648 ;; Get the list of elements and objects to ignore, and put it into
1649 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1650 ;; been done during parse tree filtering.
1651 (setq info
1652 (plist-put info
1653 :ignore-list
1654 (append (org-export--populate-ignore-list data info)
1655 (plist-get info :ignore-list))))
1656 ;; Compute `:headline-offset' in order to be able to use
1657 ;; `org-export-get-relative-level'.
1658 (setq info
1659 (plist-put info
1660 :headline-offset
1661 (- 1 (org-export--get-min-level data info))))
1662 ;; Update footnotes definitions list with definitions in parse tree.
1663 ;; This is required since buffer expansion might have modified
1664 ;; boundaries of footnote definitions contained in the parse tree.
1665 ;; This way, definitions in `footnote-definition-alist' are bound to
1666 ;; match those in the parse tree.
1667 (let ((defs (plist-get info :footnote-definition-alist)))
1668 (org-element-map
1669 data 'footnote-definition
1670 (lambda (fn)
1671 (push (cons (org-element-property :label fn)
1672 `(org-data nil ,@(org-element-contents fn)))
1673 defs)))
1674 (setq info (plist-put info :footnote-definition-alist defs)))
1675 ;; Properties order doesn't matter: get the rest of the tree
1676 ;; properties.
1677 (nconc
1678 `(:target-list
1679 ,(org-element-map
1680 data '(keyword target)
1681 (lambda (blob)
1682 (when (or (eq (org-element-type blob) 'target)
1683 (string= (org-element-property :key blob) "TARGET"))
1684 blob)) info)
1685 :headline-numbering ,(org-export--collect-headline-numbering data info)
1686 :exported-data ,(make-hash-table :test 'eq :size 4001))
1687 info))
1689 (defun org-export--get-min-level (data options)
1690 "Return minimum exportable headline's level in DATA.
1691 DATA is parsed tree as returned by `org-element-parse-buffer'.
1692 OPTIONS is a plist holding export options."
1693 (catch 'exit
1694 (let ((min-level 10000))
1695 (mapc
1696 (lambda (blob)
1697 (when (and (eq (org-element-type blob) 'headline)
1698 (not (memq blob (plist-get options :ignore-list))))
1699 (setq min-level
1700 (min (org-element-property :level blob) min-level)))
1701 (when (= min-level 1) (throw 'exit 1)))
1702 (org-element-contents data))
1703 ;; If no headline was found, for the sake of consistency, set
1704 ;; minimum level to 1 nonetheless.
1705 (if (= min-level 10000) 1 min-level))))
1707 (defun org-export--collect-headline-numbering (data options)
1708 "Return numbering of all exportable headlines in a parse tree.
1710 DATA is the parse tree. OPTIONS is the plist holding export
1711 options.
1713 Return an alist whose key is an headline and value is its
1714 associated numbering \(in the shape of a list of numbers\)."
1715 (let ((numbering (make-vector org-export-max-depth 0)))
1716 (org-element-map
1717 data
1718 'headline
1719 (lambda (headline)
1720 (let ((relative-level
1721 (1- (org-export-get-relative-level headline options))))
1722 (cons
1723 headline
1724 (loop for n across numbering
1725 for idx from 0 to org-export-max-depth
1726 when (< idx relative-level) collect n
1727 when (= idx relative-level) collect (aset numbering idx (1+ n))
1728 when (> idx relative-level) do (aset numbering idx 0)))))
1729 options)))
1731 (defun org-export--populate-ignore-list (data options)
1732 "Return list of elements and objects to ignore during export.
1733 DATA is the parse tree to traverse. OPTIONS is the plist holding
1734 export options."
1735 (let* (ignore
1736 walk-data
1737 ;; First find trees containing a select tag, if any.
1738 (selected (org-export--selected-trees data options))
1739 (walk-data
1740 (lambda (data)
1741 ;; Collect ignored elements or objects into IGNORE-LIST.
1742 (let ((type (org-element-type data)))
1743 (if (org-export--skip-p data options selected) (push data ignore)
1744 (if (and (eq type 'headline)
1745 (eq (plist-get options :with-archived-trees) 'headline)
1746 (org-element-property :archivedp data))
1747 ;; If headline is archived but tree below has
1748 ;; to be skipped, add it to ignore list.
1749 (mapc (lambda (e) (push e ignore))
1750 (org-element-contents data))
1751 ;; Move into secondary string, if any.
1752 (let ((sec-prop
1753 (cdr (assq type org-element-secondary-value-alist))))
1754 (when sec-prop
1755 (mapc walk-data (org-element-property sec-prop data))))
1756 ;; Move into recursive objects/elements.
1757 (mapc walk-data (org-element-contents data))))))))
1758 ;; Main call.
1759 (funcall walk-data data)
1760 ;; Return value.
1761 ignore))
1763 (defun org-export--selected-trees (data info)
1764 "Return list of headlines containing a select tag in their tree.
1765 DATA is parsed data as returned by `org-element-parse-buffer'.
1766 INFO is a plist holding export options."
1767 (let* (selected-trees
1768 walk-data ; for byte-compiler.
1769 (walk-data
1770 (function
1771 (lambda (data genealogy)
1772 (case (org-element-type data)
1773 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1774 (org-element-contents data)))
1775 (headline
1776 (let ((tags (org-element-property :tags data)))
1777 (if (loop for tag in (plist-get info :select-tags)
1778 thereis (member tag tags))
1779 ;; When a select tag is found, mark full
1780 ;; genealogy and every headline within the tree
1781 ;; as acceptable.
1782 (setq selected-trees
1783 (append
1784 genealogy
1785 (org-element-map data 'headline 'identity)
1786 selected-trees))
1787 ;; Else, continue searching in tree, recursively.
1788 (mapc
1789 (lambda (el) (funcall walk-data el (cons data genealogy)))
1790 (org-element-contents data))))))))))
1791 (funcall walk-data data nil) selected-trees))
1793 (defun org-export--skip-p (blob options selected)
1794 "Non-nil when element or object BLOB should be skipped during export.
1795 OPTIONS is the plist holding export options. SELECTED, when
1796 non-nil, is a list of headlines belonging to a tree with a select
1797 tag."
1798 (case (org-element-type blob)
1799 ;; Check headline.
1800 (headline
1801 (let ((with-tasks (plist-get options :with-tasks))
1802 (todo (org-element-property :todo-keyword blob))
1803 (todo-type (org-element-property :todo-type blob))
1804 (archived (plist-get options :with-archived-trees))
1805 (tags (org-element-property :tags blob)))
1807 ;; Ignore subtrees with an exclude tag.
1808 (loop for k in (plist-get options :exclude-tags)
1809 thereis (member k tags))
1810 ;; When a select tag is present in the buffer, ignore any tree
1811 ;; without it.
1812 (and selected (not (memq blob selected)))
1813 ;; Ignore commented sub-trees.
1814 (org-element-property :commentedp blob)
1815 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1816 (and (not archived) (org-element-property :archivedp blob))
1817 ;; Ignore tasks, if specified by `:with-tasks' property.
1818 (and todo
1819 (or (not with-tasks)
1820 (and (memq with-tasks '(todo done))
1821 (not (eq todo-type with-tasks)))
1822 (and (consp with-tasks) (not (member todo with-tasks))))))))
1823 ;; Check inlinetask.
1824 (inlinetask (not (plist-get options :with-inlinetasks)))
1825 ;; Check timestamp.
1826 (timestamp
1827 (case (plist-get options :with-timestamps)
1828 ;; No timestamp allowed.
1829 ('nil t)
1830 ;; Only active timestamps allowed and the current one isn't
1831 ;; active.
1832 (active
1833 (not (memq (org-element-property :type blob)
1834 '(active active-range))))
1835 ;; Only inactive timestamps allowed and the current one isn't
1836 ;; inactive.
1837 (inactive
1838 (not (memq (org-element-property :type blob)
1839 '(inactive inactive-range))))))
1840 ;; Check drawer.
1841 (drawer
1842 (or (not (plist-get options :with-drawers))
1843 (and (consp (plist-get options :with-drawers))
1844 (not (member (org-element-property :drawer-name blob)
1845 (plist-get options :with-drawers))))))
1846 ;; Check table-row.
1847 (table-row (org-export-table-row-is-special-p blob options))
1848 ;; Check table-cell.
1849 (table-cell
1850 (and (org-export-table-has-special-column-p
1851 (org-export-get-parent-table blob))
1852 (not (org-export-get-previous-element blob options))))
1853 ;; Check clock.
1854 (clock (not (plist-get options :with-clocks)))
1855 ;; Check planning.
1856 (planning (not (plist-get options :with-plannings)))))
1860 ;;; The Transcoder
1862 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1863 ;; `org-element-parse-buffer') and transcodes it into a specified
1864 ;; back-end output. It takes care of filtering out elements or
1865 ;; objects according to export options and organizing the output blank
1866 ;; lines and white space are preserved. The function memoizes its
1867 ;; results, so it is cheap to call it within translators.
1869 ;; Internally, three functions handle the filtering of objects and
1870 ;; elements during the export. In particular,
1871 ;; `org-export-ignore-element' marks an element or object so future
1872 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1873 ;; elements or objects should be seen as real Org syntax and
1874 ;; `org-export-expand' transforms the others back into their original
1875 ;; shape
1877 ;; `org-export-transcoder' is an accessor returning appropriate
1878 ;; translator function for a given element or object.
1880 (defun org-export-transcoder (blob info)
1881 "Return appropriate transcoder for BLOB.
1882 INFO is a plist containing export directives."
1883 (let ((type (org-element-type blob)))
1884 ;; Return contents only for complete parse trees.
1885 (if (eq type 'org-data) (lambda (blob contents info) contents)
1886 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1887 (and (functionp transcoder) transcoder)))))
1889 (defun org-export-data (data info)
1890 "Convert DATA into current back-end format.
1892 DATA is a parse tree, an element or an object or a secondary
1893 string. INFO is a plist holding export options.
1895 Return transcoded string."
1896 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1897 (if (not (eq memo 'no-memo)) memo
1898 (let* ((type (org-element-type data))
1899 (results
1900 (cond
1901 ;; Ignored element/object.
1902 ((memq data (plist-get info :ignore-list)) nil)
1903 ;; Plain text.
1904 ((eq type 'plain-text)
1905 (org-export-filter-apply-functions
1906 (plist-get info :filter-plain-text)
1907 (let ((transcoder (org-export-transcoder data info)))
1908 (if transcoder (funcall transcoder data info) data))
1909 info))
1910 ;; Uninterpreted element/object: change it back to Org
1911 ;; syntax and export again resulting raw string.
1912 ((not (org-export--interpret-p data info))
1913 (org-export-data
1914 (org-export-expand
1915 data
1916 (mapconcat (lambda (blob) (org-export-data blob info))
1917 (org-element-contents data)
1918 ""))
1919 info))
1920 ;; Secondary string.
1921 ((not type)
1922 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1923 ;; Element/Object without contents or, as a special case,
1924 ;; headline with archive tag and archived trees restricted
1925 ;; to title only.
1926 ((or (not (org-element-contents data))
1927 (and (eq type 'headline)
1928 (eq (plist-get info :with-archived-trees) 'headline)
1929 (org-element-property :archivedp data)))
1930 (let ((transcoder (org-export-transcoder data info)))
1931 (and (functionp transcoder)
1932 (funcall transcoder data nil info))))
1933 ;; Element/Object with contents.
1935 (let ((transcoder (org-export-transcoder data info)))
1936 (when transcoder
1937 (let* ((greaterp (memq type org-element-greater-elements))
1938 (objectp
1939 (and (not greaterp)
1940 (memq type org-element-recursive-objects)))
1941 (contents
1942 (mapconcat
1943 (lambda (element) (org-export-data element info))
1944 (org-element-contents
1945 (if (or greaterp objectp) data
1946 ;; Elements directly containing objects
1947 ;; must have their indentation normalized
1948 ;; first.
1949 (org-element-normalize-contents
1950 data
1951 ;; When normalizing contents of the first
1952 ;; paragraph in an item or a footnote
1953 ;; definition, ignore first line's
1954 ;; indentation: there is none and it
1955 ;; might be misleading.
1956 (when (eq type 'paragraph)
1957 (let ((parent (org-export-get-parent data)))
1958 (and
1959 (eq (car (org-element-contents parent))
1960 data)
1961 (memq (org-element-type parent)
1962 '(footnote-definition item))))))))
1963 "")))
1964 (funcall transcoder data
1965 (if (not greaterp) contents
1966 (org-element-normalize-string contents))
1967 info))))))))
1968 ;; Final result will be memoized before being returned.
1969 (puthash
1970 data
1971 (cond
1972 ((not results) nil)
1973 ((memq type '(org-data plain-text nil)) results)
1974 ;; Append the same white space between elements or objects as in
1975 ;; the original buffer, and call appropriate filters.
1977 (let ((results
1978 (org-export-filter-apply-functions
1979 (plist-get info (intern (format ":filter-%s" type)))
1980 (let ((post-blank (or (org-element-property :post-blank data)
1981 0)))
1982 (if (memq type org-element-all-elements)
1983 (concat (org-element-normalize-string results)
1984 (make-string post-blank ?\n))
1985 (concat results (make-string post-blank ? ))))
1986 info)))
1987 results)))
1988 (plist-get info :exported-data))))))
1990 (defun org-export--interpret-p (blob info)
1991 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1992 Check is done according to export options INFO, stored as
1993 a plist."
1994 (case (org-element-type blob)
1995 ;; ... entities...
1996 (entity (plist-get info :with-entities))
1997 ;; ... emphasis...
1998 (emphasis (plist-get info :with-emphasize))
1999 ;; ... fixed-width areas.
2000 (fixed-width (plist-get info :with-fixed-width))
2001 ;; ... footnotes...
2002 ((footnote-definition footnote-reference)
2003 (plist-get info :with-footnotes))
2004 ;; ... sub/superscripts...
2005 ((subscript superscript)
2006 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2007 (if (eq sub/super-p '{})
2008 (org-element-property :use-brackets-p blob)
2009 sub/super-p)))
2010 ;; ... tables...
2011 (table (plist-get info :with-tables))
2012 (otherwise t)))
2014 (defun org-export-expand (blob contents)
2015 "Expand a parsed element or object to its original state.
2016 BLOB is either an element or an object. CONTENTS is its
2017 contents, as a string or nil."
2018 (funcall
2019 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2020 blob contents))
2022 (defun org-export-ignore-element (element info)
2023 "Add ELEMENT to `:ignore-list' in INFO.
2025 Any element in `:ignore-list' will be skipped when using
2026 `org-element-map'. INFO is modified by side effects."
2027 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2031 ;;; The Filter System
2033 ;; Filters allow end-users to tweak easily the transcoded output.
2034 ;; They are the functional counterpart of hooks, as every filter in
2035 ;; a set is applied to the return value of the previous one.
2037 ;; Every set is back-end agnostic. Although, a filter is always
2038 ;; called, in addition to the string it applies to, with the back-end
2039 ;; used as argument, so it's easy for the end-user to add back-end
2040 ;; specific filters in the set. The communication channel, as
2041 ;; a plist, is required as the third argument.
2043 ;; From the developer side, filters sets can be installed in the
2044 ;; process with the help of `org-export-define-backend', which
2045 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2046 ;; association has a key among the following symbols and a function or
2047 ;; a list of functions as value.
2049 ;; - `:filter-parse-tree' applies directly on the complete parsed
2050 ;; tree. It's the only filters set that doesn't apply to a string.
2051 ;; Users can set it through `org-export-filter-parse-tree-functions'
2052 ;; variable.
2054 ;; - `:filter-final-output' applies to the final transcoded string.
2055 ;; Users can set it with `org-export-filter-final-output-functions'
2056 ;; variable
2058 ;; - `:filter-plain-text' applies to any string not recognized as Org
2059 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2060 ;; configure it.
2062 ;; - `:filter-TYPE' applies on the string returned after an element or
2063 ;; object of type TYPE has been transcoded. An user can modify
2064 ;; `org-export-filter-TYPE-functions'
2066 ;; All filters sets are applied with
2067 ;; `org-export-filter-apply-functions' function. Filters in a set are
2068 ;; applied in a LIFO fashion. It allows developers to be sure that
2069 ;; their filters will be applied first.
2071 ;; Filters properties are installed in communication channel with
2072 ;; `org-export-install-filters' function.
2074 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2075 ;; before parsing to allow for heavy structure modifications.
2078 ;;;; Before Parsing Hook
2080 (defvar org-export-before-parsing-hook nil
2081 "Hook run before parsing an export buffer.
2083 This is run after include keywords have been expanded and Babel
2084 code executed, on a copy of original buffer's area being
2085 exported. Visibility is the same as in the original one. Point
2086 is left at the beginning of the new one.
2088 Every function in this hook will be called with one argument: the
2089 back-end currently used, as a symbol.")
2092 ;;;; Special Filters
2094 (defvar org-export-filter-parse-tree-functions nil
2095 "List of functions applied to the parsed tree.
2096 Each filter is called with three arguments: the parse tree, as
2097 returned by `org-element-parse-buffer', the back-end, as
2098 a symbol, and the communication channel, as a plist. It must
2099 return the modified parse tree to transcode.")
2101 (defvar org-export-filter-final-output-functions nil
2102 "List of functions applied to the transcoded string.
2103 Each filter is called with three arguments: the full transcoded
2104 string, the back-end, as a symbol, and the communication channel,
2105 as a plist. It must return a string that will be used as the
2106 final export output.")
2108 (defvar org-export-filter-plain-text-functions nil
2109 "List of functions applied to plain text.
2110 Each filter is called with three arguments: a string which
2111 contains no Org syntax, the back-end, as a symbol, and the
2112 communication channel, as a plist. It must return a string or
2113 nil.")
2116 ;;;; Elements Filters
2118 (defvar org-export-filter-center-block-functions nil
2119 "List of functions applied to a transcoded center block.
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-clock-functions nil
2125 "List of functions applied to a transcoded clock.
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-headline-functions nil
2143 "List of functions applied to a transcoded headline.
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-inlinetask-functions nil
2149 "List of functions applied to a transcoded inlinetask.
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-plain-list-functions nil
2155 "List of functions applied to a transcoded plain-list.
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-item-functions nil
2161 "List of functions applied to a transcoded item.
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-comment-functions nil
2167 "List of functions applied to a transcoded comment.
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-comment-block-functions nil
2173 "List of functions applied to a transcoded comment-comment.
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-example-block-functions nil
2179 "List of functions applied to a transcoded example-block.
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-export-block-functions nil
2185 "List of functions applied to a transcoded export-block.
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-fixed-width-functions nil
2191 "List of functions applied to a transcoded fixed-width.
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-footnote-definition-functions nil
2197 "List of functions applied to a transcoded footnote-definition.
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-horizontal-rule-functions nil
2203 "List of functions applied to a transcoded horizontal-rule.
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-keyword-functions nil
2209 "List of functions applied to a transcoded keyword.
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-latex-environment-functions nil
2215 "List of functions applied to a transcoded latex-environment.
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-babel-call-functions nil
2221 "List of functions applied to a transcoded babel-call.
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-paragraph-functions nil
2227 "List of functions applied to a transcoded paragraph.
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-planning-functions nil
2233 "List of functions applied to a transcoded planning.
2234 Each filter is called with three arguments: the transcoded data,
2235 as a string, the back-end, as a symbol, and the communication
2236 channel, as a plist. It must return a string or nil.")
2238 (defvar org-export-filter-property-drawer-functions nil
2239 "List of functions applied to a transcoded property-drawer.
2240 Each filter is called with three arguments: the transcoded data,
2241 as a string, the back-end, as a symbol, and the communication
2242 channel, as a plist. It must return a string or nil.")
2244 (defvar org-export-filter-quote-block-functions nil
2245 "List of functions applied to a transcoded quote block.
2246 Each filter is called with three arguments: the transcoded quote
2247 data, as a string, the back-end, as a symbol, and the
2248 communication channel, as a plist. It must return a string or
2249 nil.")
2251 (defvar org-export-filter-quote-section-functions nil
2252 "List of functions applied to a transcoded quote-section.
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-section-functions nil
2258 "List of functions applied to a transcoded section.
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-special-block-functions nil
2264 "List of functions applied to a transcoded special block.
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-src-block-functions nil
2270 "List of functions applied to a transcoded src-block.
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-functions nil
2276 "List of functions applied to a transcoded table.
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-table-cell-functions nil
2282 "List of functions applied to a transcoded table-cell.
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.")
2287 (defvar org-export-filter-table-row-functions nil
2288 "List of functions applied to a transcoded table-row.
2289 Each filter is called with three arguments: the transcoded data,
2290 as a string, the back-end, as a symbol, and the communication
2291 channel, as a plist. It must return a string or nil.")
2293 (defvar org-export-filter-verse-block-functions nil
2294 "List of functions applied to a transcoded verse block.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2300 ;;;; Objects Filters
2302 (defvar org-export-filter-bold-functions nil
2303 "List of functions applied to transcoded bold text.
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-code-functions nil
2309 "List of functions applied to transcoded code text.
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-entity-functions nil
2315 "List of functions applied to a transcoded entity.
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-export-snippet-functions nil
2321 "List of functions applied to a transcoded export-snippet.
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-footnote-reference-functions nil
2327 "List of functions applied to a transcoded footnote-reference.
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-inline-babel-call-functions nil
2333 "List of functions applied to a transcoded inline-babel-call.
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-inline-src-block-functions nil
2339 "List of functions applied to a transcoded inline-src-block.
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-italic-functions nil
2345 "List of functions applied to transcoded italic text.
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-latex-fragment-functions nil
2351 "List of functions applied to a transcoded latex-fragment.
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-line-break-functions nil
2357 "List of functions applied to a transcoded line-break.
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-link-functions nil
2363 "List of functions applied to a transcoded link.
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-macro-functions nil
2369 "List of functions applied to a transcoded macro.
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-radio-target-functions nil
2375 "List of functions applied to a transcoded radio-target.
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-statistics-cookie-functions nil
2381 "List of functions applied to a transcoded statistics-cookie.
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-strike-through-functions nil
2387 "List of functions applied to transcoded strike-through text.
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-subscript-functions nil
2393 "List of functions applied to a transcoded subscript.
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-superscript-functions nil
2399 "List of functions applied to a transcoded superscript.
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-target-functions nil
2405 "List of functions applied to a transcoded target.
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-timestamp-functions nil
2411 "List of functions applied to a transcoded timestamp.
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.")
2416 (defvar org-export-filter-underline-functions nil
2417 "List of functions applied to transcoded underline text.
2418 Each filter is called with three arguments: the transcoded data,
2419 as a string, the back-end, as a symbol, and the communication
2420 channel, as a plist. It must return a string or nil.")
2422 (defvar org-export-filter-verbatim-functions nil
2423 "List of functions applied to transcoded verbatim text.
2424 Each filter is called with three arguments: the transcoded data,
2425 as a string, the back-end, as a symbol, and the communication
2426 channel, as a plist. It must return a string or nil.")
2429 ;;;; Filters Tools
2431 ;; Internal function `org-export-install-filters' installs filters
2432 ;; hard-coded in back-ends (developer filters) and filters from global
2433 ;; variables (user filters) in the communication channel.
2435 ;; Internal function `org-export-filter-apply-functions' takes care
2436 ;; about applying each filter in order to a given data. It ignores
2437 ;; filters returning a nil value but stops whenever a filter returns
2438 ;; an empty string.
2440 (defun org-export-filter-apply-functions (filters value info)
2441 "Call every function in FILTERS.
2443 Functions are called with arguments VALUE, current export
2444 back-end and INFO. A function returning a nil value will be
2445 skipped. If it returns the empty string, the process ends and
2446 VALUE is ignored.
2448 Call is done in a LIFO fashion, to be sure that developer
2449 specified filters, if any, are called first."
2450 (catch 'exit
2451 (dolist (filter filters value)
2452 (let ((result (funcall filter value (plist-get info :back-end) info)))
2453 (cond ((not result) value)
2454 ((equal value "") (throw 'exit nil))
2455 (t (setq value result)))))))
2457 (defun org-export-install-filters (info)
2458 "Install filters properties in communication channel.
2460 INFO is a plist containing the current communication channel.
2462 Return the updated communication channel."
2463 (let (plist)
2464 ;; Install user defined filters with `org-export-filters-alist'.
2465 (mapc (lambda (p)
2466 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2467 org-export-filters-alist)
2468 ;; Prepend back-end specific filters to that list.
2469 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2470 (plist-get info :back-end)))))
2471 (when (boundp back-end-filters)
2472 (mapc (lambda (p)
2473 ;; Single values get consed, lists are prepended.
2474 (let ((key (car p)) (value (cdr p)))
2475 (when value
2476 (setq plist
2477 (plist-put
2478 plist key
2479 (if (atom value) (cons value (plist-get plist key))
2480 (append value (plist-get plist key))))))))
2481 (eval back-end-filters))))
2482 ;; Return new communication channel.
2483 (org-combine-plists info plist)))
2487 ;;; Core functions
2489 ;; This is the room for the main function, `org-export-as', along with
2490 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2491 ;; They differ only by the way they output the resulting code.
2493 ;; `org-export-output-file-name' is an auxiliary function meant to be
2494 ;; used with `org-export-to-file'. With a given extension, it tries
2495 ;; to provide a canonical file name to write export output to.
2497 ;; Note that `org-export-as' doesn't really parse the current buffer,
2498 ;; but a copy of it (with the same buffer-local variables and
2499 ;; visibility), where include keywords are expanded and Babel blocks
2500 ;; are executed, if appropriate.
2501 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2503 ;; File inclusion is taken care of by
2504 ;; `org-export-expand-include-keyword' and
2505 ;; `org-export--prepare-file-contents'. Structure wise, including
2506 ;; a whole Org file in a buffer often makes little sense. For
2507 ;; example, if the file contains an headline and the include keyword
2508 ;; was within an item, the item should contain the headline. That's
2509 ;; why file inclusion should be done before any structure can be
2510 ;; associated to the file, that is before parsing.
2512 (defun org-export-as
2513 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2514 "Transcode current Org buffer into BACKEND code.
2516 If narrowing is active in the current buffer, only transcode its
2517 narrowed part.
2519 If a region is active, transcode that region.
2521 When optional argument SUBTREEP is non-nil, transcode the
2522 sub-tree at point, extracting information from the headline
2523 properties first.
2525 When optional argument VISIBLE-ONLY is non-nil, don't export
2526 contents of hidden elements.
2528 When optional argument BODY-ONLY is non-nil, only return body
2529 code, without preamble nor postamble.
2531 Optional argument EXT-PLIST, when provided, is a property list
2532 with external parameters overriding Org default settings, but
2533 still inferior to file-local settings.
2535 Optional argument NOEXPAND, when non-nil, prevents included files
2536 to be expanded and Babel code to be executed.
2538 Return code as a string."
2539 (save-excursion
2540 (save-restriction
2541 ;; Narrow buffer to an appropriate region or subtree for
2542 ;; parsing. If parsing subtree, be sure to remove main headline
2543 ;; too.
2544 (cond ((org-region-active-p)
2545 (narrow-to-region (region-beginning) (region-end)))
2546 (subtreep
2547 (org-narrow-to-subtree)
2548 (goto-char (point-min))
2549 (forward-line)
2550 (narrow-to-region (point) (point-max))))
2551 ;; 1. Get export environment from original buffer. Also install
2552 ;; user's and developer's filters.
2553 (let ((info (org-export-install-filters
2554 (org-export-get-environment backend subtreep ext-plist)))
2555 ;; 2. Get parse tree. Buffer isn't parsed directly.
2556 ;; Instead, a temporary copy is created, where include
2557 ;; keywords are expanded and code blocks are evaluated.
2558 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2559 (current-buffer))))
2560 (org-export-with-current-buffer-copy
2561 (unless noexpand
2562 (org-export-expand-include-keyword)
2563 ;; TODO: Setting `org-current-export-file' is
2564 ;; required by Org Babel to properly resolve
2565 ;; noweb references. Once "org-exp.el" is
2566 ;; removed, modify
2567 ;; `org-export-blocks-preprocess' so it accepts
2568 ;; the value as an argument instead.
2569 (let ((org-current-export-file buf))
2570 (org-export-blocks-preprocess)))
2571 (goto-char (point-min))
2572 ;; Run hook
2573 ;; `org-export-before-parsing-hook'. with current
2574 ;; back-end as argument.
2575 (run-hook-with-args
2576 'org-export-before-parsing-hook backend)
2577 ;; Eventually parse buffer.
2578 (org-element-parse-buffer nil visible-only)))))
2579 ;; 3. Call parse-tree filters to get the final tree.
2580 (setq tree
2581 (org-export-filter-apply-functions
2582 (plist-get info :filter-parse-tree) tree info))
2583 ;; 4. Now tree is complete, compute its properties and add
2584 ;; them to communication channel.
2585 (setq info
2586 (org-combine-plists
2587 info (org-export-collect-tree-properties tree info)))
2588 ;; 5. Eventually transcode TREE. Wrap the resulting string
2589 ;; into a template, if required. Eventually call
2590 ;; final-output filter.
2591 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2592 (template (cdr (assq 'template
2593 (plist-get info :translate-alist))))
2594 (output (org-export-filter-apply-functions
2595 (plist-get info :filter-final-output)
2596 (if (or (not (functionp template)) body-only) body
2597 (funcall template body info))
2598 info)))
2599 ;; Maybe add final OUTPUT to kill ring, then return it.
2600 (when org-export-copy-to-kill-ring (org-kill-new output))
2601 output)))))
2603 (defun org-export-to-buffer
2604 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2605 "Call `org-export-as' with output to a specified buffer.
2607 BACKEND is the back-end used for transcoding, as a symbol.
2609 BUFFER is the output buffer. If it already exists, it will be
2610 erased first, otherwise, it will be created.
2612 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2613 and NOEXPAND are similar to those used in `org-export-as', which
2614 see.
2616 Return buffer."
2617 (let ((out (org-export-as
2618 backend subtreep visible-only body-only ext-plist noexpand))
2619 (buffer (get-buffer-create buffer)))
2620 (with-current-buffer buffer
2621 (erase-buffer)
2622 (insert out)
2623 (goto-char (point-min)))
2624 buffer))
2626 (defun org-export-to-file
2627 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2628 "Call `org-export-as' with output to a specified file.
2630 BACKEND is the back-end used for transcoding, as a symbol. FILE
2631 is the name of the output file, as a string.
2633 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2634 and NOEXPAND are similar to those used in `org-export-as', which
2635 see.
2637 Return output file's name."
2638 ;; Checks for FILE permissions. `write-file' would do the same, but
2639 ;; we'd rather avoid needless transcoding of parse tree.
2640 (unless (file-writable-p file) (error "Output file not writable"))
2641 ;; Insert contents to a temporary buffer and write it to FILE.
2642 (let ((out (org-export-as
2643 backend subtreep visible-only body-only ext-plist noexpand)))
2644 (with-temp-buffer
2645 (insert out)
2646 (let ((coding-system-for-write org-export-coding-system))
2647 (write-file file))))
2648 ;; Return full path.
2649 file)
2651 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2652 "Return output file's name according to buffer specifications.
2654 EXTENSION is a string representing the output file extension,
2655 with the leading dot.
2657 With a non-nil optional argument SUBTREEP, try to determine
2658 output file's name by looking for \"EXPORT_FILE_NAME\" property
2659 of subtree at point.
2661 When optional argument PUB-DIR is set, use it as the publishing
2662 directory.
2664 When optional argument VISIBLE-ONLY is non-nil, don't export
2665 contents of hidden elements.
2667 Return file name as a string, or nil if it couldn't be
2668 determined."
2669 (let ((base-name
2670 ;; File name may come from EXPORT_FILE_NAME subtree property,
2671 ;; assuming point is at beginning of said sub-tree.
2672 (file-name-sans-extension
2673 (or (and subtreep
2674 (org-entry-get
2675 (save-excursion
2676 (ignore-errors (org-back-to-heading) (point)))
2677 "EXPORT_FILE_NAME" t))
2678 ;; File name may be extracted from buffer's associated
2679 ;; file, if any.
2680 (buffer-file-name (buffer-base-buffer))
2681 ;; Can't determine file name on our own: Ask user.
2682 (let ((read-file-name-function
2683 (and org-completion-use-ido 'ido-read-file-name)))
2684 (read-file-name
2685 "Output file: " pub-dir nil nil nil
2686 (lambda (name)
2687 (string= (file-name-extension name t) extension))))))))
2688 ;; Build file name. Enforce EXTENSION over whatever user may have
2689 ;; come up with. PUB-DIR, if defined, always has precedence over
2690 ;; any provided path.
2691 (cond
2692 (pub-dir
2693 (concat (file-name-as-directory pub-dir)
2694 (file-name-nondirectory base-name)
2695 extension))
2696 ((string= (file-name-nondirectory base-name) base-name)
2697 (concat (file-name-as-directory ".") base-name extension))
2698 (t (concat base-name extension)))))
2700 (defmacro org-export-with-current-buffer-copy (&rest body)
2701 "Apply BODY in a copy of the current buffer.
2703 The copy preserves local variables and visibility of the original
2704 buffer.
2706 Point is at buffer's beginning when BODY is applied."
2707 (org-with-gensyms (original-buffer offset buffer-string overlays)
2708 `(let ((,original-buffer (current-buffer))
2709 (,offset (1- (point-min)))
2710 (,buffer-string (buffer-string))
2711 (,overlays (mapcar
2712 'copy-overlay (overlays-in (point-min) (point-max)))))
2713 (with-temp-buffer
2714 (let ((buffer-invisibility-spec nil))
2715 (org-clone-local-variables
2716 ,original-buffer
2717 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2718 (insert ,buffer-string)
2719 (mapc (lambda (ov)
2720 (move-overlay
2722 (- (overlay-start ov) ,offset)
2723 (- (overlay-end ov) ,offset)
2724 (current-buffer)))
2725 ,overlays)
2726 (goto-char (point-min))
2727 (progn ,@body))))))
2728 (def-edebug-spec org-export-with-current-buffer-copy (body))
2730 (defun org-export-expand-include-keyword (&optional included dir)
2731 "Expand every include keyword in buffer.
2732 Optional argument INCLUDED is a list of included file names along
2733 with their line restriction, when appropriate. It is used to
2734 avoid infinite recursion. Optional argument DIR is the current
2735 working directory. It is used to properly resolve relative
2736 paths."
2737 (let ((case-fold-search t))
2738 (goto-char (point-min))
2739 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2740 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2741 'keyword)
2742 (beginning-of-line)
2743 ;; Extract arguments from keyword's value.
2744 (let* ((value (match-string 1))
2745 (ind (org-get-indentation))
2746 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2747 (prog1 (expand-file-name (match-string 1 value) dir)
2748 (setq value (replace-match "" nil nil value)))))
2749 (lines
2750 (and (string-match
2751 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2752 (prog1 (match-string 1 value)
2753 (setq value (replace-match "" nil nil value)))))
2754 (env (cond ((string-match "\\<example\\>" value) 'example)
2755 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2756 (match-string 1 value))))
2757 ;; Minimal level of included file defaults to the child
2758 ;; level of the current headline, if any, or one. It
2759 ;; only applies is the file is meant to be included as
2760 ;; an Org one.
2761 (minlevel
2762 (and (not env)
2763 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2764 (prog1 (string-to-number (match-string 1 value))
2765 (setq value (replace-match "" nil nil value)))
2766 (let ((cur (org-current-level)))
2767 (if cur (1+ (org-reduced-level cur)) 1))))))
2768 ;; Remove keyword.
2769 (delete-region (point) (progn (forward-line) (point)))
2770 (cond
2771 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2772 ;; Check if files has already been parsed. Look after
2773 ;; inclusion lines too, as different parts of the same file
2774 ;; can be included too.
2775 ((member (list file lines) included)
2776 (error "Recursive file inclusion: %s" file))
2778 (cond
2779 ((eq env 'example)
2780 (insert
2781 (let ((ind-str (make-string ind ? ))
2782 (contents
2783 ;; Protect sensitive contents with commas.
2784 (replace-regexp-in-string
2785 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2786 (org-export--prepare-file-contents file lines)
2787 nil nil 1)))
2788 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2789 ind-str contents ind-str))))
2790 ((stringp env)
2791 (insert
2792 (let ((ind-str (make-string ind ? ))
2793 (contents
2794 ;; Protect sensitive contents with commas.
2795 (replace-regexp-in-string
2796 (if (string= env "org") "\\(^\\)\\(.\\)"
2797 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2798 (org-export--prepare-file-contents file lines)
2799 nil nil 1)))
2800 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2801 ind-str env contents ind-str))))
2803 (insert
2804 (with-temp-buffer
2805 (org-mode)
2806 (insert
2807 (org-export--prepare-file-contents file lines ind minlevel))
2808 (org-export-expand-include-keyword
2809 (cons (list file lines) included)
2810 (file-name-directory file))
2811 (buffer-string))))))))))))
2813 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2814 "Prepare the contents of FILE for inclusion and return them as a string.
2816 When optional argument LINES is a string specifying a range of
2817 lines, include only those lines.
2819 Optional argument IND, when non-nil, is an integer specifying the
2820 global indentation of returned contents. Since its purpose is to
2821 allow an included file to stay in the same environment it was
2822 created \(i.e. a list item), it doesn't apply past the first
2823 headline encountered.
2825 Optional argument MINLEVEL, when non-nil, is an integer
2826 specifying the level that any top-level headline in the included
2827 file should have."
2828 (with-temp-buffer
2829 (insert-file-contents file)
2830 (when lines
2831 (let* ((lines (split-string lines "-"))
2832 (lbeg (string-to-number (car lines)))
2833 (lend (string-to-number (cadr lines)))
2834 (beg (if (zerop lbeg) (point-min)
2835 (goto-char (point-min))
2836 (forward-line (1- lbeg))
2837 (point)))
2838 (end (if (zerop lend) (point-max)
2839 (goto-char (point-min))
2840 (forward-line (1- lend))
2841 (point))))
2842 (narrow-to-region beg end)))
2843 ;; Remove blank lines at beginning and end of contents. The logic
2844 ;; behind that removal is that blank lines around include keyword
2845 ;; override blank lines in included file.
2846 (goto-char (point-min))
2847 (org-skip-whitespace)
2848 (beginning-of-line)
2849 (delete-region (point-min) (point))
2850 (goto-char (point-max))
2851 (skip-chars-backward " \r\t\n")
2852 (forward-line)
2853 (delete-region (point) (point-max))
2854 ;; If IND is set, preserve indentation of include keyword until
2855 ;; the first headline encountered.
2856 (when ind
2857 (unless (eq major-mode 'org-mode) (org-mode))
2858 (goto-char (point-min))
2859 (let ((ind-str (make-string ind ? )))
2860 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2861 ;; Do not move footnote definitions out of column 0.
2862 (unless (and (looking-at org-footnote-definition-re)
2863 (eq (org-element-type (org-element-at-point))
2864 'footnote-definition))
2865 (insert ind-str))
2866 (forward-line))))
2867 ;; When MINLEVEL is specified, compute minimal level for headlines
2868 ;; in the file (CUR-MIN), and remove stars to each headline so
2869 ;; that headlines with minimal level have a level of MINLEVEL.
2870 (when minlevel
2871 (unless (eq major-mode 'org-mode) (org-mode))
2872 (let ((levels (org-map-entries
2873 (lambda () (org-reduced-level (org-current-level))))))
2874 (when levels
2875 (let ((offset (- minlevel (apply 'min levels))))
2876 (unless (zerop offset)
2877 (when org-odd-levels-only (setq offset (* offset 2)))
2878 ;; Only change stars, don't bother moving whole
2879 ;; sections.
2880 (org-map-entries
2881 (lambda () (if (< offset 0) (delete-char (abs offset))
2882 (insert (make-string offset ?*))))))))))
2883 (buffer-string)))
2886 ;;; Tools For Back-Ends
2888 ;; A whole set of tools is available to help build new exporters. Any
2889 ;; function general enough to have its use across many back-ends
2890 ;; should be added here.
2892 ;; As of now, functions operating on footnotes, headlines, links,
2893 ;; macros, references, src-blocks, tables and tables of contents are
2894 ;; implemented.
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 (defun org-export-read-attribute (attribute element &optional property)
2903 "Turn ATTRIBUTE property from ELEMENT into a plist.
2905 When optional argument PROPERTY is non-nil, return the value of
2906 that property within attributes.
2908 This function assumes attributes are defined as \":keyword
2909 value\" pairs. It is appropriate for `:attr_html' like
2910 properties."
2911 (let ((attributes
2912 (let ((value (org-element-property attribute element)))
2913 (and value
2914 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2915 (if property (plist-get attributes property) attributes)))
2918 ;;;; For Export Snippets
2920 ;; Every export snippet is transmitted to the back-end. Though, the
2921 ;; latter will only retain one type of export-snippet, ignoring
2922 ;; others, based on the former's target back-end. The function
2923 ;; `org-export-snippet-backend' returns that back-end for a given
2924 ;; export-snippet.
2926 (defun org-export-snippet-backend (export-snippet)
2927 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2928 Translation, with `org-export-snippet-translation-alist', is
2929 applied."
2930 (let ((back-end (org-element-property :back-end export-snippet)))
2931 (intern
2932 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2933 back-end))))
2936 ;;;; For Footnotes
2938 ;; `org-export-collect-footnote-definitions' is a tool to list
2939 ;; actually used footnotes definitions in the whole parse tree, or in
2940 ;; an headline, in order to add footnote listings throughout the
2941 ;; transcoded data.
2943 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2944 ;; back-ends, when they need to attach the footnote definition only to
2945 ;; the first occurrence of the corresponding label.
2947 ;; `org-export-get-footnote-definition' and
2948 ;; `org-export-get-footnote-number' provide easier access to
2949 ;; additional information relative to a footnote reference.
2951 (defun org-export-collect-footnote-definitions (data info)
2952 "Return an alist between footnote numbers, labels and definitions.
2954 DATA is the parse tree from which definitions are collected.
2955 INFO is the plist used as a communication channel.
2957 Definitions are sorted by order of references. They either
2958 appear as Org data or as a secondary string for inlined
2959 footnotes. Unreferenced definitions are ignored."
2960 (let* (num-alist
2961 collect-fn ; for byte-compiler.
2962 (collect-fn
2963 (function
2964 (lambda (data)
2965 ;; Collect footnote number, label and definition in DATA.
2966 (org-element-map
2967 data 'footnote-reference
2968 (lambda (fn)
2969 (when (org-export-footnote-first-reference-p fn info)
2970 (let ((def (org-export-get-footnote-definition fn info)))
2971 (push
2972 (list (org-export-get-footnote-number fn info)
2973 (org-element-property :label fn)
2974 def)
2975 num-alist)
2976 ;; Also search in definition for nested footnotes.
2977 (when (eq (org-element-property :type fn) 'standard)
2978 (funcall collect-fn def)))))
2979 ;; Don't enter footnote definitions since it will happen
2980 ;; when their first reference is found.
2981 info nil 'footnote-definition)))))
2982 (funcall collect-fn (plist-get info :parse-tree))
2983 (reverse num-alist)))
2985 (defun org-export-footnote-first-reference-p (footnote-reference info)
2986 "Non-nil when a footnote reference is the first one for its label.
2988 FOOTNOTE-REFERENCE is the footnote reference being considered.
2989 INFO is the plist used as a communication channel."
2990 (let ((label (org-element-property :label footnote-reference)))
2991 ;; Anonymous footnotes are always a first reference.
2992 (if (not label) t
2993 ;; Otherwise, return the first footnote with the same LABEL and
2994 ;; test if it is equal to FOOTNOTE-REFERENCE.
2995 (let* (search-refs ; for byte-compiler.
2996 (search-refs
2997 (function
2998 (lambda (data)
2999 (org-element-map
3000 data 'footnote-reference
3001 (lambda (fn)
3002 (cond
3003 ((string= (org-element-property :label fn) label)
3004 (throw 'exit fn))
3005 ;; If FN isn't inlined, be sure to traverse its
3006 ;; definition before resuming search. See
3007 ;; comments in `org-export-get-footnote-number'
3008 ;; for more information.
3009 ((eq (org-element-property :type fn) 'standard)
3010 (funcall search-refs
3011 (org-export-get-footnote-definition fn info)))))
3012 ;; Don't enter footnote definitions since it will
3013 ;; happen when their first reference is found.
3014 info 'first-match 'footnote-definition)))))
3015 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3016 footnote-reference)))))
3018 (defun org-export-get-footnote-definition (footnote-reference info)
3019 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3020 INFO is the plist used as a communication channel."
3021 (let ((label (org-element-property :label footnote-reference)))
3022 (or (org-element-property :inline-definition footnote-reference)
3023 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3025 (defun org-export-get-footnote-number (footnote info)
3026 "Return number associated to a footnote.
3028 FOOTNOTE is either a footnote reference or a footnote definition.
3029 INFO is the plist used as a communication channel."
3030 (let* ((label (org-element-property :label footnote))
3031 seen-refs
3032 search-ref ; For byte-compiler.
3033 (search-ref
3034 (function
3035 (lambda (data)
3036 ;; Search footnote references through DATA, filling
3037 ;; SEEN-REFS along the way.
3038 (org-element-map
3039 data 'footnote-reference
3040 (lambda (fn)
3041 (let ((fn-lbl (org-element-property :label fn)))
3042 (cond
3043 ;; Anonymous footnote match: return number.
3044 ((and (not fn-lbl) (eq fn footnote))
3045 (throw 'exit (1+ (length seen-refs))))
3046 ;; Labels match: return number.
3047 ((and label (string= label fn-lbl))
3048 (throw 'exit (1+ (length seen-refs))))
3049 ;; Anonymous footnote: it's always a new one. Also,
3050 ;; be sure to return nil from the `cond' so
3051 ;; `first-match' doesn't get us out of the loop.
3052 ((not fn-lbl) (push 'inline seen-refs) nil)
3053 ;; Label not seen so far: add it so SEEN-REFS.
3055 ;; Also search for subsequent references in
3056 ;; footnote definition so numbering follows reading
3057 ;; logic. Note that we don't have to care about
3058 ;; inline definitions, since `org-element-map'
3059 ;; already traverses them at the right time.
3061 ;; Once again, return nil to stay in the loop.
3062 ((not (member fn-lbl seen-refs))
3063 (push fn-lbl seen-refs)
3064 (funcall search-ref
3065 (org-export-get-footnote-definition fn info))
3066 nil))))
3067 ;; Don't enter footnote definitions since it will happen
3068 ;; when their first reference is found.
3069 info 'first-match 'footnote-definition)))))
3070 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3073 ;;;; For Headlines
3075 ;; `org-export-get-relative-level' is a shortcut to get headline
3076 ;; level, relatively to the lower headline level in the parsed tree.
3078 ;; `org-export-get-headline-number' returns the section number of an
3079 ;; headline, while `org-export-number-to-roman' allows to convert it
3080 ;; to roman numbers.
3082 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3083 ;; `org-export-last-sibling-p' are three useful predicates when it
3084 ;; comes to fulfill the `:headline-levels' property.
3086 (defun org-export-get-relative-level (headline info)
3087 "Return HEADLINE relative level within current parsed tree.
3088 INFO is a plist holding contextual information."
3089 (+ (org-element-property :level headline)
3090 (or (plist-get info :headline-offset) 0)))
3092 (defun org-export-low-level-p (headline info)
3093 "Non-nil when HEADLINE is considered as low level.
3095 INFO is a plist used as a communication channel.
3097 A low level headlines has a relative level greater than
3098 `:headline-levels' property value.
3100 Return value is the difference between HEADLINE relative level
3101 and the last level being considered as high enough, or nil."
3102 (let ((limit (plist-get info :headline-levels)))
3103 (when (wholenump limit)
3104 (let ((level (org-export-get-relative-level headline info)))
3105 (and (> level limit) (- level limit))))))
3107 (defun org-export-get-headline-number (headline info)
3108 "Return HEADLINE numbering as a list of numbers.
3109 INFO is a plist holding contextual information."
3110 (cdr (assoc headline (plist-get info :headline-numbering))))
3112 (defun org-export-numbered-headline-p (headline info)
3113 "Return a non-nil value if HEADLINE element should be numbered.
3114 INFO is a plist used as a communication channel."
3115 (let ((sec-num (plist-get info :section-numbers))
3116 (level (org-export-get-relative-level headline info)))
3117 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3119 (defun org-export-number-to-roman (n)
3120 "Convert integer N into a roman numeral."
3121 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3122 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3123 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3124 ( 1 . "I")))
3125 (res ""))
3126 (if (<= n 0)
3127 (number-to-string n)
3128 (while roman
3129 (if (>= n (caar roman))
3130 (setq n (- n (caar roman))
3131 res (concat res (cdar roman)))
3132 (pop roman)))
3133 res)))
3135 (defun org-export-get-tags (element info &optional tags)
3136 "Return list of tags associated to ELEMENT.
3138 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3139 is a plist used as a communication channel.
3141 Select tags (see `org-export-select-tags') and exclude tags (see
3142 `org-export-exclude-tags') are removed from the list.
3144 When non-nil, optional argument TAGS should be a list of strings.
3145 Any tag belonging to this list will also be removed."
3146 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3147 (member tag (plist-get info :exclude-tags))
3148 (member tag tags)))
3149 (org-element-property :tags element)))
3151 (defun org-export-first-sibling-p (headline info)
3152 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3153 INFO is a plist used as a communication channel."
3154 (not (eq (org-element-type (org-export-get-previous-element headline info))
3155 'headline)))
3157 (defun org-export-last-sibling-p (headline info)
3158 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3159 INFO is a plist used as a communication channel."
3160 (not (org-export-get-next-element headline info)))
3163 ;;;; For Links
3165 ;; `org-export-solidify-link-text' turns a string into a safer version
3166 ;; for links, replacing most non-standard characters with hyphens.
3168 ;; `org-export-get-coderef-format' returns an appropriate format
3169 ;; string for coderefs.
3171 ;; `org-export-inline-image-p' returns a non-nil value when the link
3172 ;; provided should be considered as an inline image.
3174 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3175 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3176 ;; returns an appropriate unique identifier when found, or nil.
3178 ;; `org-export-resolve-id-link' returns the first headline with
3179 ;; specified id or custom-id in parse tree, the path to the external
3180 ;; file with the id or nil when neither was found.
3182 ;; `org-export-resolve-coderef' associates a reference to a line
3183 ;; number in the element it belongs, or returns the reference itself
3184 ;; when the element isn't numbered.
3186 (defun org-export-solidify-link-text (s)
3187 "Take link text S and make a safe target out of it."
3188 (save-match-data
3189 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3191 (defun org-export-get-coderef-format (path desc)
3192 "Return format string for code reference link.
3193 PATH is the link path. DESC is its description."
3194 (save-match-data
3195 (cond ((not desc) "%s")
3196 ((string-match (regexp-quote (concat "(" path ")")) desc)
3197 (replace-match "%s" t t desc))
3198 (t desc))))
3200 (defun org-export-inline-image-p (link &optional rules)
3201 "Non-nil if LINK object points to an inline image.
3203 Optional argument is a set of RULES defining inline images. It
3204 is an alist where associations have the following shape:
3206 \(TYPE . REGEXP)
3208 Applying a rule means apply REGEXP against LINK's path when its
3209 type is TYPE. The function will return a non-nil value if any of
3210 the provided rules is non-nil. The default rule is
3211 `org-export-default-inline-image-rule'.
3213 This only applies to links without a description."
3214 (and (not (org-element-contents link))
3215 (let ((case-fold-search t)
3216 (rules (or rules org-export-default-inline-image-rule)))
3217 (catch 'exit
3218 (mapc
3219 (lambda (rule)
3220 (and (string= (org-element-property :type link) (car rule))
3221 (string-match (cdr rule)
3222 (org-element-property :path link))
3223 (throw 'exit t)))
3224 rules)
3225 ;; Return nil if no rule matched.
3226 nil))))
3228 (defun org-export-resolve-coderef (ref info)
3229 "Resolve a code reference REF.
3231 INFO is a plist used as a communication channel.
3233 Return associated line number in source code, or REF itself,
3234 depending on src-block or example element's switches."
3235 (org-element-map
3236 (plist-get info :parse-tree) '(example-block src-block)
3237 (lambda (el)
3238 (with-temp-buffer
3239 (insert (org-trim (org-element-property :value el)))
3240 (let* ((label-fmt (regexp-quote
3241 (or (org-element-property :label-fmt el)
3242 org-coderef-label-format)))
3243 (ref-re
3244 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3245 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3246 ;; Element containing REF is found. Resolve it to either
3247 ;; a label or a line number, as needed.
3248 (when (re-search-backward ref-re nil t)
3249 (cond
3250 ((org-element-property :use-labels el) ref)
3251 ((eq (org-element-property :number-lines el) 'continued)
3252 (+ (org-export-get-loc el info) (line-number-at-pos)))
3253 (t (line-number-at-pos)))))))
3254 info 'first-match))
3256 (defun org-export-resolve-fuzzy-link (link info)
3257 "Return LINK destination.
3259 INFO is a plist holding contextual information.
3261 Return value can be an object, an element, or nil:
3263 - If LINK path matches a target object (i.e. <<path>>) or
3264 element (i.e. \"#+TARGET: path\"), return it.
3266 - If LINK path exactly matches the name affiliated keyword
3267 \(i.e. #+NAME: path) of an element, return that element.
3269 - If LINK path exactly matches any headline name, return that
3270 element. If more than one headline share that name, priority
3271 will be given to the one with the closest common ancestor, if
3272 any, or the first one in the parse tree otherwise.
3274 - Otherwise, return nil.
3276 Assume LINK type is \"fuzzy\"."
3277 (let* ((path (org-element-property :path link))
3278 (match-title-p (eq (aref path 0) ?*)))
3279 (cond
3280 ;; First try to find a matching "<<path>>" unless user specified
3281 ;; he was looking for an headline (path starts with a *
3282 ;; character).
3283 ((and (not match-title-p)
3284 (loop for target in (plist-get info :target-list)
3285 when (string= (org-element-property :value target) path)
3286 return target)))
3287 ;; Then try to find an element with a matching "#+NAME: path"
3288 ;; affiliated keyword.
3289 ((and (not match-title-p)
3290 (org-element-map
3291 (plist-get info :parse-tree) org-element-all-elements
3292 (lambda (el)
3293 (when (string= (org-element-property :name el) path) el))
3294 info 'first-match)))
3295 ;; Last case: link either points to an headline or to
3296 ;; nothingness. Try to find the source, with priority given to
3297 ;; headlines with the closest common ancestor. If such candidate
3298 ;; is found, return it, otherwise return nil.
3300 (let ((find-headline
3301 (function
3302 ;; Return first headline whose `:raw-value' property
3303 ;; is NAME in parse tree DATA, or nil.
3304 (lambda (name data)
3305 (org-element-map
3306 data 'headline
3307 (lambda (headline)
3308 (when (string=
3309 (org-element-property :raw-value headline)
3310 name)
3311 headline))
3312 info 'first-match)))))
3313 ;; Search among headlines sharing an ancestor with link,
3314 ;; from closest to farthest.
3315 (or (catch 'exit
3316 (mapc
3317 (lambda (parent)
3318 (when (eq (org-element-type parent) 'headline)
3319 (let ((foundp (funcall find-headline path parent)))
3320 (when foundp (throw 'exit foundp)))))
3321 (org-export-get-genealogy link)) nil)
3322 ;; No match with a common ancestor: try the full parse-tree.
3323 (funcall find-headline
3324 (if match-title-p (substring path 1) path)
3325 (plist-get info :parse-tree))))))))
3327 (defun org-export-resolve-id-link (link info)
3328 "Return headline referenced as LINK destination.
3330 INFO is a plist used as a communication channel.
3332 Return value can be the headline element matched in current parse
3333 tree, a file name or nil. Assume LINK type is either \"id\" or
3334 \"custom-id\"."
3335 (let ((id (org-element-property :path link)))
3336 ;; First check if id is within the current parse tree.
3337 (or (org-element-map
3338 (plist-get info :parse-tree) 'headline
3339 (lambda (headline)
3340 (when (or (string= (org-element-property :id headline) id)
3341 (string= (org-element-property :custom-id headline) id))
3342 headline))
3343 info 'first-match)
3344 ;; Otherwise, look for external files.
3345 (cdr (assoc id (plist-get info :id-alist))))))
3347 (defun org-export-resolve-radio-link (link info)
3348 "Return radio-target object referenced as LINK destination.
3350 INFO is a plist used as a communication channel.
3352 Return value can be a radio-target object or nil. Assume LINK
3353 has type \"radio\"."
3354 (let ((path (org-element-property :path link)))
3355 (org-element-map
3356 (plist-get info :parse-tree) 'radio-target
3357 (lambda (radio)
3358 (when (equal (org-element-property :value radio) path) radio))
3359 info 'first-match)))
3362 ;;;; For Macros
3364 ;; `org-export-expand-macro' simply takes care of expanding macros.
3366 (defun org-export-expand-macro (macro info)
3367 "Expand MACRO and return it as a string.
3368 INFO is a plist holding export options."
3369 (let* ((key (org-element-property :key macro))
3370 (args (org-element-property :args macro))
3371 ;; User's macros are stored in the communication channel with
3372 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3373 ;; expand recursively macros within.
3374 (value (org-export-data
3375 (mapcar
3376 (lambda (obj)
3377 (if (not (stringp obj)) (org-export-data obj info)
3378 (replace-regexp-in-string
3379 "\\$[0-9]+"
3380 (lambda (arg)
3381 (nth (1- (string-to-number (substring arg 1))) args))
3382 obj)))
3383 (plist-get info (intern (format ":macro-%s" key))))
3384 info)))
3385 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3386 (when (string-match "\\`(eval\\>" value) (setq value (eval (read value))))
3387 ;; Return string.
3388 (format "%s" (or value ""))))
3391 ;;;; For References
3393 ;; `org-export-get-ordinal' associates a sequence number to any object
3394 ;; or element.
3396 (defun org-export-get-ordinal (element info &optional types predicate)
3397 "Return ordinal number of an element or object.
3399 ELEMENT is the element or object considered. INFO is the plist
3400 used as a communication channel.
3402 Optional argument TYPES, when non-nil, is a list of element or
3403 object types, as symbols, that should also be counted in.
3404 Otherwise, only provided element's type is considered.
3406 Optional argument PREDICATE is a function returning a non-nil
3407 value if the current element or object should be counted in. It
3408 accepts two arguments: the element or object being considered and
3409 the plist used as a communication channel. This allows to count
3410 only a certain type of objects (i.e. inline images).
3412 Return value is a list of numbers if ELEMENT is an headline or an
3413 item. It is nil for keywords. It represents the footnote number
3414 for footnote definitions and footnote references. If ELEMENT is
3415 a target, return the same value as if ELEMENT was the closest
3416 table, item or headline containing the target. In any other
3417 case, return the sequence number of ELEMENT among elements or
3418 objects of the same type."
3419 ;; A target keyword, representing an invisible target, never has
3420 ;; a sequence number.
3421 (unless (eq (org-element-type element) 'keyword)
3422 ;; Ordinal of a target object refer to the ordinal of the closest
3423 ;; table, item, or headline containing the object.
3424 (when (eq (org-element-type element) 'target)
3425 (setq element
3426 (loop for parent in (org-export-get-genealogy element)
3427 when
3428 (memq
3429 (org-element-type parent)
3430 '(footnote-definition footnote-reference headline item
3431 table))
3432 return parent)))
3433 (case (org-element-type element)
3434 ;; Special case 1: An headline returns its number as a list.
3435 (headline (org-export-get-headline-number element info))
3436 ;; Special case 2: An item returns its number as a list.
3437 (item (let ((struct (org-element-property :structure element)))
3438 (org-list-get-item-number
3439 (org-element-property :begin element)
3440 struct
3441 (org-list-prevs-alist struct)
3442 (org-list-parents-alist struct))))
3443 ((footnote-definition footnote-reference)
3444 (org-export-get-footnote-number element info))
3445 (otherwise
3446 (let ((counter 0))
3447 ;; Increment counter until ELEMENT is found again.
3448 (org-element-map
3449 (plist-get info :parse-tree) (or types (org-element-type element))
3450 (lambda (el)
3451 (cond
3452 ((eq element el) (1+ counter))
3453 ((not predicate) (incf counter) nil)
3454 ((funcall predicate el info) (incf counter) nil)))
3455 info 'first-match))))))
3458 ;;;; For Src-Blocks
3460 ;; `org-export-get-loc' counts number of code lines accumulated in
3461 ;; src-block or example-block elements with a "+n" switch until
3462 ;; a given element, excluded. Note: "-n" switches reset that count.
3464 ;; `org-export-unravel-code' extracts source code (along with a code
3465 ;; references alist) from an `element-block' or `src-block' type
3466 ;; element.
3468 ;; `org-export-format-code' applies a formatting function to each line
3469 ;; of code, providing relative line number and code reference when
3470 ;; appropriate. Since it doesn't access the original element from
3471 ;; which the source code is coming, it expects from the code calling
3472 ;; it to know if lines should be numbered and if code references
3473 ;; should appear.
3475 ;; Eventually, `org-export-format-code-default' is a higher-level
3476 ;; function (it makes use of the two previous functions) which handles
3477 ;; line numbering and code references inclusion, and returns source
3478 ;; code in a format suitable for plain text or verbatim output.
3480 (defun org-export-get-loc (element info)
3481 "Return accumulated lines of code up to ELEMENT.
3483 INFO is the plist used as a communication channel.
3485 ELEMENT is excluded from count."
3486 (let ((loc 0))
3487 (org-element-map
3488 (plist-get info :parse-tree)
3489 `(src-block example-block ,(org-element-type element))
3490 (lambda (el)
3491 (cond
3492 ;; ELEMENT is reached: Quit the loop.
3493 ((eq el element))
3494 ;; Only count lines from src-block and example-block elements
3495 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3496 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3497 ((let ((linums (org-element-property :number-lines el)))
3498 (when linums
3499 ;; Accumulate locs or reset them.
3500 (let ((lines (org-count-lines
3501 (org-trim (org-element-property :value el)))))
3502 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3503 ;; Return nil to stay in the loop.
3504 nil)))
3505 info 'first-match)
3506 ;; Return value.
3507 loc))
3509 (defun org-export-unravel-code (element)
3510 "Clean source code and extract references out of it.
3512 ELEMENT has either a `src-block' an `example-block' type.
3514 Return a cons cell whose CAR is the source code, cleaned from any
3515 reference and protective comma and CDR is an alist between
3516 relative line number (integer) and name of code reference on that
3517 line (string)."
3518 (let* ((line 0) refs
3519 ;; Get code and clean it. Remove blank lines at its
3520 ;; beginning and end. Also remove protective commas.
3521 (code (let ((c (replace-regexp-in-string
3522 "\\`\\([ \t]*\n\\)+" ""
3523 (replace-regexp-in-string
3524 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3525 (org-element-property :value element)))))
3526 ;; If appropriate, remove global indentation.
3527 (unless (or org-src-preserve-indentation
3528 (org-element-property :preserve-indent element))
3529 (setq c (org-remove-indentation c)))
3530 ;; Free up the protected lines. Note: Org blocks
3531 ;; have commas at the beginning or every line.
3532 (if (string= (org-element-property :language element) "org")
3533 (replace-regexp-in-string "^," "" c)
3534 (replace-regexp-in-string
3535 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3536 ;; Get format used for references.
3537 (label-fmt (regexp-quote
3538 (or (org-element-property :label-fmt element)
3539 org-coderef-label-format)))
3540 ;; Build a regexp matching a loc with a reference.
3541 (with-ref-re
3542 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3543 (replace-regexp-in-string
3544 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3545 ;; Return value.
3546 (cons
3547 ;; Code with references removed.
3548 (org-element-normalize-string
3549 (mapconcat
3550 (lambda (loc)
3551 (incf line)
3552 (if (not (string-match with-ref-re loc)) loc
3553 ;; Ref line: remove ref, and signal its position in REFS.
3554 (push (cons line (match-string 3 loc)) refs)
3555 (replace-match "" nil nil loc 1)))
3556 (org-split-string code "\n") "\n"))
3557 ;; Reference alist.
3558 refs)))
3560 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3561 "Format CODE by applying FUN line-wise and return it.
3563 CODE is a string representing the code to format. FUN is
3564 a function. It must accept three arguments: a line of
3565 code (string), the current line number (integer) or nil and the
3566 reference associated to the current line (string) or nil.
3568 Optional argument NUM-LINES can be an integer representing the
3569 number of code lines accumulated until the current code. Line
3570 numbers passed to FUN will take it into account. If it is nil,
3571 FUN's second argument will always be nil. This number can be
3572 obtained with `org-export-get-loc' function.
3574 Optional argument REF-ALIST can be an alist between relative line
3575 number (i.e. ignoring NUM-LINES) and the name of the code
3576 reference on it. If it is nil, FUN's third argument will always
3577 be nil. It can be obtained through the use of
3578 `org-export-unravel-code' function."
3579 (let ((--locs (org-split-string code "\n"))
3580 (--line 0))
3581 (org-element-normalize-string
3582 (mapconcat
3583 (lambda (--loc)
3584 (incf --line)
3585 (let ((--ref (cdr (assq --line ref-alist))))
3586 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3587 --locs "\n"))))
3589 (defun org-export-format-code-default (element info)
3590 "Return source code from ELEMENT, formatted in a standard way.
3592 ELEMENT is either a `src-block' or `example-block' element. INFO
3593 is a plist used as a communication channel.
3595 This function takes care of line numbering and code references
3596 inclusion. Line numbers, when applicable, appear at the
3597 beginning of the line, separated from the code by two white
3598 spaces. Code references, on the other hand, appear flushed to
3599 the right, separated by six white spaces from the widest line of
3600 code."
3601 ;; Extract code and references.
3602 (let* ((code-info (org-export-unravel-code element))
3603 (code (car code-info))
3604 (code-lines (org-split-string code "\n"))
3605 (refs (and (org-element-property :retain-labels element)
3606 (cdr code-info)))
3607 ;; Handle line numbering.
3608 (num-start (case (org-element-property :number-lines element)
3609 (continued (org-export-get-loc element info))
3610 (new 0)))
3611 (num-fmt
3612 (and num-start
3613 (format "%%%ds "
3614 (length (number-to-string
3615 (+ (length code-lines) num-start))))))
3616 ;; Prepare references display, if required. Any reference
3617 ;; should start six columns after the widest line of code,
3618 ;; wrapped with parenthesis.
3619 (max-width
3620 (+ (apply 'max (mapcar 'length code-lines))
3621 (if (not num-start) 0 (length (format num-fmt num-start))))))
3622 (org-export-format-code
3623 code
3624 (lambda (loc line-num ref)
3625 (let ((number-str (and num-fmt (format num-fmt line-num))))
3626 (concat
3627 number-str
3629 (and ref
3630 (concat (make-string
3631 (- (+ 6 max-width)
3632 (+ (length loc) (length number-str))) ? )
3633 (format "(%s)" ref))))))
3634 num-start refs)))
3637 ;;;; For Tables
3639 ;; `org-export-table-has-special-column-p' and and
3640 ;; `org-export-table-row-is-special-p' are predicates used to look for
3641 ;; meta-information about the table structure.
3643 ;; `org-table-has-header-p' tells when the rows before the first rule
3644 ;; should be considered as table's header.
3646 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3647 ;; and `org-export-table-cell-borders' extract information from
3648 ;; a table-cell element.
3650 ;; `org-export-table-dimensions' gives the number on rows and columns
3651 ;; in the table, ignoring horizontal rules and special columns.
3652 ;; `org-export-table-cell-address', given a table-cell object, returns
3653 ;; the absolute address of a cell. On the other hand,
3654 ;; `org-export-get-table-cell-at' does the contrary.
3656 ;; `org-export-table-cell-starts-colgroup-p',
3657 ;; `org-export-table-cell-ends-colgroup-p',
3658 ;; `org-export-table-row-starts-rowgroup-p',
3659 ;; `org-export-table-row-ends-rowgroup-p',
3660 ;; `org-export-table-row-starts-header-p' and
3661 ;; `org-export-table-row-ends-header-p' indicate position of current
3662 ;; row or cell within the table.
3664 (defun org-export-table-has-special-column-p (table)
3665 "Non-nil when TABLE has a special column.
3666 All special columns will be ignored during export."
3667 ;; The table has a special column when every first cell of every row
3668 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3669 ;; "*" "_" and "^". Though, do not consider a first row containing
3670 ;; only empty cells as special.
3671 (let ((special-column-p 'empty))
3672 (catch 'exit
3673 (mapc
3674 (lambda (row)
3675 (when (eq (org-element-property :type row) 'standard)
3676 (let ((value (org-element-contents
3677 (car (org-element-contents row)))))
3678 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3679 (setq special-column-p 'special))
3680 ((not value))
3681 (t (throw 'exit nil))))))
3682 (org-element-contents table))
3683 (eq special-column-p 'special))))
3685 (defun org-export-table-has-header-p (table info)
3686 "Non-nil when TABLE has an header.
3688 INFO is a plist used as a communication channel.
3690 A table has an header when it contains at least two row groups."
3691 (let ((rowgroup 1) row-flag)
3692 (org-element-map
3693 table 'table-row
3694 (lambda (row)
3695 (cond
3696 ((> rowgroup 1) t)
3697 ((and row-flag (eq (org-element-property :type row) 'rule))
3698 (incf rowgroup) (setq row-flag nil))
3699 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3700 (setq row-flag t) nil)))
3701 info)))
3703 (defun org-export-table-row-is-special-p (table-row info)
3704 "Non-nil if TABLE-ROW is considered special.
3706 INFO is a plist used as the communication channel.
3708 All special rows will be ignored during export."
3709 (when (eq (org-element-property :type table-row) 'standard)
3710 (let ((first-cell (org-element-contents
3711 (car (org-element-contents table-row)))))
3712 ;; A row is special either when...
3714 ;; ... it starts with a field only containing "/",
3715 (equal first-cell '("/"))
3716 ;; ... the table contains a special column and the row start
3717 ;; with a marking character among, "^", "_", "$" or "!",
3718 (and (org-export-table-has-special-column-p
3719 (org-export-get-parent table-row))
3720 (member first-cell '(("^") ("_") ("$") ("!"))))
3721 ;; ... it contains only alignment cookies and empty cells.
3722 (let ((special-row-p 'empty))
3723 (catch 'exit
3724 (mapc
3725 (lambda (cell)
3726 (let ((value (org-element-contents cell)))
3727 ;; Since VALUE is a secondary string, the following
3728 ;; checks avoid expanding it with `org-export-data'.
3729 (cond ((not value))
3730 ((and (not (cdr value))
3731 (stringp (car value))
3732 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3733 (car value)))
3734 (setq special-row-p 'cookie))
3735 (t (throw 'exit nil)))))
3736 (org-element-contents table-row))
3737 (eq special-row-p 'cookie)))))))
3739 (defun org-export-table-row-group (table-row info)
3740 "Return TABLE-ROW's group.
3742 INFO is a plist used as the communication channel.
3744 Return value is the group number, as an integer, or nil special
3745 rows and table rules. Group 1 is also table's header."
3746 (unless (or (eq (org-element-property :type table-row) 'rule)
3747 (org-export-table-row-is-special-p table-row info))
3748 (let ((group 0) row-flag)
3749 (catch 'found
3750 (mapc
3751 (lambda (row)
3752 (cond
3753 ((and (eq (org-element-property :type row) 'standard)
3754 (not (org-export-table-row-is-special-p row info)))
3755 (unless row-flag (incf group) (setq row-flag t)))
3756 ((eq (org-element-property :type row) 'rule)
3757 (setq row-flag nil)))
3758 (when (eq table-row row) (throw 'found group)))
3759 (org-element-contents (org-export-get-parent table-row)))))))
3761 (defun org-export-table-cell-width (table-cell info)
3762 "Return TABLE-CELL contents width.
3764 INFO is a plist used as the communication channel.
3766 Return value is the width given by the last width cookie in the
3767 same column as TABLE-CELL, or nil."
3768 (let* ((row (org-export-get-parent table-cell))
3769 (column (let ((cells (org-element-contents row)))
3770 (- (length cells) (length (memq table-cell cells)))))
3771 (table (org-export-get-parent-table table-cell))
3772 cookie-width)
3773 (mapc
3774 (lambda (row)
3775 (cond
3776 ;; In a special row, try to find a width cookie at COLUMN.
3777 ((org-export-table-row-is-special-p row info)
3778 (let ((value (org-element-contents
3779 (elt (org-element-contents row) column))))
3780 ;; The following checks avoid expanding unnecessarily the
3781 ;; cell with `org-export-data'
3782 (when (and value
3783 (not (cdr value))
3784 (stringp (car value))
3785 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3786 (match-string 1 (car value)))
3787 (setq cookie-width
3788 (string-to-number (match-string 1 (car value)))))))
3789 ;; Ignore table rules.
3790 ((eq (org-element-property :type row) 'rule))))
3791 (org-element-contents table))
3792 ;; Return value.
3793 cookie-width))
3795 (defun org-export-table-cell-alignment (table-cell info)
3796 "Return TABLE-CELL contents alignment.
3798 INFO is a plist used as the communication channel.
3800 Return alignment as specified by the last alignment cookie in the
3801 same column as TABLE-CELL. If no such cookie is found, a default
3802 alignment value will be deduced from fraction of numbers in the
3803 column (see `org-table-number-fraction' for more information).
3804 Possible values are `left', `right' and `center'."
3805 (let* ((row (org-export-get-parent table-cell))
3806 (column (let ((cells (org-element-contents row)))
3807 (- (length cells) (length (memq table-cell cells)))))
3808 (table (org-export-get-parent-table table-cell))
3809 (number-cells 0)
3810 (total-cells 0)
3811 cookie-align)
3812 (mapc
3813 (lambda (row)
3814 (cond
3815 ;; In a special row, try to find an alignment cookie at
3816 ;; COLUMN.
3817 ((org-export-table-row-is-special-p row info)
3818 (let ((value (org-element-contents
3819 (elt (org-element-contents row) column))))
3820 ;; Since VALUE is a secondary string, the following checks
3821 ;; avoid useless expansion through `org-export-data'.
3822 (when (and value
3823 (not (cdr value))
3824 (stringp (car value))
3825 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3826 (car value))
3827 (match-string 1 (car value)))
3828 (setq cookie-align (match-string 1 (car value))))))
3829 ;; Ignore table rules.
3830 ((eq (org-element-property :type row) 'rule))
3831 ;; In a standard row, check if cell's contents are expressing
3832 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3833 ;; Though, don't bother if an alignment cookie has already
3834 ;; defined cell's alignment.
3835 ((not cookie-align)
3836 (let ((value (org-export-data
3837 (org-element-contents
3838 (elt (org-element-contents row) column))
3839 info)))
3840 (incf total-cells)
3841 (when (string-match org-table-number-regexp value)
3842 (incf number-cells))))))
3843 (org-element-contents table))
3844 ;; Return value. Alignment specified by cookies has precedence
3845 ;; over alignment deduced from cells contents.
3846 (cond ((equal cookie-align "l") 'left)
3847 ((equal cookie-align "r") 'right)
3848 ((equal cookie-align "c") 'center)
3849 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3850 'right)
3851 (t 'left))))
3853 (defun org-export-table-cell-borders (table-cell info)
3854 "Return TABLE-CELL borders.
3856 INFO is a plist used as a communication channel.
3858 Return value is a list of symbols, or nil. Possible values are:
3859 `top', `bottom', `above', `below', `left' and `right'. Note:
3860 `top' (resp. `bottom') only happen for a cell in the first
3861 row (resp. last row) of the table, ignoring table rules, if any.
3863 Returned borders ignore special rows."
3864 (let* ((row (org-export-get-parent table-cell))
3865 (table (org-export-get-parent-table table-cell))
3866 borders)
3867 ;; Top/above border? TABLE-CELL has a border above when a rule
3868 ;; used to demarcate row groups can be found above. Hence,
3869 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3870 ;; another regular row has to be found above that rule.
3871 (let (rule-flag)
3872 (catch 'exit
3873 (mapc (lambda (row)
3874 (cond ((eq (org-element-property :type row) 'rule)
3875 (setq rule-flag t))
3876 ((not (org-export-table-row-is-special-p row info))
3877 (if rule-flag (throw 'exit (push 'above borders))
3878 (throw 'exit nil)))))
3879 ;; Look at every row before the current one.
3880 (cdr (memq row (reverse (org-element-contents table)))))
3881 ;; No rule above, or rule found starts the table (ignoring any
3882 ;; special row): TABLE-CELL is at the top of the table.
3883 (when rule-flag (push 'above borders))
3884 (push 'top borders)))
3885 ;; Bottom/below border? TABLE-CELL has a border below when next
3886 ;; non-regular row below is a rule.
3887 (let (rule-flag)
3888 (catch 'exit
3889 (mapc (lambda (row)
3890 (cond ((eq (org-element-property :type row) 'rule)
3891 (setq rule-flag t))
3892 ((not (org-export-table-row-is-special-p row info))
3893 (if rule-flag (throw 'exit (push 'below borders))
3894 (throw 'exit nil)))))
3895 ;; Look at every row after the current one.
3896 (cdr (memq row (org-element-contents table))))
3897 ;; No rule below, or rule found ends the table (modulo some
3898 ;; special row): TABLE-CELL is at the bottom of the table.
3899 (when rule-flag (push 'below borders))
3900 (push 'bottom borders)))
3901 ;; Right/left borders? They can only be specified by column
3902 ;; groups. Column groups are defined in a row starting with "/".
3903 ;; Also a column groups row only contains "<", "<>", ">" or blank
3904 ;; cells.
3905 (catch 'exit
3906 (let ((column (let ((cells (org-element-contents row)))
3907 (- (length cells) (length (memq table-cell cells))))))
3908 (mapc
3909 (lambda (row)
3910 (unless (eq (org-element-property :type row) 'rule)
3911 (when (equal (org-element-contents
3912 (car (org-element-contents row)))
3913 '("/"))
3914 (let ((column-groups
3915 (mapcar
3916 (lambda (cell)
3917 (let ((value (org-element-contents cell)))
3918 (when (member value '(("<") ("<>") (">") nil))
3919 (car value))))
3920 (org-element-contents row))))
3921 ;; There's a left border when previous cell, if
3922 ;; any, ends a group, or current one starts one.
3923 (when (or (and (not (zerop column))
3924 (member (elt column-groups (1- column))
3925 '(">" "<>")))
3926 (member (elt column-groups column) '("<" "<>")))
3927 (push 'left borders))
3928 ;; There's a right border when next cell, if any,
3929 ;; starts a group, or current one ends one.
3930 (when (or (and (/= (1+ column) (length column-groups))
3931 (member (elt column-groups (1+ column))
3932 '("<" "<>")))
3933 (member (elt column-groups column) '(">" "<>")))
3934 (push 'right borders))
3935 (throw 'exit nil)))))
3936 ;; Table rows are read in reverse order so last column groups
3937 ;; row has precedence over any previous one.
3938 (reverse (org-element-contents table)))))
3939 ;; Return value.
3940 borders))
3942 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3943 "Non-nil when TABLE-CELL is at the beginning of a row group.
3944 INFO is a plist used as a communication channel."
3945 ;; A cell starts a column group either when it is at the beginning
3946 ;; of a row (or after the special column, if any) or when it has
3947 ;; a left border.
3948 (or (eq (org-element-map
3949 (org-export-get-parent table-cell)
3950 'table-cell 'identity info 'first-match)
3951 table-cell)
3952 (memq 'left (org-export-table-cell-borders table-cell info))))
3954 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3955 "Non-nil when TABLE-CELL is at the end of a row group.
3956 INFO is a plist used as a communication channel."
3957 ;; A cell ends a column group either when it is at the end of a row
3958 ;; or when it has a right border.
3959 (or (eq (car (last (org-element-contents
3960 (org-export-get-parent table-cell))))
3961 table-cell)
3962 (memq 'right (org-export-table-cell-borders table-cell info))))
3964 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3965 "Non-nil when TABLE-ROW is at the beginning of a column group.
3966 INFO is a plist used as a communication channel."
3967 (unless (or (eq (org-element-property :type table-row) 'rule)
3968 (org-export-table-row-is-special-p table-row info))
3969 (let ((borders (org-export-table-cell-borders
3970 (car (org-element-contents table-row)) info)))
3971 (or (memq 'top borders) (memq 'above borders)))))
3973 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3974 "Non-nil when TABLE-ROW is at the end of a column group.
3975 INFO is a plist used as a communication channel."
3976 (unless (or (eq (org-element-property :type table-row) 'rule)
3977 (org-export-table-row-is-special-p table-row info))
3978 (let ((borders (org-export-table-cell-borders
3979 (car (org-element-contents table-row)) info)))
3980 (or (memq 'bottom borders) (memq 'below borders)))))
3982 (defun org-export-table-row-starts-header-p (table-row info)
3983 "Non-nil when TABLE-ROW is the first table header's row.
3984 INFO is a plist used as a communication channel."
3985 (and (org-export-table-has-header-p
3986 (org-export-get-parent-table table-row) info)
3987 (org-export-table-row-starts-rowgroup-p table-row info)
3988 (= (org-export-table-row-group table-row info) 1)))
3990 (defun org-export-table-row-ends-header-p (table-row info)
3991 "Non-nil when TABLE-ROW is the last table header's row.
3992 INFO is a plist used as a communication channel."
3993 (and (org-export-table-has-header-p
3994 (org-export-get-parent-table table-row) info)
3995 (org-export-table-row-ends-rowgroup-p table-row info)
3996 (= (org-export-table-row-group table-row info) 1)))
3998 (defun org-export-table-dimensions (table info)
3999 "Return TABLE dimensions.
4001 INFO is a plist used as a communication channel.
4003 Return value is a CONS like (ROWS . COLUMNS) where
4004 ROWS (resp. COLUMNS) is the number of exportable
4005 rows (resp. columns)."
4006 (let (first-row (columns 0) (rows 0))
4007 ;; Set number of rows, and extract first one.
4008 (org-element-map
4009 table 'table-row
4010 (lambda (row)
4011 (when (eq (org-element-property :type row) 'standard)
4012 (incf rows)
4013 (unless first-row (setq first-row row)))) info)
4014 ;; Set number of columns.
4015 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4016 ;; Return value.
4017 (cons rows columns)))
4019 (defun org-export-table-cell-address (table-cell info)
4020 "Return address of a regular TABLE-CELL object.
4022 TABLE-CELL is the cell considered. INFO is a plist used as
4023 a communication channel.
4025 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4026 zero-based index. Only exportable cells are considered. The
4027 function returns nil for other cells."
4028 (let* ((table-row (org-export-get-parent table-cell))
4029 (table (org-export-get-parent-table table-cell)))
4030 ;; Ignore cells in special rows or in special column.
4031 (unless (or (org-export-table-row-is-special-p table-row info)
4032 (and (org-export-table-has-special-column-p table)
4033 (eq (car (org-element-contents table-row)) table-cell)))
4034 (cons
4035 ;; Row number.
4036 (let ((row-count 0))
4037 (org-element-map
4038 table 'table-row
4039 (lambda (row)
4040 (cond ((eq (org-element-property :type row) 'rule) nil)
4041 ((eq row table-row) row-count)
4042 (t (incf row-count) nil)))
4043 info 'first-match))
4044 ;; Column number.
4045 (let ((col-count 0))
4046 (org-element-map
4047 table-row 'table-cell
4048 (lambda (cell)
4049 (if (eq cell table-cell) col-count (incf col-count) nil))
4050 info 'first-match))))))
4052 (defun org-export-get-table-cell-at (address table info)
4053 "Return regular table-cell object at ADDRESS in TABLE.
4055 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4056 zero-based index. TABLE is a table type element. INFO is
4057 a plist used as a communication channel.
4059 If no table-cell, among exportable cells, is found at ADDRESS,
4060 return nil."
4061 (let ((column-pos (cdr address)) (column-count 0))
4062 (org-element-map
4063 ;; Row at (car address) or nil.
4064 (let ((row-pos (car address)) (row-count 0))
4065 (org-element-map
4066 table 'table-row
4067 (lambda (row)
4068 (cond ((eq (org-element-property :type row) 'rule) nil)
4069 ((= row-count row-pos) row)
4070 (t (incf row-count) nil)))
4071 info 'first-match))
4072 'table-cell
4073 (lambda (cell)
4074 (if (= column-count column-pos) cell
4075 (incf column-count) nil))
4076 info 'first-match)))
4079 ;;;; For Tables Of Contents
4081 ;; `org-export-collect-headlines' builds a list of all exportable
4082 ;; headline elements, maybe limited to a certain depth. One can then
4083 ;; easily parse it and transcode it.
4085 ;; Building lists of tables, figures or listings is quite similar.
4086 ;; Once the generic function `org-export-collect-elements' is defined,
4087 ;; `org-export-collect-tables', `org-export-collect-figures' and
4088 ;; `org-export-collect-listings' can be derived from it.
4090 (defun org-export-collect-headlines (info &optional n)
4091 "Collect headlines in order to build a table of contents.
4093 INFO is a plist used as a communication channel.
4095 When optional argument N is an integer, it specifies the depth of
4096 the table of contents. Otherwise, it is set to the value of the
4097 last headline level. See `org-export-headline-levels' for more
4098 information.
4100 Return a list of all exportable headlines as parsed elements."
4101 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4102 (org-element-map
4103 (plist-get info :parse-tree)
4104 'headline
4105 (lambda (headline)
4106 ;; Strip contents from HEADLINE.
4107 (let ((relative-level (org-export-get-relative-level headline info)))
4108 (unless (> relative-level n) headline)))
4109 info))
4111 (defun org-export-collect-elements (type info &optional predicate)
4112 "Collect referenceable elements of a determined type.
4114 TYPE can be a symbol or a list of symbols specifying element
4115 types to search. Only elements with a caption are collected.
4117 INFO is a plist used as a communication channel.
4119 When non-nil, optional argument PREDICATE is a function accepting
4120 one argument, an element of type TYPE. It returns a non-nil
4121 value when that element should be collected.
4123 Return a list of all elements found, in order of appearance."
4124 (org-element-map
4125 (plist-get info :parse-tree) type
4126 (lambda (element)
4127 (and (org-element-property :caption element)
4128 (or (not predicate) (funcall predicate element))
4129 element))
4130 info))
4132 (defun org-export-collect-tables (info)
4133 "Build a list of tables.
4134 INFO is a plist used as a communication channel.
4136 Return a list of table elements with a caption."
4137 (org-export-collect-elements 'table info))
4139 (defun org-export-collect-figures (info predicate)
4140 "Build a list of figures.
4142 INFO is a plist used as a communication channel. PREDICATE is
4143 a function which accepts one argument: a paragraph element and
4144 whose return value is non-nil when that element should be
4145 collected.
4147 A figure is a paragraph type element, with a caption, verifying
4148 PREDICATE. The latter has to be provided since a \"figure\" is
4149 a vague concept that may depend on back-end.
4151 Return a list of elements recognized as figures."
4152 (org-export-collect-elements 'paragraph info predicate))
4154 (defun org-export-collect-listings (info)
4155 "Build a list of src blocks.
4157 INFO is a plist used as a communication channel.
4159 Return a list of src-block elements with a caption."
4160 (org-export-collect-elements 'src-block info))
4163 ;;;; Topology
4165 ;; Here are various functions to retrieve information about the
4166 ;; neighbourhood of a given element or object. Neighbours of interest
4167 ;; are direct parent (`org-export-get-parent'), parent headline
4168 ;; (`org-export-get-parent-headline'), first element containing an
4169 ;; object, (`org-export-get-parent-element'), parent table
4170 ;; (`org-export-get-parent-table'), previous element or object
4171 ;; (`org-export-get-previous-element') and next element or object
4172 ;; (`org-export-get-next-element').
4174 ;; `org-export-get-genealogy' returns the full genealogy of a given
4175 ;; element or object, from closest parent to full parse tree.
4177 (defun org-export-get-parent (blob)
4178 "Return BLOB parent or nil.
4179 BLOB is the element or object considered."
4180 (org-element-property :parent blob))
4182 (defun org-export-get-genealogy (blob)
4183 "Return full genealogy relative to a given element or object.
4185 BLOB is the element or object being considered.
4187 Ancestors are returned from closest to farthest, the last one
4188 being the full parse tree."
4189 (let (genealogy (parent blob))
4190 (while (setq parent (org-element-property :parent parent))
4191 (push parent genealogy))
4192 (nreverse genealogy)))
4194 (defun org-export-get-parent-headline (blob)
4195 "Return BLOB parent headline or nil.
4196 BLOB is the element or object being considered."
4197 (let ((parent blob))
4198 (while (and (setq parent (org-element-property :parent parent))
4199 (not (eq (org-element-type parent) 'headline))))
4200 parent))
4202 (defun org-export-get-parent-element (object)
4203 "Return first element containing OBJECT or nil.
4204 OBJECT is the object to consider."
4205 (let ((parent object))
4206 (while (and (setq parent (org-element-property :parent parent))
4207 (memq (org-element-type parent) org-element-all-objects)))
4208 parent))
4210 (defun org-export-get-parent-table (object)
4211 "Return OBJECT parent table or nil.
4212 OBJECT is either a `table-cell' or `table-element' type object."
4213 (let ((parent object))
4214 (while (and (setq parent (org-element-property :parent parent))
4215 (not (eq (org-element-type parent) 'table))))
4216 parent))
4218 (defun org-export-get-previous-element (blob info)
4219 "Return previous element or object.
4220 BLOB is an element or object. INFO is a plist used as
4221 a communication channel. Return previous exportable element or
4222 object, a string, or nil."
4223 (let (prev)
4224 (catch 'exit
4225 (mapc (lambda (obj)
4226 (cond ((eq obj blob) (throw 'exit prev))
4227 ((memq obj (plist-get info :ignore-list)))
4228 (t (setq prev obj))))
4229 (org-element-contents (org-export-get-parent blob))))))
4231 (defun org-export-get-next-element (blob info)
4232 "Return next element or object.
4233 BLOB is an element or object. INFO is a plist used as
4234 a communication channel. Return next exportable element or
4235 object, a string, or nil."
4236 (catch 'found
4237 (mapc (lambda (obj)
4238 (unless (memq obj (plist-get info :ignore-list))
4239 (throw 'found obj)))
4240 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4241 nil))
4244 ;;;; Translation
4246 ;; `org-export-translate' translates a string according to language
4247 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4248 ;; variable and a specified charset. `org-export-dictionary' contains
4249 ;; the dictionary used for the translation.
4251 (defconst org-export-dictionary
4252 '(("Author"
4253 ("ca" :default "Autor")
4254 ("cs" :default "Autor")
4255 ("da" :default "Ophavsmand")
4256 ("de" :default "Autor")
4257 ("eo" :html "A&#365;toro")
4258 ("es" :default "Autor")
4259 ("fi" :html "Tekij&auml;")
4260 ("fr" :default "Auteur")
4261 ("hu" :default "Szerz&otilde;")
4262 ("is" :html "H&ouml;fundur")
4263 ("it" :default "Autore")
4264 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4265 ("nl" :default "Auteur")
4266 ("no" :default "Forfatter")
4267 ("nb" :default "Forfatter")
4268 ("nn" :default "Forfattar")
4269 ("pl" :default "Autor")
4270 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4271 ("sv" :html "F&ouml;rfattare")
4272 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4273 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4274 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4275 ("Date"
4276 ("ca" :default "Data")
4277 ("cs" :default "Datum")
4278 ("da" :default "Dato")
4279 ("de" :default "Datum")
4280 ("eo" :default "Dato")
4281 ("es" :default "Fecha")
4282 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4283 ("hu" :html "D&aacute;tum")
4284 ("is" :default "Dagsetning")
4285 ("it" :default "Data")
4286 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4287 ("nl" :default "Datum")
4288 ("no" :default "Dato")
4289 ("nb" :default "Dato")
4290 ("nn" :default "Dato")
4291 ("pl" :default "Data")
4292 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4293 ("sv" :default "Datum")
4294 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4295 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4296 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4297 ("Equation"
4298 ("fr" :ascii "Equation" :default "Équation"))
4299 ("Figure")
4300 ("Footnotes"
4301 ("ca" :html "Peus de p&agrave;gina")
4302 ("cs" :default "Pozn\xe1mky pod carou")
4303 ("da" :default "Fodnoter")
4304 ("de" :html "Fu&szlig;noten")
4305 ("eo" :default "Piednotoj")
4306 ("es" :html "Pies de p&aacute;gina")
4307 ("fi" :default "Alaviitteet")
4308 ("fr" :default "Notes de bas de page")
4309 ("hu" :html "L&aacute;bjegyzet")
4310 ("is" :html "Aftanm&aacute;lsgreinar")
4311 ("it" :html "Note a pi&egrave; di pagina")
4312 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4313 ("nl" :default "Voetnoten")
4314 ("no" :default "Fotnoter")
4315 ("nb" :default "Fotnoter")
4316 ("nn" :default "Fotnotar")
4317 ("pl" :default "Przypis")
4318 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4319 ("sv" :default "Fotnoter")
4320 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4321 :utf-8 "Примітки")
4322 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4323 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4324 ("List of Listings"
4325 ("fr" :default "Liste des programmes"))
4326 ("List of Tables"
4327 ("fr" :default "Liste des tableaux"))
4328 ("Listing %d:"
4329 ("fr"
4330 :ascii "Programme %d :" :default "Programme nº %d :"
4331 :latin1 "Programme %d :"))
4332 ("Listing %d: %s"
4333 ("fr"
4334 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4335 :latin1 "Programme %d : %s"))
4336 ("See section %s"
4337 ("fr" :default "cf. section %s"))
4338 ("Table %d:"
4339 ("fr"
4340 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4341 ("Table %d: %s"
4342 ("fr"
4343 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4344 :latin1 "Tableau %d : %s"))
4345 ("Table of Contents"
4346 ("ca" :html "&Iacute;ndex")
4347 ("cs" :default "Obsah")
4348 ("da" :default "Indhold")
4349 ("de" :default "Inhaltsverzeichnis")
4350 ("eo" :default "Enhavo")
4351 ("es" :html "&Iacute;ndice")
4352 ("fi" :html "Sis&auml;llysluettelo")
4353 ("fr" :ascii "Sommaire" :default "Table des matières")
4354 ("hu" :html "Tartalomjegyz&eacute;k")
4355 ("is" :default "Efnisyfirlit")
4356 ("it" :default "Indice")
4357 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4358 ("nl" :default "Inhoudsopgave")
4359 ("no" :default "Innhold")
4360 ("nb" :default "Innhold")
4361 ("nn" :default "Innhald")
4362 ("pl" :html "Spis tre&#x015b;ci")
4363 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4364 :utf-8 "Содержание")
4365 ("sv" :html "Inneh&aring;ll")
4366 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4367 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4368 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4369 ("Unknown reference"
4370 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4371 "Dictionary for export engine.
4373 Alist whose CAR is the string to translate and CDR is an alist
4374 whose CAR is the language string and CDR is a plist whose
4375 properties are possible charsets and values translated terms.
4377 It is used as a database for `org-export-translate'. Since this
4378 function returns the string as-is if no translation was found,
4379 the variable only needs to record values different from the
4380 entry.")
4382 (defun org-export-translate (s encoding info)
4383 "Translate string S according to language specification.
4385 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4386 and `:utf-8'. INFO is a plist used as a communication channel.
4388 Translation depends on `:language' property. Return the
4389 translated string. If no translation is found, try to fall back
4390 to `:default' encoding. If it fails, return S."
4391 (let* ((lang (plist-get info :language))
4392 (translations (cdr (assoc lang
4393 (cdr (assoc s org-export-dictionary))))))
4394 (or (plist-get translations encoding)
4395 (plist-get translations :default)
4396 s)))
4400 ;;; The Dispatcher
4402 ;; `org-export-dispatch' is the standard interactive way to start an
4403 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4404 ;; for its interface. Most commons back-ends should have an entry in
4405 ;; it.
4407 ;;;###autoload
4408 (defun org-export-dispatch ()
4409 "Export dispatcher for Org mode.
4411 It provides an access to common export related tasks in a buffer.
4412 Its interface comes in two flavours: standard and expert. While
4413 both share the same set of bindings, only the former displays the
4414 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4415 to switch to one or the other.
4417 Return an error if key pressed has no associated command."
4418 (interactive)
4419 (let* ((input (org-export-dispatch-ui
4420 (if (listp org-export-initial-scope) org-export-initial-scope
4421 (list org-export-initial-scope))
4422 org-export-dispatch-use-expert-ui))
4423 (raw-key (car input))
4424 (optns (cdr input)))
4425 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4426 ;; depending on user's key pressed.
4427 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
4428 ;; Allow to quit with "q" key.
4429 (?q nil)
4430 ;; Export with `e-ascii' back-end.
4431 ((?A ?N ?U)
4432 (org-e-ascii-export-as-ascii
4433 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4434 `(:ascii-charset ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8)))))
4435 ((?a ?n ?u)
4436 (org-e-ascii-export-to-ascii
4437 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4438 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
4439 ;; Export with `e-latex' back-end.
4440 (?L (org-e-latex-export-as-latex
4441 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4443 (org-e-latex-export-to-latex
4444 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4446 (org-e-latex-export-to-pdf
4447 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4449 (org-open-file
4450 (org-e-latex-export-to-pdf
4451 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4452 ;; Export with `e-html' back-end.
4454 (org-e-html-export-as-html
4455 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4457 (org-e-html-export-to-html
4458 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4460 (org-open-file
4461 (org-e-html-export-to-html
4462 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4463 ;; Export with `e-odt' back-end.
4465 (org-e-odt-export-to-odt
4466 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4468 (org-open-file
4469 (org-e-odt-export-to-odt
4470 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4471 ;; Publishing facilities
4473 (org-e-publish-current-file (memq 'force optns)))
4475 (org-e-publish-current-project (memq 'force optns)))
4477 (let ((project
4478 (assoc (org-icompleting-read
4479 "Publish project: " org-e-publish-project-alist nil t)
4480 org-e-publish-project-alist)))
4481 (org-e-publish project (memq 'force optns))))
4483 (org-e-publish-all (memq 'force optns)))
4484 ;; Undefined command.
4485 (t (error "No command associated with key %s"
4486 (char-to-string raw-key))))))
4488 (defun org-export-dispatch-ui (options expertp)
4489 "Handle interface for `org-export-dispatch'.
4491 OPTIONS is a list containing current interactive options set for
4492 export. It can contain any of the following symbols:
4493 `body' toggles a body-only export
4494 `subtree' restricts export to current subtree
4495 `visible' restricts export to visible part of buffer.
4496 `force' force publishing files.
4498 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4499 buffer is provided, but indications about currently active
4500 options are given in the prompt. Moreover, \[?] allows to switch
4501 back to standard interface.
4503 Return value is a list with key pressed as CAR and a list of
4504 final interactive export options as CDR."
4505 (let ((help
4506 (format "---- (Options) -------------------------------------------
4508 \[1] Body only: %s [2] Export scope: %s
4509 \[3] Visible only: %s [4] Force publishing: %s
4512 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4514 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4516 --- (HTML Export) ----------------------------------------
4518 \[h] to HTML file [b] ... and open it
4519 \[H] to temporary buffer
4521 --- (LaTeX Export) ---------------------------------------
4523 \[l] to TEX file [L] to temporary buffer
4524 \[p] to PDF file [d] ... and open it
4526 --- (ODF Export) -----------------------------------------
4528 \[o] to ODT file [O] ... and open it
4530 --- (Publish) --------------------------------------------
4532 \[F] current file [P] current project
4533 \[X] a project [E] every project"
4534 (if (memq 'body options) "On " "Off")
4535 (if (memq 'subtree options) "Subtree" "Buffer ")
4536 (if (memq 'visible options) "On " "Off")
4537 (if (memq 'force options) "On " "Off")))
4538 (standard-prompt "Export command: ")
4539 (expert-prompt (format "Export command (%s%s%s%s): "
4540 (if (memq 'body options) "b" "-")
4541 (if (memq 'subtree options) "s" "-")
4542 (if (memq 'visible options) "v" "-")
4543 (if (memq 'force options) "f" "-")))
4544 (handle-keypress
4545 (function
4546 ;; Read a character from command input, toggling interactive
4547 ;; options when applicable. PROMPT is the displayed prompt,
4548 ;; as a string.
4549 (lambda (prompt)
4550 (let ((key (read-char-exclusive prompt)))
4551 (cond
4552 ;; Ignore non-standard characters (i.e. "M-a").
4553 ((not (characterp key)) (org-export-dispatch-ui options expertp))
4554 ;; Help key: Switch back to standard interface if
4555 ;; expert UI was active.
4556 ((eq key ??) (org-export-dispatch-ui options nil))
4557 ;; Toggle export options.
4558 ((memq key '(?1 ?2 ?3 ?4))
4559 (org-export-dispatch-ui
4560 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
4561 (?4 'force))))
4562 (if (memq option options) (remq option options)
4563 (cons option options)))
4564 expertp))
4565 ;; Action selected: Send key and options back to
4566 ;; `org-export-dispatch'.
4567 (t (cons key options))))))))
4568 ;; With expert UI, just read key with a fancy prompt. In standard
4569 ;; UI, display an intrusive help buffer.
4570 (if expertp (funcall handle-keypress expert-prompt)
4571 (save-window-excursion
4572 (delete-other-windows)
4573 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
4574 (org-fit-window-to-buffer
4575 (get-buffer-window "*Org Export/Publishing Help*"))
4576 (funcall handle-keypress standard-prompt)))))
4579 (provide 'org-export)
4580 ;;; org-export.el ends here