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