org-e-freemind.el: Reverted accidental commit
[org-mode.git] / contrib / lisp / org-export.el
blob854e5f4719fc928655c240eb8e24d8d5d88d7f7a
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 and their value type.
978 ;; + `:author' :: Author's name.
979 ;; - category :: option
980 ;; - type :: string
982 ;; + `:back-end' :: Current back-end used for transcoding.
983 ;; - category :: tree
984 ;; - type :: symbol
986 ;; + `:creator' :: String to write as creation information.
987 ;; - category :: option
988 ;; - type :: string
990 ;; + `:date' :: String to use as date.
991 ;; - category :: option
992 ;; - type :: string
994 ;; + `:description' :: Description text for the current data.
995 ;; - category :: option
996 ;; - type :: string
998 ;; + `:email' :: Author's email.
999 ;; - category :: option
1000 ;; - type :: string
1002 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1003 ;; process.
1004 ;; - category :: option
1005 ;; - type :: list of strings
1007 ;; + `:exported-data' :: Hash table used for memoizing
1008 ;; `org-export-data'.
1009 ;; - category :: tree
1010 ;; - type :: hash table
1012 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1013 ;; their definition, as parsed data. Only non-inlined footnotes
1014 ;; are represented in this alist. Also, every definition isn't
1015 ;; guaranteed to be referenced in the parse tree. The purpose of
1016 ;; this property is to preserve definitions from oblivion
1017 ;; (i.e. when the parse tree comes from a part of the original
1018 ;; buffer), it isn't meant for direct use in a back-end. To
1019 ;; retrieve a definition relative to a reference, use
1020 ;; `org-export-get-footnote-definition' instead.
1021 ;; - category :: option
1022 ;; - type :: alist (STRING . LIST)
1024 ;; + `:headline-levels' :: Maximum level being exported as an
1025 ;; headline. Comparison is done with the relative level of
1026 ;; headlines in the parse tree, not necessarily with their
1027 ;; actual level.
1028 ;; - category :: option
1029 ;; - type :: integer
1031 ;; + `:headline-offset' :: Difference between relative and real level
1032 ;; of headlines in the parse tree. For example, a value of -1
1033 ;; means a level 2 headline should be considered as level
1034 ;; 1 (cf. `org-export-get-relative-level').
1035 ;; - category :: tree
1036 ;; - type :: integer
1038 ;; + `:headline-numbering' :: Alist between headlines and their
1039 ;; numbering, as a list of numbers
1040 ;; (cf. `org-export-get-headline-number').
1041 ;; - category :: tree
1042 ;; - type :: alist (INTEGER . LIST)
1044 ;; + `:id-alist' :: Alist between ID strings and destination file's
1045 ;; path, relative to current directory. It is used by
1046 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1047 ;; external file.
1048 ;; - category :: option
1049 ;; - type :: alist (STRING . STRING)
1051 ;; + `:ignore-list' :: List of elements and objects that should be
1052 ;; ignored during export.
1053 ;; - category :: tree
1054 ;; - type :: list of elements and objects
1056 ;; + `:input-file' :: Full path to input file, if any.
1057 ;; - category :: option
1058 ;; - type :: string or nil
1060 ;; + `:keywords' :: List of keywords attached to data.
1061 ;; - category :: option
1062 ;; - type :: string
1064 ;; + `:language' :: Default language used for translations.
1065 ;; - category :: option
1066 ;; - type :: string
1068 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1069 ;; transcoding.
1070 ;; - category :: option
1071 ;; - type :: list (as returned by `org-element-parse-buffer')
1073 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1074 ;; all line breaks.
1075 ;; - category :: option
1076 ;; - type :: symbol (nil, t)
1078 ;; + `:section-numbers' :: Non-nil means transcoding should add
1079 ;; section numbers to headlines.
1080 ;; - category :: option
1081 ;; - type :: symbol (nil, t)
1083 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1084 ;; in transcoding. When such a tag is present, subtrees without
1085 ;; it are de facto excluded from the process. See
1086 ;; `use-select-tags'.
1087 ;; - category :: option
1088 ;; - type :: list of strings
1090 ;; + `:target-list' :: List of targets encountered in the parse tree.
1091 ;; This is used to partly resolve "fuzzy" links
1092 ;; (cf. `org-export-resolve-fuzzy-link').
1093 ;; - category :: tree
1094 ;; - type :: list of strings
1096 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1097 ;; a time stamp in the output.
1098 ;; - category :: option
1099 ;; - type :: symbol (nil, t)
1101 ;; + `:translate-alist' :: Alist between element and object types and
1102 ;; transcoding functions relative to the current back-end.
1103 ;; Special keys `template' and `plain-text' are also possible.
1104 ;; - category :: option
1105 ;; - type :: alist (SYMBOL . FUNCTION)
1107 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1108 ;; also be transcoded. If it is set to the `headline' symbol,
1109 ;; only the archived headline's name is retained.
1110 ;; - category :: option
1111 ;; - type :: symbol (nil, t, `headline')
1113 ;; + `:with-author' :: Non-nil means author's name should be included
1114 ;; in the output.
1115 ;; - category :: option
1116 ;; - type :: symbol (nil, t)
1118 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1119 ;; - category :: option
1120 ;; - type :: symbol (nil, t)
1122 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1123 ;; inserted at the end of the transcoded string. If the value
1124 ;; is `comment', it should be commented.
1125 ;; - category :: option
1126 ;; - type :: symbol (`comment', nil, t)
1128 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1129 ;; its value is a list of names, only drawers with such names
1130 ;; will be transcoded.
1131 ;; - category :: option
1132 ;; - type :: symbol (nil, t) or list of strings
1134 ;; + `:with-email' :: Non-nil means output should contain author's
1135 ;; email.
1136 ;; - category :: option
1137 ;; - type :: symbol (nil, t)
1139 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1140 ;; interpreted.
1141 ;; - category :: option
1142 ;; - type :: symbol (nil, t)
1144 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1145 ;; strings starting with a colon as a fixed-with (verbatim) area.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1150 ;; footnotes.
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-plannings' :: Non-nil means transcoding should include
1155 ;; planning info.
1156 ;; - category :: option
1157 ;; - type :: symbol (nil, t)
1159 ;; + `:with-priority' :: Non-nil means transcoding should include
1160 ;; priority cookies.
1161 ;; - category :: option
1162 ;; - type :: symbol (nil, t)
1164 ;; + `:with-special-strings' :: Non-nil means transcoding should
1165 ;; interpret special strings in plain text.
1166 ;; - category :: option
1167 ;; - type :: symbol (nil, t)
1169 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1170 ;; interpret subscript and superscript. With a value of "{}",
1171 ;; only interpret those using curly brackets.
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, {}, t)
1175 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1176 ;; tables.
1177 ;; - category :: option
1178 ;; - type :: symbol (nil, t)
1180 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1181 ;; headlines. A `not-in-toc' value will remove them from the
1182 ;; table of contents, if any, nonetheless.
1183 ;; - category :: option
1184 ;; - type :: symbol (nil, t, `not-in-toc')
1186 ;; + `:with-tasks' :: Non-nil means transcoding should include
1187 ;; headlines with a TODO keyword. A `todo' value will only
1188 ;; include headlines with a todo type keyword while a `done'
1189 ;; value will do the contrary. If a list of strings is provided,
1190 ;; only tasks with keywords belonging to that list will be kept.
1191 ;; - category :: option
1192 ;; - type :: symbol (t, todo, done, nil) or list of strings
1194 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1195 ;; time stamps. Special value `active' (resp. `inactive') ask to
1196 ;; export only active (resp. inactive) timestamps. Otherwise,
1197 ;; completely remove them.
1198 ;; - category :: option
1199 ;; - type :: symbol: (`active', `inactive', t, nil)
1201 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1202 ;; added to the output. An integer value limits its depth.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, t or integer)
1206 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1207 ;; include TODO keywords.
1208 ;; - category :: option
1209 ;; - type :: symbol (nil, t)
1212 ;;;; Environment Options
1214 ;; Environment options encompass all parameters defined outside the
1215 ;; scope of the parsed data. They come from five sources, in
1216 ;; increasing precedence order:
1218 ;; - Global variables,
1219 ;; - Buffer's attributes,
1220 ;; - Options keyword symbols,
1221 ;; - Buffer keywords,
1222 ;; - Subtree properties.
1224 ;; The central internal function with regards to environment options
1225 ;; is `org-export-get-environment'. It updates global variables with
1226 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1227 ;; the different sources.
1229 ;; The internal functions doing the retrieval are:
1230 ;; `org-export--get-global-options',
1231 ;; `org-export--get-buffer-attributes',
1232 ;; `org-export--parse-option-keyword',
1233 ;; `org-export--get-subtree-options' and
1234 ;; `org-export--get-inbuffer-options'
1236 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1237 ;; take care of the part relative to "#+BIND:" keywords.
1239 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1240 "Collect export options from the current buffer.
1242 Optional argument BACKEND is a symbol specifying which back-end
1243 specific options to read, if any.
1245 When optional argument SUBTREEP is non-nil, assume the export is
1246 done against the current sub-tree.
1248 Third optional argument EXT-PLIST is a property list with
1249 external parameters overriding Org default settings, but still
1250 inferior to file-local settings."
1251 ;; First install #+BIND variables.
1252 (org-export--install-letbind-maybe)
1253 ;; Get and prioritize export options...
1254 (org-combine-plists
1255 ;; ... from global variables...
1256 (org-export--get-global-options backend)
1257 ;; ... from buffer's attributes...
1258 (org-export--get-buffer-attributes)
1259 ;; ... from an external property list...
1260 ext-plist
1261 ;; ... from in-buffer settings...
1262 (org-export--get-inbuffer-options
1263 backend
1264 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1265 ;; ... and from subtree, when appropriate.
1266 (and subtreep (org-export--get-subtree-options backend))
1267 ;; Eventually install back-end symbol and its translation table.
1268 `(:back-end
1269 ,backend
1270 :translate-alist
1271 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1272 (when (boundp trans-alist) (symbol-value trans-alist))))))
1274 (defun org-export--parse-option-keyword (options &optional backend)
1275 "Parse an OPTIONS line and return values as a plist.
1276 Optional argument BACKEND is a symbol specifying which back-end
1277 specific items to read, if any."
1278 (let* ((all
1279 (append org-export-options-alist
1280 (and backend
1281 (let ((var (intern
1282 (format "org-%s-options-alist" backend))))
1283 (and (boundp var) (eval var))))))
1284 ;; Build an alist between #+OPTION: item and property-name.
1285 (alist (delq nil
1286 (mapcar (lambda (e)
1287 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1288 (car e))))
1289 all)))
1290 plist)
1291 (mapc (lambda (e)
1292 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1293 (car e)
1294 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1295 options)
1296 (setq plist (plist-put plist
1297 (cdr e)
1298 (car (read-from-string
1299 (match-string 2 options)))))))
1300 alist)
1301 plist))
1303 (defun org-export--get-subtree-options (&optional backend)
1304 "Get export options in subtree at point.
1305 Optional argument BACKEND is a symbol specifying back-end used
1306 for export. Return options as a plist."
1307 ;; For each buffer keyword, create an headline property setting the
1308 ;; same property in communication channel. The name for the property
1309 ;; is the keyword with "EXPORT_" appended to it.
1310 (org-with-wide-buffer
1311 (let (prop plist)
1312 ;; Make sure point is at an heading.
1313 (unless (org-at-heading-p) (org-back-to-heading t))
1314 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1315 ;; title as its fallback value.
1316 (when (setq prop (progn (looking-at org-todo-line-regexp)
1317 (or (save-match-data
1318 (org-entry-get (point) "EXPORT_TITLE"))
1319 (org-match-string-no-properties 3))))
1320 (setq plist
1321 (plist-put
1322 plist :title
1323 (org-element-parse-secondary-string
1324 prop (org-element-restriction 'keyword)))))
1325 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1326 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1327 (setq plist
1328 (nconc plist (org-export--parse-option-keyword prop backend))))
1329 ;; Handle other keywords.
1330 (let ((seen '("TITLE")))
1331 (mapc
1332 (lambda (option)
1333 (let ((property (nth 1 option)))
1334 (when (and property (not (member property seen)))
1335 (let* ((subtree-prop (concat "EXPORT_" property))
1336 ;; Export properties are not case-sensitive.
1337 (value (let ((case-fold-search t))
1338 (org-entry-get (point) subtree-prop))))
1339 (push property seen)
1340 (when value
1341 (setq plist
1342 (plist-put
1343 plist
1344 (car option)
1345 ;; Parse VALUE if required.
1346 (if (member property org-element-parsed-keywords)
1347 (org-element-parse-secondary-string
1348 value (org-element-restriction 'keyword))
1349 value))))))))
1350 ;; Also look for both general keywords and back-end specific
1351 ;; options if BACKEND is provided.
1352 (append (and backend
1353 (let ((var (intern
1354 (format "org-%s-options-alist" backend))))
1355 (and (boundp var) (symbol-value var))))
1356 org-export-options-alist)))
1357 ;; Return value.
1358 plist)))
1360 (defun org-export--get-inbuffer-options (&optional backend files)
1361 "Return current buffer export options, as a plist.
1363 Optional argument BACKEND, when non-nil, is a symbol specifying
1364 which back-end specific options should also be read in the
1365 process.
1367 Optional argument FILES is a list of setup files names read so
1368 far, used to avoid circular dependencies.
1370 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1371 (org-with-wide-buffer
1372 (goto-char (point-min))
1373 (let ((case-fold-search t) plist)
1374 ;; 1. Special keywords, as in `org-export-special-keywords'.
1375 (let ((special-re
1376 (format "^[ \t]*#\\+%s:" (regexp-opt 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 (format "^[ \t]*#\\+%s:"
1415 (regexp-opt
1416 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1417 (goto-char (point-min))
1418 (while (re-search-forward opt-re nil t)
1419 (let ((element (org-element-at-point)))
1420 (when (eq (org-element-type element) 'keyword)
1421 (let* ((key (org-element-property :key element))
1422 (val (org-element-property :value element))
1423 (prop (cdr (assoc key alist)))
1424 (behaviour (nth 4 (assq prop all))))
1425 (setq plist
1426 (plist-put
1427 plist prop
1428 ;; Handle value depending on specified BEHAVIOUR.
1429 (case behaviour
1430 (space
1431 (if (not (plist-get plist prop)) (org-trim val)
1432 (concat (plist-get plist prop) " " (org-trim val))))
1433 (newline
1434 (org-trim
1435 (concat (plist-get plist prop) "\n" (org-trim val))))
1436 (split
1437 `(,@(plist-get plist prop) ,@(org-split-string val)))
1438 ('t val)
1439 (otherwise (if (not (plist-member plist prop)) val
1440 (plist-get plist prop))))))))))
1441 ;; Parse keywords specified in `org-element-parsed-keywords'.
1442 (mapc
1443 (lambda (key)
1444 (let* ((prop (cdr (assoc key alist)))
1445 (value (and prop (plist-get plist prop))))
1446 (when (stringp value)
1447 (setq plist
1448 (plist-put
1449 plist prop
1450 (org-element-parse-secondary-string
1451 value (org-element-restriction 'keyword)))))))
1452 org-element-parsed-keywords))
1453 ;; 3. Return final value.
1454 plist)))
1456 (defun org-export--get-buffer-attributes ()
1457 "Return properties related to buffer attributes, as a plist."
1458 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1459 (list
1460 ;; Store full path of input file name, or nil. For internal use.
1461 :input-file visited-file
1462 :title (or (and visited-file
1463 (file-name-sans-extension
1464 (file-name-nondirectory visited-file)))
1465 (buffer-name (buffer-base-buffer)))
1466 :footnote-definition-alist
1467 ;; Footnotes definitions must be collected in the original
1468 ;; buffer, as there's no insurance that they will still be in the
1469 ;; parse tree, due to possible narrowing.
1470 (let (alist)
1471 (org-with-wide-buffer
1472 (goto-char (point-min))
1473 (while (re-search-forward org-footnote-definition-re nil t)
1474 (let ((def (org-footnote-at-definition-p)))
1475 (when def
1476 (org-skip-whitespace)
1477 (push (cons (car def)
1478 (save-restriction
1479 (narrow-to-region (point) (nth 2 def))
1480 ;; Like `org-element-parse-buffer', but
1481 ;; makes sure the definition doesn't start
1482 ;; with a section element.
1483 (org-element--parse-elements
1484 (point-min) (point-max) nil nil nil nil
1485 (list 'org-data nil))))
1486 alist))))
1487 alist))
1488 :id-alist
1489 ;; Collect id references.
1490 (let (alist)
1491 (org-with-wide-buffer
1492 (goto-char (point-min))
1493 (while (re-search-forward
1494 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1495 (let* ((id (org-match-string-no-properties 1))
1496 (file (org-id-find-id-file id)))
1497 (when file (push (cons id (file-relative-name file)) alist)))))
1498 alist))))
1500 (defun org-export--get-global-options (&optional backend)
1501 "Return global export options as a plist.
1503 Optional argument BACKEND, if non-nil, is a symbol specifying
1504 which back-end specific export options should also be read in the
1505 process."
1506 (let ((all (append org-export-options-alist
1507 (and backend
1508 (let ((var (intern
1509 (format "org-%s-options-alist" backend))))
1510 (and (boundp var) (symbol-value var))))))
1511 ;; Output value.
1512 plist)
1513 (mapc
1514 (lambda (cell)
1515 (setq plist
1516 (plist-put
1517 plist
1518 (car cell)
1519 ;; Eval default value provided. If keyword is a member
1520 ;; of `org-element-parsed-keywords', parse it as
1521 ;; a secondary string before storing it.
1522 (let ((value (eval (nth 3 cell))))
1523 (if (not (stringp value)) value
1524 (let ((keyword (nth 1 cell)))
1525 (if (not (member keyword org-element-parsed-keywords)) value
1526 (org-element-parse-secondary-string
1527 value (org-element-restriction 'keyword)))))))))
1528 all)
1529 ;; Return value.
1530 plist))
1532 (defvar org-export--allow-BIND-local nil)
1533 (defun org-export--confirm-letbind ()
1534 "Can we use #+BIND values during export?
1535 By default this will ask for confirmation by the user, to divert
1536 possible security risks."
1537 (cond
1538 ((not org-export-allow-BIND) nil)
1539 ((eq org-export-allow-BIND t) t)
1540 ((local-variable-p 'org-export--allow-BIND-local)
1541 org-export--allow-BIND-local)
1542 (t (org-set-local 'org-export--allow-BIND-local
1543 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1545 (defun org-export--install-letbind-maybe ()
1546 "Install the values from #+BIND lines as local variables.
1547 Variables must be installed before in-buffer options are
1548 retrieved."
1549 (let ((case-fold-search t) letbind pair)
1550 (org-with-wide-buffer
1551 (goto-char (point-min))
1552 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1553 (let* ((element (org-element-at-point))
1554 (value (org-element-property :value element)))
1555 (when (and (eq (org-element-type element) 'keyword)
1556 (not (equal value ""))
1557 (org-export--confirm-letbind))
1558 (push (read (format "(%s)" value)) letbind)))))
1559 (dolist (pair (nreverse letbind))
1560 (org-set-local (car pair) (nth 1 pair)))))
1563 ;;;; Tree Properties
1565 ;; Tree properties are infromation extracted from parse tree. They
1566 ;; are initialized at the beginning of the transcoding process by
1567 ;; `org-export-collect-tree-properties'.
1569 ;; Dedicated functions focus on computing the value of specific tree
1570 ;; properties during initialization. Thus,
1571 ;; `org-export--populate-ignore-list' lists elements and objects that
1572 ;; should be skipped during export, `org-export--get-min-level' gets
1573 ;; the minimal exportable level, used as a basis to compute relative
1574 ;; level for headlines. Eventually
1575 ;; `org-export--collect-headline-numbering' builds an alist between
1576 ;; headlines and their numbering.
1578 (defun org-export-collect-tree-properties (data info)
1579 "Extract tree properties from parse tree.
1581 DATA is the parse tree from which information is retrieved. INFO
1582 is a list holding export options.
1584 Following tree properties are set or updated:
1586 `:exported-data' Hash table used to memoize results from
1587 `org-export-data'.
1589 `:footnote-definition-alist' List of footnotes definitions in
1590 original buffer and current parse tree.
1592 `:headline-offset' Offset between true level of headlines and
1593 local level. An offset of -1 means an headline
1594 of level 2 should be considered as a level
1595 1 headline in the context.
1597 `:headline-numbering' Alist of all headlines as key an the
1598 associated numbering as value.
1600 `:ignore-list' List of elements that should be ignored during
1601 export.
1603 `:target-list' List of all targets in the parse tree.
1605 Return updated plist."
1606 ;; Install the parse tree in the communication channel, in order to
1607 ;; use `org-export-get-genealogy' and al.
1608 (setq info (plist-put info :parse-tree data))
1609 ;; Get the list of elements and objects to ignore, and put it into
1610 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1611 ;; been done during parse tree filtering.
1612 (setq info
1613 (plist-put info
1614 :ignore-list
1615 (append (org-export--populate-ignore-list data info)
1616 (plist-get info :ignore-list))))
1617 ;; Compute `:headline-offset' in order to be able to use
1618 ;; `org-export-get-relative-level'.
1619 (setq info
1620 (plist-put info
1621 :headline-offset
1622 (- 1 (org-export--get-min-level data info))))
1623 ;; Update footnotes definitions list with definitions in parse tree.
1624 ;; This is required since buffer expansion might have modified
1625 ;; boundaries of footnote definitions contained in the parse tree.
1626 ;; This way, definitions in `footnote-definition-alist' are bound to
1627 ;; match those in the parse tree.
1628 (let ((defs (plist-get info :footnote-definition-alist)))
1629 (org-element-map
1630 data 'footnote-definition
1631 (lambda (fn)
1632 (push (cons (org-element-property :label fn)
1633 `(org-data nil ,@(org-element-contents fn)))
1634 defs)))
1635 (setq info (plist-put info :footnote-definition-alist defs)))
1636 ;; Properties order doesn't matter: get the rest of the tree
1637 ;; properties.
1638 (nconc
1639 `(:target-list
1640 ,(org-element-map
1641 data '(keyword target)
1642 (lambda (blob)
1643 (when (or (eq (org-element-type blob) 'target)
1644 (string= (org-element-property :key blob) "TARGET"))
1645 blob)) info)
1646 :headline-numbering ,(org-export--collect-headline-numbering data info)
1647 :exported-data ,(make-hash-table :test 'eq :size 4001))
1648 info))
1650 (defun org-export--get-min-level (data options)
1651 "Return minimum exportable headline's level in DATA.
1652 DATA is parsed tree as returned by `org-element-parse-buffer'.
1653 OPTIONS is a plist holding export options."
1654 (catch 'exit
1655 (let ((min-level 10000))
1656 (mapc
1657 (lambda (blob)
1658 (when (and (eq (org-element-type blob) 'headline)
1659 (not (memq blob (plist-get options :ignore-list))))
1660 (setq min-level
1661 (min (org-element-property :level blob) min-level)))
1662 (when (= min-level 1) (throw 'exit 1)))
1663 (org-element-contents data))
1664 ;; If no headline was found, for the sake of consistency, set
1665 ;; minimum level to 1 nonetheless.
1666 (if (= min-level 10000) 1 min-level))))
1668 (defun org-export--collect-headline-numbering (data options)
1669 "Return numbering of all exportable headlines in a parse tree.
1671 DATA is the parse tree. OPTIONS is the plist holding export
1672 options.
1674 Return an alist whose key is an headline and value is its
1675 associated numbering \(in the shape of a list of numbers\)."
1676 (let ((numbering (make-vector org-export-max-depth 0)))
1677 (org-element-map
1678 data
1679 'headline
1680 (lambda (headline)
1681 (let ((relative-level
1682 (1- (org-export-get-relative-level headline options))))
1683 (cons
1684 headline
1685 (loop for n across numbering
1686 for idx from 0 to org-export-max-depth
1687 when (< idx relative-level) collect n
1688 when (= idx relative-level) collect (aset numbering idx (1+ n))
1689 when (> idx relative-level) do (aset numbering idx 0)))))
1690 options)))
1692 (defun org-export--populate-ignore-list (data options)
1693 "Return list of elements and objects to ignore during export.
1694 DATA is the parse tree to traverse. OPTIONS is the plist holding
1695 export options."
1696 (let* (ignore
1697 walk-data
1698 ;; First find trees containing a select tag, if any.
1699 (selected (org-export--selected-trees data options))
1700 (walk-data
1701 (lambda (data)
1702 ;; Collect ignored elements or objects into IGNORE-LIST.
1703 (let ((type (org-element-type data)))
1704 (if (org-export--skip-p data options selected) (push data ignore)
1705 (if (and (eq type 'headline)
1706 (eq (plist-get options :with-archived-trees) 'headline)
1707 (org-element-property :archivedp data))
1708 ;; If headline is archived but tree below has
1709 ;; to be skipped, add it to ignore list.
1710 (mapc (lambda (e) (push e ignore))
1711 (org-element-contents data))
1712 ;; Move into secondary string, if any.
1713 (let ((sec-prop
1714 (cdr (assq type org-element-secondary-value-alist))))
1715 (when sec-prop
1716 (mapc walk-data (org-element-property sec-prop data))))
1717 ;; Move into recursive objects/elements.
1718 (mapc walk-data (org-element-contents data))))))))
1719 ;; Main call.
1720 (funcall walk-data data)
1721 ;; Return value.
1722 ignore))
1724 (defun org-export--selected-trees (data info)
1725 "Return list of headlines containing a select tag in their tree.
1726 DATA is parsed data as returned by `org-element-parse-buffer'.
1727 INFO is a plist holding export options."
1728 (let* (selected-trees
1729 walk-data ; for byte-compiler.
1730 (walk-data
1731 (function
1732 (lambda (data genealogy)
1733 (case (org-element-type data)
1734 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1735 (org-element-contents data)))
1736 (headline
1737 (let ((tags (org-element-property :tags data)))
1738 (if (loop for tag in (plist-get info :select-tags)
1739 thereis (member tag tags))
1740 ;; When a select tag is found, mark full
1741 ;; genealogy and every headline within the tree
1742 ;; as acceptable.
1743 (setq selected-trees
1744 (append
1745 genealogy
1746 (org-element-map data 'headline 'identity)
1747 selected-trees))
1748 ;; Else, continue searching in tree, recursively.
1749 (mapc
1750 (lambda (el) (funcall walk-data el (cons data genealogy)))
1751 (org-element-contents data))))))))))
1752 (funcall walk-data data nil) selected-trees))
1754 (defun org-export--skip-p (blob options selected)
1755 "Non-nil when element or object BLOB should be skipped during export.
1756 OPTIONS is the plist holding export options. SELECTED, when
1757 non-nil, is a list of headlines belonging to a tree with a select
1758 tag."
1759 (case (org-element-type blob)
1760 (clock (not (plist-get options :with-clocks)))
1761 (drawer
1762 (or (not (plist-get options :with-drawers))
1763 (and (consp (plist-get options :with-drawers))
1764 (not (member (org-element-property :drawer-name blob)
1765 (plist-get options :with-drawers))))))
1766 (headline
1767 (let ((with-tasks (plist-get options :with-tasks))
1768 (todo (org-element-property :todo-keyword blob))
1769 (todo-type (org-element-property :todo-type blob))
1770 (archived (plist-get options :with-archived-trees))
1771 (tags (org-element-property :tags blob)))
1773 ;; Ignore subtrees with an exclude tag.
1774 (loop for k in (plist-get options :exclude-tags)
1775 thereis (member k tags))
1776 ;; When a select tag is present in the buffer, ignore any tree
1777 ;; without it.
1778 (and selected (not (memq blob selected)))
1779 ;; Ignore commented sub-trees.
1780 (org-element-property :commentedp blob)
1781 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1782 (and (not archived) (org-element-property :archivedp blob))
1783 ;; Ignore tasks, if specified by `:with-tasks' property.
1784 (and todo
1785 (or (not with-tasks)
1786 (and (memq with-tasks '(todo done))
1787 (not (eq todo-type with-tasks)))
1788 (and (consp with-tasks) (not (member todo with-tasks))))))))
1789 (inlinetask (not (plist-get options :with-inlinetasks)))
1790 (planning (not (plist-get options :with-plannings)))
1791 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1792 (table-cell
1793 (and (org-export-table-has-special-column-p
1794 (org-export-get-parent-table blob))
1795 (not (org-export-get-previous-element blob options))))
1796 (table-row (org-export-table-row-is-special-p blob options))
1797 (timestamp
1798 (case (plist-get options :with-timestamps)
1799 ;; No timestamp allowed.
1800 ('nil t)
1801 ;; Only active timestamps allowed and the current one isn't
1802 ;; active.
1803 (active
1804 (not (memq (org-element-property :type blob)
1805 '(active active-range))))
1806 ;; Only inactive timestamps allowed and the current one isn't
1807 ;; inactive.
1808 (inactive
1809 (not (memq (org-element-property :type blob)
1810 '(inactive inactive-range))))))))
1814 ;;; The Transcoder
1816 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1817 ;; `org-element-parse-buffer') and transcodes it into a specified
1818 ;; back-end output. It takes care of filtering out elements or
1819 ;; objects according to export options and organizing the output blank
1820 ;; lines and white space are preserved. The function memoizes its
1821 ;; results, so it is cheap to call it within translators.
1823 ;; Internally, three functions handle the filtering of objects and
1824 ;; elements during the export. In particular,
1825 ;; `org-export-ignore-element' marks an element or object so future
1826 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1827 ;; elements or objects should be seen as real Org syntax and
1828 ;; `org-export-expand' transforms the others back into their original
1829 ;; shape
1831 ;; `org-export-transcoder' is an accessor returning appropriate
1832 ;; translator function for a given element or object.
1834 (defun org-export-transcoder (blob info)
1835 "Return appropriate transcoder for BLOB.
1836 INFO is a plist containing export directives."
1837 (let ((type (org-element-type blob)))
1838 ;; Return contents only for complete parse trees.
1839 (if (eq type 'org-data) (lambda (blob contents info) contents)
1840 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1841 (and (functionp transcoder) transcoder)))))
1843 (defun org-export-data (data info)
1844 "Convert DATA into current back-end format.
1846 DATA is a parse tree, an element or an object or a secondary
1847 string. INFO is a plist holding export options.
1849 Return transcoded string."
1850 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1851 (if (not (eq memo 'no-memo)) memo
1852 (let* ((type (org-element-type data))
1853 (results
1854 (cond
1855 ;; Ignored element/object.
1856 ((memq data (plist-get info :ignore-list)) nil)
1857 ;; Plain text.
1858 ((eq type 'plain-text)
1859 (org-export-filter-apply-functions
1860 (plist-get info :filter-plain-text)
1861 (let ((transcoder (org-export-transcoder data info)))
1862 (if transcoder (funcall transcoder data info) data))
1863 info))
1864 ;; Uninterpreted element/object: change it back to Org
1865 ;; syntax and export again resulting raw string.
1866 ((not (org-export--interpret-p data info))
1867 (org-export-data
1868 (org-export-expand
1869 data
1870 (mapconcat (lambda (blob) (org-export-data blob info))
1871 (org-element-contents data)
1872 ""))
1873 info))
1874 ;; Secondary string.
1875 ((not type)
1876 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1877 ;; Element/Object without contents or, as a special case,
1878 ;; headline with archive tag and archived trees restricted
1879 ;; to title only.
1880 ((or (not (org-element-contents data))
1881 (and (eq type 'headline)
1882 (eq (plist-get info :with-archived-trees) 'headline)
1883 (org-element-property :archivedp data)))
1884 (let ((transcoder (org-export-transcoder data info)))
1885 (and (functionp transcoder)
1886 (funcall transcoder data nil info))))
1887 ;; Element/Object with contents.
1889 (let ((transcoder (org-export-transcoder data info)))
1890 (when transcoder
1891 (let* ((greaterp (memq type org-element-greater-elements))
1892 (objectp
1893 (and (not greaterp)
1894 (memq type org-element-recursive-objects)))
1895 (contents
1896 (mapconcat
1897 (lambda (element) (org-export-data element info))
1898 (org-element-contents
1899 (if (or greaterp objectp) data
1900 ;; Elements directly containing objects
1901 ;; must have their indentation normalized
1902 ;; first.
1903 (org-element-normalize-contents
1904 data
1905 ;; When normalizing contents of the first
1906 ;; paragraph in an item or a footnote
1907 ;; definition, ignore first line's
1908 ;; indentation: there is none and it
1909 ;; might be misleading.
1910 (when (eq type 'paragraph)
1911 (let ((parent (org-export-get-parent data)))
1912 (and
1913 (eq (car (org-element-contents parent))
1914 data)
1915 (memq (org-element-type parent)
1916 '(footnote-definition item))))))))
1917 "")))
1918 (funcall transcoder data
1919 (if (not greaterp) contents
1920 (org-element-normalize-string contents))
1921 info))))))))
1922 ;; Final result will be memoized before being returned.
1923 (puthash
1924 data
1925 (cond
1926 ((not results) nil)
1927 ((memq type '(org-data plain-text nil)) results)
1928 ;; Append the same white space between elements or objects as in
1929 ;; the original buffer, and call appropriate filters.
1931 (let ((results
1932 (org-export-filter-apply-functions
1933 (plist-get info (intern (format ":filter-%s" type)))
1934 (let ((post-blank (or (org-element-property :post-blank data)
1935 0)))
1936 (if (memq type org-element-all-elements)
1937 (concat (org-element-normalize-string results)
1938 (make-string post-blank ?\n))
1939 (concat results (make-string post-blank ? ))))
1940 info)))
1941 results)))
1942 (plist-get info :exported-data))))))
1944 (defun org-export--interpret-p (blob info)
1945 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1946 Check is done according to export options INFO, stored as
1947 a plist."
1948 (case (org-element-type blob)
1949 ;; ... entities...
1950 (entity (plist-get info :with-entities))
1951 ;; ... emphasis...
1952 (emphasis (plist-get info :with-emphasize))
1953 ;; ... fixed-width areas.
1954 (fixed-width (plist-get info :with-fixed-width))
1955 ;; ... footnotes...
1956 ((footnote-definition footnote-reference)
1957 (plist-get info :with-footnotes))
1958 ;; ... sub/superscripts...
1959 ((subscript superscript)
1960 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1961 (if (eq sub/super-p '{})
1962 (org-element-property :use-brackets-p blob)
1963 sub/super-p)))
1964 ;; ... tables...
1965 (table (plist-get info :with-tables))
1966 (otherwise t)))
1968 (defun org-export-expand (blob contents)
1969 "Expand a parsed element or object to its original state.
1970 BLOB is either an element or an object. CONTENTS is its
1971 contents, as a string or nil."
1972 (funcall
1973 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
1974 blob contents))
1976 (defun org-export-ignore-element (element info)
1977 "Add ELEMENT to `:ignore-list' in INFO.
1979 Any element in `:ignore-list' will be skipped when using
1980 `org-element-map'. INFO is modified by side effects."
1981 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
1985 ;;; The Filter System
1987 ;; Filters allow end-users to tweak easily the transcoded output.
1988 ;; They are the functional counterpart of hooks, as every filter in
1989 ;; a set is applied to the return value of the previous one.
1991 ;; Every set is back-end agnostic. Although, a filter is always
1992 ;; called, in addition to the string it applies to, with the back-end
1993 ;; used as argument, so it's easy for the end-user to add back-end
1994 ;; specific filters in the set. The communication channel, as
1995 ;; a plist, is required as the third argument.
1997 ;; From the developer side, filters sets can be installed in the
1998 ;; process with the help of `org-export-define-backend', which
1999 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2000 ;; association has a key among the following symbols and a function or
2001 ;; a list of functions as value.
2003 ;; - `:filter-parse-tree' applies directly on the complete parsed
2004 ;; tree. It's the only filters set that doesn't apply to a string.
2005 ;; Users can set it through `org-export-filter-parse-tree-functions'
2006 ;; variable.
2008 ;; - `:filter-final-output' applies to the final transcoded string.
2009 ;; Users can set it with `org-export-filter-final-output-functions'
2010 ;; variable
2012 ;; - `:filter-plain-text' applies to any string not recognized as Org
2013 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2014 ;; configure it.
2016 ;; - `:filter-TYPE' applies on the string returned after an element or
2017 ;; object of type TYPE has been transcoded. An user can modify
2018 ;; `org-export-filter-TYPE-functions'
2020 ;; All filters sets are applied with
2021 ;; `org-export-filter-apply-functions' function. Filters in a set are
2022 ;; applied in a LIFO fashion. It allows developers to be sure that
2023 ;; their filters will be applied first.
2025 ;; Filters properties are installed in communication channel with
2026 ;; `org-export-install-filters' function.
2028 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2029 ;; before parsing to allow for heavy structure modifications.
2032 ;;;; Before Parsing Hook
2034 (defvar org-export-before-parsing-hook nil
2035 "Hook run before parsing an export buffer.
2037 This is run after include keywords have been expanded and Babel
2038 code executed, on a copy of original buffer's area being
2039 exported. Visibility is the same as in the original one. Point
2040 is left at the beginning of the new one.
2042 Every function in this hook will be called with one argument: the
2043 back-end currently used, as a symbol.")
2046 ;;;; Special Filters
2048 (defvar org-export-filter-parse-tree-functions nil
2049 "List of functions applied to the parsed tree.
2050 Each filter is called with three arguments: the parse tree, as
2051 returned by `org-element-parse-buffer', the back-end, as
2052 a symbol, and the communication channel, as a plist. It must
2053 return the modified parse tree to transcode.")
2055 (defvar org-export-filter-final-output-functions nil
2056 "List of functions applied to the transcoded string.
2057 Each filter is called with three arguments: the full transcoded
2058 string, the back-end, as a symbol, and the communication channel,
2059 as a plist. It must return a string that will be used as the
2060 final export output.")
2062 (defvar org-export-filter-plain-text-functions nil
2063 "List of functions applied to plain text.
2064 Each filter is called with three arguments: a string which
2065 contains no Org syntax, the back-end, as a symbol, and the
2066 communication channel, as a plist. It must return a string or
2067 nil.")
2070 ;;;; Elements Filters
2072 (defvar org-export-filter-center-block-functions nil
2073 "List of functions applied to a transcoded center block.
2074 Each filter is called with three arguments: the transcoded data,
2075 as a string, the back-end, as a symbol, and the communication
2076 channel, as a plist. It must return a string or nil.")
2078 (defvar org-export-filter-clock-functions nil
2079 "List of functions applied to a transcoded clock.
2080 Each filter is called with three arguments: the transcoded data,
2081 as a string, the back-end, as a symbol, and the communication
2082 channel, as a plist. It must return a string or nil.")
2084 (defvar org-export-filter-drawer-functions nil
2085 "List of functions applied to a transcoded drawer.
2086 Each filter is called with three arguments: the transcoded data,
2087 as a string, the back-end, as a symbol, and the communication
2088 channel, as a plist. It must return a string or nil.")
2090 (defvar org-export-filter-dynamic-block-functions nil
2091 "List of functions applied to a transcoded dynamic-block.
2092 Each filter is called with three arguments: the transcoded data,
2093 as a string, the back-end, as a symbol, and the communication
2094 channel, as a plist. It must return a string or nil.")
2096 (defvar org-export-filter-headline-functions nil
2097 "List of functions applied to a transcoded headline.
2098 Each filter is called with three arguments: the transcoded data,
2099 as a string, the back-end, as a symbol, and the communication
2100 channel, as a plist. It must return a string or nil.")
2102 (defvar org-export-filter-inlinetask-functions nil
2103 "List of functions applied to a transcoded inlinetask.
2104 Each filter is called with three arguments: the transcoded data,
2105 as a string, the back-end, as a symbol, and the communication
2106 channel, as a plist. It must return a string or nil.")
2108 (defvar org-export-filter-plain-list-functions nil
2109 "List of functions applied to a transcoded plain-list.
2110 Each filter is called with three arguments: the transcoded data,
2111 as a string, the back-end, as a symbol, and the communication
2112 channel, as a plist. It must return a string or nil.")
2114 (defvar org-export-filter-item-functions nil
2115 "List of functions applied to a transcoded item.
2116 Each filter is called with three arguments: the transcoded data,
2117 as a string, the back-end, as a symbol, and the communication
2118 channel, as a plist. It must return a string or nil.")
2120 (defvar org-export-filter-comment-functions nil
2121 "List of functions applied to a transcoded comment.
2122 Each filter is called with three arguments: the transcoded data,
2123 as a string, the back-end, as a symbol, and the communication
2124 channel, as a plist. It must return a string or nil.")
2126 (defvar org-export-filter-comment-block-functions nil
2127 "List of functions applied to a transcoded comment-comment.
2128 Each filter is called with three arguments: the transcoded data,
2129 as a string, the back-end, as a symbol, and the communication
2130 channel, as a plist. It must return a string or nil.")
2132 (defvar org-export-filter-example-block-functions nil
2133 "List of functions applied to a transcoded example-block.
2134 Each filter is called with three arguments: the transcoded data,
2135 as a string, the back-end, as a symbol, and the communication
2136 channel, as a plist. It must return a string or nil.")
2138 (defvar org-export-filter-export-block-functions nil
2139 "List of functions applied to a transcoded export-block.
2140 Each filter is called with three arguments: the transcoded data,
2141 as a string, the back-end, as a symbol, and the communication
2142 channel, as a plist. It must return a string or nil.")
2144 (defvar org-export-filter-fixed-width-functions nil
2145 "List of functions applied to a transcoded fixed-width.
2146 Each filter is called with three arguments: the transcoded data,
2147 as a string, the back-end, as a symbol, and the communication
2148 channel, as a plist. It must return a string or nil.")
2150 (defvar org-export-filter-footnote-definition-functions nil
2151 "List of functions applied to a transcoded footnote-definition.
2152 Each filter is called with three arguments: the transcoded data,
2153 as a string, the back-end, as a symbol, and the communication
2154 channel, as a plist. It must return a string or nil.")
2156 (defvar org-export-filter-horizontal-rule-functions nil
2157 "List of functions applied to a transcoded horizontal-rule.
2158 Each filter is called with three arguments: the transcoded data,
2159 as a string, the back-end, as a symbol, and the communication
2160 channel, as a plist. It must return a string or nil.")
2162 (defvar org-export-filter-keyword-functions nil
2163 "List of functions applied to a transcoded keyword.
2164 Each filter is called with three arguments: the transcoded data,
2165 as a string, the back-end, as a symbol, and the communication
2166 channel, as a plist. It must return a string or nil.")
2168 (defvar org-export-filter-latex-environment-functions nil
2169 "List of functions applied to a transcoded latex-environment.
2170 Each filter is called with three arguments: the transcoded data,
2171 as a string, the back-end, as a symbol, and the communication
2172 channel, as a plist. It must return a string or nil.")
2174 (defvar org-export-filter-babel-call-functions nil
2175 "List of functions applied to a transcoded babel-call.
2176 Each filter is called with three arguments: the transcoded data,
2177 as a string, the back-end, as a symbol, and the communication
2178 channel, as a plist. It must return a string or nil.")
2180 (defvar org-export-filter-paragraph-functions nil
2181 "List of functions applied to a transcoded paragraph.
2182 Each filter is called with three arguments: the transcoded data,
2183 as a string, the back-end, as a symbol, and the communication
2184 channel, as a plist. It must return a string or nil.")
2186 (defvar org-export-filter-planning-functions nil
2187 "List of functions applied to a transcoded planning.
2188 Each filter is called with three arguments: the transcoded data,
2189 as a string, the back-end, as a symbol, and the communication
2190 channel, as a plist. It must return a string or nil.")
2192 (defvar org-export-filter-property-drawer-functions nil
2193 "List of functions applied to a transcoded property-drawer.
2194 Each filter is called with three arguments: the transcoded data,
2195 as a string, the back-end, as a symbol, and the communication
2196 channel, as a plist. It must return a string or nil.")
2198 (defvar org-export-filter-quote-block-functions nil
2199 "List of functions applied to a transcoded quote block.
2200 Each filter is called with three arguments: the transcoded quote
2201 data, as a string, the back-end, as a symbol, and the
2202 communication channel, as a plist. It must return a string or
2203 nil.")
2205 (defvar org-export-filter-quote-section-functions nil
2206 "List of functions applied to a transcoded quote-section.
2207 Each filter is called with three arguments: the transcoded data,
2208 as a string, the back-end, as a symbol, and the communication
2209 channel, as a plist. It must return a string or nil.")
2211 (defvar org-export-filter-section-functions nil
2212 "List of functions applied to a transcoded section.
2213 Each filter is called with three arguments: the transcoded data,
2214 as a string, the back-end, as a symbol, and the communication
2215 channel, as a plist. It must return a string or nil.")
2217 (defvar org-export-filter-special-block-functions nil
2218 "List of functions applied to a transcoded special block.
2219 Each filter is called with three arguments: the transcoded data,
2220 as a string, the back-end, as a symbol, and the communication
2221 channel, as a plist. It must return a string or nil.")
2223 (defvar org-export-filter-src-block-functions nil
2224 "List of functions applied to a transcoded src-block.
2225 Each filter is called with three arguments: the transcoded data,
2226 as a string, the back-end, as a symbol, and the communication
2227 channel, as a plist. It must return a string or nil.")
2229 (defvar org-export-filter-table-functions nil
2230 "List of functions applied to a transcoded table.
2231 Each filter is called with three arguments: the transcoded data,
2232 as a string, the back-end, as a symbol, and the communication
2233 channel, as a plist. It must return a string or nil.")
2235 (defvar org-export-filter-table-cell-functions nil
2236 "List of functions applied to a transcoded table-cell.
2237 Each filter is called with three arguments: the transcoded data,
2238 as a string, the back-end, as a symbol, and the communication
2239 channel, as a plist. It must return a string or nil.")
2241 (defvar org-export-filter-table-row-functions nil
2242 "List of functions applied to a transcoded table-row.
2243 Each filter is called with three arguments: the transcoded data,
2244 as a string, the back-end, as a symbol, and the communication
2245 channel, as a plist. It must return a string or nil.")
2247 (defvar org-export-filter-verse-block-functions nil
2248 "List of functions applied to a transcoded verse block.
2249 Each filter is called with three arguments: the transcoded data,
2250 as a string, the back-end, as a symbol, and the communication
2251 channel, as a plist. It must return a string or nil.")
2254 ;;;; Objects Filters
2256 (defvar org-export-filter-bold-functions nil
2257 "List of functions applied to transcoded bold text.
2258 Each filter is called with three arguments: the transcoded data,
2259 as a string, the back-end, as a symbol, and the communication
2260 channel, as a plist. It must return a string or nil.")
2262 (defvar org-export-filter-code-functions nil
2263 "List of functions applied to transcoded code text.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-entity-functions nil
2269 "List of functions applied to a transcoded entity.
2270 Each filter is called with three arguments: the transcoded data,
2271 as a string, the back-end, as a symbol, and the communication
2272 channel, as a plist. It must return a string or nil.")
2274 (defvar org-export-filter-export-snippet-functions nil
2275 "List of functions applied to a transcoded export-snippet.
2276 Each filter is called with three arguments: the transcoded data,
2277 as a string, the back-end, as a symbol, and the communication
2278 channel, as a plist. It must return a string or nil.")
2280 (defvar org-export-filter-footnote-reference-functions nil
2281 "List of functions applied to a transcoded footnote-reference.
2282 Each filter is called with three arguments: the transcoded data,
2283 as a string, the back-end, as a symbol, and the communication
2284 channel, as a plist. It must return a string or nil.")
2286 (defvar org-export-filter-inline-babel-call-functions nil
2287 "List of functions applied to a transcoded inline-babel-call.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2292 (defvar org-export-filter-inline-src-block-functions nil
2293 "List of functions applied to a transcoded inline-src-block.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2298 (defvar org-export-filter-italic-functions nil
2299 "List of functions applied to transcoded italic text.
2300 Each filter is called with three arguments: the transcoded data,
2301 as a string, the back-end, as a symbol, and the communication
2302 channel, as a plist. It must return a string or nil.")
2304 (defvar org-export-filter-latex-fragment-functions nil
2305 "List of functions applied to a transcoded latex-fragment.
2306 Each filter is called with three arguments: the transcoded data,
2307 as a string, the back-end, as a symbol, and the communication
2308 channel, as a plist. It must return a string or nil.")
2310 (defvar org-export-filter-line-break-functions nil
2311 "List of functions applied to a transcoded line-break.
2312 Each filter is called with three arguments: the transcoded data,
2313 as a string, the back-end, as a symbol, and the communication
2314 channel, as a plist. It must return a string or nil.")
2316 (defvar org-export-filter-link-functions nil
2317 "List of functions applied to a transcoded link.
2318 Each filter is called with three arguments: the transcoded data,
2319 as a string, the back-end, as a symbol, and the communication
2320 channel, as a plist. It must return a string or nil.")
2322 (defvar org-export-filter-macro-functions nil
2323 "List of functions applied to a transcoded macro.
2324 Each filter is called with three arguments: the transcoded data,
2325 as a string, the back-end, as a symbol, and the communication
2326 channel, as a plist. It must return a string or nil.")
2328 (defvar org-export-filter-radio-target-functions nil
2329 "List of functions applied to a transcoded radio-target.
2330 Each filter is called with three arguments: the transcoded data,
2331 as a string, the back-end, as a symbol, and the communication
2332 channel, as a plist. It must return a string or nil.")
2334 (defvar org-export-filter-statistics-cookie-functions nil
2335 "List of functions applied to a transcoded statistics-cookie.
2336 Each filter is called with three arguments: the transcoded data,
2337 as a string, the back-end, as a symbol, and the communication
2338 channel, as a plist. It must return a string or nil.")
2340 (defvar org-export-filter-strike-through-functions nil
2341 "List of functions applied to transcoded strike-through text.
2342 Each filter is called with three arguments: the transcoded data,
2343 as a string, the back-end, as a symbol, and the communication
2344 channel, as a plist. It must return a string or nil.")
2346 (defvar org-export-filter-subscript-functions nil
2347 "List of functions applied to a transcoded subscript.
2348 Each filter is called with three arguments: the transcoded data,
2349 as a string, the back-end, as a symbol, and the communication
2350 channel, as a plist. It must return a string or nil.")
2352 (defvar org-export-filter-superscript-functions nil
2353 "List of functions applied to a transcoded superscript.
2354 Each filter is called with three arguments: the transcoded data,
2355 as a string, the back-end, as a symbol, and the communication
2356 channel, as a plist. It must return a string or nil.")
2358 (defvar org-export-filter-target-functions nil
2359 "List of functions applied to a transcoded target.
2360 Each filter is called with three arguments: the transcoded data,
2361 as a string, the back-end, as a symbol, and the communication
2362 channel, as a plist. It must return a string or nil.")
2364 (defvar org-export-filter-timestamp-functions nil
2365 "List of functions applied to a transcoded timestamp.
2366 Each filter is called with three arguments: the transcoded data,
2367 as a string, the back-end, as a symbol, and the communication
2368 channel, as a plist. It must return a string or nil.")
2370 (defvar org-export-filter-underline-functions nil
2371 "List of functions applied to transcoded underline text.
2372 Each filter is called with three arguments: the transcoded data,
2373 as a string, the back-end, as a symbol, and the communication
2374 channel, as a plist. It must return a string or nil.")
2376 (defvar org-export-filter-verbatim-functions nil
2377 "List of functions applied to transcoded verbatim text.
2378 Each filter is called with three arguments: the transcoded data,
2379 as a string, the back-end, as a symbol, and the communication
2380 channel, as a plist. It must return a string or nil.")
2383 ;;;; Filters Tools
2385 ;; Internal function `org-export-install-filters' installs filters
2386 ;; hard-coded in back-ends (developer filters) and filters from global
2387 ;; variables (user filters) in the communication channel.
2389 ;; Internal function `org-export-filter-apply-functions' takes care
2390 ;; about applying each filter in order to a given data. It ignores
2391 ;; filters returning a nil value but stops whenever a filter returns
2392 ;; an empty string.
2394 (defun org-export-filter-apply-functions (filters value info)
2395 "Call every function in FILTERS.
2397 Functions are called with arguments VALUE, current export
2398 back-end and INFO. A function returning a nil value will be
2399 skipped. If it returns the empty string, the process ends and
2400 VALUE is ignored.
2402 Call is done in a LIFO fashion, to be sure that developer
2403 specified filters, if any, are called first."
2404 (catch 'exit
2405 (dolist (filter filters value)
2406 (let ((result (funcall filter value (plist-get info :back-end) info)))
2407 (cond ((not result) value)
2408 ((equal value "") (throw 'exit nil))
2409 (t (setq value result)))))))
2411 (defun org-export-install-filters (info)
2412 "Install filters properties in communication channel.
2414 INFO is a plist containing the current communication channel.
2416 Return the updated communication channel."
2417 (let (plist)
2418 ;; Install user defined filters with `org-export-filters-alist'.
2419 (mapc (lambda (p)
2420 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2421 org-export-filters-alist)
2422 ;; Prepend back-end specific filters to that list.
2423 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2424 (plist-get info :back-end)))))
2425 (when (boundp back-end-filters)
2426 (mapc (lambda (p)
2427 ;; Single values get consed, lists are prepended.
2428 (let ((key (car p)) (value (cdr p)))
2429 (when value
2430 (setq plist
2431 (plist-put
2432 plist key
2433 (if (atom value) (cons value (plist-get plist key))
2434 (append value (plist-get plist key))))))))
2435 (eval back-end-filters))))
2436 ;; Return new communication channel.
2437 (org-combine-plists info plist)))
2441 ;;; Core functions
2443 ;; This is the room for the main function, `org-export-as', along with
2444 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2445 ;; They differ only by the way they output the resulting code.
2447 ;; `org-export-output-file-name' is an auxiliary function meant to be
2448 ;; used with `org-export-to-file'. With a given extension, it tries
2449 ;; to provide a canonical file name to write export output to.
2451 ;; Note that `org-export-as' doesn't really parse the current buffer,
2452 ;; but a copy of it (with the same buffer-local variables and
2453 ;; visibility), where macros and include keywords are expanded and
2454 ;; Babel blocks are executed, if appropriate.
2455 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2457 ;; File inclusion is taken care of by
2458 ;; `org-export-expand-include-keyword' and
2459 ;; `org-export--prepare-file-contents'. Structure wise, including
2460 ;; a whole Org file in a buffer often makes little sense. For
2461 ;; example, if the file contains an headline and the include keyword
2462 ;; was within an item, the item should contain the headline. That's
2463 ;; why file inclusion should be done before any structure can be
2464 ;; associated to the file, that is before parsing.
2466 (defun org-export-as
2467 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2468 "Transcode current Org buffer into BACKEND code.
2470 If narrowing is active in the current buffer, only transcode its
2471 narrowed part.
2473 If a region is active, transcode that region.
2475 When optional argument SUBTREEP is non-nil, transcode the
2476 sub-tree at point, extracting information from the headline
2477 properties first.
2479 When optional argument VISIBLE-ONLY is non-nil, don't export
2480 contents of hidden elements.
2482 When optional argument BODY-ONLY is non-nil, only return body
2483 code, without preamble nor postamble.
2485 Optional argument EXT-PLIST, when provided, is a property list
2486 with external parameters overriding Org default settings, but
2487 still inferior to file-local settings.
2489 Optional argument NOEXPAND, when non-nil, prevents included files
2490 to be expanded and Babel code to be executed.
2492 Return code as a string."
2493 (save-excursion
2494 (save-restriction
2495 ;; Narrow buffer to an appropriate region or subtree for
2496 ;; parsing. If parsing subtree, be sure to remove main headline
2497 ;; too.
2498 (cond ((org-region-active-p)
2499 (narrow-to-region (region-beginning) (region-end)))
2500 (subtreep
2501 (org-narrow-to-subtree)
2502 (goto-char (point-min))
2503 (forward-line)
2504 (narrow-to-region (point) (point-max))))
2505 ;; 1. Get export environment from original buffer. Also install
2506 ;; user's and developer's filters.
2507 (let ((info (org-export-install-filters
2508 (org-export-get-environment backend subtreep ext-plist)))
2509 ;; 2. Get parse tree. Buffer isn't parsed directly.
2510 ;; Instead, a temporary copy is created, where macros
2511 ;; and include keywords are expanded and code blocks
2512 ;; are evaluated.
2513 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2514 (current-buffer))))
2515 (org-export-with-current-buffer-copy
2516 (unless noexpand
2517 (org-macro-replace-all)
2518 (org-export-expand-include-keyword)
2519 ;; TODO: Setting `org-current-export-file' is
2520 ;; required by Org Babel to properly resolve
2521 ;; noweb references. Once "org-exp.el" is
2522 ;; removed, modify
2523 ;; `org-export-blocks-preprocess' so it accepts
2524 ;; the value as an argument instead.
2525 (let ((org-current-export-file buf))
2526 (org-export-blocks-preprocess)))
2527 (goto-char (point-min))
2528 ;; Run hook
2529 ;; `org-export-before-parsing-hook'. with current
2530 ;; back-end as argument.
2531 (run-hook-with-args
2532 'org-export-before-parsing-hook backend)
2533 ;; Eventually parse buffer.
2534 (org-element-parse-buffer nil visible-only)))))
2535 ;; 3. Call parse-tree filters to get the final tree.
2536 (setq tree
2537 (org-export-filter-apply-functions
2538 (plist-get info :filter-parse-tree) tree info))
2539 ;; 4. Now tree is complete, compute its properties and add
2540 ;; them to communication channel.
2541 (setq info
2542 (org-combine-plists
2543 info (org-export-collect-tree-properties tree info)))
2544 ;; 5. Eventually transcode TREE. Wrap the resulting string
2545 ;; into a template, if required. Eventually call
2546 ;; final-output filter.
2547 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2548 (template (cdr (assq 'template
2549 (plist-get info :translate-alist))))
2550 (output (org-export-filter-apply-functions
2551 (plist-get info :filter-final-output)
2552 (if (or (not (functionp template)) body-only) body
2553 (funcall template body info))
2554 info)))
2555 ;; Maybe add final OUTPUT to kill ring, then return it.
2556 (when org-export-copy-to-kill-ring (org-kill-new output))
2557 output)))))
2559 (defun org-export-to-buffer
2560 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2561 "Call `org-export-as' with output to a specified buffer.
2563 BACKEND is the back-end used for transcoding, as a symbol.
2565 BUFFER is the output buffer. If it already exists, it will be
2566 erased first, otherwise, it will be created.
2568 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2569 and NOEXPAND are similar to those used in `org-export-as', which
2570 see.
2572 Return buffer."
2573 (let ((out (org-export-as
2574 backend subtreep visible-only body-only ext-plist noexpand))
2575 (buffer (get-buffer-create buffer)))
2576 (with-current-buffer buffer
2577 (erase-buffer)
2578 (insert out)
2579 (goto-char (point-min)))
2580 buffer))
2582 (defun org-export-to-file
2583 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2584 "Call `org-export-as' with output to a specified file.
2586 BACKEND is the back-end used for transcoding, as a symbol. FILE
2587 is the name of the output file, as a string.
2589 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2590 and NOEXPAND are similar to those used in `org-export-as', which
2591 see.
2593 Return output file's name."
2594 ;; Checks for FILE permissions. `write-file' would do the same, but
2595 ;; we'd rather avoid needless transcoding of parse tree.
2596 (unless (file-writable-p file) (error "Output file not writable"))
2597 ;; Insert contents to a temporary buffer and write it to FILE.
2598 (let ((out (org-export-as
2599 backend subtreep visible-only body-only ext-plist noexpand)))
2600 (with-temp-buffer
2601 (insert out)
2602 (let ((coding-system-for-write org-export-coding-system))
2603 (write-file file))))
2604 ;; Return full path.
2605 file)
2607 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2608 "Return output file's name according to buffer specifications.
2610 EXTENSION is a string representing the output file extension,
2611 with the leading dot.
2613 With a non-nil optional argument SUBTREEP, try to determine
2614 output file's name by looking for \"EXPORT_FILE_NAME\" property
2615 of subtree at point.
2617 When optional argument PUB-DIR is set, use it as the publishing
2618 directory.
2620 When optional argument VISIBLE-ONLY is non-nil, don't export
2621 contents of hidden elements.
2623 Return file name as a string, or nil if it couldn't be
2624 determined."
2625 (let ((base-name
2626 ;; File name may come from EXPORT_FILE_NAME subtree property,
2627 ;; assuming point is at beginning of said sub-tree.
2628 (file-name-sans-extension
2629 (or (and subtreep
2630 (org-entry-get
2631 (save-excursion
2632 (ignore-errors (org-back-to-heading) (point)))
2633 "EXPORT_FILE_NAME" t))
2634 ;; File name may be extracted from buffer's associated
2635 ;; file, if any.
2636 (buffer-file-name (buffer-base-buffer))
2637 ;; Can't determine file name on our own: Ask user.
2638 (let ((read-file-name-function
2639 (and org-completion-use-ido 'ido-read-file-name)))
2640 (read-file-name
2641 "Output file: " pub-dir nil nil nil
2642 (lambda (name)
2643 (string= (file-name-extension name t) extension))))))))
2644 ;; Build file name. Enforce EXTENSION over whatever user may have
2645 ;; come up with. PUB-DIR, if defined, always has precedence over
2646 ;; any provided path.
2647 (cond
2648 (pub-dir
2649 (concat (file-name-as-directory pub-dir)
2650 (file-name-nondirectory base-name)
2651 extension))
2652 ((string= (file-name-nondirectory base-name) base-name)
2653 (concat (file-name-as-directory ".") base-name extension))
2654 (t (concat base-name extension)))))
2656 (defmacro org-export-with-current-buffer-copy (&rest body)
2657 "Apply BODY in a copy of the current buffer.
2659 The copy preserves local variables and visibility of the original
2660 buffer.
2662 Point is at buffer's beginning when BODY is applied."
2663 (org-with-gensyms (original-buffer offset buffer-string overlays)
2664 `(let ((,original-buffer (current-buffer))
2665 (,offset (1- (point-min)))
2666 (,buffer-string (buffer-string))
2667 (,overlays (mapcar
2668 'copy-overlay (overlays-in (point-min) (point-max)))))
2669 (with-temp-buffer
2670 (let ((buffer-invisibility-spec nil))
2671 (org-clone-local-variables
2672 ,original-buffer
2673 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2674 (insert ,buffer-string)
2675 (mapc (lambda (ov)
2676 (move-overlay
2678 (- (overlay-start ov) ,offset)
2679 (- (overlay-end ov) ,offset)
2680 (current-buffer)))
2681 ,overlays)
2682 (goto-char (point-min))
2683 (progn ,@body))))))
2684 (def-edebug-spec org-export-with-current-buffer-copy (body))
2686 (defun org-export-expand-include-keyword (&optional included dir)
2687 "Expand every include keyword in buffer.
2688 Optional argument INCLUDED is a list of included file names along
2689 with their line restriction, when appropriate. It is used to
2690 avoid infinite recursion. Optional argument DIR is the current
2691 working directory. It is used to properly resolve relative
2692 paths."
2693 (let ((case-fold-search t))
2694 (goto-char (point-min))
2695 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2696 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2697 'keyword)
2698 (beginning-of-line)
2699 ;; Extract arguments from keyword's value.
2700 (let* ((value (match-string 1))
2701 (ind (org-get-indentation))
2702 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2703 (prog1 (expand-file-name (match-string 1 value) dir)
2704 (setq value (replace-match "" nil nil value)))))
2705 (lines
2706 (and (string-match
2707 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2708 (prog1 (match-string 1 value)
2709 (setq value (replace-match "" nil nil value)))))
2710 (env (cond ((string-match "\\<example\\>" value) 'example)
2711 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2712 (match-string 1 value))))
2713 ;; Minimal level of included file defaults to the child
2714 ;; level of the current headline, if any, or one. It
2715 ;; only applies is the file is meant to be included as
2716 ;; an Org one.
2717 (minlevel
2718 (and (not env)
2719 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2720 (prog1 (string-to-number (match-string 1 value))
2721 (setq value (replace-match "" nil nil value)))
2722 (let ((cur (org-current-level)))
2723 (if cur (1+ (org-reduced-level cur)) 1))))))
2724 ;; Remove keyword.
2725 (delete-region (point) (progn (forward-line) (point)))
2726 (cond
2727 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2728 ;; Check if files has already been parsed. Look after
2729 ;; inclusion lines too, as different parts of the same file
2730 ;; can be included too.
2731 ((member (list file lines) included)
2732 (error "Recursive file inclusion: %s" file))
2734 (cond
2735 ((eq env 'example)
2736 (insert
2737 (let ((ind-str (make-string ind ? ))
2738 (contents
2739 ;; Protect sensitive contents with commas.
2740 (replace-regexp-in-string
2741 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2742 (org-export--prepare-file-contents file lines)
2743 nil nil 1)))
2744 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2745 ind-str contents ind-str))))
2746 ((stringp env)
2747 (insert
2748 (let ((ind-str (make-string ind ? ))
2749 (contents
2750 ;; Protect sensitive contents with commas.
2751 (replace-regexp-in-string
2752 (if (string= env "org") "\\(^\\)\\(.\\)"
2753 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2754 (org-export--prepare-file-contents file lines)
2755 nil nil 1)))
2756 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2757 ind-str env contents ind-str))))
2759 (insert
2760 (with-temp-buffer
2761 (org-mode)
2762 (insert
2763 (org-export--prepare-file-contents file lines ind minlevel))
2764 (org-export-expand-include-keyword
2765 (cons (list file lines) included)
2766 (file-name-directory file))
2767 (buffer-string))))))))))))
2769 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2770 "Prepare the contents of FILE for inclusion and return them as a string.
2772 When optional argument LINES is a string specifying a range of
2773 lines, include only those lines.
2775 Optional argument IND, when non-nil, is an integer specifying the
2776 global indentation of returned contents. Since its purpose is to
2777 allow an included file to stay in the same environment it was
2778 created \(i.e. a list item), it doesn't apply past the first
2779 headline encountered.
2781 Optional argument MINLEVEL, when non-nil, is an integer
2782 specifying the level that any top-level headline in the included
2783 file should have."
2784 (with-temp-buffer
2785 (insert-file-contents file)
2786 (when lines
2787 (let* ((lines (split-string lines "-"))
2788 (lbeg (string-to-number (car lines)))
2789 (lend (string-to-number (cadr lines)))
2790 (beg (if (zerop lbeg) (point-min)
2791 (goto-char (point-min))
2792 (forward-line (1- lbeg))
2793 (point)))
2794 (end (if (zerop lend) (point-max)
2795 (goto-char (point-min))
2796 (forward-line (1- lend))
2797 (point))))
2798 (narrow-to-region beg end)))
2799 ;; Remove blank lines at beginning and end of contents. The logic
2800 ;; behind that removal is that blank lines around include keyword
2801 ;; override blank lines in included file.
2802 (goto-char (point-min))
2803 (org-skip-whitespace)
2804 (beginning-of-line)
2805 (delete-region (point-min) (point))
2806 (goto-char (point-max))
2807 (skip-chars-backward " \r\t\n")
2808 (forward-line)
2809 (delete-region (point) (point-max))
2810 ;; If IND is set, preserve indentation of include keyword until
2811 ;; the first headline encountered.
2812 (when ind
2813 (unless (eq major-mode 'org-mode) (org-mode))
2814 (goto-char (point-min))
2815 (let ((ind-str (make-string ind ? )))
2816 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2817 ;; Do not move footnote definitions out of column 0.
2818 (unless (and (looking-at org-footnote-definition-re)
2819 (eq (org-element-type (org-element-at-point))
2820 'footnote-definition))
2821 (insert ind-str))
2822 (forward-line))))
2823 ;; When MINLEVEL is specified, compute minimal level for headlines
2824 ;; in the file (CUR-MIN), and remove stars to each headline so
2825 ;; that headlines with minimal level have a level of MINLEVEL.
2826 (when minlevel
2827 (unless (eq major-mode 'org-mode) (org-mode))
2828 (let ((levels (org-map-entries
2829 (lambda () (org-reduced-level (org-current-level))))))
2830 (when levels
2831 (let ((offset (- minlevel (apply 'min levels))))
2832 (unless (zerop offset)
2833 (when org-odd-levels-only (setq offset (* offset 2)))
2834 ;; Only change stars, don't bother moving whole
2835 ;; sections.
2836 (org-map-entries
2837 (lambda () (if (< offset 0) (delete-char (abs offset))
2838 (insert (make-string offset ?*))))))))))
2839 (buffer-string)))
2842 ;;; Tools For Back-Ends
2844 ;; A whole set of tools is available to help build new exporters. Any
2845 ;; function general enough to have its use across many back-ends
2846 ;; should be added here.
2848 ;; As of now, functions operating on footnotes, headlines, links,
2849 ;; references, src-blocks, tables and tables of contents are
2850 ;; implemented.
2852 ;;;; For Affiliated Keywords
2854 ;; `org-export-read-attribute' reads a property from a given element
2855 ;; as a plist. It can be used to normalize affiliated keywords'
2856 ;; syntax.
2858 (defun org-export-read-attribute (attribute element &optional property)
2859 "Turn ATTRIBUTE property from ELEMENT into a plist.
2861 When optional argument PROPERTY is non-nil, return the value of
2862 that property within attributes.
2864 This function assumes attributes are defined as \":keyword
2865 value\" pairs. It is appropriate for `:attr_html' like
2866 properties."
2867 (let ((attributes
2868 (let ((value (org-element-property attribute element)))
2869 (and value
2870 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2871 (if property (plist-get attributes property) attributes)))
2874 ;;;; For Export Snippets
2876 ;; Every export snippet is transmitted to the back-end. Though, the
2877 ;; latter will only retain one type of export-snippet, ignoring
2878 ;; others, based on the former's target back-end. The function
2879 ;; `org-export-snippet-backend' returns that back-end for a given
2880 ;; export-snippet.
2882 (defun org-export-snippet-backend (export-snippet)
2883 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2884 Translation, with `org-export-snippet-translation-alist', is
2885 applied."
2886 (let ((back-end (org-element-property :back-end export-snippet)))
2887 (intern
2888 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2889 back-end))))
2892 ;;;; For Footnotes
2894 ;; `org-export-collect-footnote-definitions' is a tool to list
2895 ;; actually used footnotes definitions in the whole parse tree, or in
2896 ;; an headline, in order to add footnote listings throughout the
2897 ;; transcoded data.
2899 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2900 ;; back-ends, when they need to attach the footnote definition only to
2901 ;; the first occurrence of the corresponding label.
2903 ;; `org-export-get-footnote-definition' and
2904 ;; `org-export-get-footnote-number' provide easier access to
2905 ;; additional information relative to a footnote reference.
2907 (defun org-export-collect-footnote-definitions (data info)
2908 "Return an alist between footnote numbers, labels and definitions.
2910 DATA is the parse tree from which definitions are collected.
2911 INFO is the plist used as a communication channel.
2913 Definitions are sorted by order of references. They either
2914 appear as Org data or as a secondary string for inlined
2915 footnotes. Unreferenced definitions are ignored."
2916 (let* (num-alist
2917 collect-fn ; for byte-compiler.
2918 (collect-fn
2919 (function
2920 (lambda (data)
2921 ;; Collect footnote number, label and definition in DATA.
2922 (org-element-map
2923 data 'footnote-reference
2924 (lambda (fn)
2925 (when (org-export-footnote-first-reference-p fn info)
2926 (let ((def (org-export-get-footnote-definition fn info)))
2927 (push
2928 (list (org-export-get-footnote-number fn info)
2929 (org-element-property :label fn)
2930 def)
2931 num-alist)
2932 ;; Also search in definition for nested footnotes.
2933 (when (eq (org-element-property :type fn) 'standard)
2934 (funcall collect-fn def)))))
2935 ;; Don't enter footnote definitions since it will happen
2936 ;; when their first reference is found.
2937 info nil 'footnote-definition)))))
2938 (funcall collect-fn (plist-get info :parse-tree))
2939 (reverse num-alist)))
2941 (defun org-export-footnote-first-reference-p (footnote-reference info)
2942 "Non-nil when a footnote reference is the first one for its label.
2944 FOOTNOTE-REFERENCE is the footnote reference being considered.
2945 INFO is the plist used as a communication channel."
2946 (let ((label (org-element-property :label footnote-reference)))
2947 ;; Anonymous footnotes are always a first reference.
2948 (if (not label) t
2949 ;; Otherwise, return the first footnote with the same LABEL and
2950 ;; test if it is equal to FOOTNOTE-REFERENCE.
2951 (let* (search-refs ; for byte-compiler.
2952 (search-refs
2953 (function
2954 (lambda (data)
2955 (org-element-map
2956 data 'footnote-reference
2957 (lambda (fn)
2958 (cond
2959 ((string= (org-element-property :label fn) label)
2960 (throw 'exit fn))
2961 ;; If FN isn't inlined, be sure to traverse its
2962 ;; definition before resuming search. See
2963 ;; comments in `org-export-get-footnote-number'
2964 ;; for more information.
2965 ((eq (org-element-property :type fn) 'standard)
2966 (funcall search-refs
2967 (org-export-get-footnote-definition fn info)))))
2968 ;; Don't enter footnote definitions since it will
2969 ;; happen when their first reference is found.
2970 info 'first-match 'footnote-definition)))))
2971 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
2972 footnote-reference)))))
2974 (defun org-export-get-footnote-definition (footnote-reference info)
2975 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2976 INFO is the plist used as a communication channel."
2977 (let ((label (org-element-property :label footnote-reference)))
2978 (or (org-element-property :inline-definition footnote-reference)
2979 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2981 (defun org-export-get-footnote-number (footnote info)
2982 "Return number associated to a footnote.
2984 FOOTNOTE is either a footnote reference or a footnote definition.
2985 INFO is the plist used as a communication channel."
2986 (let* ((label (org-element-property :label footnote))
2987 seen-refs
2988 search-ref ; For byte-compiler.
2989 (search-ref
2990 (function
2991 (lambda (data)
2992 ;; Search footnote references through DATA, filling
2993 ;; SEEN-REFS along the way.
2994 (org-element-map
2995 data 'footnote-reference
2996 (lambda (fn)
2997 (let ((fn-lbl (org-element-property :label fn)))
2998 (cond
2999 ;; Anonymous footnote match: return number.
3000 ((and (not fn-lbl) (eq fn footnote))
3001 (throw 'exit (1+ (length seen-refs))))
3002 ;; Labels match: return number.
3003 ((and label (string= label fn-lbl))
3004 (throw 'exit (1+ (length seen-refs))))
3005 ;; Anonymous footnote: it's always a new one. Also,
3006 ;; be sure to return nil from the `cond' so
3007 ;; `first-match' doesn't get us out of the loop.
3008 ((not fn-lbl) (push 'inline seen-refs) nil)
3009 ;; Label not seen so far: add it so SEEN-REFS.
3011 ;; Also search for subsequent references in
3012 ;; footnote definition so numbering follows reading
3013 ;; logic. Note that we don't have to care about
3014 ;; inline definitions, since `org-element-map'
3015 ;; already traverses them at the right time.
3017 ;; Once again, return nil to stay in the loop.
3018 ((not (member fn-lbl seen-refs))
3019 (push fn-lbl seen-refs)
3020 (funcall search-ref
3021 (org-export-get-footnote-definition fn info))
3022 nil))))
3023 ;; Don't enter footnote definitions since it will happen
3024 ;; when their first reference is found.
3025 info 'first-match 'footnote-definition)))))
3026 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3029 ;;;; For Headlines
3031 ;; `org-export-get-relative-level' is a shortcut to get headline
3032 ;; level, relatively to the lower headline level in the parsed tree.
3034 ;; `org-export-get-headline-number' returns the section number of an
3035 ;; headline, while `org-export-number-to-roman' allows to convert it
3036 ;; to roman numbers.
3038 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3039 ;; `org-export-last-sibling-p' are three useful predicates when it
3040 ;; comes to fulfill the `:headline-levels' property.
3042 (defun org-export-get-relative-level (headline info)
3043 "Return HEADLINE relative level within current parsed tree.
3044 INFO is a plist holding contextual information."
3045 (+ (org-element-property :level headline)
3046 (or (plist-get info :headline-offset) 0)))
3048 (defun org-export-low-level-p (headline info)
3049 "Non-nil when HEADLINE is considered as low level.
3051 INFO is a plist used as a communication channel.
3053 A low level headlines has a relative level greater than
3054 `:headline-levels' property value.
3056 Return value is the difference between HEADLINE relative level
3057 and the last level being considered as high enough, or nil."
3058 (let ((limit (plist-get info :headline-levels)))
3059 (when (wholenump limit)
3060 (let ((level (org-export-get-relative-level headline info)))
3061 (and (> level limit) (- level limit))))))
3063 (defun org-export-get-headline-number (headline info)
3064 "Return HEADLINE numbering as a list of numbers.
3065 INFO is a plist holding contextual information."
3066 (cdr (assoc headline (plist-get info :headline-numbering))))
3068 (defun org-export-numbered-headline-p (headline info)
3069 "Return a non-nil value if HEADLINE element should be numbered.
3070 INFO is a plist used as a communication channel."
3071 (let ((sec-num (plist-get info :section-numbers))
3072 (level (org-export-get-relative-level headline info)))
3073 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3075 (defun org-export-number-to-roman (n)
3076 "Convert integer N into a roman numeral."
3077 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3078 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3079 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3080 ( 1 . "I")))
3081 (res ""))
3082 (if (<= n 0)
3083 (number-to-string n)
3084 (while roman
3085 (if (>= n (caar roman))
3086 (setq n (- n (caar roman))
3087 res (concat res (cdar roman)))
3088 (pop roman)))
3089 res)))
3091 (defun org-export-get-tags (element info &optional tags)
3092 "Return list of tags associated to ELEMENT.
3094 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3095 is a plist used as a communication channel.
3097 Select tags (see `org-export-select-tags') and exclude tags (see
3098 `org-export-exclude-tags') are removed from the list.
3100 When non-nil, optional argument TAGS should be a list of strings.
3101 Any tag belonging to this list will also be removed."
3102 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3103 (member tag (plist-get info :exclude-tags))
3104 (member tag tags)))
3105 (org-element-property :tags element)))
3107 (defun org-export-first-sibling-p (headline info)
3108 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3109 INFO is a plist used as a communication channel."
3110 (not (eq (org-element-type (org-export-get-previous-element headline info))
3111 'headline)))
3113 (defun org-export-last-sibling-p (headline info)
3114 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3115 INFO is a plist used as a communication channel."
3116 (not (org-export-get-next-element headline info)))
3119 ;;;; For Links
3121 ;; `org-export-solidify-link-text' turns a string into a safer version
3122 ;; for links, replacing most non-standard characters with hyphens.
3124 ;; `org-export-get-coderef-format' returns an appropriate format
3125 ;; string for coderefs.
3127 ;; `org-export-inline-image-p' returns a non-nil value when the link
3128 ;; provided should be considered as an inline image.
3130 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3131 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3132 ;; returns an appropriate unique identifier when found, or nil.
3134 ;; `org-export-resolve-id-link' returns the first headline with
3135 ;; specified id or custom-id in parse tree, the path to the external
3136 ;; file with the id or nil when neither was found.
3138 ;; `org-export-resolve-coderef' associates a reference to a line
3139 ;; number in the element it belongs, or returns the reference itself
3140 ;; when the element isn't numbered.
3142 (defun org-export-solidify-link-text (s)
3143 "Take link text S and make a safe target out of it."
3144 (save-match-data
3145 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3147 (defun org-export-get-coderef-format (path desc)
3148 "Return format string for code reference link.
3149 PATH is the link path. DESC is its description."
3150 (save-match-data
3151 (cond ((not desc) "%s")
3152 ((string-match (regexp-quote (concat "(" path ")")) desc)
3153 (replace-match "%s" t t desc))
3154 (t desc))))
3156 (defun org-export-inline-image-p (link &optional rules)
3157 "Non-nil if LINK object points to an inline image.
3159 Optional argument is a set of RULES defining inline images. It
3160 is an alist where associations have the following shape:
3162 \(TYPE . REGEXP)
3164 Applying a rule means apply REGEXP against LINK's path when its
3165 type is TYPE. The function will return a non-nil value if any of
3166 the provided rules is non-nil. The default rule is
3167 `org-export-default-inline-image-rule'.
3169 This only applies to links without a description."
3170 (and (not (org-element-contents link))
3171 (let ((case-fold-search t)
3172 (rules (or rules org-export-default-inline-image-rule)))
3173 (catch 'exit
3174 (mapc
3175 (lambda (rule)
3176 (and (string= (org-element-property :type link) (car rule))
3177 (string-match (cdr rule)
3178 (org-element-property :path link))
3179 (throw 'exit t)))
3180 rules)
3181 ;; Return nil if no rule matched.
3182 nil))))
3184 (defun org-export-resolve-coderef (ref info)
3185 "Resolve a code reference REF.
3187 INFO is a plist used as a communication channel.
3189 Return associated line number in source code, or REF itself,
3190 depending on src-block or example element's switches."
3191 (org-element-map
3192 (plist-get info :parse-tree) '(example-block src-block)
3193 (lambda (el)
3194 (with-temp-buffer
3195 (insert (org-trim (org-element-property :value el)))
3196 (let* ((label-fmt (regexp-quote
3197 (or (org-element-property :label-fmt el)
3198 org-coderef-label-format)))
3199 (ref-re
3200 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3201 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3202 ;; Element containing REF is found. Resolve it to either
3203 ;; a label or a line number, as needed.
3204 (when (re-search-backward ref-re nil t)
3205 (cond
3206 ((org-element-property :use-labels el) ref)
3207 ((eq (org-element-property :number-lines el) 'continued)
3208 (+ (org-export-get-loc el info) (line-number-at-pos)))
3209 (t (line-number-at-pos)))))))
3210 info 'first-match))
3212 (defun org-export-resolve-fuzzy-link (link info)
3213 "Return LINK destination.
3215 INFO is a plist holding contextual information.
3217 Return value can be an object, an element, or nil:
3219 - If LINK path matches a target object (i.e. <<path>>) or
3220 element (i.e. \"#+TARGET: path\"), return it.
3222 - If LINK path exactly matches the name affiliated keyword
3223 \(i.e. #+NAME: path) of an element, return that element.
3225 - If LINK path exactly matches any headline name, return that
3226 element. If more than one headline share that name, priority
3227 will be given to the one with the closest common ancestor, if
3228 any, or the first one in the parse tree otherwise.
3230 - Otherwise, return nil.
3232 Assume LINK type is \"fuzzy\"."
3233 (let* ((path (org-element-property :path link))
3234 (match-title-p (eq (aref path 0) ?*)))
3235 (cond
3236 ;; First try to find a matching "<<path>>" unless user specified
3237 ;; he was looking for an headline (path starts with a *
3238 ;; character).
3239 ((and (not match-title-p)
3240 (loop for target in (plist-get info :target-list)
3241 when (string= (org-element-property :value target) path)
3242 return target)))
3243 ;; Then try to find an element with a matching "#+NAME: path"
3244 ;; affiliated keyword.
3245 ((and (not match-title-p)
3246 (org-element-map
3247 (plist-get info :parse-tree) org-element-all-elements
3248 (lambda (el)
3249 (when (string= (org-element-property :name el) path) el))
3250 info 'first-match)))
3251 ;; Last case: link either points to an headline or to
3252 ;; nothingness. Try to find the source, with priority given to
3253 ;; headlines with the closest common ancestor. If such candidate
3254 ;; is found, return it, otherwise return nil.
3256 (let ((find-headline
3257 (function
3258 ;; Return first headline whose `:raw-value' property
3259 ;; is NAME in parse tree DATA, or nil.
3260 (lambda (name data)
3261 (org-element-map
3262 data 'headline
3263 (lambda (headline)
3264 (when (string=
3265 (org-element-property :raw-value headline)
3266 name)
3267 headline))
3268 info 'first-match)))))
3269 ;; Search among headlines sharing an ancestor with link,
3270 ;; from closest to farthest.
3271 (or (catch 'exit
3272 (mapc
3273 (lambda (parent)
3274 (when (eq (org-element-type parent) 'headline)
3275 (let ((foundp (funcall find-headline path parent)))
3276 (when foundp (throw 'exit foundp)))))
3277 (org-export-get-genealogy link)) nil)
3278 ;; No match with a common ancestor: try the full parse-tree.
3279 (funcall find-headline
3280 (if match-title-p (substring path 1) path)
3281 (plist-get info :parse-tree))))))))
3283 (defun org-export-resolve-id-link (link info)
3284 "Return headline referenced as LINK destination.
3286 INFO is a plist used as a communication channel.
3288 Return value can be the headline element matched in current parse
3289 tree, a file name or nil. Assume LINK type is either \"id\" or
3290 \"custom-id\"."
3291 (let ((id (org-element-property :path link)))
3292 ;; First check if id is within the current parse tree.
3293 (or (org-element-map
3294 (plist-get info :parse-tree) 'headline
3295 (lambda (headline)
3296 (when (or (string= (org-element-property :id headline) id)
3297 (string= (org-element-property :custom-id headline) id))
3298 headline))
3299 info 'first-match)
3300 ;; Otherwise, look for external files.
3301 (cdr (assoc id (plist-get info :id-alist))))))
3303 (defun org-export-resolve-radio-link (link info)
3304 "Return radio-target object referenced as LINK destination.
3306 INFO is a plist used as a communication channel.
3308 Return value can be a radio-target object or nil. Assume LINK
3309 has type \"radio\"."
3310 (let ((path (org-element-property :path link)))
3311 (org-element-map
3312 (plist-get info :parse-tree) 'radio-target
3313 (lambda (radio)
3314 (when (equal (org-element-property :value radio) path) radio))
3315 info 'first-match)))
3318 ;;;; For References
3320 ;; `org-export-get-ordinal' associates a sequence number to any object
3321 ;; or element.
3323 (defun org-export-get-ordinal (element info &optional types predicate)
3324 "Return ordinal number of an element or object.
3326 ELEMENT is the element or object considered. INFO is the plist
3327 used as a communication channel.
3329 Optional argument TYPES, when non-nil, is a list of element or
3330 object types, as symbols, that should also be counted in.
3331 Otherwise, only provided element's type is considered.
3333 Optional argument PREDICATE is a function returning a non-nil
3334 value if the current element or object should be counted in. It
3335 accepts two arguments: the element or object being considered and
3336 the plist used as a communication channel. This allows to count
3337 only a certain type of objects (i.e. inline images).
3339 Return value is a list of numbers if ELEMENT is an headline or an
3340 item. It is nil for keywords. It represents the footnote number
3341 for footnote definitions and footnote references. If ELEMENT is
3342 a target, return the same value as if ELEMENT was the closest
3343 table, item or headline containing the target. In any other
3344 case, return the sequence number of ELEMENT among elements or
3345 objects of the same type."
3346 ;; A target keyword, representing an invisible target, never has
3347 ;; a sequence number.
3348 (unless (eq (org-element-type element) 'keyword)
3349 ;; Ordinal of a target object refer to the ordinal of the closest
3350 ;; table, item, or headline containing the object.
3351 (when (eq (org-element-type element) 'target)
3352 (setq element
3353 (loop for parent in (org-export-get-genealogy element)
3354 when
3355 (memq
3356 (org-element-type parent)
3357 '(footnote-definition footnote-reference headline item
3358 table))
3359 return parent)))
3360 (case (org-element-type element)
3361 ;; Special case 1: An headline returns its number as a list.
3362 (headline (org-export-get-headline-number element info))
3363 ;; Special case 2: An item returns its number as a list.
3364 (item (let ((struct (org-element-property :structure element)))
3365 (org-list-get-item-number
3366 (org-element-property :begin element)
3367 struct
3368 (org-list-prevs-alist struct)
3369 (org-list-parents-alist struct))))
3370 ((footnote-definition footnote-reference)
3371 (org-export-get-footnote-number element info))
3372 (otherwise
3373 (let ((counter 0))
3374 ;; Increment counter until ELEMENT is found again.
3375 (org-element-map
3376 (plist-get info :parse-tree) (or types (org-element-type element))
3377 (lambda (el)
3378 (cond
3379 ((eq element el) (1+ counter))
3380 ((not predicate) (incf counter) nil)
3381 ((funcall predicate el info) (incf counter) nil)))
3382 info 'first-match))))))
3385 ;;;; For Src-Blocks
3387 ;; `org-export-get-loc' counts number of code lines accumulated in
3388 ;; src-block or example-block elements with a "+n" switch until
3389 ;; a given element, excluded. Note: "-n" switches reset that count.
3391 ;; `org-export-unravel-code' extracts source code (along with a code
3392 ;; references alist) from an `element-block' or `src-block' type
3393 ;; element.
3395 ;; `org-export-format-code' applies a formatting function to each line
3396 ;; of code, providing relative line number and code reference when
3397 ;; appropriate. Since it doesn't access the original element from
3398 ;; which the source code is coming, it expects from the code calling
3399 ;; it to know if lines should be numbered and if code references
3400 ;; should appear.
3402 ;; Eventually, `org-export-format-code-default' is a higher-level
3403 ;; function (it makes use of the two previous functions) which handles
3404 ;; line numbering and code references inclusion, and returns source
3405 ;; code in a format suitable for plain text or verbatim output.
3407 (defun org-export-get-loc (element info)
3408 "Return accumulated lines of code up to ELEMENT.
3410 INFO is the plist used as a communication channel.
3412 ELEMENT is excluded from count."
3413 (let ((loc 0))
3414 (org-element-map
3415 (plist-get info :parse-tree)
3416 `(src-block example-block ,(org-element-type element))
3417 (lambda (el)
3418 (cond
3419 ;; ELEMENT is reached: Quit the loop.
3420 ((eq el element))
3421 ;; Only count lines from src-block and example-block elements
3422 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3423 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3424 ((let ((linums (org-element-property :number-lines el)))
3425 (when linums
3426 ;; Accumulate locs or reset them.
3427 (let ((lines (org-count-lines
3428 (org-trim (org-element-property :value el)))))
3429 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3430 ;; Return nil to stay in the loop.
3431 nil)))
3432 info 'first-match)
3433 ;; Return value.
3434 loc))
3436 (defun org-export-unravel-code (element)
3437 "Clean source code and extract references out of it.
3439 ELEMENT has either a `src-block' an `example-block' type.
3441 Return a cons cell whose CAR is the source code, cleaned from any
3442 reference and protective comma and CDR is an alist between
3443 relative line number (integer) and name of code reference on that
3444 line (string)."
3445 (let* ((line 0) refs
3446 ;; Get code and clean it. Remove blank lines at its
3447 ;; beginning and end. Also remove protective commas.
3448 (code (let ((c (replace-regexp-in-string
3449 "\\`\\([ \t]*\n\\)+" ""
3450 (replace-regexp-in-string
3451 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3452 (org-element-property :value element)))))
3453 ;; If appropriate, remove global indentation.
3454 (unless (or org-src-preserve-indentation
3455 (org-element-property :preserve-indent element))
3456 (setq c (org-remove-indentation c)))
3457 ;; Free up the protected lines. Note: Org blocks
3458 ;; have commas at the beginning or every line.
3459 (if (string= (org-element-property :language element) "org")
3460 (replace-regexp-in-string "^," "" c)
3461 (replace-regexp-in-string
3462 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3463 ;; Get format used for references.
3464 (label-fmt (regexp-quote
3465 (or (org-element-property :label-fmt element)
3466 org-coderef-label-format)))
3467 ;; Build a regexp matching a loc with a reference.
3468 (with-ref-re
3469 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3470 (replace-regexp-in-string
3471 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3472 ;; Return value.
3473 (cons
3474 ;; Code with references removed.
3475 (org-element-normalize-string
3476 (mapconcat
3477 (lambda (loc)
3478 (incf line)
3479 (if (not (string-match with-ref-re loc)) loc
3480 ;; Ref line: remove ref, and signal its position in REFS.
3481 (push (cons line (match-string 3 loc)) refs)
3482 (replace-match "" nil nil loc 1)))
3483 (org-split-string code "\n") "\n"))
3484 ;; Reference alist.
3485 refs)))
3487 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3488 "Format CODE by applying FUN line-wise and return it.
3490 CODE is a string representing the code to format. FUN is
3491 a function. It must accept three arguments: a line of
3492 code (string), the current line number (integer) or nil and the
3493 reference associated to the current line (string) or nil.
3495 Optional argument NUM-LINES can be an integer representing the
3496 number of code lines accumulated until the current code. Line
3497 numbers passed to FUN will take it into account. If it is nil,
3498 FUN's second argument will always be nil. This number can be
3499 obtained with `org-export-get-loc' function.
3501 Optional argument REF-ALIST can be an alist between relative line
3502 number (i.e. ignoring NUM-LINES) and the name of the code
3503 reference on it. If it is nil, FUN's third argument will always
3504 be nil. It can be obtained through the use of
3505 `org-export-unravel-code' function."
3506 (let ((--locs (org-split-string code "\n"))
3507 (--line 0))
3508 (org-element-normalize-string
3509 (mapconcat
3510 (lambda (--loc)
3511 (incf --line)
3512 (let ((--ref (cdr (assq --line ref-alist))))
3513 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3514 --locs "\n"))))
3516 (defun org-export-format-code-default (element info)
3517 "Return source code from ELEMENT, formatted in a standard way.
3519 ELEMENT is either a `src-block' or `example-block' element. INFO
3520 is a plist used as a communication channel.
3522 This function takes care of line numbering and code references
3523 inclusion. Line numbers, when applicable, appear at the
3524 beginning of the line, separated from the code by two white
3525 spaces. Code references, on the other hand, appear flushed to
3526 the right, separated by six white spaces from the widest line of
3527 code."
3528 ;; Extract code and references.
3529 (let* ((code-info (org-export-unravel-code element))
3530 (code (car code-info))
3531 (code-lines (org-split-string code "\n"))
3532 (refs (and (org-element-property :retain-labels element)
3533 (cdr code-info)))
3534 ;; Handle line numbering.
3535 (num-start (case (org-element-property :number-lines element)
3536 (continued (org-export-get-loc element info))
3537 (new 0)))
3538 (num-fmt
3539 (and num-start
3540 (format "%%%ds "
3541 (length (number-to-string
3542 (+ (length code-lines) num-start))))))
3543 ;; Prepare references display, if required. Any reference
3544 ;; should start six columns after the widest line of code,
3545 ;; wrapped with parenthesis.
3546 (max-width
3547 (+ (apply 'max (mapcar 'length code-lines))
3548 (if (not num-start) 0 (length (format num-fmt num-start))))))
3549 (org-export-format-code
3550 code
3551 (lambda (loc line-num ref)
3552 (let ((number-str (and num-fmt (format num-fmt line-num))))
3553 (concat
3554 number-str
3556 (and ref
3557 (concat (make-string
3558 (- (+ 6 max-width)
3559 (+ (length loc) (length number-str))) ? )
3560 (format "(%s)" ref))))))
3561 num-start refs)))
3564 ;;;; For Tables
3566 ;; `org-export-table-has-special-column-p' and and
3567 ;; `org-export-table-row-is-special-p' are predicates used to look for
3568 ;; meta-information about the table structure.
3570 ;; `org-table-has-header-p' tells when the rows before the first rule
3571 ;; should be considered as table's header.
3573 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3574 ;; and `org-export-table-cell-borders' extract information from
3575 ;; a table-cell element.
3577 ;; `org-export-table-dimensions' gives the number on rows and columns
3578 ;; in the table, ignoring horizontal rules and special columns.
3579 ;; `org-export-table-cell-address', given a table-cell object, returns
3580 ;; the absolute address of a cell. On the other hand,
3581 ;; `org-export-get-table-cell-at' does the contrary.
3583 ;; `org-export-table-cell-starts-colgroup-p',
3584 ;; `org-export-table-cell-ends-colgroup-p',
3585 ;; `org-export-table-row-starts-rowgroup-p',
3586 ;; `org-export-table-row-ends-rowgroup-p',
3587 ;; `org-export-table-row-starts-header-p' and
3588 ;; `org-export-table-row-ends-header-p' indicate position of current
3589 ;; row or cell within the table.
3591 (defun org-export-table-has-special-column-p (table)
3592 "Non-nil when TABLE has a special column.
3593 All special columns will be ignored during export."
3594 ;; The table has a special column when every first cell of every row
3595 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3596 ;; "*" "_" and "^". Though, do not consider a first row containing
3597 ;; only empty cells as special.
3598 (let ((special-column-p 'empty))
3599 (catch 'exit
3600 (mapc
3601 (lambda (row)
3602 (when (eq (org-element-property :type row) 'standard)
3603 (let ((value (org-element-contents
3604 (car (org-element-contents row)))))
3605 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3606 (setq special-column-p 'special))
3607 ((not value))
3608 (t (throw 'exit nil))))))
3609 (org-element-contents table))
3610 (eq special-column-p 'special))))
3612 (defun org-export-table-has-header-p (table info)
3613 "Non-nil when TABLE has an header.
3615 INFO is a plist used as a communication channel.
3617 A table has an header when it contains at least two row groups."
3618 (let ((rowgroup 1) row-flag)
3619 (org-element-map
3620 table 'table-row
3621 (lambda (row)
3622 (cond
3623 ((> rowgroup 1) t)
3624 ((and row-flag (eq (org-element-property :type row) 'rule))
3625 (incf rowgroup) (setq row-flag nil))
3626 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3627 (setq row-flag t) nil)))
3628 info)))
3630 (defun org-export-table-row-is-special-p (table-row info)
3631 "Non-nil if TABLE-ROW is considered special.
3633 INFO is a plist used as the communication channel.
3635 All special rows will be ignored during export."
3636 (when (eq (org-element-property :type table-row) 'standard)
3637 (let ((first-cell (org-element-contents
3638 (car (org-element-contents table-row)))))
3639 ;; A row is special either when...
3641 ;; ... it starts with a field only containing "/",
3642 (equal first-cell '("/"))
3643 ;; ... the table contains a special column and the row start
3644 ;; with a marking character among, "^", "_", "$" or "!",
3645 (and (org-export-table-has-special-column-p
3646 (org-export-get-parent table-row))
3647 (member first-cell '(("^") ("_") ("$") ("!"))))
3648 ;; ... it contains only alignment cookies and empty cells.
3649 (let ((special-row-p 'empty))
3650 (catch 'exit
3651 (mapc
3652 (lambda (cell)
3653 (let ((value (org-element-contents cell)))
3654 ;; Since VALUE is a secondary string, the following
3655 ;; checks avoid expanding it with `org-export-data'.
3656 (cond ((not value))
3657 ((and (not (cdr value))
3658 (stringp (car value))
3659 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3660 (car value)))
3661 (setq special-row-p 'cookie))
3662 (t (throw 'exit nil)))))
3663 (org-element-contents table-row))
3664 (eq special-row-p 'cookie)))))))
3666 (defun org-export-table-row-group (table-row info)
3667 "Return TABLE-ROW's group.
3669 INFO is a plist used as the communication channel.
3671 Return value is the group number, as an integer, or nil special
3672 rows and table rules. Group 1 is also table's header."
3673 (unless (or (eq (org-element-property :type table-row) 'rule)
3674 (org-export-table-row-is-special-p table-row info))
3675 (let ((group 0) row-flag)
3676 (catch 'found
3677 (mapc
3678 (lambda (row)
3679 (cond
3680 ((and (eq (org-element-property :type row) 'standard)
3681 (not (org-export-table-row-is-special-p row info)))
3682 (unless row-flag (incf group) (setq row-flag t)))
3683 ((eq (org-element-property :type row) 'rule)
3684 (setq row-flag nil)))
3685 (when (eq table-row row) (throw 'found group)))
3686 (org-element-contents (org-export-get-parent table-row)))))))
3688 (defun org-export-table-cell-width (table-cell info)
3689 "Return TABLE-CELL contents width.
3691 INFO is a plist used as the communication channel.
3693 Return value is the width given by the last width cookie in the
3694 same column as TABLE-CELL, or nil."
3695 (let* ((row (org-export-get-parent table-cell))
3696 (column (let ((cells (org-element-contents row)))
3697 (- (length cells) (length (memq table-cell cells)))))
3698 (table (org-export-get-parent-table table-cell))
3699 cookie-width)
3700 (mapc
3701 (lambda (row)
3702 (cond
3703 ;; In a special row, try to find a width cookie at COLUMN.
3704 ((org-export-table-row-is-special-p row info)
3705 (let ((value (org-element-contents
3706 (elt (org-element-contents row) column))))
3707 ;; The following checks avoid expanding unnecessarily the
3708 ;; cell with `org-export-data'
3709 (when (and value
3710 (not (cdr value))
3711 (stringp (car value))
3712 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3713 (match-string 1 (car value)))
3714 (setq cookie-width
3715 (string-to-number (match-string 1 (car value)))))))
3716 ;; Ignore table rules.
3717 ((eq (org-element-property :type row) 'rule))))
3718 (org-element-contents table))
3719 ;; Return value.
3720 cookie-width))
3722 (defun org-export-table-cell-alignment (table-cell info)
3723 "Return TABLE-CELL contents alignment.
3725 INFO is a plist used as the communication channel.
3727 Return alignment as specified by the last alignment cookie in the
3728 same column as TABLE-CELL. If no such cookie is found, a default
3729 alignment value will be deduced from fraction of numbers in the
3730 column (see `org-table-number-fraction' for more information).
3731 Possible values are `left', `right' and `center'."
3732 (let* ((row (org-export-get-parent table-cell))
3733 (column (let ((cells (org-element-contents row)))
3734 (- (length cells) (length (memq table-cell cells)))))
3735 (table (org-export-get-parent-table table-cell))
3736 (number-cells 0)
3737 (total-cells 0)
3738 cookie-align)
3739 (mapc
3740 (lambda (row)
3741 (cond
3742 ;; In a special row, try to find an alignment cookie at
3743 ;; COLUMN.
3744 ((org-export-table-row-is-special-p row info)
3745 (let ((value (org-element-contents
3746 (elt (org-element-contents row) column))))
3747 ;; Since VALUE is a secondary string, the following checks
3748 ;; avoid useless expansion through `org-export-data'.
3749 (when (and value
3750 (not (cdr value))
3751 (stringp (car value))
3752 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3753 (car value))
3754 (match-string 1 (car value)))
3755 (setq cookie-align (match-string 1 (car value))))))
3756 ;; Ignore table rules.
3757 ((eq (org-element-property :type row) 'rule))
3758 ;; In a standard row, check if cell's contents are expressing
3759 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3760 ;; Though, don't bother if an alignment cookie has already
3761 ;; defined cell's alignment.
3762 ((not cookie-align)
3763 (let ((value (org-export-data
3764 (org-element-contents
3765 (elt (org-element-contents row) column))
3766 info)))
3767 (incf total-cells)
3768 (when (string-match org-table-number-regexp value)
3769 (incf number-cells))))))
3770 (org-element-contents table))
3771 ;; Return value. Alignment specified by cookies has precedence
3772 ;; over alignment deduced from cells contents.
3773 (cond ((equal cookie-align "l") 'left)
3774 ((equal cookie-align "r") 'right)
3775 ((equal cookie-align "c") 'center)
3776 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3777 'right)
3778 (t 'left))))
3780 (defun org-export-table-cell-borders (table-cell info)
3781 "Return TABLE-CELL borders.
3783 INFO is a plist used as a communication channel.
3785 Return value is a list of symbols, or nil. Possible values are:
3786 `top', `bottom', `above', `below', `left' and `right'. Note:
3787 `top' (resp. `bottom') only happen for a cell in the first
3788 row (resp. last row) of the table, ignoring table rules, if any.
3790 Returned borders ignore special rows."
3791 (let* ((row (org-export-get-parent table-cell))
3792 (table (org-export-get-parent-table table-cell))
3793 borders)
3794 ;; Top/above border? TABLE-CELL has a border above when a rule
3795 ;; used to demarcate row groups can be found above. Hence,
3796 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3797 ;; another regular row has to be found above that rule.
3798 (let (rule-flag)
3799 (catch 'exit
3800 (mapc (lambda (row)
3801 (cond ((eq (org-element-property :type row) 'rule)
3802 (setq rule-flag t))
3803 ((not (org-export-table-row-is-special-p row info))
3804 (if rule-flag (throw 'exit (push 'above borders))
3805 (throw 'exit nil)))))
3806 ;; Look at every row before the current one.
3807 (cdr (memq row (reverse (org-element-contents table)))))
3808 ;; No rule above, or rule found starts the table (ignoring any
3809 ;; special row): TABLE-CELL is at the top of the table.
3810 (when rule-flag (push 'above borders))
3811 (push 'top borders)))
3812 ;; Bottom/below border? TABLE-CELL has a border below when next
3813 ;; non-regular row below is a rule.
3814 (let (rule-flag)
3815 (catch 'exit
3816 (mapc (lambda (row)
3817 (cond ((eq (org-element-property :type row) 'rule)
3818 (setq rule-flag t))
3819 ((not (org-export-table-row-is-special-p row info))
3820 (if rule-flag (throw 'exit (push 'below borders))
3821 (throw 'exit nil)))))
3822 ;; Look at every row after the current one.
3823 (cdr (memq row (org-element-contents table))))
3824 ;; No rule below, or rule found ends the table (modulo some
3825 ;; special row): TABLE-CELL is at the bottom of the table.
3826 (when rule-flag (push 'below borders))
3827 (push 'bottom borders)))
3828 ;; Right/left borders? They can only be specified by column
3829 ;; groups. Column groups are defined in a row starting with "/".
3830 ;; Also a column groups row only contains "<", "<>", ">" or blank
3831 ;; cells.
3832 (catch 'exit
3833 (let ((column (let ((cells (org-element-contents row)))
3834 (- (length cells) (length (memq table-cell cells))))))
3835 (mapc
3836 (lambda (row)
3837 (unless (eq (org-element-property :type row) 'rule)
3838 (when (equal (org-element-contents
3839 (car (org-element-contents row)))
3840 '("/"))
3841 (let ((column-groups
3842 (mapcar
3843 (lambda (cell)
3844 (let ((value (org-element-contents cell)))
3845 (when (member value '(("<") ("<>") (">") nil))
3846 (car value))))
3847 (org-element-contents row))))
3848 ;; There's a left border when previous cell, if
3849 ;; any, ends a group, or current one starts one.
3850 (when (or (and (not (zerop column))
3851 (member (elt column-groups (1- column))
3852 '(">" "<>")))
3853 (member (elt column-groups column) '("<" "<>")))
3854 (push 'left borders))
3855 ;; There's a right border when next cell, if any,
3856 ;; starts a group, or current one ends one.
3857 (when (or (and (/= (1+ column) (length column-groups))
3858 (member (elt column-groups (1+ column))
3859 '("<" "<>")))
3860 (member (elt column-groups column) '(">" "<>")))
3861 (push 'right borders))
3862 (throw 'exit nil)))))
3863 ;; Table rows are read in reverse order so last column groups
3864 ;; row has precedence over any previous one.
3865 (reverse (org-element-contents table)))))
3866 ;; Return value.
3867 borders))
3869 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3870 "Non-nil when TABLE-CELL is at the beginning of a row group.
3871 INFO is a plist used as a communication channel."
3872 ;; A cell starts a column group either when it is at the beginning
3873 ;; of a row (or after the special column, if any) or when it has
3874 ;; a left border.
3875 (or (eq (org-element-map
3876 (org-export-get-parent table-cell)
3877 'table-cell 'identity info 'first-match)
3878 table-cell)
3879 (memq 'left (org-export-table-cell-borders table-cell info))))
3881 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3882 "Non-nil when TABLE-CELL is at the end of a row group.
3883 INFO is a plist used as a communication channel."
3884 ;; A cell ends a column group either when it is at the end of a row
3885 ;; or when it has a right border.
3886 (or (eq (car (last (org-element-contents
3887 (org-export-get-parent table-cell))))
3888 table-cell)
3889 (memq 'right (org-export-table-cell-borders table-cell info))))
3891 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3892 "Non-nil when TABLE-ROW is at the beginning of a column group.
3893 INFO is a plist used as a communication channel."
3894 (unless (or (eq (org-element-property :type table-row) 'rule)
3895 (org-export-table-row-is-special-p table-row info))
3896 (let ((borders (org-export-table-cell-borders
3897 (car (org-element-contents table-row)) info)))
3898 (or (memq 'top borders) (memq 'above borders)))))
3900 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3901 "Non-nil when TABLE-ROW is at the end of a column group.
3902 INFO is a plist used as a communication channel."
3903 (unless (or (eq (org-element-property :type table-row) 'rule)
3904 (org-export-table-row-is-special-p table-row info))
3905 (let ((borders (org-export-table-cell-borders
3906 (car (org-element-contents table-row)) info)))
3907 (or (memq 'bottom borders) (memq 'below borders)))))
3909 (defun org-export-table-row-starts-header-p (table-row info)
3910 "Non-nil when TABLE-ROW is the first table header's row.
3911 INFO is a plist used as a communication channel."
3912 (and (org-export-table-has-header-p
3913 (org-export-get-parent-table table-row) info)
3914 (org-export-table-row-starts-rowgroup-p table-row info)
3915 (= (org-export-table-row-group table-row info) 1)))
3917 (defun org-export-table-row-ends-header-p (table-row info)
3918 "Non-nil when TABLE-ROW is the last table header's row.
3919 INFO is a plist used as a communication channel."
3920 (and (org-export-table-has-header-p
3921 (org-export-get-parent-table table-row) info)
3922 (org-export-table-row-ends-rowgroup-p table-row info)
3923 (= (org-export-table-row-group table-row info) 1)))
3925 (defun org-export-table-dimensions (table info)
3926 "Return TABLE dimensions.
3928 INFO is a plist used as a communication channel.
3930 Return value is a CONS like (ROWS . COLUMNS) where
3931 ROWS (resp. COLUMNS) is the number of exportable
3932 rows (resp. columns)."
3933 (let (first-row (columns 0) (rows 0))
3934 ;; Set number of rows, and extract first one.
3935 (org-element-map
3936 table 'table-row
3937 (lambda (row)
3938 (when (eq (org-element-property :type row) 'standard)
3939 (incf rows)
3940 (unless first-row (setq first-row row)))) info)
3941 ;; Set number of columns.
3942 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
3943 ;; Return value.
3944 (cons rows columns)))
3946 (defun org-export-table-cell-address (table-cell info)
3947 "Return address of a regular TABLE-CELL object.
3949 TABLE-CELL is the cell considered. INFO is a plist used as
3950 a communication channel.
3952 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3953 zero-based index. Only exportable cells are considered. The
3954 function returns nil for other cells."
3955 (let* ((table-row (org-export-get-parent table-cell))
3956 (table (org-export-get-parent-table table-cell)))
3957 ;; Ignore cells in special rows or in special column.
3958 (unless (or (org-export-table-row-is-special-p table-row info)
3959 (and (org-export-table-has-special-column-p table)
3960 (eq (car (org-element-contents table-row)) table-cell)))
3961 (cons
3962 ;; Row number.
3963 (let ((row-count 0))
3964 (org-element-map
3965 table 'table-row
3966 (lambda (row)
3967 (cond ((eq (org-element-property :type row) 'rule) nil)
3968 ((eq row table-row) row-count)
3969 (t (incf row-count) nil)))
3970 info 'first-match))
3971 ;; Column number.
3972 (let ((col-count 0))
3973 (org-element-map
3974 table-row 'table-cell
3975 (lambda (cell)
3976 (if (eq cell table-cell) col-count (incf col-count) nil))
3977 info 'first-match))))))
3979 (defun org-export-get-table-cell-at (address table info)
3980 "Return regular table-cell object at ADDRESS in TABLE.
3982 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3983 zero-based index. TABLE is a table type element. INFO is
3984 a plist used as a communication channel.
3986 If no table-cell, among exportable cells, is found at ADDRESS,
3987 return nil."
3988 (let ((column-pos (cdr address)) (column-count 0))
3989 (org-element-map
3990 ;; Row at (car address) or nil.
3991 (let ((row-pos (car address)) (row-count 0))
3992 (org-element-map
3993 table 'table-row
3994 (lambda (row)
3995 (cond ((eq (org-element-property :type row) 'rule) nil)
3996 ((= row-count row-pos) row)
3997 (t (incf row-count) nil)))
3998 info 'first-match))
3999 'table-cell
4000 (lambda (cell)
4001 (if (= column-count column-pos) cell
4002 (incf column-count) nil))
4003 info 'first-match)))
4006 ;;;; For Tables Of Contents
4008 ;; `org-export-collect-headlines' builds a list of all exportable
4009 ;; headline elements, maybe limited to a certain depth. One can then
4010 ;; easily parse it and transcode it.
4012 ;; Building lists of tables, figures or listings is quite similar.
4013 ;; Once the generic function `org-export-collect-elements' is defined,
4014 ;; `org-export-collect-tables', `org-export-collect-figures' and
4015 ;; `org-export-collect-listings' can be derived from it.
4017 (defun org-export-collect-headlines (info &optional n)
4018 "Collect headlines in order to build a table of contents.
4020 INFO is a plist used as a communication channel.
4022 When optional argument N is an integer, it specifies the depth of
4023 the table of contents. Otherwise, it is set to the value of the
4024 last headline level. See `org-export-headline-levels' for more
4025 information.
4027 Return a list of all exportable headlines as parsed elements."
4028 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4029 (org-element-map
4030 (plist-get info :parse-tree)
4031 'headline
4032 (lambda (headline)
4033 ;; Strip contents from HEADLINE.
4034 (let ((relative-level (org-export-get-relative-level headline info)))
4035 (unless (> relative-level n) headline)))
4036 info))
4038 (defun org-export-collect-elements (type info &optional predicate)
4039 "Collect referenceable elements of a determined type.
4041 TYPE can be a symbol or a list of symbols specifying element
4042 types to search. Only elements with a caption are collected.
4044 INFO is a plist used as a communication channel.
4046 When non-nil, optional argument PREDICATE is a function accepting
4047 one argument, an element of type TYPE. It returns a non-nil
4048 value when that element should be collected.
4050 Return a list of all elements found, in order of appearance."
4051 (org-element-map
4052 (plist-get info :parse-tree) type
4053 (lambda (element)
4054 (and (org-element-property :caption element)
4055 (or (not predicate) (funcall predicate element))
4056 element))
4057 info))
4059 (defun org-export-collect-tables (info)
4060 "Build a list of tables.
4061 INFO is a plist used as a communication channel.
4063 Return a list of table elements with a caption."
4064 (org-export-collect-elements 'table info))
4066 (defun org-export-collect-figures (info predicate)
4067 "Build a list of figures.
4069 INFO is a plist used as a communication channel. PREDICATE is
4070 a function which accepts one argument: a paragraph element and
4071 whose return value is non-nil when that element should be
4072 collected.
4074 A figure is a paragraph type element, with a caption, verifying
4075 PREDICATE. The latter has to be provided since a \"figure\" is
4076 a vague concept that may depend on back-end.
4078 Return a list of elements recognized as figures."
4079 (org-export-collect-elements 'paragraph info predicate))
4081 (defun org-export-collect-listings (info)
4082 "Build a list of src blocks.
4084 INFO is a plist used as a communication channel.
4086 Return a list of src-block elements with a caption."
4087 (org-export-collect-elements 'src-block info))
4090 ;;;; Topology
4092 ;; Here are various functions to retrieve information about the
4093 ;; neighbourhood of a given element or object. Neighbours of interest
4094 ;; are direct parent (`org-export-get-parent'), parent headline
4095 ;; (`org-export-get-parent-headline'), first element containing an
4096 ;; object, (`org-export-get-parent-element'), parent table
4097 ;; (`org-export-get-parent-table'), previous element or object
4098 ;; (`org-export-get-previous-element') and next element or object
4099 ;; (`org-export-get-next-element').
4101 ;; `org-export-get-genealogy' returns the full genealogy of a given
4102 ;; element or object, from closest parent to full parse tree.
4104 (defun org-export-get-parent (blob)
4105 "Return BLOB parent or nil.
4106 BLOB is the element or object considered."
4107 (org-element-property :parent blob))
4109 (defun org-export-get-genealogy (blob)
4110 "Return full genealogy relative to a given element or object.
4112 BLOB is the element or object being considered.
4114 Ancestors are returned from closest to farthest, the last one
4115 being the full parse tree."
4116 (let (genealogy (parent blob))
4117 (while (setq parent (org-element-property :parent parent))
4118 (push parent genealogy))
4119 (nreverse genealogy)))
4121 (defun org-export-get-parent-headline (blob)
4122 "Return BLOB parent headline or nil.
4123 BLOB is the element or object being considered."
4124 (let ((parent blob))
4125 (while (and (setq parent (org-element-property :parent parent))
4126 (not (eq (org-element-type parent) 'headline))))
4127 parent))
4129 (defun org-export-get-parent-element (object)
4130 "Return first element containing OBJECT or nil.
4131 OBJECT is the object to consider."
4132 (let ((parent object))
4133 (while (and (setq parent (org-element-property :parent parent))
4134 (memq (org-element-type parent) org-element-all-objects)))
4135 parent))
4137 (defun org-export-get-parent-table (object)
4138 "Return OBJECT parent table or nil.
4139 OBJECT is either a `table-cell' or `table-element' type object."
4140 (let ((parent object))
4141 (while (and (setq parent (org-element-property :parent parent))
4142 (not (eq (org-element-type parent) 'table))))
4143 parent))
4145 (defun org-export-get-previous-element (blob info)
4146 "Return previous element or object.
4147 BLOB is an element or object. INFO is a plist used as
4148 a communication channel. Return previous exportable element or
4149 object, a string, or nil."
4150 (let (prev)
4151 (catch 'exit
4152 (mapc (lambda (obj)
4153 (cond ((eq obj blob) (throw 'exit prev))
4154 ((memq obj (plist-get info :ignore-list)))
4155 (t (setq prev obj))))
4156 (org-element-contents (org-export-get-parent blob))))))
4158 (defun org-export-get-next-element (blob info)
4159 "Return next element or object.
4160 BLOB is an element or object. INFO is a plist used as
4161 a communication channel. Return next exportable element or
4162 object, a string, or nil."
4163 (catch 'found
4164 (mapc (lambda (obj)
4165 (unless (memq obj (plist-get info :ignore-list))
4166 (throw 'found obj)))
4167 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4168 nil))
4171 ;;;; Translation
4173 ;; `org-export-translate' translates a string according to language
4174 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4175 ;; variable and a specified charset. `org-export-dictionary' contains
4176 ;; the dictionary used for the translation.
4178 (defconst org-export-dictionary
4179 '(("Author"
4180 ("ca" :default "Autor")
4181 ("cs" :default "Autor")
4182 ("da" :default "Ophavsmand")
4183 ("de" :default "Autor")
4184 ("eo" :html "A&#365;toro")
4185 ("es" :default "Autor")
4186 ("fi" :html "Tekij&auml;")
4187 ("fr" :default "Auteur")
4188 ("hu" :default "Szerz&otilde;")
4189 ("is" :html "H&ouml;fundur")
4190 ("it" :default "Autore")
4191 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4192 ("nl" :default "Auteur")
4193 ("no" :default "Forfatter")
4194 ("nb" :default "Forfatter")
4195 ("nn" :default "Forfattar")
4196 ("pl" :default "Autor")
4197 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4198 ("sv" :html "F&ouml;rfattare")
4199 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4200 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4201 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4202 ("Date"
4203 ("ca" :default "Data")
4204 ("cs" :default "Datum")
4205 ("da" :default "Dato")
4206 ("de" :default "Datum")
4207 ("eo" :default "Dato")
4208 ("es" :default "Fecha")
4209 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4210 ("hu" :html "D&aacute;tum")
4211 ("is" :default "Dagsetning")
4212 ("it" :default "Data")
4213 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4214 ("nl" :default "Datum")
4215 ("no" :default "Dato")
4216 ("nb" :default "Dato")
4217 ("nn" :default "Dato")
4218 ("pl" :default "Data")
4219 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4220 ("sv" :default "Datum")
4221 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4222 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4223 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4224 ("Equation"
4225 ("fr" :ascii "Equation" :default "Équation"))
4226 ("Figure")
4227 ("Footnotes"
4228 ("ca" :html "Peus de p&agrave;gina")
4229 ("cs" :default "Pozn\xe1mky pod carou")
4230 ("da" :default "Fodnoter")
4231 ("de" :html "Fu&szlig;noten")
4232 ("eo" :default "Piednotoj")
4233 ("es" :html "Pies de p&aacute;gina")
4234 ("fi" :default "Alaviitteet")
4235 ("fr" :default "Notes de bas de page")
4236 ("hu" :html "L&aacute;bjegyzet")
4237 ("is" :html "Aftanm&aacute;lsgreinar")
4238 ("it" :html "Note a pi&egrave; di pagina")
4239 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4240 ("nl" :default "Voetnoten")
4241 ("no" :default "Fotnoter")
4242 ("nb" :default "Fotnoter")
4243 ("nn" :default "Fotnotar")
4244 ("pl" :default "Przypis")
4245 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4246 ("sv" :default "Fotnoter")
4247 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4248 :utf-8 "Примітки")
4249 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4250 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4251 ("List of Listings"
4252 ("fr" :default "Liste des programmes"))
4253 ("List of Tables"
4254 ("fr" :default "Liste des tableaux"))
4255 ("Listing %d:"
4256 ("fr"
4257 :ascii "Programme %d :" :default "Programme nº %d :"
4258 :latin1 "Programme %d :"))
4259 ("Listing %d: %s"
4260 ("fr"
4261 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4262 :latin1 "Programme %d : %s"))
4263 ("See section %s"
4264 ("fr" :default "cf. section %s"))
4265 ("Table %d:"
4266 ("fr"
4267 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4268 ("Table %d: %s"
4269 ("fr"
4270 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4271 :latin1 "Tableau %d : %s"))
4272 ("Table of Contents"
4273 ("ca" :html "&Iacute;ndex")
4274 ("cs" :default "Obsah")
4275 ("da" :default "Indhold")
4276 ("de" :default "Inhaltsverzeichnis")
4277 ("eo" :default "Enhavo")
4278 ("es" :html "&Iacute;ndice")
4279 ("fi" :html "Sis&auml;llysluettelo")
4280 ("fr" :ascii "Sommaire" :default "Table des matières")
4281 ("hu" :html "Tartalomjegyz&eacute;k")
4282 ("is" :default "Efnisyfirlit")
4283 ("it" :default "Indice")
4284 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4285 ("nl" :default "Inhoudsopgave")
4286 ("no" :default "Innhold")
4287 ("nb" :default "Innhold")
4288 ("nn" :default "Innhald")
4289 ("pl" :html "Spis tre&#x015b;ci")
4290 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4291 :utf-8 "Содержание")
4292 ("sv" :html "Inneh&aring;ll")
4293 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4294 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4295 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4296 ("Unknown reference"
4297 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4298 "Dictionary for export engine.
4300 Alist whose CAR is the string to translate and CDR is an alist
4301 whose CAR is the language string and CDR is a plist whose
4302 properties are possible charsets and values translated terms.
4304 It is used as a database for `org-export-translate'. Since this
4305 function returns the string as-is if no translation was found,
4306 the variable only needs to record values different from the
4307 entry.")
4309 (defun org-export-translate (s encoding info)
4310 "Translate string S according to language specification.
4312 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4313 and `:utf-8'. INFO is a plist used as a communication channel.
4315 Translation depends on `:language' property. Return the
4316 translated string. If no translation is found, try to fall back
4317 to `:default' encoding. If it fails, return S."
4318 (let* ((lang (plist-get info :language))
4319 (translations (cdr (assoc lang
4320 (cdr (assoc s org-export-dictionary))))))
4321 (or (plist-get translations encoding)
4322 (plist-get translations :default)
4323 s)))
4327 ;;; The Dispatcher
4329 ;; `org-export-dispatch' is the standard interactive way to start an
4330 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4331 ;; for its interface. Most commons back-ends should have an entry in
4332 ;; it.
4334 ;;;###autoload
4335 (defun org-export-dispatch ()
4336 "Export dispatcher for Org mode.
4338 It provides an access to common export related tasks in a buffer.
4339 Its interface comes in two flavours: standard and expert. While
4340 both share the same set of bindings, only the former displays the
4341 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4342 to switch to one or the other.
4344 Return an error if key pressed has no associated command."
4345 (interactive)
4346 (let* ((input (org-export-dispatch-ui
4347 (if (listp org-export-initial-scope) org-export-initial-scope
4348 (list org-export-initial-scope))
4349 org-export-dispatch-use-expert-ui))
4350 (raw-key (car input))
4351 (optns (cdr input)))
4352 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4353 ;; depending on user's key pressed.
4354 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
4355 ;; Allow to quit with "q" key.
4356 (?q nil)
4357 ;; Export with `e-ascii' back-end.
4358 ((?A ?N ?U)
4359 (org-e-ascii-export-as-ascii
4360 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4361 `(:ascii-charset ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8)))))
4362 ((?a ?n ?u)
4363 (org-e-ascii-export-to-ascii
4364 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4365 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
4366 ;; Export with `e-latex' back-end.
4367 (?L (org-e-latex-export-as-latex
4368 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4370 (org-e-latex-export-to-latex
4371 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4373 (org-e-latex-export-to-pdf
4374 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4376 (org-open-file
4377 (org-e-latex-export-to-pdf
4378 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4379 ;; Export with `e-html' back-end.
4381 (org-e-html-export-as-html
4382 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4384 (org-e-html-export-to-html
4385 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4387 (org-open-file
4388 (org-e-html-export-to-html
4389 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4390 ;; Export with `e-odt' back-end.
4392 (org-e-odt-export-to-odt
4393 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4395 (org-open-file
4396 (org-e-odt-export-to-odt
4397 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4398 'system))
4399 ;; Export with `e-freemind' back-end.
4401 (org-e-freemind-export-to-freemind
4402 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4404 (org-open-file
4405 (org-e-freemind-export-to-freemind
4406 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4407 'system))
4408 ;; Publishing facilities
4410 (org-e-publish-current-file (memq 'force optns)))
4412 (org-e-publish-current-project (memq 'force optns)))
4414 (let ((project
4415 (assoc (org-icompleting-read
4416 "Publish project: " org-e-publish-project-alist nil t)
4417 org-e-publish-project-alist)))
4418 (org-e-publish project (memq 'force optns))))
4420 (org-e-publish-all (memq 'force optns)))
4421 ;; Undefined command.
4422 (t (error "No command associated with key %s"
4423 (char-to-string raw-key))))))
4425 (defun org-export-dispatch-ui (options expertp)
4426 "Handle interface for `org-export-dispatch'.
4428 OPTIONS is a list containing current interactive options set for
4429 export. It can contain any of the following symbols:
4430 `body' toggles a body-only export
4431 `subtree' restricts export to current subtree
4432 `visible' restricts export to visible part of buffer.
4433 `force' force publishing files.
4435 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4436 buffer is provided, but indications about currently active
4437 options are given in the prompt. Moreover, \[?] allows to switch
4438 back to standard interface.
4440 Return value is a list with key pressed as CAR and a list of
4441 final interactive export options as CDR."
4442 (let ((help
4443 (format "---- (Options) -------------------------------------------
4445 \[1] Body only: %s [2] Export scope: %s
4446 \[3] Visible only: %s [4] Force publishing: %s
4449 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4451 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4453 --- (HTML Export) ----------------------------------------
4455 \[h] to HTML file [b] ... and open it
4456 \[H] to temporary buffer
4458 --- (LaTeX Export) ---------------------------------------
4460 \[l] to TEX file [L] to temporary buffer
4461 \[p] to PDF file [d] ... and open it
4463 --- (ODF Export) -----------------------------------------
4465 \[o] to ODT file [O] ... and open it
4467 --- (Publish) --------------------------------------------
4469 \[F] current file [P] current project
4470 \[X] a project [E] every project"
4471 (if (memq 'body options) "On " "Off")
4472 (if (memq 'subtree options) "Subtree" "Buffer ")
4473 (if (memq 'visible options) "On " "Off")
4474 (if (memq 'force options) "On " "Off")))
4475 (standard-prompt "Export command: ")
4476 (expert-prompt (format "Export command (%s%s%s%s): "
4477 (if (memq 'body options) "b" "-")
4478 (if (memq 'subtree options) "s" "-")
4479 (if (memq 'visible options) "v" "-")
4480 (if (memq 'force options) "f" "-")))
4481 (handle-keypress
4482 (function
4483 ;; Read a character from command input, toggling interactive
4484 ;; options when applicable. PROMPT is the displayed prompt,
4485 ;; as a string.
4486 (lambda (prompt)
4487 (let ((key (read-char-exclusive prompt)))
4488 (cond
4489 ;; Ignore non-standard characters (i.e. "M-a").
4490 ((not (characterp key)) (org-export-dispatch-ui options expertp))
4491 ;; Help key: Switch back to standard interface if
4492 ;; expert UI was active.
4493 ((eq key ??) (org-export-dispatch-ui options nil))
4494 ;; Toggle export options.
4495 ((memq key '(?1 ?2 ?3 ?4))
4496 (org-export-dispatch-ui
4497 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
4498 (?4 'force))))
4499 (if (memq option options) (remq option options)
4500 (cons option options)))
4501 expertp))
4502 ;; Action selected: Send key and options back to
4503 ;; `org-export-dispatch'.
4504 (t (cons key options))))))))
4505 ;; With expert UI, just read key with a fancy prompt. In standard
4506 ;; UI, display an intrusive help buffer.
4507 (if expertp (funcall handle-keypress expert-prompt)
4508 (save-window-excursion
4509 (delete-other-windows)
4510 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
4511 (org-fit-window-to-buffer
4512 (get-buffer-window "*Org Export/Publishing Help*"))
4513 (funcall handle-keypress standard-prompt)))))
4516 (provide 'org-export)
4517 ;;; org-export.el ends here