ox-html.el (org-html-format-inline-image): Fix missing string in formatting string
[org-mode.git] / lisp / ox.el
blobc5b6d7ca5d4542a44bda516baa19fae25ac99281
1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. 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 ;; following part of then file.
70 ;; Then, a wrapper macro for asynchronous export,
71 ;; `org-export-async-start', along with tools to display results. are
72 ;; given in the penultimate part.
74 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
75 ;; back-ends is provided in the last one.
77 ;;; Code:
79 (eval-when-compile (require 'cl))
80 (require 'org-element)
81 (require 'org-macro)
82 (require 'ob-exp)
84 (declare-function org-publish "ox-publish" (project &optional force async))
85 (declare-function org-publish-all "ox-publish" (&optional force async))
86 (declare-function
87 org-publish-current-file "ox-publish" (&optional force async))
88 (declare-function org-publish-current-project "ox-publish"
89 (&optional force async))
91 (defvar org-publish-project-alist)
92 (defvar org-table-number-fraction)
93 (defvar org-table-number-regexp)
97 ;;; Internal Variables
99 ;; Among internal variables, the most important is
100 ;; `org-export-options-alist'. This variable define the global export
101 ;; options, shared between every exporter, and how they are acquired.
103 (defconst org-export-max-depth 19
104 "Maximum nesting depth for headlines, counting from 0.")
106 (defconst org-export-options-alist
107 '((:author "AUTHOR" nil user-full-name t)
108 (:creator "CREATOR" nil org-export-creator-string)
109 (:date "DATE" nil nil t)
110 (:description "DESCRIPTION" nil nil newline)
111 (:email "EMAIL" nil user-mail-address t)
112 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
113 (:headline-levels nil "H" org-export-headline-levels)
114 (:keywords "KEYWORDS" nil nil space)
115 (:language "LANGUAGE" nil org-export-default-language t)
116 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
117 (:section-numbers nil "num" org-export-with-section-numbers)
118 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
119 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
120 (:title "TITLE" nil nil space)
121 (:with-archived-trees nil "arch" org-export-with-archived-trees)
122 (:with-author nil "author" org-export-with-author)
123 (:with-clocks nil "c" org-export-with-clocks)
124 (:with-creator nil "creator" org-export-with-creator)
125 (:with-date nil "date" org-export-with-date)
126 (:with-drawers nil "d" org-export-with-drawers)
127 (:with-email nil "email" org-export-with-email)
128 (:with-emphasize nil "*" org-export-with-emphasize)
129 (:with-entities nil "e" org-export-with-entities)
130 (:with-fixed-width nil ":" org-export-with-fixed-width)
131 (:with-footnotes nil "f" org-export-with-footnotes)
132 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
133 (:with-latex nil "tex" org-export-with-latex)
134 (:with-plannings nil "p" org-export-with-planning)
135 (:with-priority nil "pri" org-export-with-priority)
136 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
137 (:with-special-strings nil "-" org-export-with-special-strings)
138 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
139 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
140 (:with-toc nil "toc" org-export-with-toc)
141 (:with-tables nil "|" org-export-with-tables)
142 (:with-tags nil "tags" org-export-with-tags)
143 (:with-tasks nil "tasks" org-export-with-tasks)
144 (:with-timestamps nil "<" org-export-with-timestamps)
145 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
146 "Alist between export properties and ways to set them.
148 The CAR of the alist is the property name, and the CDR is a list
149 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
151 KEYWORD is a string representing a buffer keyword, or nil. Each
152 property defined this way can also be set, during subtree
153 export, through a headline property named after the keyword
154 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
155 property).
156 OPTION is a string that could be found in an #+OPTIONS: line.
157 DEFAULT is the default value for the property.
158 BEHAVIOUR determines how Org should handle multiple keywords for
159 the same property. It is a symbol among:
160 nil Keep old value and discard the new one.
161 t Replace old value with the new one.
162 `space' Concatenate the values, separating them with a space.
163 `newline' Concatenate the values, separating them with
164 a newline.
165 `split' Split values at white spaces, and cons them to the
166 previous list.
168 Values set through KEYWORD and OPTION have precedence over
169 DEFAULT.
171 All these properties should be back-end agnostic. Back-end
172 specific properties are set through `org-export-define-backend'.
173 Properties redefined there have precedence over these.")
175 (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
176 "List of in-buffer keywords that require special treatment.
177 These keywords are not directly associated to a property. The
178 way they are handled must be hard-coded into
179 `org-export--get-inbuffer-options' function.")
181 (defconst org-export-filters-alist
182 '((:filter-bold . org-export-filter-bold-functions)
183 (:filter-babel-call . org-export-filter-babel-call-functions)
184 (:filter-center-block . org-export-filter-center-block-functions)
185 (:filter-clock . org-export-filter-clock-functions)
186 (:filter-code . org-export-filter-code-functions)
187 (:filter-comment . org-export-filter-comment-functions)
188 (:filter-comment-block . org-export-filter-comment-block-functions)
189 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
190 (:filter-drawer . org-export-filter-drawer-functions)
191 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
192 (:filter-entity . org-export-filter-entity-functions)
193 (:filter-example-block . org-export-filter-example-block-functions)
194 (:filter-export-block . org-export-filter-export-block-functions)
195 (:filter-export-snippet . org-export-filter-export-snippet-functions)
196 (:filter-final-output . org-export-filter-final-output-functions)
197 (:filter-fixed-width . org-export-filter-fixed-width-functions)
198 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
199 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
200 (:filter-headline . org-export-filter-headline-functions)
201 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
202 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
203 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
204 (:filter-inlinetask . org-export-filter-inlinetask-functions)
205 (:filter-italic . org-export-filter-italic-functions)
206 (:filter-item . org-export-filter-item-functions)
207 (:filter-keyword . org-export-filter-keyword-functions)
208 (:filter-latex-environment . org-export-filter-latex-environment-functions)
209 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
210 (:filter-line-break . org-export-filter-line-break-functions)
211 (:filter-link . org-export-filter-link-functions)
212 (:filter-macro . org-export-filter-macro-functions)
213 (:filter-node-property . org-export-filter-node-property-functions)
214 (:filter-options . org-export-filter-options-functions)
215 (:filter-paragraph . org-export-filter-paragraph-functions)
216 (:filter-parse-tree . org-export-filter-parse-tree-functions)
217 (:filter-plain-list . org-export-filter-plain-list-functions)
218 (:filter-plain-text . org-export-filter-plain-text-functions)
219 (:filter-planning . org-export-filter-planning-functions)
220 (:filter-property-drawer . org-export-filter-property-drawer-functions)
221 (:filter-quote-block . org-export-filter-quote-block-functions)
222 (:filter-quote-section . org-export-filter-quote-section-functions)
223 (:filter-radio-target . org-export-filter-radio-target-functions)
224 (:filter-section . org-export-filter-section-functions)
225 (:filter-special-block . org-export-filter-special-block-functions)
226 (:filter-src-block . org-export-filter-src-block-functions)
227 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
228 (:filter-strike-through . org-export-filter-strike-through-functions)
229 (:filter-subscript . org-export-filter-subscript-functions)
230 (:filter-superscript . org-export-filter-superscript-functions)
231 (:filter-table . org-export-filter-table-functions)
232 (:filter-table-cell . org-export-filter-table-cell-functions)
233 (:filter-table-row . org-export-filter-table-row-functions)
234 (:filter-target . org-export-filter-target-functions)
235 (:filter-timestamp . org-export-filter-timestamp-functions)
236 (:filter-underline . org-export-filter-underline-functions)
237 (:filter-verbatim . org-export-filter-verbatim-functions)
238 (:filter-verse-block . org-export-filter-verse-block-functions))
239 "Alist between filters properties and initial values.
241 The key of each association is a property name accessible through
242 the communication channel. Its value is a configurable global
243 variable defining initial filters.
245 This list is meant to install user specified filters. Back-end
246 developers may install their own filters using
247 `org-export-define-backend'. Filters defined there will always
248 be prepended to the current list, so they always get applied
249 first.")
251 (defconst org-export-default-inline-image-rule
252 `(("file" .
253 ,(format "\\.%s\\'"
254 (regexp-opt
255 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
256 "xpm" "pbm" "pgm" "ppm") t))))
257 "Default rule for link matching an inline image.
258 This rule applies to links with no description. By default, it
259 will be considered as an inline image if it targets a local file
260 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
261 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
262 See `org-export-inline-image-p' for more information about
263 rules.")
265 (defvar org-export-async-debug nil
266 "Non-nil means asynchronous export process should leave data behind.
268 This data is found in the appropriate \"*Org Export Process*\"
269 buffer, and in files prefixed with \"org-export-process\" and
270 located in `temporary-file-directory'.
272 When non-nil, it will also set `debug-on-error' to a non-nil
273 value in the external process.")
275 (defvar org-export-stack-contents nil
276 "Record asynchronously generated export results and processes.
277 This is an alist: its CAR is the source of the
278 result (destination file or buffer for a finished process,
279 original buffer for a running one) and its CDR is a list
280 containing the back-end used, as a symbol, and either a process
281 or the time at which it finished. It is used to build the menu
282 from `org-export-stack'.")
284 (defvar org-export-registered-backends nil
285 "List of backends currently available in the exporter.
287 A backend is stored as a list where CAR is its name, as a symbol,
288 and CDR is a plist with the following properties:
289 `:filters-alist', `:menu-entry', `:options-alist' and
290 `:translate-alist'.
292 This variable is set with `org-export-define-backend' and
293 `org-export-define-derived-backend' functions.")
295 (defvar org-export-dispatch-last-action nil
296 "Last command called from the dispatcher.
297 The value should be a list. Its CAR is the action, as a symbol,
298 and its CDR is a list of export options.")
300 (defvar org-export-dispatch-last-position (make-marker)
301 "The position where the last export command was created using the dispatcher.
302 This marker will be used with `C-u C-c C-e' to make sure export repetition
303 uses the same subtree if the previous command was restricted to a subtree.")
306 ;;; User-configurable Variables
308 ;; Configuration for the masses.
310 ;; They should never be accessed directly, as their value is to be
311 ;; stored in a property list (cf. `org-export-options-alist').
312 ;; Back-ends will read their value from there instead.
314 (defgroup org-export nil
315 "Options for exporting Org mode files."
316 :tag "Org Export"
317 :group 'org)
319 (defgroup org-export-general nil
320 "General options for export engine."
321 :tag "Org Export General"
322 :group 'org-export)
324 (defcustom org-export-with-archived-trees 'headline
325 "Whether sub-trees with the ARCHIVE tag should be exported.
327 This can have three different values:
328 nil Do not export, pretend this tree is not present.
329 t Do export the entire tree.
330 `headline' Only export the headline, but skip the tree below it.
332 This option can also be set with the #+OPTIONS line,
333 e.g. \"arch:nil\"."
334 :group 'org-export-general
335 :type '(choice
336 (const :tag "Not at all" nil)
337 (const :tag "Headline only" 'headline)
338 (const :tag "Entirely" t)))
340 (defcustom org-export-with-author t
341 "Non-nil means insert author name into the exported file.
342 This option can also be set with the #+OPTIONS line,
343 e.g. \"author:nil\"."
344 :group 'org-export-general
345 :type 'boolean)
347 (defcustom org-export-with-clocks nil
348 "Non-nil means export CLOCK keywords.
349 This option can also be set with the #+OPTIONS line,
350 e.g. \"c:t\"."
351 :group 'org-export-general
352 :type 'boolean)
354 (defcustom org-export-with-creator 'comment
355 "Non-nil means the postamble should contain a creator sentence.
357 The sentence can be set in `org-export-creator-string' and
358 defaults to \"Generated by Org mode XX in Emacs XXX.\".
360 If the value is `comment' insert it as a comment."
361 :group 'org-export-general
362 :type '(choice
363 (const :tag "No creator sentence" nil)
364 (const :tag "Sentence as a comment" 'comment)
365 (const :tag "Insert the sentence" t)))
367 (defcustom org-export-with-date t
368 "Non-nil means insert date in the exported document.
369 This options can also be set with the OPTIONS keyword,
370 e.g. \"date:nil\".")
372 (defcustom org-export-creator-string
373 (format "Generated by Org mode %s in Emacs %s."
374 (if (fboundp 'org-version) (org-version) "(Unknown)")
375 emacs-version)
376 "String to insert at the end of the generated document."
377 :group 'org-export-general
378 :type '(string :tag "Creator string"))
380 (defcustom org-export-with-drawers '(not "LOGBOOK")
381 "Non-nil means export contents of standard drawers.
383 When t, all drawers are exported. This may also be a list of
384 drawer names to export. If that list starts with `not', only
385 drawers with such names will be ignored.
387 This variable doesn't apply to properties drawers.
389 This option can also be set with the #+OPTIONS line,
390 e.g. \"d:nil\"."
391 :group 'org-export-general
392 :version "24.4"
393 :package-version '(Org . "8.0")
394 :type '(choice
395 (const :tag "All drawers" t)
396 (const :tag "None" nil)
397 (repeat :tag "Selected drawers"
398 (string :tag "Drawer name"))
399 (list :tag "Ignored drawers"
400 (const :format "" not)
401 (repeat :tag "Specify names of drawers to ignore during export"
402 :inline t
403 (string :tag "Drawer name")))))
405 (defcustom org-export-with-email nil
406 "Non-nil means insert author email into the exported file.
407 This option can also be set with the #+OPTIONS line,
408 e.g. \"email:t\"."
409 :group 'org-export-general
410 :type 'boolean)
412 (defcustom org-export-with-emphasize t
413 "Non-nil means interpret *word*, /word/, _word_ and +word+.
415 If the export target supports emphasizing text, the word will be
416 typeset in bold, italic, with an underline or strike-through,
417 respectively.
419 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
420 :group 'org-export-general
421 :type 'boolean)
423 (defcustom org-export-exclude-tags '("noexport")
424 "Tags that exclude a tree from export.
426 All trees carrying any of these tags will be excluded from
427 export. This is without condition, so even subtrees inside that
428 carry one of the `org-export-select-tags' will be removed.
430 This option can also be set with the #+EXCLUDE_TAGS: keyword."
431 :group 'org-export-general
432 :type '(repeat (string :tag "Tag")))
434 (defcustom org-export-with-fixed-width t
435 "Non-nil means lines starting with \":\" will be in fixed width font.
437 This can be used to have pre-formatted text, fragments of code
438 etc. For example:
439 : ;; Some Lisp examples
440 : (while (defc cnt)
441 : (ding))
442 will be looking just like this in also HTML. See also the QUOTE
443 keyword. Not all export backends support this.
445 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
446 :group 'org-export-general
447 :type 'boolean)
449 (defcustom org-export-with-footnotes t
450 "Non-nil means Org footnotes should be exported.
451 This option can also be set with the #+OPTIONS line,
452 e.g. \"f:nil\"."
453 :group 'org-export-general
454 :type 'boolean)
456 (defcustom org-export-with-latex t
457 "Non-nil means process LaTeX environments and fragments.
459 This option can also be set with the +OPTIONS line,
460 e.g. \"tex:verbatim\". Allowed values are:
462 nil Ignore math snippets.
463 `verbatim' Keep everything in verbatim.
464 t Allow export of math snippets."
465 :group 'org-export-general
466 :version "24.4"
467 :package-version '(Org . "8.0")
468 :type '(choice
469 (const :tag "Do not process math in any way" nil)
470 (const :tag "Interpret math snippets" t)
471 (const :tag "Leave math verbatim" verbatim)))
473 (defcustom org-export-headline-levels 3
474 "The last level which is still exported as a headline.
476 Inferior levels will produce itemize or enumerate lists when
477 exported.
479 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
480 :group 'org-export-general
481 :type 'integer)
483 (defcustom org-export-default-language "en"
484 "The default language for export and clocktable translations, as a string.
485 This may have an association in
486 `org-clock-clocktable-language-setup'."
487 :group 'org-export-general
488 :type '(string :tag "Language"))
490 (defcustom org-export-preserve-breaks nil
491 "Non-nil means preserve all line breaks when exporting.
492 This option can also be set with the #+OPTIONS line,
493 e.g. \"\\n:t\"."
494 :group 'org-export-general
495 :type 'boolean)
497 (defcustom org-export-with-entities t
498 "Non-nil means interpret entities when exporting.
500 For example, HTML export converts \\alpha to &alpha; and \\AA to
501 &Aring;.
503 For a list of supported names, see the constant `org-entities'
504 and the user option `org-entities-user'.
506 This option can also be set with the #+OPTIONS line,
507 e.g. \"e:nil\"."
508 :group 'org-export-general
509 :type 'boolean)
511 (defcustom org-export-with-inlinetasks t
512 "Non-nil means inlinetasks should be exported.
513 This option can also be set with the #+OPTIONS line,
514 e.g. \"inline:nil\"."
515 :group 'org-export-general
516 :version "24.4"
517 :package-version '(Org . "8.0")
518 :type 'boolean)
520 (defcustom org-export-with-planning nil
521 "Non-nil means include planning info in export.
522 This option can also be set with the #+OPTIONS: line,
523 e.g. \"p:t\"."
524 :group 'org-export-general
525 :version "24.4"
526 :package-version '(Org . "8.0")
527 :type 'boolean)
529 (defcustom org-export-with-priority nil
530 "Non-nil means include priority cookies in export.
531 This option can also be set with the #+OPTIONS line,
532 e.g. \"pri:t\"."
533 :group 'org-export-general
534 :type 'boolean)
536 (defcustom org-export-with-section-numbers t
537 "Non-nil means add section numbers to headlines when exporting.
539 When set to an integer n, numbering will only happen for
540 headlines whose relative level is higher or equal to n.
542 This option can also be set with the #+OPTIONS line,
543 e.g. \"num:t\"."
544 :group 'org-export-general
545 :type 'boolean)
547 (defcustom org-export-select-tags '("export")
548 "Tags that select a tree for export.
550 If any such tag is found in a buffer, all trees that do not carry
551 one of these tags will be ignored during export. Inside trees
552 that are selected like this, you can still deselect a subtree by
553 tagging it with one of the `org-export-exclude-tags'.
555 This option can also be set with the #+SELECT_TAGS: keyword."
556 :group 'org-export-general
557 :type '(repeat (string :tag "Tag")))
559 (defcustom org-export-with-smart-quotes nil
560 "Non-nil means activate smart quotes during export.
561 This option can also be set with the #+OPTIONS: line,
562 e.g. \"':t\"."
563 :group 'org-export-general
564 :version "24.4"
565 :package-version '(Org . "8.0")
566 :type 'boolean)
568 (defcustom org-export-with-special-strings t
569 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
571 When this option is turned on, these strings will be exported as:
573 Org HTML LaTeX UTF-8
574 -----+----------+--------+-------
575 \\- &shy; \\-
576 -- &ndash; -- –
577 --- &mdash; --- —
578 ... &hellip; \\ldots …
580 This option can also be set with the #+OPTIONS line,
581 e.g. \"-:nil\"."
582 :group 'org-export-general
583 :type 'boolean)
585 (defcustom org-export-with-statistics-cookies t
586 "Non-nil means include statistics cookies in export.
587 This option can also be set with the #+OPTIONS: line,
588 e.g. \"stat:nil\""
589 :group 'org-export-general
590 :version "24.4"
591 :package-version '(Org . "8.0")
592 :type 'boolean)
594 (defcustom org-export-with-sub-superscripts t
595 "Non-nil means interpret \"_\" and \"^\" for export.
597 When this option is turned on, you can use TeX-like syntax for
598 sub- and superscripts. Several characters after \"_\" or \"^\"
599 will be considered as a single item - so grouping with {} is
600 normally not needed. For example, the following things will be
601 parsed as single sub- or superscripts.
603 10^24 or 10^tau several digits will be considered 1 item.
604 10^-12 or 10^-tau a leading sign with digits or a word
605 x^2-y^3 will be read as x^2 - y^3, because items are
606 terminated by almost any nonword/nondigit char.
607 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
609 Still, ambiguity is possible - so when in doubt use {} to enclose
610 the sub/superscript. If you set this variable to the symbol
611 `{}', the braces are *required* in order to trigger
612 interpretations as sub/superscript. This can be helpful in
613 documents that need \"_\" frequently in plain text.
615 This option can also be set with the #+OPTIONS line,
616 e.g. \"^:nil\"."
617 :group 'org-export-general
618 :type '(choice
619 (const :tag "Interpret them" t)
620 (const :tag "Curly brackets only" {})
621 (const :tag "Do not interpret them" nil)))
623 (defcustom org-export-with-toc t
624 "Non-nil means create a table of contents in exported files.
626 The TOC contains headlines with levels up
627 to`org-export-headline-levels'. When an integer, include levels
628 up to N in the toc, this may then be different from
629 `org-export-headline-levels', but it will not be allowed to be
630 larger than the number of headline levels. When nil, no table of
631 contents is made.
633 This option can also be set with the #+OPTIONS line,
634 e.g. \"toc:nil\" or \"toc:3\"."
635 :group 'org-export-general
636 :type '(choice
637 (const :tag "No Table of Contents" nil)
638 (const :tag "Full Table of Contents" t)
639 (integer :tag "TOC to level")))
641 (defcustom org-export-with-tables t
642 "If non-nil, lines starting with \"|\" define a table.
643 For example:
645 | Name | Address | Birthday |
646 |-------------+----------+-----------|
647 | Arthur Dent | England | 29.2.2100 |
649 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
650 :group 'org-export-general
651 :type 'boolean)
653 (defcustom org-export-with-tags t
654 "If nil, do not export tags, just remove them from headlines.
656 If this is the symbol `not-in-toc', tags will be removed from
657 table of contents entries, but still be shown in the headlines of
658 the document.
660 This option can also be set with the #+OPTIONS line,
661 e.g. \"tags:nil\"."
662 :group 'org-export-general
663 :type '(choice
664 (const :tag "Off" nil)
665 (const :tag "Not in TOC" not-in-toc)
666 (const :tag "On" t)))
668 (defcustom org-export-with-tasks t
669 "Non-nil means include TODO items for export.
671 This may have the following values:
672 t include tasks independent of state.
673 `todo' include only tasks that are not yet done.
674 `done' include only tasks that are already done.
675 nil ignore all tasks.
676 list of keywords include tasks with these keywords.
678 This option can also be set with the #+OPTIONS line,
679 e.g. \"tasks:nil\"."
680 :group 'org-export-general
681 :type '(choice
682 (const :tag "All tasks" t)
683 (const :tag "No tasks" nil)
684 (const :tag "Not-done tasks" todo)
685 (const :tag "Only done tasks" done)
686 (repeat :tag "Specific TODO keywords"
687 (string :tag "Keyword"))))
689 (defcustom org-export-time-stamp-file t
690 "Non-nil means insert a time stamp into the exported file.
691 The time stamp shows when the file was created.
693 This option can also be set with the #+OPTIONS line,
694 e.g. \"timestamp:nil\"."
695 :group 'org-export-general
696 :type 'boolean)
698 (defcustom org-export-with-timestamps t
699 "Non nil means allow timestamps in export.
701 It can be set to `active', `inactive', t or nil, in order to
702 export, respectively, only active timestamps, only inactive ones,
703 all of them or none.
705 This option can also be set with the #+OPTIONS line, e.g.
706 \"<:nil\"."
707 :group 'org-export-general
708 :type '(choice
709 (const :tag "All timestamps" t)
710 (const :tag "Only active timestamps" active)
711 (const :tag "Only inactive timestamps" inactive)
712 (const :tag "No timestamp" nil)))
714 (defcustom org-export-with-todo-keywords t
715 "Non-nil means include TODO keywords in export.
716 When nil, remove all these keywords from the export."
717 :group 'org-export-general
718 :type 'boolean)
720 (defcustom org-export-allow-bind-keywords nil
721 "Non-nil means BIND keywords can define local variable values.
722 This is a potential security risk, which is why the default value
723 is nil. You can also allow them through local buffer variables."
724 :group 'org-export-general
725 :version "24.4"
726 :package-version '(Org . "8.0")
727 :type 'boolean)
729 (defcustom org-export-snippet-translation-alist nil
730 "Alist between export snippets back-ends and exporter back-ends.
732 This variable allows to provide shortcuts for export snippets.
734 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
735 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
736 HTML code while every other back-end will ignore it."
737 :group 'org-export-general
738 :version "24.4"
739 :package-version '(Org . "8.0")
740 :type '(repeat
741 (cons (string :tag "Shortcut")
742 (string :tag "Back-end"))))
744 (defcustom org-export-coding-system nil
745 "Coding system for the exported file."
746 :group 'org-export-general
747 :version "24.4"
748 :package-version '(Org . "8.0")
749 :type 'coding-system)
751 (defcustom org-export-copy-to-kill-ring t
752 "Non-nil means exported stuff will also be pushed onto the kill ring."
753 :group 'org-export-general
754 :type 'boolean)
756 (defcustom org-export-initial-scope 'buffer
757 "The initial scope when exporting with `org-export-dispatch'.
758 This variable can be either set to `buffer' or `subtree'."
759 :group 'org-export-general
760 :type '(choice
761 (const :tag "Export current buffer" 'buffer)
762 (const :tag "Export current subtree" 'subtree)))
764 (defcustom org-export-show-temporary-export-buffer t
765 "Non-nil means show buffer after exporting to temp buffer.
766 When Org exports to a file, the buffer visiting that file is ever
767 shown, but remains buried. However, when exporting to
768 a temporary buffer, that buffer is popped up in a second window.
769 When this variable is nil, the buffer remains buried also in
770 these cases."
771 :group 'org-export-general
772 :type 'boolean)
774 (defcustom org-export-in-background nil
775 "Non-nil means export and publishing commands will run in background.
776 Results from an asynchronous export are never displayed
777 automatically. But you can retrieve them with \\[org-export-stack]."
778 :group 'org-export-general
779 :version "24.4"
780 :package-version '(Org . "8.0")
781 :type 'boolean)
783 (defcustom org-export-async-init-file user-init-file
784 "File used to initialize external export process.
785 Value must be an absolute file name. It defaults to user's
786 initialization file. Though, a specific configuration makes the
787 process faster and the export more portable."
788 :group 'org-export-general
789 :version "24.4"
790 :package-version '(Org . "8.0")
791 :type '(file :must-match t))
793 (defcustom org-export-invisible-backends nil
794 "List of back-ends that shouldn't appear in the dispatcher.
796 Any back-end belonging to this list or derived from a back-end
797 belonging to it will not appear in the dispatcher menu.
799 Indeed, Org may require some export back-ends without notice. If
800 these modules are never to be used interactively, adding them
801 here will avoid cluttering the dispatcher menu."
802 :group 'org-export-general
803 :version "24.4"
804 :package-version '(Org . "8.0")
805 :type '(repeat (symbol :tag "Back-End")))
807 (defcustom org-export-dispatch-use-expert-ui nil
808 "Non-nil means using a non-intrusive `org-export-dispatch'.
809 In that case, no help buffer is displayed. Though, an indicator
810 for current export scope is added to the prompt (\"b\" when
811 output is restricted to body only, \"s\" when it is restricted to
812 the current subtree, \"v\" when only visible elements are
813 considered for export, \"f\" when publishing functions should be
814 passed the FORCE argument and \"a\" when the export should be
815 asynchronous). Also, \[?] allows to switch back to standard
816 mode."
817 :group 'org-export-general
818 :version "24.4"
819 :package-version '(Org . "8.0")
820 :type 'boolean)
824 ;;; Defining Back-ends
826 ;; `org-export-define-backend' is the standard way to define an export
827 ;; back-end. It allows to specify translators, filters, buffer
828 ;; options and a menu entry. If the new back-end shares translators
829 ;; with another back-end, `org-export-define-derived-backend' may be
830 ;; used instead.
832 ;; Internally, a back-end is stored as a list, of which CAR is the
833 ;; name of the back-end, as a symbol, and CDR a plist. Accessors to
834 ;; properties of a given back-end are: `org-export-backend-filters',
835 ;; `org-export-backend-menu', `org-export-backend-options' and
836 ;; `org-export-backend-translate-table'.
838 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
839 ;; when a given back-end hasn't been registered yet.
841 (defmacro org-export-define-backend (backend translators &rest body)
842 "Define a new back-end BACKEND.
844 TRANSLATORS is an alist between object or element types and
845 functions handling them.
847 These functions should return a string without any trailing
848 space, or nil. They must accept three arguments: the object or
849 element itself, its contents or nil when it isn't recursive and
850 the property list used as a communication channel.
852 Contents, when not nil, are stripped from any global indentation
853 \(although the relative one is preserved). They also always end
854 with a single newline character.
856 If, for a given type, no function is found, that element or
857 object type will simply be ignored, along with any blank line or
858 white space at its end. The same will happen if the function
859 returns the nil value. If that function returns the empty
860 string, the type will be ignored, but the blank lines or white
861 spaces will be kept.
863 In addition to element and object types, one function can be
864 associated to the `template' (or `inner-template') symbol and
865 another one to the `plain-text' symbol.
867 The former returns the final transcoded string, and can be used
868 to add a preamble and a postamble to document's body. It must
869 accept two arguments: the transcoded string and the property list
870 containing export options. A function associated to `template'
871 will not be applied if export has option \"body-only\".
872 A function associated to `inner-template' is always applied.
874 The latter, when defined, is to be called on every text not
875 recognized as an element or an object. It must accept two
876 arguments: the text string and the information channel. It is an
877 appropriate place to protect special chars relative to the
878 back-end.
880 BODY can start with pre-defined keyword arguments. The following
881 keywords are understood:
883 :export-block
885 String, or list of strings, representing block names that
886 will not be parsed. This is used to specify blocks that will
887 contain raw code specific to the back-end. These blocks
888 still have to be handled by the relative `export-block' type
889 translator.
891 :filters-alist
893 Alist between filters and function, or list of functions,
894 specific to the back-end. See `org-export-filters-alist' for
895 a list of all allowed filters. Filters defined here
896 shouldn't make a back-end test, as it may prevent back-ends
897 derived from this one to behave properly.
899 :menu-entry
901 Menu entry for the export dispatcher. It should be a list
902 like:
904 \(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
906 where :
908 KEY is a free character selecting the back-end.
910 DESCRIPTION-OR-ORDINAL is either a string or a number.
912 If it is a string, is will be used to name the back-end in
913 its menu entry. If it is a number, the following menu will
914 be displayed as a sub-menu of the back-end with the same
915 KEY. Also, the number will be used to determine in which
916 order such sub-menus will appear (lowest first).
918 ACTION-OR-MENU is either a function or an alist.
920 If it is an action, it will be called with four
921 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
922 BODY-ONLY. See `org-export-as' for further explanations on
923 some of them.
925 If it is an alist, associations should follow the
926 pattern:
928 \(KEY DESCRIPTION ACTION)
930 where KEY, DESCRIPTION and ACTION are described above.
932 Valid values include:
934 \(?m \"My Special Back-end\" my-special-export-function)
938 \(?l \"Export to LaTeX\"
939 \(?p \"As PDF file\" org-latex-export-to-pdf)
940 \(?o \"As PDF file and open\"
941 \(lambda (a s v b)
942 \(if a (org-latex-export-to-pdf t s v b)
943 \(org-open-file
944 \(org-latex-export-to-pdf nil s v b)))))))
946 or the following, which will be added to the previous
947 sub-menu,
949 \(?l 1
950 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
951 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
953 :options-alist
955 Alist between back-end specific properties introduced in
956 communication channel and how their value are acquired. See
957 `org-export-options-alist' for more information about
958 structure of the values."
959 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
960 (indent 1))
961 (let (export-block filters menu-entry options contents)
962 (while (keywordp (car body))
963 (case (pop body)
964 (:export-block (let ((names (pop body)))
965 (setq export-block
966 (if (consp names) (mapcar 'upcase names)
967 (list (upcase names))))))
968 (:filters-alist (setq filters (pop body)))
969 (:menu-entry (setq menu-entry (pop body)))
970 (:options-alist (setq options (pop body)))
971 (t (pop body))))
972 (setq contents (append (list :translate-alist translators)
973 (and filters (list :filters-alist filters))
974 (and options (list :options-alist options))
975 (and menu-entry (list :menu-entry menu-entry))))
976 `(progn
977 ;; Register back-end.
978 (let ((registeredp (assq ',backend org-export-registered-backends)))
979 (if registeredp (setcdr registeredp ',contents)
980 (push (cons ',backend ',contents) org-export-registered-backends)))
981 ;; Tell parser to not parse EXPORT-BLOCK blocks.
982 ,(when export-block
983 `(mapc
984 (lambda (name)
985 (add-to-list 'org-element-block-name-alist
986 `(,name . org-element-export-block-parser)))
987 ',export-block))
988 ;; Splice in the body, if any.
989 ,@body)))
991 (defmacro org-export-define-derived-backend (child parent &rest body)
992 "Create a new back-end as a variant of an existing one.
994 CHILD is the name of the derived back-end. PARENT is the name of
995 the parent back-end.
997 BODY can start with pre-defined keyword arguments. The following
998 keywords are understood:
1000 :export-block
1002 String, or list of strings, representing block names that
1003 will not be parsed. This is used to specify blocks that will
1004 contain raw code specific to the back-end. These blocks
1005 still have to be handled by the relative `export-block' type
1006 translator.
1008 :filters-alist
1010 Alist of filters that will overwrite or complete filters
1011 defined in PARENT back-end. See `org-export-filters-alist'
1012 for a list of allowed filters.
1014 :menu-entry
1016 Menu entry for the export dispatcher. See
1017 `org-export-define-backend' for more information about the
1018 expected value.
1020 :options-alist
1022 Alist of back-end specific properties that will overwrite or
1023 complete those defined in PARENT back-end. Refer to
1024 `org-export-options-alist' for more information about
1025 structure of the values.
1027 :translate-alist
1029 Alist of element and object types and transcoders that will
1030 overwrite or complete transcode table from PARENT back-end.
1031 Refer to `org-export-define-backend' for detailed information
1032 about transcoders.
1034 As an example, here is how one could define \"my-latex\" back-end
1035 as a variant of `latex' back-end with a custom template function:
1037 \(org-export-define-derived-backend my-latex latex
1038 :translate-alist ((template . my-latex-template-fun)))
1040 The back-end could then be called with, for example:
1042 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1043 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
1044 (indent 2))
1045 (let (export-block filters menu-entry options translators contents)
1046 (while (keywordp (car body))
1047 (case (pop body)
1048 (:export-block (let ((names (pop body)))
1049 (setq export-block
1050 (if (consp names) (mapcar 'upcase names)
1051 (list (upcase names))))))
1052 (:filters-alist (setq filters (pop body)))
1053 (:menu-entry (setq menu-entry (pop body)))
1054 (:options-alist (setq options (pop body)))
1055 (:translate-alist (setq translators (pop body)))
1056 (t (pop body))))
1057 (setq contents (append
1058 (list :parent parent)
1059 (let ((p-table (org-export-backend-translate-table parent)))
1060 (list :translate-alist (append translators p-table)))
1061 (let ((p-filters (org-export-backend-filters parent)))
1062 (list :filters-alist (append filters p-filters)))
1063 (let ((p-options (org-export-backend-options parent)))
1064 (list :options-alist (append options p-options)))
1065 (and menu-entry (list :menu-entry menu-entry))))
1066 `(progn
1067 (org-export-barf-if-invalid-backend ',parent)
1068 ;; Register back-end.
1069 (let ((registeredp (assq ',child org-export-registered-backends)))
1070 (if registeredp (setcdr registeredp ',contents)
1071 (push (cons ',child ',contents) org-export-registered-backends)))
1072 ;; Tell parser to not parse EXPORT-BLOCK blocks.
1073 ,(when export-block
1074 `(mapc
1075 (lambda (name)
1076 (add-to-list 'org-element-block-name-alist
1077 `(,name . org-element-export-block-parser)))
1078 ',export-block))
1079 ;; Splice in the body, if any.
1080 ,@body)))
1082 (defun org-export-backend-parent (backend)
1083 "Return back-end from which BACKEND is derived, or nil."
1084 (plist-get (cdr (assq backend org-export-registered-backends)) :parent))
1086 (defun org-export-backend-filters (backend)
1087 "Return filters for BACKEND."
1088 (plist-get (cdr (assq backend org-export-registered-backends))
1089 :filters-alist))
1091 (defun org-export-backend-menu (backend)
1092 "Return menu entry for BACKEND."
1093 (plist-get (cdr (assq backend org-export-registered-backends))
1094 :menu-entry))
1096 (defun org-export-backend-options (backend)
1097 "Return export options for BACKEND."
1098 (plist-get (cdr (assq backend org-export-registered-backends))
1099 :options-alist))
1101 (defun org-export-backend-translate-table (backend)
1102 "Return translate table for BACKEND."
1103 (plist-get (cdr (assq backend org-export-registered-backends))
1104 :translate-alist))
1106 (defun org-export-barf-if-invalid-backend (backend)
1107 "Signal an error if BACKEND isn't defined."
1108 (unless (org-export-backend-translate-table backend)
1109 (error "Unknown \"%s\" back-end: Aborting export" backend)))
1111 (defun org-export-derived-backend-p (backend &rest backends)
1112 "Non-nil if BACKEND is derived from one of BACKENDS."
1113 (let ((parent backend))
1114 (while (and (not (memq parent backends))
1115 (setq parent (org-export-backend-parent parent))))
1116 parent))
1120 ;;; The Communication Channel
1122 ;; During export process, every function has access to a number of
1123 ;; properties. They are of two types:
1125 ;; 1. Environment options are collected once at the very beginning of
1126 ;; the process, out of the original buffer and configuration.
1127 ;; Collecting them is handled by `org-export-get-environment'
1128 ;; function.
1130 ;; Most environment options are defined through the
1131 ;; `org-export-options-alist' variable.
1133 ;; 2. Tree properties are extracted directly from the parsed tree,
1134 ;; just before export, by `org-export-collect-tree-properties'.
1136 ;; Here is the full list of properties available during transcode
1137 ;; process, with their category and their value type.
1139 ;; + `:author' :: Author's name.
1140 ;; - category :: option
1141 ;; - type :: string
1143 ;; + `:back-end' :: Current back-end used for transcoding.
1144 ;; - category :: tree
1145 ;; - type :: symbol
1147 ;; + `:creator' :: String to write as creation information.
1148 ;; - category :: option
1149 ;; - type :: string
1151 ;; + `:date' :: String to use as date.
1152 ;; - category :: option
1153 ;; - type :: string
1155 ;; + `:description' :: Description text for the current data.
1156 ;; - category :: option
1157 ;; - type :: string
1159 ;; + `:email' :: Author's email.
1160 ;; - category :: option
1161 ;; - type :: string
1163 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1164 ;; process.
1165 ;; - category :: option
1166 ;; - type :: list of strings
1168 ;; + `:export-options' :: List of export options available for current
1169 ;; process.
1170 ;; - category :: none
1171 ;; - type :: list of symbols, among `subtree', `body-only' and
1172 ;; `visible-only'.
1174 ;; + `:exported-data' :: Hash table used for memoizing
1175 ;; `org-export-data'.
1176 ;; - category :: tree
1177 ;; - type :: hash table
1179 ;; + `:filetags' :: List of global tags for buffer. Used by
1180 ;; `org-export-get-tags' to get tags with inheritance.
1181 ;; - category :: option
1182 ;; - type :: list of strings
1184 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1185 ;; their definition, as parsed data. Only non-inlined footnotes
1186 ;; are represented in this alist. Also, every definition isn't
1187 ;; guaranteed to be referenced in the parse tree. The purpose of
1188 ;; this property is to preserve definitions from oblivion
1189 ;; (i.e. when the parse tree comes from a part of the original
1190 ;; buffer), it isn't meant for direct use in a back-end. To
1191 ;; retrieve a definition relative to a reference, use
1192 ;; `org-export-get-footnote-definition' instead.
1193 ;; - category :: option
1194 ;; - type :: alist (STRING . LIST)
1196 ;; + `:headline-levels' :: Maximum level being exported as an
1197 ;; headline. Comparison is done with the relative level of
1198 ;; headlines in the parse tree, not necessarily with their
1199 ;; actual level.
1200 ;; - category :: option
1201 ;; - type :: integer
1203 ;; + `:headline-offset' :: Difference between relative and real level
1204 ;; of headlines in the parse tree. For example, a value of -1
1205 ;; means a level 2 headline should be considered as level
1206 ;; 1 (cf. `org-export-get-relative-level').
1207 ;; - category :: tree
1208 ;; - type :: integer
1210 ;; + `:headline-numbering' :: Alist between headlines and their
1211 ;; numbering, as a list of numbers
1212 ;; (cf. `org-export-get-headline-number').
1213 ;; - category :: tree
1214 ;; - type :: alist (INTEGER . LIST)
1216 ;; + `:id-alist' :: Alist between ID strings and destination file's
1217 ;; path, relative to current directory. It is used by
1218 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1219 ;; external file.
1220 ;; - category :: option
1221 ;; - type :: alist (STRING . STRING)
1223 ;; + `:ignore-list' :: List of elements and objects that should be
1224 ;; ignored during export.
1225 ;; - category :: tree
1226 ;; - type :: list of elements and objects
1228 ;; + `:input-file' :: Full path to input file, if any.
1229 ;; - category :: option
1230 ;; - type :: string or nil
1232 ;; + `:keywords' :: List of keywords attached to data.
1233 ;; - category :: option
1234 ;; - type :: string
1236 ;; + `:language' :: Default language used for translations.
1237 ;; - category :: option
1238 ;; - type :: string
1240 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1241 ;; transcoding.
1242 ;; - category :: option
1243 ;; - type :: list (as returned by `org-element-parse-buffer')
1245 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1246 ;; all line breaks.
1247 ;; - category :: option
1248 ;; - type :: symbol (nil, t)
1250 ;; + `:section-numbers' :: Non-nil means transcoding should add
1251 ;; section numbers to headlines.
1252 ;; - category :: option
1253 ;; - type :: symbol (nil, t)
1255 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1256 ;; in transcoding. When such a tag is present, subtrees without
1257 ;; it are de facto excluded from the process. See
1258 ;; `use-select-tags'.
1259 ;; - category :: option
1260 ;; - type :: list of strings
1262 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1263 ;; a time stamp in the output.
1264 ;; - category :: option
1265 ;; - type :: symbol (nil, t)
1267 ;; + `:translate-alist' :: Alist between element and object types and
1268 ;; transcoding functions relative to the current back-end.
1269 ;; Special keys `inner-template', `template' and `plain-text' are
1270 ;; also possible.
1271 ;; - category :: option
1272 ;; - type :: alist (SYMBOL . FUNCTION)
1274 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1275 ;; also be transcoded. If it is set to the `headline' symbol,
1276 ;; only the archived headline's name is retained.
1277 ;; - category :: option
1278 ;; - type :: symbol (nil, t, `headline')
1280 ;; + `:with-author' :: Non-nil means author's name should be included
1281 ;; in the output.
1282 ;; - category :: option
1283 ;; - type :: symbol (nil, t)
1285 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1286 ;; - category :: option
1287 ;; - type :: symbol (nil, t)
1289 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1290 ;; inserted at the end of the transcoded string. If the value
1291 ;; is `comment', it should be commented.
1292 ;; - category :: option
1293 ;; - type :: symbol (`comment', nil, t)
1295 ;; + `:with-date' :: Non-nil means output should contain a date.
1296 ;; - category :: option
1297 ;; - type :. symbol (nil, t)
1299 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1300 ;; its value is a list of names, only drawers with such names
1301 ;; will be transcoded. If that list starts with `not', drawer
1302 ;; with these names will be skipped.
1303 ;; - category :: option
1304 ;; - type :: symbol (nil, t) or list of strings
1306 ;; + `:with-email' :: Non-nil means output should contain author's
1307 ;; email.
1308 ;; - category :: option
1309 ;; - type :: symbol (nil, t)
1311 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1312 ;; interpreted.
1313 ;; - category :: option
1314 ;; - type :: symbol (nil, t)
1316 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1317 ;; strings starting with a colon as a fixed-with (verbatim) area.
1318 ;; - category :: option
1319 ;; - type :: symbol (nil, t)
1321 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1322 ;; footnotes.
1323 ;; - category :: option
1324 ;; - type :: symbol (nil, t)
1326 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1327 ;; `latex-fragment' objects should appear in export output. When
1328 ;; this property is set to `verbatim', they will be left as-is.
1329 ;; - category :: option
1330 ;; - type :: symbol (`verbatim', nil, t)
1332 ;; + `:with-plannings' :: Non-nil means transcoding should include
1333 ;; planning info.
1334 ;; - category :: option
1335 ;; - type :: symbol (nil, t)
1337 ;; + `:with-priority' :: Non-nil means transcoding should include
1338 ;; priority cookies.
1339 ;; - category :: option
1340 ;; - type :: symbol (nil, t)
1342 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1343 ;; plain text.
1344 ;; - category :: option
1345 ;; - type :: symbol (nil, t)
1347 ;; + `:with-special-strings' :: Non-nil means transcoding should
1348 ;; interpret special strings in plain text.
1349 ;; - category :: option
1350 ;; - type :: symbol (nil, t)
1352 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1353 ;; interpret subscript and superscript. With a value of "{}",
1354 ;; only interpret those using curly brackets.
1355 ;; - category :: option
1356 ;; - type :: symbol (nil, {}, t)
1358 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1359 ;; tables.
1360 ;; - category :: option
1361 ;; - type :: symbol (nil, t)
1363 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1364 ;; headlines. A `not-in-toc' value will remove them from the
1365 ;; table of contents, if any, nonetheless.
1366 ;; - category :: option
1367 ;; - type :: symbol (nil, t, `not-in-toc')
1369 ;; + `:with-tasks' :: Non-nil means transcoding should include
1370 ;; headlines with a TODO keyword. A `todo' value will only
1371 ;; include headlines with a todo type keyword while a `done'
1372 ;; value will do the contrary. If a list of strings is provided,
1373 ;; only tasks with keywords belonging to that list will be kept.
1374 ;; - category :: option
1375 ;; - type :: symbol (t, todo, done, nil) or list of strings
1377 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1378 ;; time stamps. Special value `active' (resp. `inactive') ask to
1379 ;; export only active (resp. inactive) timestamps. Otherwise,
1380 ;; completely remove them.
1381 ;; - category :: option
1382 ;; - type :: symbol: (`active', `inactive', t, nil)
1384 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1385 ;; added to the output. An integer value limits its depth.
1386 ;; - category :: option
1387 ;; - type :: symbol (nil, t or integer)
1389 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1390 ;; include TODO keywords.
1391 ;; - category :: option
1392 ;; - type :: symbol (nil, t)
1395 ;;;; Environment Options
1397 ;; Environment options encompass all parameters defined outside the
1398 ;; scope of the parsed data. They come from five sources, in
1399 ;; increasing precedence order:
1401 ;; - Global variables,
1402 ;; - Buffer's attributes,
1403 ;; - Options keyword symbols,
1404 ;; - Buffer keywords,
1405 ;; - Subtree properties.
1407 ;; The central internal function with regards to environment options
1408 ;; is `org-export-get-environment'. It updates global variables with
1409 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1410 ;; the different sources.
1412 ;; The internal functions doing the retrieval are:
1413 ;; `org-export--get-global-options',
1414 ;; `org-export--get-buffer-attributes',
1415 ;; `org-export--parse-option-keyword',
1416 ;; `org-export--get-subtree-options' and
1417 ;; `org-export--get-inbuffer-options'
1419 ;; Also, `org-export--install-letbind-maybe' takes care of the part
1420 ;; relative to "#+BIND:" keywords.
1422 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1423 "Collect export options from the current buffer.
1425 Optional argument BACKEND is a symbol specifying which back-end
1426 specific options to read, if any.
1428 When optional argument SUBTREEP is non-nil, assume the export is
1429 done against the current sub-tree.
1431 Third optional argument EXT-PLIST is a property list with
1432 external parameters overriding Org default settings, but still
1433 inferior to file-local settings."
1434 ;; First install #+BIND variables.
1435 (org-export--install-letbind-maybe)
1436 ;; Get and prioritize export options...
1437 (org-combine-plists
1438 ;; ... from global variables...
1439 (org-export--get-global-options backend)
1440 ;; ... from an external property list...
1441 ext-plist
1442 ;; ... from in-buffer settings...
1443 (org-export--get-inbuffer-options backend)
1444 ;; ... and from subtree, when appropriate.
1445 (and subtreep (org-export--get-subtree-options backend))
1446 ;; Eventually add misc. properties.
1447 (list
1448 :back-end
1449 backend
1450 :translate-alist
1451 (org-export-backend-translate-table backend)
1452 :footnote-definition-alist
1453 ;; Footnotes definitions must be collected in the original
1454 ;; buffer, as there's no insurance that they will still be in
1455 ;; the parse tree, due to possible narrowing.
1456 (let (alist)
1457 (org-with-wide-buffer
1458 (goto-char (point-min))
1459 (while (re-search-forward org-footnote-definition-re nil t)
1460 (let ((def (save-match-data (org-element-at-point))))
1461 (when (eq (org-element-type def) 'footnote-definition)
1462 (push
1463 (cons (org-element-property :label def)
1464 (let ((cbeg (org-element-property :contents-begin def)))
1465 (when cbeg
1466 (org-element--parse-elements
1467 cbeg (org-element-property :contents-end def)
1468 nil nil nil nil (list 'org-data nil)))))
1469 alist))))
1470 alist))
1471 :id-alist
1472 ;; Collect id references.
1473 (let (alist)
1474 (org-with-wide-buffer
1475 (goto-char (point-min))
1476 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1477 (let ((link (org-element-context)))
1478 (when (eq (org-element-type link) 'link)
1479 (let* ((id (org-element-property :path link))
1480 (file (org-id-find-id-file id)))
1481 (when file
1482 (push (cons id (file-relative-name file)) alist)))))))
1483 alist))))
1485 (defun org-export--parse-option-keyword (options &optional backend)
1486 "Parse an OPTIONS line and return values as a plist.
1487 Optional argument BACKEND is a symbol specifying which back-end
1488 specific items to read, if any."
1489 (let* ((all
1490 ;; Priority is given to back-end specific options.
1491 (append (and backend (org-export-backend-options backend))
1492 org-export-options-alist))
1493 plist)
1494 (dolist (option all)
1495 (let ((property (car option))
1496 (item (nth 2 option)))
1497 (when (and item
1498 (not (plist-member plist property))
1499 (string-match (concat "\\(\\`\\|[ \t]\\)"
1500 (regexp-quote item)
1501 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1502 options))
1503 (setq plist (plist-put plist
1504 property
1505 (car (read-from-string
1506 (match-string 2 options))))))))
1507 plist))
1509 (defun org-export--get-subtree-options (&optional backend)
1510 "Get export options in subtree at point.
1511 Optional argument BACKEND is a symbol specifying back-end used
1512 for export. Return options as a plist."
1513 ;; For each buffer keyword, create a headline property setting the
1514 ;; same property in communication channel. The name for the property
1515 ;; is the keyword with "EXPORT_" appended to it.
1516 (org-with-wide-buffer
1517 (let (prop plist)
1518 ;; Make sure point is at an heading.
1519 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
1520 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1521 ;; title as its fallback value.
1522 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1523 (progn (looking-at org-todo-line-regexp)
1524 (org-match-string-no-properties 3))))
1525 (setq plist
1526 (plist-put
1527 plist :title
1528 (org-element-parse-secondary-string
1529 prop (org-element-restriction 'keyword)))))
1530 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1531 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1532 (setq plist
1533 (nconc plist (org-export--parse-option-keyword prop backend))))
1534 ;; Handle other keywords. TITLE keyword is excluded as it has
1535 ;; been handled already.
1536 (let ((seen '("TITLE")))
1537 (mapc
1538 (lambda (option)
1539 (let ((property (car option))
1540 (keyword (nth 1 option)))
1541 (when (and keyword (not (member keyword seen)))
1542 (let* ((subtree-prop (concat "EXPORT_" keyword))
1543 ;; Export properties are not case-sensitive.
1544 (value (let ((case-fold-search t))
1545 (org-entry-get (point) subtree-prop))))
1546 (push keyword seen)
1547 (when (and value (not (plist-member plist property)))
1548 (setq plist
1549 (plist-put
1550 plist
1551 property
1552 (cond
1553 ;; Parse VALUE if required.
1554 ((member keyword org-element-document-properties)
1555 (org-element-parse-secondary-string
1556 value (org-element-restriction 'keyword)))
1557 ;; If BEHAVIOUR is `split' expected value is
1558 ;; a list of strings, not a string.
1559 ((eq (nth 4 option) 'split) (org-split-string value))
1560 (t value)))))))))
1561 ;; Look for both general keywords and back-end specific
1562 ;; options, with priority given to the latter.
1563 (append (and backend (org-export-backend-options backend))
1564 org-export-options-alist)))
1565 ;; Return value.
1566 plist)))
1568 (defun org-export--get-inbuffer-options (&optional backend)
1569 "Return current buffer export options, as a plist.
1571 Optional argument BACKEND, when non-nil, is a symbol specifying
1572 which back-end specific options should also be read in the
1573 process.
1575 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1576 (let* (plist
1577 get-options ; For byte-compiler.
1578 (case-fold-search t)
1579 (options (append
1580 ;; Priority is given to back-end specific options.
1581 (and backend (org-export-backend-options backend))
1582 org-export-options-alist))
1583 (regexp (format "^[ \t]*#\\+%s:"
1584 (regexp-opt (nconc (delq nil (mapcar 'cadr options))
1585 org-export-special-keywords))))
1586 (find-opt
1587 (lambda (keyword)
1588 ;; Return property name associated to KEYWORD.
1589 (catch 'exit
1590 (mapc (lambda (option)
1591 (when (equal (nth 1 option) keyword)
1592 (throw 'exit (car option))))
1593 options))))
1594 (get-options
1595 (lambda (&optional files plist)
1596 ;; Recursively read keywords in buffer. FILES is a list
1597 ;; of files read so far. PLIST is the current property
1598 ;; list obtained.
1599 (org-with-wide-buffer
1600 (goto-char (point-min))
1601 (while (re-search-forward regexp nil t)
1602 (let ((element (org-element-at-point)))
1603 (when (eq (org-element-type element) 'keyword)
1604 (let ((key (org-element-property :key element))
1605 (val (org-element-property :value element)))
1606 (cond
1607 ;; Options in `org-export-special-keywords'.
1608 ((equal key "SETUPFILE")
1609 (let ((file (expand-file-name
1610 (org-remove-double-quotes (org-trim val)))))
1611 ;; Avoid circular dependencies.
1612 (unless (member file files)
1613 (with-temp-buffer
1614 (insert (org-file-contents file 'noerror))
1615 (org-mode)
1616 (setq plist (funcall get-options
1617 (cons file files) plist))))))
1618 ((equal key "OPTIONS")
1619 (setq plist
1620 (org-combine-plists
1621 plist
1622 (org-export--parse-option-keyword val backend))))
1623 ((equal key "FILETAGS")
1624 (setq plist
1625 (org-combine-plists
1626 plist
1627 (list :filetags
1628 (org-uniquify
1629 (append (org-split-string val ":")
1630 (plist-get plist :filetags)))))))
1632 ;; Options in `org-export-options-alist'.
1633 (let* ((prop (funcall find-opt key))
1634 (behaviour (nth 4 (assq prop options))))
1635 (setq plist
1636 (plist-put
1637 plist prop
1638 ;; Handle value depending on specified
1639 ;; BEHAVIOUR.
1640 (case behaviour
1641 (space
1642 (if (not (plist-get plist prop))
1643 (org-trim val)
1644 (concat (plist-get plist prop)
1646 (org-trim val))))
1647 (newline
1648 (org-trim (concat (plist-get plist prop)
1649 "\n"
1650 (org-trim val))))
1651 (split `(,@(plist-get plist prop)
1652 ,@(org-split-string val)))
1653 ('t val)
1654 (otherwise
1655 (if (not (plist-member plist prop)) val
1656 (plist-get plist prop)))))))))))))
1657 ;; Return final value.
1658 plist))))
1659 ;; Read options in the current buffer.
1660 (setq plist (funcall get-options buffer-file-name nil))
1661 ;; Parse keywords specified in `org-element-document-properties'.
1662 (mapc (lambda (keyword)
1663 ;; Find the property associated to the keyword.
1664 (let* ((prop (funcall find-opt keyword))
1665 (value (and prop (plist-get plist prop))))
1666 (when (stringp value)
1667 (setq plist
1668 (plist-put plist prop
1669 (org-element-parse-secondary-string
1670 value (org-element-restriction 'keyword)))))))
1671 org-element-document-properties)
1672 ;; Return value.
1673 plist))
1675 (defun org-export--get-buffer-attributes ()
1676 "Return properties related to buffer attributes, as a plist."
1677 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1678 (list
1679 ;; Store full path of input file name, or nil. For internal use.
1680 :input-file visited-file
1681 :title (or (and visited-file
1682 (file-name-sans-extension
1683 (file-name-nondirectory visited-file)))
1684 (buffer-name (buffer-base-buffer))))))
1686 (defun org-export--get-global-options (&optional backend)
1687 "Return global export options as a plist.
1689 Optional argument BACKEND, if non-nil, is a symbol specifying
1690 which back-end specific export options should also be read in the
1691 process."
1692 (let ((all
1693 ;; Priority is given to back-end specific options.
1694 (append (and backend (org-export-backend-options backend))
1695 org-export-options-alist))
1696 plist)
1697 (mapc
1698 (lambda (cell)
1699 (let ((prop (car cell)))
1700 (unless (plist-member plist prop)
1701 (setq plist
1702 (plist-put
1703 plist
1704 prop
1705 ;; Eval default value provided. If keyword is a member
1706 ;; of `org-element-document-properties', parse it as
1707 ;; a secondary string before storing it.
1708 (let ((value (eval (nth 3 cell))))
1709 (if (not (stringp value)) value
1710 (let ((keyword (nth 1 cell)))
1711 (if (not (member keyword org-element-document-properties))
1712 value
1713 (org-element-parse-secondary-string
1714 value (org-element-restriction 'keyword)))))))))))
1715 all)
1716 ;; Return value.
1717 plist))
1719 (defun org-export--install-letbind-maybe ()
1720 "Install the values from #+BIND lines as local variables.
1721 Variables must be installed before in-buffer options are
1722 retrieved."
1723 (when org-export-allow-bind-keywords
1724 (let ((case-fold-search t) letbind)
1725 (org-with-wide-buffer
1726 (goto-char (point-min))
1727 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1728 (let* ((element (org-element-at-point))
1729 (value (org-element-property :value element)))
1730 (when (and (eq (org-element-type element) 'keyword)
1731 (not (equal value "")))
1732 (push (read (format "(%s)" value)) letbind)))))
1733 (dolist (pair (nreverse letbind))
1734 (org-set-local (car pair) (nth 1 pair))))))
1737 ;;;; Tree Properties
1739 ;; Tree properties are information extracted from parse tree. They
1740 ;; are initialized at the beginning of the transcoding process by
1741 ;; `org-export-collect-tree-properties'.
1743 ;; Dedicated functions focus on computing the value of specific tree
1744 ;; properties during initialization. Thus,
1745 ;; `org-export--populate-ignore-list' lists elements and objects that
1746 ;; should be skipped during export, `org-export--get-min-level' gets
1747 ;; the minimal exportable level, used as a basis to compute relative
1748 ;; level for headlines. Eventually
1749 ;; `org-export--collect-headline-numbering' builds an alist between
1750 ;; headlines and their numbering.
1752 (defun org-export-collect-tree-properties (data info)
1753 "Extract tree properties from parse tree.
1755 DATA is the parse tree from which information is retrieved. INFO
1756 is a list holding export options.
1758 Following tree properties are set or updated:
1760 `:exported-data' Hash table used to memoize results from
1761 `org-export-data'.
1763 `:footnote-definition-alist' List of footnotes definitions in
1764 original buffer and current parse tree.
1766 `:headline-offset' Offset between true level of headlines and
1767 local level. An offset of -1 means a headline
1768 of level 2 should be considered as a level
1769 1 headline in the context.
1771 `:headline-numbering' Alist of all headlines as key an the
1772 associated numbering as value.
1774 `:ignore-list' List of elements that should be ignored during
1775 export.
1777 Return updated plist."
1778 ;; Install the parse tree in the communication channel, in order to
1779 ;; use `org-export-get-genealogy' and al.
1780 (setq info (plist-put info :parse-tree data))
1781 ;; Get the list of elements and objects to ignore, and put it into
1782 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1783 ;; been done during parse tree filtering.
1784 (setq info
1785 (plist-put info
1786 :ignore-list
1787 (append (org-export--populate-ignore-list data info)
1788 (plist-get info :ignore-list))))
1789 ;; Compute `:headline-offset' in order to be able to use
1790 ;; `org-export-get-relative-level'.
1791 (setq info
1792 (plist-put info
1793 :headline-offset
1794 (- 1 (org-export--get-min-level data info))))
1795 ;; Update footnotes definitions list with definitions in parse tree.
1796 ;; This is required since buffer expansion might have modified
1797 ;; boundaries of footnote definitions contained in the parse tree.
1798 ;; This way, definitions in `footnote-definition-alist' are bound to
1799 ;; match those in the parse tree.
1800 (let ((defs (plist-get info :footnote-definition-alist)))
1801 (org-element-map data 'footnote-definition
1802 (lambda (fn)
1803 (push (cons (org-element-property :label fn)
1804 `(org-data nil ,@(org-element-contents fn)))
1805 defs)))
1806 (setq info (plist-put info :footnote-definition-alist defs)))
1807 ;; Properties order doesn't matter: get the rest of the tree
1808 ;; properties.
1809 (nconc
1810 `(:headline-numbering ,(org-export--collect-headline-numbering data info)
1811 :exported-data ,(make-hash-table :test 'eq :size 4001))
1812 info))
1814 (defun org-export--get-min-level (data options)
1815 "Return minimum exportable headline's level in DATA.
1816 DATA is parsed tree as returned by `org-element-parse-buffer'.
1817 OPTIONS is a plist holding export options."
1818 (catch 'exit
1819 (let ((min-level 10000))
1820 (mapc
1821 (lambda (blob)
1822 (when (and (eq (org-element-type blob) 'headline)
1823 (not (org-element-property :footnote-section-p blob))
1824 (not (memq blob (plist-get options :ignore-list))))
1825 (setq min-level (min (org-element-property :level blob) min-level)))
1826 (when (= min-level 1) (throw 'exit 1)))
1827 (org-element-contents data))
1828 ;; If no headline was found, for the sake of consistency, set
1829 ;; minimum level to 1 nonetheless.
1830 (if (= min-level 10000) 1 min-level))))
1832 (defun org-export--collect-headline-numbering (data options)
1833 "Return numbering of all exportable headlines in a parse tree.
1835 DATA is the parse tree. OPTIONS is the plist holding export
1836 options.
1838 Return an alist whose key is a headline and value is its
1839 associated numbering \(in the shape of a list of numbers\) or nil
1840 for a footnotes section."
1841 (let ((numbering (make-vector org-export-max-depth 0)))
1842 (org-element-map data 'headline
1843 (lambda (headline)
1844 (unless (org-element-property :footnote-section-p headline)
1845 (let ((relative-level
1846 (1- (org-export-get-relative-level headline options))))
1847 (cons
1848 headline
1849 (loop for n across numbering
1850 for idx from 0 to org-export-max-depth
1851 when (< idx relative-level) collect n
1852 when (= idx relative-level) collect (aset numbering idx (1+ n))
1853 when (> idx relative-level) do (aset numbering idx 0))))))
1854 options)))
1856 (defun org-export--populate-ignore-list (data options)
1857 "Return list of elements and objects to ignore during export.
1858 DATA is the parse tree to traverse. OPTIONS is the plist holding
1859 export options."
1860 (let* (ignore
1861 walk-data
1862 ;; First find trees containing a select tag, if any.
1863 (selected (org-export--selected-trees data options))
1864 (walk-data
1865 (lambda (data)
1866 ;; Collect ignored elements or objects into IGNORE-LIST.
1867 (let ((type (org-element-type data)))
1868 (if (org-export--skip-p data options selected) (push data ignore)
1869 (if (and (eq type 'headline)
1870 (eq (plist-get options :with-archived-trees) 'headline)
1871 (org-element-property :archivedp data))
1872 ;; If headline is archived but tree below has
1873 ;; to be skipped, add it to ignore list.
1874 (mapc (lambda (e) (push e ignore))
1875 (org-element-contents data))
1876 ;; Move into secondary string, if any.
1877 (let ((sec-prop
1878 (cdr (assq type org-element-secondary-value-alist))))
1879 (when sec-prop
1880 (mapc walk-data (org-element-property sec-prop data))))
1881 ;; Move into recursive objects/elements.
1882 (mapc walk-data (org-element-contents data))))))))
1883 ;; Main call.
1884 (funcall walk-data data)
1885 ;; Return value.
1886 ignore))
1888 (defun org-export--selected-trees (data info)
1889 "Return list of headlines and inlinetasks with a select tag in their tree.
1890 DATA is parsed data as returned by `org-element-parse-buffer'.
1891 INFO is a plist holding export options."
1892 (let* (selected-trees
1893 walk-data ; For byte-compiler.
1894 (walk-data
1895 (function
1896 (lambda (data genealogy)
1897 (let ((type (org-element-type data)))
1898 (cond
1899 ((memq type '(headline inlinetask))
1900 (let ((tags (org-element-property :tags data)))
1901 (if (loop for tag in (plist-get info :select-tags)
1902 thereis (member tag tags))
1903 ;; When a select tag is found, mark full
1904 ;; genealogy and every headline within the tree
1905 ;; as acceptable.
1906 (setq selected-trees
1907 (append
1908 genealogy
1909 (org-element-map data '(headline inlinetask)
1910 'identity)
1911 selected-trees))
1912 ;; If at a headline, continue searching in tree,
1913 ;; recursively.
1914 (when (eq type 'headline)
1915 (mapc (lambda (el)
1916 (funcall walk-data el (cons data genealogy)))
1917 (org-element-contents data))))))
1918 ((or (eq type 'org-data)
1919 (memq type org-element-greater-elements))
1920 (mapc (lambda (el) (funcall walk-data el genealogy))
1921 (org-element-contents data)))))))))
1922 (funcall walk-data data nil)
1923 selected-trees))
1925 (defun org-export--skip-p (blob options selected)
1926 "Non-nil when element or object BLOB should be skipped during export.
1927 OPTIONS is the plist holding export options. SELECTED, when
1928 non-nil, is a list of headlines or inlinetasks belonging to
1929 a tree with a select tag."
1930 (case (org-element-type blob)
1931 (clock (not (plist-get options :with-clocks)))
1932 (drawer
1933 (let ((with-drawers-p (plist-get options :with-drawers)))
1934 (or (not with-drawers-p)
1935 (and (consp with-drawers-p)
1936 ;; If `:with-drawers' value starts with `not', ignore
1937 ;; every drawer whose name belong to that list.
1938 ;; Otherwise, ignore drawers whose name isn't in that
1939 ;; list.
1940 (let ((name (org-element-property :drawer-name blob)))
1941 (if (eq (car with-drawers-p) 'not)
1942 (member-ignore-case name (cdr with-drawers-p))
1943 (not (member-ignore-case name with-drawers-p))))))))
1944 ((headline inlinetask)
1945 (let ((with-tasks (plist-get options :with-tasks))
1946 (todo (org-element-property :todo-keyword blob))
1947 (todo-type (org-element-property :todo-type blob))
1948 (archived (plist-get options :with-archived-trees))
1949 (tags (org-element-property :tags blob)))
1951 (and (eq (org-element-type blob) 'inlinetask)
1952 (not (plist-get options :with-inlinetasks)))
1953 ;; Ignore subtrees with an exclude tag.
1954 (loop for k in (plist-get options :exclude-tags)
1955 thereis (member k tags))
1956 ;; When a select tag is present in the buffer, ignore any tree
1957 ;; without it.
1958 (and selected (not (memq blob selected)))
1959 ;; Ignore commented sub-trees.
1960 (org-element-property :commentedp blob)
1961 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1962 (and (not archived) (org-element-property :archivedp blob))
1963 ;; Ignore tasks, if specified by `:with-tasks' property.
1964 (and todo
1965 (or (not with-tasks)
1966 (and (memq with-tasks '(todo done))
1967 (not (eq todo-type with-tasks)))
1968 (and (consp with-tasks) (not (member todo with-tasks))))))))
1969 ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
1970 (planning (not (plist-get options :with-plannings)))
1971 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1972 (table-cell
1973 (and (org-export-table-has-special-column-p
1974 (org-export-get-parent-table blob))
1975 (not (org-export-get-previous-element blob options))))
1976 (table-row (org-export-table-row-is-special-p blob options))
1977 (timestamp
1978 (case (plist-get options :with-timestamps)
1979 ;; No timestamp allowed.
1980 ('nil t)
1981 ;; Only active timestamps allowed and the current one isn't
1982 ;; active.
1983 (active
1984 (not (memq (org-element-property :type blob)
1985 '(active active-range))))
1986 ;; Only inactive timestamps allowed and the current one isn't
1987 ;; inactive.
1988 (inactive
1989 (not (memq (org-element-property :type blob)
1990 '(inactive inactive-range))))))))
1994 ;;; The Transcoder
1996 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1997 ;; `org-element-parse-buffer') and transcodes it into a specified
1998 ;; back-end output. It takes care of filtering out elements or
1999 ;; objects according to export options and organizing the output blank
2000 ;; lines and white space are preserved. The function memoizes its
2001 ;; results, so it is cheap to call it within translators.
2003 ;; It is possible to modify locally the back-end used by
2004 ;; `org-export-data' or even use a temporary back-end by using
2005 ;; `org-export-data-with-translations' and
2006 ;; `org-export-data-with-backend'.
2008 ;; Internally, three functions handle the filtering of objects and
2009 ;; elements during the export. In particular,
2010 ;; `org-export-ignore-element' marks an element or object so future
2011 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
2012 ;; elements or objects should be seen as real Org syntax and
2013 ;; `org-export-expand' transforms the others back into their original
2014 ;; shape
2016 ;; `org-export-transcoder' is an accessor returning appropriate
2017 ;; translator function for a given element or object.
2019 (defun org-export-transcoder (blob info)
2020 "Return appropriate transcoder for BLOB.
2021 INFO is a plist containing export directives."
2022 (let ((type (org-element-type blob)))
2023 ;; Return contents only for complete parse trees.
2024 (if (eq type 'org-data) (lambda (blob contents info) contents)
2025 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
2026 (and (functionp transcoder) transcoder)))))
2028 (defun org-export-data (data info)
2029 "Convert DATA into current back-end format.
2031 DATA is a parse tree, an element or an object or a secondary
2032 string. INFO is a plist holding export options.
2034 Return transcoded string."
2035 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
2036 (if (not (eq memo 'no-memo)) memo
2037 (let* ((type (org-element-type data))
2038 (results
2039 (cond
2040 ;; Ignored element/object.
2041 ((memq data (plist-get info :ignore-list)) nil)
2042 ;; Plain text.
2043 ((eq type 'plain-text)
2044 (org-export-filter-apply-functions
2045 (plist-get info :filter-plain-text)
2046 (let ((transcoder (org-export-transcoder data info)))
2047 (if transcoder (funcall transcoder data info) data))
2048 info))
2049 ;; Uninterpreted element/object: change it back to Org
2050 ;; syntax and export again resulting raw string.
2051 ((not (org-export--interpret-p data info))
2052 (org-export-data
2053 (org-export-expand
2054 data
2055 (mapconcat (lambda (blob) (org-export-data blob info))
2056 (org-element-contents data)
2057 ""))
2058 info))
2059 ;; Secondary string.
2060 ((not type)
2061 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
2062 ;; Element/Object without contents or, as a special case,
2063 ;; headline with archive tag and archived trees restricted
2064 ;; to title only.
2065 ((or (not (org-element-contents data))
2066 (and (eq type 'headline)
2067 (eq (plist-get info :with-archived-trees) 'headline)
2068 (org-element-property :archivedp data)))
2069 (let ((transcoder (org-export-transcoder data info)))
2070 (and (functionp transcoder)
2071 (funcall transcoder data nil info))))
2072 ;; Element/Object with contents.
2074 (let ((transcoder (org-export-transcoder data info)))
2075 (when transcoder
2076 (let* ((greaterp (memq type org-element-greater-elements))
2077 (objectp
2078 (and (not greaterp)
2079 (memq type org-element-recursive-objects)))
2080 (contents
2081 (mapconcat
2082 (lambda (element) (org-export-data element info))
2083 (org-element-contents
2084 (if (or greaterp objectp) data
2085 ;; Elements directly containing objects
2086 ;; must have their indentation normalized
2087 ;; first.
2088 (org-element-normalize-contents
2089 data
2090 ;; When normalizing contents of the first
2091 ;; paragraph in an item or a footnote
2092 ;; definition, ignore first line's
2093 ;; indentation: there is none and it
2094 ;; might be misleading.
2095 (when (eq type 'paragraph)
2096 (let ((parent (org-export-get-parent data)))
2097 (and
2098 (eq (car (org-element-contents parent))
2099 data)
2100 (memq (org-element-type parent)
2101 '(footnote-definition item))))))))
2102 "")))
2103 (funcall transcoder data
2104 (if (not greaterp) contents
2105 (org-element-normalize-string contents))
2106 info))))))))
2107 ;; Final result will be memoized before being returned.
2108 (puthash
2109 data
2110 (cond
2111 ((not results) nil)
2112 ((memq type '(org-data plain-text nil)) results)
2113 ;; Append the same white space between elements or objects as in
2114 ;; the original buffer, and call appropriate filters.
2116 (let ((results
2117 (org-export-filter-apply-functions
2118 (plist-get info (intern (format ":filter-%s" type)))
2119 (let ((post-blank (or (org-element-property :post-blank data)
2120 0)))
2121 (if (memq type org-element-all-elements)
2122 (concat (org-element-normalize-string results)
2123 (make-string post-blank ?\n))
2124 (concat results (make-string post-blank ? ))))
2125 info)))
2126 results)))
2127 (plist-get info :exported-data))))))
2129 (defun org-export-data-with-translations (data translations info)
2130 "Convert DATA into another format using a given translation table.
2131 DATA is an element, an object, a secondary string or a string.
2132 TRANSLATIONS is an alist between element or object types and
2133 a functions handling them. See `org-export-define-backend' for
2134 more information. INFO is a plist used as a communication
2135 channel."
2136 (org-export-data
2137 data
2138 ;; Set-up a new communication channel with TRANSLATIONS as the
2139 ;; translate table and a new hash table for memoization.
2140 (org-combine-plists
2141 info
2142 (list :translate-alist translations
2143 ;; Size of the hash table is reduced since this function
2144 ;; will probably be used on short trees.
2145 :exported-data (make-hash-table :test 'eq :size 401)))))
2147 (defun org-export-data-with-backend (data backend info)
2148 "Convert DATA into BACKEND format.
2150 DATA is an element, an object, a secondary string or a string.
2151 BACKEND is a symbol. INFO is a plist used as a communication
2152 channel.
2154 Unlike to `org-export-with-backend', this function will
2155 recursively convert DATA using BACKEND translation table."
2156 (org-export-barf-if-invalid-backend backend)
2157 (org-export-data-with-translations
2158 data (org-export-backend-translate-table backend) info))
2160 (defun org-export--interpret-p (blob info)
2161 "Non-nil if element or object BLOB should be interpreted during export.
2162 If nil, BLOB will appear as raw Org syntax. Check is done
2163 according to export options INFO, stored as a plist."
2164 (case (org-element-type blob)
2165 ;; ... entities...
2166 (entity (plist-get info :with-entities))
2167 ;; ... emphasis...
2168 ((bold italic strike-through underline)
2169 (plist-get info :with-emphasize))
2170 ;; ... fixed-width areas.
2171 (fixed-width (plist-get info :with-fixed-width))
2172 ;; ... footnotes...
2173 ((footnote-definition footnote-reference)
2174 (plist-get info :with-footnotes))
2175 ;; ... LaTeX environments and fragments...
2176 ((latex-environment latex-fragment)
2177 (let ((with-latex-p (plist-get info :with-latex)))
2178 (and with-latex-p (not (eq with-latex-p 'verbatim)))))
2179 ;; ... sub/superscripts...
2180 ((subscript superscript)
2181 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2182 (if (eq sub/super-p '{})
2183 (org-element-property :use-brackets-p blob)
2184 sub/super-p)))
2185 ;; ... tables...
2186 (table (plist-get info :with-tables))
2187 (otherwise t)))
2189 (defun org-export-expand (blob contents)
2190 "Expand a parsed element or object to its original state.
2191 BLOB is either an element or an object. CONTENTS is its
2192 contents, as a string or nil."
2193 (funcall
2194 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2195 blob contents))
2197 (defun org-export-ignore-element (element info)
2198 "Add ELEMENT to `:ignore-list' in INFO.
2200 Any element in `:ignore-list' will be skipped when using
2201 `org-element-map'. INFO is modified by side effects."
2202 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2206 ;;; The Filter System
2208 ;; Filters allow end-users to tweak easily the transcoded output.
2209 ;; They are the functional counterpart of hooks, as every filter in
2210 ;; a set is applied to the return value of the previous one.
2212 ;; Every set is back-end agnostic. Although, a filter is always
2213 ;; called, in addition to the string it applies to, with the back-end
2214 ;; used as argument, so it's easy for the end-user to add back-end
2215 ;; specific filters in the set. The communication channel, as
2216 ;; a plist, is required as the third argument.
2218 ;; From the developer side, filters sets can be installed in the
2219 ;; process with the help of `org-export-define-backend', which
2220 ;; internally stores filters as an alist. Each association has a key
2221 ;; among the following symbols and a function or a list of functions
2222 ;; as value.
2224 ;; - `:filter-options' applies to the property list containing export
2225 ;; options. Unlike to other filters, functions in this list accept
2226 ;; two arguments instead of three: the property list containing
2227 ;; export options and the back-end. Users can set its value through
2228 ;; `org-export-filter-options-functions' variable.
2230 ;; - `:filter-parse-tree' applies directly to the complete parsed
2231 ;; tree. Users can set it through
2232 ;; `org-export-filter-parse-tree-functions' variable.
2234 ;; - `:filter-final-output' applies to the final transcoded string.
2235 ;; Users can set it with `org-export-filter-final-output-functions'
2236 ;; variable
2238 ;; - `:filter-plain-text' applies to any string not recognized as Org
2239 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2240 ;; configure it.
2242 ;; - `:filter-TYPE' applies on the string returned after an element or
2243 ;; object of type TYPE has been transcoded. An user can modify
2244 ;; `org-export-filter-TYPE-functions'
2246 ;; All filters sets are applied with
2247 ;; `org-export-filter-apply-functions' function. Filters in a set are
2248 ;; applied in a LIFO fashion. It allows developers to be sure that
2249 ;; their filters will be applied first.
2251 ;; Filters properties are installed in communication channel with
2252 ;; `org-export-install-filters' function.
2254 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2255 ;; `org-export-before-parsing-hook') are run at the beginning of the
2256 ;; export process and just before parsing to allow for heavy structure
2257 ;; modifications.
2260 ;;;; Hooks
2262 (defvar org-export-before-processing-hook nil
2263 "Hook run at the beginning of the export process.
2265 This is run before include keywords and macros are expanded and
2266 Babel code blocks executed, on a copy of the original buffer
2267 being exported. Visibility and narrowing are preserved. Point
2268 is at the beginning of the buffer.
2270 Every function in this hook will be called with one argument: the
2271 back-end currently used, as a symbol.")
2273 (defvar org-export-before-parsing-hook nil
2274 "Hook run before parsing an export buffer.
2276 This is run after include keywords and macros have been expanded
2277 and Babel code blocks executed, on a copy of the original buffer
2278 being exported. Visibility and narrowing are preserved. Point
2279 is at the beginning of the buffer.
2281 Every function in this hook will be called with one argument: the
2282 back-end currently used, as a symbol.")
2285 ;;;; Special Filters
2287 (defvar org-export-filter-options-functions nil
2288 "List of functions applied to the export options.
2289 Each filter is called with two arguments: the export options, as
2290 a plist, and the back-end, as a symbol. It must return
2291 a property list containing export options.")
2293 (defvar org-export-filter-parse-tree-functions nil
2294 "List of functions applied to the parsed tree.
2295 Each filter is called with three arguments: the parse tree, as
2296 returned by `org-element-parse-buffer', the back-end, as
2297 a symbol, and the communication channel, as a plist. It must
2298 return the modified parse tree to transcode.")
2300 (defvar org-export-filter-plain-text-functions nil
2301 "List of functions applied to plain text.
2302 Each filter is called with three arguments: a string which
2303 contains no Org syntax, the back-end, as a symbol, and the
2304 communication channel, as a plist. It must return a string or
2305 nil.")
2307 (defvar org-export-filter-final-output-functions nil
2308 "List of functions applied to the transcoded string.
2309 Each filter is called with three arguments: the full transcoded
2310 string, the back-end, as a symbol, and the communication channel,
2311 as a plist. It must return a string that will be used as the
2312 final export output.")
2315 ;;;; Elements Filters
2317 (defvar org-export-filter-babel-call-functions nil
2318 "List of functions applied to a transcoded babel-call.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-center-block-functions nil
2324 "List of functions applied to a transcoded center block.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-clock-functions nil
2330 "List of functions applied to a transcoded clock.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-comment-functions nil
2336 "List of functions applied to a transcoded comment.
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-comment-block-functions nil
2342 "List of functions applied to a transcoded comment-block.
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-diary-sexp-functions nil
2348 "List of functions applied to a transcoded diary-sexp.
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-drawer-functions nil
2354 "List of functions applied to a transcoded drawer.
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-dynamic-block-functions nil
2360 "List of functions applied to a transcoded dynamic-block.
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-example-block-functions nil
2366 "List of functions applied to a transcoded example-block.
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-export-block-functions nil
2372 "List of functions applied to a transcoded export-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-fixed-width-functions nil
2378 "List of functions applied to a transcoded fixed-width.
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-footnote-definition-functions nil
2384 "List of functions applied to a transcoded footnote-definition.
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-headline-functions nil
2390 "List of functions applied to a transcoded headline.
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-horizontal-rule-functions nil
2396 "List of functions applied to a transcoded horizontal-rule.
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-inlinetask-functions nil
2402 "List of functions applied to a transcoded inlinetask.
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-item-functions nil
2408 "List of functions applied to a transcoded item.
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-keyword-functions nil
2414 "List of functions applied to a transcoded keyword.
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-latex-environment-functions nil
2420 "List of functions applied to a transcoded latex-environment.
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-node-property-functions nil
2426 "List of functions applied to a transcoded node-property.
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-paragraph-functions nil
2432 "List of functions applied to a transcoded paragraph.
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-plain-list-functions nil
2438 "List of functions applied to a transcoded plain-list.
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-planning-functions nil
2444 "List of functions applied to a transcoded planning.
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-property-drawer-functions nil
2450 "List of functions applied to a transcoded property-drawer.
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-quote-block-functions nil
2456 "List of functions applied to a transcoded quote block.
2457 Each filter is called with three arguments: the transcoded quote
2458 data, as a string, the back-end, as a symbol, and the
2459 communication channel, as a plist. It must return a string or
2460 nil.")
2462 (defvar org-export-filter-quote-section-functions nil
2463 "List of functions applied to a transcoded quote-section.
2464 Each filter is called with three arguments: the transcoded data,
2465 as a string, the back-end, as a symbol, and the communication
2466 channel, as a plist. It must return a string or nil.")
2468 (defvar org-export-filter-section-functions nil
2469 "List of functions applied to a transcoded section.
2470 Each filter is called with three arguments: the transcoded data,
2471 as a string, the back-end, as a symbol, and the communication
2472 channel, as a plist. It must return a string or nil.")
2474 (defvar org-export-filter-special-block-functions nil
2475 "List of functions applied to a transcoded special block.
2476 Each filter is called with three arguments: the transcoded data,
2477 as a string, the back-end, as a symbol, and the communication
2478 channel, as a plist. It must return a string or nil.")
2480 (defvar org-export-filter-src-block-functions nil
2481 "List of functions applied to a transcoded src-block.
2482 Each filter is called with three arguments: the transcoded data,
2483 as a string, the back-end, as a symbol, and the communication
2484 channel, as a plist. It must return a string or nil.")
2486 (defvar org-export-filter-table-functions nil
2487 "List of functions applied to a transcoded table.
2488 Each filter is called with three arguments: the transcoded data,
2489 as a string, the back-end, as a symbol, and the communication
2490 channel, as a plist. It must return a string or nil.")
2492 (defvar org-export-filter-table-cell-functions nil
2493 "List of functions applied to a transcoded table-cell.
2494 Each filter is called with three arguments: the transcoded data,
2495 as a string, the back-end, as a symbol, and the communication
2496 channel, as a plist. It must return a string or nil.")
2498 (defvar org-export-filter-table-row-functions nil
2499 "List of functions applied to a transcoded table-row.
2500 Each filter is called with three arguments: the transcoded data,
2501 as a string, the back-end, as a symbol, and the communication
2502 channel, as a plist. It must return a string or nil.")
2504 (defvar org-export-filter-verse-block-functions nil
2505 "List of functions applied to a transcoded verse block.
2506 Each filter is called with three arguments: the transcoded data,
2507 as a string, the back-end, as a symbol, and the communication
2508 channel, as a plist. It must return a string or nil.")
2511 ;;;; Objects Filters
2513 (defvar org-export-filter-bold-functions nil
2514 "List of functions applied to transcoded bold text.
2515 Each filter is called with three arguments: the transcoded data,
2516 as a string, the back-end, as a symbol, and the communication
2517 channel, as a plist. It must return a string or nil.")
2519 (defvar org-export-filter-code-functions nil
2520 "List of functions applied to transcoded code text.
2521 Each filter is called with three arguments: the transcoded data,
2522 as a string, the back-end, as a symbol, and the communication
2523 channel, as a plist. It must return a string or nil.")
2525 (defvar org-export-filter-entity-functions nil
2526 "List of functions applied to a transcoded entity.
2527 Each filter is called with three arguments: the transcoded data,
2528 as a string, the back-end, as a symbol, and the communication
2529 channel, as a plist. It must return a string or nil.")
2531 (defvar org-export-filter-export-snippet-functions nil
2532 "List of functions applied to a transcoded export-snippet.
2533 Each filter is called with three arguments: the transcoded data,
2534 as a string, the back-end, as a symbol, and the communication
2535 channel, as a plist. It must return a string or nil.")
2537 (defvar org-export-filter-footnote-reference-functions nil
2538 "List of functions applied to a transcoded footnote-reference.
2539 Each filter is called with three arguments: the transcoded data,
2540 as a string, the back-end, as a symbol, and the communication
2541 channel, as a plist. It must return a string or nil.")
2543 (defvar org-export-filter-inline-babel-call-functions nil
2544 "List of functions applied to a transcoded inline-babel-call.
2545 Each filter is called with three arguments: the transcoded data,
2546 as a string, the back-end, as a symbol, and the communication
2547 channel, as a plist. It must return a string or nil.")
2549 (defvar org-export-filter-inline-src-block-functions nil
2550 "List of functions applied to a transcoded inline-src-block.
2551 Each filter is called with three arguments: the transcoded data,
2552 as a string, the back-end, as a symbol, and the communication
2553 channel, as a plist. It must return a string or nil.")
2555 (defvar org-export-filter-italic-functions nil
2556 "List of functions applied to transcoded italic text.
2557 Each filter is called with three arguments: the transcoded data,
2558 as a string, the back-end, as a symbol, and the communication
2559 channel, as a plist. It must return a string or nil.")
2561 (defvar org-export-filter-latex-fragment-functions nil
2562 "List of functions applied to a transcoded latex-fragment.
2563 Each filter is called with three arguments: the transcoded data,
2564 as a string, the back-end, as a symbol, and the communication
2565 channel, as a plist. It must return a string or nil.")
2567 (defvar org-export-filter-line-break-functions nil
2568 "List of functions applied to a transcoded line-break.
2569 Each filter is called with three arguments: the transcoded data,
2570 as a string, the back-end, as a symbol, and the communication
2571 channel, as a plist. It must return a string or nil.")
2573 (defvar org-export-filter-link-functions nil
2574 "List of functions applied to a transcoded link.
2575 Each filter is called with three arguments: the transcoded data,
2576 as a string, the back-end, as a symbol, and the communication
2577 channel, as a plist. It must return a string or nil.")
2579 (defvar org-export-filter-macro-functions nil
2580 "List of functions applied to a transcoded macro.
2581 Each filter is called with three arguments: the transcoded data,
2582 as a string, the back-end, as a symbol, and the communication
2583 channel, as a plist. It must return a string or nil.")
2585 (defvar org-export-filter-radio-target-functions nil
2586 "List of functions applied to a transcoded radio-target.
2587 Each filter is called with three arguments: the transcoded data,
2588 as a string, the back-end, as a symbol, and the communication
2589 channel, as a plist. It must return a string or nil.")
2591 (defvar org-export-filter-statistics-cookie-functions nil
2592 "List of functions applied to a transcoded statistics-cookie.
2593 Each filter is called with three arguments: the transcoded data,
2594 as a string, the back-end, as a symbol, and the communication
2595 channel, as a plist. It must return a string or nil.")
2597 (defvar org-export-filter-strike-through-functions nil
2598 "List of functions applied to transcoded strike-through text.
2599 Each filter is called with three arguments: the transcoded data,
2600 as a string, the back-end, as a symbol, and the communication
2601 channel, as a plist. It must return a string or nil.")
2603 (defvar org-export-filter-subscript-functions nil
2604 "List of functions applied to a transcoded subscript.
2605 Each filter is called with three arguments: the transcoded data,
2606 as a string, the back-end, as a symbol, and the communication
2607 channel, as a plist. It must return a string or nil.")
2609 (defvar org-export-filter-superscript-functions nil
2610 "List of functions applied to a transcoded superscript.
2611 Each filter is called with three arguments: the transcoded data,
2612 as a string, the back-end, as a symbol, and the communication
2613 channel, as a plist. It must return a string or nil.")
2615 (defvar org-export-filter-target-functions nil
2616 "List of functions applied to a transcoded target.
2617 Each filter is called with three arguments: the transcoded data,
2618 as a string, the back-end, as a symbol, and the communication
2619 channel, as a plist. It must return a string or nil.")
2621 (defvar org-export-filter-timestamp-functions nil
2622 "List of functions applied to a transcoded timestamp.
2623 Each filter is called with three arguments: the transcoded data,
2624 as a string, the back-end, as a symbol, and the communication
2625 channel, as a plist. It must return a string or nil.")
2627 (defvar org-export-filter-underline-functions nil
2628 "List of functions applied to transcoded underline text.
2629 Each filter is called with three arguments: the transcoded data,
2630 as a string, the back-end, as a symbol, and the communication
2631 channel, as a plist. It must return a string or nil.")
2633 (defvar org-export-filter-verbatim-functions nil
2634 "List of functions applied to transcoded verbatim text.
2635 Each filter is called with three arguments: the transcoded data,
2636 as a string, the back-end, as a symbol, and the communication
2637 channel, as a plist. It must return a string or nil.")
2640 ;;;; Filters Tools
2642 ;; Internal function `org-export-install-filters' installs filters
2643 ;; hard-coded in back-ends (developer filters) and filters from global
2644 ;; variables (user filters) in the communication channel.
2646 ;; Internal function `org-export-filter-apply-functions' takes care
2647 ;; about applying each filter in order to a given data. It ignores
2648 ;; filters returning a nil value but stops whenever a filter returns
2649 ;; an empty string.
2651 (defun org-export-filter-apply-functions (filters value info)
2652 "Call every function in FILTERS.
2654 Functions are called with arguments VALUE, current export
2655 back-end and INFO. A function returning a nil value will be
2656 skipped. If it returns the empty string, the process ends and
2657 VALUE is ignored.
2659 Call is done in a LIFO fashion, to be sure that developer
2660 specified filters, if any, are called first."
2661 (catch 'exit
2662 (dolist (filter filters value)
2663 (let ((result (funcall filter value (plist-get info :back-end) info)))
2664 (cond ((not result) value)
2665 ((equal value "") (throw 'exit nil))
2666 (t (setq value result)))))))
2668 (defun org-export-install-filters (info)
2669 "Install filters properties in communication channel.
2670 INFO is a plist containing the current communication channel.
2671 Return the updated communication channel."
2672 (let (plist)
2673 ;; Install user-defined filters with `org-export-filters-alist'
2674 ;; and filters already in INFO (through ext-plist mechanism).
2675 (mapc (lambda (p)
2676 (let* ((prop (car p))
2677 (info-value (plist-get info prop))
2678 (default-value (symbol-value (cdr p))))
2679 (setq plist
2680 (plist-put plist prop
2681 ;; Filters in INFO will be called
2682 ;; before those user provided.
2683 (append (if (listp info-value) info-value
2684 (list info-value))
2685 default-value)))))
2686 org-export-filters-alist)
2687 ;; Prepend back-end specific filters to that list.
2688 (mapc (lambda (p)
2689 ;; Single values get consed, lists are appended.
2690 (let ((key (car p)) (value (cdr p)))
2691 (when value
2692 (setq plist
2693 (plist-put
2694 plist key
2695 (if (atom value) (cons value (plist-get plist key))
2696 (append value (plist-get plist key))))))))
2697 (org-export-backend-filters (plist-get info :back-end)))
2698 ;; Return new communication channel.
2699 (org-combine-plists info plist)))
2703 ;;; Core functions
2705 ;; This is the room for the main function, `org-export-as', along with
2706 ;; its derivatives, `org-export-to-buffer', `org-export-to-file' and
2707 ;; `org-export-string-as'. They differ either by the way they output
2708 ;; the resulting code (for the first two) or by the input type (for
2709 ;; the latter).
2711 ;; `org-export-output-file-name' is an auxiliary function meant to be
2712 ;; used with `org-export-to-file'. With a given extension, it tries
2713 ;; to provide a canonical file name to write export output to.
2715 ;; Note that `org-export-as' doesn't really parse the current buffer,
2716 ;; but a copy of it (with the same buffer-local variables and
2717 ;; visibility), where macros and include keywords are expanded and
2718 ;; Babel blocks are executed, if appropriate.
2719 ;; `org-export-with-buffer-copy' macro prepares that copy.
2721 ;; File inclusion is taken care of by
2722 ;; `org-export-expand-include-keyword' and
2723 ;; `org-export--prepare-file-contents'. Structure wise, including
2724 ;; a whole Org file in a buffer often makes little sense. For
2725 ;; example, if the file contains a headline and the include keyword
2726 ;; was within an item, the item should contain the headline. That's
2727 ;; why file inclusion should be done before any structure can be
2728 ;; associated to the file, that is before parsing.
2730 (defun org-export-copy-buffer ()
2731 "Return a copy of the current buffer.
2732 The copy preserves Org buffer-local variables, visibility and
2733 narrowing."
2734 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2735 (new-buf (generate-new-buffer (buffer-name))))
2736 (with-current-buffer new-buf
2737 (funcall copy-buffer-fun)
2738 (set-buffer-modified-p nil))
2739 new-buf))
2741 (defmacro org-export-with-buffer-copy (&rest body)
2742 "Apply BODY in a copy of the current buffer.
2743 The copy preserves local variables, visibility and contents of
2744 the original buffer. Point is at the beginning of the buffer
2745 when BODY is applied."
2746 (declare (debug t))
2747 (org-with-gensyms (buf-copy)
2748 `(let ((,buf-copy (org-export-copy-buffer)))
2749 (unwind-protect
2750 (with-current-buffer ,buf-copy
2751 (goto-char (point-min))
2752 (progn ,@body))
2753 (and (buffer-live-p ,buf-copy)
2754 ;; Kill copy without confirmation.
2755 (progn (with-current-buffer ,buf-copy
2756 (restore-buffer-modified-p nil))
2757 (kill-buffer ,buf-copy)))))))
2759 (defun org-export--generate-copy-script (buffer)
2760 "Generate a function duplicating BUFFER.
2762 The copy will preserve local variables, visibility, contents and
2763 narrowing of the original buffer. If a region was active in
2764 BUFFER, contents will be narrowed to that region instead.
2766 The resulting function can be evaled at a later time, from
2767 another buffer, effectively cloning the original buffer there.
2769 The function assumes BUFFER's major mode is `org-mode'."
2770 (with-current-buffer buffer
2771 `(lambda ()
2772 (let ((inhibit-modification-hooks t))
2773 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2774 ;; already in BUFFER.
2775 (let ((org-mode-hook nil)) (org-mode))
2776 ;; Buffer local variables.
2777 ,@(let (local-vars)
2778 (mapc
2779 (lambda (entry)
2780 (when (consp entry)
2781 (let ((var (car entry))
2782 (val (cdr entry)))
2783 (and (not (eq var 'org-font-lock-keywords))
2784 (or (memq var
2785 '(default-directory
2786 buffer-file-name
2787 buffer-file-coding-system))
2788 (string-match "^\\(org-\\|orgtbl-\\)"
2789 (symbol-name var)))
2790 ;; Skip unreadable values, as they cannot be
2791 ;; sent to external process.
2792 (or (not val) (ignore-errors (read (format "%S" val))))
2793 (push `(set (make-local-variable (quote ,var))
2794 (quote ,val))
2795 local-vars)))))
2796 (buffer-local-variables (buffer-base-buffer)))
2797 local-vars)
2798 ;; Whole buffer contents.
2799 (insert
2800 ,(org-with-wide-buffer
2801 (buffer-substring-no-properties
2802 (point-min) (point-max))))
2803 ;; Narrowing.
2804 ,(if (org-region-active-p)
2805 `(narrow-to-region ,(region-beginning) ,(region-end))
2806 `(narrow-to-region ,(point-min) ,(point-max)))
2807 ;; Current position of point.
2808 (goto-char ,(point))
2809 ;; Overlays with invisible property.
2810 ,@(let (ov-set)
2811 (mapc
2812 (lambda (ov)
2813 (let ((invis-prop (overlay-get ov 'invisible)))
2814 (when invis-prop
2815 (push `(overlay-put
2816 (make-overlay ,(overlay-start ov)
2817 ,(overlay-end ov))
2818 'invisible (quote ,invis-prop))
2819 ov-set))))
2820 (overlays-in (point-min) (point-max)))
2821 ov-set)))))
2823 ;;;###autoload
2824 (defun org-export-as
2825 (backend &optional subtreep visible-only body-only ext-plist)
2826 "Transcode current Org buffer into BACKEND code.
2828 If narrowing is active in the current buffer, only transcode its
2829 narrowed part.
2831 If a region is active, transcode that region.
2833 When optional argument SUBTREEP is non-nil, transcode the
2834 sub-tree at point, extracting information from the headline
2835 properties first.
2837 When optional argument VISIBLE-ONLY is non-nil, don't export
2838 contents of hidden elements.
2840 When optional argument BODY-ONLY is non-nil, only return body
2841 code, without surrounding template.
2843 Optional argument EXT-PLIST, when provided, is a property list
2844 with external parameters overriding Org default settings, but
2845 still inferior to file-local settings.
2847 Return code as a string."
2848 (org-export-barf-if-invalid-backend backend)
2849 (save-excursion
2850 (save-restriction
2851 ;; Narrow buffer to an appropriate region or subtree for
2852 ;; parsing. If parsing subtree, be sure to remove main headline
2853 ;; too.
2854 (cond ((org-region-active-p)
2855 (narrow-to-region (region-beginning) (region-end)))
2856 (subtreep
2857 (org-narrow-to-subtree)
2858 (goto-char (point-min))
2859 (forward-line)
2860 (narrow-to-region (point) (point-max))))
2861 ;; Initialize communication channel with original buffer
2862 ;; attributes, unavailable in its copy.
2863 (let ((info (org-combine-plists
2864 (list :export-options
2865 (delq nil
2866 (list (and subtreep 'subtree)
2867 (and visible-only 'visible-only)
2868 (and body-only 'body-only))))
2869 (org-export--get-buffer-attributes)))
2870 tree)
2871 ;; Update communication channel and get parse tree. Buffer
2872 ;; isn't parsed directly. Instead, a temporary copy is
2873 ;; created, where include keywords, macros are expanded and
2874 ;; code blocks are evaluated.
2875 (org-export-with-buffer-copy
2876 ;; Run first hook with current back-end as argument.
2877 (run-hook-with-args 'org-export-before-processing-hook backend)
2878 (org-export-expand-include-keyword)
2879 ;; Update macro templates since #+INCLUDE keywords might have
2880 ;; added some new ones.
2881 (org-macro-initialize-templates)
2882 (org-macro-replace-all org-macro-templates)
2883 (org-export-execute-babel-code)
2884 ;; Update radio targets since keyword inclusion might have
2885 ;; added some more.
2886 (org-update-radio-target-regexp)
2887 ;; Run last hook with current back-end as argument.
2888 (goto-char (point-min))
2889 (run-hook-with-args 'org-export-before-parsing-hook backend)
2890 ;; Update communication channel with environment. Also
2891 ;; install user's and developer's filters.
2892 (setq info
2893 (org-export-install-filters
2894 (org-combine-plists
2895 info (org-export-get-environment backend subtreep ext-plist))))
2896 ;; Expand export-specific set of macros: {{{author}}},
2897 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2898 ;; once regular macros have been expanded, since document
2899 ;; keywords may contain one of them.
2900 (org-macro-replace-all
2901 (list (cons "author"
2902 (org-element-interpret-data (plist-get info :author)))
2903 (cons "date"
2904 (org-element-interpret-data (plist-get info :date)))
2905 ;; EMAIL is not a parsed keyword: store it as-is.
2906 (cons "email" (or (plist-get info :email) ""))
2907 (cons "title"
2908 (org-element-interpret-data (plist-get info :title)))))
2909 ;; Call options filters and update export options. We do not
2910 ;; use `org-export-filter-apply-functions' here since the
2911 ;; arity of such filters is different.
2912 (dolist (filter (plist-get info :filter-options))
2913 (let ((result (funcall filter info backend)))
2914 (when result (setq info result))))
2915 ;; Parse buffer and call parse-tree filter on it.
2916 (setq tree
2917 (org-export-filter-apply-functions
2918 (plist-get info :filter-parse-tree)
2919 (org-element-parse-buffer nil visible-only) info))
2920 ;; Now tree is complete, compute its properties and add them
2921 ;; to communication channel.
2922 (setq info
2923 (org-combine-plists
2924 info (org-export-collect-tree-properties tree info)))
2925 ;; Eventually transcode TREE. Wrap the resulting string into
2926 ;; a template.
2927 (let* ((body (org-element-normalize-string
2928 (or (org-export-data tree info) "")))
2929 (inner-template (cdr (assq 'inner-template
2930 (plist-get info :translate-alist))))
2931 (full-body (if (not (functionp inner-template)) body
2932 (funcall inner-template body info)))
2933 (template (cdr (assq 'template
2934 (plist-get info :translate-alist)))))
2935 ;; Remove all text properties since they cannot be
2936 ;; retrieved from an external process. Finally call
2937 ;; final-output filter and return result.
2938 (org-no-properties
2939 (org-export-filter-apply-functions
2940 (plist-get info :filter-final-output)
2941 (if (or (not (functionp template)) body-only) full-body
2942 (funcall template full-body info))
2943 info))))))))
2945 ;;;###autoload
2946 (defun org-export-to-buffer
2947 (backend buffer &optional subtreep visible-only body-only ext-plist)
2948 "Call `org-export-as' with output to a specified buffer.
2950 BACKEND is the back-end used for transcoding, as a symbol.
2952 BUFFER is the output buffer. If it already exists, it will be
2953 erased first, otherwise, it will be created.
2955 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2956 EXT-PLIST are similar to those used in `org-export-as', which
2957 see.
2959 If `org-export-copy-to-kill-ring' is non-nil, add buffer contents
2960 to kill ring. Return buffer."
2961 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
2962 (buffer (get-buffer-create buffer)))
2963 (with-current-buffer buffer
2964 (erase-buffer)
2965 (insert out)
2966 (goto-char (point-min)))
2967 ;; Maybe add buffer contents to kill ring.
2968 (when (and org-export-copy-to-kill-ring (org-string-nw-p out))
2969 (org-kill-new out))
2970 ;; Return buffer.
2971 buffer))
2973 ;;;###autoload
2974 (defun org-export-to-file
2975 (backend file &optional subtreep visible-only body-only ext-plist)
2976 "Call `org-export-as' with output to a specified file.
2978 BACKEND is the back-end used for transcoding, as a symbol. FILE
2979 is the name of the output file, as a string.
2981 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2982 EXT-PLIST are similar to those used in `org-export-as', which
2983 see.
2985 If `org-export-copy-to-kill-ring' is non-nil, add file contents
2986 to kill ring. Return output file's name."
2987 ;; Checks for FILE permissions. `write-file' would do the same, but
2988 ;; we'd rather avoid needless transcoding of parse tree.
2989 (unless (file-writable-p file) (error "Output file not writable"))
2990 ;; Insert contents to a temporary buffer and write it to FILE.
2991 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist)))
2992 (with-temp-buffer
2993 (insert out)
2994 (let ((coding-system-for-write org-export-coding-system))
2995 (write-file file)))
2996 ;; Maybe add file contents to kill ring.
2997 (when (and org-export-copy-to-kill-ring (org-string-nw-p out))
2998 (org-kill-new out)))
2999 ;; Return full path.
3000 file)
3002 ;;;###autoload
3003 (defun org-export-string-as (string backend &optional body-only ext-plist)
3004 "Transcode STRING into BACKEND code.
3006 When optional argument BODY-ONLY is non-nil, only return body
3007 code, without preamble nor postamble.
3009 Optional argument EXT-PLIST, when provided, is a property list
3010 with external parameters overriding Org default settings, but
3011 still inferior to file-local settings.
3013 Return code as a string."
3014 (with-temp-buffer
3015 (insert string)
3016 (org-mode)
3017 (org-export-as backend nil nil body-only ext-plist)))
3019 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
3020 "Return output file's name according to buffer specifications.
3022 EXTENSION is a string representing the output file extension,
3023 with the leading dot.
3025 With a non-nil optional argument SUBTREEP, try to determine
3026 output file's name by looking for \"EXPORT_FILE_NAME\" property
3027 of subtree at point.
3029 When optional argument PUB-DIR is set, use it as the publishing
3030 directory.
3032 When optional argument VISIBLE-ONLY is non-nil, don't export
3033 contents of hidden elements.
3035 Return file name as a string."
3036 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
3037 (base-name
3038 ;; File name may come from EXPORT_FILE_NAME subtree
3039 ;; property, assuming point is at beginning of said
3040 ;; sub-tree.
3041 (file-name-sans-extension
3042 (or (and subtreep
3043 (org-entry-get
3044 (save-excursion
3045 (ignore-errors (org-back-to-heading) (point)))
3046 "EXPORT_FILE_NAME" t))
3047 ;; File name may be extracted from buffer's associated
3048 ;; file, if any.
3049 (and visited-file (file-name-nondirectory visited-file))
3050 ;; Can't determine file name on our own: Ask user.
3051 (let ((read-file-name-function
3052 (and org-completion-use-ido 'ido-read-file-name)))
3053 (read-file-name
3054 "Output file: " pub-dir nil nil nil
3055 (lambda (name)
3056 (string= (file-name-extension name t) extension)))))))
3057 (output-file
3058 ;; Build file name. Enforce EXTENSION over whatever user
3059 ;; may have come up with. PUB-DIR, if defined, always has
3060 ;; precedence over any provided path.
3061 (cond
3062 (pub-dir
3063 (concat (file-name-as-directory pub-dir)
3064 (file-name-nondirectory base-name)
3065 extension))
3066 ((file-name-absolute-p base-name) (concat base-name extension))
3067 (t (concat (file-name-as-directory ".") base-name extension)))))
3068 ;; If writing to OUTPUT-FILE would overwrite original file, append
3069 ;; EXTENSION another time to final name.
3070 (if (and visited-file (org-file-equal-p visited-file output-file))
3071 (concat output-file extension)
3072 output-file)))
3074 (defun org-export-expand-include-keyword (&optional included dir)
3075 "Expand every include keyword in buffer.
3076 Optional argument INCLUDED is a list of included file names along
3077 with their line restriction, when appropriate. It is used to
3078 avoid infinite recursion. Optional argument DIR is the current
3079 working directory. It is used to properly resolve relative
3080 paths."
3081 (let ((case-fold-search t))
3082 (goto-char (point-min))
3083 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
3084 (when (eq (org-element-type (save-match-data (org-element-at-point)))
3085 'keyword)
3086 (beginning-of-line)
3087 ;; Extract arguments from keyword's value.
3088 (let* ((value (match-string 1))
3089 (ind (org-get-indentation))
3090 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
3091 (prog1 (expand-file-name (match-string 1 value) dir)
3092 (setq value (replace-match "" nil nil value)))))
3093 (lines
3094 (and (string-match
3095 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
3096 (prog1 (match-string 1 value)
3097 (setq value (replace-match "" nil nil value)))))
3098 (env (cond ((string-match "\\<example\\>" value) 'example)
3099 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3100 (match-string 1 value))))
3101 ;; Minimal level of included file defaults to the child
3102 ;; level of the current headline, if any, or one. It
3103 ;; only applies is the file is meant to be included as
3104 ;; an Org one.
3105 (minlevel
3106 (and (not env)
3107 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3108 (prog1 (string-to-number (match-string 1 value))
3109 (setq value (replace-match "" nil nil value)))
3110 (let ((cur (org-current-level)))
3111 (if cur (1+ (org-reduced-level cur)) 1))))))
3112 ;; Remove keyword.
3113 (delete-region (point) (progn (forward-line) (point)))
3114 (cond
3115 ((not file) (error "Invalid syntax in INCLUDE keyword"))
3116 ((not (file-readable-p file)) (error "Cannot include file %s" file))
3117 ;; Check if files has already been parsed. Look after
3118 ;; inclusion lines too, as different parts of the same file
3119 ;; can be included too.
3120 ((member (list file lines) included)
3121 (error "Recursive file inclusion: %s" file))
3123 (cond
3124 ((eq env 'example)
3125 (insert
3126 (let ((ind-str (make-string ind ? ))
3127 (contents
3128 (org-escape-code-in-string
3129 (org-export--prepare-file-contents file lines))))
3130 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3131 ind-str contents ind-str))))
3132 ((stringp env)
3133 (insert
3134 (let ((ind-str (make-string ind ? ))
3135 (contents
3136 (org-escape-code-in-string
3137 (org-export--prepare-file-contents file lines))))
3138 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3139 ind-str env contents ind-str))))
3141 (insert
3142 (with-temp-buffer
3143 (org-mode)
3144 (insert
3145 (org-export--prepare-file-contents file lines ind minlevel))
3146 (org-export-expand-include-keyword
3147 (cons (list file lines) included)
3148 (file-name-directory file))
3149 (buffer-string))))))))))))
3151 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
3152 "Prepare the contents of FILE for inclusion and return them as a string.
3154 When optional argument LINES is a string specifying a range of
3155 lines, include only those lines.
3157 Optional argument IND, when non-nil, is an integer specifying the
3158 global indentation of returned contents. Since its purpose is to
3159 allow an included file to stay in the same environment it was
3160 created \(i.e. a list item), it doesn't apply past the first
3161 headline encountered.
3163 Optional argument MINLEVEL, when non-nil, is an integer
3164 specifying the level that any top-level headline in the included
3165 file should have."
3166 (with-temp-buffer
3167 (insert-file-contents file)
3168 (when lines
3169 (let* ((lines (split-string lines "-"))
3170 (lbeg (string-to-number (car lines)))
3171 (lend (string-to-number (cadr lines)))
3172 (beg (if (zerop lbeg) (point-min)
3173 (goto-char (point-min))
3174 (forward-line (1- lbeg))
3175 (point)))
3176 (end (if (zerop lend) (point-max)
3177 (goto-char (point-min))
3178 (forward-line (1- lend))
3179 (point))))
3180 (narrow-to-region beg end)))
3181 ;; Remove blank lines at beginning and end of contents. The logic
3182 ;; behind that removal is that blank lines around include keyword
3183 ;; override blank lines in included file.
3184 (goto-char (point-min))
3185 (org-skip-whitespace)
3186 (beginning-of-line)
3187 (delete-region (point-min) (point))
3188 (goto-char (point-max))
3189 (skip-chars-backward " \r\t\n")
3190 (forward-line)
3191 (delete-region (point) (point-max))
3192 ;; If IND is set, preserve indentation of include keyword until
3193 ;; the first headline encountered.
3194 (when ind
3195 (unless (eq major-mode 'org-mode) (org-mode))
3196 (goto-char (point-min))
3197 (let ((ind-str (make-string ind ? )))
3198 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3199 ;; Do not move footnote definitions out of column 0.
3200 (unless (and (looking-at org-footnote-definition-re)
3201 (eq (org-element-type (org-element-at-point))
3202 'footnote-definition))
3203 (insert ind-str))
3204 (forward-line))))
3205 ;; When MINLEVEL is specified, compute minimal level for headlines
3206 ;; in the file (CUR-MIN), and remove stars to each headline so
3207 ;; that headlines with minimal level have a level of MINLEVEL.
3208 (when minlevel
3209 (unless (eq major-mode 'org-mode) (org-mode))
3210 (org-with-limited-levels
3211 (let ((levels (org-map-entries
3212 (lambda () (org-reduced-level (org-current-level))))))
3213 (when levels
3214 (let ((offset (- minlevel (apply 'min levels))))
3215 (unless (zerop offset)
3216 (when org-odd-levels-only (setq offset (* offset 2)))
3217 ;; Only change stars, don't bother moving whole
3218 ;; sections.
3219 (org-map-entries
3220 (lambda () (if (< offset 0) (delete-char (abs offset))
3221 (insert (make-string offset ?*)))))))))))
3222 (org-element-normalize-string (buffer-string))))
3224 (defun org-export-execute-babel-code ()
3225 "Execute every Babel code in the visible part of current buffer."
3226 ;; Get a pristine copy of current buffer so Babel references can be
3227 ;; properly resolved.
3228 (let ((reference (org-export-copy-buffer)))
3229 (unwind-protect (let ((org-current-export-file reference))
3230 (org-babel-exp-process-buffer))
3231 (kill-buffer reference))))
3235 ;;; Tools For Back-Ends
3237 ;; A whole set of tools is available to help build new exporters. Any
3238 ;; function general enough to have its use across many back-ends
3239 ;; should be added here.
3241 ;;;; For Affiliated Keywords
3243 ;; `org-export-read-attribute' reads a property from a given element
3244 ;; as a plist. It can be used to normalize affiliated keywords'
3245 ;; syntax.
3247 ;; Since captions can span over multiple lines and accept dual values,
3248 ;; their internal representation is a bit tricky. Therefore,
3249 ;; `org-export-get-caption' transparently returns a given element's
3250 ;; caption as a secondary string.
3252 (defun org-export-read-attribute (attribute element &optional property)
3253 "Turn ATTRIBUTE property from ELEMENT into a plist.
3255 When optional argument PROPERTY is non-nil, return the value of
3256 that property within attributes.
3258 This function assumes attributes are defined as \":keyword
3259 value\" pairs. It is appropriate for `:attr_html' like
3260 properties. All values will become strings except the empty
3261 string and \"nil\", which will become nil."
3262 (let ((attributes
3263 (let ((value (org-element-property attribute element)))
3264 (when value
3265 (let ((s (mapconcat 'identity value " ")) result)
3266 (while (string-match
3267 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3269 (let ((value (substring s 0 (match-beginning 0))))
3270 (push (and (not (member value '("nil" ""))) value) result))
3271 (push (intern (match-string 1 s)) result)
3272 (setq s (substring s (match-end 0))))
3273 ;; Ignore any string before the first property with `cdr'.
3274 (cdr (nreverse (cons (and (org-string-nw-p s)
3275 (not (equal s "nil"))
3277 result))))))))
3278 (if property (plist-get attributes property) attributes)))
3280 (defun org-export-get-caption (element &optional shortp)
3281 "Return caption from ELEMENT as a secondary string.
3283 When optional argument SHORTP is non-nil, return short caption,
3284 as a secondary string, instead.
3286 Caption lines are separated by a white space."
3287 (let ((full-caption (org-element-property :caption element)) caption)
3288 (dolist (line full-caption (cdr caption))
3289 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3290 (when cap
3291 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3294 ;;;; For Derived Back-ends
3296 ;; `org-export-with-backend' is a function allowing to locally use
3297 ;; another back-end to transcode some object or element. In a derived
3298 ;; back-end, it may be used as a fall-back function once all specific
3299 ;; cases have been treated.
3301 (defun org-export-with-backend (back-end data &optional contents info)
3302 "Call a transcoder from BACK-END on DATA.
3303 CONTENTS, when non-nil, is the transcoded contents of DATA
3304 element, as a string. INFO, when non-nil, is the communication
3305 channel used for export, as a plist.."
3306 (org-export-barf-if-invalid-backend back-end)
3307 (let ((type (org-element-type data)))
3308 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3309 (let ((transcoder
3310 (cdr (assq type (org-export-backend-translate-table back-end)))))
3311 (if (functionp transcoder) (funcall transcoder data contents info)
3312 (error "No foreign transcoder available"))))))
3315 ;;;; For Export Snippets
3317 ;; Every export snippet is transmitted to the back-end. Though, the
3318 ;; latter will only retain one type of export-snippet, ignoring
3319 ;; others, based on the former's target back-end. The function
3320 ;; `org-export-snippet-backend' returns that back-end for a given
3321 ;; export-snippet.
3323 (defun org-export-snippet-backend (export-snippet)
3324 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3325 Translation, with `org-export-snippet-translation-alist', is
3326 applied."
3327 (let ((back-end (org-element-property :back-end export-snippet)))
3328 (intern
3329 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3330 back-end))))
3333 ;;;; For Footnotes
3335 ;; `org-export-collect-footnote-definitions' is a tool to list
3336 ;; actually used footnotes definitions in the whole parse tree, or in
3337 ;; a headline, in order to add footnote listings throughout the
3338 ;; transcoded data.
3340 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3341 ;; back-ends, when they need to attach the footnote definition only to
3342 ;; the first occurrence of the corresponding label.
3344 ;; `org-export-get-footnote-definition' and
3345 ;; `org-export-get-footnote-number' provide easier access to
3346 ;; additional information relative to a footnote reference.
3348 (defun org-export-collect-footnote-definitions (data info)
3349 "Return an alist between footnote numbers, labels and definitions.
3351 DATA is the parse tree from which definitions are collected.
3352 INFO is the plist used as a communication channel.
3354 Definitions are sorted by order of references. They either
3355 appear as Org data or as a secondary string for inlined
3356 footnotes. Unreferenced definitions are ignored."
3357 (let* (num-alist
3358 collect-fn ; for byte-compiler.
3359 (collect-fn
3360 (function
3361 (lambda (data)
3362 ;; Collect footnote number, label and definition in DATA.
3363 (org-element-map data 'footnote-reference
3364 (lambda (fn)
3365 (when (org-export-footnote-first-reference-p fn info)
3366 (let ((def (org-export-get-footnote-definition fn info)))
3367 (push
3368 (list (org-export-get-footnote-number fn info)
3369 (org-element-property :label fn)
3370 def)
3371 num-alist)
3372 ;; Also search in definition for nested footnotes.
3373 (when (eq (org-element-property :type fn) 'standard)
3374 (funcall collect-fn def)))))
3375 ;; Don't enter footnote definitions since it will happen
3376 ;; when their first reference is found.
3377 info nil 'footnote-definition)))))
3378 (funcall collect-fn (plist-get info :parse-tree))
3379 (reverse num-alist)))
3381 (defun org-export-footnote-first-reference-p (footnote-reference info)
3382 "Non-nil when a footnote reference is the first one for its label.
3384 FOOTNOTE-REFERENCE is the footnote reference being considered.
3385 INFO is the plist used as a communication channel."
3386 (let ((label (org-element-property :label footnote-reference)))
3387 ;; Anonymous footnotes are always a first reference.
3388 (if (not label) t
3389 ;; Otherwise, return the first footnote with the same LABEL and
3390 ;; test if it is equal to FOOTNOTE-REFERENCE.
3391 (let* (search-refs ; for byte-compiler.
3392 (search-refs
3393 (function
3394 (lambda (data)
3395 (org-element-map data 'footnote-reference
3396 (lambda (fn)
3397 (cond
3398 ((string= (org-element-property :label fn) label)
3399 (throw 'exit fn))
3400 ;; If FN isn't inlined, be sure to traverse its
3401 ;; definition before resuming search. See
3402 ;; comments in `org-export-get-footnote-number'
3403 ;; for more information.
3404 ((eq (org-element-property :type fn) 'standard)
3405 (funcall search-refs
3406 (org-export-get-footnote-definition fn info)))))
3407 ;; Don't enter footnote definitions since it will
3408 ;; happen when their first reference is found.
3409 info 'first-match 'footnote-definition)))))
3410 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3411 footnote-reference)))))
3413 (defun org-export-get-footnote-definition (footnote-reference info)
3414 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3415 INFO is the plist used as a communication channel. If no such
3416 definition can be found, return the \"DEFINITION NOT FOUND\"
3417 string."
3418 (let ((label (org-element-property :label footnote-reference)))
3419 (or (org-element-property :inline-definition footnote-reference)
3420 (cdr (assoc label (plist-get info :footnote-definition-alist)))
3421 "DEFINITION NOT FOUND.")))
3423 (defun org-export-get-footnote-number (footnote info)
3424 "Return number associated to a footnote.
3426 FOOTNOTE is either a footnote reference or a footnote definition.
3427 INFO is the plist used as a communication channel."
3428 (let* ((label (org-element-property :label footnote))
3429 seen-refs
3430 search-ref ; For byte-compiler.
3431 (search-ref
3432 (function
3433 (lambda (data)
3434 ;; Search footnote references through DATA, filling
3435 ;; SEEN-REFS along the way.
3436 (org-element-map data 'footnote-reference
3437 (lambda (fn)
3438 (let ((fn-lbl (org-element-property :label fn)))
3439 (cond
3440 ;; Anonymous footnote match: return number.
3441 ((and (not fn-lbl) (eq fn footnote))
3442 (throw 'exit (1+ (length seen-refs))))
3443 ;; Labels match: return number.
3444 ((and label (string= label fn-lbl))
3445 (throw 'exit (1+ (length seen-refs))))
3446 ;; Anonymous footnote: it's always a new one.
3447 ;; Also, be sure to return nil from the `cond' so
3448 ;; `first-match' doesn't get us out of the loop.
3449 ((not fn-lbl) (push 'inline seen-refs) nil)
3450 ;; Label not seen so far: add it so SEEN-REFS.
3452 ;; Also search for subsequent references in
3453 ;; footnote definition so numbering follows
3454 ;; reading logic. Note that we don't have to care
3455 ;; about inline definitions, since
3456 ;; `org-element-map' already traverses them at the
3457 ;; right time.
3459 ;; Once again, return nil to stay in the loop.
3460 ((not (member fn-lbl seen-refs))
3461 (push fn-lbl seen-refs)
3462 (funcall search-ref
3463 (org-export-get-footnote-definition fn info))
3464 nil))))
3465 ;; Don't enter footnote definitions since it will
3466 ;; happen when their first reference is found.
3467 info 'first-match 'footnote-definition)))))
3468 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3471 ;;;; For Headlines
3473 ;; `org-export-get-relative-level' is a shortcut to get headline
3474 ;; level, relatively to the lower headline level in the parsed tree.
3476 ;; `org-export-get-headline-number' returns the section number of an
3477 ;; headline, while `org-export-number-to-roman' allows to convert it
3478 ;; to roman numbers.
3480 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3481 ;; `org-export-last-sibling-p' are three useful predicates when it
3482 ;; comes to fulfill the `:headline-levels' property.
3484 ;; `org-export-get-tags', `org-export-get-category' and
3485 ;; `org-export-get-node-property' extract useful information from an
3486 ;; headline or a parent headline. They all handle inheritance.
3488 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3489 ;; as a secondary string, suitable for table of contents. It falls
3490 ;; back onto default title.
3492 (defun org-export-get-relative-level (headline info)
3493 "Return HEADLINE relative level within current parsed tree.
3494 INFO is a plist holding contextual information."
3495 (+ (org-element-property :level headline)
3496 (or (plist-get info :headline-offset) 0)))
3498 (defun org-export-low-level-p (headline info)
3499 "Non-nil when HEADLINE is considered as low level.
3501 INFO is a plist used as a communication channel.
3503 A low level headlines has a relative level greater than
3504 `:headline-levels' property value.
3506 Return value is the difference between HEADLINE relative level
3507 and the last level being considered as high enough, or nil."
3508 (let ((limit (plist-get info :headline-levels)))
3509 (when (wholenump limit)
3510 (let ((level (org-export-get-relative-level headline info)))
3511 (and (> level limit) (- level limit))))))
3513 (defun org-export-get-headline-number (headline info)
3514 "Return HEADLINE numbering as a list of numbers.
3515 INFO is a plist holding contextual information."
3516 (cdr (assoc headline (plist-get info :headline-numbering))))
3518 (defun org-export-numbered-headline-p (headline info)
3519 "Return a non-nil value if HEADLINE element should be numbered.
3520 INFO is a plist used as a communication channel."
3521 (let ((sec-num (plist-get info :section-numbers))
3522 (level (org-export-get-relative-level headline info)))
3523 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3525 (defun org-export-number-to-roman (n)
3526 "Convert integer N into a roman numeral."
3527 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3528 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3529 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3530 ( 1 . "I")))
3531 (res ""))
3532 (if (<= n 0)
3533 (number-to-string n)
3534 (while roman
3535 (if (>= n (caar roman))
3536 (setq n (- n (caar roman))
3537 res (concat res (cdar roman)))
3538 (pop roman)))
3539 res)))
3541 (defun org-export-get-tags (element info &optional tags inherited)
3542 "Return list of tags associated to ELEMENT.
3544 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3545 is a plist used as a communication channel.
3547 Select tags (see `org-export-select-tags') and exclude tags (see
3548 `org-export-exclude-tags') are removed from the list.
3550 When non-nil, optional argument TAGS should be a list of strings.
3551 Any tag belonging to this list will also be removed.
3553 When optional argument INHERITED is non-nil, tags can also be
3554 inherited from parent headlines and FILETAGS keywords."
3555 (org-remove-if
3556 (lambda (tag) (or (member tag (plist-get info :select-tags))
3557 (member tag (plist-get info :exclude-tags))
3558 (member tag tags)))
3559 (if (not inherited) (org-element-property :tags element)
3560 ;; Build complete list of inherited tags.
3561 (let ((current-tag-list (org-element-property :tags element)))
3562 (mapc
3563 (lambda (parent)
3564 (mapc
3565 (lambda (tag)
3566 (when (and (memq (org-element-type parent) '(headline inlinetask))
3567 (not (member tag current-tag-list)))
3568 (push tag current-tag-list)))
3569 (org-element-property :tags parent)))
3570 (org-export-get-genealogy element))
3571 ;; Add FILETAGS keywords and return results.
3572 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3574 (defun org-export-get-node-property (property blob &optional inherited)
3575 "Return node PROPERTY value for BLOB.
3577 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3578 element or object.
3580 If optional argument INHERITED is non-nil, the value can be
3581 inherited from a parent headline.
3583 Return value is a string or nil."
3584 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3585 (org-export-get-parent-headline blob))))
3586 (if (not inherited) (org-element-property property blob)
3587 (let ((parent headline) value)
3588 (catch 'found
3589 (while parent
3590 (when (plist-member (nth 1 parent) property)
3591 (throw 'found (org-element-property property parent)))
3592 (setq parent (org-element-property :parent parent))))))))
3594 (defun org-export-get-category (blob info)
3595 "Return category for element or object BLOB.
3597 INFO is a plist used as a communication channel.
3599 CATEGORY is automatically inherited from a parent headline, from
3600 #+CATEGORY: keyword or created out of original file name. If all
3601 fail, the fall-back value is \"???\"."
3602 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3603 (org-export-get-parent-headline blob))))
3604 ;; Almost like `org-export-node-property', but we cannot trust
3605 ;; `plist-member' as every headline has a `:CATEGORY'
3606 ;; property, would it be nil or equal to "???" (which has the
3607 ;; same meaning).
3608 (let ((parent headline) value)
3609 (catch 'found
3610 (while parent
3611 (let ((category (org-element-property :CATEGORY parent)))
3612 (and category (not (equal "???" category))
3613 (throw 'found category)))
3614 (setq parent (org-element-property :parent parent))))))
3615 (org-element-map (plist-get info :parse-tree) 'keyword
3616 (lambda (kwd)
3617 (when (equal (org-element-property :key kwd) "CATEGORY")
3618 (org-element-property :value kwd)))
3619 info 'first-match)
3620 (let ((file (plist-get info :input-file)))
3621 (and file (file-name-sans-extension (file-name-nondirectory file))))
3622 "???"))
3624 (defun org-export-get-alt-title (headline info)
3625 "Return alternative title for HEADLINE, as a secondary string.
3626 INFO is a plist used as a communication channel. If no optional
3627 title is defined, fall-back to the regular title."
3628 (or (org-element-property :alt-title headline)
3629 (org-element-property :title headline)))
3631 (defun org-export-first-sibling-p (headline info)
3632 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3633 INFO is a plist used as a communication channel."
3634 (not (eq (org-element-type (org-export-get-previous-element headline info))
3635 'headline)))
3637 (defun org-export-last-sibling-p (headline info)
3638 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3639 INFO is a plist used as a communication channel."
3640 (not (org-export-get-next-element headline info)))
3643 ;;;; For Links
3645 ;; `org-export-solidify-link-text' turns a string into a safer version
3646 ;; for links, replacing most non-standard characters with hyphens.
3648 ;; `org-export-get-coderef-format' returns an appropriate format
3649 ;; string for coderefs.
3651 ;; `org-export-inline-image-p' returns a non-nil value when the link
3652 ;; provided should be considered as an inline image.
3654 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3655 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3656 ;; returns an appropriate unique identifier when found, or nil.
3658 ;; `org-export-resolve-id-link' returns the first headline with
3659 ;; specified id or custom-id in parse tree, the path to the external
3660 ;; file with the id or nil when neither was found.
3662 ;; `org-export-resolve-coderef' associates a reference to a line
3663 ;; number in the element it belongs, or returns the reference itself
3664 ;; when the element isn't numbered.
3666 (defun org-export-solidify-link-text (s)
3667 "Take link text S and make a safe target out of it."
3668 (save-match-data
3669 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3671 (defun org-export-get-coderef-format (path desc)
3672 "Return format string for code reference link.
3673 PATH is the link path. DESC is its description."
3674 (save-match-data
3675 (cond ((not desc) "%s")
3676 ((string-match (regexp-quote (concat "(" path ")")) desc)
3677 (replace-match "%s" t t desc))
3678 (t desc))))
3680 (defun org-export-inline-image-p (link &optional rules)
3681 "Non-nil if LINK object points to an inline image.
3683 Optional argument is a set of RULES defining inline images. It
3684 is an alist where associations have the following shape:
3686 \(TYPE . REGEXP)
3688 Applying a rule means apply REGEXP against LINK's path when its
3689 type is TYPE. The function will return a non-nil value if any of
3690 the provided rules is non-nil. The default rule is
3691 `org-export-default-inline-image-rule'.
3693 This only applies to links without a description."
3694 (and (not (org-element-contents link))
3695 (let ((case-fold-search t)
3696 (rules (or rules org-export-default-inline-image-rule)))
3697 (catch 'exit
3698 (mapc
3699 (lambda (rule)
3700 (and (string= (org-element-property :type link) (car rule))
3701 (string-match (cdr rule)
3702 (org-element-property :path link))
3703 (throw 'exit t)))
3704 rules)
3705 ;; Return nil if no rule matched.
3706 nil))))
3708 (defun org-export-resolve-coderef (ref info)
3709 "Resolve a code reference REF.
3711 INFO is a plist used as a communication channel.
3713 Return associated line number in source code, or REF itself,
3714 depending on src-block or example element's switches."
3715 (org-element-map (plist-get info :parse-tree) '(example-block src-block)
3716 (lambda (el)
3717 (with-temp-buffer
3718 (insert (org-trim (org-element-property :value el)))
3719 (let* ((label-fmt (regexp-quote
3720 (or (org-element-property :label-fmt el)
3721 org-coderef-label-format)))
3722 (ref-re
3723 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3724 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3725 ;; Element containing REF is found. Resolve it to either
3726 ;; a label or a line number, as needed.
3727 (when (re-search-backward ref-re nil t)
3728 (cond
3729 ((org-element-property :use-labels el) ref)
3730 ((eq (org-element-property :number-lines el) 'continued)
3731 (+ (org-export-get-loc el info) (line-number-at-pos)))
3732 (t (line-number-at-pos)))))))
3733 info 'first-match))
3735 (defun org-export-resolve-fuzzy-link (link info)
3736 "Return LINK destination.
3738 INFO is a plist holding contextual information.
3740 Return value can be an object, an element, or nil:
3742 - If LINK path matches a target object (i.e. <<path>>) or
3743 element (i.e. \"#+TARGET: path\"), return it.
3745 - If LINK path exactly matches the name affiliated keyword
3746 \(i.e. #+NAME: path) of an element, return that element.
3748 - If LINK path exactly matches any headline name, return that
3749 element. If more than one headline share that name, priority
3750 will be given to the one with the closest common ancestor, if
3751 any, or the first one in the parse tree otherwise.
3753 - Otherwise, return nil.
3755 Assume LINK type is \"fuzzy\". White spaces are not
3756 significant."
3757 (let* ((raw-path (org-element-property :path link))
3758 (match-title-p (eq (aref raw-path 0) ?*))
3759 ;; Split PATH at white spaces so matches are space
3760 ;; insensitive.
3761 (path (org-split-string
3762 (if match-title-p (substring raw-path 1) raw-path))))
3763 (cond
3764 ;; First try to find a matching "<<path>>" unless user specified
3765 ;; he was looking for a headline (path starts with a *
3766 ;; character).
3767 ((and (not match-title-p)
3768 (org-element-map (plist-get info :parse-tree) '(keyword target)
3769 (lambda (blob)
3770 (and (or (eq (org-element-type blob) 'target)
3771 (equal (org-element-property :key blob) "TARGET"))
3772 (equal (org-split-string (org-element-property :value blob))
3773 path)
3774 blob))
3775 info t)))
3776 ;; Then try to find an element with a matching "#+NAME: path"
3777 ;; affiliated keyword.
3778 ((and (not match-title-p)
3779 (org-element-map (plist-get info :parse-tree)
3780 org-element-all-elements
3781 (lambda (el)
3782 (let ((name (org-element-property :name el)))
3783 (when (and name (equal (org-split-string name) path)) el)))
3784 info 'first-match)))
3785 ;; Last case: link either points to a headline or to
3786 ;; nothingness. Try to find the source, with priority given to
3787 ;; headlines with the closest common ancestor. If such candidate
3788 ;; is found, return it, otherwise return nil.
3790 (let ((find-headline
3791 (function
3792 ;; Return first headline whose `:raw-value' property is
3793 ;; NAME in parse tree DATA, or nil. Statistics cookies
3794 ;; are ignored.
3795 (lambda (name data)
3796 (org-element-map data 'headline
3797 (lambda (headline)
3798 (when (equal (org-split-string
3799 (replace-regexp-in-string
3800 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
3801 (org-element-property :raw-value headline)))
3802 name)
3803 headline))
3804 info 'first-match)))))
3805 ;; Search among headlines sharing an ancestor with link, from
3806 ;; closest to farthest.
3807 (or (catch 'exit
3808 (mapc
3809 (lambda (parent)
3810 (when (eq (org-element-type parent) 'headline)
3811 (let ((foundp (funcall find-headline path parent)))
3812 (when foundp (throw 'exit foundp)))))
3813 (org-export-get-genealogy link)) nil)
3814 ;; No match with a common ancestor: try full parse-tree.
3815 (funcall find-headline path (plist-get info :parse-tree))))))))
3817 (defun org-export-resolve-id-link (link info)
3818 "Return headline referenced as LINK destination.
3820 INFO is a plist used as a communication channel.
3822 Return value can be the headline element matched in current parse
3823 tree, a file name or nil. Assume LINK type is either \"id\" or
3824 \"custom-id\"."
3825 (let ((id (org-element-property :path link)))
3826 ;; First check if id is within the current parse tree.
3827 (or (org-element-map (plist-get info :parse-tree) 'headline
3828 (lambda (headline)
3829 (when (or (string= (org-element-property :ID headline) id)
3830 (string= (org-element-property :CUSTOM_ID headline) id))
3831 headline))
3832 info 'first-match)
3833 ;; Otherwise, look for external files.
3834 (cdr (assoc id (plist-get info :id-alist))))))
3836 (defun org-export-resolve-radio-link (link info)
3837 "Return radio-target object referenced as LINK destination.
3839 INFO is a plist used as a communication channel.
3841 Return value can be a radio-target object or nil. Assume LINK
3842 has type \"radio\"."
3843 (let ((path (org-element-property :path link)))
3844 (org-element-map (plist-get info :parse-tree) 'radio-target
3845 (lambda (radio)
3846 (and (compare-strings
3847 (org-element-property :value radio) 0 nil path 0 nil t)
3848 radio))
3849 info 'first-match)))
3852 ;;;; For References
3854 ;; `org-export-get-ordinal' associates a sequence number to any object
3855 ;; or element.
3857 (defun org-export-get-ordinal (element info &optional types predicate)
3858 "Return ordinal number of an element or object.
3860 ELEMENT is the element or object considered. INFO is the plist
3861 used as a communication channel.
3863 Optional argument TYPES, when non-nil, is a list of element or
3864 object types, as symbols, that should also be counted in.
3865 Otherwise, only provided element's type is considered.
3867 Optional argument PREDICATE is a function returning a non-nil
3868 value if the current element or object should be counted in. It
3869 accepts two arguments: the element or object being considered and
3870 the plist used as a communication channel. This allows to count
3871 only a certain type of objects (i.e. inline images).
3873 Return value is a list of numbers if ELEMENT is a headline or an
3874 item. It is nil for keywords. It represents the footnote number
3875 for footnote definitions and footnote references. If ELEMENT is
3876 a target, return the same value as if ELEMENT was the closest
3877 table, item or headline containing the target. In any other
3878 case, return the sequence number of ELEMENT among elements or
3879 objects of the same type."
3880 ;; A target keyword, representing an invisible target, never has
3881 ;; a sequence number.
3882 (unless (eq (org-element-type element) 'keyword)
3883 ;; Ordinal of a target object refer to the ordinal of the closest
3884 ;; table, item, or headline containing the object.
3885 (when (eq (org-element-type element) 'target)
3886 (setq element
3887 (loop for parent in (org-export-get-genealogy element)
3888 when
3889 (memq
3890 (org-element-type parent)
3891 '(footnote-definition footnote-reference headline item
3892 table))
3893 return parent)))
3894 (case (org-element-type element)
3895 ;; Special case 1: A headline returns its number as a list.
3896 (headline (org-export-get-headline-number element info))
3897 ;; Special case 2: An item returns its number as a list.
3898 (item (let ((struct (org-element-property :structure element)))
3899 (org-list-get-item-number
3900 (org-element-property :begin element)
3901 struct
3902 (org-list-prevs-alist struct)
3903 (org-list-parents-alist struct))))
3904 ((footnote-definition footnote-reference)
3905 (org-export-get-footnote-number element info))
3906 (otherwise
3907 (let ((counter 0))
3908 ;; Increment counter until ELEMENT is found again.
3909 (org-element-map (plist-get info :parse-tree)
3910 (or types (org-element-type element))
3911 (lambda (el)
3912 (cond
3913 ((eq element el) (1+ counter))
3914 ((not predicate) (incf counter) nil)
3915 ((funcall predicate el info) (incf counter) nil)))
3916 info 'first-match))))))
3919 ;;;; For Src-Blocks
3921 ;; `org-export-get-loc' counts number of code lines accumulated in
3922 ;; src-block or example-block elements with a "+n" switch until
3923 ;; a given element, excluded. Note: "-n" switches reset that count.
3925 ;; `org-export-unravel-code' extracts source code (along with a code
3926 ;; references alist) from an `element-block' or `src-block' type
3927 ;; element.
3929 ;; `org-export-format-code' applies a formatting function to each line
3930 ;; of code, providing relative line number and code reference when
3931 ;; appropriate. Since it doesn't access the original element from
3932 ;; which the source code is coming, it expects from the code calling
3933 ;; it to know if lines should be numbered and if code references
3934 ;; should appear.
3936 ;; Eventually, `org-export-format-code-default' is a higher-level
3937 ;; function (it makes use of the two previous functions) which handles
3938 ;; line numbering and code references inclusion, and returns source
3939 ;; code in a format suitable for plain text or verbatim output.
3941 (defun org-export-get-loc (element info)
3942 "Return accumulated lines of code up to ELEMENT.
3944 INFO is the plist used as a communication channel.
3946 ELEMENT is excluded from count."
3947 (let ((loc 0))
3948 (org-element-map (plist-get info :parse-tree)
3949 `(src-block example-block ,(org-element-type element))
3950 (lambda (el)
3951 (cond
3952 ;; ELEMENT is reached: Quit the loop.
3953 ((eq el element))
3954 ;; Only count lines from src-block and example-block elements
3955 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3956 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3957 ((let ((linums (org-element-property :number-lines el)))
3958 (when linums
3959 ;; Accumulate locs or reset them.
3960 (let ((lines (org-count-lines
3961 (org-trim (org-element-property :value el)))))
3962 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3963 ;; Return nil to stay in the loop.
3964 nil)))
3965 info 'first-match)
3966 ;; Return value.
3967 loc))
3969 (defun org-export-unravel-code (element)
3970 "Clean source code and extract references out of it.
3972 ELEMENT has either a `src-block' an `example-block' type.
3974 Return a cons cell whose CAR is the source code, cleaned from any
3975 reference and protective comma and CDR is an alist between
3976 relative line number (integer) and name of code reference on that
3977 line (string)."
3978 (let* ((line 0) refs
3979 ;; Get code and clean it. Remove blank lines at its
3980 ;; beginning and end.
3981 (code (let ((c (replace-regexp-in-string
3982 "\\`\\([ \t]*\n\\)+" ""
3983 (replace-regexp-in-string
3984 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
3985 (org-element-property :value element)))))
3986 ;; If appropriate, remove global indentation.
3987 (if (or org-src-preserve-indentation
3988 (org-element-property :preserve-indent element))
3990 (org-remove-indentation c))))
3991 ;; Get format used for references.
3992 (label-fmt (regexp-quote
3993 (or (org-element-property :label-fmt element)
3994 org-coderef-label-format)))
3995 ;; Build a regexp matching a loc with a reference.
3996 (with-ref-re
3997 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3998 (replace-regexp-in-string
3999 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
4000 ;; Return value.
4001 (cons
4002 ;; Code with references removed.
4003 (org-element-normalize-string
4004 (mapconcat
4005 (lambda (loc)
4006 (incf line)
4007 (if (not (string-match with-ref-re loc)) loc
4008 ;; Ref line: remove ref, and signal its position in REFS.
4009 (push (cons line (match-string 3 loc)) refs)
4010 (replace-match "" nil nil loc 1)))
4011 (org-split-string code "\n") "\n"))
4012 ;; Reference alist.
4013 refs)))
4015 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4016 "Format CODE by applying FUN line-wise and return it.
4018 CODE is a string representing the code to format. FUN is
4019 a function. It must accept three arguments: a line of
4020 code (string), the current line number (integer) or nil and the
4021 reference associated to the current line (string) or nil.
4023 Optional argument NUM-LINES can be an integer representing the
4024 number of code lines accumulated until the current code. Line
4025 numbers passed to FUN will take it into account. If it is nil,
4026 FUN's second argument will always be nil. This number can be
4027 obtained with `org-export-get-loc' function.
4029 Optional argument REF-ALIST can be an alist between relative line
4030 number (i.e. ignoring NUM-LINES) and the name of the code
4031 reference on it. If it is nil, FUN's third argument will always
4032 be nil. It can be obtained through the use of
4033 `org-export-unravel-code' function."
4034 (let ((--locs (org-split-string code "\n"))
4035 (--line 0))
4036 (org-element-normalize-string
4037 (mapconcat
4038 (lambda (--loc)
4039 (incf --line)
4040 (let ((--ref (cdr (assq --line ref-alist))))
4041 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4042 --locs "\n"))))
4044 (defun org-export-format-code-default (element info)
4045 "Return source code from ELEMENT, formatted in a standard way.
4047 ELEMENT is either a `src-block' or `example-block' element. INFO
4048 is a plist used as a communication channel.
4050 This function takes care of line numbering and code references
4051 inclusion. Line numbers, when applicable, appear at the
4052 beginning of the line, separated from the code by two white
4053 spaces. Code references, on the other hand, appear flushed to
4054 the right, separated by six white spaces from the widest line of
4055 code."
4056 ;; Extract code and references.
4057 (let* ((code-info (org-export-unravel-code element))
4058 (code (car code-info))
4059 (code-lines (org-split-string code "\n")))
4060 (if (null code-lines) ""
4061 (let* ((refs (and (org-element-property :retain-labels element)
4062 (cdr code-info)))
4063 ;; Handle line numbering.
4064 (num-start (case (org-element-property :number-lines element)
4065 (continued (org-export-get-loc element info))
4066 (new 0)))
4067 (num-fmt
4068 (and num-start
4069 (format "%%%ds "
4070 (length (number-to-string
4071 (+ (length code-lines) num-start))))))
4072 ;; Prepare references display, if required. Any reference
4073 ;; should start six columns after the widest line of code,
4074 ;; wrapped with parenthesis.
4075 (max-width
4076 (+ (apply 'max (mapcar 'length code-lines))
4077 (if (not num-start) 0 (length (format num-fmt num-start))))))
4078 (org-export-format-code
4079 code
4080 (lambda (loc line-num ref)
4081 (let ((number-str (and num-fmt (format num-fmt line-num))))
4082 (concat
4083 number-str
4085 (and ref
4086 (concat (make-string
4087 (- (+ 6 max-width)
4088 (+ (length loc) (length number-str))) ? )
4089 (format "(%s)" ref))))))
4090 num-start refs)))))
4093 ;;;; For Tables
4095 ;; `org-export-table-has-special-column-p' and and
4096 ;; `org-export-table-row-is-special-p' are predicates used to look for
4097 ;; meta-information about the table structure.
4099 ;; `org-table-has-header-p' tells when the rows before the first rule
4100 ;; should be considered as table's header.
4102 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4103 ;; and `org-export-table-cell-borders' extract information from
4104 ;; a table-cell element.
4106 ;; `org-export-table-dimensions' gives the number on rows and columns
4107 ;; in the table, ignoring horizontal rules and special columns.
4108 ;; `org-export-table-cell-address', given a table-cell object, returns
4109 ;; the absolute address of a cell. On the other hand,
4110 ;; `org-export-get-table-cell-at' does the contrary.
4112 ;; `org-export-table-cell-starts-colgroup-p',
4113 ;; `org-export-table-cell-ends-colgroup-p',
4114 ;; `org-export-table-row-starts-rowgroup-p',
4115 ;; `org-export-table-row-ends-rowgroup-p',
4116 ;; `org-export-table-row-starts-header-p' and
4117 ;; `org-export-table-row-ends-header-p' indicate position of current
4118 ;; row or cell within the table.
4120 (defun org-export-table-has-special-column-p (table)
4121 "Non-nil when TABLE has a special column.
4122 All special columns will be ignored during export."
4123 ;; The table has a special column when every first cell of every row
4124 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4125 ;; "*" "_" and "^". Though, do not consider a first row containing
4126 ;; only empty cells as special.
4127 (let ((special-column-p 'empty))
4128 (catch 'exit
4129 (mapc
4130 (lambda (row)
4131 (when (eq (org-element-property :type row) 'standard)
4132 (let ((value (org-element-contents
4133 (car (org-element-contents row)))))
4134 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4135 (setq special-column-p 'special))
4136 ((not value))
4137 (t (throw 'exit nil))))))
4138 (org-element-contents table))
4139 (eq special-column-p 'special))))
4141 (defun org-export-table-has-header-p (table info)
4142 "Non-nil when TABLE has an header.
4144 INFO is a plist used as a communication channel.
4146 A table has an header when it contains at least two row groups."
4147 (let ((rowgroup 1) row-flag)
4148 (org-element-map table 'table-row
4149 (lambda (row)
4150 (cond
4151 ((> rowgroup 1) t)
4152 ((and row-flag (eq (org-element-property :type row) 'rule))
4153 (incf rowgroup) (setq row-flag nil))
4154 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
4155 (setq row-flag t) nil)))
4156 info)))
4158 (defun org-export-table-row-is-special-p (table-row info)
4159 "Non-nil if TABLE-ROW is considered special.
4161 INFO is a plist used as the communication channel.
4163 All special rows will be ignored during export."
4164 (when (eq (org-element-property :type table-row) 'standard)
4165 (let ((first-cell (org-element-contents
4166 (car (org-element-contents table-row)))))
4167 ;; A row is special either when...
4169 ;; ... it starts with a field only containing "/",
4170 (equal first-cell '("/"))
4171 ;; ... the table contains a special column and the row start
4172 ;; with a marking character among, "^", "_", "$" or "!",
4173 (and (org-export-table-has-special-column-p
4174 (org-export-get-parent table-row))
4175 (member first-cell '(("^") ("_") ("$") ("!"))))
4176 ;; ... it contains only alignment cookies and empty cells.
4177 (let ((special-row-p 'empty))
4178 (catch 'exit
4179 (mapc
4180 (lambda (cell)
4181 (let ((value (org-element-contents cell)))
4182 ;; Since VALUE is a secondary string, the following
4183 ;; checks avoid expanding it with `org-export-data'.
4184 (cond ((not value))
4185 ((and (not (cdr value))
4186 (stringp (car value))
4187 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4188 (car value)))
4189 (setq special-row-p 'cookie))
4190 (t (throw 'exit nil)))))
4191 (org-element-contents table-row))
4192 (eq special-row-p 'cookie)))))))
4194 (defun org-export-table-row-group (table-row info)
4195 "Return TABLE-ROW's group.
4197 INFO is a plist used as the communication channel.
4199 Return value is the group number, as an integer, or nil special
4200 rows and table rules. Group 1 is also table's header."
4201 (unless (or (eq (org-element-property :type table-row) 'rule)
4202 (org-export-table-row-is-special-p table-row info))
4203 (let ((group 0) row-flag)
4204 (catch 'found
4205 (mapc
4206 (lambda (row)
4207 (cond
4208 ((and (eq (org-element-property :type row) 'standard)
4209 (not (org-export-table-row-is-special-p row info)))
4210 (unless row-flag (incf group) (setq row-flag t)))
4211 ((eq (org-element-property :type row) 'rule)
4212 (setq row-flag nil)))
4213 (when (eq table-row row) (throw 'found group)))
4214 (org-element-contents (org-export-get-parent table-row)))))))
4216 (defun org-export-table-cell-width (table-cell info)
4217 "Return TABLE-CELL contents width.
4219 INFO is a plist used as the communication channel.
4221 Return value is the width given by the last width cookie in the
4222 same column as TABLE-CELL, or nil."
4223 (let* ((row (org-export-get-parent table-cell))
4224 (column (let ((cells (org-element-contents row)))
4225 (- (length cells) (length (memq table-cell cells)))))
4226 (table (org-export-get-parent-table table-cell))
4227 cookie-width)
4228 (mapc
4229 (lambda (row)
4230 (cond
4231 ;; In a special row, try to find a width cookie at COLUMN.
4232 ((org-export-table-row-is-special-p row info)
4233 (let ((value (org-element-contents
4234 (elt (org-element-contents row) column))))
4235 ;; The following checks avoid expanding unnecessarily the
4236 ;; cell with `org-export-data'
4237 (when (and value
4238 (not (cdr value))
4239 (stringp (car value))
4240 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
4241 (match-string 1 (car value)))
4242 (setq cookie-width
4243 (string-to-number (match-string 1 (car value)))))))
4244 ;; Ignore table rules.
4245 ((eq (org-element-property :type row) 'rule))))
4246 (org-element-contents table))
4247 ;; Return value.
4248 cookie-width))
4250 (defun org-export-table-cell-alignment (table-cell info)
4251 "Return TABLE-CELL contents alignment.
4253 INFO is a plist used as the communication channel.
4255 Return alignment as specified by the last alignment cookie in the
4256 same column as TABLE-CELL. If no such cookie is found, a default
4257 alignment value will be deduced from fraction of numbers in the
4258 column (see `org-table-number-fraction' for more information).
4259 Possible values are `left', `right' and `center'."
4260 (let* ((row (org-export-get-parent table-cell))
4261 (column (let ((cells (org-element-contents row)))
4262 (- (length cells) (length (memq table-cell cells)))))
4263 (table (org-export-get-parent-table table-cell))
4264 (number-cells 0)
4265 (total-cells 0)
4266 cookie-align)
4267 (mapc
4268 (lambda (row)
4269 (cond
4270 ;; In a special row, try to find an alignment cookie at
4271 ;; COLUMN.
4272 ((org-export-table-row-is-special-p row info)
4273 (let ((value (org-element-contents
4274 (elt (org-element-contents row) column))))
4275 ;; Since VALUE is a secondary string, the following checks
4276 ;; avoid useless expansion through `org-export-data'.
4277 (when (and value
4278 (not (cdr value))
4279 (stringp (car value))
4280 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4281 (car value))
4282 (match-string 1 (car value)))
4283 (setq cookie-align (match-string 1 (car value))))))
4284 ;; Ignore table rules.
4285 ((eq (org-element-property :type row) 'rule))
4286 ;; In a standard row, check if cell's contents are expressing
4287 ;; some kind of number. Increase NUMBER-CELLS accordingly.
4288 ;; Though, don't bother if an alignment cookie has already
4289 ;; defined cell's alignment.
4290 ((not cookie-align)
4291 (let ((value (org-export-data
4292 (org-element-contents
4293 (elt (org-element-contents row) column))
4294 info)))
4295 (incf total-cells)
4296 (when (string-match org-table-number-regexp value)
4297 (incf number-cells))))))
4298 (org-element-contents table))
4299 ;; Return value. Alignment specified by cookies has precedence
4300 ;; over alignment deduced from cells contents.
4301 (cond ((equal cookie-align "l") 'left)
4302 ((equal cookie-align "r") 'right)
4303 ((equal cookie-align "c") 'center)
4304 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
4305 'right)
4306 (t 'left))))
4308 (defun org-export-table-cell-borders (table-cell info)
4309 "Return TABLE-CELL borders.
4311 INFO is a plist used as a communication channel.
4313 Return value is a list of symbols, or nil. Possible values are:
4314 `top', `bottom', `above', `below', `left' and `right'. Note:
4315 `top' (resp. `bottom') only happen for a cell in the first
4316 row (resp. last row) of the table, ignoring table rules, if any.
4318 Returned borders ignore special rows."
4319 (let* ((row (org-export-get-parent table-cell))
4320 (table (org-export-get-parent-table table-cell))
4321 borders)
4322 ;; Top/above border? TABLE-CELL has a border above when a rule
4323 ;; used to demarcate row groups can be found above. Hence,
4324 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4325 ;; another regular row has to be found above that rule.
4326 (let (rule-flag)
4327 (catch 'exit
4328 (mapc (lambda (row)
4329 (cond ((eq (org-element-property :type row) 'rule)
4330 (setq rule-flag t))
4331 ((not (org-export-table-row-is-special-p row info))
4332 (if rule-flag (throw 'exit (push 'above borders))
4333 (throw 'exit nil)))))
4334 ;; Look at every row before the current one.
4335 (cdr (memq row (reverse (org-element-contents table)))))
4336 ;; No rule above, or rule found starts the table (ignoring any
4337 ;; special row): TABLE-CELL is at the top of the table.
4338 (when rule-flag (push 'above borders))
4339 (push 'top borders)))
4340 ;; Bottom/below border? TABLE-CELL has a border below when next
4341 ;; non-regular row below is a rule.
4342 (let (rule-flag)
4343 (catch 'exit
4344 (mapc (lambda (row)
4345 (cond ((eq (org-element-property :type row) 'rule)
4346 (setq rule-flag t))
4347 ((not (org-export-table-row-is-special-p row info))
4348 (if rule-flag (throw 'exit (push 'below borders))
4349 (throw 'exit nil)))))
4350 ;; Look at every row after the current one.
4351 (cdr (memq row (org-element-contents table))))
4352 ;; No rule below, or rule found ends the table (modulo some
4353 ;; special row): TABLE-CELL is at the bottom of the table.
4354 (when rule-flag (push 'below borders))
4355 (push 'bottom borders)))
4356 ;; Right/left borders? They can only be specified by column
4357 ;; groups. Column groups are defined in a row starting with "/".
4358 ;; Also a column groups row only contains "<", "<>", ">" or blank
4359 ;; cells.
4360 (catch 'exit
4361 (let ((column (let ((cells (org-element-contents row)))
4362 (- (length cells) (length (memq table-cell cells))))))
4363 (mapc
4364 (lambda (row)
4365 (unless (eq (org-element-property :type row) 'rule)
4366 (when (equal (org-element-contents
4367 (car (org-element-contents row)))
4368 '("/"))
4369 (let ((column-groups
4370 (mapcar
4371 (lambda (cell)
4372 (let ((value (org-element-contents cell)))
4373 (when (member value '(("<") ("<>") (">") nil))
4374 (car value))))
4375 (org-element-contents row))))
4376 ;; There's a left border when previous cell, if
4377 ;; any, ends a group, or current one starts one.
4378 (when (or (and (not (zerop column))
4379 (member (elt column-groups (1- column))
4380 '(">" "<>")))
4381 (member (elt column-groups column) '("<" "<>")))
4382 (push 'left borders))
4383 ;; There's a right border when next cell, if any,
4384 ;; starts a group, or current one ends one.
4385 (when (or (and (/= (1+ column) (length column-groups))
4386 (member (elt column-groups (1+ column))
4387 '("<" "<>")))
4388 (member (elt column-groups column) '(">" "<>")))
4389 (push 'right borders))
4390 (throw 'exit nil)))))
4391 ;; Table rows are read in reverse order so last column groups
4392 ;; row has precedence over any previous one.
4393 (reverse (org-element-contents table)))))
4394 ;; Return value.
4395 borders))
4397 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4398 "Non-nil when TABLE-CELL is at the beginning of a row group.
4399 INFO is a plist used as a communication channel."
4400 ;; A cell starts a column group either when it is at the beginning
4401 ;; of a row (or after the special column, if any) or when it has
4402 ;; a left border.
4403 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
4404 'identity info 'first-match)
4405 table-cell)
4406 (memq 'left (org-export-table-cell-borders table-cell info))))
4408 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4409 "Non-nil when TABLE-CELL is at the end of a row group.
4410 INFO is a plist used as a communication channel."
4411 ;; A cell ends a column group either when it is at the end of a row
4412 ;; or when it has a right border.
4413 (or (eq (car (last (org-element-contents
4414 (org-export-get-parent table-cell))))
4415 table-cell)
4416 (memq 'right (org-export-table-cell-borders table-cell info))))
4418 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4419 "Non-nil when TABLE-ROW is at the beginning of a column group.
4420 INFO is a plist used as a communication channel."
4421 (unless (or (eq (org-element-property :type table-row) 'rule)
4422 (org-export-table-row-is-special-p table-row info))
4423 (let ((borders (org-export-table-cell-borders
4424 (car (org-element-contents table-row)) info)))
4425 (or (memq 'top borders) (memq 'above borders)))))
4427 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4428 "Non-nil when TABLE-ROW is at the end of a column group.
4429 INFO is a plist used as a communication channel."
4430 (unless (or (eq (org-element-property :type table-row) 'rule)
4431 (org-export-table-row-is-special-p table-row info))
4432 (let ((borders (org-export-table-cell-borders
4433 (car (org-element-contents table-row)) info)))
4434 (or (memq 'bottom borders) (memq 'below borders)))))
4436 (defun org-export-table-row-starts-header-p (table-row info)
4437 "Non-nil when TABLE-ROW is the first table header's row.
4438 INFO is a plist used as a communication channel."
4439 (and (org-export-table-has-header-p
4440 (org-export-get-parent-table table-row) info)
4441 (org-export-table-row-starts-rowgroup-p table-row info)
4442 (= (org-export-table-row-group table-row info) 1)))
4444 (defun org-export-table-row-ends-header-p (table-row info)
4445 "Non-nil when TABLE-ROW is the last table header's row.
4446 INFO is a plist used as a communication channel."
4447 (and (org-export-table-has-header-p
4448 (org-export-get-parent-table table-row) info)
4449 (org-export-table-row-ends-rowgroup-p table-row info)
4450 (= (org-export-table-row-group table-row info) 1)))
4452 (defun org-export-table-dimensions (table info)
4453 "Return TABLE dimensions.
4455 INFO is a plist used as a communication channel.
4457 Return value is a CONS like (ROWS . COLUMNS) where
4458 ROWS (resp. COLUMNS) is the number of exportable
4459 rows (resp. columns)."
4460 (let (first-row (columns 0) (rows 0))
4461 ;; Set number of rows, and extract first one.
4462 (org-element-map table 'table-row
4463 (lambda (row)
4464 (when (eq (org-element-property :type row) 'standard)
4465 (incf rows)
4466 (unless first-row (setq first-row row)))) info)
4467 ;; Set number of columns.
4468 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4469 ;; Return value.
4470 (cons rows columns)))
4472 (defun org-export-table-cell-address (table-cell info)
4473 "Return address of a regular TABLE-CELL object.
4475 TABLE-CELL is the cell considered. INFO is a plist used as
4476 a communication channel.
4478 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4479 zero-based index. Only exportable cells are considered. The
4480 function returns nil for other cells."
4481 (let* ((table-row (org-export-get-parent table-cell))
4482 (table (org-export-get-parent-table table-cell)))
4483 ;; Ignore cells in special rows or in special column.
4484 (unless (or (org-export-table-row-is-special-p table-row info)
4485 (and (org-export-table-has-special-column-p table)
4486 (eq (car (org-element-contents table-row)) table-cell)))
4487 (cons
4488 ;; Row number.
4489 (let ((row-count 0))
4490 (org-element-map table 'table-row
4491 (lambda (row)
4492 (cond ((eq (org-element-property :type row) 'rule) nil)
4493 ((eq row table-row) row-count)
4494 (t (incf row-count) nil)))
4495 info 'first-match))
4496 ;; Column number.
4497 (let ((col-count 0))
4498 (org-element-map table-row 'table-cell
4499 (lambda (cell)
4500 (if (eq cell table-cell) col-count (incf col-count) nil))
4501 info 'first-match))))))
4503 (defun org-export-get-table-cell-at (address table info)
4504 "Return regular table-cell object at ADDRESS in TABLE.
4506 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4507 zero-based index. TABLE is a table type element. INFO is
4508 a plist used as a communication channel.
4510 If no table-cell, among exportable cells, is found at ADDRESS,
4511 return nil."
4512 (let ((column-pos (cdr address)) (column-count 0))
4513 (org-element-map
4514 ;; Row at (car address) or nil.
4515 (let ((row-pos (car address)) (row-count 0))
4516 (org-element-map table 'table-row
4517 (lambda (row)
4518 (cond ((eq (org-element-property :type row) 'rule) nil)
4519 ((= row-count row-pos) row)
4520 (t (incf row-count) nil)))
4521 info 'first-match))
4522 'table-cell
4523 (lambda (cell)
4524 (if (= column-count column-pos) cell
4525 (incf column-count) nil))
4526 info 'first-match)))
4529 ;;;; For Tables Of Contents
4531 ;; `org-export-collect-headlines' builds a list of all exportable
4532 ;; headline elements, maybe limited to a certain depth. One can then
4533 ;; easily parse it and transcode it.
4535 ;; Building lists of tables, figures or listings is quite similar.
4536 ;; Once the generic function `org-export-collect-elements' is defined,
4537 ;; `org-export-collect-tables', `org-export-collect-figures' and
4538 ;; `org-export-collect-listings' can be derived from it.
4540 (defun org-export-collect-headlines (info &optional n)
4541 "Collect headlines in order to build a table of contents.
4543 INFO is a plist used as a communication channel.
4545 When optional argument N is an integer, it specifies the depth of
4546 the table of contents. Otherwise, it is set to the value of the
4547 last headline level. See `org-export-headline-levels' for more
4548 information.
4550 Return a list of all exportable headlines as parsed elements.
4551 Footnote sections, if any, will be ignored."
4552 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4553 (org-element-map (plist-get info :parse-tree) 'headline
4554 (lambda (headline)
4555 (unless (org-element-property :footnote-section-p headline)
4556 ;; Strip contents from HEADLINE.
4557 (let ((relative-level (org-export-get-relative-level headline info)))
4558 (unless (> relative-level n) headline))))
4559 info))
4561 (defun org-export-collect-elements (type info &optional predicate)
4562 "Collect referenceable elements of a determined type.
4564 TYPE can be a symbol or a list of symbols specifying element
4565 types to search. Only elements with a caption are collected.
4567 INFO is a plist used as a communication channel.
4569 When non-nil, optional argument PREDICATE is a function accepting
4570 one argument, an element of type TYPE. It returns a non-nil
4571 value when that element should be collected.
4573 Return a list of all elements found, in order of appearance."
4574 (org-element-map (plist-get info :parse-tree) type
4575 (lambda (element)
4576 (and (org-element-property :caption element)
4577 (or (not predicate) (funcall predicate element))
4578 element))
4579 info))
4581 (defun org-export-collect-tables (info)
4582 "Build a list of tables.
4583 INFO is a plist used as a communication channel.
4585 Return a list of table elements with a caption."
4586 (org-export-collect-elements 'table info))
4588 (defun org-export-collect-figures (info predicate)
4589 "Build a list of figures.
4591 INFO is a plist used as a communication channel. PREDICATE is
4592 a function which accepts one argument: a paragraph element and
4593 whose return value is non-nil when that element should be
4594 collected.
4596 A figure is a paragraph type element, with a caption, verifying
4597 PREDICATE. The latter has to be provided since a \"figure\" is
4598 a vague concept that may depend on back-end.
4600 Return a list of elements recognized as figures."
4601 (org-export-collect-elements 'paragraph info predicate))
4603 (defun org-export-collect-listings (info)
4604 "Build a list of src blocks.
4606 INFO is a plist used as a communication channel.
4608 Return a list of src-block elements with a caption."
4609 (org-export-collect-elements 'src-block info))
4612 ;;;; Smart Quotes
4614 ;; The main function for the smart quotes sub-system is
4615 ;; `org-export-activate-smart-quotes', which replaces every quote in
4616 ;; a given string from the parse tree with its "smart" counterpart.
4618 ;; Dictionary for smart quotes is stored in
4619 ;; `org-export-smart-quotes-alist'.
4621 ;; Internally, regexps matching potential smart quotes (checks at
4622 ;; string boundaries are also necessary) are defined in
4623 ;; `org-export-smart-quotes-regexps'.
4625 (defconst org-export-smart-quotes-alist
4626 '(("de"
4627 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4628 :texinfo "@quotedblbase{}")
4629 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4630 :texinfo "@quotedblleft{}")
4631 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4632 :texinfo "@quotesinglbase{}")
4633 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4634 :texinfo "@quoteleft{}")
4635 (apostrophe :utf-8 "’" :html "&rsquo;"))
4636 ("en"
4637 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4638 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4639 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4640 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4641 (apostrophe :utf-8 "’" :html "&rsquo;"))
4642 ("es"
4643 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4644 :texinfo "@guillemetleft{}")
4645 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4646 :texinfo "@guillemetright{}")
4647 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4648 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4649 (apostrophe :utf-8 "’" :html "&rsquo;"))
4650 ("fr"
4651 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4652 :texinfo "@guillemetleft{}@tie{}")
4653 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4654 :texinfo "@tie{}@guillemetright{}")
4655 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4656 :texinfo "@guillemetleft{}@tie{}")
4657 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4658 :texinfo "@tie{}@guillemetright{}")
4659 (apostrophe :utf-8 "’" :html "&rsquo;")))
4660 "Smart quotes translations.
4662 Alist whose CAR is a language string and CDR is an alist with
4663 quote type as key and a plist associating various encodings to
4664 their translation as value.
4666 A quote type can be any symbol among `opening-double-quote',
4667 `closing-double-quote', `opening-single-quote',
4668 `closing-single-quote' and `apostrophe'.
4670 Valid encodings include `:utf-8', `:html', `:latex' and
4671 `:texinfo'.
4673 If no translation is found, the quote character is left as-is.")
4675 (defconst org-export-smart-quotes-regexps
4676 (list
4677 ;; Possible opening quote at beginning of string.
4678 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4679 ;; Possible closing quote at beginning of string.
4680 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4681 ;; Possible apostrophe at beginning of string.
4682 "\\`\\('\\)\\S-"
4683 ;; Opening single and double quotes.
4684 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4685 ;; Closing single and double quotes.
4686 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4687 ;; Apostrophe.
4688 "\\S-\\('\\)\\S-"
4689 ;; Possible opening quote at end of string.
4690 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4691 ;; Possible closing quote at end of string.
4692 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4693 ;; Possible apostrophe at end of string.
4694 "\\S-\\('\\)\\'")
4695 "List of regexps matching a quote or an apostrophe.
4696 In every regexp, quote or apostrophe matched is put in group 1.")
4698 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4699 "Replace regular quotes with \"smart\" quotes in string S.
4701 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4702 `:utf-8'. INFO is a plist used as a communication channel.
4704 The function has to retrieve information about string
4705 surroundings in parse tree. It can only happen with an
4706 unmodified string. Thus, if S has already been through another
4707 process, a non-nil ORIGINAL optional argument will provide that
4708 original string.
4710 Return the new string."
4711 (if (equal s "") ""
4712 (let* ((prev (org-export-get-previous-element (or original s) info))
4713 ;; Try to be flexible when computing number of blanks
4714 ;; before object. The previous object may be a string
4715 ;; introduced by the back-end and not completely parsed.
4716 (pre-blank (and prev
4717 (or (org-element-property :post-blank prev)
4718 ;; A string with missing `:post-blank'
4719 ;; property.
4720 (and (stringp prev)
4721 (string-match " *\\'" prev)
4722 (length (match-string 0 prev)))
4723 ;; Fallback value.
4724 0)))
4725 (next (org-export-get-next-element (or original s) info))
4726 (get-smart-quote
4727 (lambda (q type)
4728 ;; Return smart quote associated to a give quote Q, as
4729 ;; a string. TYPE is a symbol among `open', `close' and
4730 ;; `apostrophe'.
4731 (let ((key (case type
4732 (apostrophe 'apostrophe)
4733 (open (if (equal "'" q) 'opening-single-quote
4734 'opening-double-quote))
4735 (otherwise (if (equal "'" q) 'closing-single-quote
4736 'closing-double-quote)))))
4737 (or (plist-get
4738 (cdr (assq key
4739 (cdr (assoc (plist-get info :language)
4740 org-export-smart-quotes-alist))))
4741 encoding)
4742 q)))))
4743 (if (or (equal "\"" s) (equal "'" s))
4744 ;; Only a quote: no regexp can match. We have to check both
4745 ;; sides and decide what to do.
4746 (cond ((and (not prev) (not next)) s)
4747 ((not prev) (funcall get-smart-quote s 'open))
4748 ((and (not next) (zerop pre-blank))
4749 (funcall get-smart-quote s 'close))
4750 ((not next) s)
4751 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
4752 (t (funcall get-smart-quote 'open)))
4753 ;; 1. Replace quote character at the beginning of S.
4754 (cond
4755 ;; Apostrophe?
4756 ((and prev (zerop pre-blank)
4757 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4758 (setq s (replace-match
4759 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4760 nil t s 1)))
4761 ;; Closing quote?
4762 ((and prev (zerop pre-blank)
4763 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4764 (setq s (replace-match
4765 (funcall get-smart-quote (match-string 1 s) 'close)
4766 nil t s 1)))
4767 ;; Opening quote?
4768 ((and (or (not prev) (> pre-blank 0))
4769 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4770 (setq s (replace-match
4771 (funcall get-smart-quote (match-string 1 s) 'open)
4772 nil t s 1))))
4773 ;; 2. Replace quotes in the middle of the string.
4774 (setq s (replace-regexp-in-string
4775 ;; Opening quotes.
4776 (nth 3 org-export-smart-quotes-regexps)
4777 (lambda (text)
4778 (funcall get-smart-quote (match-string 1 text) 'open))
4779 s nil t 1))
4780 (setq s (replace-regexp-in-string
4781 ;; Closing quotes.
4782 (nth 4 org-export-smart-quotes-regexps)
4783 (lambda (text)
4784 (funcall get-smart-quote (match-string 1 text) 'close))
4785 s nil t 1))
4786 (setq s (replace-regexp-in-string
4787 ;; Apostrophes.
4788 (nth 5 org-export-smart-quotes-regexps)
4789 (lambda (text)
4790 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
4791 s nil t 1))
4792 ;; 3. Replace quote character at the end of S.
4793 (cond
4794 ;; Apostrophe?
4795 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4796 (setq s (replace-match
4797 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4798 nil t s 1)))
4799 ;; Closing quote?
4800 ((and (not next)
4801 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4802 (setq s (replace-match
4803 (funcall get-smart-quote (match-string 1 s) 'close)
4804 nil t s 1)))
4805 ;; Opening quote?
4806 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4807 (setq s (replace-match
4808 (funcall get-smart-quote (match-string 1 s) 'open)
4809 nil t s 1))))
4810 ;; Return string with smart quotes.
4811 s))))
4813 ;;;; Topology
4815 ;; Here are various functions to retrieve information about the
4816 ;; neighbourhood of a given element or object. Neighbours of interest
4817 ;; are direct parent (`org-export-get-parent'), parent headline
4818 ;; (`org-export-get-parent-headline'), first element containing an
4819 ;; object, (`org-export-get-parent-element'), parent table
4820 ;; (`org-export-get-parent-table'), previous element or object
4821 ;; (`org-export-get-previous-element') and next element or object
4822 ;; (`org-export-get-next-element').
4824 ;; `org-export-get-genealogy' returns the full genealogy of a given
4825 ;; element or object, from closest parent to full parse tree.
4827 (defun org-export-get-parent (blob)
4828 "Return BLOB parent or nil.
4829 BLOB is the element or object considered."
4830 (org-element-property :parent blob))
4832 (defun org-export-get-genealogy (blob)
4833 "Return full genealogy relative to a given element or object.
4835 BLOB is the element or object being considered.
4837 Ancestors are returned from closest to farthest, the last one
4838 being the full parse tree."
4839 (let (genealogy (parent blob))
4840 (while (setq parent (org-element-property :parent parent))
4841 (push parent genealogy))
4842 (nreverse genealogy)))
4844 (defun org-export-get-parent-headline (blob)
4845 "Return BLOB parent headline or nil.
4846 BLOB is the element or object being considered."
4847 (let ((parent blob))
4848 (while (and (setq parent (org-element-property :parent parent))
4849 (not (eq (org-element-type parent) 'headline))))
4850 parent))
4852 (defun org-export-get-parent-element (object)
4853 "Return first element containing OBJECT or nil.
4854 OBJECT is the object to consider."
4855 (let ((parent object))
4856 (while (and (setq parent (org-element-property :parent parent))
4857 (memq (org-element-type parent) org-element-all-objects)))
4858 parent))
4860 (defun org-export-get-parent-table (object)
4861 "Return OBJECT parent table or nil.
4862 OBJECT is either a `table-cell' or `table-element' type object."
4863 (let ((parent object))
4864 (while (and (setq parent (org-element-property :parent parent))
4865 (not (eq (org-element-type parent) 'table))))
4866 parent))
4868 (defun org-export-get-previous-element (blob info &optional n)
4869 "Return previous element or object.
4871 BLOB is an element or object. INFO is a plist used as
4872 a communication channel. Return previous exportable element or
4873 object, a string, or nil.
4875 When optional argument N is a positive integer, return a list
4876 containing up to N siblings before BLOB, from farthest to
4877 closest. With any other non-nil value, return a list containing
4878 all of them."
4879 (let ((siblings
4880 ;; An object can belong to the contents of its parent or
4881 ;; to a secondary string. We check the latter option
4882 ;; first.
4883 (let ((parent (org-export-get-parent blob)))
4884 (or (and (not (memq (org-element-type blob)
4885 org-element-all-elements))
4886 (let ((sec-value
4887 (org-element-property
4888 (cdr (assq (org-element-type parent)
4889 org-element-secondary-value-alist))
4890 parent)))
4891 (and (memq blob sec-value) sec-value)))
4892 (org-element-contents parent))))
4893 prev)
4894 (catch 'exit
4895 (mapc (lambda (obj)
4896 (cond ((memq obj (plist-get info :ignore-list)))
4897 ((null n) (throw 'exit obj))
4898 ((not (wholenump n)) (push obj prev))
4899 ((zerop n) (throw 'exit prev))
4900 (t (decf n) (push obj prev))))
4901 (cdr (memq blob (reverse siblings))))
4902 prev)))
4904 (defun org-export-get-next-element (blob info &optional n)
4905 "Return next element or object.
4907 BLOB is an element or object. INFO is a plist used as
4908 a communication channel. Return next exportable element or
4909 object, a string, or nil.
4911 When optional argument N is a positive integer, return a list
4912 containing up to N siblings after BLOB, from closest to farthest.
4913 With any other non-nil value, return a list containing all of
4914 them."
4915 (let ((siblings
4916 ;; An object can belong to the contents of its parent or to
4917 ;; a secondary string. We check the latter option first.
4918 (let ((parent (org-export-get-parent blob)))
4919 (or (and (not (memq (org-element-type blob)
4920 org-element-all-objects))
4921 (let ((sec-value
4922 (org-element-property
4923 (cdr (assq (org-element-type parent)
4924 org-element-secondary-value-alist))
4925 parent)))
4926 (cdr (memq blob sec-value))))
4927 (cdr (memq blob (org-element-contents parent))))))
4928 next)
4929 (catch 'exit
4930 (mapc (lambda (obj)
4931 (cond ((memq obj (plist-get info :ignore-list)))
4932 ((null n) (throw 'exit obj))
4933 ((not (wholenump n)) (push obj next))
4934 ((zerop n) (throw 'exit (nreverse next)))
4935 (t (decf n) (push obj next))))
4936 siblings)
4937 (nreverse next))))
4940 ;;;; Translation
4942 ;; `org-export-translate' translates a string according to language
4943 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4944 ;; variable and a specified charset. `org-export-dictionary' contains
4945 ;; the dictionary used for the translation.
4947 (defconst org-export-dictionary
4948 '(("Author"
4949 ("ca" :default "Autor")
4950 ("cs" :default "Autor")
4951 ("da" :default "Ophavsmand")
4952 ("de" :default "Autor")
4953 ("eo" :html "A&#365;toro")
4954 ("es" :default "Autor")
4955 ("fi" :html "Tekij&auml;")
4956 ("fr" :default "Auteur")
4957 ("hu" :default "Szerz&otilde;")
4958 ("is" :html "H&ouml;fundur")
4959 ("it" :default "Autore")
4960 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4961 ("nl" :default "Auteur")
4962 ("no" :default "Forfatter")
4963 ("nb" :default "Forfatter")
4964 ("nn" :default "Forfattar")
4965 ("pl" :default "Autor")
4966 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4967 ("sv" :html "F&ouml;rfattare")
4968 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4969 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4970 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4971 ("Date"
4972 ("ca" :default "Data")
4973 ("cs" :default "Datum")
4974 ("da" :default "Dato")
4975 ("de" :default "Datum")
4976 ("eo" :default "Dato")
4977 ("es" :default "Fecha")
4978 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4979 ("hu" :html "D&aacute;tum")
4980 ("is" :default "Dagsetning")
4981 ("it" :default "Data")
4982 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4983 ("nl" :default "Datum")
4984 ("no" :default "Dato")
4985 ("nb" :default "Dato")
4986 ("nn" :default "Dato")
4987 ("pl" :default "Data")
4988 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4989 ("sv" :default "Datum")
4990 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4991 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4992 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4993 ("Equation"
4994 ("fr" :ascii "Equation" :default "Équation"))
4995 ("Figure")
4996 ("Footnotes"
4997 ("ca" :html "Peus de p&agrave;gina")
4998 ("cs" :default "Pozn\xe1mky pod carou")
4999 ("da" :default "Fodnoter")
5000 ("de" :html "Fu&szlig;noten")
5001 ("eo" :default "Piednotoj")
5002 ("es" :html "Pies de p&aacute;gina")
5003 ("fi" :default "Alaviitteet")
5004 ("fr" :default "Notes de bas de page")
5005 ("hu" :html "L&aacute;bjegyzet")
5006 ("is" :html "Aftanm&aacute;lsgreinar")
5007 ("it" :html "Note a pi&egrave; di pagina")
5008 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
5009 ("nl" :default "Voetnoten")
5010 ("no" :default "Fotnoter")
5011 ("nb" :default "Fotnoter")
5012 ("nn" :default "Fotnotar")
5013 ("pl" :default "Przypis")
5014 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5015 ("sv" :default "Fotnoter")
5016 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5017 :utf-8 "Примітки")
5018 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5019 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5020 ("List of Listings"
5021 ("fr" :default "Liste des programmes"))
5022 ("List of Tables"
5023 ("fr" :default "Liste des tableaux"))
5024 ("Listing %d:"
5025 ("fr"
5026 :ascii "Programme %d :" :default "Programme nº %d :"
5027 :latin1 "Programme %d :"))
5028 ("Listing %d: %s"
5029 ("fr"
5030 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
5031 :latin1 "Programme %d : %s"))
5032 ("See section %s"
5033 ("fr" :default "cf. section %s"))
5034 ("Table %d:"
5035 ("fr"
5036 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
5037 ("Table %d: %s"
5038 ("fr"
5039 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
5040 :latin1 "Tableau %d : %s"))
5041 ("Table of Contents"
5042 ("ca" :html "&Iacute;ndex")
5043 ("cs" :default "Obsah")
5044 ("da" :default "Indhold")
5045 ("de" :default "Inhaltsverzeichnis")
5046 ("eo" :default "Enhavo")
5047 ("es" :html "&Iacute;ndice")
5048 ("fi" :html "Sis&auml;llysluettelo")
5049 ("fr" :ascii "Sommaire" :default "Table des matières")
5050 ("hu" :html "Tartalomjegyz&eacute;k")
5051 ("is" :default "Efnisyfirlit")
5052 ("it" :default "Indice")
5053 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
5054 ("nl" :default "Inhoudsopgave")
5055 ("no" :default "Innhold")
5056 ("nb" :default "Innhold")
5057 ("nn" :default "Innhald")
5058 ("pl" :html "Spis tre&#x015b;ci")
5059 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5060 :utf-8 "Содержание")
5061 ("sv" :html "Inneh&aring;ll")
5062 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5063 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5064 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5065 ("Unknown reference"
5066 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
5067 "Dictionary for export engine.
5069 Alist whose CAR is the string to translate and CDR is an alist
5070 whose CAR is the language string and CDR is a plist whose
5071 properties are possible charsets and values translated terms.
5073 It is used as a database for `org-export-translate'. Since this
5074 function returns the string as-is if no translation was found,
5075 the variable only needs to record values different from the
5076 entry.")
5078 (defun org-export-translate (s encoding info)
5079 "Translate string S according to language specification.
5081 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5082 and `:utf-8'. INFO is a plist used as a communication channel.
5084 Translation depends on `:language' property. Return the
5085 translated string. If no translation is found, try to fall back
5086 to `:default' encoding. If it fails, return S."
5087 (let* ((lang (plist-get info :language))
5088 (translations (cdr (assoc lang
5089 (cdr (assoc s org-export-dictionary))))))
5090 (or (plist-get translations encoding)
5091 (plist-get translations :default)
5092 s)))
5096 ;;; Asynchronous Export
5098 ;; `org-export-async-start' is the entry point for asynchronous
5099 ;; export. It recreates current buffer (including visibility,
5100 ;; narrowing and visited file) in an external Emacs process, and
5101 ;; evaluates a command there. It then applies a function on the
5102 ;; returned results in the current process.
5104 ;; Asynchronously generated results are never displayed directly.
5105 ;; Instead, they are stored in `org-export-stack-contents'. They can
5106 ;; then be retrieved by calling `org-export-stack'.
5108 ;; Export Stack is viewed through a dedicated major mode
5109 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5110 ;;`org-export-stack-delete', `org-export-stack-view' and
5111 ;;`org-export-stack-clear'.
5113 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5114 ;; It should used whenever `org-export-async-start' is called.
5116 (defmacro org-export-async-start (fun &rest body)
5117 "Call function FUN on the results returned by BODY evaluation.
5119 BODY evaluation happens in an asynchronous process, from a buffer
5120 which is an exact copy of the current one.
5122 Use `org-export-add-to-stack' in FUN in order to register results
5123 in the stack. Examples for, respectively a temporary buffer and
5124 a file are:
5126 \(org-export-async-start
5127 \(lambda (output)
5128 \(with-current-buffer (get-buffer-create \"*Org BACKEND Export*\")
5129 \(erase-buffer)
5130 \(insert output)
5131 \(goto-char (point-min))
5132 \(org-export-add-to-stack (current-buffer) 'backend)))
5133 `(org-export-as 'backend ,subtreep ,visible-only ,body-only ',ext-plist))
5137 \(org-export-async-start
5138 \(lambda (f) (org-export-add-to-stack f 'backend))
5139 `(expand-file-name
5140 \(org-export-to-file
5141 'backend ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))"
5142 (declare (indent 1) (debug t))
5143 (org-with-gensyms (process temp-file copy-fun proc-buffer handler)
5144 ;; Write the full sexp evaluating BODY in a copy of the current
5145 ;; buffer to a temporary file, as it may be too long for program
5146 ;; args in `start-process'.
5147 `(with-temp-message "Initializing asynchronous export process"
5148 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5149 (,temp-file (make-temp-file "org-export-process")))
5150 (with-temp-file ,temp-file
5151 (insert
5152 (format
5153 "%S"
5154 `(with-temp-buffer
5155 ,(when org-export-async-debug '(setq debug-on-error t))
5156 ;; Ignore `kill-emacs-hook' and code evaluation
5157 ;; queries from Babel as we need a truly
5158 ;; non-interactive process.
5159 (setq kill-emacs-hook nil
5160 org-babel-confirm-evaluate-answer-no t)
5161 ;; Initialize export framework in external process.
5162 (require 'ox)
5163 ;; Re-create current buffer there.
5164 (funcall ,,copy-fun)
5165 (restore-buffer-modified-p nil)
5166 ;; Sexp to evaluate in the buffer.
5167 (print (progn ,,@body))))))
5168 ;; Start external process.
5169 (let* ((process-connection-type nil)
5170 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5171 (,process
5172 (start-process
5173 "org-export-process" ,proc-buffer
5174 (expand-file-name invocation-name invocation-directory)
5175 "-Q" "--batch"
5176 "-l" org-export-async-init-file
5177 "-l" ,temp-file)))
5178 ;; Register running process in stack.
5179 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5180 ;; Set-up sentinel in order to catch results.
5181 (set-process-sentinel
5182 ,process
5183 (let ((handler ',fun))
5184 `(lambda (p status)
5185 (let ((proc-buffer (process-buffer p)))
5186 (when (eq (process-status p) 'exit)
5187 (unwind-protect
5188 (if (zerop (process-exit-status p))
5189 (unwind-protect
5190 (let ((results
5191 (with-current-buffer proc-buffer
5192 (goto-char (point-max))
5193 (backward-sexp)
5194 (read (current-buffer)))))
5195 (funcall ,handler results))
5196 (unless org-export-async-debug
5197 (and (get-buffer proc-buffer)
5198 (kill-buffer proc-buffer))))
5199 (org-export-add-to-stack proc-buffer nil p)
5200 (ding)
5201 (message "Process '%s' exited abnormally" p))
5202 (unless org-export-async-debug
5203 (delete-file ,,temp-file)))))))))))))
5205 (defun org-export-add-to-stack (source backend &optional process)
5206 "Add a new result to export stack if not present already.
5208 SOURCE is a buffer or a file name containing export results.
5209 BACKEND is a symbol representing export back-end used to generate
5212 Entries already pointing to SOURCE and unavailable entries are
5213 removed beforehand. Return the new stack."
5214 (setq org-export-stack-contents
5215 (cons (list source backend (or process (current-time)))
5216 (org-export-stack-remove source))))
5218 (defun org-export-stack ()
5219 "Menu for asynchronous export results and running processes."
5220 (interactive)
5221 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5222 (set-buffer buffer)
5223 (when (zerop (buffer-size)) (org-export-stack-mode))
5224 (org-export-stack-refresh)
5225 (pop-to-buffer buffer))
5226 (message "Type \"q\" to quit, \"?\" for help"))
5228 (defun org-export--stack-source-at-point ()
5229 "Return source from export results at point in stack."
5230 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5231 (if (not source) (error "Source unavailable, please refresh buffer")
5232 (let ((source-name (if (stringp source) source (buffer-name source))))
5233 (if (save-excursion
5234 (beginning-of-line)
5235 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5236 source
5237 ;; SOURCE is not consistent with current line. The stack
5238 ;; view is outdated.
5239 (error "Source unavailable; type `g' to update buffer"))))))
5241 (defun org-export-stack-clear ()
5242 "Remove all entries from export stack."
5243 (interactive)
5244 (setq org-export-stack-contents nil))
5246 (defun org-export-stack-refresh (&rest dummy)
5247 "Refresh the asynchronous export stack.
5248 DUMMY is ignored. Unavailable sources are removed from the list.
5249 Return the new stack."
5250 (let ((inhibit-read-only t))
5251 (org-preserve-lc
5252 (erase-buffer)
5253 (insert (concat
5254 (let ((counter 0))
5255 (mapconcat
5256 (lambda (entry)
5257 (let ((proc-p (processp (nth 2 entry))))
5258 (concat
5259 ;; Back-end.
5260 (format " %-12s " (or (nth 1 entry) ""))
5261 ;; Age.
5262 (let ((data (nth 2 entry)))
5263 (if proc-p (format " %6s " (process-status data))
5264 ;; Compute age of the results.
5265 (org-format-seconds
5266 "%4h:%.2m "
5267 (float-time (time-since data)))))
5268 ;; Source.
5269 (format " %s"
5270 (let ((source (car entry)))
5271 (if (stringp source) source
5272 (buffer-name source)))))))
5273 ;; Clear stack from exited processes, dead buffers or
5274 ;; non-existent files.
5275 (setq org-export-stack-contents
5276 (org-remove-if-not
5277 (lambda (el)
5278 (if (processp (nth 2 el))
5279 (buffer-live-p (process-buffer (nth 2 el)))
5280 (let ((source (car el)))
5281 (if (bufferp source) (buffer-live-p source)
5282 (file-exists-p source)))))
5283 org-export-stack-contents)) "\n")))))))
5285 (defun org-export-stack-remove (&optional source)
5286 "Remove export results at point from stack.
5287 If optional argument SOURCE is non-nil, remove it instead."
5288 (interactive)
5289 (let ((source (or source (org-export--stack-source-at-point))))
5290 (setq org-export-stack-contents
5291 (org-remove-if (lambda (el) (equal (car el) source))
5292 org-export-stack-contents))))
5294 (defun org-export-stack-view (&optional in-emacs)
5295 "View export results at point in stack.
5296 With an optional prefix argument IN-EMACS, force viewing files
5297 within Emacs."
5298 (interactive "P")
5299 (let ((source (org-export--stack-source-at-point)))
5300 (cond ((processp source)
5301 (org-switch-to-buffer-other-window (process-buffer source)))
5302 ((bufferp source) (org-switch-to-buffer-other-window source))
5303 (t (org-open-file source in-emacs)))))
5305 (defconst org-export-stack-mode-map
5306 (let ((km (make-sparse-keymap)))
5307 (define-key km " " 'next-line)
5308 (define-key km "n" 'next-line)
5309 (define-key km "\C-n" 'next-line)
5310 (define-key km [down] 'next-line)
5311 (define-key km "p" 'previous-line)
5312 (define-key km "\C-p" 'previous-line)
5313 (define-key km "\C-?" 'previous-line)
5314 (define-key km [up] 'previous-line)
5315 (define-key km "C" 'org-export-stack-clear)
5316 (define-key km "v" 'org-export-stack-view)
5317 (define-key km (kbd "RET") 'org-export-stack-view)
5318 (define-key km "d" 'org-export-stack-remove)
5320 "Keymap for Org Export Stack.")
5322 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5323 "Mode for displaying asynchronous export stack.
5325 Type \\[org-export-stack] to visualize the asynchronous export
5326 stack.
5328 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5329 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5330 stack completely.
5332 Removing entries in an Org Export Stack buffer doesn't affect
5333 files or buffers, only the display.
5335 \\{org-export-stack-mode-map}"
5336 (abbrev-mode 0)
5337 (auto-fill-mode 0)
5338 (setq buffer-read-only t
5339 buffer-undo-list t
5340 truncate-lines t
5341 header-line-format
5342 '(:eval
5343 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5344 (add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5345 (set (make-local-variable 'revert-buffer-function)
5346 'org-export-stack-refresh))
5350 ;;; The Dispatcher
5352 ;; `org-export-dispatch' is the standard interactive way to start an
5353 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5354 ;; for its interface, which, in turn, delegates response to key
5355 ;; pressed to `org-export--dispatch-action'.
5357 ;;;###autoload
5358 (defun org-export-dispatch (&optional arg)
5359 "Export dispatcher for Org mode.
5361 It provides an access to common export related tasks in a buffer.
5362 Its interface comes in two flavours: standard and expert.
5364 While both share the same set of bindings, only the former
5365 displays the valid keys associations in a dedicated buffer.
5366 Scrolling (resp. line-wise motion) in this buffer is done with
5367 SPC and DEL (resp. C-n and C-p) keys.
5369 Set variable `org-export-dispatch-use-expert-ui' to switch to one
5370 flavour or the other.
5372 When ARG is \\[universal-argument], repeat the last export action, with the same set
5373 of options used back then, on the current buffer.
5375 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
5376 (interactive "P")
5377 (let* ((input
5378 (cond ((equal arg '(16)) '(stack))
5379 ((and arg org-export-dispatch-last-action))
5380 (t (save-window-excursion
5381 (unwind-protect
5382 (progn
5383 ;; Remember where we are
5384 (move-marker org-export-dispatch-last-position
5385 (point)
5386 (org-base-buffer (current-buffer)))
5387 ;; Get and store an export command
5388 (setq org-export-dispatch-last-action
5389 (org-export--dispatch-ui
5390 (list org-export-initial-scope
5391 (and org-export-in-background 'async))
5393 org-export-dispatch-use-expert-ui)))
5394 (and (get-buffer "*Org Export Dispatcher*")
5395 (kill-buffer "*Org Export Dispatcher*")))))))
5396 (action (car input))
5397 (optns (cdr input)))
5398 (unless (memq 'subtree optns)
5399 (move-marker org-export-dispatch-last-position nil))
5400 (case action
5401 ;; First handle special hard-coded actions.
5402 (stack (org-export-stack))
5403 (publish-current-file
5404 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
5405 (publish-current-project
5406 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
5407 (publish-choose-project
5408 (org-publish (assoc (org-icompleting-read
5409 "Publish project: "
5410 org-publish-project-alist nil t)
5411 org-publish-project-alist)
5412 (memq 'force optns)
5413 (memq 'async optns)))
5414 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
5415 (otherwise
5416 (save-excursion
5417 (when arg
5418 ;; Repeating command, maybe move cursor
5419 ;; to restore subtree context
5420 (if (eq (marker-buffer org-export-dispatch-last-position)
5421 (org-base-buffer (current-buffer)))
5422 (goto-char org-export-dispatch-last-position)
5423 ;; We are in a differnet buffer, forget position
5424 (move-marker org-export-dispatch-last-position nil)))
5425 (funcall action
5426 ;; Return a symbol instead of a list to ease
5427 ;; asynchronous export macro use.
5428 (and (memq 'async optns) t)
5429 (and (memq 'subtree optns) t)
5430 (and (memq 'visible optns) t)
5431 (and (memq 'body optns) t)))))))
5433 (defun org-export--dispatch-ui (options first-key expertp)
5434 "Handle interface for `org-export-dispatch'.
5436 OPTIONS is a list containing current interactive options set for
5437 export. It can contain any of the following symbols:
5438 `body' toggles a body-only export
5439 `subtree' restricts export to current subtree
5440 `visible' restricts export to visible part of buffer.
5441 `force' force publishing files.
5442 `async' use asynchronous export process
5444 FIRST-KEY is the key pressed to select the first level menu. It
5445 is nil when this menu hasn't been selected yet.
5447 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5448 buffer is provided, but indications about currently active
5449 options are given in the prompt. Moreover, \[?] allows to switch
5450 back to standard interface."
5451 (let* ((fontify-key
5452 (lambda (key &optional access-key)
5453 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5454 ;; non-nil is the required first-level key to activate
5455 ;; KEY. When its value is t, activate KEY independently
5456 ;; on the first key, if any. A nil value means KEY will
5457 ;; only be activated at first level.
5458 (if (or (eq access-key t) (eq access-key first-key))
5459 (org-propertize key 'face 'org-warning)
5460 key)))
5461 (fontify-value
5462 (lambda (value)
5463 ;; Fontify VALUE string.
5464 (org-propertize value 'face 'font-lock-variable-name-face)))
5465 ;; Prepare menu entries by extracting them from
5466 ;; `org-export-registered-backends', and sorting them by
5467 ;; access key and by ordinal, if any.
5468 (backends
5469 (sort
5470 (sort
5471 (delq nil
5472 (mapcar
5473 (lambda (b)
5474 (let ((name (car b)))
5475 (catch 'ignored
5476 ;; Ignore any back-end belonging to
5477 ;; `org-export-invisible-backends' or derived
5478 ;; from one of them.
5479 (dolist (ignored org-export-invisible-backends)
5480 (when (org-export-derived-backend-p name ignored)
5481 (throw 'ignored nil)))
5482 (org-export-backend-menu name))))
5483 org-export-registered-backends))
5484 (lambda (a b)
5485 (let ((key-a (nth 1 a))
5486 (key-b (nth 1 b)))
5487 (cond ((and (numberp key-a) (numberp key-b))
5488 (< key-a key-b))
5489 ((numberp key-b) t)))))
5490 (lambda (a b) (< (car a) (car b)))))
5491 ;; Compute a list of allowed keys based on the first key
5492 ;; pressed, if any. Some keys (?^B, ?^V, ?^S, ?^F, ?^A
5493 ;; and ?q) are always available.
5494 (allowed-keys
5495 (nconc (list 2 22 19 6 1)
5496 (if (not first-key) (org-uniquify (mapcar 'car backends))
5497 (let (sub-menu)
5498 (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
5499 (when (eq (car backend) first-key)
5500 (setq sub-menu (append (nth 2 backend) sub-menu))))))
5501 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
5502 ((not first-key) (list ?P)))
5503 (list ?&)
5504 (when expertp (list ??))
5505 (list ?q)))
5506 ;; Build the help menu for standard UI.
5507 (help
5508 (unless expertp
5509 (concat
5510 ;; Options are hard-coded.
5511 (format "Options
5512 [%s] Body only: %s [%s] Visible only: %s
5513 [%s] Export scope: %s [%s] Force publishing: %s
5514 [%s] Async export: %s\n"
5515 (funcall fontify-key "C-b" t)
5516 (funcall fontify-value
5517 (if (memq 'body options) "On " "Off"))
5518 (funcall fontify-key "C-v" t)
5519 (funcall fontify-value
5520 (if (memq 'visible options) "On " "Off"))
5521 (funcall fontify-key "C-s" t)
5522 (funcall fontify-value
5523 (if (memq 'subtree options) "Subtree" "Buffer "))
5524 (funcall fontify-key "C-f" t)
5525 (funcall fontify-value
5526 (if (memq 'force options) "On " "Off"))
5527 (funcall fontify-key "C-a" t)
5528 (funcall fontify-value
5529 (if (memq 'async options) "On " "Off")))
5530 ;; Display registered back-end entries. When a key
5531 ;; appears for the second time, do not create another
5532 ;; entry, but append its sub-menu to existing menu.
5533 (let (last-key)
5534 (mapconcat
5535 (lambda (entry)
5536 (let ((top-key (car entry)))
5537 (concat
5538 (unless (eq top-key last-key)
5539 (setq last-key top-key)
5540 (format "\n[%s] %s\n"
5541 (funcall fontify-key (char-to-string top-key))
5542 (nth 1 entry)))
5543 (let ((sub-menu (nth 2 entry)))
5544 (unless (functionp sub-menu)
5545 ;; Split sub-menu into two columns.
5546 (let ((index -1))
5547 (concat
5548 (mapconcat
5549 (lambda (sub-entry)
5550 (incf index)
5551 (format
5552 (if (zerop (mod index 2)) " [%s] %-26s"
5553 "[%s] %s\n")
5554 (funcall fontify-key
5555 (char-to-string (car sub-entry))
5556 top-key)
5557 (nth 1 sub-entry)))
5558 sub-menu "")
5559 (when (zerop (mod index 2)) "\n"))))))))
5560 backends ""))
5561 ;; Publishing menu is hard-coded.
5562 (format "\n[%s] Publish
5563 [%s] Current file [%s] Current project
5564 [%s] Choose project [%s] All projects\n\n"
5565 (funcall fontify-key "P")
5566 (funcall fontify-key "f" ?P)
5567 (funcall fontify-key "p" ?P)
5568 (funcall fontify-key "x" ?P)
5569 (funcall fontify-key "a" ?P))
5570 (format "\[%s] Export stack\n" (funcall fontify-key "&" t))
5571 (format "\[%s] %s"
5572 (funcall fontify-key "q" t)
5573 (if first-key "Main menu" "Exit")))))
5574 ;; Build prompts for both standard and expert UI.
5575 (standard-prompt (unless expertp "Export command: "))
5576 (expert-prompt
5577 (when expertp
5578 (format
5579 "Export command (C-%s%s%s%s%s) [%s]: "
5580 (if (memq 'body options) (funcall fontify-key "b" t) "b")
5581 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
5582 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
5583 (if (memq 'force options) (funcall fontify-key "f" t) "f")
5584 (if (memq 'async options) (funcall fontify-key "a" t) "a")
5585 (mapconcat (lambda (k)
5586 ;; Strip control characters.
5587 (unless (< k 27) (char-to-string k)))
5588 allowed-keys "")))))
5589 ;; With expert UI, just read key with a fancy prompt. In standard
5590 ;; UI, display an intrusive help buffer.
5591 (if expertp
5592 (org-export--dispatch-action
5593 expert-prompt allowed-keys backends options first-key expertp)
5594 ;; At first call, create frame layout in order to display menu.
5595 (unless (get-buffer "*Org Export Dispatcher*")
5596 (delete-other-windows)
5597 (org-switch-to-buffer-other-window
5598 (get-buffer-create "*Org Export Dispatcher*"))
5599 (setq cursor-type nil
5600 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
5601 ;; Make sure that invisible cursor will not highlight square
5602 ;; brackets.
5603 (set-syntax-table (copy-syntax-table))
5604 (modify-syntax-entry ?\[ "w"))
5605 ;; At this point, the buffer containing the menu exists and is
5606 ;; visible in the current window. So, refresh it.
5607 (with-current-buffer "*Org Export Dispatcher*"
5608 ;; Refresh help. Maintain display continuity by re-visiting
5609 ;; previous window position.
5610 (let ((pos (window-start)))
5611 (erase-buffer)
5612 (insert help)
5613 (set-window-start nil pos)))
5614 (org-fit-window-to-buffer)
5615 (org-export--dispatch-action
5616 standard-prompt allowed-keys backends options first-key expertp))))
5618 (defun org-export--dispatch-action
5619 (prompt allowed-keys backends options first-key expertp)
5620 "Read a character from command input and act accordingly.
5622 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
5623 a list of characters available at a given step in the process.
5624 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
5625 EXPERTP are the same as defined in `org-export--dispatch-ui',
5626 which see.
5628 Toggle export options when required. Otherwise, return value is
5629 a list with action as CAR and a list of interactive export
5630 options as CDR."
5631 (let (key)
5632 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
5633 ;; C-p, SPC, DEL).
5634 (while (and (setq key (read-char-exclusive prompt))
5635 (not expertp)
5636 (memq key '(14 16 ?\s ?\d)))
5637 (case key
5638 (14 (if (not (pos-visible-in-window-p (point-max)))
5639 (ignore-errors (scroll-up-line))
5640 (message "End of buffer")
5641 (sit-for 1)))
5642 (16 (if (not (pos-visible-in-window-p (point-min)))
5643 (ignore-errors (scroll-down-line))
5644 (message "Beginning of buffer")
5645 (sit-for 1)))
5646 (?\s (if (not (pos-visible-in-window-p (point-max)))
5647 (scroll-up nil)
5648 (message "End of buffer")
5649 (sit-for 1)))
5650 (?\d (if (not (pos-visible-in-window-p (point-min)))
5651 (scroll-down nil)
5652 (message "Beginning of buffer")
5653 (sit-for 1)))))
5654 (cond
5655 ;; Ignore undefined associations.
5656 ((not (memq key allowed-keys))
5657 (ding)
5658 (unless expertp (message "Invalid key") (sit-for 1))
5659 (org-export--dispatch-ui options first-key expertp))
5660 ;; q key at first level aborts export. At second
5661 ;; level, cancel first key instead.
5662 ((eq key ?q) (if (not first-key) (error "Export aborted")
5663 (org-export--dispatch-ui options nil expertp)))
5664 ;; Help key: Switch back to standard interface if
5665 ;; expert UI was active.
5666 ((eq key ??) (org-export--dispatch-ui options first-key nil))
5667 ;; Switch to asynchronous export stack.
5668 ((eq key ?&) '(stack))
5669 ;; Toggle export options
5670 ;; C-b (2) C-v (22) C-s (19) C-f (6) C-a (1)
5671 ((memq key '(2 22 19 6 1))
5672 (org-export--dispatch-ui
5673 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
5674 (6 'force) (1 'async))))
5675 (if (memq option options) (remq option options)
5676 (cons option options)))
5677 first-key expertp))
5678 ;; Action selected: Send key and options back to
5679 ;; `org-export-dispatch'.
5680 ((or first-key (functionp (nth 2 (assq key backends))))
5681 (cons (cond
5682 ((not first-key) (nth 2 (assq key backends)))
5683 ;; Publishing actions are hard-coded. Send a special
5684 ;; signal to `org-export-dispatch'.
5685 ((eq first-key ?P)
5686 (case key
5687 (?f 'publish-current-file)
5688 (?p 'publish-current-project)
5689 (?x 'publish-choose-project)
5690 (?a 'publish-all)))
5691 ;; Return first action associated to FIRST-KEY + KEY
5692 ;; path. Indeed, derived backends can share the same
5693 ;; FIRST-KEY.
5694 (t (catch 'found
5695 (mapc (lambda (backend)
5696 (let ((match (assq key (nth 2 backend))))
5697 (when match (throw 'found (nth 2 match)))))
5698 (member (assq first-key backends) backends)))))
5699 options))
5700 ;; Otherwise, enter sub-menu.
5701 (t (org-export--dispatch-ui options key expertp)))))
5705 (provide 'ox)
5707 ;; Local variables:
5708 ;; generated-autoload-file: "org-loaddefs.el"
5709 ;; End:
5711 ;;; ox.el ends here