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