org-export: Fix *:nil export option
[org-mode.git] / contrib / lisp / org-export.el
blob2597109622f7c2c90187adbdf0f6219985782aa1
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 defines one mandatory information: his translation table.
52 ;; Its value is an alist whose keys are elements and objects types and
53 ;; values translator functions. See function's docstring for more
54 ;; information about translators.
56 ;; Optionally, `org-export-define-backend' can also support specific
57 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
58 ;; function documentation for more information.
60 ;; If the new back-end shares most properties with another one,
61 ;; `org-export-define-derived-backend' can be used to simplify the
62 ;; process.
64 ;; Any back-end can define its own variables. Among them, those
65 ;; customizable should belong to the `org-export-BACKEND' group.
67 ;; Tools for common tasks across back-ends are implemented in the
68 ;; penultimate part of this file. A dispatcher for standard back-ends
69 ;; is provided in the last one.
71 ;;; Code:
73 (eval-when-compile (require 'cl))
74 (require 'org-element)
75 (require 'ob-exp)
77 (declare-function org-e-publish "org-e-publish" (project &optional force))
78 (declare-function org-e-publish-all "org-e-publish" (&optional force))
79 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
80 (declare-function org-e-publish-current-project "org-e-publish"
81 (&optional force))
83 (defvar org-e-publish-project-alist)
84 (defvar org-table-number-fraction)
85 (defvar org-table-number-regexp)
89 ;;; Internal Variables
91 ;; Among internal variables, the most important is
92 ;; `org-export-options-alist'. This variable define the global export
93 ;; options, shared between every exporter, and how they are acquired.
95 (defconst org-export-max-depth 19
96 "Maximum nesting depth for headlines, counting from 0.")
98 (defconst org-export-options-alist
99 '((:author "AUTHOR" nil user-full-name t)
100 (:creator "CREATOR" nil org-export-creator-string)
101 (:date "DATE" nil nil t)
102 (:description "DESCRIPTION" nil nil newline)
103 (:email "EMAIL" nil user-mail-address t)
104 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
105 (:headline-levels nil "H" org-export-headline-levels)
106 (:keywords "KEYWORDS" nil nil space)
107 (:language "LANGUAGE" nil org-export-default-language t)
108 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
109 (:section-numbers nil "num" org-export-with-section-numbers)
110 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
111 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
112 (:title "TITLE" nil nil space)
113 (:with-archived-trees nil "arch" org-export-with-archived-trees)
114 (:with-author nil "author" org-export-with-author)
115 (:with-clocks nil "c" org-export-with-clocks)
116 (:with-creator nil "creator" org-export-with-creator)
117 (:with-date nil "date" org-export-with-date)
118 (:with-drawers nil "d" org-export-with-drawers)
119 (:with-email nil "email" org-export-with-email)
120 (:with-emphasize nil "*" org-export-with-emphasize)
121 (:with-entities nil "e" org-export-with-entities)
122 (:with-fixed-width nil ":" org-export-with-fixed-width)
123 (:with-footnotes nil "f" org-export-with-footnotes)
124 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
125 (:with-plannings nil "p" org-export-with-planning)
126 (:with-priority nil "pri" org-export-with-priority)
127 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
128 (:with-special-strings nil "-" org-export-with-special-strings)
129 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
130 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
131 (:with-toc nil "toc" org-export-with-toc)
132 (:with-tables nil "|" org-export-with-tables)
133 (:with-tags nil "tags" org-export-with-tags)
134 (:with-tasks nil "tasks" org-export-with-tasks)
135 (:with-timestamps nil "<" org-export-with-timestamps)
136 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
137 "Alist between export properties and ways to set them.
139 The CAR of the alist is the property name, and the CDR is a list
140 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
142 KEYWORD is a string representing a buffer keyword, or nil. Each
143 property defined this way can also be set, during subtree
144 export, through an headline property named after the keyword
145 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
146 property).
147 OPTION is a string that could be found in an #+OPTIONS: line.
148 DEFAULT is the default value for the property.
149 BEHAVIOUR determine how Org should handle multiple keywords for
150 the same property. It is a symbol among:
151 nil Keep old value and discard the new one.
152 t Replace old value with the new one.
153 `space' Concatenate the values, separating them with a space.
154 `newline' Concatenate the values, separating them with
155 a newline.
156 `split' Split values at white spaces, and cons them to the
157 previous list.
159 KEYWORD and OPTION have precedence over DEFAULT.
161 All these properties should be back-end agnostic. Back-end
162 specific properties are set through `org-export-define-backend'.
163 Properties redefined there have precedence over these.")
165 (defconst org-export-special-keywords '("FILETAGS" "SETUP_FILE" "OPTIONS")
166 "List of in-buffer keywords that require special treatment.
167 These keywords are not directly associated to a property. The
168 way they are handled must be hard-coded into
169 `org-export--get-inbuffer-options' function.")
171 (defconst org-export-filters-alist
172 '((:filter-bold . org-export-filter-bold-functions)
173 (:filter-babel-call . org-export-filter-babel-call-functions)
174 (:filter-center-block . org-export-filter-center-block-functions)
175 (:filter-clock . org-export-filter-clock-functions)
176 (:filter-code . org-export-filter-code-functions)
177 (:filter-comment . org-export-filter-comment-functions)
178 (:filter-comment-block . org-export-filter-comment-block-functions)
179 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
180 (:filter-drawer . org-export-filter-drawer-functions)
181 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
182 (:filter-entity . org-export-filter-entity-functions)
183 (:filter-example-block . org-export-filter-example-block-functions)
184 (:filter-export-block . org-export-filter-export-block-functions)
185 (:filter-export-snippet . org-export-filter-export-snippet-functions)
186 (:filter-final-output . org-export-filter-final-output-functions)
187 (:filter-fixed-width . org-export-filter-fixed-width-functions)
188 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
189 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
190 (:filter-headline . org-export-filter-headline-functions)
191 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
192 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
193 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
194 (:filter-inlinetask . org-export-filter-inlinetask-functions)
195 (:filter-italic . org-export-filter-italic-functions)
196 (:filter-item . org-export-filter-item-functions)
197 (:filter-keyword . org-export-filter-keyword-functions)
198 (:filter-latex-environment . org-export-filter-latex-environment-functions)
199 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
200 (:filter-line-break . org-export-filter-line-break-functions)
201 (:filter-link . org-export-filter-link-functions)
202 (:filter-macro . org-export-filter-macro-functions)
203 (:filter-node-property . org-export-filter-node-property-functions)
204 (:filter-paragraph . org-export-filter-paragraph-functions)
205 (:filter-parse-tree . org-export-filter-parse-tree-functions)
206 (:filter-plain-list . org-export-filter-plain-list-functions)
207 (:filter-plain-text . org-export-filter-plain-text-functions)
208 (:filter-planning . org-export-filter-planning-functions)
209 (:filter-property-drawer . org-export-filter-property-drawer-functions)
210 (:filter-quote-block . org-export-filter-quote-block-functions)
211 (:filter-quote-section . org-export-filter-quote-section-functions)
212 (:filter-radio-target . org-export-filter-radio-target-functions)
213 (:filter-section . org-export-filter-section-functions)
214 (:filter-special-block . org-export-filter-special-block-functions)
215 (:filter-src-block . org-export-filter-src-block-functions)
216 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
217 (:filter-strike-through . org-export-filter-strike-through-functions)
218 (:filter-subscript . org-export-filter-subscript-functions)
219 (:filter-superscript . org-export-filter-superscript-functions)
220 (:filter-table . org-export-filter-table-functions)
221 (:filter-table-cell . org-export-filter-table-cell-functions)
222 (:filter-table-row . org-export-filter-table-row-functions)
223 (:filter-target . org-export-filter-target-functions)
224 (:filter-timestamp . org-export-filter-timestamp-functions)
225 (:filter-underline . org-export-filter-underline-functions)
226 (:filter-verbatim . org-export-filter-verbatim-functions)
227 (:filter-verse-block . org-export-filter-verse-block-functions))
228 "Alist between filters properties and initial values.
230 The key of each association is a property name accessible through
231 the communication channel. Its value is a configurable global
232 variable defining initial filters.
234 This list is meant to install user specified filters. Back-end
235 developers may install their own filters using
236 `org-export-define-backend'. Filters defined there will always
237 be prepended to the current list, so they always get applied
238 first.")
240 (defconst org-export-default-inline-image-rule
241 `(("file" .
242 ,(format "\\.%s\\'"
243 (regexp-opt
244 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
245 "xpm" "pbm" "pgm" "ppm") t))))
246 "Default rule for link matching an inline image.
247 This rule applies to links with no description. By default, it
248 will be considered as an inline image if it targets a local file
249 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
250 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
251 See `org-export-inline-image-p' for more information about
252 rules.")
254 (defvar org-export-registered-backends nil
255 "List of backends currently available in the exporter.
257 A backend is stored as a list where CAR is its name, as a symbol,
258 and CDR is a plist with the following properties:
259 `:filters-alist', `:menu-entry', `:options-alist' and
260 `:translate-alist'.
262 This variable is set with `org-export-define-backend' and
263 `org-export-define-derived-backend' functions.")
267 ;;; User-configurable Variables
269 ;; Configuration for the masses.
271 ;; They should never be accessed directly, as their value is to be
272 ;; stored in a property list (cf. `org-export-options-alist').
273 ;; Back-ends will read their value from there instead.
275 (defgroup org-export nil
276 "Options for exporting Org mode files."
277 :tag "Org Export"
278 :group 'org)
280 (defgroup org-export-general nil
281 "General options for export engine."
282 :tag "Org Export General"
283 :group 'org-export)
285 (defcustom org-export-with-archived-trees 'headline
286 "Whether sub-trees with the ARCHIVE tag should be exported.
288 This can have three different values:
289 nil Do not export, pretend this tree is not present.
290 t Do export the entire tree.
291 `headline' Only export the headline, but skip the tree below it.
293 This option can also be set with the #+OPTIONS line,
294 e.g. \"arch:nil\"."
295 :group 'org-export-general
296 :type '(choice
297 (const :tag "Not at all" nil)
298 (const :tag "Headline only" 'headline)
299 (const :tag "Entirely" t)))
301 (defcustom org-export-with-author t
302 "Non-nil means insert author name into the exported file.
303 This option can also be set with the #+OPTIONS line,
304 e.g. \"author:nil\"."
305 :group 'org-export-general
306 :type 'boolean)
308 (defcustom org-export-with-clocks nil
309 "Non-nil means export CLOCK keywords.
310 This option can also be set with the #+OPTIONS line,
311 e.g. \"c:t\"."
312 :group 'org-export-general
313 :type 'boolean)
315 (defcustom org-export-with-creator 'comment
316 "Non-nil means the postamble should contain a creator sentence.
318 The sentence can be set in `org-export-creator-string' and
319 defaults to \"Generated by Org mode XX in Emacs XXX.\".
321 If the value is `comment' insert it as a comment."
322 :group 'org-export-general
323 :type '(choice
324 (const :tag "No creator sentence" nil)
325 (const :tag "Sentence as a comment" 'comment)
326 (const :tag "Insert the sentence" t)))
328 (defcustom org-export-with-date t
329 "Non-nil means insert date in the exported document.
330 This options can also be set with the OPTIONS keyword,
331 e.g. \"date:nil\".")
333 (defcustom org-export-creator-string
334 (format "Generated by Org mode %s in Emacs %s."
335 (if (fboundp 'org-version) (org-version) "(Unknown)")
336 emacs-version)
337 "String to insert at the end of the generated document."
338 :group 'org-export-general
339 :type '(string :tag "Creator string"))
341 (defcustom org-export-with-drawers t
342 "Non-nil means export contents of standard drawers.
344 When t, all drawers are exported. This may also be a list of
345 drawer names to export. This variable doesn't apply to
346 properties drawers.
348 This option can also be set with the #+OPTIONS line,
349 e.g. \"d:nil\"."
350 :group 'org-export-general
351 :type '(choice
352 (const :tag "All drawers" t)
353 (const :tag "None" nil)
354 (repeat :tag "Selected drawers"
355 (string :tag "Drawer name"))))
357 (defcustom org-export-with-email nil
358 "Non-nil means insert author email into the exported file.
359 This option can also be set with the #+OPTIONS line,
360 e.g. \"email:t\"."
361 :group 'org-export-general
362 :type 'boolean)
364 (defcustom org-export-with-emphasize t
365 "Non-nil means interpret *word*, /word/, _word_ and +word+.
367 If the export target supports emphasizing text, the word will be
368 typeset in bold, italic, with an underline or strike-through,
369 respectively.
371 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
372 :group 'org-export-general
373 :type 'boolean)
375 (defcustom org-export-exclude-tags '("noexport")
376 "Tags that exclude a tree from export.
378 All trees carrying any of these tags will be excluded from
379 export. This is without condition, so even subtrees inside that
380 carry one of the `org-export-select-tags' will be removed.
382 This option can also be set with the #+EXCLUDE_TAGS: keyword."
383 :group 'org-export-general
384 :type '(repeat (string :tag "Tag")))
386 (defcustom org-export-with-fixed-width t
387 "Non-nil means lines starting with \":\" will be in fixed width font.
389 This can be used to have pre-formatted text, fragments of code
390 etc. For example:
391 : ;; Some Lisp examples
392 : (while (defc cnt)
393 : (ding))
394 will be looking just like this in also HTML. See also the QUOTE
395 keyword. Not all export backends support this.
397 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
398 :group 'org-export-translation
399 :type 'boolean)
401 (defcustom org-export-with-footnotes t
402 "Non-nil means Org footnotes should be exported.
403 This option can also be set with the #+OPTIONS line,
404 e.g. \"f:nil\"."
405 :group 'org-export-general
406 :type 'boolean)
408 (defcustom org-export-headline-levels 3
409 "The last level which is still exported as a headline.
411 Inferior levels will produce itemize lists when exported.
413 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
414 :group 'org-export-general
415 :type 'integer)
417 (defcustom org-export-default-language "en"
418 "The default language for export and clocktable translations, as a string.
419 This may have an association in
420 `org-clock-clocktable-language-setup'."
421 :group 'org-export-general
422 :type '(string :tag "Language"))
424 (defcustom org-export-preserve-breaks nil
425 "Non-nil means preserve all line breaks when exporting.
427 Normally, in HTML output paragraphs will be reformatted.
429 This option can also be set with the #+OPTIONS line,
430 e.g. \"\\n:t\"."
431 :group 'org-export-general
432 :type 'boolean)
434 (defcustom org-export-with-entities t
435 "Non-nil means interpret entities when exporting.
437 For example, HTML export converts \\alpha to &alpha; and \\AA to
438 &Aring;.
440 For a list of supported names, see the constant `org-entities'
441 and the user option `org-entities-user'.
443 This option can also be set with the #+OPTIONS line,
444 e.g. \"e:nil\"."
445 :group 'org-export-general
446 :type 'boolean)
448 (defcustom org-export-with-inlinetasks t
449 "Non-nil means inlinetasks should be exported.
450 This option can also be set with the #+OPTIONS line,
451 e.g. \"inline:nil\"."
452 :group 'org-export-general
453 :type 'boolean)
455 (defcustom org-export-with-planning nil
456 "Non-nil means include planning info in export.
457 This option can also be set with the #+OPTIONS: line,
458 e.g. \"p:t\"."
459 :group 'org-export-general
460 :type 'boolean)
462 (defcustom org-export-with-priority nil
463 "Non-nil means include priority cookies in export.
464 This option can also be set with the #+OPTIONS line,
465 e.g. \"pri:t\"."
466 :group 'org-export-general
467 :type 'boolean)
469 (defcustom org-export-with-section-numbers t
470 "Non-nil means add section numbers to headlines when exporting.
472 When set to an integer n, numbering will only happen for
473 headlines whose relative level is higher or equal to n.
475 This option can also be set with the #+OPTIONS line,
476 e.g. \"num:t\"."
477 :group 'org-export-general
478 :type 'boolean)
480 (defcustom org-export-select-tags '("export")
481 "Tags that select a tree for export.
483 If any such tag is found in a buffer, all trees that do not carry
484 one of these tags will be ignored during export. Inside trees
485 that are selected like this, you can still deselect a subtree by
486 tagging it with one of the `org-export-exclude-tags'.
488 This option can also be set with the #+SELECT_TAGS: keyword."
489 :group 'org-export-general
490 :type '(repeat (string :tag "Tag")))
492 (defcustom org-export-with-smart-quotes nil
493 "Non-nil means activate smart quotes during export.
494 This option can also be set with the #+OPTIONS: line,
495 e.g. \"':t\"."
496 :group 'org-export-general
497 :type 'boolean)
499 (defcustom org-export-with-special-strings t
500 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
502 When this option is turned on, these strings will be exported as:
504 Org HTML LaTeX UTF-8
505 -----+----------+--------+-------
506 \\- &shy; \\-
507 -- &ndash; -- –
508 --- &mdash; --- —
509 ... &hellip; \\ldots …
511 This option can also be set with the #+OPTIONS line,
512 e.g. \"-:nil\"."
513 :group 'org-export-general
514 :type 'boolean)
516 (defcustom org-export-with-statistics-cookies t
517 "Non-nil means include statistics cookies in export.
518 This option can also be set with the #+OPTIONS: line,
519 e.g. \"stat:nil\""
520 :group 'org-export-general
521 :type 'boolean)
523 (defcustom org-export-with-sub-superscripts t
524 "Non-nil means interpret \"_\" and \"^\" for export.
526 When this option is turned on, you can use TeX-like syntax for
527 sub- and superscripts. Several characters after \"_\" or \"^\"
528 will be considered as a single item - so grouping with {} is
529 normally not needed. For example, the following things will be
530 parsed as single sub- or superscripts.
532 10^24 or 10^tau several digits will be considered 1 item.
533 10^-12 or 10^-tau a leading sign with digits or a word
534 x^2-y^3 will be read as x^2 - y^3, because items are
535 terminated by almost any nonword/nondigit char.
536 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
538 Still, ambiguity is possible - so when in doubt use {} to enclose
539 the sub/superscript. If you set this variable to the symbol
540 `{}', the braces are *required* in order to trigger
541 interpretations as sub/superscript. This can be helpful in
542 documents that need \"_\" frequently in plain text.
544 This option can also be set with the #+OPTIONS line,
545 e.g. \"^:nil\"."
546 :group 'org-export-general
547 :type '(choice
548 (const :tag "Interpret them" t)
549 (const :tag "Curly brackets only" {})
550 (const :tag "Do not interpret them" nil)))
552 (defcustom org-export-with-toc t
553 "Non-nil means create a table of contents in exported files.
555 The TOC contains headlines with levels up
556 to`org-export-headline-levels'. When an integer, include levels
557 up to N in the toc, this may then be different from
558 `org-export-headline-levels', but it will not be allowed to be
559 larger than the number of headline levels. When nil, no table of
560 contents is made.
562 This option can also be set with the #+OPTIONS line,
563 e.g. \"toc:nil\" or \"toc:3\"."
564 :group 'org-export-general
565 :type '(choice
566 (const :tag "No Table of Contents" nil)
567 (const :tag "Full Table of Contents" t)
568 (integer :tag "TOC to level")))
570 (defcustom org-export-with-tables t
571 "If non-nil, lines starting with \"|\" define a table.
572 For example:
574 | Name | Address | Birthday |
575 |-------------+----------+-----------|
576 | Arthur Dent | England | 29.2.2100 |
578 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
579 :group 'org-export-general
580 :type 'boolean)
582 (defcustom org-export-with-tags t
583 "If nil, do not export tags, just remove them from headlines.
585 If this is the symbol `not-in-toc', tags will be removed from
586 table of contents entries, but still be shown in the headlines of
587 the document.
589 This option can also be set with the #+OPTIONS line,
590 e.g. \"tags:nil\"."
591 :group 'org-export-general
592 :type '(choice
593 (const :tag "Off" nil)
594 (const :tag "Not in TOC" not-in-toc)
595 (const :tag "On" t)))
597 (defcustom org-export-with-tasks t
598 "Non-nil means include TODO items for export.
600 This may have the following values:
601 t include tasks independent of state.
602 `todo' include only tasks that are not yet done.
603 `done' include only tasks that are already done.
604 nil ignore all tasks.
605 list of keywords include tasks with these keywords.
607 This option can also be set with the #+OPTIONS line,
608 e.g. \"tasks:nil\"."
609 :group 'org-export-general
610 :type '(choice
611 (const :tag "All tasks" t)
612 (const :tag "No tasks" nil)
613 (const :tag "Not-done tasks" todo)
614 (const :tag "Only done tasks" done)
615 (repeat :tag "Specific TODO keywords"
616 (string :tag "Keyword"))))
618 (defcustom org-export-time-stamp-file t
619 "Non-nil means insert a time stamp into the exported file.
620 The time stamp shows when the file was created.
622 This option can also be set with the #+OPTIONS line,
623 e.g. \"timestamp:nil\"."
624 :group 'org-export-general
625 :type 'boolean)
627 (defcustom org-export-with-timestamps t
628 "Non nil means allow timestamps in export.
630 It can be set to `active', `inactive', t or nil, in order to
631 export, respectively, only active timestamps, only inactive ones,
632 all of them or none.
634 This option can also be set with the #+OPTIONS line, e.g.
635 \"<:nil\"."
636 :group 'org-export-general
637 :type '(choice
638 (const :tag "All timestamps" t)
639 (const :tag "Only active timestamps" active)
640 (const :tag "Only inactive timestamps" inactive)
641 (const :tag "No timestamp" nil)))
643 (defcustom org-export-with-todo-keywords t
644 "Non-nil means include TODO keywords in export.
645 When nil, remove all these keywords from the export."
646 :group 'org-export-general
647 :type 'boolean)
649 (defcustom org-export-allow-BIND 'confirm
650 "Non-nil means allow #+BIND to define local variable values for export.
651 This is a potential security risk, which is why the user must
652 confirm the use of these lines."
653 :group 'org-export-general
654 :type '(choice
655 (const :tag "Never" nil)
656 (const :tag "Always" t)
657 (const :tag "Ask a confirmation for each file" confirm)))
659 (defcustom org-export-snippet-translation-alist nil
660 "Alist between export snippets back-ends and exporter back-ends.
662 This variable allows to provide shortcuts for export snippets.
664 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
665 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
666 HTML code while every other back-end will ignore it."
667 :group 'org-export-general
668 :type '(repeat
669 (cons
670 (string :tag "Shortcut")
671 (string :tag "Back-end"))))
673 (defcustom org-export-coding-system nil
674 "Coding system for the exported file."
675 :group 'org-export-general
676 :type 'coding-system)
678 (defcustom org-export-copy-to-kill-ring t
679 "Non-nil means exported stuff will also be pushed onto the kill ring."
680 :group 'org-export-general
681 :type 'boolean)
683 (defcustom org-export-initial-scope 'buffer
684 "The initial scope when exporting with `org-export-dispatch'.
685 This variable can be either set to `buffer' or `subtree'."
686 :group 'org-export-general
687 :type '(choice
688 (const :tag "Export current buffer" 'buffer)
689 (const :tag "Export current subtree" 'subtree)))
691 (defcustom org-export-show-temporary-export-buffer t
692 "Non-nil means show buffer after exporting to temp buffer.
693 When Org exports to a file, the buffer visiting that file is ever
694 shown, but remains buried. However, when exporting to
695 a temporary buffer, that buffer is popped up in a second window.
696 When this variable is nil, the buffer remains buried also in
697 these cases."
698 :group 'org-export-general
699 :type 'boolean)
701 (defcustom org-export-dispatch-use-expert-ui nil
702 "Non-nil means using a non-intrusive `org-export-dispatch'.
703 In that case, no help buffer is displayed. Though, an indicator
704 for current export scope is added to the prompt (\"b\" when
705 output is restricted to body only, \"s\" when it is restricted to
706 the current subtree, \"v\" when only visible elements are
707 considered for export and \"f\" when publishing functions should
708 be passed the FORCE argument). Also, \[?] allows to switch back
709 to standard mode."
710 :group 'org-export-general
711 :type 'boolean)
715 ;;; Defining Back-ends
717 ;; `org-export-define-backend' is the standard way to define an export
718 ;; back-end. It allows to specify translators, filters, buffer
719 ;; options and a menu entry. If the new back-end shares translators
720 ;; with another back-end, `org-export-define-derived-backend' may be
721 ;; used instead.
723 ;; Internally, a back-end is stored as a list, of which CAR is the
724 ;; name of the back-end, as a symbol, and CDR a plist. Accessors to
725 ;; properties of a given back-end are: `org-export-backend-filters',
726 ;; `org-export-backend-menu', `org-export-backend-options' and
727 ;; `org-export-backend-translate-table'.
729 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
730 ;; when a given back-end hasn't been registered yet.
732 (defmacro org-export-define-backend (backend translators &rest body)
733 "Define a new back-end BACKEND.
735 TRANSLATORS is an alist between object or element types and
736 functions handling them.
738 These functions should return a string without any trailing
739 space, or nil. They must accept three arguments: the object or
740 element itself, its contents or nil when it isn't recursive and
741 the property list used as a communication channel.
743 Contents, when not nil, are stripped from any global indentation
744 \(although the relative one is preserved). They also always end
745 with a single newline character.
747 If, for a given type, no function is found, that element or
748 object type will simply be ignored, along with any blank line or
749 white space at its end. The same will happen if the function
750 returns the nil value. If that function returns the empty
751 string, the type will be ignored, but the blank lines or white
752 spaces will be kept.
754 In addition to element and object types, one function can be
755 associated to the `template' symbol and another one to the
756 `plain-text' symbol.
758 The former returns the final transcoded string, and can be used
759 to add a preamble and a postamble to document's body. It must
760 accept two arguments: the transcoded string and the property list
761 containing export options.
763 The latter, when defined, is to be called on every text not
764 recognized as an element or an object. It must accept two
765 arguments: the text string and the information channel. It is an
766 appropriate place to protect special chars relative to the
767 back-end.
769 BODY can start with pre-defined keyword arguments. The following
770 keywords are understood:
772 :export-block
774 String, or list of strings, representing block names that
775 will not be parsed. This is used to specify blocks that will
776 contain raw code specific to the back-end. These blocks
777 still have to be handled by the relative `export-block' type
778 translator.
780 :filters-alist
782 Alist between filters and function, or list of functions,
783 specific to the back-end. See `org-export-filters-alist' for
784 a list of all allowed filters. Filters defined here
785 shouldn't make a back-end test, as it may prevent back-ends
786 derived from this one to behave properly.
788 :menu-entry
790 Menu entry for the export dispatcher. It should be a list
791 like:
793 \(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
795 where :
797 KEY is a free character selecting the back-end.
799 DESCRIPTION-OR-ORDINAL is either a string or a number.
801 If it is a string, is will be used to name the back-end in
802 its menu entry. If it is a number, the following menu will
803 be displayed as a sub-menu of the back-end with the same
804 KEY. Also, the number will be used to determine in which
805 order such sub-menus will appear (lowest first).
807 ACTION-OR-MENU is either a function or an alist.
809 If it is an action, it will be called with three arguments:
810 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
811 for further explanations.
813 If it is an alist, associations should follow the
814 pattern:
816 \(KEY DESCRIPTION ACTION)
818 where KEY, DESCRIPTION and ACTION are described above.
820 Valid values include:
822 \(?m \"My Special Back-end\" my-special-export-function)
826 \(?l \"Export to LaTeX\"
827 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
828 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
829 \(?p \"PDF file\" org-e-latex-export-to-pdf)
830 \(?o \"PDF file and open\"
831 \(lambda (subtree visible body-only)
832 \(org-open-file
833 \(org-e-latex-export-to-pdf subtree visible body-only))))))
835 :options-alist
837 Alist between back-end specific properties introduced in
838 communication channel and how their value are acquired. See
839 `org-export-options-alist' for more information about
840 structure of the values."
841 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
842 (indent 1))
843 (let (export-block filters menu-entry options contents)
844 (while (keywordp (car body))
845 (case (pop body)
846 (:export-block (let ((names (pop body)))
847 (setq export-block
848 (if (consp names) (mapcar 'upcase names)
849 (list (upcase names))))))
850 (:filters-alist (setq filters (pop body)))
851 (:menu-entry (setq menu-entry (pop body)))
852 (:options-alist (setq options (pop body)))
853 (t (pop body))))
854 (setq contents (append (list :translate-alist translators)
855 (and filters (list :filters-alist filters))
856 (and options (list :options-alist options))
857 (and menu-entry (list :menu-entry menu-entry))))
858 `(progn
859 ;; Register back-end.
860 (let ((registeredp (assq ',backend org-export-registered-backends)))
861 (if registeredp (setcdr registeredp ',contents)
862 (push (cons ',backend ',contents) org-export-registered-backends)))
863 ;; Tell parser to not parse EXPORT-BLOCK blocks.
864 ,(when export-block
865 `(mapc
866 (lambda (name)
867 (add-to-list 'org-element-block-name-alist
868 `(,name . org-element-export-block-parser)))
869 ',export-block))
870 ;; Splice in the body, if any.
871 ,@body)))
873 (defmacro org-export-define-derived-backend (child parent &rest body)
874 "Create a new back-end as a variant of an existing one.
876 CHILD is the name of the derived back-end. PARENT is the name of
877 the parent back-end.
879 BODY can start with pre-defined keyword arguments. The following
880 keywords are understood:
882 :export-block
884 String, or list of strings, representing block names that
885 will not be parsed. This is used to specify blocks that will
886 contain raw code specific to the back-end. These blocks
887 still have to be handled by the relative `export-block' type
888 translator.
890 :filters-alist
892 Alist of filters that will overwrite or complete filters
893 defined in PARENT back-end. See `org-export-filters-alist'
894 for a list of allowed filters.
896 :menu-entry
898 Menu entry for the export dispatcher. See
899 `org-export-define-backend' for more information about the
900 expected value.
902 :options-alist
904 Alist of back-end specific properties that will overwrite or
905 complete those defined in PARENT back-end. Refer to
906 `org-export-options-alist' for more information about
907 structure of the values.
909 :translate-alist
911 Alist of element and object types and transcoders that will
912 overwrite or complete transcode table from PARENT back-end.
913 Refer to `org-export-define-backend' for detailed information
914 about transcoders.
916 As an example, here is how one could define \"my-latex\" back-end
917 as a variant of `e-latex' back-end with a custom template
918 function:
920 \(org-export-define-derived-backend my-latex e-latex
921 :translate-alist ((template . my-latex-template-fun)))
923 The back-end could then be called with, for example:
925 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
926 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
927 (indent 2))
928 (org-export-barf-if-invalid-backend parent)
929 (let (export-block filters menu-entry options translators contents)
930 (while (keywordp (car body))
931 (case (pop body)
932 (:export-block (let ((names (pop body)))
933 (setq export-block
934 (if (consp names) (mapcar 'upcase names)
935 (list (upcase names))))))
936 (:filters-alist (setq filters (pop body)))
937 (:menu-entry (setq menu-entry (pop body)))
938 (:options-alist (setq options (pop body)))
939 (:translate-alist (setq translators (pop body)))
940 (t (pop body))))
941 (setq contents (append
942 (list :parent parent)
943 (let ((p-table (org-export-backend-translate-table parent)))
944 (list :translate-alist (append translators p-table)))
945 (let ((p-filters (org-export-backend-filters parent)))
946 (list :filters-alist (append filters p-filters)))
947 (let ((p-options (org-export-backend-options parent)))
948 (list :options-alist (append options p-options)))
949 (and menu-entry (list :menu-entry menu-entry))))
950 `(progn
951 ;; Register back-end.
952 (let ((registeredp (assq ',child org-export-registered-backends)))
953 (if registeredp (setcdr registeredp ',contents)
954 (push (cons ',child ',contents) org-export-registered-backends)))
955 ;; Tell parser to not parse EXPORT-BLOCK blocks.
956 ,(when export-block
957 `(mapc
958 (lambda (name)
959 (add-to-list 'org-element-block-name-alist
960 `(,name . org-element-export-block-parser)))
961 ',export-block))
962 ;; Splice in the body, if any.
963 ,@body)))
965 (defun org-export-backend-filters (backend)
966 "Return filters for BACKEND."
967 (plist-get (cdr (assq backend org-export-registered-backends))
968 :filters-alist))
970 (defun org-export-backend-menu (backend)
971 "Return menu entry for BACKEND."
972 (plist-get (cdr (assq backend org-export-registered-backends))
973 :menu-entry))
975 (defun org-export-backend-options (backend)
976 "Return export options for BACKEND."
977 (plist-get (cdr (assq backend org-export-registered-backends))
978 :options-alist))
980 (defun org-export-backend-translate-table (backend)
981 "Return translate table for BACKEND."
982 (plist-get (cdr (assq backend org-export-registered-backends))
983 :translate-alist))
985 (defun org-export-barf-if-invalid-backend (backend)
986 "Signal an error if BACKEND isn't defined."
987 (unless (org-export-backend-translate-table backend)
988 (error "Unknown \"%s\" back-end: Aborting export" backend)))
990 (defun org-export-derived-backend-p (backend &rest backends)
991 "Non-nil if BACKEND is derived from one of BACKENDS."
992 (let ((parent backend))
993 (while (and (not (memq parent backends))
994 (setq parent
995 (plist-get (cdr (assq parent
996 org-export-registered-backends))
997 :parent))))
998 parent))
1002 ;;; The Communication Channel
1004 ;; During export process, every function has access to a number of
1005 ;; properties. They are of two types:
1007 ;; 1. Environment options are collected once at the very beginning of
1008 ;; the process, out of the original buffer and configuration.
1009 ;; Collecting them is handled by `org-export-get-environment'
1010 ;; function.
1012 ;; Most environment options are defined through the
1013 ;; `org-export-options-alist' variable.
1015 ;; 2. Tree properties are extracted directly from the parsed tree,
1016 ;; just before export, by `org-export-collect-tree-properties'.
1018 ;; Here is the full list of properties available during transcode
1019 ;; process, with their category and their value type.
1021 ;; + `:author' :: Author's name.
1022 ;; - category :: option
1023 ;; - type :: string
1025 ;; + `:back-end' :: Current back-end used for transcoding.
1026 ;; - category :: tree
1027 ;; - type :: symbol
1029 ;; + `:creator' :: String to write as creation information.
1030 ;; - category :: option
1031 ;; - type :: string
1033 ;; + `:date' :: String to use as date.
1034 ;; - category :: option
1035 ;; - type :: string
1037 ;; + `:description' :: Description text for the current data.
1038 ;; - category :: option
1039 ;; - type :: string
1041 ;; + `:email' :: Author's email.
1042 ;; - category :: option
1043 ;; - type :: string
1045 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1046 ;; process.
1047 ;; - category :: option
1048 ;; - type :: list of strings
1050 ;; + `:exported-data' :: Hash table used for memoizing
1051 ;; `org-export-data'.
1052 ;; - category :: tree
1053 ;; - type :: hash table
1055 ;; + `:filetags' :: List of global tags for buffer. Used by
1056 ;; `org-export-get-tags' to get tags with inheritance.
1057 ;; - category :: option
1058 ;; - type :: list of strings
1060 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1061 ;; their definition, as parsed data. Only non-inlined footnotes
1062 ;; are represented in this alist. Also, every definition isn't
1063 ;; guaranteed to be referenced in the parse tree. The purpose of
1064 ;; this property is to preserve definitions from oblivion
1065 ;; (i.e. when the parse tree comes from a part of the original
1066 ;; buffer), it isn't meant for direct use in a back-end. To
1067 ;; retrieve a definition relative to a reference, use
1068 ;; `org-export-get-footnote-definition' instead.
1069 ;; - category :: option
1070 ;; - type :: alist (STRING . LIST)
1072 ;; + `:headline-levels' :: Maximum level being exported as an
1073 ;; headline. Comparison is done with the relative level of
1074 ;; headlines in the parse tree, not necessarily with their
1075 ;; actual level.
1076 ;; - category :: option
1077 ;; - type :: integer
1079 ;; + `:headline-offset' :: Difference between relative and real level
1080 ;; of headlines in the parse tree. For example, a value of -1
1081 ;; means a level 2 headline should be considered as level
1082 ;; 1 (cf. `org-export-get-relative-level').
1083 ;; - category :: tree
1084 ;; - type :: integer
1086 ;; + `:headline-numbering' :: Alist between headlines and their
1087 ;; numbering, as a list of numbers
1088 ;; (cf. `org-export-get-headline-number').
1089 ;; - category :: tree
1090 ;; - type :: alist (INTEGER . LIST)
1092 ;; + `:id-alist' :: Alist between ID strings and destination file's
1093 ;; path, relative to current directory. It is used by
1094 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1095 ;; external file.
1096 ;; - category :: option
1097 ;; - type :: alist (STRING . STRING)
1099 ;; + `:ignore-list' :: List of elements and objects that should be
1100 ;; ignored during export.
1101 ;; - category :: tree
1102 ;; - type :: list of elements and objects
1104 ;; + `:input-file' :: Full path to input file, if any.
1105 ;; - category :: option
1106 ;; - type :: string or nil
1108 ;; + `:keywords' :: List of keywords attached to data.
1109 ;; - category :: option
1110 ;; - type :: string
1112 ;; + `:language' :: Default language used for translations.
1113 ;; - category :: option
1114 ;; - type :: string
1116 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1117 ;; transcoding.
1118 ;; - category :: option
1119 ;; - type :: list (as returned by `org-element-parse-buffer')
1121 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1122 ;; all line breaks.
1123 ;; - category :: option
1124 ;; - type :: symbol (nil, t)
1126 ;; + `:section-numbers' :: Non-nil means transcoding should add
1127 ;; section numbers to headlines.
1128 ;; - category :: option
1129 ;; - type :: symbol (nil, t)
1131 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1132 ;; in transcoding. When such a tag is present, subtrees without
1133 ;; it are de facto excluded from the process. See
1134 ;; `use-select-tags'.
1135 ;; - category :: option
1136 ;; - type :: list of strings
1138 ;; + `:target-list' :: List of targets encountered in the parse tree.
1139 ;; This is used to partly resolve "fuzzy" links
1140 ;; (cf. `org-export-resolve-fuzzy-link').
1141 ;; - category :: tree
1142 ;; - type :: list of strings
1144 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1145 ;; a time stamp in the output.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:translate-alist' :: Alist between element and object types and
1150 ;; transcoding functions relative to the current back-end.
1151 ;; Special keys `template' and `plain-text' are also possible.
1152 ;; - category :: option
1153 ;; - type :: alist (SYMBOL . FUNCTION)
1155 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1156 ;; also be transcoded. If it is set to the `headline' symbol,
1157 ;; only the archived headline's name is retained.
1158 ;; - category :: option
1159 ;; - type :: symbol (nil, t, `headline')
1161 ;; + `:with-author' :: Non-nil means author's name should be included
1162 ;; in the output.
1163 ;; - category :: option
1164 ;; - type :: symbol (nil, t)
1166 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, t)
1170 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1171 ;; inserted at the end of the transcoded string. If the value
1172 ;; is `comment', it should be commented.
1173 ;; - category :: option
1174 ;; - type :: symbol (`comment', nil, t)
1176 ;; + `:with-date' :: Non-nil means output should contain a date.
1177 ;; - category :: option
1178 ;; - type :. symbol (nil, t)
1180 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1181 ;; its value is a list of names, only drawers with such names
1182 ;; will be transcoded.
1183 ;; - category :: option
1184 ;; - type :: symbol (nil, t) or list of strings
1186 ;; + `:with-email' :: Non-nil means output should contain author's
1187 ;; email.
1188 ;; - category :: option
1189 ;; - type :: symbol (nil, t)
1191 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1192 ;; interpreted.
1193 ;; - category :: option
1194 ;; - type :: symbol (nil, t)
1196 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1197 ;; strings starting with a colon as a fixed-with (verbatim) area.
1198 ;; - category :: option
1199 ;; - type :: symbol (nil, t)
1201 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1202 ;; footnotes.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, t)
1206 ;; + `:with-plannings' :: Non-nil means transcoding should include
1207 ;; planning info.
1208 ;; - category :: option
1209 ;; - type :: symbol (nil, t)
1211 ;; + `:with-priority' :: Non-nil means transcoding should include
1212 ;; priority cookies.
1213 ;; - category :: option
1214 ;; - type :: symbol (nil, t)
1216 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1217 ;; plain text.
1218 ;; - category :: option
1219 ;; - type :: symbol (nil, t)
1221 ;; + `:with-special-strings' :: Non-nil means transcoding should
1222 ;; interpret special strings in plain text.
1223 ;; - category :: option
1224 ;; - type :: symbol (nil, t)
1226 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1227 ;; interpret subscript and superscript. With a value of "{}",
1228 ;; only interpret those using curly brackets.
1229 ;; - category :: option
1230 ;; - type :: symbol (nil, {}, t)
1232 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1233 ;; tables.
1234 ;; - category :: option
1235 ;; - type :: symbol (nil, t)
1237 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1238 ;; headlines. A `not-in-toc' value will remove them from the
1239 ;; table of contents, if any, nonetheless.
1240 ;; - category :: option
1241 ;; - type :: symbol (nil, t, `not-in-toc')
1243 ;; + `:with-tasks' :: Non-nil means transcoding should include
1244 ;; headlines with a TODO keyword. A `todo' value will only
1245 ;; include headlines with a todo type keyword while a `done'
1246 ;; value will do the contrary. If a list of strings is provided,
1247 ;; only tasks with keywords belonging to that list will be kept.
1248 ;; - category :: option
1249 ;; - type :: symbol (t, todo, done, nil) or list of strings
1251 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1252 ;; time stamps. Special value `active' (resp. `inactive') ask to
1253 ;; export only active (resp. inactive) timestamps. Otherwise,
1254 ;; completely remove them.
1255 ;; - category :: option
1256 ;; - type :: symbol: (`active', `inactive', t, nil)
1258 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1259 ;; added to the output. An integer value limits its depth.
1260 ;; - category :: option
1261 ;; - type :: symbol (nil, t or integer)
1263 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1264 ;; include TODO keywords.
1265 ;; - category :: option
1266 ;; - type :: symbol (nil, t)
1269 ;;;; Environment Options
1271 ;; Environment options encompass all parameters defined outside the
1272 ;; scope of the parsed data. They come from five sources, in
1273 ;; increasing precedence order:
1275 ;; - Global variables,
1276 ;; - Buffer's attributes,
1277 ;; - Options keyword symbols,
1278 ;; - Buffer keywords,
1279 ;; - Subtree properties.
1281 ;; The central internal function with regards to environment options
1282 ;; is `org-export-get-environment'. It updates global variables with
1283 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1284 ;; the different sources.
1286 ;; The internal functions doing the retrieval are:
1287 ;; `org-export--get-global-options',
1288 ;; `org-export--get-buffer-attributes',
1289 ;; `org-export--parse-option-keyword',
1290 ;; `org-export--get-subtree-options' and
1291 ;; `org-export--get-inbuffer-options'
1293 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1294 ;; take care of the part relative to "#+BIND:" keywords.
1296 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1297 "Collect export options from the current buffer.
1299 Optional argument BACKEND is a symbol specifying which back-end
1300 specific options to read, if any.
1302 When optional argument SUBTREEP is non-nil, assume the export is
1303 done against the current sub-tree.
1305 Third optional argument EXT-PLIST is a property list with
1306 external parameters overriding Org default settings, but still
1307 inferior to file-local settings."
1308 ;; First install #+BIND variables.
1309 (org-export--install-letbind-maybe)
1310 ;; Get and prioritize export options...
1311 (org-combine-plists
1312 ;; ... from global variables...
1313 (org-export--get-global-options backend)
1314 ;; ... from an external property list...
1315 ext-plist
1316 ;; ... from in-buffer settings...
1317 (org-export--get-inbuffer-options
1318 backend
1319 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1320 ;; ... and from subtree, when appropriate.
1321 (and subtreep (org-export--get-subtree-options backend))
1322 ;; Eventually add misc. properties.
1323 (list
1324 :back-end
1325 backend
1326 :translate-alist
1327 (org-export-backend-translate-table backend)
1328 :footnote-definition-alist
1329 ;; Footnotes definitions must be collected in the original
1330 ;; buffer, as there's no insurance that they will still be in
1331 ;; the parse tree, due to possible narrowing.
1332 (let (alist)
1333 (org-with-wide-buffer
1334 (goto-char (point-min))
1335 (while (re-search-forward org-footnote-definition-re nil t)
1336 (let ((def (save-match-data (org-element-at-point))))
1337 (when (eq (org-element-type def) 'footnote-definition)
1338 (push
1339 (cons (org-element-property :label def)
1340 (let ((cbeg (org-element-property :contents-begin def)))
1341 (when cbeg
1342 (org-element--parse-elements
1343 cbeg (org-element-property :contents-end def)
1344 nil nil nil nil (list 'org-data nil)))))
1345 alist))))
1346 alist))
1347 :id-alist
1348 ;; Collect id references.
1349 (let (alist)
1350 (org-with-wide-buffer
1351 (goto-char (point-min))
1352 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1353 (let ((link (org-element-context)))
1354 (when (eq (org-element-type link) 'link)
1355 (let* ((id (org-element-property :path link))
1356 (file (org-id-find-id-file id)))
1357 (when file
1358 (push (cons id (file-relative-name file)) alist)))))))
1359 alist))))
1361 (defun org-export--parse-option-keyword (options &optional backend)
1362 "Parse an OPTIONS line and return values as a plist.
1363 Optional argument BACKEND is a symbol specifying which back-end
1364 specific items to read, if any."
1365 (let* ((all (append org-export-options-alist
1366 (and backend (org-export-backend-options backend))))
1367 ;; Build an alist between #+OPTION: item and property-name.
1368 (alist (delq nil
1369 (mapcar (lambda (e)
1370 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1371 (car e))))
1372 all)))
1373 plist)
1374 (mapc (lambda (e)
1375 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1376 (car e)
1377 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1378 options)
1379 (setq plist (plist-put plist
1380 (cdr e)
1381 (car (read-from-string
1382 (match-string 2 options)))))))
1383 alist)
1384 plist))
1386 (defun org-export--get-subtree-options (&optional backend)
1387 "Get export options in subtree at point.
1388 Optional argument BACKEND is a symbol specifying back-end used
1389 for export. Return options as a plist."
1390 ;; For each buffer keyword, create an headline property setting the
1391 ;; same property in communication channel. The name for the property
1392 ;; is the keyword with "EXPORT_" appended to it.
1393 (org-with-wide-buffer
1394 (let (prop plist)
1395 ;; Make sure point is at an heading.
1396 (unless (org-at-heading-p) (org-back-to-heading t))
1397 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1398 ;; title as its fallback value.
1399 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1400 (progn (looking-at org-todo-line-regexp)
1401 (org-match-string-no-properties 3))))
1402 (setq plist
1403 (plist-put
1404 plist :title
1405 (org-element-parse-secondary-string
1406 prop (org-element-restriction 'keyword)))))
1407 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1408 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1409 (setq plist
1410 (nconc plist (org-export--parse-option-keyword prop backend))))
1411 ;; Handle other keywords. TITLE keyword is excluded as it has
1412 ;; been handled already.
1413 (let ((seen '("TITLE")))
1414 (mapc
1415 (lambda (option)
1416 (let ((property (nth 1 option)))
1417 (when (and property (not (member property seen)))
1418 (let* ((subtree-prop (concat "EXPORT_" property))
1419 ;; Export properties are not case-sensitive.
1420 (value (let ((case-fold-search t))
1421 (org-entry-get (point) subtree-prop))))
1422 (push property seen)
1423 (when value
1424 (setq plist
1425 (plist-put
1426 plist
1427 (car option)
1428 (cond
1429 ;; Parse VALUE if required.
1430 ((member property org-element-document-properties)
1431 (org-element-parse-secondary-string
1432 value (org-element-restriction 'keyword)))
1433 ;; If BEHAVIOUR is `split' expected value is
1434 ;; a list of strings, not a string.
1435 ((eq (nth 4 option) 'split) (org-split-string value))
1436 (t value)))))))))
1437 ;; Also look for both general keywords and back-end specific
1438 ;; options if BACKEND is provided.
1439 (append (and backend (org-export-backend-options backend))
1440 org-export-options-alist)))
1441 ;; Return value.
1442 plist)))
1444 (defun org-export--get-inbuffer-options (&optional backend files)
1445 "Return current buffer export options, as a plist.
1447 Optional argument BACKEND, when non-nil, is a symbol specifying
1448 which back-end specific options should also be read in the
1449 process.
1451 Optional argument FILES is a list of setup files names read so
1452 far, used to avoid circular dependencies.
1454 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1455 (org-with-wide-buffer
1456 (goto-char (point-min))
1457 (let ((case-fold-search t) plist)
1458 ;; 1. Special keywords, as in `org-export-special-keywords'.
1459 (let ((special-re
1460 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1461 (while (re-search-forward special-re nil t)
1462 (let ((element (org-element-at-point)))
1463 (when (eq (org-element-type element) 'keyword)
1464 (let* ((key (org-element-property :key element))
1465 (val (org-element-property :value element))
1466 (prop
1467 (cond
1468 ((equal key "SETUP_FILE")
1469 (let ((file
1470 (expand-file-name
1471 (org-remove-double-quotes (org-trim val)))))
1472 ;; Avoid circular dependencies.
1473 (unless (member file files)
1474 (with-temp-buffer
1475 (insert (org-file-contents file 'noerror))
1476 (org-mode)
1477 (org-export--get-inbuffer-options
1478 backend (cons file files))))))
1479 ((equal key "OPTIONS")
1480 (org-export--parse-option-keyword val backend))
1481 ((equal key "FILETAGS")
1482 (list :filetags
1483 (org-uniquify
1484 (append (org-split-string val ":")
1485 (plist-get plist :filetags))))))))
1486 (setq plist (org-combine-plists plist prop)))))))
1487 ;; 2. Standard options, as in `org-export-options-alist'.
1488 (let* ((all (append org-export-options-alist
1489 ;; Also look for back-end specific options if
1490 ;; BACKEND is defined.
1491 (and backend (org-export-backend-options backend))))
1492 ;; Build ALIST between keyword name and property name.
1493 (alist
1494 (delq nil (mapcar
1495 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1496 all)))
1497 ;; Build regexp matching all keywords associated to export
1498 ;; options. Note: the search is case insensitive.
1499 (opt-re (format "^[ \t]*#\\+%s:"
1500 (regexp-opt
1501 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1502 (goto-char (point-min))
1503 (while (re-search-forward opt-re nil t)
1504 (let ((element (org-element-at-point)))
1505 (when (eq (org-element-type element) 'keyword)
1506 (let* ((key (org-element-property :key element))
1507 (val (org-element-property :value element))
1508 (prop (cdr (assoc key alist)))
1509 (behaviour (nth 4 (assq prop all))))
1510 (setq plist
1511 (plist-put
1512 plist prop
1513 ;; Handle value depending on specified BEHAVIOUR.
1514 (case behaviour
1515 (space
1516 (if (not (plist-get plist prop)) (org-trim val)
1517 (concat (plist-get plist prop) " " (org-trim val))))
1518 (newline
1519 (org-trim
1520 (concat (plist-get plist prop) "\n" (org-trim val))))
1521 (split
1522 `(,@(plist-get plist prop) ,@(org-split-string val)))
1523 ('t val)
1524 (otherwise (if (not (plist-member plist prop)) val
1525 (plist-get plist prop))))))))))
1526 ;; Parse keywords specified in
1527 ;; `org-element-document-properties'.
1528 (mapc
1529 (lambda (key)
1530 (let* ((prop (cdr (assoc key alist)))
1531 (value (and prop (plist-get plist prop))))
1532 (when (stringp value)
1533 (setq plist
1534 (plist-put
1535 plist prop
1536 (org-element-parse-secondary-string
1537 value (org-element-restriction 'keyword)))))))
1538 org-element-document-properties))
1539 ;; 3. Return final value.
1540 plist)))
1542 (defun org-export--get-buffer-attributes ()
1543 "Return properties related to buffer attributes, as a plist."
1544 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1545 (list
1546 ;; Store full path of input file name, or nil. For internal use.
1547 :input-file visited-file
1548 :title (or (and visited-file
1549 (file-name-sans-extension
1550 (file-name-nondirectory visited-file)))
1551 (buffer-name (buffer-base-buffer))))))
1553 (defun org-export--get-global-options (&optional backend)
1554 "Return global export options as a plist.
1556 Optional argument BACKEND, if non-nil, is a symbol specifying
1557 which back-end specific export options should also be read in the
1558 process."
1559 (let ((all (append org-export-options-alist
1560 (and backend (org-export-backend-options backend))))
1561 ;; Output value.
1562 plist)
1563 (mapc
1564 (lambda (cell)
1565 (setq plist
1566 (plist-put
1567 plist
1568 (car cell)
1569 ;; Eval default value provided. If keyword is a member
1570 ;; of `org-element-document-properties', parse it as
1571 ;; a secondary string before storing it.
1572 (let ((value (eval (nth 3 cell))))
1573 (if (not (stringp value)) value
1574 (let ((keyword (nth 1 cell)))
1575 (if (not (member keyword org-element-document-properties))
1576 value
1577 (org-element-parse-secondary-string
1578 value (org-element-restriction 'keyword)))))))))
1579 all)
1580 ;; Return value.
1581 plist))
1583 (defvar org-export--allow-BIND-local nil)
1584 (defun org-export--confirm-letbind ()
1585 "Can we use #+BIND values during export?
1586 By default this will ask for confirmation by the user, to divert
1587 possible security risks."
1588 (cond
1589 ((not org-export-allow-BIND) nil)
1590 ((eq org-export-allow-BIND t) t)
1591 ((local-variable-p 'org-export--allow-BIND-local)
1592 org-export--allow-BIND-local)
1593 (t (org-set-local 'org-export--allow-BIND-local
1594 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1596 (defun org-export--install-letbind-maybe ()
1597 "Install the values from #+BIND lines as local variables.
1598 Variables must be installed before in-buffer options are
1599 retrieved."
1600 (let ((case-fold-search t) letbind pair)
1601 (org-with-wide-buffer
1602 (goto-char (point-min))
1603 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1604 (let* ((element (org-element-at-point))
1605 (value (org-element-property :value element)))
1606 (when (and (eq (org-element-type element) 'keyword)
1607 (not (equal value ""))
1608 (org-export--confirm-letbind))
1609 (push (read (format "(%s)" value)) letbind)))))
1610 (dolist (pair (nreverse letbind))
1611 (org-set-local (car pair) (nth 1 pair)))))
1614 ;;;; Tree Properties
1616 ;; Tree properties are information extracted from parse tree. They
1617 ;; are initialized at the beginning of the transcoding process by
1618 ;; `org-export-collect-tree-properties'.
1620 ;; Dedicated functions focus on computing the value of specific tree
1621 ;; properties during initialization. Thus,
1622 ;; `org-export--populate-ignore-list' lists elements and objects that
1623 ;; should be skipped during export, `org-export--get-min-level' gets
1624 ;; the minimal exportable level, used as a basis to compute relative
1625 ;; level for headlines. Eventually
1626 ;; `org-export--collect-headline-numbering' builds an alist between
1627 ;; headlines and their numbering.
1629 (defun org-export-collect-tree-properties (data info)
1630 "Extract tree properties from parse tree.
1632 DATA is the parse tree from which information is retrieved. INFO
1633 is a list holding export options.
1635 Following tree properties are set or updated:
1637 `:exported-data' Hash table used to memoize results from
1638 `org-export-data'.
1640 `:footnote-definition-alist' List of footnotes definitions in
1641 original buffer and current parse tree.
1643 `:headline-offset' Offset between true level of headlines and
1644 local level. An offset of -1 means an headline
1645 of level 2 should be considered as a level
1646 1 headline in the context.
1648 `:headline-numbering' Alist of all headlines as key an the
1649 associated numbering as value.
1651 `:ignore-list' List of elements that should be ignored during
1652 export.
1654 `:target-list' List of all targets in the parse tree.
1656 Return updated plist."
1657 ;; Install the parse tree in the communication channel, in order to
1658 ;; use `org-export-get-genealogy' and al.
1659 (setq info (plist-put info :parse-tree data))
1660 ;; Get the list of elements and objects to ignore, and put it into
1661 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1662 ;; been done during parse tree filtering.
1663 (setq info
1664 (plist-put info
1665 :ignore-list
1666 (append (org-export--populate-ignore-list data info)
1667 (plist-get info :ignore-list))))
1668 ;; Compute `:headline-offset' in order to be able to use
1669 ;; `org-export-get-relative-level'.
1670 (setq info
1671 (plist-put info
1672 :headline-offset
1673 (- 1 (org-export--get-min-level data info))))
1674 ;; Update footnotes definitions list with definitions in parse tree.
1675 ;; This is required since buffer expansion might have modified
1676 ;; boundaries of footnote definitions contained in the parse tree.
1677 ;; This way, definitions in `footnote-definition-alist' are bound to
1678 ;; match those in the parse tree.
1679 (let ((defs (plist-get info :footnote-definition-alist)))
1680 (org-element-map
1681 data 'footnote-definition
1682 (lambda (fn)
1683 (push (cons (org-element-property :label fn)
1684 `(org-data nil ,@(org-element-contents fn)))
1685 defs)))
1686 (setq info (plist-put info :footnote-definition-alist defs)))
1687 ;; Properties order doesn't matter: get the rest of the tree
1688 ;; properties.
1689 (nconc
1690 `(:target-list
1691 ,(org-element-map
1692 data '(keyword target)
1693 (lambda (blob)
1694 (when (or (eq (org-element-type blob) 'target)
1695 (string= (org-element-property :key blob) "TARGET"))
1696 blob)) info)
1697 :headline-numbering ,(org-export--collect-headline-numbering data info)
1698 :exported-data ,(make-hash-table :test 'eq :size 4001))
1699 info))
1701 (defun org-export--get-min-level (data options)
1702 "Return minimum exportable headline's level in DATA.
1703 DATA is parsed tree as returned by `org-element-parse-buffer'.
1704 OPTIONS is a plist holding export options."
1705 (catch 'exit
1706 (let ((min-level 10000))
1707 (mapc
1708 (lambda (blob)
1709 (when (and (eq (org-element-type blob) 'headline)
1710 (not (memq blob (plist-get options :ignore-list))))
1711 (setq min-level
1712 (min (org-element-property :level blob) min-level)))
1713 (when (= min-level 1) (throw 'exit 1)))
1714 (org-element-contents data))
1715 ;; If no headline was found, for the sake of consistency, set
1716 ;; minimum level to 1 nonetheless.
1717 (if (= min-level 10000) 1 min-level))))
1719 (defun org-export--collect-headline-numbering (data options)
1720 "Return numbering of all exportable headlines in a parse tree.
1722 DATA is the parse tree. OPTIONS is the plist holding export
1723 options.
1725 Return an alist whose key is an headline and value is its
1726 associated numbering \(in the shape of a list of numbers\)."
1727 (let ((numbering (make-vector org-export-max-depth 0)))
1728 (org-element-map
1729 data
1730 'headline
1731 (lambda (headline)
1732 (let ((relative-level
1733 (1- (org-export-get-relative-level headline options))))
1734 (cons
1735 headline
1736 (loop for n across numbering
1737 for idx from 0 to org-export-max-depth
1738 when (< idx relative-level) collect n
1739 when (= idx relative-level) collect (aset numbering idx (1+ n))
1740 when (> idx relative-level) do (aset numbering idx 0)))))
1741 options)))
1743 (defun org-export--populate-ignore-list (data options)
1744 "Return list of elements and objects to ignore during export.
1745 DATA is the parse tree to traverse. OPTIONS is the plist holding
1746 export options."
1747 (let* (ignore
1748 walk-data
1749 ;; First find trees containing a select tag, if any.
1750 (selected (org-export--selected-trees data options))
1751 (walk-data
1752 (lambda (data)
1753 ;; Collect ignored elements or objects into IGNORE-LIST.
1754 (let ((type (org-element-type data)))
1755 (if (org-export--skip-p data options selected) (push data ignore)
1756 (if (and (eq type 'headline)
1757 (eq (plist-get options :with-archived-trees) 'headline)
1758 (org-element-property :archivedp data))
1759 ;; If headline is archived but tree below has
1760 ;; to be skipped, add it to ignore list.
1761 (mapc (lambda (e) (push e ignore))
1762 (org-element-contents data))
1763 ;; Move into secondary string, if any.
1764 (let ((sec-prop
1765 (cdr (assq type org-element-secondary-value-alist))))
1766 (when sec-prop
1767 (mapc walk-data (org-element-property sec-prop data))))
1768 ;; Move into recursive objects/elements.
1769 (mapc walk-data (org-element-contents data))))))))
1770 ;; Main call.
1771 (funcall walk-data data)
1772 ;; Return value.
1773 ignore))
1775 (defun org-export--selected-trees (data info)
1776 "Return list of headlines containing a select tag in their tree.
1777 DATA is parsed data as returned by `org-element-parse-buffer'.
1778 INFO is a plist holding export options."
1779 (let* (selected-trees
1780 walk-data ; for byte-compiler.
1781 (walk-data
1782 (function
1783 (lambda (data genealogy)
1784 (case (org-element-type data)
1785 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1786 (org-element-contents data)))
1787 (headline
1788 (let ((tags (org-element-property :tags data)))
1789 (if (loop for tag in (plist-get info :select-tags)
1790 thereis (member tag tags))
1791 ;; When a select tag is found, mark full
1792 ;; genealogy and every headline within the tree
1793 ;; as acceptable.
1794 (setq selected-trees
1795 (append
1796 genealogy
1797 (org-element-map data 'headline 'identity)
1798 selected-trees))
1799 ;; Else, continue searching in tree, recursively.
1800 (mapc
1801 (lambda (el) (funcall walk-data el (cons data genealogy)))
1802 (org-element-contents data))))))))))
1803 (funcall walk-data data nil) selected-trees))
1805 (defun org-export--skip-p (blob options selected)
1806 "Non-nil when element or object BLOB should be skipped during export.
1807 OPTIONS is the plist holding export options. SELECTED, when
1808 non-nil, is a list of headlines belonging to a tree with a select
1809 tag."
1810 (case (org-element-type blob)
1811 (clock (not (plist-get options :with-clocks)))
1812 (drawer
1813 (or (not (plist-get options :with-drawers))
1814 (and (consp (plist-get options :with-drawers))
1815 (not (member (org-element-property :drawer-name blob)
1816 (plist-get options :with-drawers))))))
1817 (headline
1818 (let ((with-tasks (plist-get options :with-tasks))
1819 (todo (org-element-property :todo-keyword blob))
1820 (todo-type (org-element-property :todo-type blob))
1821 (archived (plist-get options :with-archived-trees))
1822 (tags (org-element-property :tags blob)))
1824 ;; Ignore subtrees with an exclude tag.
1825 (loop for k in (plist-get options :exclude-tags)
1826 thereis (member k tags))
1827 ;; When a select tag is present in the buffer, ignore any tree
1828 ;; without it.
1829 (and selected (not (memq blob selected)))
1830 ;; Ignore commented sub-trees.
1831 (org-element-property :commentedp blob)
1832 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1833 (and (not archived) (org-element-property :archivedp blob))
1834 ;; Ignore tasks, if specified by `:with-tasks' property.
1835 (and todo
1836 (or (not with-tasks)
1837 (and (memq with-tasks '(todo done))
1838 (not (eq todo-type with-tasks)))
1839 (and (consp with-tasks) (not (member todo with-tasks))))))))
1840 (inlinetask (not (plist-get options :with-inlinetasks)))
1841 (planning (not (plist-get options :with-plannings)))
1842 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1843 (table-cell
1844 (and (org-export-table-has-special-column-p
1845 (org-export-get-parent-table blob))
1846 (not (org-export-get-previous-element blob options))))
1847 (table-row (org-export-table-row-is-special-p blob options))
1848 (timestamp
1849 (case (plist-get options :with-timestamps)
1850 ;; No timestamp allowed.
1851 ('nil t)
1852 ;; Only active timestamps allowed and the current one isn't
1853 ;; active.
1854 (active
1855 (not (memq (org-element-property :type blob)
1856 '(active active-range))))
1857 ;; Only inactive timestamps allowed and the current one isn't
1858 ;; inactive.
1859 (inactive
1860 (not (memq (org-element-property :type blob)
1861 '(inactive inactive-range))))))))
1865 ;;; The Transcoder
1867 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1868 ;; `org-element-parse-buffer') and transcodes it into a specified
1869 ;; back-end output. It takes care of filtering out elements or
1870 ;; objects according to export options and organizing the output blank
1871 ;; lines and white space are preserved. The function memoizes its
1872 ;; results, so it is cheap to call it within translators.
1874 ;; Internally, three functions handle the filtering of objects and
1875 ;; elements during the export. In particular,
1876 ;; `org-export-ignore-element' marks an element or object so future
1877 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1878 ;; elements or objects should be seen as real Org syntax and
1879 ;; `org-export-expand' transforms the others back into their original
1880 ;; shape
1882 ;; `org-export-transcoder' is an accessor returning appropriate
1883 ;; translator function for a given element or object.
1885 (defun org-export-transcoder (blob info)
1886 "Return appropriate transcoder for BLOB.
1887 INFO is a plist containing export directives."
1888 (let ((type (org-element-type blob)))
1889 ;; Return contents only for complete parse trees.
1890 (if (eq type 'org-data) (lambda (blob contents info) contents)
1891 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1892 (and (functionp transcoder) transcoder)))))
1894 (defun org-export-data (data info)
1895 "Convert DATA into current back-end format.
1897 DATA is a parse tree, an element or an object or a secondary
1898 string. INFO is a plist holding export options.
1900 Return transcoded string."
1901 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1902 (if (not (eq memo 'no-memo)) memo
1903 (let* ((type (org-element-type data))
1904 (results
1905 (cond
1906 ;; Ignored element/object.
1907 ((memq data (plist-get info :ignore-list)) nil)
1908 ;; Plain text. All residual text properties from parse
1909 ;; tree (i.e. `:parent' property) are removed.
1910 ((eq type 'plain-text)
1911 (org-no-properties
1912 (org-export-filter-apply-functions
1913 (plist-get info :filter-plain-text)
1914 (let ((transcoder (org-export-transcoder data info)))
1915 (if transcoder (funcall transcoder data info) data))
1916 info)))
1917 ;; Uninterpreted element/object: change it back to Org
1918 ;; syntax and export again resulting raw string.
1919 ((not (org-export--interpret-p data info))
1920 (org-export-data
1921 (org-export-expand
1922 data
1923 (mapconcat (lambda (blob) (org-export-data blob info))
1924 (org-element-contents data)
1925 ""))
1926 info))
1927 ;; Secondary string.
1928 ((not type)
1929 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1930 ;; Element/Object without contents or, as a special case,
1931 ;; headline with archive tag and archived trees restricted
1932 ;; to title only.
1933 ((or (not (org-element-contents data))
1934 (and (eq type 'headline)
1935 (eq (plist-get info :with-archived-trees) 'headline)
1936 (org-element-property :archivedp data)))
1937 (let ((transcoder (org-export-transcoder data info)))
1938 (and (functionp transcoder)
1939 (funcall transcoder data nil info))))
1940 ;; Element/Object with contents.
1942 (let ((transcoder (org-export-transcoder data info)))
1943 (when transcoder
1944 (let* ((greaterp (memq type org-element-greater-elements))
1945 (objectp
1946 (and (not greaterp)
1947 (memq type org-element-recursive-objects)))
1948 (contents
1949 (mapconcat
1950 (lambda (element) (org-export-data element info))
1951 (org-element-contents
1952 (if (or greaterp objectp) data
1953 ;; Elements directly containing objects
1954 ;; must have their indentation normalized
1955 ;; first.
1956 (org-element-normalize-contents
1957 data
1958 ;; When normalizing contents of the first
1959 ;; paragraph in an item or a footnote
1960 ;; definition, ignore first line's
1961 ;; indentation: there is none and it
1962 ;; might be misleading.
1963 (when (eq type 'paragraph)
1964 (let ((parent (org-export-get-parent data)))
1965 (and
1966 (eq (car (org-element-contents parent))
1967 data)
1968 (memq (org-element-type parent)
1969 '(footnote-definition item))))))))
1970 "")))
1971 (funcall transcoder data
1972 (if (not greaterp) contents
1973 (org-element-normalize-string contents))
1974 info))))))))
1975 ;; Final result will be memoized before being returned.
1976 (puthash
1977 data
1978 (cond
1979 ((not results) nil)
1980 ((memq type '(org-data plain-text nil)) results)
1981 ;; Append the same white space between elements or objects as in
1982 ;; the original buffer, and call appropriate filters.
1984 (let ((results
1985 (org-export-filter-apply-functions
1986 (plist-get info (intern (format ":filter-%s" type)))
1987 (let ((post-blank (or (org-element-property :post-blank data)
1988 0)))
1989 (if (memq type org-element-all-elements)
1990 (concat (org-element-normalize-string results)
1991 (make-string post-blank ?\n))
1992 (concat results (make-string post-blank ? ))))
1993 info)))
1994 results)))
1995 (plist-get info :exported-data))))))
1997 (defun org-export--interpret-p (blob info)
1998 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1999 Check is done according to export options INFO, stored as
2000 a plist."
2001 (case (org-element-type blob)
2002 ;; ... entities...
2003 (entity (plist-get info :with-entities))
2004 ;; ... emphasis...
2005 ((bold italic strike-through underline)
2006 (plist-get info :with-emphasize))
2007 ;; ... fixed-width areas.
2008 (fixed-width (plist-get info :with-fixed-width))
2009 ;; ... footnotes...
2010 ((footnote-definition footnote-reference)
2011 (plist-get info :with-footnotes))
2012 ;; ... sub/superscripts...
2013 ((subscript superscript)
2014 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2015 (if (eq sub/super-p '{})
2016 (org-element-property :use-brackets-p blob)
2017 sub/super-p)))
2018 ;; ... tables...
2019 (table (plist-get info :with-tables))
2020 (otherwise t)))
2022 (defun org-export-expand (blob contents)
2023 "Expand a parsed element or object to its original state.
2024 BLOB is either an element or an object. CONTENTS is its
2025 contents, as a string or nil."
2026 (funcall
2027 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2028 blob contents))
2030 (defun org-export-ignore-element (element info)
2031 "Add ELEMENT to `:ignore-list' in INFO.
2033 Any element in `:ignore-list' will be skipped when using
2034 `org-element-map'. INFO is modified by side effects."
2035 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2039 ;;; The Filter System
2041 ;; Filters allow end-users to tweak easily the transcoded output.
2042 ;; They are the functional counterpart of hooks, as every filter in
2043 ;; a set is applied to the return value of the previous one.
2045 ;; Every set is back-end agnostic. Although, a filter is always
2046 ;; called, in addition to the string it applies to, with the back-end
2047 ;; used as argument, so it's easy for the end-user to add back-end
2048 ;; specific filters in the set. The communication channel, as
2049 ;; a plist, is required as the third argument.
2051 ;; From the developer side, filters sets can be installed in the
2052 ;; process with the help of `org-export-define-backend', which
2053 ;; internally stores filters as an alist. Each association has a key
2054 ;; among the following symbols and a function or a list of functions
2055 ;; as value.
2057 ;; - `:filter-parse-tree' applies directly on the complete parsed
2058 ;; tree. It's the only filters set that doesn't apply to a string.
2059 ;; Users can set it through `org-export-filter-parse-tree-functions'
2060 ;; variable.
2062 ;; - `:filter-final-output' applies to the final transcoded string.
2063 ;; Users can set it with `org-export-filter-final-output-functions'
2064 ;; variable
2066 ;; - `:filter-plain-text' applies to any string not recognized as Org
2067 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2068 ;; configure it.
2070 ;; - `:filter-TYPE' applies on the string returned after an element or
2071 ;; object of type TYPE has been transcoded. An user can modify
2072 ;; `org-export-filter-TYPE-functions'
2074 ;; All filters sets are applied with
2075 ;; `org-export-filter-apply-functions' function. Filters in a set are
2076 ;; applied in a LIFO fashion. It allows developers to be sure that
2077 ;; their filters will be applied first.
2079 ;; Filters properties are installed in communication channel with
2080 ;; `org-export-install-filters' function.
2082 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2083 ;; `org-export-before-parsing-hook') are run at the beginning of the
2084 ;; export process and just before parsing to allow for heavy structure
2085 ;; modifications.
2088 ;;;; Hooks
2090 (defvar org-export-before-processing-hook nil
2091 "Hook run at the beginning of the export process.
2093 This is run before include keywords and macros are expanded and
2094 Babel code blocks executed, on a copy of the original buffer
2095 being exported. Visibility and narrowing are preserved. Point
2096 is at the beginning of the buffer.
2098 Every function in this hook will be called with one argument: the
2099 back-end currently used, as a symbol.")
2101 (defvar org-export-before-parsing-hook nil
2102 "Hook run before parsing an export buffer.
2104 This is run after include keywords and macros have been expanded
2105 and Babel code blocks executed, on a copy of the original buffer
2106 being exported. Visibility and narrowing are preserved. Point
2107 is at the beginning of the buffer.
2109 Every function in this hook will be called with one argument: the
2110 back-end currently used, as a symbol.")
2113 ;;;; Special Filters
2115 (defvar org-export-filter-parse-tree-functions nil
2116 "List of functions applied to the parsed tree.
2117 Each filter is called with three arguments: the parse tree, as
2118 returned by `org-element-parse-buffer', the back-end, as
2119 a symbol, and the communication channel, as a plist. It must
2120 return the modified parse tree to transcode.")
2122 (defvar org-export-filter-final-output-functions nil
2123 "List of functions applied to the transcoded string.
2124 Each filter is called with three arguments: the full transcoded
2125 string, the back-end, as a symbol, and the communication channel,
2126 as a plist. It must return a string that will be used as the
2127 final export output.")
2129 (defvar org-export-filter-plain-text-functions nil
2130 "List of functions applied to plain text.
2131 Each filter is called with three arguments: a string which
2132 contains no Org syntax, the back-end, as a symbol, and the
2133 communication channel, as a plist. It must return a string or
2134 nil.")
2137 ;;;; Elements Filters
2139 (defvar org-export-filter-babel-call-functions nil
2140 "List of functions applied to a transcoded babel-call.
2141 Each filter is called with three arguments: the transcoded data,
2142 as a string, the back-end, as a symbol, and the communication
2143 channel, as a plist. It must return a string or nil.")
2145 (defvar org-export-filter-center-block-functions nil
2146 "List of functions applied to a transcoded center block.
2147 Each filter is called with three arguments: the transcoded data,
2148 as a string, the back-end, as a symbol, and the communication
2149 channel, as a plist. It must return a string or nil.")
2151 (defvar org-export-filter-clock-functions nil
2152 "List of functions applied to a transcoded clock.
2153 Each filter is called with three arguments: the transcoded data,
2154 as a string, the back-end, as a symbol, and the communication
2155 channel, as a plist. It must return a string or nil.")
2157 (defvar org-export-filter-comment-functions nil
2158 "List of functions applied to a transcoded comment.
2159 Each filter is called with three arguments: the transcoded data,
2160 as a string, the back-end, as a symbol, and the communication
2161 channel, as a plist. It must return a string or nil.")
2163 (defvar org-export-filter-comment-block-functions nil
2164 "List of functions applied to a transcoded comment-block.
2165 Each filter is called with three arguments: the transcoded data,
2166 as a string, the back-end, as a symbol, and the communication
2167 channel, as a plist. It must return a string or nil.")
2169 (defvar org-export-filter-diary-sexp-functions nil
2170 "List of functions applied to a transcoded diary-sexp.
2171 Each filter is called with three arguments: the transcoded data,
2172 as a string, the back-end, as a symbol, and the communication
2173 channel, as a plist. It must return a string or nil.")
2175 (defvar org-export-filter-drawer-functions nil
2176 "List of functions applied to a transcoded drawer.
2177 Each filter is called with three arguments: the transcoded data,
2178 as a string, the back-end, as a symbol, and the communication
2179 channel, as a plist. It must return a string or nil.")
2181 (defvar org-export-filter-dynamic-block-functions nil
2182 "List of functions applied to a transcoded dynamic-block.
2183 Each filter is called with three arguments: the transcoded data,
2184 as a string, the back-end, as a symbol, and the communication
2185 channel, as a plist. It must return a string or nil.")
2187 (defvar org-export-filter-example-block-functions nil
2188 "List of functions applied to a transcoded example-block.
2189 Each filter is called with three arguments: the transcoded data,
2190 as a string, the back-end, as a symbol, and the communication
2191 channel, as a plist. It must return a string or nil.")
2193 (defvar org-export-filter-export-block-functions nil
2194 "List of functions applied to a transcoded export-block.
2195 Each filter is called with three arguments: the transcoded data,
2196 as a string, the back-end, as a symbol, and the communication
2197 channel, as a plist. It must return a string or nil.")
2199 (defvar org-export-filter-fixed-width-functions nil
2200 "List of functions applied to a transcoded fixed-width.
2201 Each filter is called with three arguments: the transcoded data,
2202 as a string, the back-end, as a symbol, and the communication
2203 channel, as a plist. It must return a string or nil.")
2205 (defvar org-export-filter-footnote-definition-functions nil
2206 "List of functions applied to a transcoded footnote-definition.
2207 Each filter is called with three arguments: the transcoded data,
2208 as a string, the back-end, as a symbol, and the communication
2209 channel, as a plist. It must return a string or nil.")
2211 (defvar org-export-filter-headline-functions nil
2212 "List of functions applied to a transcoded headline.
2213 Each filter is called with three arguments: the transcoded data,
2214 as a string, the back-end, as a symbol, and the communication
2215 channel, as a plist. It must return a string or nil.")
2217 (defvar org-export-filter-horizontal-rule-functions nil
2218 "List of functions applied to a transcoded horizontal-rule.
2219 Each filter is called with three arguments: the transcoded data,
2220 as a string, the back-end, as a symbol, and the communication
2221 channel, as a plist. It must return a string or nil.")
2223 (defvar org-export-filter-inlinetask-functions nil
2224 "List of functions applied to a transcoded inlinetask.
2225 Each filter is called with three arguments: the transcoded data,
2226 as a string, the back-end, as a symbol, and the communication
2227 channel, as a plist. It must return a string or nil.")
2229 (defvar org-export-filter-item-functions nil
2230 "List of functions applied to a transcoded item.
2231 Each filter is called with three arguments: the transcoded data,
2232 as a string, the back-end, as a symbol, and the communication
2233 channel, as a plist. It must return a string or nil.")
2235 (defvar org-export-filter-keyword-functions nil
2236 "List of functions applied to a transcoded keyword.
2237 Each filter is called with three arguments: the transcoded data,
2238 as a string, the back-end, as a symbol, and the communication
2239 channel, as a plist. It must return a string or nil.")
2241 (defvar org-export-filter-latex-environment-functions nil
2242 "List of functions applied to a transcoded latex-environment.
2243 Each filter is called with three arguments: the transcoded data,
2244 as a string, the back-end, as a symbol, and the communication
2245 channel, as a plist. It must return a string or nil.")
2247 (defvar org-export-filter-node-property-functions nil
2248 "List of functions applied to a transcoded node-property.
2249 Each filter is called with three arguments: the transcoded data,
2250 as a string, the back-end, as a symbol, and the communication
2251 channel, as a plist. It must return a string or nil.")
2253 (defvar org-export-filter-paragraph-functions nil
2254 "List of functions applied to a transcoded paragraph.
2255 Each filter is called with three arguments: the transcoded data,
2256 as a string, the back-end, as a symbol, and the communication
2257 channel, as a plist. It must return a string or nil.")
2259 (defvar org-export-filter-plain-list-functions nil
2260 "List of functions applied to a transcoded plain-list.
2261 Each filter is called with three arguments: the transcoded data,
2262 as a string, the back-end, as a symbol, and the communication
2263 channel, as a plist. It must return a string or nil.")
2265 (defvar org-export-filter-planning-functions nil
2266 "List of functions applied to a transcoded planning.
2267 Each filter is called with three arguments: the transcoded data,
2268 as a string, the back-end, as a symbol, and the communication
2269 channel, as a plist. It must return a string or nil.")
2271 (defvar org-export-filter-property-drawer-functions nil
2272 "List of functions applied to a transcoded property-drawer.
2273 Each filter is called with three arguments: the transcoded data,
2274 as a string, the back-end, as a symbol, and the communication
2275 channel, as a plist. It must return a string or nil.")
2277 (defvar org-export-filter-quote-block-functions nil
2278 "List of functions applied to a transcoded quote block.
2279 Each filter is called with three arguments: the transcoded quote
2280 data, as a string, the back-end, as a symbol, and the
2281 communication channel, as a plist. It must return a string or
2282 nil.")
2284 (defvar org-export-filter-quote-section-functions nil
2285 "List of functions applied to a transcoded quote-section.
2286 Each filter is called with three arguments: the transcoded data,
2287 as a string, the back-end, as a symbol, and the communication
2288 channel, as a plist. It must return a string or nil.")
2290 (defvar org-export-filter-section-functions nil
2291 "List of functions applied to a transcoded section.
2292 Each filter is called with three arguments: the transcoded data,
2293 as a string, the back-end, as a symbol, and the communication
2294 channel, as a plist. It must return a string or nil.")
2296 (defvar org-export-filter-special-block-functions nil
2297 "List of functions applied to a transcoded special block.
2298 Each filter is called with three arguments: the transcoded data,
2299 as a string, the back-end, as a symbol, and the communication
2300 channel, as a plist. It must return a string or nil.")
2302 (defvar org-export-filter-src-block-functions nil
2303 "List of functions applied to a transcoded src-block.
2304 Each filter is called with three arguments: the transcoded data,
2305 as a string, the back-end, as a symbol, and the communication
2306 channel, as a plist. It must return a string or nil.")
2308 (defvar org-export-filter-table-functions nil
2309 "List of functions applied to a transcoded table.
2310 Each filter is called with three arguments: the transcoded data,
2311 as a string, the back-end, as a symbol, and the communication
2312 channel, as a plist. It must return a string or nil.")
2314 (defvar org-export-filter-table-cell-functions nil
2315 "List of functions applied to a transcoded table-cell.
2316 Each filter is called with three arguments: the transcoded data,
2317 as a string, the back-end, as a symbol, and the communication
2318 channel, as a plist. It must return a string or nil.")
2320 (defvar org-export-filter-table-row-functions nil
2321 "List of functions applied to a transcoded table-row.
2322 Each filter is called with three arguments: the transcoded data,
2323 as a string, the back-end, as a symbol, and the communication
2324 channel, as a plist. It must return a string or nil.")
2326 (defvar org-export-filter-verse-block-functions nil
2327 "List of functions applied to a transcoded verse block.
2328 Each filter is called with three arguments: the transcoded data,
2329 as a string, the back-end, as a symbol, and the communication
2330 channel, as a plist. It must return a string or nil.")
2333 ;;;; Objects Filters
2335 (defvar org-export-filter-bold-functions nil
2336 "List of functions applied to transcoded bold text.
2337 Each filter is called with three arguments: the transcoded data,
2338 as a string, the back-end, as a symbol, and the communication
2339 channel, as a plist. It must return a string or nil.")
2341 (defvar org-export-filter-code-functions nil
2342 "List of functions applied to transcoded code text.
2343 Each filter is called with three arguments: the transcoded data,
2344 as a string, the back-end, as a symbol, and the communication
2345 channel, as a plist. It must return a string or nil.")
2347 (defvar org-export-filter-entity-functions nil
2348 "List of functions applied to a transcoded entity.
2349 Each filter is called with three arguments: the transcoded data,
2350 as a string, the back-end, as a symbol, and the communication
2351 channel, as a plist. It must return a string or nil.")
2353 (defvar org-export-filter-export-snippet-functions nil
2354 "List of functions applied to a transcoded export-snippet.
2355 Each filter is called with three arguments: the transcoded data,
2356 as a string, the back-end, as a symbol, and the communication
2357 channel, as a plist. It must return a string or nil.")
2359 (defvar org-export-filter-footnote-reference-functions nil
2360 "List of functions applied to a transcoded footnote-reference.
2361 Each filter is called with three arguments: the transcoded data,
2362 as a string, the back-end, as a symbol, and the communication
2363 channel, as a plist. It must return a string or nil.")
2365 (defvar org-export-filter-inline-babel-call-functions nil
2366 "List of functions applied to a transcoded inline-babel-call.
2367 Each filter is called with three arguments: the transcoded data,
2368 as a string, the back-end, as a symbol, and the communication
2369 channel, as a plist. It must return a string or nil.")
2371 (defvar org-export-filter-inline-src-block-functions nil
2372 "List of functions applied to a transcoded inline-src-block.
2373 Each filter is called with three arguments: the transcoded data,
2374 as a string, the back-end, as a symbol, and the communication
2375 channel, as a plist. It must return a string or nil.")
2377 (defvar org-export-filter-italic-functions nil
2378 "List of functions applied to transcoded italic text.
2379 Each filter is called with three arguments: the transcoded data,
2380 as a string, the back-end, as a symbol, and the communication
2381 channel, as a plist. It must return a string or nil.")
2383 (defvar org-export-filter-latex-fragment-functions nil
2384 "List of functions applied to a transcoded latex-fragment.
2385 Each filter is called with three arguments: the transcoded data,
2386 as a string, the back-end, as a symbol, and the communication
2387 channel, as a plist. It must return a string or nil.")
2389 (defvar org-export-filter-line-break-functions nil
2390 "List of functions applied to a transcoded line-break.
2391 Each filter is called with three arguments: the transcoded data,
2392 as a string, the back-end, as a symbol, and the communication
2393 channel, as a plist. It must return a string or nil.")
2395 (defvar org-export-filter-link-functions nil
2396 "List of functions applied to a transcoded link.
2397 Each filter is called with three arguments: the transcoded data,
2398 as a string, the back-end, as a symbol, and the communication
2399 channel, as a plist. It must return a string or nil.")
2401 (defvar org-export-filter-macro-functions nil
2402 "List of functions applied to a transcoded macro.
2403 Each filter is called with three arguments: the transcoded data,
2404 as a string, the back-end, as a symbol, and the communication
2405 channel, as a plist. It must return a string or nil.")
2407 (defvar org-export-filter-radio-target-functions nil
2408 "List of functions applied to a transcoded radio-target.
2409 Each filter is called with three arguments: the transcoded data,
2410 as a string, the back-end, as a symbol, and the communication
2411 channel, as a plist. It must return a string or nil.")
2413 (defvar org-export-filter-statistics-cookie-functions nil
2414 "List of functions applied to a transcoded statistics-cookie.
2415 Each filter is called with three arguments: the transcoded data,
2416 as a string, the back-end, as a symbol, and the communication
2417 channel, as a plist. It must return a string or nil.")
2419 (defvar org-export-filter-strike-through-functions nil
2420 "List of functions applied to transcoded strike-through text.
2421 Each filter is called with three arguments: the transcoded data,
2422 as a string, the back-end, as a symbol, and the communication
2423 channel, as a plist. It must return a string or nil.")
2425 (defvar org-export-filter-subscript-functions nil
2426 "List of functions applied to a transcoded subscript.
2427 Each filter is called with three arguments: the transcoded data,
2428 as a string, the back-end, as a symbol, and the communication
2429 channel, as a plist. It must return a string or nil.")
2431 (defvar org-export-filter-superscript-functions nil
2432 "List of functions applied to a transcoded superscript.
2433 Each filter is called with three arguments: the transcoded data,
2434 as a string, the back-end, as a symbol, and the communication
2435 channel, as a plist. It must return a string or nil.")
2437 (defvar org-export-filter-target-functions nil
2438 "List of functions applied to a transcoded target.
2439 Each filter is called with three arguments: the transcoded data,
2440 as a string, the back-end, as a symbol, and the communication
2441 channel, as a plist. It must return a string or nil.")
2443 (defvar org-export-filter-timestamp-functions nil
2444 "List of functions applied to a transcoded timestamp.
2445 Each filter is called with three arguments: the transcoded data,
2446 as a string, the back-end, as a symbol, and the communication
2447 channel, as a plist. It must return a string or nil.")
2449 (defvar org-export-filter-underline-functions nil
2450 "List of functions applied to transcoded underline text.
2451 Each filter is called with three arguments: the transcoded data,
2452 as a string, the back-end, as a symbol, and the communication
2453 channel, as a plist. It must return a string or nil.")
2455 (defvar org-export-filter-verbatim-functions nil
2456 "List of functions applied to transcoded verbatim text.
2457 Each filter is called with three arguments: the transcoded data,
2458 as a string, the back-end, as a symbol, and the communication
2459 channel, as a plist. It must return a string or nil.")
2462 ;;;; Filters Tools
2464 ;; Internal function `org-export-install-filters' installs filters
2465 ;; hard-coded in back-ends (developer filters) and filters from global
2466 ;; variables (user filters) in the communication channel.
2468 ;; Internal function `org-export-filter-apply-functions' takes care
2469 ;; about applying each filter in order to a given data. It ignores
2470 ;; filters returning a nil value but stops whenever a filter returns
2471 ;; an empty string.
2473 (defun org-export-filter-apply-functions (filters value info)
2474 "Call every function in FILTERS.
2476 Functions are called with arguments VALUE, current export
2477 back-end and INFO. A function returning a nil value will be
2478 skipped. If it returns the empty string, the process ends and
2479 VALUE is ignored.
2481 Call is done in a LIFO fashion, to be sure that developer
2482 specified filters, if any, are called first."
2483 (catch 'exit
2484 (dolist (filter filters value)
2485 (let ((result (funcall filter value (plist-get info :back-end) info)))
2486 (cond ((not result) value)
2487 ((equal value "") (throw 'exit nil))
2488 (t (setq value result)))))))
2490 (defun org-export-install-filters (info)
2491 "Install filters properties in communication channel.
2493 INFO is a plist containing the current communication channel.
2495 Return the updated communication channel."
2496 (let (plist)
2497 ;; Install user defined filters with `org-export-filters-alist'.
2498 (mapc (lambda (p)
2499 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2500 org-export-filters-alist)
2501 ;; Prepend back-end specific filters to that list.
2502 (mapc (lambda (p)
2503 ;; Single values get consed, lists are prepended.
2504 (let ((key (car p)) (value (cdr p)))
2505 (when value
2506 (setq plist
2507 (plist-put
2508 plist key
2509 (if (atom value) (cons value (plist-get plist key))
2510 (append value (plist-get plist key))))))))
2511 (org-export-backend-filters (plist-get info :back-end)))
2512 ;; Return new communication channel.
2513 (org-combine-plists info plist)))
2517 ;;; Core functions
2519 ;; This is the room for the main function, `org-export-as', along with
2520 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2521 ;; They differ only by the way they output the resulting code.
2523 ;; `org-export-output-file-name' is an auxiliary function meant to be
2524 ;; used with `org-export-to-file'. With a given extension, it tries
2525 ;; to provide a canonical file name to write export output to.
2527 ;; Note that `org-export-as' doesn't really parse the current buffer,
2528 ;; but a copy of it (with the same buffer-local variables and
2529 ;; visibility), where macros and include keywords are expanded and
2530 ;; Babel blocks are executed, if appropriate.
2531 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2533 ;; File inclusion is taken care of by
2534 ;; `org-export-expand-include-keyword' and
2535 ;; `org-export--prepare-file-contents'. Structure wise, including
2536 ;; a whole Org file in a buffer often makes little sense. For
2537 ;; example, if the file contains an headline and the include keyword
2538 ;; was within an item, the item should contain the headline. That's
2539 ;; why file inclusion should be done before any structure can be
2540 ;; associated to the file, that is before parsing.
2542 (defun org-export-as
2543 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2544 "Transcode current Org buffer into BACKEND code.
2546 If narrowing is active in the current buffer, only transcode its
2547 narrowed part.
2549 If a region is active, transcode that region.
2551 When optional argument SUBTREEP is non-nil, transcode the
2552 sub-tree at point, extracting information from the headline
2553 properties first.
2555 When optional argument VISIBLE-ONLY is non-nil, don't export
2556 contents of hidden elements.
2558 When optional argument BODY-ONLY is non-nil, only return body
2559 code, without preamble nor postamble.
2561 Optional argument EXT-PLIST, when provided, is a property list
2562 with external parameters overriding Org default settings, but
2563 still inferior to file-local settings.
2565 Optional argument NOEXPAND, when non-nil, prevents included files
2566 to be expanded and Babel code to be executed.
2568 Return code as a string."
2569 ;; Barf if BACKEND isn't registered.
2570 (org-export-barf-if-invalid-backend backend)
2571 (save-excursion
2572 (save-restriction
2573 ;; Narrow buffer to an appropriate region or subtree for
2574 ;; parsing. If parsing subtree, be sure to remove main headline
2575 ;; too.
2576 (cond ((org-region-active-p)
2577 (narrow-to-region (region-beginning) (region-end)))
2578 (subtreep
2579 (org-narrow-to-subtree)
2580 (goto-char (point-min))
2581 (forward-line)
2582 (narrow-to-region (point) (point-max))))
2583 ;; Initialize communication channel with original buffer
2584 ;; attributes, unavailable in its copy.
2585 (let ((info (org-export--get-buffer-attributes)) tree)
2586 (org-export-with-current-buffer-copy
2587 ;; Run first hook with current back-end as argument.
2588 (run-hook-with-args 'org-export-before-processing-hook backend)
2589 ;; Update communication channel and get parse tree. Buffer
2590 ;; isn't parsed directly. Instead, a temporary copy is
2591 ;; created, where include keywords, macros are expanded and
2592 ;; code blocks are evaluated.
2593 (unless noexpand
2594 (org-export-expand-include-keyword)
2595 ;; Update macro templates since #+INCLUDE keywords might
2596 ;; have added some new ones.
2597 (org-macro-initialize-templates)
2598 (org-macro-replace-all org-macro-templates)
2599 (org-export-execute-babel-code))
2600 ;; Update radio targets since keyword inclusion might have
2601 ;; added some more.
2602 (org-update-radio-target-regexp)
2603 ;; Run last hook with current back-end as argument.
2604 (goto-char (point-min))
2605 (run-hook-with-args 'org-export-before-parsing-hook backend)
2606 ;; Update communication channel with environment. Also
2607 ;; install user's and developer's filters.
2608 (setq info
2609 (org-export-install-filters
2610 (org-combine-plists
2611 info (org-export-get-environment backend subtreep ext-plist))))
2612 ;; Expand export-specific set of macros: {{{author}}},
2613 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2614 ;; once regular macros have been expanded, since document
2615 ;; keywords may contain one of them.
2616 (unless noexpand
2617 (org-macro-replace-all
2618 (list (cons "author"
2619 (org-element-interpret-data (plist-get info :author)))
2620 (cons "date"
2621 (org-element-interpret-data (plist-get info :date)))
2622 ;; EMAIL is not a parsed keyword: store it as-is.
2623 (cons "email" (or (plist-get info :email) ""))
2624 (cons "title"
2625 (org-element-interpret-data (plist-get info :title))))))
2626 ;; Eventually parse buffer. Call parse-tree filters to get
2627 ;; the final tree.
2628 (setq tree
2629 (org-export-filter-apply-functions
2630 (plist-get info :filter-parse-tree)
2631 (org-element-parse-buffer nil visible-only) info)))
2632 ;; Now tree is complete, compute its properties and add them
2633 ;; to communication channel.
2634 (setq info
2635 (org-combine-plists
2636 info (org-export-collect-tree-properties tree info)))
2637 ;; Eventually transcode TREE. Wrap the resulting string into
2638 ;; a template, if required. Finally call final-output filter.
2639 (let* ((body (org-element-normalize-string
2640 (or (org-export-data tree info) "")))
2641 (template (cdr (assq 'template
2642 (plist-get info :translate-alist))))
2643 (output (org-export-filter-apply-functions
2644 (plist-get info :filter-final-output)
2645 (if (or (not (functionp template)) body-only) body
2646 (funcall template body info))
2647 info)))
2648 ;; Maybe add final OUTPUT to kill ring, then return it.
2649 (when (and org-export-copy-to-kill-ring (org-string-nw-p output))
2650 (org-kill-new output))
2651 output)))))
2653 (defun org-export-to-buffer
2654 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2655 "Call `org-export-as' with output to a specified buffer.
2657 BACKEND is the back-end used for transcoding, as a symbol.
2659 BUFFER is the output buffer. If it already exists, it will be
2660 erased first, otherwise, it will be created.
2662 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2663 and NOEXPAND are similar to those used in `org-export-as', which
2664 see.
2666 Return buffer."
2667 (let ((out (org-export-as
2668 backend subtreep visible-only body-only ext-plist noexpand))
2669 (buffer (get-buffer-create buffer)))
2670 (with-current-buffer buffer
2671 (erase-buffer)
2672 (insert out)
2673 (goto-char (point-min)))
2674 buffer))
2676 (defun org-export-to-file
2677 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2678 "Call `org-export-as' with output to a specified file.
2680 BACKEND is the back-end used for transcoding, as a symbol. FILE
2681 is the name of the output file, as a string.
2683 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2684 and NOEXPAND are similar to those used in `org-export-as', which
2685 see.
2687 Return output file's name."
2688 ;; Checks for FILE permissions. `write-file' would do the same, but
2689 ;; we'd rather avoid needless transcoding of parse tree.
2690 (unless (file-writable-p file) (error "Output file not writable"))
2691 ;; Insert contents to a temporary buffer and write it to FILE.
2692 (let ((out (org-export-as
2693 backend subtreep visible-only body-only ext-plist noexpand)))
2694 (with-temp-buffer
2695 (insert out)
2696 (let ((coding-system-for-write org-export-coding-system))
2697 (write-file file))))
2698 ;; Return full path.
2699 file)
2701 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2702 "Return output file's name according to buffer specifications.
2704 EXTENSION is a string representing the output file extension,
2705 with the leading dot.
2707 With a non-nil optional argument SUBTREEP, try to determine
2708 output file's name by looking for \"EXPORT_FILE_NAME\" property
2709 of subtree at point.
2711 When optional argument PUB-DIR is set, use it as the publishing
2712 directory.
2714 When optional argument VISIBLE-ONLY is non-nil, don't export
2715 contents of hidden elements.
2717 Return file name as a string, or nil if it couldn't be
2718 determined."
2719 (let ((base-name
2720 ;; File name may come from EXPORT_FILE_NAME subtree property,
2721 ;; assuming point is at beginning of said sub-tree.
2722 (file-name-sans-extension
2723 (or (and subtreep
2724 (org-entry-get
2725 (save-excursion
2726 (ignore-errors (org-back-to-heading) (point)))
2727 "EXPORT_FILE_NAME" t))
2728 ;; File name may be extracted from buffer's associated
2729 ;; file, if any.
2730 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2731 (and visited-file (file-name-nondirectory visited-file)))
2732 ;; Can't determine file name on our own: Ask user.
2733 (let ((read-file-name-function
2734 (and org-completion-use-ido 'ido-read-file-name)))
2735 (read-file-name
2736 "Output file: " pub-dir nil nil nil
2737 (lambda (name)
2738 (string= (file-name-extension name t) extension))))))))
2739 ;; Build file name. Enforce EXTENSION over whatever user may have
2740 ;; come up with. PUB-DIR, if defined, always has precedence over
2741 ;; any provided path.
2742 (cond
2743 (pub-dir
2744 (concat (file-name-as-directory pub-dir)
2745 (file-name-nondirectory base-name)
2746 extension))
2747 ((file-name-absolute-p base-name) (concat base-name extension))
2748 (t (concat (file-name-as-directory ".") base-name extension)))))
2750 (defmacro org-export-with-current-buffer-copy (&rest body)
2751 "Apply BODY in a copy of the current buffer.
2753 The copy preserves local variables and visibility of the original
2754 buffer.
2756 Point is at buffer's beginning when BODY is applied."
2757 (declare (debug (body)))
2758 (org-with-gensyms (original-buffer offset buffer-string overlays region)
2759 `(let* ((,original-buffer (current-buffer))
2760 (,region (list (point-min) (point-max)))
2761 (,buffer-string (org-with-wide-buffer (buffer-string)))
2762 (,overlays (mapcar 'copy-overlay (apply 'overlays-in ,region))))
2763 (with-temp-buffer
2764 (let ((buffer-invisibility-spec nil))
2765 (org-clone-local-variables
2766 ,original-buffer
2767 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2768 (insert ,buffer-string)
2769 (apply 'narrow-to-region ,region)
2770 (mapc (lambda (ov)
2771 (move-overlay
2772 ov (overlay-start ov) (overlay-end ov) (current-buffer)))
2773 ,overlays)
2774 (goto-char (point-min))
2775 (progn ,@body))))))
2777 (defun org-export-expand-include-keyword (&optional included dir)
2778 "Expand every include keyword in buffer.
2779 Optional argument INCLUDED is a list of included file names along
2780 with their line restriction, when appropriate. It is used to
2781 avoid infinite recursion. Optional argument DIR is the current
2782 working directory. It is used to properly resolve relative
2783 paths."
2784 (let ((case-fold-search t))
2785 (goto-char (point-min))
2786 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
2787 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2788 'keyword)
2789 (beginning-of-line)
2790 ;; Extract arguments from keyword's value.
2791 (let* ((value (match-string 1))
2792 (ind (org-get-indentation))
2793 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2794 (prog1 (expand-file-name (match-string 1 value) dir)
2795 (setq value (replace-match "" nil nil value)))))
2796 (lines
2797 (and (string-match
2798 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2799 (prog1 (match-string 1 value)
2800 (setq value (replace-match "" nil nil value)))))
2801 (env (cond ((string-match "\\<example\\>" value) 'example)
2802 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2803 (match-string 1 value))))
2804 ;; Minimal level of included file defaults to the child
2805 ;; level of the current headline, if any, or one. It
2806 ;; only applies is the file is meant to be included as
2807 ;; an Org one.
2808 (minlevel
2809 (and (not env)
2810 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2811 (prog1 (string-to-number (match-string 1 value))
2812 (setq value (replace-match "" nil nil value)))
2813 (let ((cur (org-current-level)))
2814 (if cur (1+ (org-reduced-level cur)) 1))))))
2815 ;; Remove keyword.
2816 (delete-region (point) (progn (forward-line) (point)))
2817 (cond
2818 ((not file) (error "Invalid syntax in INCLUDE keyword"))
2819 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2820 ;; Check if files has already been parsed. Look after
2821 ;; inclusion lines too, as different parts of the same file
2822 ;; can be included too.
2823 ((member (list file lines) included)
2824 (error "Recursive file inclusion: %s" file))
2826 (cond
2827 ((eq env 'example)
2828 (insert
2829 (let ((ind-str (make-string ind ? ))
2830 (contents
2831 (org-escape-code-in-string
2832 (org-export--prepare-file-contents file lines))))
2833 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2834 ind-str contents ind-str))))
2835 ((stringp env)
2836 (insert
2837 (let ((ind-str (make-string ind ? ))
2838 (contents
2839 (org-escape-code-in-string
2840 (org-export--prepare-file-contents file lines))))
2841 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2842 ind-str env contents ind-str))))
2844 (insert
2845 (with-temp-buffer
2846 (org-mode)
2847 (insert
2848 (org-export--prepare-file-contents file lines ind minlevel))
2849 (org-export-expand-include-keyword
2850 (cons (list file lines) included)
2851 (file-name-directory file))
2852 (buffer-string))))))))))))
2854 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2855 "Prepare the contents of FILE for inclusion and return them as a string.
2857 When optional argument LINES is a string specifying a range of
2858 lines, include only those lines.
2860 Optional argument IND, when non-nil, is an integer specifying the
2861 global indentation of returned contents. Since its purpose is to
2862 allow an included file to stay in the same environment it was
2863 created \(i.e. a list item), it doesn't apply past the first
2864 headline encountered.
2866 Optional argument MINLEVEL, when non-nil, is an integer
2867 specifying the level that any top-level headline in the included
2868 file should have."
2869 (with-temp-buffer
2870 (insert-file-contents file)
2871 (when lines
2872 (let* ((lines (split-string lines "-"))
2873 (lbeg (string-to-number (car lines)))
2874 (lend (string-to-number (cadr lines)))
2875 (beg (if (zerop lbeg) (point-min)
2876 (goto-char (point-min))
2877 (forward-line (1- lbeg))
2878 (point)))
2879 (end (if (zerop lend) (point-max)
2880 (goto-char (point-min))
2881 (forward-line (1- lend))
2882 (point))))
2883 (narrow-to-region beg end)))
2884 ;; Remove blank lines at beginning and end of contents. The logic
2885 ;; behind that removal is that blank lines around include keyword
2886 ;; override blank lines in included file.
2887 (goto-char (point-min))
2888 (org-skip-whitespace)
2889 (beginning-of-line)
2890 (delete-region (point-min) (point))
2891 (goto-char (point-max))
2892 (skip-chars-backward " \r\t\n")
2893 (forward-line)
2894 (delete-region (point) (point-max))
2895 ;; If IND is set, preserve indentation of include keyword until
2896 ;; the first headline encountered.
2897 (when ind
2898 (unless (eq major-mode 'org-mode) (org-mode))
2899 (goto-char (point-min))
2900 (let ((ind-str (make-string ind ? )))
2901 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2902 ;; Do not move footnote definitions out of column 0.
2903 (unless (and (looking-at org-footnote-definition-re)
2904 (eq (org-element-type (org-element-at-point))
2905 'footnote-definition))
2906 (insert ind-str))
2907 (forward-line))))
2908 ;; When MINLEVEL is specified, compute minimal level for headlines
2909 ;; in the file (CUR-MIN), and remove stars to each headline so
2910 ;; that headlines with minimal level have a level of MINLEVEL.
2911 (when minlevel
2912 (unless (eq major-mode 'org-mode) (org-mode))
2913 (org-with-limited-levels
2914 (let ((levels (org-map-entries
2915 (lambda () (org-reduced-level (org-current-level))))))
2916 (when levels
2917 (let ((offset (- minlevel (apply 'min levels))))
2918 (unless (zerop offset)
2919 (when org-odd-levels-only (setq offset (* offset 2)))
2920 ;; Only change stars, don't bother moving whole
2921 ;; sections.
2922 (org-map-entries
2923 (lambda () (if (< offset 0) (delete-char (abs offset))
2924 (insert (make-string offset ?*)))))))))))
2925 (org-element-normalize-string (buffer-string))))
2927 (defun org-export-execute-babel-code ()
2928 "Execute every Babel code in the visible part of current buffer.
2929 This function will return an error if the current buffer is
2930 visiting a file."
2931 ;; Get a pristine copy of current buffer so Babel references can be
2932 ;; properly resolved.
2933 (let* (clone-buffer-hook (reference (clone-buffer)))
2934 (unwind-protect (let ((org-current-export-file reference))
2935 (org-export-blocks-preprocess))
2936 (kill-buffer reference))))
2940 ;;; Tools For Back-Ends
2942 ;; A whole set of tools is available to help build new exporters. Any
2943 ;; function general enough to have its use across many back-ends
2944 ;; should be added here.
2946 ;;;; For Affiliated Keywords
2948 ;; `org-export-read-attribute' reads a property from a given element
2949 ;; as a plist. It can be used to normalize affiliated keywords'
2950 ;; syntax.
2952 ;; Since captions can span over multiple lines and accept dual values,
2953 ;; their internal representation is a bit tricky. Therefore,
2954 ;; `org-export-get-caption' transparently returns a given element's
2955 ;; caption as a secondary string.
2957 (defun org-export-read-attribute (attribute element &optional property)
2958 "Turn ATTRIBUTE property from ELEMENT into a plist.
2960 When optional argument PROPERTY is non-nil, return the value of
2961 that property within attributes.
2963 This function assumes attributes are defined as \":keyword
2964 value\" pairs. It is appropriate for `:attr_html' like
2965 properties."
2966 (let ((attributes
2967 (let ((value (org-element-property attribute element)))
2968 (and value
2969 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2970 (if property (plist-get attributes property) attributes)))
2972 (defun org-export-get-caption (element &optional shortp)
2973 "Return caption from ELEMENT as a secondary string.
2975 When optional argument SHORTP is non-nil, return short caption,
2976 as a secondary string, instead.
2978 Caption lines are separated by a white space."
2979 (let ((full-caption (org-element-property :caption element)) caption)
2980 (dolist (line full-caption (cdr caption))
2981 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2982 (when cap
2983 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2986 ;;;; For Derived Back-ends
2988 ;; `org-export-with-backend' is a function allowing to locally use
2989 ;; another back-end to transcode some object or element. In a derived
2990 ;; back-end, it may be used as a fall-back function once all specific
2991 ;; cases have been treated.
2993 (defun org-export-with-backend (back-end data &optional contents info)
2994 "Call a transcoder from BACK-END on DATA.
2995 CONTENTS, when non-nil, is the transcoded contents of DATA
2996 element, as a string. INFO, when non-nil, is the communication
2997 channel used for export, as a plist.."
2998 (org-export-barf-if-invalid-backend back-end)
2999 (let ((type (org-element-type data)))
3000 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3001 (let ((transcoder
3002 (cdr (assq type (org-export-backend-translate-table back-end)))))
3003 (if (functionp transcoder) (funcall transcoder data contents info)
3004 (error "No foreign transcoder available"))))))
3007 ;;;; For Export Snippets
3009 ;; Every export snippet is transmitted to the back-end. Though, the
3010 ;; latter will only retain one type of export-snippet, ignoring
3011 ;; others, based on the former's target back-end. The function
3012 ;; `org-export-snippet-backend' returns that back-end for a given
3013 ;; export-snippet.
3015 (defun org-export-snippet-backend (export-snippet)
3016 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3017 Translation, with `org-export-snippet-translation-alist', is
3018 applied."
3019 (let ((back-end (org-element-property :back-end export-snippet)))
3020 (intern
3021 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3022 back-end))))
3025 ;;;; For Footnotes
3027 ;; `org-export-collect-footnote-definitions' is a tool to list
3028 ;; actually used footnotes definitions in the whole parse tree, or in
3029 ;; an headline, in order to add footnote listings throughout the
3030 ;; transcoded data.
3032 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3033 ;; back-ends, when they need to attach the footnote definition only to
3034 ;; the first occurrence of the corresponding label.
3036 ;; `org-export-get-footnote-definition' and
3037 ;; `org-export-get-footnote-number' provide easier access to
3038 ;; additional information relative to a footnote reference.
3040 (defun org-export-collect-footnote-definitions (data info)
3041 "Return an alist between footnote numbers, labels and definitions.
3043 DATA is the parse tree from which definitions are collected.
3044 INFO is the plist used as a communication channel.
3046 Definitions are sorted by order of references. They either
3047 appear as Org data or as a secondary string for inlined
3048 footnotes. Unreferenced definitions are ignored."
3049 (let* (num-alist
3050 collect-fn ; for byte-compiler.
3051 (collect-fn
3052 (function
3053 (lambda (data)
3054 ;; Collect footnote number, label and definition in DATA.
3055 (org-element-map
3056 data 'footnote-reference
3057 (lambda (fn)
3058 (when (org-export-footnote-first-reference-p fn info)
3059 (let ((def (org-export-get-footnote-definition fn info)))
3060 (push
3061 (list (org-export-get-footnote-number fn info)
3062 (org-element-property :label fn)
3063 def)
3064 num-alist)
3065 ;; Also search in definition for nested footnotes.
3066 (when (eq (org-element-property :type fn) 'standard)
3067 (funcall collect-fn def)))))
3068 ;; Don't enter footnote definitions since it will happen
3069 ;; when their first reference is found.
3070 info nil 'footnote-definition)))))
3071 (funcall collect-fn (plist-get info :parse-tree))
3072 (reverse num-alist)))
3074 (defun org-export-footnote-first-reference-p (footnote-reference info)
3075 "Non-nil when a footnote reference is the first one for its label.
3077 FOOTNOTE-REFERENCE is the footnote reference being considered.
3078 INFO is the plist used as a communication channel."
3079 (let ((label (org-element-property :label footnote-reference)))
3080 ;; Anonymous footnotes are always a first reference.
3081 (if (not label) t
3082 ;; Otherwise, return the first footnote with the same LABEL and
3083 ;; test if it is equal to FOOTNOTE-REFERENCE.
3084 (let* (search-refs ; for byte-compiler.
3085 (search-refs
3086 (function
3087 (lambda (data)
3088 (org-element-map
3089 data 'footnote-reference
3090 (lambda (fn)
3091 (cond
3092 ((string= (org-element-property :label fn) label)
3093 (throw 'exit fn))
3094 ;; If FN isn't inlined, be sure to traverse its
3095 ;; definition before resuming search. See
3096 ;; comments in `org-export-get-footnote-number'
3097 ;; for more information.
3098 ((eq (org-element-property :type fn) 'standard)
3099 (funcall search-refs
3100 (org-export-get-footnote-definition fn info)))))
3101 ;; Don't enter footnote definitions since it will
3102 ;; happen when their first reference is found.
3103 info 'first-match 'footnote-definition)))))
3104 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3105 footnote-reference)))))
3107 (defun org-export-get-footnote-definition (footnote-reference info)
3108 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3109 INFO is the plist used as a communication channel."
3110 (let ((label (org-element-property :label footnote-reference)))
3111 (or (org-element-property :inline-definition footnote-reference)
3112 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3114 (defun org-export-get-footnote-number (footnote info)
3115 "Return number associated to a footnote.
3117 FOOTNOTE is either a footnote reference or a footnote definition.
3118 INFO is the plist used as a communication channel."
3119 (let* ((label (org-element-property :label footnote))
3120 seen-refs
3121 search-ref ; For byte-compiler.
3122 (search-ref
3123 (function
3124 (lambda (data)
3125 ;; Search footnote references through DATA, filling
3126 ;; SEEN-REFS along the way.
3127 (org-element-map
3128 data 'footnote-reference
3129 (lambda (fn)
3130 (let ((fn-lbl (org-element-property :label fn)))
3131 (cond
3132 ;; Anonymous footnote match: return number.
3133 ((and (not fn-lbl) (eq fn footnote))
3134 (throw 'exit (1+ (length seen-refs))))
3135 ;; Labels match: return number.
3136 ((and label (string= label fn-lbl))
3137 (throw 'exit (1+ (length seen-refs))))
3138 ;; Anonymous footnote: it's always a new one. Also,
3139 ;; be sure to return nil from the `cond' so
3140 ;; `first-match' doesn't get us out of the loop.
3141 ((not fn-lbl) (push 'inline seen-refs) nil)
3142 ;; Label not seen so far: add it so SEEN-REFS.
3144 ;; Also search for subsequent references in
3145 ;; footnote definition so numbering follows reading
3146 ;; logic. Note that we don't have to care about
3147 ;; inline definitions, since `org-element-map'
3148 ;; already traverses them at the right time.
3150 ;; Once again, return nil to stay in the loop.
3151 ((not (member fn-lbl seen-refs))
3152 (push fn-lbl seen-refs)
3153 (funcall search-ref
3154 (org-export-get-footnote-definition fn info))
3155 nil))))
3156 ;; Don't enter footnote definitions since it will happen
3157 ;; when their first reference is found.
3158 info 'first-match 'footnote-definition)))))
3159 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3162 ;;;; For Headlines
3164 ;; `org-export-get-relative-level' is a shortcut to get headline
3165 ;; level, relatively to the lower headline level in the parsed tree.
3167 ;; `org-export-get-headline-number' returns the section number of an
3168 ;; headline, while `org-export-number-to-roman' allows to convert it
3169 ;; to roman numbers.
3171 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3172 ;; `org-export-last-sibling-p' are three useful predicates when it
3173 ;; comes to fulfill the `:headline-levels' property.
3175 ;; `org-export-get-tags', `org-export-get-category' and
3176 ;; `org-export-get-node-property' extract useful information from an
3177 ;; headline or a parent headline. They all handle inheritance.
3179 (defun org-export-get-relative-level (headline info)
3180 "Return HEADLINE relative level within current parsed tree.
3181 INFO is a plist holding contextual information."
3182 (+ (org-element-property :level headline)
3183 (or (plist-get info :headline-offset) 0)))
3185 (defun org-export-low-level-p (headline info)
3186 "Non-nil when HEADLINE is considered as low level.
3188 INFO is a plist used as a communication channel.
3190 A low level headlines has a relative level greater than
3191 `:headline-levels' property value.
3193 Return value is the difference between HEADLINE relative level
3194 and the last level being considered as high enough, or nil."
3195 (let ((limit (plist-get info :headline-levels)))
3196 (when (wholenump limit)
3197 (let ((level (org-export-get-relative-level headline info)))
3198 (and (> level limit) (- level limit))))))
3200 (defun org-export-get-headline-number (headline info)
3201 "Return HEADLINE numbering as a list of numbers.
3202 INFO is a plist holding contextual information."
3203 (cdr (assoc headline (plist-get info :headline-numbering))))
3205 (defun org-export-numbered-headline-p (headline info)
3206 "Return a non-nil value if HEADLINE element should be numbered.
3207 INFO is a plist used as a communication channel."
3208 (let ((sec-num (plist-get info :section-numbers))
3209 (level (org-export-get-relative-level headline info)))
3210 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3212 (defun org-export-number-to-roman (n)
3213 "Convert integer N into a roman numeral."
3214 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3215 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3216 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3217 ( 1 . "I")))
3218 (res ""))
3219 (if (<= n 0)
3220 (number-to-string n)
3221 (while roman
3222 (if (>= n (caar roman))
3223 (setq n (- n (caar roman))
3224 res (concat res (cdar roman)))
3225 (pop roman)))
3226 res)))
3228 (defun org-export-get-tags (element info &optional tags inherited)
3229 "Return list of tags associated to ELEMENT.
3231 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3232 is a plist used as a communication channel.
3234 Select tags (see `org-export-select-tags') and exclude tags (see
3235 `org-export-exclude-tags') are removed from the list.
3237 When non-nil, optional argument TAGS should be a list of strings.
3238 Any tag belonging to this list will also be removed.
3240 When optional argument INHERITED is non-nil, tags can also be
3241 inherited from parent headlines and FILETAGS keywords."
3242 (org-remove-if
3243 (lambda (tag) (or (member tag (plist-get info :select-tags))
3244 (member tag (plist-get info :exclude-tags))
3245 (member tag tags)))
3246 (if (not inherited) (org-element-property :tags element)
3247 ;; Build complete list of inherited tags.
3248 (let ((current-tag-list (org-element-property :tags element)))
3249 (mapc
3250 (lambda (parent)
3251 (mapc
3252 (lambda (tag)
3253 (when (and (memq (org-element-type parent) '(headline inlinetask))
3254 (not (member tag current-tag-list)))
3255 (push tag current-tag-list)))
3256 (org-element-property :tags parent)))
3257 (org-export-get-genealogy element))
3258 ;; Add FILETAGS keywords and return results.
3259 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3261 (defun org-export-get-node-property (property blob &optional inherited)
3262 "Return node PROPERTY value for BLOB.
3264 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3265 element or object.
3267 If optional argument INHERITED is non-nil, the value can be
3268 inherited from a parent headline.
3270 Return value is a string or nil."
3271 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3272 (org-export-get-parent-headline blob))))
3273 (if (not inherited) (org-element-property property blob)
3274 (let ((parent headline) value)
3275 (catch 'found
3276 (while parent
3277 (when (plist-member (nth 1 parent) property)
3278 (throw 'found (org-element-property property parent)))
3279 (setq parent (org-element-property :parent parent))))))))
3281 (defun org-export-get-category (blob info)
3282 "Return category for element or object BLOB.
3284 INFO is a plist used as a communication channel.
3286 CATEGORY is automatically inherited from a parent headline, from
3287 #+CATEGORY: keyword or created out of original file name. If all
3288 fail, the fall-back value is \"???\"."
3289 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3290 (org-export-get-parent-headline blob))))
3291 ;; Almost like `org-export-node-property', but we cannot trust
3292 ;; `plist-member' as every headline has a `:category'
3293 ;; property, would it be nil or equal to "???" (which has the
3294 ;; same meaning).
3295 (let ((parent headline) value)
3296 (catch 'found
3297 (while parent
3298 (let ((category (org-element-property :category parent)))
3299 (and category (not (equal "???" category))
3300 (throw 'found category)))
3301 (setq parent (org-element-property :parent parent))))))
3302 (org-element-map
3303 (plist-get info :parse-tree) 'keyword
3304 (lambda (kwd)
3305 (when (equal (org-element-property :key kwd) "CATEGORY")
3306 (org-element-property :value kwd)))
3307 info 'first-match)
3308 (let ((file (plist-get info :input-file)))
3309 (and file (file-name-sans-extension (file-name-nondirectory file))))
3310 "???"))
3312 (defun org-export-first-sibling-p (headline info)
3313 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3314 INFO is a plist used as a communication channel."
3315 (not (eq (org-element-type (org-export-get-previous-element headline info))
3316 'headline)))
3318 (defun org-export-last-sibling-p (headline info)
3319 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3320 INFO is a plist used as a communication channel."
3321 (not (org-export-get-next-element headline info)))
3324 ;;;; For Links
3326 ;; `org-export-solidify-link-text' turns a string into a safer version
3327 ;; for links, replacing most non-standard characters with hyphens.
3329 ;; `org-export-get-coderef-format' returns an appropriate format
3330 ;; string for coderefs.
3332 ;; `org-export-inline-image-p' returns a non-nil value when the link
3333 ;; provided should be considered as an inline image.
3335 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3336 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3337 ;; returns an appropriate unique identifier when found, or nil.
3339 ;; `org-export-resolve-id-link' returns the first headline with
3340 ;; specified id or custom-id in parse tree, the path to the external
3341 ;; file with the id or nil when neither was found.
3343 ;; `org-export-resolve-coderef' associates a reference to a line
3344 ;; number in the element it belongs, or returns the reference itself
3345 ;; when the element isn't numbered.
3347 (defun org-export-solidify-link-text (s)
3348 "Take link text S and make a safe target out of it."
3349 (save-match-data
3350 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3352 (defun org-export-get-coderef-format (path desc)
3353 "Return format string for code reference link.
3354 PATH is the link path. DESC is its description."
3355 (save-match-data
3356 (cond ((not desc) "%s")
3357 ((string-match (regexp-quote (concat "(" path ")")) desc)
3358 (replace-match "%s" t t desc))
3359 (t desc))))
3361 (defun org-export-inline-image-p (link &optional rules)
3362 "Non-nil if LINK object points to an inline image.
3364 Optional argument is a set of RULES defining inline images. It
3365 is an alist where associations have the following shape:
3367 \(TYPE . REGEXP)
3369 Applying a rule means apply REGEXP against LINK's path when its
3370 type is TYPE. The function will return a non-nil value if any of
3371 the provided rules is non-nil. The default rule is
3372 `org-export-default-inline-image-rule'.
3374 This only applies to links without a description."
3375 (and (not (org-element-contents link))
3376 (let ((case-fold-search t)
3377 (rules (or rules org-export-default-inline-image-rule)))
3378 (catch 'exit
3379 (mapc
3380 (lambda (rule)
3381 (and (string= (org-element-property :type link) (car rule))
3382 (string-match (cdr rule)
3383 (org-element-property :path link))
3384 (throw 'exit t)))
3385 rules)
3386 ;; Return nil if no rule matched.
3387 nil))))
3389 (defun org-export-resolve-coderef (ref info)
3390 "Resolve a code reference REF.
3392 INFO is a plist used as a communication channel.
3394 Return associated line number in source code, or REF itself,
3395 depending on src-block or example element's switches."
3396 (org-element-map
3397 (plist-get info :parse-tree) '(example-block src-block)
3398 (lambda (el)
3399 (with-temp-buffer
3400 (insert (org-trim (org-element-property :value el)))
3401 (let* ((label-fmt (regexp-quote
3402 (or (org-element-property :label-fmt el)
3403 org-coderef-label-format)))
3404 (ref-re
3405 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3406 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3407 ;; Element containing REF is found. Resolve it to either
3408 ;; a label or a line number, as needed.
3409 (when (re-search-backward ref-re nil t)
3410 (cond
3411 ((org-element-property :use-labels el) ref)
3412 ((eq (org-element-property :number-lines el) 'continued)
3413 (+ (org-export-get-loc el info) (line-number-at-pos)))
3414 (t (line-number-at-pos)))))))
3415 info 'first-match))
3417 (defun org-export-resolve-fuzzy-link (link info)
3418 "Return LINK destination.
3420 INFO is a plist holding contextual information.
3422 Return value can be an object, an element, or nil:
3424 - If LINK path matches a target object (i.e. <<path>>) or
3425 element (i.e. \"#+TARGET: path\"), return it.
3427 - If LINK path exactly matches the name affiliated keyword
3428 \(i.e. #+NAME: path) of an element, return that element.
3430 - If LINK path exactly matches any headline name, return that
3431 element. If more than one headline share that name, priority
3432 will be given to the one with the closest common ancestor, if
3433 any, or the first one in the parse tree otherwise.
3435 - Otherwise, return nil.
3437 Assume LINK type is \"fuzzy\"."
3438 (let* ((path (org-element-property :path link))
3439 (match-title-p (eq (aref path 0) ?*)))
3440 (cond
3441 ;; First try to find a matching "<<path>>" unless user specified
3442 ;; he was looking for an headline (path starts with a *
3443 ;; character).
3444 ((and (not match-title-p)
3445 (loop for target in (plist-get info :target-list)
3446 when (string= (org-element-property :value target) path)
3447 return target)))
3448 ;; Then try to find an element with a matching "#+NAME: path"
3449 ;; affiliated keyword.
3450 ((and (not match-title-p)
3451 (org-element-map
3452 (plist-get info :parse-tree) org-element-all-elements
3453 (lambda (el)
3454 (when (string= (org-element-property :name el) path) el))
3455 info 'first-match)))
3456 ;; Last case: link either points to an headline or to
3457 ;; nothingness. Try to find the source, with priority given to
3458 ;; headlines with the closest common ancestor. If such candidate
3459 ;; is found, return it, otherwise return nil.
3461 (let ((find-headline
3462 (function
3463 ;; Return first headline whose `:raw-value' property
3464 ;; is NAME in parse tree DATA, or nil.
3465 (lambda (name data)
3466 (org-element-map
3467 data 'headline
3468 (lambda (headline)
3469 (when (string=
3470 (org-element-property :raw-value headline)
3471 name)
3472 headline))
3473 info 'first-match)))))
3474 ;; Search among headlines sharing an ancestor with link,
3475 ;; from closest to farthest.
3476 (or (catch 'exit
3477 (mapc
3478 (lambda (parent)
3479 (when (eq (org-element-type parent) 'headline)
3480 (let ((foundp (funcall find-headline path parent)))
3481 (when foundp (throw 'exit foundp)))))
3482 (org-export-get-genealogy link)) nil)
3483 ;; No match with a common ancestor: try the full parse-tree.
3484 (funcall find-headline
3485 (if match-title-p (substring path 1) path)
3486 (plist-get info :parse-tree))))))))
3488 (defun org-export-resolve-id-link (link info)
3489 "Return headline referenced as LINK destination.
3491 INFO is a plist used as a communication channel.
3493 Return value can be the headline element matched in current parse
3494 tree, a file name or nil. Assume LINK type is either \"id\" or
3495 \"custom-id\"."
3496 (let ((id (org-element-property :path link)))
3497 ;; First check if id is within the current parse tree.
3498 (or (org-element-map
3499 (plist-get info :parse-tree) 'headline
3500 (lambda (headline)
3501 (when (or (string= (org-element-property :id headline) id)
3502 (string= (org-element-property :custom-id headline) id))
3503 headline))
3504 info 'first-match)
3505 ;; Otherwise, look for external files.
3506 (cdr (assoc id (plist-get info :id-alist))))))
3508 (defun org-export-resolve-radio-link (link info)
3509 "Return radio-target object referenced as LINK destination.
3511 INFO is a plist used as a communication channel.
3513 Return value can be a radio-target object or nil. Assume LINK
3514 has type \"radio\"."
3515 (let ((path (org-element-property :path link)))
3516 (org-element-map
3517 (plist-get info :parse-tree) 'radio-target
3518 (lambda (radio)
3519 (when (equal (org-element-property :value radio) path) radio))
3520 info 'first-match)))
3523 ;;;; For References
3525 ;; `org-export-get-ordinal' associates a sequence number to any object
3526 ;; or element.
3528 (defun org-export-get-ordinal (element info &optional types predicate)
3529 "Return ordinal number of an element or object.
3531 ELEMENT is the element or object considered. INFO is the plist
3532 used as a communication channel.
3534 Optional argument TYPES, when non-nil, is a list of element or
3535 object types, as symbols, that should also be counted in.
3536 Otherwise, only provided element's type is considered.
3538 Optional argument PREDICATE is a function returning a non-nil
3539 value if the current element or object should be counted in. It
3540 accepts two arguments: the element or object being considered and
3541 the plist used as a communication channel. This allows to count
3542 only a certain type of objects (i.e. inline images).
3544 Return value is a list of numbers if ELEMENT is an headline or an
3545 item. It is nil for keywords. It represents the footnote number
3546 for footnote definitions and footnote references. If ELEMENT is
3547 a target, return the same value as if ELEMENT was the closest
3548 table, item or headline containing the target. In any other
3549 case, return the sequence number of ELEMENT among elements or
3550 objects of the same type."
3551 ;; A target keyword, representing an invisible target, never has
3552 ;; a sequence number.
3553 (unless (eq (org-element-type element) 'keyword)
3554 ;; Ordinal of a target object refer to the ordinal of the closest
3555 ;; table, item, or headline containing the object.
3556 (when (eq (org-element-type element) 'target)
3557 (setq element
3558 (loop for parent in (org-export-get-genealogy element)
3559 when
3560 (memq
3561 (org-element-type parent)
3562 '(footnote-definition footnote-reference headline item
3563 table))
3564 return parent)))
3565 (case (org-element-type element)
3566 ;; Special case 1: An headline returns its number as a list.
3567 (headline (org-export-get-headline-number element info))
3568 ;; Special case 2: An item returns its number as a list.
3569 (item (let ((struct (org-element-property :structure element)))
3570 (org-list-get-item-number
3571 (org-element-property :begin element)
3572 struct
3573 (org-list-prevs-alist struct)
3574 (org-list-parents-alist struct))))
3575 ((footnote-definition footnote-reference)
3576 (org-export-get-footnote-number element info))
3577 (otherwise
3578 (let ((counter 0))
3579 ;; Increment counter until ELEMENT is found again.
3580 (org-element-map
3581 (plist-get info :parse-tree) (or types (org-element-type element))
3582 (lambda (el)
3583 (cond
3584 ((eq element el) (1+ counter))
3585 ((not predicate) (incf counter) nil)
3586 ((funcall predicate el info) (incf counter) nil)))
3587 info 'first-match))))))
3590 ;;;; For Src-Blocks
3592 ;; `org-export-get-loc' counts number of code lines accumulated in
3593 ;; src-block or example-block elements with a "+n" switch until
3594 ;; a given element, excluded. Note: "-n" switches reset that count.
3596 ;; `org-export-unravel-code' extracts source code (along with a code
3597 ;; references alist) from an `element-block' or `src-block' type
3598 ;; element.
3600 ;; `org-export-format-code' applies a formatting function to each line
3601 ;; of code, providing relative line number and code reference when
3602 ;; appropriate. Since it doesn't access the original element from
3603 ;; which the source code is coming, it expects from the code calling
3604 ;; it to know if lines should be numbered and if code references
3605 ;; should appear.
3607 ;; Eventually, `org-export-format-code-default' is a higher-level
3608 ;; function (it makes use of the two previous functions) which handles
3609 ;; line numbering and code references inclusion, and returns source
3610 ;; code in a format suitable for plain text or verbatim output.
3612 (defun org-export-get-loc (element info)
3613 "Return accumulated lines of code up to ELEMENT.
3615 INFO is the plist used as a communication channel.
3617 ELEMENT is excluded from count."
3618 (let ((loc 0))
3619 (org-element-map
3620 (plist-get info :parse-tree)
3621 `(src-block example-block ,(org-element-type element))
3622 (lambda (el)
3623 (cond
3624 ;; ELEMENT is reached: Quit the loop.
3625 ((eq el element))
3626 ;; Only count lines from src-block and example-block elements
3627 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3628 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3629 ((let ((linums (org-element-property :number-lines el)))
3630 (when linums
3631 ;; Accumulate locs or reset them.
3632 (let ((lines (org-count-lines
3633 (org-trim (org-element-property :value el)))))
3634 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3635 ;; Return nil to stay in the loop.
3636 nil)))
3637 info 'first-match)
3638 ;; Return value.
3639 loc))
3641 (defun org-export-unravel-code (element)
3642 "Clean source code and extract references out of it.
3644 ELEMENT has either a `src-block' an `example-block' type.
3646 Return a cons cell whose CAR is the source code, cleaned from any
3647 reference and protective comma and CDR is an alist between
3648 relative line number (integer) and name of code reference on that
3649 line (string)."
3650 (let* ((line 0) refs
3651 ;; Get code and clean it. Remove blank lines at its
3652 ;; beginning and end.
3653 (code (let ((c (replace-regexp-in-string
3654 "\\`\\([ \t]*\n\\)+" ""
3655 (replace-regexp-in-string
3656 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3657 (org-element-property :value element)))))
3658 ;; If appropriate, remove global indentation.
3659 (if (or org-src-preserve-indentation
3660 (org-element-property :preserve-indent element))
3662 (org-remove-indentation c))))
3663 ;; Get format used for references.
3664 (label-fmt (regexp-quote
3665 (or (org-element-property :label-fmt element)
3666 org-coderef-label-format)))
3667 ;; Build a regexp matching a loc with a reference.
3668 (with-ref-re
3669 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3670 (replace-regexp-in-string
3671 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3672 ;; Return value.
3673 (cons
3674 ;; Code with references removed.
3675 (org-element-normalize-string
3676 (mapconcat
3677 (lambda (loc)
3678 (incf line)
3679 (if (not (string-match with-ref-re loc)) loc
3680 ;; Ref line: remove ref, and signal its position in REFS.
3681 (push (cons line (match-string 3 loc)) refs)
3682 (replace-match "" nil nil loc 1)))
3683 (org-split-string code "\n") "\n"))
3684 ;; Reference alist.
3685 refs)))
3687 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3688 "Format CODE by applying FUN line-wise and return it.
3690 CODE is a string representing the code to format. FUN is
3691 a function. It must accept three arguments: a line of
3692 code (string), the current line number (integer) or nil and the
3693 reference associated to the current line (string) or nil.
3695 Optional argument NUM-LINES can be an integer representing the
3696 number of code lines accumulated until the current code. Line
3697 numbers passed to FUN will take it into account. If it is nil,
3698 FUN's second argument will always be nil. This number can be
3699 obtained with `org-export-get-loc' function.
3701 Optional argument REF-ALIST can be an alist between relative line
3702 number (i.e. ignoring NUM-LINES) and the name of the code
3703 reference on it. If it is nil, FUN's third argument will always
3704 be nil. It can be obtained through the use of
3705 `org-export-unravel-code' function."
3706 (let ((--locs (org-split-string code "\n"))
3707 (--line 0))
3708 (org-element-normalize-string
3709 (mapconcat
3710 (lambda (--loc)
3711 (incf --line)
3712 (let ((--ref (cdr (assq --line ref-alist))))
3713 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3714 --locs "\n"))))
3716 (defun org-export-format-code-default (element info)
3717 "Return source code from ELEMENT, formatted in a standard way.
3719 ELEMENT is either a `src-block' or `example-block' element. INFO
3720 is a plist used as a communication channel.
3722 This function takes care of line numbering and code references
3723 inclusion. Line numbers, when applicable, appear at the
3724 beginning of the line, separated from the code by two white
3725 spaces. Code references, on the other hand, appear flushed to
3726 the right, separated by six white spaces from the widest line of
3727 code."
3728 ;; Extract code and references.
3729 (let* ((code-info (org-export-unravel-code element))
3730 (code (car code-info))
3731 (code-lines (org-split-string code "\n"))
3732 (refs (and (org-element-property :retain-labels element)
3733 (cdr code-info)))
3734 ;; Handle line numbering.
3735 (num-start (case (org-element-property :number-lines element)
3736 (continued (org-export-get-loc element info))
3737 (new 0)))
3738 (num-fmt
3739 (and num-start
3740 (format "%%%ds "
3741 (length (number-to-string
3742 (+ (length code-lines) num-start))))))
3743 ;; Prepare references display, if required. Any reference
3744 ;; should start six columns after the widest line of code,
3745 ;; wrapped with parenthesis.
3746 (max-width
3747 (+ (apply 'max (mapcar 'length code-lines))
3748 (if (not num-start) 0 (length (format num-fmt num-start))))))
3749 (org-export-format-code
3750 code
3751 (lambda (loc line-num ref)
3752 (let ((number-str (and num-fmt (format num-fmt line-num))))
3753 (concat
3754 number-str
3756 (and ref
3757 (concat (make-string
3758 (- (+ 6 max-width)
3759 (+ (length loc) (length number-str))) ? )
3760 (format "(%s)" ref))))))
3761 num-start refs)))
3764 ;;;; For Tables
3766 ;; `org-export-table-has-special-column-p' and and
3767 ;; `org-export-table-row-is-special-p' are predicates used to look for
3768 ;; meta-information about the table structure.
3770 ;; `org-table-has-header-p' tells when the rows before the first rule
3771 ;; should be considered as table's header.
3773 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3774 ;; and `org-export-table-cell-borders' extract information from
3775 ;; a table-cell element.
3777 ;; `org-export-table-dimensions' gives the number on rows and columns
3778 ;; in the table, ignoring horizontal rules and special columns.
3779 ;; `org-export-table-cell-address', given a table-cell object, returns
3780 ;; the absolute address of a cell. On the other hand,
3781 ;; `org-export-get-table-cell-at' does the contrary.
3783 ;; `org-export-table-cell-starts-colgroup-p',
3784 ;; `org-export-table-cell-ends-colgroup-p',
3785 ;; `org-export-table-row-starts-rowgroup-p',
3786 ;; `org-export-table-row-ends-rowgroup-p',
3787 ;; `org-export-table-row-starts-header-p' and
3788 ;; `org-export-table-row-ends-header-p' indicate position of current
3789 ;; row or cell within the table.
3791 (defun org-export-table-has-special-column-p (table)
3792 "Non-nil when TABLE has a special column.
3793 All special columns will be ignored during export."
3794 ;; The table has a special column when every first cell of every row
3795 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3796 ;; "*" "_" and "^". Though, do not consider a first row containing
3797 ;; only empty cells as special.
3798 (let ((special-column-p 'empty))
3799 (catch 'exit
3800 (mapc
3801 (lambda (row)
3802 (when (eq (org-element-property :type row) 'standard)
3803 (let ((value (org-element-contents
3804 (car (org-element-contents row)))))
3805 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3806 (setq special-column-p 'special))
3807 ((not value))
3808 (t (throw 'exit nil))))))
3809 (org-element-contents table))
3810 (eq special-column-p 'special))))
3812 (defun org-export-table-has-header-p (table info)
3813 "Non-nil when TABLE has an header.
3815 INFO is a plist used as a communication channel.
3817 A table has an header when it contains at least two row groups."
3818 (let ((rowgroup 1) row-flag)
3819 (org-element-map
3820 table 'table-row
3821 (lambda (row)
3822 (cond
3823 ((> rowgroup 1) t)
3824 ((and row-flag (eq (org-element-property :type row) 'rule))
3825 (incf rowgroup) (setq row-flag nil))
3826 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3827 (setq row-flag t) nil)))
3828 info)))
3830 (defun org-export-table-row-is-special-p (table-row info)
3831 "Non-nil if TABLE-ROW is considered special.
3833 INFO is a plist used as the communication channel.
3835 All special rows will be ignored during export."
3836 (when (eq (org-element-property :type table-row) 'standard)
3837 (let ((first-cell (org-element-contents
3838 (car (org-element-contents table-row)))))
3839 ;; A row is special either when...
3841 ;; ... it starts with a field only containing "/",
3842 (equal first-cell '("/"))
3843 ;; ... the table contains a special column and the row start
3844 ;; with a marking character among, "^", "_", "$" or "!",
3845 (and (org-export-table-has-special-column-p
3846 (org-export-get-parent table-row))
3847 (member first-cell '(("^") ("_") ("$") ("!"))))
3848 ;; ... it contains only alignment cookies and empty cells.
3849 (let ((special-row-p 'empty))
3850 (catch 'exit
3851 (mapc
3852 (lambda (cell)
3853 (let ((value (org-element-contents cell)))
3854 ;; Since VALUE is a secondary string, the following
3855 ;; checks avoid expanding it with `org-export-data'.
3856 (cond ((not value))
3857 ((and (not (cdr value))
3858 (stringp (car value))
3859 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3860 (car value)))
3861 (setq special-row-p 'cookie))
3862 (t (throw 'exit nil)))))
3863 (org-element-contents table-row))
3864 (eq special-row-p 'cookie)))))))
3866 (defun org-export-table-row-group (table-row info)
3867 "Return TABLE-ROW's group.
3869 INFO is a plist used as the communication channel.
3871 Return value is the group number, as an integer, or nil special
3872 rows and table rules. Group 1 is also table's header."
3873 (unless (or (eq (org-element-property :type table-row) 'rule)
3874 (org-export-table-row-is-special-p table-row info))
3875 (let ((group 0) row-flag)
3876 (catch 'found
3877 (mapc
3878 (lambda (row)
3879 (cond
3880 ((and (eq (org-element-property :type row) 'standard)
3881 (not (org-export-table-row-is-special-p row info)))
3882 (unless row-flag (incf group) (setq row-flag t)))
3883 ((eq (org-element-property :type row) 'rule)
3884 (setq row-flag nil)))
3885 (when (eq table-row row) (throw 'found group)))
3886 (org-element-contents (org-export-get-parent table-row)))))))
3888 (defun org-export-table-cell-width (table-cell info)
3889 "Return TABLE-CELL contents width.
3891 INFO is a plist used as the communication channel.
3893 Return value is the width given by the last width cookie in the
3894 same column as TABLE-CELL, or nil."
3895 (let* ((row (org-export-get-parent table-cell))
3896 (column (let ((cells (org-element-contents row)))
3897 (- (length cells) (length (memq table-cell cells)))))
3898 (table (org-export-get-parent-table table-cell))
3899 cookie-width)
3900 (mapc
3901 (lambda (row)
3902 (cond
3903 ;; In a special row, try to find a width cookie at COLUMN.
3904 ((org-export-table-row-is-special-p row info)
3905 (let ((value (org-element-contents
3906 (elt (org-element-contents row) column))))
3907 ;; The following checks avoid expanding unnecessarily the
3908 ;; cell with `org-export-data'
3909 (when (and value
3910 (not (cdr value))
3911 (stringp (car value))
3912 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3913 (match-string 1 (car value)))
3914 (setq cookie-width
3915 (string-to-number (match-string 1 (car value)))))))
3916 ;; Ignore table rules.
3917 ((eq (org-element-property :type row) 'rule))))
3918 (org-element-contents table))
3919 ;; Return value.
3920 cookie-width))
3922 (defun org-export-table-cell-alignment (table-cell info)
3923 "Return TABLE-CELL contents alignment.
3925 INFO is a plist used as the communication channel.
3927 Return alignment as specified by the last alignment cookie in the
3928 same column as TABLE-CELL. If no such cookie is found, a default
3929 alignment value will be deduced from fraction of numbers in the
3930 column (see `org-table-number-fraction' for more information).
3931 Possible values are `left', `right' and `center'."
3932 (let* ((row (org-export-get-parent table-cell))
3933 (column (let ((cells (org-element-contents row)))
3934 (- (length cells) (length (memq table-cell cells)))))
3935 (table (org-export-get-parent-table table-cell))
3936 (number-cells 0)
3937 (total-cells 0)
3938 cookie-align)
3939 (mapc
3940 (lambda (row)
3941 (cond
3942 ;; In a special row, try to find an alignment cookie at
3943 ;; COLUMN.
3944 ((org-export-table-row-is-special-p row info)
3945 (let ((value (org-element-contents
3946 (elt (org-element-contents row) column))))
3947 ;; Since VALUE is a secondary string, the following checks
3948 ;; avoid useless expansion through `org-export-data'.
3949 (when (and value
3950 (not (cdr value))
3951 (stringp (car value))
3952 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3953 (car value))
3954 (match-string 1 (car value)))
3955 (setq cookie-align (match-string 1 (car value))))))
3956 ;; Ignore table rules.
3957 ((eq (org-element-property :type row) 'rule))
3958 ;; In a standard row, check if cell's contents are expressing
3959 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3960 ;; Though, don't bother if an alignment cookie has already
3961 ;; defined cell's alignment.
3962 ((not cookie-align)
3963 (let ((value (org-export-data
3964 (org-element-contents
3965 (elt (org-element-contents row) column))
3966 info)))
3967 (incf total-cells)
3968 (when (string-match org-table-number-regexp value)
3969 (incf number-cells))))))
3970 (org-element-contents table))
3971 ;; Return value. Alignment specified by cookies has precedence
3972 ;; over alignment deduced from cells contents.
3973 (cond ((equal cookie-align "l") 'left)
3974 ((equal cookie-align "r") 'right)
3975 ((equal cookie-align "c") 'center)
3976 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3977 'right)
3978 (t 'left))))
3980 (defun org-export-table-cell-borders (table-cell info)
3981 "Return TABLE-CELL borders.
3983 INFO is a plist used as a communication channel.
3985 Return value is a list of symbols, or nil. Possible values are:
3986 `top', `bottom', `above', `below', `left' and `right'. Note:
3987 `top' (resp. `bottom') only happen for a cell in the first
3988 row (resp. last row) of the table, ignoring table rules, if any.
3990 Returned borders ignore special rows."
3991 (let* ((row (org-export-get-parent table-cell))
3992 (table (org-export-get-parent-table table-cell))
3993 borders)
3994 ;; Top/above border? TABLE-CELL has a border above when a rule
3995 ;; used to demarcate row groups can be found above. Hence,
3996 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3997 ;; another regular row has to be found above that rule.
3998 (let (rule-flag)
3999 (catch 'exit
4000 (mapc (lambda (row)
4001 (cond ((eq (org-element-property :type row) 'rule)
4002 (setq rule-flag t))
4003 ((not (org-export-table-row-is-special-p row info))
4004 (if rule-flag (throw 'exit (push 'above borders))
4005 (throw 'exit nil)))))
4006 ;; Look at every row before the current one.
4007 (cdr (memq row (reverse (org-element-contents table)))))
4008 ;; No rule above, or rule found starts the table (ignoring any
4009 ;; special row): TABLE-CELL is at the top of the table.
4010 (when rule-flag (push 'above borders))
4011 (push 'top borders)))
4012 ;; Bottom/below border? TABLE-CELL has a border below when next
4013 ;; non-regular row below is a rule.
4014 (let (rule-flag)
4015 (catch 'exit
4016 (mapc (lambda (row)
4017 (cond ((eq (org-element-property :type row) 'rule)
4018 (setq rule-flag t))
4019 ((not (org-export-table-row-is-special-p row info))
4020 (if rule-flag (throw 'exit (push 'below borders))
4021 (throw 'exit nil)))))
4022 ;; Look at every row after the current one.
4023 (cdr (memq row (org-element-contents table))))
4024 ;; No rule below, or rule found ends the table (modulo some
4025 ;; special row): TABLE-CELL is at the bottom of the table.
4026 (when rule-flag (push 'below borders))
4027 (push 'bottom borders)))
4028 ;; Right/left borders? They can only be specified by column
4029 ;; groups. Column groups are defined in a row starting with "/".
4030 ;; Also a column groups row only contains "<", "<>", ">" or blank
4031 ;; cells.
4032 (catch 'exit
4033 (let ((column (let ((cells (org-element-contents row)))
4034 (- (length cells) (length (memq table-cell cells))))))
4035 (mapc
4036 (lambda (row)
4037 (unless (eq (org-element-property :type row) 'rule)
4038 (when (equal (org-element-contents
4039 (car (org-element-contents row)))
4040 '("/"))
4041 (let ((column-groups
4042 (mapcar
4043 (lambda (cell)
4044 (let ((value (org-element-contents cell)))
4045 (when (member value '(("<") ("<>") (">") nil))
4046 (car value))))
4047 (org-element-contents row))))
4048 ;; There's a left border when previous cell, if
4049 ;; any, ends a group, or current one starts one.
4050 (when (or (and (not (zerop column))
4051 (member (elt column-groups (1- column))
4052 '(">" "<>")))
4053 (member (elt column-groups column) '("<" "<>")))
4054 (push 'left borders))
4055 ;; There's a right border when next cell, if any,
4056 ;; starts a group, or current one ends one.
4057 (when (or (and (/= (1+ column) (length column-groups))
4058 (member (elt column-groups (1+ column))
4059 '("<" "<>")))
4060 (member (elt column-groups column) '(">" "<>")))
4061 (push 'right borders))
4062 (throw 'exit nil)))))
4063 ;; Table rows are read in reverse order so last column groups
4064 ;; row has precedence over any previous one.
4065 (reverse (org-element-contents table)))))
4066 ;; Return value.
4067 borders))
4069 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4070 "Non-nil when TABLE-CELL is at the beginning of a row group.
4071 INFO is a plist used as a communication channel."
4072 ;; A cell starts a column group either when it is at the beginning
4073 ;; of a row (or after the special column, if any) or when it has
4074 ;; a left border.
4075 (or (eq (org-element-map
4076 (org-export-get-parent table-cell)
4077 'table-cell 'identity info 'first-match)
4078 table-cell)
4079 (memq 'left (org-export-table-cell-borders table-cell info))))
4081 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4082 "Non-nil when TABLE-CELL is at the end of a row group.
4083 INFO is a plist used as a communication channel."
4084 ;; A cell ends a column group either when it is at the end of a row
4085 ;; or when it has a right border.
4086 (or (eq (car (last (org-element-contents
4087 (org-export-get-parent table-cell))))
4088 table-cell)
4089 (memq 'right (org-export-table-cell-borders table-cell info))))
4091 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4092 "Non-nil when TABLE-ROW is at the beginning of a column group.
4093 INFO is a plist used as a communication channel."
4094 (unless (or (eq (org-element-property :type table-row) 'rule)
4095 (org-export-table-row-is-special-p table-row info))
4096 (let ((borders (org-export-table-cell-borders
4097 (car (org-element-contents table-row)) info)))
4098 (or (memq 'top borders) (memq 'above borders)))))
4100 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4101 "Non-nil when TABLE-ROW is at the end of a column group.
4102 INFO is a plist used as a communication channel."
4103 (unless (or (eq (org-element-property :type table-row) 'rule)
4104 (org-export-table-row-is-special-p table-row info))
4105 (let ((borders (org-export-table-cell-borders
4106 (car (org-element-contents table-row)) info)))
4107 (or (memq 'bottom borders) (memq 'below borders)))))
4109 (defun org-export-table-row-starts-header-p (table-row info)
4110 "Non-nil when TABLE-ROW is the first table header's row.
4111 INFO is a plist used as a communication channel."
4112 (and (org-export-table-has-header-p
4113 (org-export-get-parent-table table-row) info)
4114 (org-export-table-row-starts-rowgroup-p table-row info)
4115 (= (org-export-table-row-group table-row info) 1)))
4117 (defun org-export-table-row-ends-header-p (table-row info)
4118 "Non-nil when TABLE-ROW is the last table header's row.
4119 INFO is a plist used as a communication channel."
4120 (and (org-export-table-has-header-p
4121 (org-export-get-parent-table table-row) info)
4122 (org-export-table-row-ends-rowgroup-p table-row info)
4123 (= (org-export-table-row-group table-row info) 1)))
4125 (defun org-export-table-dimensions (table info)
4126 "Return TABLE dimensions.
4128 INFO is a plist used as a communication channel.
4130 Return value is a CONS like (ROWS . COLUMNS) where
4131 ROWS (resp. COLUMNS) is the number of exportable
4132 rows (resp. columns)."
4133 (let (first-row (columns 0) (rows 0))
4134 ;; Set number of rows, and extract first one.
4135 (org-element-map
4136 table 'table-row
4137 (lambda (row)
4138 (when (eq (org-element-property :type row) 'standard)
4139 (incf rows)
4140 (unless first-row (setq first-row row)))) info)
4141 ;; Set number of columns.
4142 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4143 ;; Return value.
4144 (cons rows columns)))
4146 (defun org-export-table-cell-address (table-cell info)
4147 "Return address of a regular TABLE-CELL object.
4149 TABLE-CELL is the cell considered. INFO is a plist used as
4150 a communication channel.
4152 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4153 zero-based index. Only exportable cells are considered. The
4154 function returns nil for other cells."
4155 (let* ((table-row (org-export-get-parent table-cell))
4156 (table (org-export-get-parent-table table-cell)))
4157 ;; Ignore cells in special rows or in special column.
4158 (unless (or (org-export-table-row-is-special-p table-row info)
4159 (and (org-export-table-has-special-column-p table)
4160 (eq (car (org-element-contents table-row)) table-cell)))
4161 (cons
4162 ;; Row number.
4163 (let ((row-count 0))
4164 (org-element-map
4165 table 'table-row
4166 (lambda (row)
4167 (cond ((eq (org-element-property :type row) 'rule) nil)
4168 ((eq row table-row) row-count)
4169 (t (incf row-count) nil)))
4170 info 'first-match))
4171 ;; Column number.
4172 (let ((col-count 0))
4173 (org-element-map
4174 table-row 'table-cell
4175 (lambda (cell)
4176 (if (eq cell table-cell) col-count (incf col-count) nil))
4177 info 'first-match))))))
4179 (defun org-export-get-table-cell-at (address table info)
4180 "Return regular table-cell object at ADDRESS in TABLE.
4182 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4183 zero-based index. TABLE is a table type element. INFO is
4184 a plist used as a communication channel.
4186 If no table-cell, among exportable cells, is found at ADDRESS,
4187 return nil."
4188 (let ((column-pos (cdr address)) (column-count 0))
4189 (org-element-map
4190 ;; Row at (car address) or nil.
4191 (let ((row-pos (car address)) (row-count 0))
4192 (org-element-map
4193 table 'table-row
4194 (lambda (row)
4195 (cond ((eq (org-element-property :type row) 'rule) nil)
4196 ((= row-count row-pos) row)
4197 (t (incf row-count) nil)))
4198 info 'first-match))
4199 'table-cell
4200 (lambda (cell)
4201 (if (= column-count column-pos) cell
4202 (incf column-count) nil))
4203 info 'first-match)))
4206 ;;;; For Tables Of Contents
4208 ;; `org-export-collect-headlines' builds a list of all exportable
4209 ;; headline elements, maybe limited to a certain depth. One can then
4210 ;; easily parse it and transcode it.
4212 ;; Building lists of tables, figures or listings is quite similar.
4213 ;; Once the generic function `org-export-collect-elements' is defined,
4214 ;; `org-export-collect-tables', `org-export-collect-figures' and
4215 ;; `org-export-collect-listings' can be derived from it.
4217 (defun org-export-collect-headlines (info &optional n)
4218 "Collect headlines in order to build a table of contents.
4220 INFO is a plist used as a communication channel.
4222 When optional argument N is an integer, it specifies the depth of
4223 the table of contents. Otherwise, it is set to the value of the
4224 last headline level. See `org-export-headline-levels' for more
4225 information.
4227 Return a list of all exportable headlines as parsed elements."
4228 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4229 (org-element-map
4230 (plist-get info :parse-tree)
4231 'headline
4232 (lambda (headline)
4233 ;; Strip contents from HEADLINE.
4234 (let ((relative-level (org-export-get-relative-level headline info)))
4235 (unless (> relative-level n) headline)))
4236 info))
4238 (defun org-export-collect-elements (type info &optional predicate)
4239 "Collect referenceable elements of a determined type.
4241 TYPE can be a symbol or a list of symbols specifying element
4242 types to search. Only elements with a caption are collected.
4244 INFO is a plist used as a communication channel.
4246 When non-nil, optional argument PREDICATE is a function accepting
4247 one argument, an element of type TYPE. It returns a non-nil
4248 value when that element should be collected.
4250 Return a list of all elements found, in order of appearance."
4251 (org-element-map
4252 (plist-get info :parse-tree) type
4253 (lambda (element)
4254 (and (org-element-property :caption element)
4255 (or (not predicate) (funcall predicate element))
4256 element))
4257 info))
4259 (defun org-export-collect-tables (info)
4260 "Build a list of tables.
4261 INFO is a plist used as a communication channel.
4263 Return a list of table elements with a caption."
4264 (org-export-collect-elements 'table info))
4266 (defun org-export-collect-figures (info predicate)
4267 "Build a list of figures.
4269 INFO is a plist used as a communication channel. PREDICATE is
4270 a function which accepts one argument: a paragraph element and
4271 whose return value is non-nil when that element should be
4272 collected.
4274 A figure is a paragraph type element, with a caption, verifying
4275 PREDICATE. The latter has to be provided since a \"figure\" is
4276 a vague concept that may depend on back-end.
4278 Return a list of elements recognized as figures."
4279 (org-export-collect-elements 'paragraph info predicate))
4281 (defun org-export-collect-listings (info)
4282 "Build a list of src blocks.
4284 INFO is a plist used as a communication channel.
4286 Return a list of src-block elements with a caption."
4287 (org-export-collect-elements 'src-block info))
4290 ;;;; For Timestamps
4292 ;; `org-export-timestamp-has-time-p' is a predicate to know if hours
4293 ;; and minutes are defined in a given timestamp.
4295 ;; `org-export-format-timestamp' allows to format a timestamp object
4296 ;; with an arbitrary format string.
4298 (defun org-export-timestamp-has-time-p (timestamp)
4299 "Non-nil when TIMESTAMP has a time specified."
4300 (org-element-property :hour-start timestamp))
4302 (defun org-export-format-timestamp (timestamp format &optional end utc)
4303 "Format a TIMESTAMP element into a string.
4305 FORMAT is a format specifier to be passed to
4306 `format-time-string'.
4308 When optional argument END is non-nil, use end of date-range or
4309 time-range, if possible.
4311 When optional argument UTC is non-nil, time will be expressed as
4312 Universal Time."
4313 (format-time-string
4314 format
4315 (apply 'encode-time
4316 (cons 0
4317 (mapcar
4318 (lambda (prop) (or (org-element-property prop timestamp) 0))
4319 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
4320 '(:minute-start :hour-start :day-start :month-start
4321 :year-start)))))
4322 utc))
4324 (defun org-export-split-timestamp-range (timestamp &optional end)
4325 "Extract a timestamp object from a date or time range.
4327 TIMESTAMP is a timestamp object. END, when non-nil, means extract
4328 the end of the range. Otherwise, extract its start.
4330 Return a new timestamp object sharing the same parent as
4331 TIMESTAMP."
4332 (let ((type (org-element-property :type timestamp)))
4333 (if (memq type '(active inactive diary)) timestamp
4334 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
4335 ;; Set new type.
4336 (org-element-put-property
4337 split-ts :type (if (eq type 'active-range) 'active 'inactive))
4338 ;; Copy start properties over end properties if END is
4339 ;; non-nil. Otherwise, copy end properties over `start' ones.
4340 (let ((p-alist '((:minute-start . :minute-end)
4341 (:hour-start . :hour-end)
4342 (:day-start . :day-end)
4343 (:month-start . :month-end)
4344 (:year-start . :year-end))))
4345 (dolist (p-cell p-alist)
4346 (org-element-put-property
4347 split-ts
4348 (funcall (if end 'car 'cdr) p-cell)
4349 (org-element-property
4350 (funcall (if end 'cdr 'car) p-cell) split-ts)))
4351 ;; Eventually refresh `:raw-value'.
4352 (org-element-put-property split-ts :raw-value nil)
4353 (org-element-put-property
4354 split-ts :raw-value (org-element-interpret-data split-ts)))))))
4356 (defun org-export-translate-timestamp (timestamp &optional boundary)
4357 "Apply `org-translate-time' on a TIMESTAMP object.
4358 When optional argument BOUNDARY is non-nil, it is either the
4359 symbol `start' or `end'. In this case, only translate the
4360 starting or ending part of TIMESTAMP if it is a date or time
4361 range. Otherwise, translate both parts."
4362 (if (and (not boundary)
4363 (memq (org-element-property :type timestamp)
4364 '(active-range inactive-range)))
4365 (concat
4366 (org-translate-time
4367 (org-element-property :raw-value
4368 (org-export-split-timestamp-range timestamp)))
4369 "--"
4370 (org-translate-time
4371 (org-element-property :raw-value
4372 (org-export-split-timestamp-range timestamp t))))
4373 (org-translate-time
4374 (org-element-property
4375 :raw-value
4376 (if (not boundary) timestamp
4377 (org-export-split-timestamp-range timestamp (eq boundary 'end)))))))
4380 ;;;; Smart Quotes
4382 ;; The main function for the smart quotes sub-system is
4383 ;; `org-export-activate-smart-quotes', which replaces every quote in
4384 ;; a given string from the parse tree with its "smart" counterpart.
4386 ;; Dictionary for smart quotes is stored in
4387 ;; `org-export-smart-quotes-alist'.
4389 ;; Internally, regexps matching potential smart quotes (checks at
4390 ;; string boundaries are also necessary) are defined in
4391 ;; `org-export-smart-quotes-regexps'.
4393 (defconst org-export-smart-quotes-alist
4394 '(("de"
4395 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4396 :texinfo "@quotedblbase{}")
4397 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4398 :texinfo "@quotedblleft{}")
4399 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4400 :texinfo "@quotesinglbase{}")
4401 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4402 :texinfo "@quoteleft{}")
4403 (apostrophe :utf-8 "’" :html "&rsquo;"))
4404 ("en"
4405 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4406 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4407 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4408 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4409 (apostrophe :utf-8 "’" :html "&rsquo;"))
4410 ("es"
4411 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4412 :texinfo "@guillemetleft{}")
4413 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4414 :texinfo "@guillemetright{}")
4415 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4416 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4417 (apostrophe :utf-8 "’" :html "&rsquo;"))
4418 ("fr"
4419 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4420 :texinfo "@guillemetleft{}@tie{}")
4421 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4422 :texinfo "@tie{}@guillemetright{}")
4423 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4424 :texinfo "@guillemetleft{}@tie{}")
4425 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4426 :texinfo "@tie{}@guillemetright{}")
4427 (apostrophe :utf-8 "’" :html "&rsquo;")))
4428 "Smart quotes translations.
4430 Alist whose CAR is a language string and CDR is an alist with
4431 quote type as key and a plist associating various encodings to
4432 their translation as value.
4434 A quote type can be any symbol among `opening-double-quote',
4435 `closing-double-quote', `opening-single-quote',
4436 `closing-single-quote' and `apostrophe'.
4438 Valid encodings include `:utf-8', `:html', `:latex' and
4439 `:texinfo'.
4441 If no translation is found, the quote character is left as-is.")
4443 (defconst org-export-smart-quotes-regexps
4444 (list
4445 ;; Possible opening quote at beginning of string.
4446 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4447 ;; Possible closing quote at beginning of string.
4448 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4449 ;; Possible apostrophe at beginning of string.
4450 "\\`\\('\\)\\S-"
4451 ;; Opening single and double quotes.
4452 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4453 ;; Closing single and double quotes.
4454 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4455 ;; Apostrophe.
4456 "\\S-\\('\\)\\S-"
4457 ;; Possible opening quote at end of string.
4458 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4459 ;; Possible closing quote at end of string.
4460 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4461 ;; Possible apostrophe at end of string.
4462 "\\S-\\('\\)\\'")
4463 "List of regexps matching a quote or an apostrophe.
4464 In every regexp, quote or apostrophe matched is put in group 1.")
4466 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4467 "Replace regular quotes with \"smart\" quotes in string S.
4469 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4470 `:utf-8'. INFO is a plist used as a communication channel.
4472 The function has to retrieve information about string
4473 surroundings in parse tree. It can only happen with an
4474 unmodified string. Thus, if S has already been through another
4475 process, a non-nil ORIGINAL optional argument will provide that
4476 original string.
4478 Return the new string."
4479 (if (equal s "") ""
4480 (let* ((prev (org-export-get-previous-element (or original s) info))
4481 (pre-blank (and prev (org-element-property :post-blank prev)))
4482 (next (org-export-get-next-element (or original s) info))
4483 (get-smart-quote
4484 (lambda (q type)
4485 ;; Return smart quote associated to a give quote Q, as
4486 ;; a string. TYPE is a symbol among `open', `close' and
4487 ;; `apostrophe'.
4488 (let ((key (case type
4489 (apostrophe 'apostrophe)
4490 (open (if (equal "'" q) 'opening-single-quote
4491 'opening-double-quote))
4492 (otherwise (if (equal "'" q) 'closing-single-quote
4493 'closing-double-quote)))))
4494 (or (plist-get
4495 (cdr (assq key
4496 (cdr (assoc (plist-get info :language)
4497 org-export-smart-quotes-alist))))
4498 encoding)
4499 q)))))
4500 (if (or (equal "\"" s) (equal "'" s))
4501 ;; Only a quote: no regexp can match. We have to check both
4502 ;; sides and decide what to do.
4503 (cond ((and (not prev) (not next)) s)
4504 ((not prev) (funcall get-smart-quote s 'open))
4505 ((and (not next) (zerop pre-blank))
4506 (funcall get-smart-quote s 'close))
4507 ((not next) s)
4508 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
4509 (t (funcall get-smart-quote 'open)))
4510 ;; 1. Replace quote character at the beginning of S.
4511 (cond
4512 ;; Apostrophe?
4513 ((and prev (zerop pre-blank)
4514 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4515 (setq s (replace-match
4516 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4517 nil t s 1)))
4518 ;; Closing quote?
4519 ((and prev (zerop pre-blank)
4520 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4521 (setq s (replace-match
4522 (funcall get-smart-quote (match-string 1 s) 'close)
4523 nil t s 1)))
4524 ;; Opening quote?
4525 ((and (or (not prev) (> pre-blank 0))
4526 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4527 (setq s (replace-match
4528 (funcall get-smart-quote (match-string 1 s) 'open)
4529 nil t s 1))))
4530 ;; 2. Replace quotes in the middle of the string.
4531 (setq s (replace-regexp-in-string
4532 ;; Opening quotes.
4533 (nth 3 org-export-smart-quotes-regexps)
4534 (lambda (text)
4535 (funcall get-smart-quote (match-string 1 text) 'open))
4536 s nil t 1))
4537 (setq s (replace-regexp-in-string
4538 ;; Closing quotes.
4539 (nth 4 org-export-smart-quotes-regexps)
4540 (lambda (text)
4541 (funcall get-smart-quote (match-string 1 text) 'close))
4542 s nil t 1))
4543 (setq s (replace-regexp-in-string
4544 ;; Apostrophes.
4545 (nth 5 org-export-smart-quotes-regexps)
4546 (lambda (text)
4547 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
4548 s nil t 1))
4549 ;; 3. Replace quote character at the end of S.
4550 (cond
4551 ;; Apostrophe?
4552 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4553 (setq s (replace-match
4554 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4555 nil t s 1)))
4556 ;; Closing quote?
4557 ((and (not next)
4558 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4559 (setq s (replace-match
4560 (funcall get-smart-quote (match-string 1 s) 'close)
4561 nil t s 1)))
4562 ;; Opening quote?
4563 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4564 (setq s (replace-match
4565 (funcall get-smart-quote (match-string 1 s) 'open)
4566 nil t s 1))))
4567 ;; Return string with smart quotes.
4568 s))))
4570 ;;;; Topology
4572 ;; Here are various functions to retrieve information about the
4573 ;; neighbourhood of a given element or object. Neighbours of interest
4574 ;; are direct parent (`org-export-get-parent'), parent headline
4575 ;; (`org-export-get-parent-headline'), first element containing an
4576 ;; object, (`org-export-get-parent-element'), parent table
4577 ;; (`org-export-get-parent-table'), previous element or object
4578 ;; (`org-export-get-previous-element') and next element or object
4579 ;; (`org-export-get-next-element').
4581 ;; `org-export-get-genealogy' returns the full genealogy of a given
4582 ;; element or object, from closest parent to full parse tree.
4584 (defun org-export-get-parent (blob)
4585 "Return BLOB parent or nil.
4586 BLOB is the element or object considered."
4587 (org-element-property :parent blob))
4589 (defun org-export-get-genealogy (blob)
4590 "Return full genealogy relative to a given element or object.
4592 BLOB is the element or object being considered.
4594 Ancestors are returned from closest to farthest, the last one
4595 being the full parse tree."
4596 (let (genealogy (parent blob))
4597 (while (setq parent (org-element-property :parent parent))
4598 (push parent genealogy))
4599 (nreverse genealogy)))
4601 (defun org-export-get-parent-headline (blob)
4602 "Return BLOB parent headline or nil.
4603 BLOB is the element or object being considered."
4604 (let ((parent blob))
4605 (while (and (setq parent (org-element-property :parent parent))
4606 (not (eq (org-element-type parent) 'headline))))
4607 parent))
4609 (defun org-export-get-parent-element (object)
4610 "Return first element containing OBJECT or nil.
4611 OBJECT is the object to consider."
4612 (let ((parent object))
4613 (while (and (setq parent (org-element-property :parent parent))
4614 (memq (org-element-type parent) org-element-all-objects)))
4615 parent))
4617 (defun org-export-get-parent-table (object)
4618 "Return OBJECT parent table or nil.
4619 OBJECT is either a `table-cell' or `table-element' type object."
4620 (let ((parent object))
4621 (while (and (setq parent (org-element-property :parent parent))
4622 (not (eq (org-element-type parent) 'table))))
4623 parent))
4625 (defun org-export-get-previous-element (blob info)
4626 "Return previous element or object.
4627 BLOB is an element or object. INFO is a plist used as
4628 a communication channel. Return previous exportable element or
4629 object, a string, or nil."
4630 (let (prev)
4631 (catch 'exit
4632 (mapc (lambda (obj)
4633 (cond ((eq obj blob) (throw 'exit prev))
4634 ((memq obj (plist-get info :ignore-list)))
4635 (t (setq prev obj))))
4636 ;; An object can belong to the contents of its parent or
4637 ;; to a secondary string. We check the latter option
4638 ;; first.
4639 (let ((parent (org-export-get-parent blob)))
4640 (or (and (not (memq (org-element-type blob)
4641 org-element-all-elements))
4642 (let ((sec-value
4643 (org-element-property
4644 (cdr (assq (org-element-type parent)
4645 org-element-secondary-value-alist))
4646 parent)))
4647 (and (memq blob sec-value) sec-value)))
4648 (org-element-contents parent)))))))
4650 (defun org-export-get-next-element (blob info)
4651 "Return next element or object.
4652 BLOB is an element or object. INFO is a plist used as
4653 a communication channel. Return next exportable element or
4654 object, a string, or nil."
4655 (catch 'found
4656 (mapc (lambda (obj)
4657 (unless (memq obj (plist-get info :ignore-list))
4658 (throw 'found obj)))
4659 ;; An object can belong to the contents of its parent or to
4660 ;; a secondary string. We check the latter option first.
4661 (let ((parent (org-export-get-parent blob)))
4662 (or (and (not (memq (org-element-type blob)
4663 org-element-all-objects))
4664 (let ((sec-value
4665 (org-element-property
4666 (cdr (assq (org-element-type parent)
4667 org-element-secondary-value-alist))
4668 parent)))
4669 (cdr (memq blob sec-value))))
4670 (cdr (memq blob (org-element-contents parent))))))
4671 nil))
4674 ;;;; Translation
4676 ;; `org-export-translate' translates a string according to language
4677 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4678 ;; variable and a specified charset. `org-export-dictionary' contains
4679 ;; the dictionary used for the translation.
4681 (defconst org-export-dictionary
4682 '(("Author"
4683 ("ca" :default "Autor")
4684 ("cs" :default "Autor")
4685 ("da" :default "Ophavsmand")
4686 ("de" :default "Autor")
4687 ("eo" :html "A&#365;toro")
4688 ("es" :default "Autor")
4689 ("fi" :html "Tekij&auml;")
4690 ("fr" :default "Auteur")
4691 ("hu" :default "Szerz&otilde;")
4692 ("is" :html "H&ouml;fundur")
4693 ("it" :default "Autore")
4694 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4695 ("nl" :default "Auteur")
4696 ("no" :default "Forfatter")
4697 ("nb" :default "Forfatter")
4698 ("nn" :default "Forfattar")
4699 ("pl" :default "Autor")
4700 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4701 ("sv" :html "F&ouml;rfattare")
4702 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4703 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4704 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4705 ("Date"
4706 ("ca" :default "Data")
4707 ("cs" :default "Datum")
4708 ("da" :default "Dato")
4709 ("de" :default "Datum")
4710 ("eo" :default "Dato")
4711 ("es" :default "Fecha")
4712 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4713 ("hu" :html "D&aacute;tum")
4714 ("is" :default "Dagsetning")
4715 ("it" :default "Data")
4716 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4717 ("nl" :default "Datum")
4718 ("no" :default "Dato")
4719 ("nb" :default "Dato")
4720 ("nn" :default "Dato")
4721 ("pl" :default "Data")
4722 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4723 ("sv" :default "Datum")
4724 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4725 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4726 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4727 ("Equation"
4728 ("fr" :ascii "Equation" :default "Équation"))
4729 ("Figure")
4730 ("Footnotes"
4731 ("ca" :html "Peus de p&agrave;gina")
4732 ("cs" :default "Pozn\xe1mky pod carou")
4733 ("da" :default "Fodnoter")
4734 ("de" :html "Fu&szlig;noten")
4735 ("eo" :default "Piednotoj")
4736 ("es" :html "Pies de p&aacute;gina")
4737 ("fi" :default "Alaviitteet")
4738 ("fr" :default "Notes de bas de page")
4739 ("hu" :html "L&aacute;bjegyzet")
4740 ("is" :html "Aftanm&aacute;lsgreinar")
4741 ("it" :html "Note a pi&egrave; di pagina")
4742 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4743 ("nl" :default "Voetnoten")
4744 ("no" :default "Fotnoter")
4745 ("nb" :default "Fotnoter")
4746 ("nn" :default "Fotnotar")
4747 ("pl" :default "Przypis")
4748 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4749 ("sv" :default "Fotnoter")
4750 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4751 :utf-8 "Примітки")
4752 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4753 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4754 ("List of Listings"
4755 ("fr" :default "Liste des programmes"))
4756 ("List of Tables"
4757 ("fr" :default "Liste des tableaux"))
4758 ("Listing %d:"
4759 ("fr"
4760 :ascii "Programme %d :" :default "Programme nº %d :"
4761 :latin1 "Programme %d :"))
4762 ("Listing %d: %s"
4763 ("fr"
4764 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4765 :latin1 "Programme %d : %s"))
4766 ("See section %s"
4767 ("fr" :default "cf. section %s"))
4768 ("Table %d:"
4769 ("fr"
4770 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4771 ("Table %d: %s"
4772 ("fr"
4773 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4774 :latin1 "Tableau %d : %s"))
4775 ("Table of Contents"
4776 ("ca" :html "&Iacute;ndex")
4777 ("cs" :default "Obsah")
4778 ("da" :default "Indhold")
4779 ("de" :default "Inhaltsverzeichnis")
4780 ("eo" :default "Enhavo")
4781 ("es" :html "&Iacute;ndice")
4782 ("fi" :html "Sis&auml;llysluettelo")
4783 ("fr" :ascii "Sommaire" :default "Table des matières")
4784 ("hu" :html "Tartalomjegyz&eacute;k")
4785 ("is" :default "Efnisyfirlit")
4786 ("it" :default "Indice")
4787 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4788 ("nl" :default "Inhoudsopgave")
4789 ("no" :default "Innhold")
4790 ("nb" :default "Innhold")
4791 ("nn" :default "Innhald")
4792 ("pl" :html "Spis tre&#x015b;ci")
4793 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4794 :utf-8 "Содержание")
4795 ("sv" :html "Inneh&aring;ll")
4796 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4797 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4798 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4799 ("Unknown reference"
4800 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4801 "Dictionary for export engine.
4803 Alist whose CAR is the string to translate and CDR is an alist
4804 whose CAR is the language string and CDR is a plist whose
4805 properties are possible charsets and values translated terms.
4807 It is used as a database for `org-export-translate'. Since this
4808 function returns the string as-is if no translation was found,
4809 the variable only needs to record values different from the
4810 entry.")
4812 (defun org-export-translate (s encoding info)
4813 "Translate string S according to language specification.
4815 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4816 and `:utf-8'. INFO is a plist used as a communication channel.
4818 Translation depends on `:language' property. Return the
4819 translated string. If no translation is found, try to fall back
4820 to `:default' encoding. If it fails, return S."
4821 (let* ((lang (plist-get info :language))
4822 (translations (cdr (assoc lang
4823 (cdr (assoc s org-export-dictionary))))))
4824 (or (plist-get translations encoding)
4825 (plist-get translations :default)
4826 s)))
4830 ;;; The Dispatcher
4832 ;; `org-export-dispatch' is the standard interactive way to start an
4833 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4834 ;; for its interface, which, in turn, delegates response to key
4835 ;; pressed to `org-export-dispatch-action'.
4837 ;;;###autoload
4838 (defun org-export-dispatch ()
4839 "Export dispatcher for Org mode.
4841 It provides an access to common export related tasks in a buffer.
4842 Its interface comes in two flavours: standard and expert. While
4843 both share the same set of bindings, only the former displays the
4844 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4845 to switch to one or the other."
4846 (interactive)
4847 (let* ((input (save-window-excursion
4848 (unwind-protect
4849 (org-export-dispatch-ui (list org-export-initial-scope)
4851 org-export-dispatch-use-expert-ui)
4852 (and (get-buffer "*Org Export Dispatcher*")
4853 (kill-buffer "*Org Export Dispatcher*")))))
4854 (action (car input))
4855 (optns (cdr input)))
4856 (case action
4857 ;; First handle special hard-coded actions.
4858 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4859 (publish-current-project
4860 (org-e-publish-current-project (memq 'force optns)))
4861 (publish-choose-project
4862 (org-e-publish (assoc (org-icompleting-read
4863 "Publish project: "
4864 org-e-publish-project-alist nil t)
4865 org-e-publish-project-alist)
4866 (memq 'force optns)))
4867 (publish-all (org-e-publish-all (memq 'force optns)))
4868 (otherwise
4869 (funcall action
4870 (memq 'subtree optns)
4871 (memq 'visible optns)
4872 (memq 'body optns))))))
4874 (defun org-export-dispatch-ui (options first-key expertp)
4875 "Handle interface for `org-export-dispatch'.
4877 OPTIONS is a list containing current interactive options set for
4878 export. It can contain any of the following symbols:
4879 `body' toggles a body-only export
4880 `subtree' restricts export to current subtree
4881 `visible' restricts export to visible part of buffer.
4882 `force' force publishing files.
4884 FIRST-KEY is the key pressed to select the first level menu. It
4885 is nil when this menu hasn't been selected yet.
4887 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4888 buffer is provided, but indications about currently active
4889 options are given in the prompt. Moreover, \[?] allows to switch
4890 back to standard interface."
4891 (let* ((fontify-key
4892 (lambda (key &optional access-key)
4893 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4894 ;; non-nil is the required first-level key to activate
4895 ;; KEY. When its value is t, activate KEY independently
4896 ;; on the first key, if any. A nil value means KEY will
4897 ;; only be activated at first level.
4898 (if (or (eq access-key t) (eq access-key first-key))
4899 (org-add-props key nil 'face 'org-warning)
4900 (org-no-properties key))))
4901 ;; Prepare menu entries by extracting them from
4902 ;; `org-export-registered-backends', and sorting them by
4903 ;; access key and by ordinal, if any.
4904 (backends (sort
4905 (sort
4906 (delq nil
4907 (mapcar (lambda (b)
4908 (org-export-backend-menu (car b)))
4909 org-export-registered-backends))
4910 (lambda (a b)
4911 (let ((key-a (nth 1 a))
4912 (key-b (nth 1 b)))
4913 (cond ((and (numberp key-a) (numberp key-b))
4914 (< key-a key-b))
4915 ((numberp key-b) t)))))
4916 (lambda (a b) (< (car a) (car b)))))
4917 ;; Compute a list of allowed keys based on the first key
4918 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4919 ;; always available.
4920 (allowed-keys
4921 (nconc (list ?1 ?2 ?3 ?4)
4922 (if (not first-key) (org-uniquify (mapcar 'car backends))
4923 (let (sub-menu)
4924 (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
4925 (when (eq (car backend) first-key)
4926 (setq sub-menu (append (nth 2 backend) sub-menu))))))
4927 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4928 ((not first-key) (list ?P)))
4929 (when expertp (list ??))
4930 (list ?q)))
4931 ;; Build the help menu for standard UI.
4932 (help
4933 (unless expertp
4934 (concat
4935 ;; Options are hard-coded.
4936 (format "Options
4937 [%s] Body only: %s [%s] Visible only: %s
4938 [%s] Export scope: %s [%s] Force publishing: %s\n"
4939 (funcall fontify-key "1" t)
4940 (if (memq 'body options) "On " "Off")
4941 (funcall fontify-key "2" t)
4942 (if (memq 'visible options) "On " "Off")
4943 (funcall fontify-key "3" t)
4944 (if (memq 'subtree options) "Subtree" "Buffer ")
4945 (funcall fontify-key "4" t)
4946 (if (memq 'force options) "On " "Off"))
4947 ;; Display registered back-end entries. When a key
4948 ;; appears for the second time, do not create another
4949 ;; entry, but append its sub-menu to existing menu.
4950 (let (last-key)
4951 (mapconcat
4952 (lambda (entry)
4953 (let ((top-key (car entry)))
4954 (concat
4955 (unless (eq top-key last-key)
4956 (setq last-key top-key)
4957 (format "\n[%s] %s\n"
4958 (funcall fontify-key (char-to-string top-key))
4959 (nth 1 entry)))
4960 (let ((sub-menu (nth 2 entry)))
4961 (unless (functionp sub-menu)
4962 ;; Split sub-menu into two columns.
4963 (let ((index -1))
4964 (concat
4965 (mapconcat
4966 (lambda (sub-entry)
4967 (incf index)
4968 (format
4969 (if (zerop (mod index 2)) " [%s] %-24s"
4970 "[%s] %s\n")
4971 (funcall fontify-key
4972 (char-to-string (car sub-entry))
4973 top-key)
4974 (nth 1 sub-entry)))
4975 sub-menu "")
4976 (when (zerop (mod index 2)) "\n"))))))))
4977 backends ""))
4978 ;; Publishing menu is hard-coded.
4979 (format "\n[%s] Publish
4980 [%s] Current file [%s] Current project
4981 [%s] Choose project [%s] All projects\n\n"
4982 (funcall fontify-key "P")
4983 (funcall fontify-key "f" ?P)
4984 (funcall fontify-key "p" ?P)
4985 (funcall fontify-key "x" ?P)
4986 (funcall fontify-key "a" ?P))
4987 (format "\[%s] %s"
4988 (funcall fontify-key "q" t)
4989 (if first-key "Main menu" "Exit")))))
4990 ;; Build prompts for both standard and expert UI.
4991 (standard-prompt (unless expertp "Export command: "))
4992 (expert-prompt
4993 (when expertp
4994 (format
4995 "Export command (Options: %s%s%s%s) [%s]: "
4996 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4997 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4998 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4999 (if (memq 'force options) (funcall fontify-key "f" t) "-")
5000 (concat allowed-keys)))))
5001 ;; With expert UI, just read key with a fancy prompt. In standard
5002 ;; UI, display an intrusive help buffer.
5003 (if expertp
5004 (org-export-dispatch-action
5005 expert-prompt allowed-keys backends options first-key expertp)
5006 ;; At first call, create frame layout in order to display menu.
5007 (unless (get-buffer "*Org Export Dispatcher*")
5008 (delete-other-windows)
5009 (org-switch-to-buffer-other-window
5010 (get-buffer-create "*Org Export Dispatcher*"))
5011 (setq cursor-type nil))
5012 ;; At this point, the buffer containing the menu exists and is
5013 ;; visible in the current window. So, refresh it.
5014 (with-current-buffer "*Org Export Dispatcher*"
5015 (erase-buffer)
5016 (insert help))
5017 (org-fit-window-to-buffer)
5018 (org-export-dispatch-action
5019 standard-prompt allowed-keys backends options first-key expertp))))
5021 (defun org-export-dispatch-action
5022 (prompt allowed-keys backends options first-key expertp)
5023 "Read a character from command input and act accordingly.
5025 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
5026 a list of characters available at a given step in the process.
5027 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
5028 EXPERTP are the same as defined in `org-export-dispatch-ui',
5029 which see.
5031 Toggle export options when required. Otherwise, return value is
5032 a list with action as CAR and a list of interactive export
5033 options as CDR."
5034 (let ((key (let ((k (read-char-exclusive prompt)))
5035 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
5036 ;; depending on user's key pressed.
5037 (if (< k 27) (+ k 96) k))))
5038 (cond
5039 ;; Ignore non-standard characters (i.e. "M-a") and
5040 ;; undefined associations.
5041 ((not (memq key allowed-keys))
5042 (ding)
5043 (unless expertp (message "Invalid key") (sit-for 1))
5044 (org-export-dispatch-ui options first-key expertp))
5045 ;; q key at first level aborts export. At second
5046 ;; level, cancel first key instead.
5047 ((eq key ?q) (if (not first-key) (error "Export aborted")
5048 (org-export-dispatch-ui options nil expertp)))
5049 ;; Help key: Switch back to standard interface if
5050 ;; expert UI was active.
5051 ((eq key ??) (org-export-dispatch-ui options first-key nil))
5052 ;; Toggle export options.
5053 ((memq key '(?1 ?2 ?3 ?4))
5054 (org-export-dispatch-ui
5055 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
5056 (?4 'force))))
5057 (if (memq option options) (remq option options)
5058 (cons option options)))
5059 first-key expertp))
5060 ;; Action selected: Send key and options back to
5061 ;; `org-export-dispatch'.
5062 ((or first-key (functionp (nth 2 (assq key backends))))
5063 (cons (cond
5064 ((not first-key) (nth 2 (assq key backends)))
5065 ;; Publishing actions are hard-coded. Send a special
5066 ;; signal to `org-export-dispatch'.
5067 ((eq first-key ?P)
5068 (case key
5069 (?f 'publish-current-file)
5070 (?p 'publish-current-project)
5071 (?x 'publish-choose-project)
5072 (?a 'publish-all)))
5073 ;; Return first action associated to FIRST-KEY + KEY
5074 ;; path. Indeed, derived backends can share the same
5075 ;; FIRST-KEY.
5076 (t (catch 'found
5077 (mapc (lambda (backend)
5078 (let ((match (assq key (nth 2 backend))))
5079 (when match (throw 'found (nth 2 match)))))
5080 (member (assq first-key backends) backends)))))
5081 options))
5082 ;; Otherwise, enter sub-menu.
5083 (t (org-export-dispatch-ui options key expertp)))))
5086 (provide 'org-export)
5087 ;;; org-export.el ends here