1 ;;; ox.el --- Export Framework for Org Mode -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2019 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; This library implements a generic export engine for Org, built on
26 ;; its syntactical parser: Org Elements.
28 ;; Besides that parser, the generic exporter is made of three distinct
31 ;; - The communication channel consists of a property list, which is
32 ;; created and updated during the process. Its use is to offer
33 ;; every piece of information, would it be about initial environment
34 ;; or contextual data, all in a single place.
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 functions is `org-export-as'. It returns the transcoded
48 ;; buffer as a string. Its derivatives are `org-export-to-buffer' and
49 ;; `org-export-to-file'.
51 ;; An export back-end is defined with `org-export-define-backend'.
52 ;; This function can also support specific buffer keywords, OPTION
53 ;; keyword's items and filters. Refer to function's documentation for
56 ;; If the new back-end shares most properties with another one,
57 ;; `org-export-define-derived-backend' can be used to simplify the
60 ;; Any back-end can define its own variables. Among them, those
61 ;; customizable should belong to the `org-export-BACKEND' group.
63 ;; Tools for common tasks across back-ends are implemented in the
64 ;; following part of the file.
66 ;; Eventually, a dispatcher (`org-export-dispatch') is provided in the
69 ;; See <https://orgmode.org/worg/dev/org-export-reference.html> for
77 (require 'org-element
)
79 (require 'tabulated-list
)
81 (declare-function org-src-coderef-format
"org-src" (&optional element
))
82 (declare-function org-src-coderef-regexp
"org-src" (fmt &optional label
))
83 (declare-function org-publish
"ox-publish" (project &optional force async
))
84 (declare-function org-publish-all
"ox-publish" (&optional force async
))
85 (declare-function org-publish-current-file
"ox-publish" (&optional force async
))
86 (declare-function org-publish-current-project
"ox-publish" (&optional force async
))
88 (defvar org-publish-project-alist
)
89 (defvar org-table-number-fraction
)
90 (defvar org-table-number-regexp
)
93 ;;; Internal Variables
95 ;; Among internal variables, the most important is
96 ;; `org-export-options-alist'. This variable define the global export
97 ;; options, shared between every exporter, and how they are acquired.
99 (defconst org-export-max-depth
19
100 "Maximum nesting depth for headlines, counting from 0.")
102 (defconst org-export-options-alist
103 '((:title
"TITLE" nil nil parse
)
104 (:date
"DATE" nil nil parse
)
105 (:author
"AUTHOR" nil user-full-name parse
)
106 (:email
"EMAIL" nil user-mail-address t
)
107 (:language
"LANGUAGE" nil org-export-default-language t
)
108 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
109 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
110 (:creator
"CREATOR" nil org-export-creator-string
)
111 (:headline-levels nil
"H" org-export-headline-levels
)
112 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
113 (:section-numbers nil
"num" org-export-with-section-numbers
)
114 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
115 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
116 (:with-author nil
"author" org-export-with-author
)
117 (:with-broken-links nil
"broken-links" org-export-with-broken-links
)
118 (:with-clocks nil
"c" org-export-with-clocks
)
119 (:with-creator nil
"creator" org-export-with-creator
)
120 (:with-date nil
"date" org-export-with-date
)
121 (:with-drawers nil
"d" org-export-with-drawers
)
122 (:with-email nil
"email" org-export-with-email
)
123 (:with-emphasize nil
"*" org-export-with-emphasize
)
124 (:with-entities nil
"e" org-export-with-entities
)
125 (:with-fixed-width nil
":" org-export-with-fixed-width
)
126 (:with-footnotes nil
"f" org-export-with-footnotes
)
127 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
128 (:with-latex nil
"tex" org-export-with-latex
)
129 (:with-planning nil
"p" org-export-with-planning
)
130 (:with-priority nil
"pri" org-export-with-priority
)
131 (:with-properties nil
"prop" org-export-with-properties
)
132 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
133 (:with-special-strings nil
"-" org-export-with-special-strings
)
134 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
135 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
136 (:with-toc nil
"toc" org-export-with-toc
)
137 (:with-tables nil
"|" org-export-with-tables
)
138 (:with-tags nil
"tags" org-export-with-tags
)
139 (:with-tasks nil
"tasks" org-export-with-tasks
)
140 (:with-timestamps nil
"<" org-export-with-timestamps
)
141 (:with-title nil
"title" org-export-with-title
)
142 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
143 "Alist between export properties and ways to set them.
145 The key of the alist is the property name, and the value is a list
146 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
148 KEYWORD is a string representing a buffer keyword, or nil. Each
149 property defined this way can also be set, during subtree
150 export, through a headline property named after the keyword
151 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
153 OPTION is a string that could be found in an #+OPTIONS: line.
154 DEFAULT is the default value for the property.
155 BEHAVIOR determines how Org should handle multiple keywords for
156 the same property. It is a symbol among:
157 nil Keep old value and discard the new one.
158 t Replace old value with the new one.
159 `space' Concatenate the values, separating them with a space.
160 `newline' Concatenate the values, separating them with
162 `split' Split values at white spaces, and cons them to the
164 `parse' Parse value as a list of strings and Org objects,
165 which can then be transcoded with, e.g.,
166 `org-export-data'. It implies `space' behavior.
168 Values set through KEYWORD and OPTION have precedence over
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-body . org-export-filter-body-functions
)
183 (:filter-bold . org-export-filter-bold-functions
)
184 (:filter-babel-call . org-export-filter-babel-call-functions
)
185 (:filter-center-block . org-export-filter-center-block-functions
)
186 (:filter-clock . org-export-filter-clock-functions
)
187 (:filter-code . org-export-filter-code-functions
)
188 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
189 (:filter-drawer . org-export-filter-drawer-functions
)
190 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
191 (:filter-entity . org-export-filter-entity-functions
)
192 (:filter-example-block . org-export-filter-example-block-functions
)
193 (:filter-export-block . org-export-filter-export-block-functions
)
194 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
195 (:filter-final-output . org-export-filter-final-output-functions
)
196 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
197 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
198 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
199 (:filter-headline . org-export-filter-headline-functions
)
200 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
201 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
202 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
203 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
204 (:filter-italic . org-export-filter-italic-functions
)
205 (:filter-item . org-export-filter-item-functions
)
206 (:filter-keyword . org-export-filter-keyword-functions
)
207 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
208 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
209 (:filter-line-break . org-export-filter-line-break-functions
)
210 (:filter-link . org-export-filter-link-functions
)
211 (:filter-node-property . org-export-filter-node-property-functions
)
212 (:filter-options . org-export-filter-options-functions
)
213 (:filter-paragraph . org-export-filter-paragraph-functions
)
214 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
215 (:filter-plain-list . org-export-filter-plain-list-functions
)
216 (:filter-plain-text . org-export-filter-plain-text-functions
)
217 (:filter-planning . org-export-filter-planning-functions
)
218 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
219 (:filter-quote-block . org-export-filter-quote-block-functions
)
220 (:filter-radio-target . org-export-filter-radio-target-functions
)
221 (:filter-section . org-export-filter-section-functions
)
222 (:filter-special-block . org-export-filter-special-block-functions
)
223 (:filter-src-block . org-export-filter-src-block-functions
)
224 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
225 (:filter-strike-through . org-export-filter-strike-through-functions
)
226 (:filter-subscript . org-export-filter-subscript-functions
)
227 (:filter-superscript . org-export-filter-superscript-functions
)
228 (:filter-table . org-export-filter-table-functions
)
229 (:filter-table-cell . org-export-filter-table-cell-functions
)
230 (:filter-table-row . org-export-filter-table-row-functions
)
231 (:filter-target . org-export-filter-target-functions
)
232 (:filter-timestamp . org-export-filter-timestamp-functions
)
233 (:filter-underline . org-export-filter-underline-functions
)
234 (:filter-verbatim . org-export-filter-verbatim-functions
)
235 (:filter-verse-block . org-export-filter-verse-block-functions
))
236 "Alist between filters properties and initial values.
238 The key of each association is a property name accessible through
239 the communication channel. Its value is a configurable global
240 variable defining initial filters.
242 This list is meant to install user specified filters. Back-end
243 developers may install their own filters using
244 `org-export-define-backend'. Filters defined there will always
245 be prepended to the current list, so they always get applied
248 (defconst org-export-default-inline-image-rule
252 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
253 "xpm" "pbm" "pgm" "ppm") t
))))
254 "Default rule for link matching an inline image.
255 This rule applies to links with no description. By default, it
256 will be considered as an inline image if it targets a local file
257 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
258 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
259 See `org-export-inline-image-p' for more information about
262 (defconst org-export-ignored-local-variables
263 '(org-font-lock-keywords
264 org-element--cache org-element--cache-objects org-element--cache-sync-keys
265 org-element--cache-sync-requests org-element--cache-sync-timer
)
266 "List of variables not copied through upon buffer duplication.
267 Export process takes place on a copy of the original buffer.
268 When this copy is created, all Org related local variables not in
269 this list are copied to the new buffer. Variables with an
270 unreadable value are also ignored.")
272 (defvar org-export-async-debug nil
273 "Non-nil means asynchronous export process should leave data behind.
275 This data is found in the appropriate \"*Org Export Process*\"
276 buffer, and in files prefixed with \"org-export-process\" and
277 located in `temporary-file-directory'.
279 When non-nil, it will also set `debug-on-error' to a non-nil
280 value in the external process.")
282 (defvar org-export-stack-contents nil
283 "Record asynchronously generated export results and processes.
284 This is an alist: its CAR is the source of the
285 result (destination file or buffer for a finished process,
286 original buffer for a running one) and its CDR is a list
287 containing the back-end used, as a symbol, and either a process
288 or the time at which it finished. It is used to build the menu
289 from `org-export-stack'.")
291 (defvar org-export-registered-backends nil
292 "List of backends currently available in the exporter.
293 This variable is set with `org-export-define-backend' and
294 `org-export-define-derived-backend' functions.")
296 (defvar org-export-dispatch-last-action nil
297 "Last command called from the dispatcher.
298 The value should be a list. Its CAR is the action, as a symbol,
299 and its CDR is a list of export options.")
301 (defvar org-export-dispatch-last-position
(make-marker)
302 "The position where the last export command was created using the dispatcher.
303 This marker will be used with `C-u C-c C-e' to make sure export repetition
304 uses the same subtree if the previous command was restricted to a subtree.")
306 ;; For compatibility with Org < 8
307 (defvar org-export-current-backend nil
308 "Name, if any, of the back-end used during an export process.
310 Its value is a symbol such as `html', `latex', `ascii', or nil if
311 the back-end is anonymous (see `org-export-create-backend') or if
312 there is no export process in progress.
314 It can be used to teach Babel blocks how to act differently
315 according to the back-end used.")
319 ;;; User-configurable Variables
321 ;; Configuration for the masses.
323 ;; They should never be accessed directly, as their value is to be
324 ;; stored in a property list (cf. `org-export-options-alist').
325 ;; Back-ends will read their value from there instead.
327 (defgroup org-export nil
328 "Options for exporting Org mode files."
332 (defgroup org-export-general nil
333 "General options for export engine."
334 :tag
"Org Export General"
337 (defcustom org-export-with-archived-trees
'headline
338 "Whether sub-trees with the ARCHIVE tag should be exported.
340 This can have three different values:
341 nil Do not export, pretend this tree is not present.
342 t Do export the entire tree.
343 `headline' Only export the headline, but skip the tree below it.
345 This option can also be set with the OPTIONS keyword,
347 :group
'org-export-general
349 (const :tag
"Not at all" nil
)
350 (const :tag
"Headline only" headline
)
351 (const :tag
"Entirely" t
))
352 :safe
(lambda (x) (memq x
'(t nil headline
))))
354 (defcustom org-export-with-author t
355 "Non-nil means insert author name into the exported file.
356 This option can also be set with the OPTIONS keyword,
357 e.g. \"author:nil\"."
358 :group
'org-export-general
362 (defcustom org-export-with-clocks nil
363 "Non-nil means export CLOCK keywords.
364 This option can also be set with the OPTIONS keyword,
366 :group
'org-export-general
370 (defcustom org-export-with-creator nil
371 "Non-nil means the postamble should contain a creator sentence.
373 The sentence can be set in `org-export-creator-string', which
376 This option can also be set with the OPTIONS keyword, e.g.,
378 :group
'org-export-general
380 :package-version
'(Org .
"8.3")
384 (defcustom org-export-with-date t
385 "Non-nil means insert date in the exported document.
386 This option can also be set with the OPTIONS keyword,
388 :group
'org-export-general
392 (defcustom org-export-date-timestamp-format nil
393 "Time-stamp format string to use for DATE keyword.
395 The format string, when specified, only applies if date consists
396 in a single time-stamp. Otherwise its value will be ignored.
398 See `format-time-string' for details on how to build this
400 :group
'org-export-general
402 (string :tag
"Time-stamp format string")
403 (const :tag
"No format string" nil
))
404 :safe
(lambda (x) (or (null x
) (stringp x
))))
406 (defcustom org-export-creator-string
407 (format "Emacs %s (Org mode %s)"
409 (if (fboundp 'org-version
) (org-version) "unknown version"))
410 "Information about the creator of the document.
411 This option can also be set on with the CREATOR keyword."
412 :group
'org-export-general
413 :type
'(string :tag
"Creator string")
416 (defcustom org-export-with-drawers
'(not "LOGBOOK")
417 "Non-nil means export contents of standard drawers.
419 When t, all drawers are exported. This may also be a list of
420 drawer names to export, as strings. If that list starts with
421 `not', only drawers with such names will be ignored.
423 This variable doesn't apply to properties drawers. See
424 `org-export-with-properties' instead.
426 This option can also be set with the OPTIONS keyword,
428 :group
'org-export-general
430 :package-version
'(Org .
"8.0")
432 (const :tag
"All drawers" t
)
433 (const :tag
"None" nil
)
434 (repeat :tag
"Selected drawers"
435 (string :tag
"Drawer name"))
436 (list :tag
"Ignored drawers"
437 (const :format
"" not
)
438 (repeat :tag
"Specify names of drawers to ignore during export"
440 (string :tag
"Drawer name"))))
441 :safe
(lambda (x) (or (booleanp x
) (consp x
))))
443 (defcustom org-export-with-email nil
444 "Non-nil means insert author email into the exported file.
445 This option can also be set with the OPTIONS keyword,
447 :group
'org-export-general
451 (defcustom org-export-with-emphasize t
452 "Non-nil means interpret *word*, /word/, _word_ and +word+.
454 If the export target supports emphasizing text, the word will be
455 typeset in bold, italic, with an underline or strike-through,
458 This option can also be set with the OPTIONS keyword,
460 :group
'org-export-general
464 (defcustom org-export-exclude-tags
'("noexport")
465 "Tags that exclude a tree from export.
467 All trees carrying any of these tags will be excluded from
468 export. This is without condition, so even subtrees inside that
469 carry one of the `org-export-select-tags' will be removed.
471 This option can also be set with the EXCLUDE_TAGS keyword."
472 :group
'org-export-general
473 :type
'(repeat (string :tag
"Tag"))
474 :safe
(lambda (x) (and (listp x
) (cl-every #'stringp x
))))
476 (defcustom org-export-with-fixed-width t
477 "Non-nil means export lines starting with \":\".
478 This option can also be set with the OPTIONS keyword,
480 :group
'org-export-general
482 :package-version
'(Org .
"8.0")
486 (defcustom org-export-with-footnotes t
487 "Non-nil means Org footnotes should be exported.
488 This option can also be set with the OPTIONS keyword,
490 :group
'org-export-general
494 (defcustom org-export-with-latex t
495 "Non-nil means process LaTeX environments and fragments.
497 This option can also be set with the OPTIONS line,
498 e.g. \"tex:verbatim\". Allowed values are:
500 nil Ignore math snippets.
501 `verbatim' Keep everything in verbatim.
502 t Allow export of math snippets."
503 :group
'org-export-general
505 :package-version
'(Org .
"8.0")
507 (const :tag
"Do not process math in any way" nil
)
508 (const :tag
"Interpret math snippets" t
)
509 (const :tag
"Leave math verbatim" verbatim
))
510 :safe
(lambda (x) (memq x
'(t nil verbatim
))))
512 (defcustom org-export-headline-levels
3
513 "The last level which is still exported as a headline.
515 Inferior levels will usually produce itemize or enumerate lists
516 when exported, but back-end behavior may differ.
518 This option can also be set with the OPTIONS keyword,
520 :group
'org-export-general
524 (defcustom org-export-default-language
"en"
525 "The default language for export and clocktable translations, as a string.
526 This may have an association in
527 `org-clock-clocktable-language-setup',
528 `org-export-smart-quotes-alist' and `org-export-dictionary'.
529 This option can also be set with the LANGUAGE keyword."
530 :group
'org-export-general
531 :type
'(string :tag
"Language")
534 (defcustom org-export-preserve-breaks nil
535 "Non-nil means preserve all line breaks when exporting.
536 This option can also be set with the OPTIONS keyword,
538 :group
'org-export-general
542 (defcustom org-export-with-entities t
543 "Non-nil means interpret entities when exporting.
545 For example, HTML export converts \\alpha to α and \\AA to
548 For a list of supported names, see the constant `org-entities'
549 and the user option `org-entities-user'.
551 This option can also be set with the OPTIONS keyword,
553 :group
'org-export-general
557 (defcustom org-export-with-inlinetasks t
558 "Non-nil means inlinetasks should be exported.
559 This option can also be set with the OPTIONS keyword,
560 e.g. \"inline:nil\"."
561 :group
'org-export-general
563 :package-version
'(Org .
"8.0")
567 (defcustom org-export-with-planning nil
568 "Non-nil means include planning info in export.
570 Planning info is the line containing either SCHEDULED:,
571 DEADLINE:, CLOSED: time-stamps, or a combination of them.
573 This option can also be set with the OPTIONS keyword,
575 :group
'org-export-general
577 :package-version
'(Org .
"8.0")
581 (defcustom org-export-with-priority nil
582 "Non-nil means include priority cookies in export.
583 This option can also be set with the OPTIONS keyword,
585 :group
'org-export-general
589 (defcustom org-export-with-properties nil
590 "Non-nil means export contents of properties drawers.
592 When t, all properties are exported. This may also be a list of
593 properties to export, as strings.
595 This option can also be set with the OPTIONS keyword,
597 :group
'org-export-general
599 :package-version
'(Org .
"8.3")
601 (const :tag
"All properties" t
)
602 (const :tag
"None" nil
)
603 (repeat :tag
"Selected properties"
604 (string :tag
"Property name")))
605 :safe
(lambda (x) (or (booleanp x
)
606 (and (listp x
) (cl-every #'stringp x
)))))
608 (defcustom org-export-with-section-numbers t
609 "Non-nil means add section numbers to headlines when exporting.
611 When set to an integer n, numbering will only happen for
612 headlines whose relative level is higher or equal to n.
614 This option can also be set with the OPTIONS keyword,
616 :group
'org-export-general
620 (defcustom org-export-select-tags
'("export")
621 "Tags that select a tree for export.
623 If any such tag is found in a buffer, all trees that do not carry
624 one of these tags will be ignored during export. Inside trees
625 that are selected like this, you can still deselect a subtree by
626 tagging it with one of the `org-export-exclude-tags'.
628 This option can also be set with the SELECT_TAGS keyword."
629 :group
'org-export-general
630 :type
'(repeat (string :tag
"Tag"))
631 :safe
(lambda (x) (and (listp x
) (cl-every #'stringp x
))))
633 (defcustom org-export-with-smart-quotes nil
634 "Non-nil means activate smart quotes during export.
635 This option can also be set with the OPTIONS keyword,
638 When setting this to non-nil, you need to take care of
639 using the correct Babel package when exporting to LaTeX.
640 E.g., you can load Babel for french like this:
642 #+LATEX_HEADER: \\usepackage[french]{babel}"
643 :group
'org-export-general
645 :package-version
'(Org .
"8.0")
649 (defcustom org-export-with-special-strings t
650 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
652 When this option is turned on, these strings will be exported as:
655 -----+----------+--------+-------
659 ... … \\ldots …
661 This option can also be set with the OPTIONS keyword,
663 :group
'org-export-general
667 (defcustom org-export-with-statistics-cookies t
668 "Non-nil means include statistics cookies in export.
669 This option can also be set with the OPTIONS keyword,
671 :group
'org-export-general
673 :package-version
'(Org .
"8.0")
677 (defcustom org-export-with-sub-superscripts t
678 "Non-nil means interpret \"_\" and \"^\" for export.
680 If you want to control how Org displays those characters, see
681 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
682 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
685 When this option is turned on, you can use TeX-like syntax for
686 sub- and superscripts and see them exported correctly.
688 You can also set the option with #+OPTIONS: ^:t
690 Several characters after \"_\" or \"^\" will be considered as a
691 single item - so grouping with {} is normally not needed. For
692 example, the following things will be parsed as single sub- or
695 10^24 or 10^tau several digits will be considered 1 item.
696 10^-12 or 10^-tau a leading sign with digits or a word
697 x^2-y^3 will be read as x^2 - y^3, because items are
698 terminated by almost any nonword/nondigit char.
699 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
701 Still, ambiguity is possible. So when in doubt, use {} to enclose
702 the sub/superscript. If you set this variable to the symbol `{}',
703 the braces are *required* in order to trigger interpretations as
704 sub/superscript. This can be helpful in documents that need \"_\"
705 frequently in plain text."
706 :group
'org-export-general
708 :package-version
'(Org .
"8.0")
710 (const :tag
"Interpret them" t
)
711 (const :tag
"Curly brackets only" {})
712 (const :tag
"Do not interpret them" nil
))
713 :safe
(lambda (x) (memq x
'(t nil
{}))))
715 (defcustom org-export-with-toc t
716 "Non-nil means create a table of contents in exported files.
718 The table of contents contains headlines with levels up to
719 `org-export-headline-levels'.
721 When this variable is set to an integer N, include levels up to
722 N in the table of contents. Although it may then be different
723 from `org-export-headline-levels', it is cannot be larger than
724 the number of headline levels.
726 When nil, no table of contents is created.
728 This option can also be set with the OPTIONS keyword,
729 e.g. \"toc:nil\" or \"toc:3\"."
730 :group
'org-export-general
732 (const :tag
"No Table of Contents" nil
)
733 (const :tag
"Full Table of Contents" t
)
734 (integer :tag
"TOC to level"))
739 (defcustom org-export-with-tables t
740 "Non-nil means export tables.
741 This option can also be set with the OPTIONS keyword,
743 :group
'org-export-general
745 :package-version
'(Org .
"8.0")
749 (defcustom org-export-with-tags t
750 "If nil, do not export tags, just remove them from headlines.
752 If this is the symbol `not-in-toc', tags will be removed from
753 table of contents entries, but still be shown in the headlines of
756 This option can also be set with the OPTIONS keyword,
758 :group
'org-export-general
760 (const :tag
"Off" nil
)
761 (const :tag
"Not in TOC" not-in-toc
)
763 :safe
(lambda (x) (memq x
'(t nil not-in-toc
))))
765 (defcustom org-export-with-tasks t
766 "Non-nil means include TODO items for export.
768 This may have the following values:
769 t include tasks independent of state.
770 `todo' include only tasks that are not yet done.
771 `done' include only tasks that are already done.
772 nil ignore all tasks.
773 list of keywords include tasks with these keywords.
775 This option can also be set with the OPTIONS keyword,
777 :group
'org-export-general
779 (const :tag
"All tasks" t
)
780 (const :tag
"No tasks" nil
)
781 (const :tag
"Not-done tasks" todo
)
782 (const :tag
"Only done tasks" done
)
783 (repeat :tag
"Specific TODO keywords"
784 (string :tag
"Keyword")))
785 :safe
(lambda (x) (or (memq x
'(nil t todo done
))
787 (cl-every #'stringp x
)))))
789 (defcustom org-export-with-title t
790 "Non-nil means print title into the exported file.
791 This option can also be set with the OPTIONS keyword,
793 :group
'org-export-general
795 :package-version
'(Org .
"8.3")
799 (defcustom org-export-time-stamp-file t
800 "Non-nil means insert a time stamp into the exported file.
801 The time stamp shows when the file was created. This option can
802 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
803 :group
'org-export-general
807 (defcustom org-export-with-timestamps t
808 "Non nil means allow timestamps in export.
810 It can be set to any of the following values:
811 t export all timestamps.
812 `active' export active timestamps only.
813 `inactive' export inactive timestamps only.
814 nil do not export timestamps
816 This only applies to timestamps isolated in a paragraph
817 containing only timestamps. Other timestamps are always
820 This option can also be set with the OPTIONS keyword, e.g.
822 :group
'org-export-general
824 (const :tag
"All timestamps" t
)
825 (const :tag
"Only active timestamps" active
)
826 (const :tag
"Only inactive timestamps" inactive
)
827 (const :tag
"No timestamp" nil
))
828 :safe
(lambda (x) (memq x
'(t nil active inactive
))))
830 (defcustom org-export-with-todo-keywords t
831 "Non-nil means include TODO keywords in export.
832 When nil, remove all these keywords from the export. This option
833 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
834 :group
'org-export-general
837 (defcustom org-export-allow-bind-keywords nil
838 "Non-nil means BIND keywords can define local variable values.
839 This is a potential security risk, which is why the default value
840 is nil. You can also allow them through local buffer variables."
841 :group
'org-export-general
843 :package-version
'(Org .
"8.0")
846 (defcustom org-export-with-broken-links nil
847 "Non-nil means do not raise an error on broken links.
849 When this variable is non-nil, broken links are ignored, without
850 stopping the export process. If it is set to `mark', broken
851 links are marked as such in the output, with a string like
855 where PATH is the un-resolvable reference.
857 This option can also be set with the OPTIONS keyword, e.g.,
858 \"broken-links:mark\"."
859 :group
'org-export-general
861 :package-version
'(Org .
"9.0")
863 (const :tag
"Ignore broken links" t
)
864 (const :tag
"Mark broken links in output" mark
)
865 (const :tag
"Raise an error" nil
)))
867 (defcustom org-export-snippet-translation-alist nil
868 "Alist between export snippets back-ends and exporter back-ends.
870 This variable allows providing shortcuts for export snippets.
872 For example, with a value of \\='((\"h\" . \"html\")), the
873 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
874 HTML code while every other back-end will ignore it."
875 :group
'org-export-general
877 :package-version
'(Org .
"8.0")
879 (cons (string :tag
"Shortcut")
880 (string :tag
"Back-end")))
884 (cl-every #'stringp
(mapcar #'car x
))
885 (cl-every #'stringp
(mapcar #'cdr x
)))))
887 (defcustom org-export-global-macros nil
888 "Alist between macro names and expansion templates.
890 This variable defines macro expansion templates available
891 globally. Associations follow the pattern
895 where NAME is a string beginning with a letter and consisting of
896 alphanumeric characters only.
898 TEMPLATE is the string to which the macro is going to be
899 expanded. Inside, \"$1\", \"$2\"... are place-holders for
900 macro's arguments. Moreover, if the template starts with
901 \"(eval\", it will be parsed as an Elisp expression and evaluated
903 :group
'org-export-general
905 :package-version
'(Org .
"9.1")
907 (cons (string :tag
"Name")
908 (string :tag
"Template"))))
910 (defcustom org-export-coding-system nil
911 "Coding system for the exported file."
912 :group
'org-export-general
914 :package-version
'(Org .
"8.0")
915 :type
'coding-system
)
917 (defcustom org-export-copy-to-kill-ring nil
918 "Non-nil means pushing export output to the kill ring.
919 This variable is ignored during asynchronous export."
920 :group
'org-export-general
922 :package-version
'(Org .
"8.3")
924 (const :tag
"Always" t
)
925 (const :tag
"When export is done interactively" if-interactive
)
926 (const :tag
"Never" nil
)))
928 (defcustom org-export-initial-scope
'buffer
929 "The initial scope when exporting with `org-export-dispatch'.
930 This variable can be either set to `buffer' or `subtree'."
931 :group
'org-export-general
933 (const :tag
"Export current buffer" buffer
)
934 (const :tag
"Export current subtree" subtree
)))
936 (defcustom org-export-show-temporary-export-buffer t
937 "Non-nil means show buffer after exporting to temp buffer.
938 When Org exports to a file, the buffer visiting that file is never
939 shown, but remains buried. However, when exporting to
940 a temporary buffer, that buffer is popped up in a second window.
941 When this variable is nil, the buffer remains buried also in
943 :group
'org-export-general
946 (defcustom org-export-in-background nil
947 "Non-nil means export and publishing commands will run in background.
948 Results from an asynchronous export are never displayed
949 automatically. But you can retrieve them with `\\[org-export-stack]'."
950 :group
'org-export-general
952 :package-version
'(Org .
"8.0")
955 (defcustom org-export-async-init-file nil
956 "File used to initialize external export process.
958 Value must be either nil or an absolute file name. When nil, the
959 external process is launched like a regular Emacs session,
960 loading user's initialization file and any site specific
961 configuration. If a file is provided, it, and only it, is loaded
964 Therefore, using a specific configuration makes the process to
965 load faster and the export more portable."
966 :group
'org-export-general
968 :package-version
'(Org .
"8.0")
970 (const :tag
"Regular startup" nil
)
971 (file :tag
"Specific start-up file" :must-match t
)))
973 (defcustom org-export-dispatch-use-expert-ui nil
974 "Non-nil means using a non-intrusive `org-export-dispatch'.
975 In that case, no help buffer is displayed. Though, an indicator
976 for current export scope is added to the prompt (\"b\" when
977 output is restricted to body only, \"s\" when it is restricted to
978 the current subtree, \"v\" when only visible elements are
979 considered for export, \"f\" when publishing functions should be
980 passed the FORCE argument and \"a\" when the export should be
981 asynchronous). Also, [?] allows switching back to standard
983 :group
'org-export-general
985 :package-version
'(Org .
"8.0")
990 ;;; Defining Back-ends
992 ;; An export back-end is a structure with `org-export-backend' type
993 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
996 ;; At the lowest level, a back-end is created with
997 ;; `org-export-create-backend' function.
999 ;; A named back-end can be registered with
1000 ;; `org-export-register-backend' function. A registered back-end can
1001 ;; later be referred to by its name, with `org-export-get-backend'
1002 ;; function. Also, such a back-end can become the parent of a derived
1003 ;; back-end from which slot values will be inherited by default.
1004 ;; `org-export-derived-backend-p' can check if a given back-end is
1005 ;; derived from a list of back-end names.
1007 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
1008 ;; `org-export-get-all-filters' return the full alist of transcoders,
1009 ;; options and filters, including those inherited from ancestors.
1011 ;; At a higher level, `org-export-define-backend' is the standard way
1012 ;; to define an export back-end. If the new back-end is similar to
1013 ;; a registered back-end, `org-export-define-derived-backend' may be
1016 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
1017 ;; when a given back-end hasn't been registered yet.
1019 (cl-defstruct (org-export-backend (:constructor org-export-create-backend
)
1021 name parent transcoders options filters blocks menu
)
1024 (defun org-export-get-backend (name)
1025 "Return export back-end named after NAME.
1026 NAME is a symbol. Return nil if no such back-end is found."
1027 (cl-find-if (lambda (b) (and (eq name
(org-export-backend-name b
))))
1028 org-export-registered-backends
))
1030 (defun org-export-register-backend (backend)
1031 "Register BACKEND as a known export back-end.
1032 BACKEND is a structure with `org-export-backend' type."
1033 ;; Refuse to register an unnamed back-end.
1034 (unless (org-export-backend-name backend
)
1035 (error "Cannot register a unnamed export back-end"))
1036 ;; Refuse to register a back-end with an unknown parent.
1037 (let ((parent (org-export-backend-parent backend
)))
1038 (when (and parent
(not (org-export-get-backend parent
)))
1039 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
1040 ;; If a back-end with the same name as BACKEND is already
1041 ;; registered, replace it with BACKEND. Otherwise, simply add
1042 ;; BACKEND to the list of registered back-ends.
1043 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
1044 (if old
(setcar (memq old org-export-registered-backends
) backend
)
1045 (push backend org-export-registered-backends
))))
1047 (defun org-export-barf-if-invalid-backend (backend)
1048 "Signal an error if BACKEND isn't defined."
1049 (unless (org-export-backend-p backend
)
1050 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
1052 (defun org-export-derived-backend-p (backend &rest backends
)
1053 "Non-nil if BACKEND is derived from one of BACKENDS.
1054 BACKEND is an export back-end, as returned by, e.g.,
1055 `org-export-create-backend', or a symbol referring to
1056 a registered back-end. BACKENDS is constituted of symbols."
1057 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1060 (while (org-export-backend-parent backend
)
1061 (when (memq (org-export-backend-name backend
) backends
)
1064 (org-export-get-backend (org-export-backend-parent backend
))))
1065 (memq (org-export-backend-name backend
) backends
))))
1067 (defun org-export-get-all-transcoders (backend)
1068 "Return full translation table for BACKEND.
1070 BACKEND is an export back-end, as return by, e.g,,
1071 `org-export-create-backend'. Return value is an alist where
1072 keys are element or object types, as symbols, and values are
1075 Unlike to `org-export-backend-transcoders', this function
1076 also returns transcoders inherited from parent back-ends,
1078 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1080 (let ((transcoders (org-export-backend-transcoders backend
))
1082 (while (setq parent
(org-export-backend-parent backend
))
1083 (setq backend
(org-export-get-backend parent
))
1085 (append transcoders
(org-export-backend-transcoders backend
))))
1088 (defun org-export-get-all-options (backend)
1089 "Return export options for BACKEND.
1091 BACKEND is an export back-end, as return by, e.g,,
1092 `org-export-create-backend'. See `org-export-options-alist'
1093 for the shape of the return value.
1095 Unlike to `org-export-backend-options', this function also
1096 returns options inherited from parent back-ends, if any.
1098 Return nil if BACKEND is unknown."
1099 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1101 (let ((options (org-export-backend-options backend
))
1103 (while (setq parent
(org-export-backend-parent backend
))
1104 (setq backend
(org-export-get-backend parent
))
1105 (setq options
(append options
(org-export-backend-options backend
))))
1108 (defun org-export-get-all-filters (backend)
1109 "Return complete list of filters for BACKEND.
1111 BACKEND is an export back-end, as return by, e.g,,
1112 `org-export-create-backend'. Return value is an alist where
1113 keys are symbols and values lists of functions.
1115 Unlike to `org-export-backend-filters', this function also
1116 returns filters inherited from parent back-ends, if any."
1117 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1119 (let ((filters (org-export-backend-filters backend
))
1121 (while (setq parent
(org-export-backend-parent backend
))
1122 (setq backend
(org-export-get-backend parent
))
1123 (setq filters
(append filters
(org-export-backend-filters backend
))))
1126 (defun org-export-define-backend (backend transcoders
&rest body
)
1127 "Define a new back-end BACKEND.
1129 TRANSCODERS is an alist between object or element types and
1130 functions handling them.
1132 These functions should return a string without any trailing
1133 space, or nil. They must accept three arguments: the object or
1134 element itself, its contents or nil when it isn't recursive and
1135 the property list used as a communication channel.
1137 Contents, when not nil, are stripped from any global indentation
1138 \(although the relative one is preserved). They also always end
1139 with a single newline character.
1141 If, for a given type, no function is found, that element or
1142 object type will simply be ignored, along with any blank line or
1143 white space at its end. The same will happen if the function
1144 returns the nil value. If that function returns the empty
1145 string, the type will be ignored, but the blank lines or white
1146 spaces will be kept.
1148 In addition to element and object types, one function can be
1149 associated to the `template' (or `inner-template') symbol and
1150 another one to the `plain-text' symbol.
1152 The former returns the final transcoded string, and can be used
1153 to add a preamble and a postamble to document's body. It must
1154 accept two arguments: the transcoded string and the property list
1155 containing export options. A function associated to `template'
1156 will not be applied if export has option \"body-only\".
1157 A function associated to `inner-template' is always applied.
1159 The latter, when defined, is to be called on every text not
1160 recognized as an element or an object. It must accept two
1161 arguments: the text string and the information channel. It is an
1162 appropriate place to protect special chars relative to the
1165 BODY can start with pre-defined keyword arguments. The following
1166 keywords are understood:
1170 Alist between filters and function, or list of functions,
1171 specific to the back-end. See `org-export-filters-alist' for
1172 a list of all allowed filters. Filters defined here
1173 shouldn't make a back-end test, as it may prevent back-ends
1174 derived from this one to behave properly.
1178 Menu entry for the export dispatcher. It should be a list
1181 \\='(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1185 KEY is a free character selecting the back-end.
1187 DESCRIPTION-OR-ORDINAL is either a string or a number.
1189 If it is a string, is will be used to name the back-end in
1190 its menu entry. If it is a number, the following menu will
1191 be displayed as a sub-menu of the back-end with the same
1192 KEY. Also, the number will be used to determine in which
1193 order such sub-menus will appear (lowest first).
1195 ACTION-OR-MENU is either a function or an alist.
1197 If it is an action, it will be called with four
1198 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1199 BODY-ONLY. See `org-export-as' for further explanations on
1202 If it is an alist, associations should follow the
1205 \\='(KEY DESCRIPTION ACTION)
1207 where KEY, DESCRIPTION and ACTION are described above.
1209 Valid values include:
1211 \\='(?m \"My Special Back-end\" my-special-export-function)
1215 \\='(?l \"Export to LaTeX\"
1216 (?p \"As PDF file\" org-latex-export-to-pdf)
1217 (?o \"As PDF file and open\"
1219 (if a (org-latex-export-to-pdf t s v b)
1221 (org-latex-export-to-pdf nil s v b)))))))
1223 or the following, which will be added to the previous
1227 ((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1228 (?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1232 Alist between back-end specific properties introduced in
1233 communication channel and how their value are acquired. See
1234 `org-export-options-alist' for more information about
1235 structure of the values."
1236 (declare (indent 1))
1237 (let (filters menu-entry options
)
1238 (while (keywordp (car body
))
1239 (let ((keyword (pop body
)))
1241 (:filters-alist
(setq filters
(pop body
)))
1242 (:menu-entry
(setq menu-entry
(pop body
)))
1243 (:options-alist
(setq options
(pop body
)))
1244 (_ (error "Unknown keyword: %s" keyword
)))))
1245 (org-export-register-backend
1246 (org-export-create-backend :name backend
1247 :transcoders transcoders
1250 :menu menu-entry
))))
1252 (defun org-export-define-derived-backend (child parent
&rest body
)
1253 "Create a new back-end as a variant of an existing one.
1255 CHILD is the name of the derived back-end. PARENT is the name of
1256 the parent back-end.
1258 BODY can start with pre-defined keyword arguments. The following
1259 keywords are understood:
1263 Alist of filters that will overwrite or complete filters
1264 defined in PARENT back-end. See `org-export-filters-alist'
1265 for a list of allowed filters.
1269 Menu entry for the export dispatcher. See
1270 `org-export-define-backend' for more information about the
1275 Alist of back-end specific properties that will overwrite or
1276 complete those defined in PARENT back-end. Refer to
1277 `org-export-options-alist' for more information about
1278 structure of the values.
1282 Alist of element and object types and transcoders that will
1283 overwrite or complete transcode table from PARENT back-end.
1284 Refer to `org-export-define-backend' for detailed information
1287 As an example, here is how one could define \"my-latex\" back-end
1288 as a variant of `latex' back-end with a custom template function:
1290 (org-export-define-derived-backend \\='my-latex \\='latex
1291 :translate-alist \\='((template . my-latex-template-fun)))
1293 The back-end could then be called with, for example:
1295 (org-export-to-buffer \\='my-latex \"*Test my-latex*\")"
1296 (declare (indent 2))
1297 (let (filters menu-entry options transcoders
)
1298 (while (keywordp (car body
))
1299 (let ((keyword (pop body
)))
1301 (:filters-alist
(setq filters
(pop body
)))
1302 (:menu-entry
(setq menu-entry
(pop body
)))
1303 (:options-alist
(setq options
(pop body
)))
1304 (:translate-alist
(setq transcoders
(pop body
)))
1305 (_ (error "Unknown keyword: %s" keyword
)))))
1306 (org-export-register-backend
1307 (org-export-create-backend :name child
1309 :transcoders transcoders
1312 :menu menu-entry
))))
1316 ;;; The Communication Channel
1318 ;; During export process, every function has access to a number of
1319 ;; properties. They are of two types:
1321 ;; 1. Environment options are collected once at the very beginning of
1322 ;; the process, out of the original buffer and configuration.
1323 ;; Collecting them is handled by `org-export-get-environment'
1326 ;; Most environment options are defined through the
1327 ;; `org-export-options-alist' variable.
1329 ;; 2. Tree properties are extracted directly from the parsed tree,
1330 ;; just before export, by `org-export--collect-tree-properties'.
1332 ;;;; Environment Options
1334 ;; Environment options encompass all parameters defined outside the
1335 ;; scope of the parsed data. They come from five sources, in
1336 ;; increasing precedence order:
1338 ;; - Global variables,
1339 ;; - Buffer's attributes,
1340 ;; - Options keyword symbols,
1341 ;; - Buffer keywords,
1342 ;; - Subtree properties.
1344 ;; The central internal function with regards to environment options
1345 ;; is `org-export-get-environment'. It updates global variables with
1346 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1347 ;; the different sources.
1349 ;; The internal functions doing the retrieval are:
1350 ;; `org-export--get-global-options',
1351 ;; `org-export--get-buffer-attributes',
1352 ;; `org-export--parse-option-keyword',
1353 ;; `org-export--get-subtree-options' and
1354 ;; `org-export--get-inbuffer-options'
1356 ;; Also, `org-export--list-bound-variables' collects bound variables
1357 ;; along with their value in order to set them as buffer local
1358 ;; variables later in the process.
1361 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1362 "Collect export options from the current buffer.
1364 Optional argument BACKEND is an export back-end, as returned by
1365 `org-export-create-backend'.
1367 When optional argument SUBTREEP is non-nil, assume the export is
1368 done against the current sub-tree.
1370 Third optional argument EXT-PLIST is a property list with
1371 external parameters overriding Org default settings, but still
1372 inferior to file-local settings."
1373 ;; First install #+BIND variables since these must be set before
1374 ;; global options are read.
1375 (dolist (pair (org-export--list-bound-variables))
1376 (set (make-local-variable (car pair
)) (nth 1 pair
)))
1377 ;; Get and prioritize export options...
1379 ;; ... from global variables...
1380 (org-export--get-global-options backend
)
1381 ;; ... from an external property list...
1383 ;; ... from in-buffer settings...
1384 (org-export--get-inbuffer-options backend
)
1385 ;; ... and from subtree, when appropriate.
1386 (and subtreep
(org-export--get-subtree-options backend
))))
1388 (defun org-export--parse-option-keyword (options &optional backend
)
1389 "Parse an OPTIONS line and return values as a plist.
1390 Optional argument BACKEND is an export back-end, as returned by,
1391 e.g., `org-export-create-backend'. It specifies which back-end
1392 specific items to read, if any."
1395 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*" options s
)
1396 (setq s
(match-end 0))
1397 (push (cons (match-string 1 options
)
1398 (read (match-string 2 options
)))
1401 ;; Priority is given to back-end specific options.
1402 (all (append (org-export-get-all-options backend
)
1403 org-export-options-alist
))
1406 (dolist (entry all plist
)
1407 (let ((item (nth 2 entry
)))
1409 (let ((v (assoc-string item line t
)))
1410 (when v
(setq plist
(plist-put plist
(car entry
) (cdr v
)))))))))))
1412 (defun org-export--get-subtree-options (&optional backend
)
1413 "Get export options in subtree at point.
1414 Optional argument BACKEND is an export back-end, as returned by,
1415 e.g., `org-export-create-backend'. It specifies back-end used
1416 for export. Return options as a plist."
1417 ;; For each buffer keyword, create a headline property setting the
1418 ;; same property in communication channel. The name for the
1419 ;; property is the keyword with "EXPORT_" appended to it.
1420 (org-with-wide-buffer
1421 ;; Make sure point is at a heading.
1422 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1424 ;; EXPORT_OPTIONS are parsed in a non-standard way. Take
1425 ;; care of them right from the start.
1426 (let ((o (org-entry-get (point) "EXPORT_OPTIONS" 'selective
)))
1427 (and o
(org-export--parse-option-keyword o backend
))))
1428 ;; Take care of EXPORT_TITLE. If it isn't defined, use
1429 ;; headline's title (with no todo keyword, priority cookie or
1430 ;; tag) as its fallback value.
1433 (or (org-entry-get (point) "EXPORT_TITLE" 'selective
)
1434 (let ((case-fold-search nil
))
1435 (looking-at org-complex-heading-regexp
)
1436 (match-string-no-properties 4))))))
1437 ;; Look for both general keywords and back-end specific
1438 ;; options, with priority given to the latter.
1439 (options (append (org-export-get-all-options backend
)
1440 org-export-options-alist
)))
1441 ;; Handle other keywords. Then return PLIST.
1442 (dolist (option options plist
)
1443 (let ((property (car option
))
1444 (keyword (nth 1 option
)))
1447 (or (cdr (assoc keyword cache
))
1448 (let ((v (org-entry-get (point)
1449 (concat "EXPORT_" keyword
)
1451 (push (cons keyword v
) cache
) v
))))
1456 (cl-case (nth 4 option
)
1458 (org-element-parse-secondary-string
1459 value
(org-element-restriction 'keyword
)))
1460 (split (split-string value
))
1461 (t value
))))))))))))
1463 (defun org-export--get-inbuffer-options (&optional backend
)
1464 "Return current buffer export options, as a plist.
1466 Optional argument BACKEND, when non-nil, is an export back-end,
1467 as returned by, e.g., `org-export-create-backend'. It specifies
1468 which back-end specific options should also be read in the
1471 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1472 (let* ((case-fold-search t
)
1474 ;; Priority is given to back-end specific options.
1475 (org-export-get-all-options backend
)
1476 org-export-options-alist
))
1477 (regexp (format "^[ \t]*#\\+%s:"
1478 (regexp-opt (nconc (delq nil
(mapcar #'cadr options
))
1479 org-export-special-keywords
))))
1481 (letrec ((find-properties
1483 ;; Return all properties associated to KEYWORD.
1485 (dolist (option options properties
)
1486 (when (equal (nth 1 option
) keyword
)
1487 (cl-pushnew (car option
) properties
))))))
1489 (lambda (&optional files
)
1490 ;; Recursively read keywords in buffer. FILES is
1491 ;; a list of files read so far. PLIST is the current
1492 ;; property list obtained.
1493 (org-with-wide-buffer
1494 (goto-char (point-min))
1495 (while (re-search-forward regexp nil t
)
1496 (let ((element (org-element-at-point)))
1497 (when (eq (org-element-type element
) 'keyword
)
1498 (let ((key (org-element-property :key element
))
1499 (val (org-element-property :value element
)))
1501 ;; Options in `org-export-special-keywords'.
1502 ((equal key
"SETUPFILE")
1503 (let* ((uri (org-strip-quotes (org-trim val
)))
1504 (uri-is-url (org-file-url-p uri
))
1507 (expand-file-name uri
))))
1508 ;; Avoid circular dependencies.
1509 (unless (member uri files
)
1512 (setq default-directory
1513 (file-name-directory uri
)))
1514 (insert (org-file-contents uri
'noerror
))
1515 (let ((org-inhibit-startup t
)) (org-mode))
1516 (funcall get-options
(cons uri files
))))))
1517 ((equal key
"OPTIONS")
1521 (org-export--parse-option-keyword
1523 ((equal key
"FILETAGS")
1530 (org-split-string val
":")
1531 (plist-get plist
:filetags
)))))))
1533 ;; Options in `org-export-options-alist'.
1534 (dolist (property (funcall find-properties key
))
1539 ;; Handle value depending on specified
1541 (cl-case (nth 4 (assq property options
))
1543 (unless (memq property to-parse
)
1544 (push property to-parse
))
1545 ;; Even if `parse' implies `space'
1546 ;; behavior, we separate line with
1547 ;; "\n" so as to preserve
1548 ;; line-breaks. However, empty
1549 ;; lines are forbidden since `parse'
1550 ;; doesn't allow more than one
1552 (let ((old (plist-get plist property
)))
1553 (cond ((not (org-string-nw-p val
)) old
)
1554 (old (concat old
"\n" val
))
1557 (if (not (plist-get plist property
))
1559 (concat (plist-get plist property
)
1564 (concat (plist-get plist property
)
1567 (split `(,@(plist-get plist property
)
1568 ,@(split-string val
)))
1571 (if (not (plist-member plist property
)) val
1572 (plist-get plist property
)))))))))))))))))
1573 ;; Read options in the current buffer and return value.
1574 (funcall get-options
(and buffer-file-name
(list buffer-file-name
)))
1575 ;; Parse properties in TO-PARSE. Remove newline characters not
1576 ;; involved in line breaks to simulate `space' behavior.
1577 ;; Finally return options.
1578 (dolist (p to-parse plist
)
1579 (let ((value (org-element-parse-secondary-string
1581 (org-element-restriction 'keyword
))))
1582 (org-element-map value
'plain-text
1584 (org-element-set-element
1585 s
(replace-regexp-in-string "\n" " " s
))))
1586 (setq plist
(plist-put plist p value
)))))))
1588 (defun org-export--get-export-attributes
1589 (&optional backend subtreep visible-only body-only
)
1590 "Return properties related to export process, as a plist.
1591 Optional arguments BACKEND, SUBTREEP, VISIBLE-ONLY and BODY-ONLY
1592 are like the arguments with the same names of function
1594 (list :export-options
(delq nil
1595 (list (and subtreep
'subtree
)
1596 (and visible-only
'visible-only
)
1597 (and body-only
'body-only
)))
1599 :translate-alist
(org-export-get-all-transcoders backend
)
1600 :exported-data
(make-hash-table :test
#'eq
:size
4001)))
1602 (defun org-export--get-buffer-attributes ()
1603 "Return properties related to buffer attributes, as a plist."
1604 (list :input-buffer
(buffer-name (buffer-base-buffer))
1605 :input-file
(buffer-file-name (buffer-base-buffer))))
1607 (defun org-export--get-global-options (&optional backend
)
1608 "Return global export options as a plist.
1609 Optional argument BACKEND, if non-nil, is an export back-end, as
1610 returned by, e.g., `org-export-create-backend'. It specifies
1611 which back-end specific export options should also be read in the
1614 ;; Priority is given to back-end specific options.
1615 (all (append (org-export-get-all-options backend
)
1616 org-export-options-alist
)))
1617 (dolist (cell all plist
)
1618 (let ((prop (car cell
)))
1619 (unless (plist-member plist prop
)
1624 ;; Evaluate default value provided.
1625 (let ((value (eval (nth 3 cell
))))
1626 (if (eq (nth 4 cell
) 'parse
)
1627 (org-element-parse-secondary-string
1628 value
(org-element-restriction 'keyword
))
1631 (defun org-export--list-bound-variables ()
1632 "Return variables bound from BIND keywords in current buffer.
1633 Also look for BIND keywords in setup files. The return value is
1634 an alist where associations are (VARIABLE-NAME VALUE)."
1635 (when org-export-allow-bind-keywords
1636 (letrec ((collect-bind
1637 (lambda (files alist
)
1638 ;; Return an alist between variable names and their
1639 ;; value. FILES is a list of setup files names read
1640 ;; so far, used to avoid circular dependencies. ALIST
1641 ;; is the alist collected so far.
1642 (let ((case-fold-search t
))
1643 (org-with-wide-buffer
1644 (goto-char (point-min))
1645 (while (re-search-forward
1646 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1647 (let ((element (org-element-at-point)))
1648 (when (eq (org-element-type element
) 'keyword
)
1649 (let ((val (org-element-property :value element
)))
1650 (if (equal (org-element-property :key element
)
1652 (push (read (format "(%s)" val
)) alist
)
1653 ;; Enter setup file.
1654 (let* ((uri (org-strip-quotes val
))
1655 (uri-is-url (org-file-url-p uri
))
1658 (expand-file-name uri
))))
1659 ;; Avoid circular dependencies.
1660 (unless (member uri files
)
1663 (setq default-directory
1664 (file-name-directory uri
)))
1665 (let ((org-inhibit-startup t
)) (org-mode))
1666 (insert (org-file-contents uri
'noerror
))
1668 (funcall collect-bind
1672 ;; Return value in appropriate order of appearance.
1673 (nreverse (funcall collect-bind nil nil
)))))
1675 ;; defsubst org-export-get-parent must be defined before first use,
1676 ;; was originally defined in the topology section
1678 (defsubst org-export-get-parent
(blob)
1679 "Return BLOB parent or nil.
1680 BLOB is the element or object considered."
1681 (org-element-property :parent blob
))
1683 ;;;; Tree Properties
1685 ;; Tree properties are information extracted from parse tree. They
1686 ;; are initialized at the beginning of the transcoding process by
1687 ;; `org-export--collect-tree-properties'.
1689 ;; Dedicated functions focus on computing the value of specific tree
1690 ;; properties during initialization. Thus,
1691 ;; `org-export--populate-ignore-list' lists elements and objects that
1692 ;; should be skipped during export, `org-export--get-min-level' gets
1693 ;; the minimal exportable level, used as a basis to compute relative
1694 ;; level for headlines. Eventually
1695 ;; `org-export--collect-headline-numbering' builds an alist between
1696 ;; headlines and their numbering.
1698 (defun org-export--collect-tree-properties (data info
)
1699 "Extract tree properties from parse tree.
1701 DATA is the parse tree from which information is retrieved. INFO
1702 is a list holding export options.
1704 Following tree properties are set or updated:
1706 `:headline-offset' Offset between true level of headlines and
1707 local level. An offset of -1 means a headline
1708 of level 2 should be considered as a level
1709 1 headline in the context.
1711 `:headline-numbering' Alist of all headlines as key and the
1712 associated numbering as value.
1714 `:id-alist' Alist of all ID references as key and associated file
1717 Return updated plist."
1718 ;; Install the parse tree in the communication channel.
1719 (setq info
(plist-put info
:parse-tree data
))
1720 ;; Compute `:headline-offset' in order to be able to use
1721 ;; `org-export-get-relative-level'.
1725 (- 1 (org-export--get-min-level data info
))))
1726 ;; From now on, properties order doesn't matter: get the rest of the
1730 (list :headline-numbering
(org-export--collect-headline-numbering data info
)
1732 (org-element-map data
'link
1734 (and (string= (org-element-property :type l
) "id")
1735 (let* ((id (org-element-property :path l
))
1736 (file (car (org-id-find id
))))
1737 (and file
(cons id
(file-relative-name file
))))))))))
1739 (defun org-export--get-min-level (data options
)
1740 "Return minimum exportable headline's level in DATA.
1741 DATA is parsed tree as returned by `org-element-parse-buffer'.
1742 OPTIONS is a plist holding export options."
1744 (let ((min-level 10000))
1745 (dolist (datum (org-element-contents data
))
1746 (when (and (eq (org-element-type datum
) 'headline
)
1747 (not (org-element-property :footnote-section-p datum
))
1748 (not (memq datum
(plist-get options
:ignore-list
))))
1749 (setq min-level
(min (org-element-property :level datum
) min-level
))
1750 (when (= min-level
1) (throw 'exit
1))))
1751 ;; If no headline was found, for the sake of consistency, set
1752 ;; minimum level to 1 nonetheless.
1753 (if (= min-level
10000) 1 min-level
))))
1755 (defun org-export--collect-headline-numbering (data options
)
1756 "Return numbering of all exportable, numbered headlines in a parse tree.
1758 DATA is the parse tree. OPTIONS is the plist holding export
1761 Return an alist whose key is a headline and value is its
1762 associated numbering \(in the shape of a list of numbers) or nil
1763 for a footnotes section."
1764 (let ((numbering (make-vector org-export-max-depth
0)))
1765 (org-element-map data
'headline
1767 (when (and (org-export-numbered-headline-p headline options
)
1768 (not (org-element-property :footnote-section-p headline
)))
1769 (let ((relative-level
1770 (1- (org-export-get-relative-level headline options
))))
1774 for n across numbering
1775 for idx from
0 to org-export-max-depth
1776 when
(< idx relative-level
) collect n
1777 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1778 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1781 (defun org-export--selected-trees (data info
)
1782 "List headlines and inlinetasks with a select tag in their tree.
1783 DATA is parsed data as returned by `org-element-parse-buffer'.
1784 INFO is a plist holding export options."
1785 (let ((select (cl-mapcan (lambda (tag) (org-tags-expand tag t
))
1786 (plist-get info
:select-tags
))))
1787 (if (cl-some (lambda (tag) (member tag select
)) (plist-get info
:filetags
))
1788 ;; If FILETAGS contains a select tag, every headline or
1789 ;; inlinetask is returned.
1790 (org-element-map data
'(headline inlinetask
) #'identity
)
1791 (letrec ((selected-trees nil
)
1793 (lambda (data genealogy
)
1794 (let ((type (org-element-type data
)))
1796 ((memq type
'(headline inlinetask
))
1797 (let ((tags (org-element-property :tags data
)))
1798 (if (cl-some (lambda (tag) (member tag select
)) tags
)
1799 ;; When a select tag is found, mark full
1800 ;; genealogy and every headline within the
1801 ;; tree as acceptable.
1802 (setq selected-trees
1805 (org-element-map data
'(headline inlinetask
)
1808 ;; If at a headline, continue searching in
1809 ;; tree, recursively.
1810 (when (eq type
'headline
)
1811 (dolist (el (org-element-contents data
))
1812 (funcall walk-data el
(cons data genealogy
)))))))
1813 ((or (eq type
'org-data
)
1814 (memq type org-element-greater-elements
))
1815 (dolist (el (org-element-contents data
))
1816 (funcall walk-data el genealogy
))))))))
1817 (funcall walk-data data nil
)
1820 (defun org-export--skip-p (datum options selected excluded
)
1821 "Non-nil when element or object DATUM should be skipped during export.
1822 OPTIONS is the plist holding export options. SELECTED, when
1823 non-nil, is a list of headlines or inlinetasks belonging to
1824 a tree with a select tag. EXCLUDED is a list of tags, as
1825 strings. Any headline or inlinetask marked with one of those is
1827 (cl-case (org-element-type datum
)
1828 ((comment comment-block
)
1829 ;; Skip all comments and comment blocks. Make to keep maximum
1830 ;; number of blank lines around the comment so as to preserve
1831 ;; local structure of the document upon interpreting it back into
1833 (let* ((previous (org-export-get-previous-element datum options
))
1834 (before (or (org-element-property :post-blank previous
) 0))
1835 (after (or (org-element-property :post-blank datum
) 0)))
1837 (org-element-put-property previous
:post-blank
(max before after
1))))
1839 (clock (not (plist-get options
:with-clocks
)))
1841 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1842 (or (not with-drawers-p
)
1843 (and (consp with-drawers-p
)
1844 ;; If `:with-drawers' value starts with `not', ignore
1845 ;; every drawer whose name belong to that list.
1846 ;; Otherwise, ignore drawers whose name isn't in that
1848 (let ((name (org-element-property :drawer-name datum
)))
1849 (if (eq (car with-drawers-p
) 'not
)
1850 (member-ignore-case name
(cdr with-drawers-p
))
1851 (not (member-ignore-case name with-drawers-p
))))))))
1852 (fixed-width (not (plist-get options
:with-fixed-width
)))
1853 ((footnote-definition footnote-reference
)
1854 (not (plist-get options
:with-footnotes
)))
1855 ((headline inlinetask
)
1856 (let ((with-tasks (plist-get options
:with-tasks
))
1857 (todo (org-element-property :todo-keyword datum
))
1858 (todo-type (org-element-property :todo-type datum
))
1859 (archived (plist-get options
:with-archived-trees
))
1860 (tags (org-export-get-tags datum options nil t
)))
1862 (and (eq (org-element-type datum
) 'inlinetask
)
1863 (not (plist-get options
:with-inlinetasks
)))
1864 ;; Ignore subtrees with an exclude tag.
1865 (cl-some (lambda (tag) (member tag excluded
)) tags
)
1866 ;; When a select tag is present in the buffer, ignore any tree
1868 (and selected
(not (memq datum selected
)))
1869 ;; Ignore commented sub-trees.
1870 (org-element-property :commentedp datum
)
1871 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1872 (and (not archived
) (org-element-property :archivedp datum
))
1873 ;; Ignore tasks, if specified by `:with-tasks' property.
1875 (or (not with-tasks
)
1876 (and (memq with-tasks
'(todo done
))
1877 (not (eq todo-type with-tasks
)))
1878 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1879 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1881 (let ((properties-set (plist-get options
:with-properties
)))
1882 (cond ((null properties-set
) t
)
1883 ((consp properties-set
)
1884 (not (member-ignore-case (org-element-property :key datum
)
1885 properties-set
))))))
1886 (planning (not (plist-get options
:with-planning
)))
1887 (property-drawer (not (plist-get options
:with-properties
)))
1888 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1889 (table (not (plist-get options
:with-tables
)))
1891 (and (org-export-table-has-special-column-p
1892 (org-export-get-parent-table datum
))
1893 (org-export-first-sibling-p datum options
)))
1894 (table-row (org-export-table-row-is-special-p datum options
))
1896 ;; `:with-timestamps' only applies to isolated timestamps
1897 ;; objects, i.e. timestamp objects in a paragraph containing only
1898 ;; timestamps and whitespaces.
1899 (when (let ((parent (org-export-get-parent-element datum
)))
1900 (and (memq (org-element-type parent
) '(paragraph verse-block
))
1901 (not (org-element-map parent
1903 (remq 'timestamp org-element-all-objects
))
1905 (or (not (stringp obj
)) (org-string-nw-p obj
)))
1907 (cl-case (plist-get options
:with-timestamps
)
1910 (not (memq (org-element-property :type datum
) '(active active-range
))))
1912 (not (memq (org-element-property :type datum
)
1913 '(inactive inactive-range
)))))))))
1918 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1919 ;; `org-element-parse-buffer') and transcodes it into a specified
1920 ;; back-end output. It takes care of filtering out elements or
1921 ;; objects according to export options and organizing the output blank
1922 ;; lines and white space are preserved. The function memoizes its
1923 ;; results, so it is cheap to call it within transcoders.
1925 ;; It is possible to modify locally the back-end used by
1926 ;; `org-export-data' or even use a temporary back-end by using
1927 ;; `org-export-data-with-backend'.
1929 ;; `org-export-transcoder' is an accessor returning appropriate
1930 ;; translator function for a given element or object.
1932 (defun org-export-transcoder (blob info
)
1933 "Return appropriate transcoder for BLOB.
1934 INFO is a plist containing export directives."
1935 (let ((type (org-element-type blob
)))
1936 ;; Return contents only for complete parse trees.
1937 (if (eq type
'org-data
) (lambda (_datum contents _info
) contents
)
1938 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1939 (and (functionp transcoder
) transcoder
)))))
1941 (defun org-export-data (data info
)
1942 "Convert DATA into current back-end format.
1944 DATA is a parse tree, an element or an object or a secondary
1945 string. INFO is a plist holding export options.
1948 (or (gethash data
(plist-get info
:exported-data
))
1949 ;; Handle broken links according to
1950 ;; `org-export-with-broken-links'.
1952 ((broken-link-handler
1954 `(condition-case err
1957 (pcase (plist-get info
:with-broken-links
)
1958 (`nil
(user-error "Unable to resolve link: %S" (nth 1 err
)))
1959 (`mark
(org-export-data
1960 (format "[BROKEN LINK: %s]" (nth 1 err
)) info
))
1962 (let* ((type (org-element-type data
))
1963 (parent (org-export-get-parent data
))
1966 ;; Ignored element/object.
1967 ((memq data
(plist-get info
:ignore-list
)) nil
)
1969 ((eq type
'plain-text
)
1970 (org-export-filter-apply-functions
1971 (plist-get info
:filter-plain-text
)
1972 (let ((transcoder (org-export-transcoder data info
)))
1973 (if transcoder
(funcall transcoder data info
) data
))
1975 ;; Secondary string.
1977 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1978 ;; Element/Object without contents or, as a special
1979 ;; case, headline with archive tag and archived trees
1980 ;; restricted to title only.
1981 ((or (not (org-element-contents data
))
1982 (and (eq type
'headline
)
1983 (eq (plist-get info
:with-archived-trees
) 'headline
)
1984 (org-element-property :archivedp data
)))
1985 (let ((transcoder (org-export-transcoder data info
)))
1986 (or (and (functionp transcoder
)
1987 (broken-link-handler
1988 (funcall transcoder data nil info
)))
1989 ;; Export snippets never return a nil value so
1990 ;; that white spaces following them are never
1992 (and (eq type
'export-snippet
) ""))))
1993 ;; Element/Object with contents.
1995 (let ((transcoder (org-export-transcoder data info
)))
1997 (let* ((greaterp (memq type org-element-greater-elements
))
2000 (memq type org-element-recursive-objects
)))
2003 (lambda (element) (org-export-data element info
))
2004 (org-element-contents
2005 (if (or greaterp objectp
) data
2006 ;; Elements directly containing
2007 ;; objects must have their indentation
2008 ;; normalized first.
2009 (org-element-normalize-contents
2011 ;; When normalizing first paragraph
2013 ;; a footnote-definition, ignore
2014 ;; first line's indentation.
2016 (eq type
'paragraph
)
2017 (memq (org-element-type parent
)
2018 '(footnote-definition item
))
2019 (eq (car (org-element-contents parent
))
2021 (eq (org-element-property :pre-blank parent
)
2024 (broken-link-handler
2025 (funcall transcoder data
2026 (if (not greaterp
) contents
2027 (org-element-normalize-string contents
))
2029 ;; Final result will be memoized before being returned.
2034 ((memq type
'(org-data plain-text nil
)) results
)
2035 ;; Append the same white space between elements or objects
2036 ;; as in the original buffer, and call appropriate filters.
2038 (org-export-filter-apply-functions
2039 (plist-get info
(intern (format ":filter-%s" type
)))
2040 (let ((blank (or (org-element-property :post-blank data
) 0)))
2041 (if (eq (org-element-class data parent
) 'object
)
2042 (concat results
(make-string blank ?\s
))
2043 (concat (org-element-normalize-string results
)
2044 (make-string blank ?
\n))))
2046 (plist-get info
:exported-data
))))))
2048 (defun org-export-data-with-backend (data backend info
)
2049 "Convert DATA into BACKEND format.
2051 DATA is an element, an object, a secondary string or a string.
2052 BACKEND is a symbol. INFO is a plist used as a communication
2055 Unlike to `org-export-with-backend', this function will
2056 recursively convert DATA using BACKEND translation table."
2057 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2058 ;; Set-up a new communication channel with translations defined in
2059 ;; BACKEND as the translate table and a new hash table for
2064 (list :back-end backend
2065 :translate-alist
(org-export-get-all-transcoders backend
)
2066 ;; Size of the hash table is reduced since this
2067 ;; function will probably be used on small trees.
2068 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2069 (prog1 (org-export-data data new-info
)
2070 ;; Preserve `:internal-references', as those do not depend on
2071 ;; the back-end used; we need to make sure that any new
2072 ;; reference when the temporary back-end was active gets through
2074 (plist-put info
:internal-references
2075 (plist-get new-info
:internal-references
)))))
2077 (defun org-export-expand (blob contents
&optional with-affiliated
)
2078 "Expand a parsed element or object to its original state.
2080 BLOB is either an element or an object. CONTENTS is its
2081 contents, as a string or nil.
2083 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2084 keywords before output."
2085 (let ((type (org-element-type blob
)))
2086 (concat (and with-affiliated
2087 (eq (org-element-class blob
) 'element
)
2088 (org-element--interpret-affiliated-keywords blob
))
2089 (funcall (intern (format "org-element-%s-interpreter" type
))
2094 ;;; The Filter System
2096 ;; Filters allow end-users to tweak easily the transcoded output.
2097 ;; They are the functional counterpart of hooks, as every filter in
2098 ;; a set is applied to the return value of the previous one.
2100 ;; Every set is back-end agnostic. Although, a filter is always
2101 ;; called, in addition to the string it applies to, with the back-end
2102 ;; used as argument, so it's easy for the end-user to add back-end
2103 ;; specific filters in the set. The communication channel, as
2104 ;; a plist, is required as the third argument.
2106 ;; From the developer side, filters sets can be installed in the
2107 ;; process with the help of `org-export-define-backend', which
2108 ;; internally stores filters as an alist. Each association has a key
2109 ;; among the following symbols and a function or a list of functions
2112 ;; - `:filter-options' applies to the property list containing export
2113 ;; options. Unlike to other filters, functions in this list accept
2114 ;; two arguments instead of three: the property list containing
2115 ;; export options and the back-end. Users can set its value through
2116 ;; `org-export-filter-options-functions' variable.
2118 ;; - `:filter-parse-tree' applies directly to the complete parsed
2119 ;; tree. Users can set it through
2120 ;; `org-export-filter-parse-tree-functions' variable.
2122 ;; - `:filter-body' applies to the body of the output, before template
2123 ;; translator chimes in. Users can set it through
2124 ;; `org-export-filter-body-functions' variable.
2126 ;; - `:filter-final-output' applies to the final transcoded string.
2127 ;; Users can set it with `org-export-filter-final-output-functions'
2130 ;; - `:filter-plain-text' applies to any string not recognized as Org
2131 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2134 ;; - `:filter-TYPE' applies on the string returned after an element or
2135 ;; object of type TYPE has been transcoded. A user can modify
2136 ;; `org-export-filter-TYPE-functions' to install these filters.
2138 ;; All filters sets are applied with
2139 ;; `org-export-filter-apply-functions' function. Filters in a set are
2140 ;; applied in a LIFO fashion. It allows developers to be sure that
2141 ;; their filters will be applied first.
2143 ;; Filters properties are installed in communication channel with
2144 ;; `org-export-install-filters' function.
2146 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2147 ;; `org-export-before-parsing-hook') are run at the beginning of the
2148 ;; export process and just before parsing to allow for heavy structure
2154 (defvar org-export-before-processing-hook nil
2155 "Hook run at the beginning of the export process.
2157 This is run before include keywords and macros are expanded and
2158 Babel code blocks executed, on a copy of the original buffer
2159 being exported. Visibility and narrowing are preserved. Point
2160 is at the beginning of the buffer.
2162 Every function in this hook will be called with one argument: the
2163 back-end currently used, as a symbol.")
2165 (defvar org-export-before-parsing-hook nil
2166 "Hook run before parsing an export buffer.
2168 This is run after include keywords and macros have been expanded
2169 and Babel code blocks executed, on a copy of the original buffer
2170 being exported. Visibility and narrowing are preserved. Point
2171 is at the beginning of the buffer.
2173 Every function in this hook will be called with one argument: the
2174 back-end currently used, as a symbol.")
2177 ;;;; Special Filters
2179 (defvar org-export-filter-options-functions nil
2180 "List of functions applied to the export options.
2181 Each filter is called with two arguments: the export options, as
2182 a plist, and the back-end, as a symbol. It must return
2183 a property list containing export options.")
2185 (defvar org-export-filter-parse-tree-functions nil
2186 "List of functions applied to the parsed tree.
2187 Each filter is called with three arguments: the parse tree, as
2188 returned by `org-element-parse-buffer', the back-end, as
2189 a symbol, and the communication channel, as a plist. It must
2190 return the modified parse tree to transcode.")
2192 (defvar org-export-filter-plain-text-functions nil
2193 "List of functions applied to plain text.
2194 Each filter is called with three arguments: a string which
2195 contains no Org syntax, the back-end, as a symbol, and the
2196 communication channel, as a plist. It must return a string or
2199 (defvar org-export-filter-body-functions nil
2200 "List of functions applied to transcoded body.
2201 Each filter is called with three arguments: a string which
2202 contains no Org syntax, the back-end, as a symbol, and the
2203 communication channel, as a plist. It must return a string or
2206 (defvar org-export-filter-final-output-functions nil
2207 "List of functions applied to the transcoded string.
2208 Each filter is called with three arguments: the full transcoded
2209 string, the back-end, as a symbol, and the communication channel,
2210 as a plist. It must return a string that will be used as the
2211 final export output.")
2214 ;;;; Elements Filters
2216 (defvar org-export-filter-babel-call-functions nil
2217 "List of functions applied to a transcoded babel-call.
2218 Each filter is called with three arguments: the transcoded data,
2219 as a string, the back-end, as a symbol, and the communication
2220 channel, as a plist. It must return a string or nil.")
2222 (defvar org-export-filter-center-block-functions nil
2223 "List of functions applied to a transcoded center block.
2224 Each filter is called with three arguments: the transcoded data,
2225 as a string, the back-end, as a symbol, and the communication
2226 channel, as a plist. It must return a string or nil.")
2228 (defvar org-export-filter-clock-functions nil
2229 "List of functions applied to a transcoded clock.
2230 Each filter is called with three arguments: the transcoded data,
2231 as a string, the back-end, as a symbol, and the communication
2232 channel, as a plist. It must return a string or nil.")
2234 (defvar org-export-filter-diary-sexp-functions nil
2235 "List of functions applied to a transcoded diary-sexp.
2236 Each filter is called with three arguments: the transcoded data,
2237 as a string, the back-end, as a symbol, and the communication
2238 channel, as a plist. It must return a string or nil.")
2240 (defvar org-export-filter-drawer-functions nil
2241 "List of functions applied to a transcoded drawer.
2242 Each filter is called with three arguments: the transcoded data,
2243 as a string, the back-end, as a symbol, and the communication
2244 channel, as a plist. It must return a string or nil.")
2246 (defvar org-export-filter-dynamic-block-functions nil
2247 "List of functions applied to a transcoded dynamic-block.
2248 Each filter is called with three arguments: the transcoded data,
2249 as a string, the back-end, as a symbol, and the communication
2250 channel, as a plist. It must return a string or nil.")
2252 (defvar org-export-filter-example-block-functions nil
2253 "List of functions applied to a transcoded example-block.
2254 Each filter is called with three arguments: the transcoded data,
2255 as a string, the back-end, as a symbol, and the communication
2256 channel, as a plist. It must return a string or nil.")
2258 (defvar org-export-filter-export-block-functions nil
2259 "List of functions applied to a transcoded export-block.
2260 Each filter is called with three arguments: the transcoded data,
2261 as a string, the back-end, as a symbol, and the communication
2262 channel, as a plist. It must return a string or nil.")
2264 (defvar org-export-filter-fixed-width-functions nil
2265 "List of functions applied to a transcoded fixed-width.
2266 Each filter is called with three arguments: the transcoded data,
2267 as a string, the back-end, as a symbol, and the communication
2268 channel, as a plist. It must return a string or nil.")
2270 (defvar org-export-filter-footnote-definition-functions nil
2271 "List of functions applied to a transcoded footnote-definition.
2272 Each filter is called with three arguments: the transcoded data,
2273 as a string, the back-end, as a symbol, and the communication
2274 channel, as a plist. It must return a string or nil.")
2276 (defvar org-export-filter-headline-functions nil
2277 "List of functions applied to a transcoded headline.
2278 Each filter is called with three arguments: the transcoded data,
2279 as a string, the back-end, as a symbol, and the communication
2280 channel, as a plist. It must return a string or nil.")
2282 (defvar org-export-filter-horizontal-rule-functions nil
2283 "List of functions applied to a transcoded horizontal-rule.
2284 Each filter is called with three arguments: the transcoded data,
2285 as a string, the back-end, as a symbol, and the communication
2286 channel, as a plist. It must return a string or nil.")
2288 (defvar org-export-filter-inlinetask-functions nil
2289 "List of functions applied to a transcoded inlinetask.
2290 Each filter is called with three arguments: the transcoded data,
2291 as a string, the back-end, as a symbol, and the communication
2292 channel, as a plist. It must return a string or nil.")
2294 (defvar org-export-filter-item-functions nil
2295 "List of functions applied to a transcoded item.
2296 Each filter is called with three arguments: the transcoded data,
2297 as a string, the back-end, as a symbol, and the communication
2298 channel, as a plist. It must return a string or nil.")
2300 (defvar org-export-filter-keyword-functions nil
2301 "List of functions applied to a transcoded keyword.
2302 Each filter is called with three arguments: the transcoded data,
2303 as a string, the back-end, as a symbol, and the communication
2304 channel, as a plist. It must return a string or nil.")
2306 (defvar org-export-filter-latex-environment-functions nil
2307 "List of functions applied to a transcoded latex-environment.
2308 Each filter is called with three arguments: the transcoded data,
2309 as a string, the back-end, as a symbol, and the communication
2310 channel, as a plist. It must return a string or nil.")
2312 (defvar org-export-filter-node-property-functions nil
2313 "List of functions applied to a transcoded node-property.
2314 Each filter is called with three arguments: the transcoded data,
2315 as a string, the back-end, as a symbol, and the communication
2316 channel, as a plist. It must return a string or nil.")
2318 (defvar org-export-filter-paragraph-functions nil
2319 "List of functions applied to a transcoded paragraph.
2320 Each filter is called with three arguments: the transcoded data,
2321 as a string, the back-end, as a symbol, and the communication
2322 channel, as a plist. It must return a string or nil.")
2324 (defvar org-export-filter-plain-list-functions nil
2325 "List of functions applied to a transcoded plain-list.
2326 Each filter is called with three arguments: the transcoded data,
2327 as a string, the back-end, as a symbol, and the communication
2328 channel, as a plist. It must return a string or nil.")
2330 (defvar org-export-filter-planning-functions nil
2331 "List of functions applied to a transcoded planning.
2332 Each filter is called with three arguments: the transcoded data,
2333 as a string, the back-end, as a symbol, and the communication
2334 channel, as a plist. It must return a string or nil.")
2336 (defvar org-export-filter-property-drawer-functions nil
2337 "List of functions applied to a transcoded property-drawer.
2338 Each filter is called with three arguments: the transcoded data,
2339 as a string, the back-end, as a symbol, and the communication
2340 channel, as a plist. It must return a string or nil.")
2342 (defvar org-export-filter-quote-block-functions nil
2343 "List of functions applied to a transcoded quote block.
2344 Each filter is called with three arguments: the transcoded quote
2345 data, as a string, the back-end, as a symbol, and the
2346 communication channel, as a plist. It must return a string or
2349 (defvar org-export-filter-section-functions nil
2350 "List of functions applied to a transcoded section.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-special-block-functions nil
2356 "List of functions applied to a transcoded special block.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-src-block-functions nil
2362 "List of functions applied to a transcoded src-block.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-table-functions nil
2368 "List of functions applied to a transcoded table.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-table-cell-functions nil
2374 "List of functions applied to a transcoded table-cell.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-table-row-functions nil
2380 "List of functions applied to a transcoded table-row.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-verse-block-functions nil
2386 "List of functions applied to a transcoded verse block.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2392 ;;;; Objects Filters
2394 (defvar org-export-filter-bold-functions nil
2395 "List of functions applied to transcoded bold text.
2396 Each filter is called with three arguments: the transcoded data,
2397 as a string, the back-end, as a symbol, and the communication
2398 channel, as a plist. It must return a string or nil.")
2400 (defvar org-export-filter-code-functions nil
2401 "List of functions applied to transcoded code text.
2402 Each filter is called with three arguments: the transcoded data,
2403 as a string, the back-end, as a symbol, and the communication
2404 channel, as a plist. It must return a string or nil.")
2406 (defvar org-export-filter-entity-functions nil
2407 "List of functions applied to a transcoded entity.
2408 Each filter is called with three arguments: the transcoded data,
2409 as a string, the back-end, as a symbol, and the communication
2410 channel, as a plist. It must return a string or nil.")
2412 (defvar org-export-filter-export-snippet-functions nil
2413 "List of functions applied to a transcoded export-snippet.
2414 Each filter is called with three arguments: the transcoded data,
2415 as a string, the back-end, as a symbol, and the communication
2416 channel, as a plist. It must return a string or nil.")
2418 (defvar org-export-filter-footnote-reference-functions nil
2419 "List of functions applied to a transcoded footnote-reference.
2420 Each filter is called with three arguments: the transcoded data,
2421 as a string, the back-end, as a symbol, and the communication
2422 channel, as a plist. It must return a string or nil.")
2424 (defvar org-export-filter-inline-babel-call-functions nil
2425 "List of functions applied to a transcoded inline-babel-call.
2426 Each filter is called with three arguments: the transcoded data,
2427 as a string, the back-end, as a symbol, and the communication
2428 channel, as a plist. It must return a string or nil.")
2430 (defvar org-export-filter-inline-src-block-functions nil
2431 "List of functions applied to a transcoded inline-src-block.
2432 Each filter is called with three arguments: the transcoded data,
2433 as a string, the back-end, as a symbol, and the communication
2434 channel, as a plist. It must return a string or nil.")
2436 (defvar org-export-filter-italic-functions nil
2437 "List of functions applied to transcoded italic text.
2438 Each filter is called with three arguments: the transcoded data,
2439 as a string, the back-end, as a symbol, and the communication
2440 channel, as a plist. It must return a string or nil.")
2442 (defvar org-export-filter-latex-fragment-functions nil
2443 "List of functions applied to a transcoded latex-fragment.
2444 Each filter is called with three arguments: the transcoded data,
2445 as a string, the back-end, as a symbol, and the communication
2446 channel, as a plist. It must return a string or nil.")
2448 (defvar org-export-filter-line-break-functions nil
2449 "List of functions applied to a transcoded line-break.
2450 Each filter is called with three arguments: the transcoded data,
2451 as a string, the back-end, as a symbol, and the communication
2452 channel, as a plist. It must return a string or nil.")
2454 (defvar org-export-filter-link-functions nil
2455 "List of functions applied to a transcoded link.
2456 Each filter is called with three arguments: the transcoded data,
2457 as a string, the back-end, as a symbol, and the communication
2458 channel, as a plist. It must return a string or nil.")
2460 (defvar org-export-filter-radio-target-functions nil
2461 "List of functions applied to a transcoded radio-target.
2462 Each filter is called with three arguments: the transcoded data,
2463 as a string, the back-end, as a symbol, and the communication
2464 channel, as a plist. It must return a string or nil.")
2466 (defvar org-export-filter-statistics-cookie-functions nil
2467 "List of functions applied to a transcoded statistics-cookie.
2468 Each filter is called with three arguments: the transcoded data,
2469 as a string, the back-end, as a symbol, and the communication
2470 channel, as a plist. It must return a string or nil.")
2472 (defvar org-export-filter-strike-through-functions nil
2473 "List of functions applied to transcoded strike-through text.
2474 Each filter is called with three arguments: the transcoded data,
2475 as a string, the back-end, as a symbol, and the communication
2476 channel, as a plist. It must return a string or nil.")
2478 (defvar org-export-filter-subscript-functions nil
2479 "List of functions applied to a transcoded subscript.
2480 Each filter is called with three arguments: the transcoded data,
2481 as a string, the back-end, as a symbol, and the communication
2482 channel, as a plist. It must return a string or nil.")
2484 (defvar org-export-filter-superscript-functions nil
2485 "List of functions applied to a transcoded superscript.
2486 Each filter is called with three arguments: the transcoded data,
2487 as a string, the back-end, as a symbol, and the communication
2488 channel, as a plist. It must return a string or nil.")
2490 (defvar org-export-filter-target-functions nil
2491 "List of functions applied to a transcoded target.
2492 Each filter is called with three arguments: the transcoded data,
2493 as a string, the back-end, as a symbol, and the communication
2494 channel, as a plist. It must return a string or nil.")
2496 (defvar org-export-filter-timestamp-functions nil
2497 "List of functions applied to a transcoded timestamp.
2498 Each filter is called with three arguments: the transcoded data,
2499 as a string, the back-end, as a symbol, and the communication
2500 channel, as a plist. It must return a string or nil.")
2502 (defvar org-export-filter-underline-functions nil
2503 "List of functions applied to transcoded underline text.
2504 Each filter is called with three arguments: the transcoded data,
2505 as a string, the back-end, as a symbol, and the communication
2506 channel, as a plist. It must return a string or nil.")
2508 (defvar org-export-filter-verbatim-functions nil
2509 "List of functions applied to transcoded verbatim text.
2510 Each filter is called with three arguments: the transcoded data,
2511 as a string, the back-end, as a symbol, and the communication
2512 channel, as a plist. It must return a string or nil.")
2517 ;; Internal function `org-export-install-filters' installs filters
2518 ;; hard-coded in back-ends (developer filters) and filters from global
2519 ;; variables (user filters) in the communication channel.
2521 ;; Internal function `org-export-filter-apply-functions' takes care
2522 ;; about applying each filter in order to a given data. It ignores
2523 ;; filters returning a nil value but stops whenever a filter returns
2526 (defun org-export-filter-apply-functions (filters value info
)
2527 "Call every function in FILTERS.
2529 Functions are called with three arguments: a value, the export
2530 back-end name and the communication channel. First function in
2531 FILTERS is called with VALUE as its first argument. Second
2532 function in FILTERS is called with the previous result as its
2535 Functions returning nil are skipped. Any function returning the
2536 empty string ends the process, which returns the empty string.
2538 Call is done in a LIFO fashion, to be sure that developer
2539 specified filters, if any, are called first."
2541 (let* ((backend (plist-get info
:back-end
))
2542 (backend-name (and backend
(org-export-backend-name backend
))))
2543 (dolist (filter filters value
)
2544 (let ((result (funcall filter value backend-name info
)))
2545 (cond ((not result
))
2546 ((equal result
"") (throw :exit
""))
2547 (t (setq value result
))))))))
2549 (defun org-export-install-filters (info)
2550 "Install filters properties in communication channel.
2551 INFO is a plist containing the current communication channel.
2552 Return the updated communication channel."
2554 ;; Install user-defined filters with `org-export-filters-alist'
2555 ;; and filters already in INFO (through ext-plist mechanism).
2556 (dolist (p org-export-filters-alist
)
2557 (let* ((prop (car p
))
2558 (info-value (plist-get info prop
))
2559 (default-value (symbol-value (cdr p
))))
2561 (plist-put plist prop
2562 ;; Filters in INFO will be called
2563 ;; before those user provided.
2564 (append (if (listp info-value
) info-value
2567 ;; Prepend back-end specific filters to that list.
2568 (dolist (p (org-export-get-all-filters (plist-get info
:back-end
)))
2569 ;; Single values get consed, lists are appended.
2570 (let ((key (car p
)) (value (cdr p
)))
2575 (if (atom value
) (cons value
(plist-get plist key
))
2576 (append value
(plist-get plist key
))))))))
2577 ;; Return new communication channel.
2578 (org-combine-plists info plist
)))
2584 ;; This is the room for the main function, `org-export-as', along with
2585 ;; its derivative, `org-export-string-as'.
2586 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2587 ;; function should be added to kill ring.
2589 ;; Note that `org-export-as' doesn't really parse the current buffer,
2590 ;; but a copy of it (with the same buffer-local variables and
2591 ;; visibility), where macros and include keywords are expanded and
2592 ;; Babel blocks are executed, if appropriate.
2593 ;; `org-export-with-buffer-copy' macro prepares that copy.
2595 ;; File inclusion is taken care of by
2596 ;; `org-export-expand-include-keyword' and
2597 ;; `org-export--prepare-file-contents'. Structure wise, including
2598 ;; a whole Org file in a buffer often makes little sense. For
2599 ;; example, if the file contains a headline and the include keyword
2600 ;; was within an item, the item should contain the headline. That's
2601 ;; why file inclusion should be done before any structure can be
2602 ;; associated to the file, that is before parsing.
2604 ;; `org-export-insert-default-template' is a command to insert
2605 ;; a default template (or a back-end specific template) at point or in
2608 (defun org-export-copy-buffer ()
2609 "Return a copy of the current buffer.
2610 The copy preserves Org buffer-local variables, visibility and
2612 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2613 (new-buf (generate-new-buffer (buffer-name))))
2614 (with-current-buffer new-buf
2615 (funcall copy-buffer-fun
)
2616 (set-buffer-modified-p nil
))
2619 (defmacro org-export-with-buffer-copy
(&rest body
)
2620 "Apply BODY in a copy of the current buffer.
2621 The copy preserves local variables, visibility and contents of
2622 the original buffer. Point is at the beginning of the buffer
2623 when BODY is applied."
2625 (org-with-gensyms (buf-copy)
2626 `(let ((,buf-copy
(org-export-copy-buffer)))
2628 (with-current-buffer ,buf-copy
2629 (goto-char (point-min))
2631 (and (buffer-live-p ,buf-copy
)
2632 ;; Kill copy without confirmation.
2633 (progn (with-current-buffer ,buf-copy
2634 (restore-buffer-modified-p nil
))
2635 (kill-buffer ,buf-copy
)))))))
2637 (defun org-export--generate-copy-script (buffer)
2638 "Generate a function duplicating BUFFER.
2640 The copy will preserve local variables, visibility, contents and
2641 narrowing of the original buffer. If a region was active in
2642 BUFFER, contents will be narrowed to that region instead.
2644 The resulting function can be evaluated at a later time, from
2645 another buffer, effectively cloning the original buffer there.
2647 The function assumes BUFFER's major mode is `org-mode'."
2648 (with-current-buffer buffer
2650 (let ((inhibit-modification-hooks t
))
2651 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2652 ;; already in BUFFER.
2653 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2654 ;; Copy specific buffer local variables and variables set
2655 ;; through BIND keywords.
2656 ,@(let ((bound-variables (org-export--list-bound-variables))
2658 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2660 (let ((var (car entry
))
2662 (and (not (memq var org-export-ignored-local-variables
))
2666 buffer-file-coding-system
))
2667 (assq var bound-variables
)
2668 (string-match "^\\(org-\\|orgtbl-\\)"
2670 ;; Skip unreadable values, as they cannot be
2671 ;; sent to external process.
2672 (or (not val
) (ignore-errors (read (format "%S" val
))))
2673 (push `(set (make-local-variable (quote ,var
))
2676 ;; Whole buffer contents.
2677 (insert ,(org-with-wide-buffer (buffer-string)))
2679 ,(if (org-region-active-p)
2680 `(narrow-to-region ,(region-beginning) ,(region-end))
2681 `(narrow-to-region ,(point-min) ,(point-max)))
2682 ;; Current position of point.
2683 (goto-char ,(point))
2684 ;; Overlays with invisible property.
2686 (dolist (ov (overlays-in (point-min) (point-max)) ov-set
)
2687 (let ((invis-prop (overlay-get ov
'invisible
)))
2690 (make-overlay ,(overlay-start ov
)
2692 'invisible
(quote ,invis-prop
))
2695 (defun org-export--delete-comment-trees ()
2696 "Delete commented trees and commented inlinetasks in the buffer.
2697 Narrowing, if any, is ignored."
2698 (org-with-wide-buffer
2699 (goto-char (point-min))
2700 (let* ((case-fold-search t
)
2701 (regexp (concat org-outline-regexp-bol
".*" org-comment-string
)))
2702 (while (re-search-forward regexp nil t
)
2703 (let ((element (org-element-at-point)))
2704 (when (org-element-property :commentedp element
)
2705 (delete-region (org-element-property :begin element
)
2706 (org-element-property :end element
))))))))
2708 (defun org-export--prune-tree (data info
)
2709 "Prune non exportable elements from DATA.
2710 DATA is the parse tree to traverse. INFO is the plist holding
2711 export info. Also set `:ignore-list' in INFO to a list of
2712 objects which should be ignored during export, but not removed
2714 (letrec ((ignore nil
)
2715 ;; First find trees containing a select tag, if any.
2716 (selected (org-export--selected-trees data info
))
2717 ;; List tags that prevent export of headlines.
2718 (excluded (cl-mapcan (lambda (tag) (org-tags-expand tag t
))
2719 (plist-get info
:exclude-tags
)))
2722 ;; Prune non-exportable elements and objects from tree.
2723 ;; As a special case, special rows and cells from tables
2724 ;; are stored in IGNORE, as they still need to be
2725 ;; accessed during export.
2727 (let ((type (org-element-type data
)))
2728 (if (org-export--skip-p data info selected excluded
)
2729 (if (memq type
'(table-cell table-row
)) (push data ignore
)
2730 (org-element-extract-element data
))
2731 (if (and (eq type
'headline
)
2732 (eq (plist-get info
:with-archived-trees
)
2734 (org-element-property :archivedp data
))
2735 ;; If headline is archived but tree below has
2736 ;; to be skipped, remove contents.
2737 (org-element-set-contents data
)
2738 ;; Move into recursive objects/elements.
2739 (mapc walk-data
(org-element-contents data
)))
2740 ;; Move into secondary string, if any.
2741 (dolist (p (cdr (assq type
2742 org-element-secondary-value-alist
)))
2743 (mapc walk-data
(org-element-property p data
))))))))
2745 ;; Collect definitions before possibly pruning them so as
2746 ;; to avoid parsing them again if they are required.
2747 (org-element-map data
'(footnote-definition footnote-reference
)
2750 ((eq 'footnote-definition
(org-element-type f
)) f
)
2751 ((and (eq 'inline
(org-element-property :type f
))
2752 (org-element-property :label f
))
2755 ;; If a select tag is active, also ignore the section before the
2756 ;; first headline, if any.
2758 (let ((first-element (car (org-element-contents data
))))
2759 (when (eq (org-element-type first-element
) 'section
)
2760 (org-element-extract-element first-element
))))
2761 ;; Prune tree and communication channel.
2762 (funcall walk-data data
)
2763 (dolist (entry (append
2764 ;; Priority is given to back-end specific options.
2765 (org-export-get-all-options (plist-get info
:back-end
))
2766 org-export-options-alist
))
2767 (when (eq (nth 4 entry
) 'parse
)
2768 (funcall walk-data
(plist-get info
(car entry
)))))
2769 (let ((missing (org-export--missing-definitions data definitions
)))
2770 (funcall walk-data missing
)
2771 (org-export--install-footnote-definitions missing data
))
2772 ;; Eventually set `:ignore-list'.
2773 (plist-put info
:ignore-list ignore
)))
2775 (defun org-export--missing-definitions (tree definitions
)
2776 "List footnote definitions missing from TREE.
2777 Missing definitions are searched within DEFINITIONS, which is
2778 a list of footnote definitions or in the widened buffer."
2781 ;; List all footnote labels encountered in DATA. Inline
2782 ;; footnote references are ignored.
2783 (org-element-map data
'footnote-reference
2785 (and (eq (org-element-property :type reference
) 'standard
)
2786 (org-element-property :label reference
))))))
2787 defined undefined missing-definitions
)
2788 ;; Partition DIRECT-REFERENCES between DEFINED and UNDEFINED
2790 (let ((known-definitions
2791 (org-element-map tree
'(footnote-reference footnote-definition
)
2793 (and (or (eq (org-element-type f
) 'footnote-definition
)
2794 (eq (org-element-property :type f
) 'inline
))
2795 (org-element-property :label f
)))))
2797 (dolist (l (funcall list-labels tree
))
2798 (cond ((member l seen
))
2799 ((member l known-definitions
) (push l defined
))
2800 (t (push l undefined
)))))
2801 ;; Complete MISSING-DEFINITIONS by finding the definition of every
2802 ;; undefined label, first by looking into DEFINITIONS, then by
2803 ;; searching the widened buffer. This is a recursive process
2804 ;; since definitions found can themselves contain an undefined
2807 (let* ((label (pop undefined
))
2811 (lambda (d) (and (equal (org-element-property :label d
) label
)
2814 ((pcase (org-footnote-get-definition label
)
2816 (org-with-wide-buffer
2818 (let ((datum (org-element-context)))
2819 (if (eq (org-element-type datum
) 'footnote-reference
)
2821 ;; Parse definition with contents.
2824 (org-element-property :begin datum
)
2825 (org-element-property :end datum
))
2826 (org-element-map (org-element-parse-buffer)
2827 'footnote-definition
#'identity nil t
))))))
2829 (t (user-error "Definition not found for footnote %s" label
)))))
2830 (push label defined
)
2831 (push definition missing-definitions
)
2832 ;; Look for footnote references within DEFINITION, since
2833 ;; we may need to also find their definition.
2834 (dolist (l (funcall list-labels definition
))
2835 (unless (or (member l defined
) ;Known label
2836 (member l undefined
)) ;Processed later
2837 (push l undefined
)))))
2838 ;; MISSING-DEFINITIONS may contain footnote references with inline
2839 ;; definitions. Make sure those are changed into real footnote
2842 (if (eq (org-element-type d
) 'footnote-definition
) d
2843 (let ((label (org-element-property :label d
)))
2844 (apply #'org-element-create
2845 'footnote-definition
`(:label
,label
:post-blank
1)
2846 (org-element-contents d
)))))
2847 missing-definitions
)))
2849 (defun org-export--install-footnote-definitions (definitions tree
)
2850 "Install footnote definitions in tree.
2852 DEFINITIONS is the list of footnote definitions to install. TREE
2855 If there is a footnote section in TREE, definitions found are
2856 appended to it. If `org-footnote-section' is non-nil, a new
2857 footnote section containing all definitions is inserted in TREE.
2858 Otherwise, definitions are appended at the end of the section
2859 containing their first reference."
2861 ((null definitions
))
2862 ;; If there is a footnote section, insert definitions there.
2863 ((let ((footnote-section
2864 (org-element-map tree
'headline
2865 (lambda (h) (and (org-element-property :footnote-section-p h
) h
))
2867 (and footnote-section
2868 (apply #'org-element-adopt-elements
2870 (nreverse definitions
)))))
2871 ;; If there should be a footnote section, create one containing all
2872 ;; the definitions at the end of the tree.
2873 (org-footnote-section
2874 (org-element-adopt-elements
2876 (org-element-create 'headline
2877 (list :footnote-section-p t
2879 :title org-footnote-section
2880 :raw-value org-footnote-section
)
2881 (apply #'org-element-create
2884 (nreverse definitions
)))))
2885 ;; Otherwise add each definition at the end of the section where it
2886 ;; is first referenced.
2891 ;; Insert footnote definitions in the same section as
2892 ;; their first reference in DATA.
2893 (org-element-map data
'footnote-reference
2895 (when (eq (org-element-property :type reference
) 'standard
)
2896 (let ((label (org-element-property :label reference
)))
2897 (unless (member label seen
)
2902 (and (equal (org-element-property :label d
)
2906 (org-element-adopt-elements
2907 (org-element-lineage reference
'(section))
2909 ;; Also insert definitions for nested
2910 ;; references, if any.
2911 (funcall insert-definitions definition
))))))))))
2912 (funcall insert-definitions tree
)))))
2914 (defun org-export--remove-uninterpreted-data (data info
)
2915 "Change uninterpreted elements back into Org syntax.
2916 DATA is a parse tree or a secondary string. INFO is a plist
2917 containing export options. It is modified by side effect and
2918 returned by the function."
2919 (org-element-map data
2920 '(entity bold italic latex-environment latex-fragment strike-through
2921 subscript superscript underline
)
2924 (cl-case (org-element-type datum
)
2927 (and (not (plist-get info
:with-entities
))
2929 (org-export-expand datum nil
)
2931 (or (org-element-property :post-blank datum
) 0)
2934 ((bold italic strike-through underline
)
2935 (and (not (plist-get info
:with-emphasize
))
2936 (let ((marker (cl-case (org-element-type datum
)
2939 (strike-through "+")
2943 (org-element-contents datum
)
2947 (or (org-element-property :post-blank datum
)
2950 ;; ... LaTeX environments and fragments...
2951 ((latex-environment latex-fragment
)
2952 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2953 (list (org-export-expand datum nil
))))
2954 ;; ... sub/superscripts...
2955 ((subscript superscript
)
2956 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2957 (bracketp (org-element-property :use-brackets-p datum
)))
2958 (and (or (not sub
/super-p
)
2959 (and (eq sub
/super-p
'{}) (not bracketp
)))
2962 (if (eq (org-element-type datum
) 'subscript
)
2965 (and bracketp
"{")))
2966 (org-element-contents datum
)
2969 (and (org-element-property :post-blank datum
)
2971 (org-element-property :post-blank datum
)
2974 ;; Splice NEW at DATUM location in parse tree.
2975 (dolist (e new
(org-element-extract-element datum
))
2976 (unless (equal e
"") (org-element-insert-before e datum
))))))
2978 ;; Return modified parse tree.
2982 (defun org-export-as
2983 (backend &optional subtreep visible-only body-only ext-plist
)
2984 "Transcode current Org buffer into BACKEND code.
2986 BACKEND is either an export back-end, as returned by, e.g.,
2987 `org-export-create-backend', or a symbol referring to
2988 a registered back-end.
2990 If narrowing is active in the current buffer, only transcode its
2993 If a region is active, transcode that region.
2995 When optional argument SUBTREEP is non-nil, transcode the
2996 sub-tree at point, extracting information from the headline
2999 When optional argument VISIBLE-ONLY is non-nil, don't export
3000 contents of hidden elements.
3002 When optional argument BODY-ONLY is non-nil, only return body
3003 code, without surrounding template.
3005 Optional argument EXT-PLIST, when provided, is a property list
3006 with external parameters overriding Org default settings, but
3007 still inferior to file-local settings.
3009 Return code as a string."
3010 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3011 (org-export-barf-if-invalid-backend backend
)
3014 ;; Narrow buffer to an appropriate region or subtree for
3015 ;; parsing. If parsing subtree, be sure to remove main
3016 ;; headline, planning data and property drawer.
3017 (cond ((org-region-active-p)
3018 (narrow-to-region (region-beginning) (region-end)))
3020 (org-narrow-to-subtree)
3021 (goto-char (point-min))
3022 (org-end-of-meta-data)
3023 (narrow-to-region (point) (point-max))))
3024 ;; Initialize communication channel with original buffer
3025 ;; attributes, unavailable in its copy.
3026 (let* ((org-export-current-backend (org-export-backend-name backend
))
3027 (info (org-combine-plists
3028 (org-export--get-export-attributes
3029 backend subtreep visible-only body-only
)
3030 (org-export--get-buffer-attributes)))
3033 (mapcar (lambda (o) (and (eq (nth 4 o
) 'parse
) (nth 1 o
)))
3034 (append (org-export-get-all-options backend
)
3035 org-export-options-alist
))))
3037 ;; Update communication channel and get parse tree. Buffer
3038 ;; isn't parsed directly. Instead, all buffer modifications
3039 ;; and consequent parsing are undertaken in a temporary copy.
3040 (org-export-with-buffer-copy
3041 ;; Run first hook with current back-end's name as argument.
3042 (run-hook-with-args 'org-export-before-processing-hook
3043 (org-export-backend-name backend
))
3044 (org-export-expand-include-keyword)
3045 (org-export--delete-comment-trees)
3046 (org-macro-initialize-templates)
3047 (org-macro-replace-all (append org-macro-templates
3048 org-export-global-macros
)
3050 ;; Refresh buffer properties and radio targets after previous
3051 ;; potentially invasive changes.
3052 (org-set-regexps-and-options)
3053 (org-update-radio-target-regexp)
3054 ;; Possibly execute Babel code. Re-run a macro expansion
3055 ;; specifically for {{{results}}} since inline source blocks
3056 ;; may have generated some more. Refresh buffer properties
3057 ;; and radio targets another time.
3058 (when org-export-use-babel
3059 (org-babel-exp-process-buffer)
3060 (org-macro-replace-all '(("results" .
"$1")) parsed-keywords
)
3061 (org-set-regexps-and-options)
3062 (org-update-radio-target-regexp))
3063 ;; Run last hook with current back-end's name as argument.
3064 ;; Update buffer properties and radio targets one last time
3066 (goto-char (point-min))
3068 (run-hook-with-args 'org-export-before-parsing-hook
3069 (org-export-backend-name backend
)))
3070 (org-set-regexps-and-options)
3071 (org-update-radio-target-regexp)
3072 ;; Update communication channel with environment.
3075 info
(org-export-get-environment backend subtreep ext-plist
)))
3076 ;; De-activate uninterpreted data from parsed keywords.
3077 (dolist (entry (append (org-export-get-all-options backend
)
3078 org-export-options-alist
))
3080 (`(,p
,_
,_
,_ parse
)
3081 (let ((value (plist-get info p
)))
3084 (org-export--remove-uninterpreted-data value info
))))
3086 ;; Install user's and developer's filters.
3087 (setq info
(org-export-install-filters info
))
3088 ;; Call options filters and update export options. We do not
3089 ;; use `org-export-filter-apply-functions' here since the
3090 ;; arity of such filters is different.
3091 (let ((backend-name (org-export-backend-name backend
)))
3092 (dolist (filter (plist-get info
:filter-options
))
3093 (let ((result (funcall filter info backend-name
)))
3094 (when result
(setq info result
)))))
3096 (setq tree
(org-element-parse-buffer nil visible-only
))
3097 ;; Prune tree from non-exported elements and transform
3098 ;; uninterpreted elements or objects in both parse tree and
3099 ;; communication channel.
3100 (org-export--prune-tree tree info
)
3101 (org-export--remove-uninterpreted-data tree info
)
3102 ;; Call parse tree filters.
3104 (org-export-filter-apply-functions
3105 (plist-get info
:filter-parse-tree
) tree info
))
3106 ;; Now tree is complete, compute its properties and add them
3107 ;; to communication channel.
3108 (setq info
(org-export--collect-tree-properties tree info
))
3109 ;; Eventually transcode TREE. Wrap the resulting string into
3111 (let* ((body (org-element-normalize-string
3112 (or (org-export-data tree info
) "")))
3113 (inner-template (cdr (assq 'inner-template
3114 (plist-get info
:translate-alist
))))
3115 (full-body (org-export-filter-apply-functions
3116 (plist-get info
:filter-body
)
3117 (if (not (functionp inner-template
)) body
3118 (funcall inner-template body info
))
3120 (template (cdr (assq 'template
3121 (plist-get info
:translate-alist
)))))
3122 ;; Remove all text properties since they cannot be
3123 ;; retrieved from an external process. Finally call
3124 ;; final-output filter and return result.
3126 (org-export-filter-apply-functions
3127 (plist-get info
:filter-final-output
)
3128 (if (or (not (functionp template
)) body-only
) full-body
3129 (funcall template full-body info
))
3133 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3134 "Transcode STRING into BACKEND code.
3136 BACKEND is either an export back-end, as returned by, e.g.,
3137 `org-export-create-backend', or a symbol referring to
3138 a registered back-end.
3140 When optional argument BODY-ONLY is non-nil, only return body
3141 code, without preamble nor postamble.
3143 Optional argument EXT-PLIST, when provided, is a property list
3144 with external parameters overriding Org default settings, but
3145 still inferior to file-local settings.
3147 Return code as a string."
3150 (let ((org-inhibit-startup t
)) (org-mode))
3151 (org-export-as backend nil nil body-only ext-plist
)))
3154 (defun org-export-replace-region-by (backend)
3155 "Replace the active region by its export to BACKEND.
3156 BACKEND is either an export back-end, as returned by, e.g.,
3157 `org-export-create-backend', or a symbol referring to
3158 a registered back-end."
3159 (unless (org-region-active-p) (user-error "No active region to replace"))
3161 (org-export-string-as
3162 (delete-and-extract-region (region-beginning) (region-end)) backend t
)))
3165 (defun org-export-insert-default-template (&optional backend subtreep
)
3166 "Insert all export keywords with default values at beginning of line.
3168 BACKEND is a symbol referring to the name of a registered export
3169 back-end, for which specific export options should be added to
3170 the template, or `default' for default template. When it is nil,
3171 the user will be prompted for a category.
3173 If SUBTREEP is non-nil, export configuration will be set up
3174 locally for the subtree through node properties."
3176 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3177 (when (and subtreep
(org-before-first-heading-p))
3178 (user-error "No subtree to set export options for"))
3179 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3183 (org-completing-read
3184 "Options category: "
3187 (symbol-name (org-export-backend-name b
)))
3188 org-export-registered-backends
))
3191 ;; Populate OPTIONS and KEYWORDS.
3192 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3193 ((org-export-backend-p backend
)
3194 (org-export-backend-options backend
))
3195 (t (org-export-backend-options
3196 (org-export-get-backend backend
)))))
3197 (let ((keyword (nth 1 entry
))
3198 (option (nth 2 entry
)))
3200 (keyword (unless (assoc keyword keywords
)
3202 (if (eq (nth 4 entry
) 'split
)
3203 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3204 (eval (nth 3 entry
)))))
3205 (push (cons keyword value
) keywords
))))
3206 (option (unless (assoc option options
)
3207 (push (cons option
(eval (nth 3 entry
))) options
))))))
3208 ;; Move to an appropriate location in order to insert options.
3209 (unless subtreep
(beginning-of-line))
3210 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3214 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3215 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3218 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3220 (insert "#+options:")
3223 (< (+ width
(length (car items
)) 1) fill-column
))
3224 (let ((item (pop items
)))
3226 (cl-incf width
(1+ (length item
))))))
3228 ;; Then the rest of keywords, in the order specified in either
3229 ;; `org-export-options-alist' or respective export back-ends.
3230 (dolist (key (nreverse keywords
))
3231 (let ((val (cond ((equal (car key
) "DATE")
3234 (org-insert-time-stamp nil
))))
3235 ((equal (car key
) "TITLE")
3236 (or (let ((visited-file
3237 (buffer-file-name (buffer-base-buffer))))
3239 (file-name-sans-extension
3240 (file-name-nondirectory visited-file
))))
3241 (buffer-name (buffer-base-buffer))))
3243 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3246 (downcase (car key
))
3247 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3249 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3250 "Expand every include keyword in buffer.
3251 Optional argument INCLUDED is a list of included file names along
3252 with their line restriction, when appropriate. It is used to
3253 avoid infinite recursion. Optional argument DIR is the current
3254 working directory. It is used to properly resolve relative
3255 paths. Optional argument FOOTNOTES is a hash-table used for
3256 storing and resolving footnotes. It is created automatically."
3257 (let ((includer-file (buffer-file-name (buffer-base-buffer)))
3258 (case-fold-search t
)
3259 (file-prefix (make-hash-table :test
#'equal
))
3261 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3262 (include-re "^[ \t]*#\\+INCLUDE:"))
3263 ;; If :minlevel is not set the text-property
3264 ;; `:org-include-induced-level' will be used to determine the
3265 ;; relative level when expanding INCLUDE.
3266 ;; Only affects included Org documents.
3267 (goto-char (point-min))
3268 (while (re-search-forward include-re nil t
)
3269 (put-text-property (line-beginning-position) (line-end-position)
3270 :org-include-induced-level
3271 (1+ (org-reduced-level (or (org-current-level) 0)))))
3272 ;; Expand INCLUDE keywords.
3273 (goto-char (point-min))
3274 (while (re-search-forward include-re nil t
)
3275 (unless (org-in-commented-heading-p)
3276 (let ((element (save-match-data (org-element-at-point))))
3277 (when (eq (org-element-type element
) 'keyword
)
3279 ;; Extract arguments from keyword's value.
3280 (let* ((value (org-element-property :value element
))
3281 (ind (current-indentation))
3283 (coding-system-for-read
3284 (or (and (string-match ":coding +\\(\\S-+\\)>" value
)
3285 (prog1 (intern (match-string 1 value
))
3286 (setq value
(replace-match "" nil nil value
))))
3287 coding-system-for-read
))
3289 (and (string-match "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
3293 (let ((matched (match-string 1 value
)))
3294 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
3296 (setq location
(match-string 2 matched
))
3298 (replace-match "" nil nil matched
1)))
3299 (expand-file-name (org-strip-quotes matched
)
3301 (setq value
(replace-match "" nil nil value
)))))
3303 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
3305 (prog1 (org-not-nil (match-string 1 value
))
3306 (setq value
(replace-match "" nil nil value
)))))
3309 ":lines +\"\\([0-9]*-[0-9]*\\)\""
3311 (prog1 (match-string 1 value
)
3312 (setq value
(replace-match "" nil nil value
)))))
3314 ((string-match "\\<example\\>" value
) 'literal
)
3315 ((string-match "\\<export\\(?: +\\(.*\\)\\)?" value
)
3317 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3319 ;; Minimal level of included file defaults to the
3320 ;; child level of the current headline, if any, or
3321 ;; one. It only applies is the file is meant to be
3322 ;; included as an Org one.
3325 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3326 (prog1 (string-to-number (match-string 1 value
))
3327 (setq value
(replace-match "" nil nil value
)))
3328 (get-text-property (point)
3329 :org-include-induced-level
))))
3330 (args (and (eq env
'literal
) (match-string 1 value
)))
3331 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3332 (match-string 1 value
))))
3334 (delete-region (point) (line-beginning-position 2))
3337 ((not (file-readable-p file
))
3338 (error "Cannot include file %s" file
))
3339 ;; Check if files has already been parsed. Look after
3340 ;; inclusion lines too, as different parts of the same
3341 ;; file can be included too.
3342 ((member (list file lines
) included
)
3343 (error "Recursive file inclusion: %s" file
))
3348 (let ((ind-str (make-string ind ?\s
))
3349 (arg-str (if (stringp args
) (format " %s" args
) ""))
3351 (org-escape-code-in-string
3352 (org-export--prepare-file-contents file lines
))))
3353 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3354 ind-str block arg-str contents ind-str block
))))
3357 (let ((ind-str (make-string ind ?\s
))
3359 (org-export--prepare-file-contents file lines
)))
3360 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3361 ind-str block contents ind-str block
))))
3365 (let ((org-inhibit-startup t
)
3368 (org-export--inclusion-absolute-lines
3369 file location only-contents lines
)
3373 (org-export--prepare-file-contents
3374 file lines ind minlevel
3375 (or (gethash file file-prefix
)
3377 (cl-incf current-prefix
)
3381 (org-export-expand-include-keyword
3382 (cons (list file lines
) included
)
3383 (file-name-directory file
)
3386 ;; Expand footnotes after all files have been
3387 ;; included. Footnotes are stored at end of buffer.
3389 (org-with-wide-buffer
3390 (goto-char (point-max))
3391 (maphash (lambda (k v
)
3392 (insert (format "\n[fn:%s] %s\n" k v
)))
3393 footnotes
))))))))))))
3395 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3396 "Resolve absolute lines for an included file with file-link.
3398 FILE is string file-name of the file to include. LOCATION is a
3399 string name within FILE to be included (located via
3400 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3401 contents of the named element will be included, as determined
3402 Org-Element. If LINES is non-nil only those lines are included.
3404 Return a string of lines to be included in the format expected by
3405 `org-export--prepare-file-contents'."
3407 (insert-file-contents file
)
3408 (unless (eq major-mode
'org-mode
)
3409 (let ((org-inhibit-startup t
)) (org-mode)))
3411 ;; Enforce consistent search.
3412 (let ((org-link-search-must-match-exact-headline nil
))
3413 (org-link-search location
))
3415 (error "%s for %s::%s" (error-message-string err
) file location
)))
3416 (let* ((element (org-element-at-point))
3418 (and only-contents
(org-element-property :contents-begin element
))))
3420 (or contents-begin
(org-element-property :begin element
))
3421 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3422 (when (and only-contents
3423 (memq (org-element-type element
) '(headline inlinetask
)))
3424 ;; Skip planning line and property-drawer.
3425 (goto-char (point-min))
3426 (when (looking-at-p org-planning-line-re
) (forward-line))
3427 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3428 (unless (bolp) (forward-line))
3429 (narrow-to-region (point) (point-max))))
3431 (org-skip-whitespace)
3433 (let* ((lines (split-string lines
"-"))
3434 (lbeg (string-to-number (car lines
)))
3435 (lend (string-to-number (cadr lines
)))
3436 (beg (if (zerop lbeg
) (point-min)
3437 (goto-char (point-min))
3438 (forward-line (1- lbeg
))
3440 (end (if (zerop lend
) (point-max)
3442 (forward-line (1- lend
))
3444 (narrow-to-region beg end
)))
3445 (let ((end (point-max)))
3446 (goto-char (point-min))
3448 (let ((start-line (line-number-at-pos)))
3454 (while (< (point) end
) (cl-incf counter
) (forward-line))
3457 (defun org-export--update-included-link (file-dir includer-dir
)
3458 "Update relative file name of link at point, if possible.
3460 FILE-DIR is the directory of the file being included.
3461 INCLUDER-DIR is the directory of the file where the inclusion is
3464 Move point after the link."
3465 (let* ((link (org-element-link-parser))
3466 (path (org-element-property :path link
)))
3467 (if (or (not (string= "file" (org-element-property :type link
)))
3468 (file-remote-p path
)
3469 (file-name-absolute-p path
))
3470 (goto-char (org-element-property :end link
))
3471 (let ((new-path (file-relative-name (expand-file-name path file-dir
)
3473 (new-link (org-element-copy link
))
3474 (contents (and (org-element-property :contents-begin link
)
3476 (org-element-property :contents-begin link
)
3477 (org-element-property :contents-end link
)))))
3478 (org-element-put-property new-link
:path new-path
)
3479 (delete-region (org-element-property :begin link
)
3480 (org-element-property :end link
))
3481 (insert (org-element-link-interpreter new-link contents
))))))
3483 (defun org-export--prepare-file-contents
3484 (file &optional lines ind minlevel id footnotes includer
)
3485 "Prepare contents of FILE for inclusion and return it as a string.
3487 When optional argument LINES is a string specifying a range of
3488 lines, include only those lines.
3490 Optional argument IND, when non-nil, is an integer specifying the
3491 global indentation of returned contents. Since its purpose is to
3492 allow an included file to stay in the same environment it was
3493 created (e.g., a list item), it doesn't apply past the first
3494 headline encountered.
3496 Optional argument MINLEVEL, when non-nil, is an integer
3497 specifying the level that any top-level headline in the included
3500 Optional argument ID is an integer that will be inserted before
3501 each footnote definition and reference if FILE is an Org file.
3502 This is useful to avoid conflicts when more than one Org file
3503 with footnotes is included in a document.
3505 Optional argument FOOTNOTES is a hash-table to store footnotes in
3506 the included document.
3508 Optional argument INCLUDER is the file name where the inclusion
3511 (insert-file-contents file
)
3513 (let* ((lines (split-string lines
"-"))
3514 (lbeg (string-to-number (car lines
)))
3515 (lend (string-to-number (cadr lines
)))
3516 (beg (if (zerop lbeg
) (point-min)
3517 (goto-char (point-min))
3518 (forward-line (1- lbeg
))
3520 (end (if (zerop lend
) (point-max)
3521 (goto-char (point-min))
3522 (forward-line (1- lend
))
3524 (narrow-to-region beg end
)))
3525 ;; Adapt all file links within the included document that contain
3526 ;; relative paths in order to make these paths relative to the
3527 ;; base document, or absolute.
3529 (let ((file-dir (file-name-directory file
))
3530 (includer-dir (file-name-directory includer
)))
3531 (unless (file-equal-p file-dir includer-dir
)
3532 (goto-char (point-min))
3533 (unless (eq major-mode
'org-mode
)
3534 (let ((org-inhibit-startup t
)) (org-mode))) ;set regexps
3535 (let ((regexp (concat org-link-plain-re
"\\|" org-link-angle-re
)))
3536 (while (re-search-forward org-link-any-re nil t
)
3537 (let ((link (save-excursion
3539 (save-match-data (org-element-context)))))
3540 (when (eq 'link
(org-element-type link
))
3541 ;; Look for file links within link's description.
3542 ;; Org doesn't support such construct, but
3543 ;; `org-export-insert-image-links' may activate
3545 (let ((contents-begin
3546 (org-element-property :contents-begin link
))
3547 (begin (org-element-property :begin link
)))
3548 (when contents-begin
3550 (goto-char (org-element-property :contents-end link
))
3551 (while (re-search-backward regexp contents-begin t
)
3553 (org-export--update-included-link
3554 file-dir includer-dir
))
3555 (goto-char (match-beginning 0)))))
3556 ;; Update current link, if necessary.
3557 (when (string= "file" (org-element-property :type link
))
3559 (org-export--update-included-link
3560 file-dir includer-dir
))))))))))
3561 ;; Remove blank lines at beginning and end of contents. The logic
3562 ;; behind that removal is that blank lines around include keyword
3563 ;; override blank lines in included file.
3564 (goto-char (point-min))
3565 (org-skip-whitespace)
3567 (delete-region (point-min) (point))
3568 (goto-char (point-max))
3569 (skip-chars-backward " \r\t\n")
3571 (delete-region (point) (point-max))
3572 ;; If IND is set, preserve indentation of include keyword until
3573 ;; the first headline encountered.
3574 (when (and ind
(> ind
0))
3575 (unless (eq major-mode
'org-mode
)
3576 (let ((org-inhibit-startup t
)) (org-mode)))
3577 (goto-char (point-min))
3578 (let ((ind-str (make-string ind ?\s
)))
3579 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3580 ;; Do not move footnote definitions out of column 0.
3581 (unless (and (looking-at org-footnote-definition-re
)
3582 (eq (org-element-type (org-element-at-point))
3583 'footnote-definition
))
3586 ;; When MINLEVEL is specified, compute minimal level for headlines
3587 ;; in the file (CUR-MIN), and remove stars to each headline so
3588 ;; that headlines with minimal level have a level of MINLEVEL.
3590 (unless (eq major-mode
'org-mode
)
3591 (let ((org-inhibit-startup t
)) (org-mode)))
3592 (org-with-limited-levels
3593 (let ((levels (org-map-entries
3594 (lambda () (org-reduced-level (org-current-level))))))
3596 (let ((offset (- minlevel
(apply #'min levels
))))
3597 (unless (zerop offset
)
3598 (when org-odd-levels-only
(setq offset
(* offset
2)))
3599 ;; Only change stars, don't bother moving whole
3603 (if (< offset
0) (delete-char (abs offset
))
3604 (insert (make-string offset ?
*)))))))))))
3605 ;; Append ID to all footnote references and definitions, so they
3606 ;; become file specific and cannot collide with footnotes in other
3607 ;; included files. Further, collect relevant footnote definitions
3608 ;; outside of LINES, in order to reintroduce them later.
3610 (let ((marker-min (point-min-marker))
3611 (marker-max (point-max-marker))
3614 ;; Generate new label from LABEL by prefixing it with
3616 (format "-%d-%s" id label
)))
3619 ;; Replace OLD label with NEW in footnote F.
3621 (goto-char (+ (org-element-property :begin f
) 4))
3622 (looking-at (regexp-quote old
))
3623 (replace-match new
))))
3625 (goto-char (point-min))
3626 (while (re-search-forward org-footnote-re nil t
)
3627 (let ((footnote (save-excursion
3629 (org-element-context))))
3630 (when (memq (org-element-type footnote
)
3631 '(footnote-definition footnote-reference
))
3632 (let* ((label (org-element-property :label footnote
)))
3633 ;; Update the footnote-reference at point and collect
3634 ;; the new label, which is only used for footnotes
3637 (let ((seen (cdr (assoc label seen-alist
))))
3638 (if seen
(funcall set-new-label footnote label seen
)
3639 (let ((new (funcall get-new-label label
)))
3640 (push (cons label new
) seen-alist
)
3641 (org-with-wide-buffer
3642 (let* ((def (org-footnote-get-definition label
))
3645 (or (< beg marker-min
)
3646 (>= beg marker-max
)))
3647 ;; Store since footnote-definition is
3648 ;; outside of LINES.
3650 (org-element-normalize-string (nth 3 def
))
3652 (funcall set-new-label footnote label new
)))))))))
3653 (set-marker marker-min nil
)
3654 (set-marker marker-max nil
)))
3655 (org-element-normalize-string (buffer-string))))
3657 (defun org-export--copy-to-kill-ring-p ()
3658 "Return a non-nil value when output should be added to the kill ring.
3659 See also `org-export-copy-to-kill-ring'."
3660 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3661 (not (or executing-kbd-macro noninteractive
))
3662 (eq org-export-copy-to-kill-ring t
)))
3666 ;;; Tools For Back-Ends
3668 ;; A whole set of tools is available to help build new exporters. Any
3669 ;; function general enough to have its use across many back-ends
3670 ;; should be added here.
3672 ;;;; For Affiliated Keywords
3674 ;; `org-export-read-attribute' reads a property from a given element
3675 ;; as a plist. It can be used to normalize affiliated keywords'
3678 ;; Since captions can span over multiple lines and accept dual values,
3679 ;; their internal representation is a bit tricky. Therefore,
3680 ;; `org-export-get-caption' transparently returns a given element's
3681 ;; caption as a secondary string.
3683 (defun org-export-read-attribute (attribute element
&optional property
)
3684 "Turn ATTRIBUTE property from ELEMENT into a plist.
3686 When optional argument PROPERTY is non-nil, return the value of
3687 that property within attributes.
3689 This function assumes attributes are defined as \":keyword
3690 value\" pairs. It is appropriate for `:attr_html' like
3693 All values will become strings except the empty string and
3694 \"nil\", which will become nil. Also, values containing only
3695 double quotes will be read as-is, which means that \"\" value
3696 will become the empty string."
3697 (let* ((prepare-value
3700 (cond ((member str
'(nil "" "nil")) nil
)
3701 ((string-match "^\"\\(\"+\\)?\"$" str
)
3702 (or (match-string 1 str
) ""))
3705 (let ((value (org-element-property attribute element
)))
3707 (let ((s (mapconcat 'identity value
" ")) result
)
3708 (while (string-match
3709 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3711 (let ((value (substring s
0 (match-beginning 0))))
3712 (push (funcall prepare-value value
) result
))
3713 (push (intern (match-string 1 s
)) result
)
3714 (setq s
(substring s
(match-end 0))))
3715 ;; Ignore any string before first property with `cdr'.
3716 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3717 (if property
(plist-get attributes property
) attributes
)))
3719 (defun org-export-get-caption (element &optional shortp
)
3720 "Return caption from ELEMENT as a secondary string.
3722 When optional argument SHORTP is non-nil, return short caption,
3723 as a secondary string, instead.
3725 Caption lines are separated by a white space."
3726 (let ((full-caption (org-element-property :caption element
)) caption
)
3727 (dolist (line full-caption
(cdr caption
))
3728 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3730 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3733 ;;;; For Derived Back-ends
3735 ;; `org-export-with-backend' is a function allowing to locally use
3736 ;; another back-end to transcode some object or element. In a derived
3737 ;; back-end, it may be used as a fall-back function once all specific
3738 ;; cases have been treated.
3740 (defun org-export-with-backend (backend data
&optional contents info
)
3741 "Call a transcoder from BACKEND on DATA.
3742 BACKEND is an export back-end, as returned by, e.g.,
3743 `org-export-create-backend', or a symbol referring to
3744 a registered back-end. DATA is an Org element, object, secondary
3745 string or string. CONTENTS, when non-nil, is the transcoded
3746 contents of DATA element, as a string. INFO, when non-nil, is
3747 the communication channel used for export, as a plist."
3748 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3749 (org-export-barf-if-invalid-backend backend
)
3750 (let ((type (org-element-type data
)))
3751 (when (memq type
'(nil org-data
)) (error "No foreign transcoder available"))
3752 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3753 (transcoder (cdr (assq type all-transcoders
))))
3754 (unless (functionp transcoder
) (error "No foreign transcoder available"))
3759 :translate-alist all-transcoders
3760 :exported-data
(make-hash-table :test
#'eq
:size
401)))))
3761 ;; `:internal-references' are shared across back-ends.
3762 (prog1 (if (eq type
'plain-text
)
3763 (funcall transcoder data new-info
)
3764 (funcall transcoder data contents new-info
))
3765 (plist-put info
:internal-references
3766 (plist-get new-info
:internal-references
)))))))
3769 ;;;; For Export Snippets
3771 ;; Every export snippet is transmitted to the back-end. Though, the
3772 ;; latter will only retain one type of export-snippet, ignoring
3773 ;; others, based on the former's target back-end. The function
3774 ;; `org-export-snippet-backend' returns that back-end for a given
3777 (defun org-export-snippet-backend (export-snippet)
3778 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3779 Translation, with `org-export-snippet-translation-alist', is
3781 (let ((back-end (org-element-property :back-end export-snippet
)))
3783 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3789 ;; `org-export-collect-footnote-definitions' is a tool to list
3790 ;; actually used footnotes definitions in the whole parse tree, or in
3791 ;; a headline, in order to add footnote listings throughout the
3794 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3795 ;; back-ends, when they need to attach the footnote definition only to
3796 ;; the first occurrence of the corresponding label.
3798 ;; `org-export-get-footnote-definition' and
3799 ;; `org-export-get-footnote-number' provide easier access to
3800 ;; additional information relative to a footnote reference.
3802 (defun org-export-get-footnote-definition (footnote-reference info
)
3803 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3804 INFO is the plist used as a communication channel. If no such
3805 definition can be found, raise an error."
3806 (let ((label (org-element-property :label footnote-reference
)))
3807 (if (not label
) (org-element-contents footnote-reference
)
3808 (let ((cache (or (plist-get info
:footnote-definition-cache
)
3809 (let ((hash (make-hash-table :test
#'equal
)))
3810 (plist-put info
:footnote-definition-cache hash
)
3813 (gethash label cache
)
3815 (org-element-map (plist-get info
:parse-tree
)
3816 '(footnote-definition footnote-reference
)
3819 ;; Skip any footnote with a different label.
3820 ;; Also skip any standard footnote reference
3821 ;; with the same label since those cannot
3822 ;; contain a definition.
3823 ((not (equal (org-element-property :label f
) label
)) nil
)
3824 ((eq (org-element-property :type f
) 'standard
) nil
)
3825 ((org-element-contents f
))
3826 ;; Even if the contents are empty, we can not
3827 ;; return nil since that would eventually raise
3828 ;; the error. Instead, return the equivalent
3833 (error "Definition not found for footnote %s" label
))))))
3835 (defun org-export--footnote-reference-map
3836 (function data info
&optional body-first
)
3837 "Apply FUNCTION on every footnote reference in DATA.
3838 INFO is a plist containing export state. By default, as soon as
3839 a new footnote reference is encountered, FUNCTION is called onto
3840 its definition. However, if BODY-FIRST is non-nil, this step is
3841 delayed until the end of the process."
3842 (letrec ((definitions nil
)
3845 (lambda (data delayp
)
3846 ;; Search footnote references through DATA, filling
3847 ;; SEEN-REFS along the way. When DELAYP is non-nil,
3848 ;; store footnote definitions so they can be entered
3850 (org-element-map data
'footnote-reference
3852 (funcall function f
)
3853 (let ((--label (org-element-property :label f
)))
3854 (unless (and --label
(member --label seen-refs
))
3855 (when --label
(push --label seen-refs
))
3856 ;; Search for subsequent references in footnote
3857 ;; definition so numbering follows reading
3858 ;; logic, unless DELAYP in non-nil.
3861 (push (org-export-get-footnote-definition f info
)
3863 ;; Do not force entering inline definitions,
3864 ;; since `org-element-map' already traverses
3865 ;; them at the right time.
3866 ((eq (org-element-property :type f
) 'inline
))
3867 (t (funcall search-ref
3868 (org-export-get-footnote-definition f info
)
3871 ;; Don't enter footnote definitions since it will
3872 ;; happen when their first reference is found.
3873 ;; Moreover, if DELAYP is non-nil, make sure we
3874 ;; postpone entering definitions of inline references.
3875 (if delayp
'(footnote-definition footnote-reference
)
3876 'footnote-definition
)))))
3877 (funcall search-ref data body-first
)
3878 (funcall search-ref
(nreverse definitions
) nil
)))
3880 (defun org-export-collect-footnote-definitions (info &optional data body-first
)
3881 "Return an alist between footnote numbers, labels and definitions.
3883 INFO is the current export state, as a plist.
3885 Definitions are collected throughout the whole parse tree, or
3888 Sorting is done by order of references. As soon as a new
3889 reference is encountered, other references are searched within
3890 its definition. However, if BODY-FIRST is non-nil, this step is
3891 delayed after the whole tree is checked. This alters results
3892 when references are found in footnote definitions.
3894 Definitions either appear as Org data or as a secondary string
3895 for inlined footnotes. Unreferenced definitions are ignored."
3896 (let ((n 0) labels alist
)
3897 (org-export--footnote-reference-map
3899 ;; Collect footnote number, label and definition.
3900 (let ((l (org-element-property :label f
)))
3901 (unless (and l
(member l labels
))
3903 (push (list n l
(org-export-get-footnote-definition f info
)) alist
))
3904 (when l
(push l labels
))))
3905 (or data
(plist-get info
:parse-tree
)) info body-first
)
3908 (defun org-export-footnote-first-reference-p
3909 (footnote-reference info
&optional data body-first
)
3910 "Non-nil when a footnote reference is the first one for its label.
3912 FOOTNOTE-REFERENCE is the footnote reference being considered.
3913 INFO is a plist containing current export state.
3915 Search is done throughout the whole parse tree, or DATA when
3918 By default, as soon as a new footnote reference is encountered,
3919 other references are searched within its definition. However, if
3920 BODY-FIRST is non-nil, this step is delayed after the whole tree
3921 is checked. This alters results when references are found in
3922 footnote definitions."
3923 (let ((label (org-element-property :label footnote-reference
)))
3924 ;; Anonymous footnotes are always a first reference.
3927 (org-export--footnote-reference-map
3929 (let ((l (org-element-property :label f
)))
3930 (when (and l label
(string= label l
))
3931 (throw 'exit
(eq footnote-reference f
)))))
3932 (or data
(plist-get info
:parse-tree
)) info body-first
)))))
3934 (defun org-export-get-footnote-number (footnote info
&optional data body-first
)
3935 "Return number associated to a footnote.
3937 FOOTNOTE is either a footnote reference or a footnote definition.
3938 INFO is the plist containing export state.
3940 Number is unique throughout the whole parse tree, or DATA, when
3943 By default, as soon as a new footnote reference is encountered,
3944 counting process moves into its definition. However, if
3945 BODY-FIRST is non-nil, this step is delayed until the end of the
3946 process, leading to a different order when footnotes are nested."
3949 (label (org-element-property :label footnote
)))
3951 (org-export--footnote-reference-map
3953 (let ((l (org-element-property :label f
)))
3955 ;; Anonymous footnote match: return number.
3956 ((and (not l
) (not label
) (eq footnote f
)) (throw 'exit
(1+ count
)))
3957 ;; Labels match: return number.
3958 ((and label l
(string= label l
)) (throw 'exit
(1+ count
)))
3959 ;; Otherwise store label and increase counter if label
3960 ;; wasn't encountered yet.
3961 ((not l
) (cl-incf count
))
3962 ((not (member l seen
)) (push l seen
) (cl-incf count
)))))
3963 (or data
(plist-get info
:parse-tree
)) info body-first
))))
3968 ;; `org-export-get-relative-level' is a shortcut to get headline
3969 ;; level, relatively to the lower headline level in the parsed tree.
3971 ;; `org-export-get-headline-number' returns the section number of an
3972 ;; headline, while `org-export-number-to-roman' allows it to be
3973 ;; converted to roman numbers. With an optional argument,
3974 ;; `org-export-get-headline-number' returns a number to unnumbered
3975 ;; headlines (used for internal id).
3977 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3978 ;; `org-export-last-sibling-p' are three useful predicates when it
3979 ;; comes to fulfill the `:headline-levels' property.
3981 ;; `org-export-get-tags', `org-export-get-category' and
3982 ;; `org-export-get-node-property' extract useful information from an
3983 ;; headline or a parent headline. They all handle inheritance.
3985 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3986 ;; as a secondary string, suitable for table of contents. It falls
3987 ;; back onto default title.
3989 (defun org-export-get-relative-level (headline info
)
3990 "Return HEADLINE relative level within current parsed tree.
3991 INFO is a plist holding contextual information."
3992 (+ (org-element-property :level headline
)
3993 (or (plist-get info
:headline-offset
) 0)))
3995 (defun org-export-low-level-p (headline info
)
3996 "Non-nil when HEADLINE is considered as low level.
3998 INFO is a plist used as a communication channel.
4000 A low level headlines has a relative level greater than
4001 `:headline-levels' property value.
4003 Return value is the difference between HEADLINE relative level
4004 and the last level being considered as high enough, or nil."
4005 (let ((limit (plist-get info
:headline-levels
)))
4006 (when (wholenump limit
)
4007 (let ((level (org-export-get-relative-level headline info
)))
4008 (and (> level limit
) (- level limit
))))))
4010 (defun org-export-get-headline-number (headline info
)
4011 "Return numbered HEADLINE numbering as a list of numbers.
4012 INFO is a plist holding contextual information."
4013 (and (org-export-numbered-headline-p headline info
)
4014 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
4016 (defun org-export-numbered-headline-p (headline info
)
4017 "Return a non-nil value if HEADLINE element should be numbered.
4018 INFO is a plist used as a communication channel."
4019 (unless (org-not-nil (org-export-get-node-property :UNNUMBERED headline t
))
4020 (let ((sec-num (plist-get info
:section-numbers
))
4021 (level (org-export-get-relative-level headline info
)))
4022 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
4024 (defun org-export-number-to-roman (n)
4025 "Convert integer N into a roman numeral."
4026 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
4027 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
4028 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
4032 (number-to-string n
)
4034 (if (>= n
(caar roman
))
4035 (setq n
(- n
(caar roman
))
4036 res
(concat res
(cdar roman
)))
4040 (defun org-export-get-tags (element info
&optional tags inherited
)
4041 "Return list of tags associated to ELEMENT.
4043 ELEMENT has either an `headline' or an `inlinetask' type. INFO
4044 is a plist used as a communication channel.
4046 When non-nil, optional argument TAGS should be a list of strings.
4047 Any tag belonging to this list will also be removed.
4049 When optional argument INHERITED is non-nil, tags can also be
4050 inherited from parent headlines and FILETAGS keywords."
4052 (lambda (tag) (member tag tags
))
4053 (if (not inherited
) (org-element-property :tags element
)
4054 ;; Build complete list of inherited tags.
4055 (let ((current-tag-list (org-element-property :tags element
)))
4056 (dolist (parent (org-element-lineage element
))
4057 (dolist (tag (org-element-property :tags parent
))
4058 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
4059 (not (member tag current-tag-list
)))
4060 (push tag current-tag-list
))))
4061 ;; Add FILETAGS keywords and return results.
4062 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
4064 (defun org-export-get-node-property (property datum
&optional inherited
)
4065 "Return node PROPERTY value for DATUM.
4067 PROPERTY is an upcase symbol (e.g., `:COOKIE_DATA'). DATUM is an
4070 If optional argument INHERITED is non-nil, the value can be
4071 inherited from a parent headline.
4073 Return value is a string or nil."
4074 (let ((headline (if (eq (org-element-type datum
) 'headline
) datum
4075 (org-export-get-parent-headline datum
))))
4076 (if (not inherited
) (org-element-property property datum
)
4077 (let ((parent headline
))
4080 (when (plist-member (nth 1 parent
) property
)
4081 (throw 'found
(org-element-property property parent
)))
4082 (setq parent
(org-element-property :parent parent
))))))))
4084 (defun org-export-get-category (blob info
)
4085 "Return category for element or object BLOB.
4087 INFO is a plist used as a communication channel.
4089 CATEGORY is automatically inherited from a parent headline, from
4090 #+CATEGORY: keyword or created out of original file name. If all
4091 fail, the fall-back value is \"???\"."
4092 (or (org-export-get-node-property :CATEGORY blob t
)
4093 (org-element-map (plist-get info
:parse-tree
) 'keyword
4095 (when (equal (org-element-property :key kwd
) "CATEGORY")
4096 (org-element-property :value kwd
)))
4098 (let ((file (plist-get info
:input-file
)))
4099 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
4102 (defun org-export-get-alt-title (headline _
)
4103 "Return alternative title for HEADLINE, as a secondary string.
4104 If no optional title is defined, fall-back to the regular title."
4105 (let ((alt (org-element-property :ALT_TITLE headline
)))
4106 (if alt
(org-element-parse-secondary-string
4107 alt
(org-element-restriction 'headline
) headline
)
4108 (org-element-property :title headline
))))
4110 (defun org-export-first-sibling-p (blob info
)
4111 "Non-nil when BLOB is the first sibling in its parent.
4112 BLOB is an element or an object. If BLOB is a headline, non-nil
4113 means it is the first sibling in the sub-tree. INFO is a plist
4114 used as a communication channel."
4115 (memq (org-element-type (org-export-get-previous-element blob info
))
4118 (defun org-export-last-sibling-p (datum info
)
4119 "Non-nil when DATUM is the last sibling in its parent.
4120 DATUM is an element or an object. INFO is a plist used as
4121 a communication channel."
4122 (let ((next (org-export-get-next-element datum info
)))
4124 (and (eq 'headline
(org-element-type datum
))
4125 (> (org-element-property :level datum
)
4126 (org-element-property :level next
))))))
4131 ;; `org-export-get-date' returns a date appropriate for the document
4132 ;; to about to be exported. In particular, it takes care of
4133 ;; `org-export-date-timestamp-format'.
4135 (defun org-export-get-date (info &optional fmt
)
4136 "Return date value for the current document.
4138 INFO is a plist used as a communication channel. FMT, when
4139 non-nil, is a time format string that will be applied on the date
4140 if it consists in a single timestamp object. It defaults to
4141 `org-export-date-timestamp-format' when nil.
4143 A proper date can be a secondary string, a string or nil. It is
4144 meant to be translated with `org-export-data' or alike."
4145 (let ((date (plist-get info
:date
))
4146 (fmt (or fmt org-export-date-timestamp-format
)))
4147 (cond ((not date
) nil
)
4150 (eq (org-element-type (car date
)) 'timestamp
))
4151 (org-timestamp-format (car date
) fmt
))
4157 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
4158 ;; in `org-link-parameters'.
4160 ;; `org-export-get-coderef-format' returns an appropriate format
4161 ;; string for coderefs.
4163 ;; `org-export-inline-image-p' returns a non-nil value when the link
4164 ;; provided should be considered as an inline image.
4166 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
4167 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
4168 ;; returns an appropriate unique identifier.
4170 ;; `org-export-resolve-id-link' returns the first headline with
4171 ;; specified id or custom-id in parse tree, the path to the external
4172 ;; file with the id.
4174 ;; `org-export-resolve-coderef' associates a reference to a line
4175 ;; number in the element it belongs, or returns the reference itself
4176 ;; when the element isn't numbered.
4178 ;; `org-export-file-uri' expands a filename as stored in :path value
4179 ;; of a "file" link into a file URI.
4181 ;; Broken links raise a `org-link-broken' error, which is caught by
4182 ;; `org-export-data' for further processing, depending on
4183 ;; `org-export-with-broken-links' value.
4185 (org-define-error 'org-link-broken
"Unable to resolve link; aborting")
4187 (defun org-export-custom-protocol-maybe (link desc backend
)
4188 "Try exporting LINK with a dedicated function.
4190 DESC is its description, as a string, or nil. BACKEND is the
4191 back-end used for export, as a symbol.
4193 Return output as a string, or nil if no protocol handles LINK.
4195 A custom protocol has precedence over regular back-end export.
4196 The function ignores links with an implicit type (e.g.,
4198 (let ((type (org-element-property :type link
)))
4199 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
4201 (let ((protocol (org-link-get-parameter type
:export
)))
4202 (and (functionp protocol
)
4204 (org-element-property :path link
)
4208 (defun org-export-get-coderef-format (path desc
)
4209 "Return format string for code reference link.
4210 PATH is the link path. DESC is its description."
4212 (cond ((not desc
) "%s")
4213 ((string-match (regexp-quote (concat "(" path
")")) desc
)
4214 (replace-match "%s" t t desc
))
4217 (defun org-export-inline-image-p (link &optional rules
)
4218 "Non-nil if LINK object points to an inline image.
4220 Optional argument is a set of RULES defining inline images. It
4221 is an alist where associations have the following shape:
4225 Applying a rule means apply REGEXP against LINK's path when its
4226 type is TYPE. The function will return a non-nil value if any of
4227 the provided rules is non-nil. The default rule is
4228 `org-export-default-inline-image-rule'.
4230 This only applies to links without a description."
4231 (and (not (org-element-contents link
))
4232 (let ((case-fold-search t
))
4233 (cl-some (lambda (rule)
4234 (and (string= (org-element-property :type link
) (car rule
))
4235 (string-match-p (cdr rule
)
4236 (org-element-property :path link
))))
4237 (or rules org-export-default-inline-image-rule
)))))
4239 (defun org-export-insert-image-links (data info
&optional rules
)
4240 "Insert image links in DATA.
4242 Org syntax does not support nested links. Nevertheless, some
4243 export back-ends support images as descriptions of links. Since
4244 images are really links to image files, we need to make an
4245 exception about links nesting.
4247 This function recognizes links whose contents are really images
4248 and turn them into proper nested links. It is meant to be used
4249 as a parse tree filter in back-ends supporting such constructs.
4251 DATA is a parse tree. INFO is the current state of the export
4252 process, as a plist.
4254 A description is a valid images if it matches any rule in RULES,
4255 if non-nil, or `org-export-default-inline-image-rule' otherwise.
4256 See `org-export-inline-image-p' for more information about the
4259 Return modified DATA."
4260 (let ((link-re (format "\\`\\(?:%s\\|%s\\)\\'"
4263 (case-fold-search t
))
4264 (org-element-map data
'link
4266 (let ((contents (org-element-interpret-data (org-element-contents l
))))
4267 (when (and (org-string-nw-p contents
)
4268 (string-match link-re contents
))
4269 (let ((type (match-string 1 contents
))
4270 (path (match-string 2 contents
)))
4271 (when (cl-some (lambda (rule)
4272 (and (string= type
(car rule
))
4273 (string-match-p (cdr rule
) path
)))
4274 (or rules org-export-default-inline-image-rule
))
4275 ;; Replace contents with image link.
4276 (org-element-adopt-elements
4277 (org-element-set-contents l nil
)
4279 (save-excursion (insert contents
))
4280 (org-element-link-parser))))))))
4284 (defun org-export-resolve-coderef (ref info
)
4285 "Resolve a code reference REF.
4287 INFO is a plist used as a communication channel.
4289 Return associated line number in source code, or REF itself,
4290 depending on src-block or example element's switches. Throw an
4291 error if no block contains REF."
4292 (or (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
4295 (insert (org-trim (org-element-property :value el
)))
4296 (let* ((label-fmt (or (org-element-property :label-fmt el
)
4297 org-coderef-label-format
))
4298 (ref-re (org-src-coderef-regexp label-fmt ref
)))
4299 ;; Element containing REF is found. Resolve it to
4300 ;; either a label or a line number, as needed.
4301 (when (re-search-backward ref-re nil t
)
4302 (if (org-element-property :use-labels el
) ref
4303 (+ (or (org-export-get-loc el info
) 0)
4304 (line-number-at-pos)))))))
4306 (signal 'org-link-broken
(list ref
))))
4308 (defun org-export-search-cells (datum)
4309 "List search cells for element or object DATUM.
4311 A search cell follows the pattern (TYPE . SEARCH) where
4313 TYPE is a symbol among `headline', `custom-id', `target' and
4316 SEARCH is the string a link is expected to match. More
4319 - headline's title, as a list of strings, if TYPE is
4322 - CUSTOM_ID value, as a string, if TYPE is `custom-id'.
4324 - target's or radio-target's name as a list of strings if
4327 - NAME affiliated keyword if TYPE is `other'.
4329 A search cell is the internal representation of a fuzzy link. It
4330 ignores white spaces and statistics cookies, if applicable."
4331 (pcase (org-element-type datum
)
4333 (let ((title (split-string
4334 (replace-regexp-in-string
4335 "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]" ""
4336 (org-element-property :raw-value datum
)))))
4339 (cons 'headline title
)
4341 (let ((custom-id (org-element-property :custom-id datum
)))
4342 (and custom-id
(cons 'custom-id custom-id
)))))))
4344 (list (cons 'target
(split-string (org-element-property :value datum
)))))
4345 ((and (let name
(org-element-property :name datum
))
4347 (list (cons 'other
(split-string name
))))
4350 (defun org-export-string-to-search-cell (s)
4351 "Return search cells associated to string S.
4352 S is either the path of a fuzzy link or a search option, i.e., it
4353 tries to match either a headline (through custom ID or title),
4354 a target or a named element."
4355 (pcase (string-to-char s
)
4356 (?
* (list (cons 'headline
(split-string (substring s
1)))))
4357 (?
# (list (cons 'custom-id
(substring s
1))))
4358 ((let search
(split-string s
))
4359 (list (cons 'target search
) (cons 'other search
)))))
4361 (defun org-export-match-search-cell-p (datum cells
)
4362 "Non-nil when DATUM matches search cells CELLS.
4363 DATUM is an element or object. CELLS is a list of search cells,
4364 as returned by `org-export-search-cells'."
4365 (let ((targets (org-export-search-cells datum
)))
4366 (and targets
(cl-some (lambda (cell) (member cell targets
)) cells
))))
4368 (defun org-export-resolve-fuzzy-link (link info
&rest pseudo-types
)
4369 "Return LINK destination.
4371 INFO is a plist holding contextual information.
4373 Return value can be an object or an element:
4375 - If LINK path matches a target object (i.e. <<path>>) return it.
4377 - If LINK path exactly matches the name affiliated keyword
4378 (i.e. #+NAME: path) of an element, return that element.
4380 - If LINK path exactly matches any headline name, return that
4383 - Otherwise, throw an error.
4385 PSEUDO-TYPES are pseudo-elements types, i.e., elements defined
4386 specifically in an export back-end, that could have a name
4389 Assume LINK type is \"fuzzy\". White spaces are not
4391 (let* ((search-cells (org-export-string-to-search-cell
4392 (org-element-property :path link
)))
4393 (link-cache (or (plist-get info
:resolve-fuzzy-link-cache
)
4394 (let ((table (make-hash-table :test
#'eq
)))
4395 (plist-put info
:resolve-fuzzy-link-cache table
)
4397 (cached (gethash search-cells link-cache
'not-found
)))
4398 (if (not (eq cached
'not-found
)) cached
4400 (org-element-map (plist-get info
:parse-tree
)
4401 (append pseudo-types
'(target) org-element-all-elements
)
4403 (and (org-export-match-search-cell-p datum search-cells
)
4406 (signal 'org-link-broken
(list (org-element-property :path link
))))
4409 ;; There can be multiple matches for un-typed searches, i.e.,
4410 ;; for searches not starting with # or *. In this case,
4411 ;; prioritize targets and names over headline titles.
4412 ;; Matching both a name and a target is not valid, and
4413 ;; therefore undefined.
4414 (or (cl-some (lambda (datum)
4415 (and (not (eq (org-element-type datum
) 'headline
))
4421 (defun org-export-resolve-id-link (link info
)
4422 "Return headline referenced as LINK destination.
4424 INFO is a plist used as a communication channel.
4426 Return value can be the headline element matched in current parse
4427 tree or a file name. Assume LINK type is either \"id\" or
4428 \"custom-id\". Throw an error if no match is found."
4429 (let ((id (org-element-property :path link
)))
4430 ;; First check if id is within the current parse tree.
4431 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4433 (when (or (equal (org-element-property :ID headline
) id
)
4434 (equal (org-element-property :CUSTOM_ID headline
) id
))
4437 ;; Otherwise, look for external files.
4438 (cdr (assoc id
(plist-get info
:id-alist
)))
4439 (signal 'org-link-broken
(list id
)))))
4441 (defun org-export-resolve-radio-link (link info
)
4442 "Return radio-target object referenced as LINK destination.
4444 INFO is a plist used as a communication channel.
4446 Return value can be a radio-target object or nil. Assume LINK
4447 has type \"radio\"."
4448 (let ((path (replace-regexp-in-string
4449 "[ \r\t\n]+" " " (org-element-property :path link
))))
4450 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4452 (and (eq (compare-strings
4453 (replace-regexp-in-string
4454 "[ \r\t\n]+" " " (org-element-property :value radio
))
4455 nil nil path nil nil t
)
4458 info
'first-match
)))
4460 (defun org-export-file-uri (filename)
4461 "Return file URI associated to FILENAME."
4462 (cond ((string-prefix-p "//" filename
) (concat "file:" filename
))
4463 ((not (file-name-absolute-p filename
)) filename
)
4464 ((file-remote-p filename
) (concat "file:/" filename
))
4466 (let ((fullname (expand-file-name filename
)))
4467 (concat (if (string-prefix-p "/" fullname
) "file://" "file:///")
4472 ;; `org-export-get-reference' associate a unique reference for any
4473 ;; object or element. It uses `org-export-new-reference' and
4474 ;; `org-export-format-reference' to, respectively, generate new
4475 ;; internal references and turn them into a string suitable for
4478 ;; `org-export-get-ordinal' associates a sequence number to any object
4481 (defun org-export-new-reference (references)
4482 "Return a unique reference, among REFERENCES.
4483 REFERENCES is an alist whose values are in-use references, as
4484 numbers. Returns a number, which is the internal representation
4485 of a reference. See also `org-export-format-reference'."
4486 ;; Generate random 7 digits hexadecimal numbers. Collisions
4487 ;; increase exponentially with the numbers of references. However,
4488 ;; the odds for encountering at least one collision with 1000 active
4489 ;; references in the same document are roughly 0.2%, so this
4490 ;; shouldn't be the bottleneck.
4491 (let ((new (random #x10000000
)))
4492 (while (rassq new references
) (setq new
(random #x10000000
)))
4495 (defun org-export-format-reference (reference)
4496 "Format REFERENCE into a string.
4497 REFERENCE is a number representing a reference, as returned by
4498 `org-export-new-reference', which see."
4499 (format "org%07x" reference
))
4501 (defun org-export-get-reference (datum info
)
4502 "Return a unique reference for DATUM, as a string.
4504 DATUM is either an element or an object. INFO is the current
4505 export state, as a plist.
4507 References for the current document are stored in
4508 `:internal-references' property. Its value is an alist with
4509 associations of the following types:
4511 (REFERENCE . DATUM) and (SEARCH-CELL . ID)
4513 REFERENCE is the reference string to be used for object or
4514 element DATUM. SEARCH-CELL is a search cell, as returned by
4515 `org-export-search-cells'. ID is a number or a string uniquely
4516 identifying DATUM within the document.
4518 This function also checks `:crossrefs' property for search cells
4519 matching DATUM before creating a new reference."
4520 (let ((cache (plist-get info
:internal-references
)))
4521 (or (car (rassq datum cache
))
4522 (let* ((crossrefs (plist-get info
:crossrefs
))
4523 (cells (org-export-search-cells datum
))
4524 ;; Preserve any pre-existing association between
4525 ;; a search cell and a reference, i.e., when some
4526 ;; previously published document referenced a location
4527 ;; within current file (see
4528 ;; `org-publish-resolve-external-link').
4530 ;; However, there is no guarantee that search cells are
4531 ;; unique, e.g., there might be duplicate custom ID or
4532 ;; two headings with the same title in the file.
4534 ;; As a consequence, before re-using any reference to
4535 ;; an element or object, we check that it doesn't refer
4536 ;; to a previous element or object.
4539 (let ((stored (cdr (assoc cell crossrefs
))))
4541 (let ((old (org-export-format-reference stored
)))
4542 (and (not (assoc old cache
)) stored
)))))
4544 (org-export-new-reference cache
)))
4545 (reference-string (org-export-format-reference new
)))
4546 ;; Cache contains both data already associated to
4547 ;; a reference and in-use internal references, so as to make
4548 ;; unique references.
4549 (dolist (cell cells
) (push (cons cell new
) cache
))
4550 ;; Retain a direct association between reference string and
4551 ;; DATUM since (1) not every object or element can be given
4552 ;; a search cell (2) it permits quick lookup.
4553 (push (cons reference-string datum
) cache
)
4554 (plist-put info
:internal-references cache
)
4555 reference-string
))))
4557 (defun org-export-get-ordinal (element info
&optional types predicate
)
4558 "Return ordinal number of an element or object.
4560 ELEMENT is the element or object considered. INFO is the plist
4561 used as a communication channel.
4563 Optional argument TYPES, when non-nil, is a list of element or
4564 object types, as symbols, that should also be counted in.
4565 Otherwise, only provided element's type is considered.
4567 Optional argument PREDICATE is a function returning a non-nil
4568 value if the current element or object should be counted in. It
4569 accepts two arguments: the element or object being considered and
4570 the plist used as a communication channel. This allows counting
4571 only a certain type of object (i.e. inline images).
4573 Return value is a list of numbers if ELEMENT is a headline or an
4574 item. It is nil for keywords. It represents the footnote number
4575 for footnote definitions and footnote references. If ELEMENT is
4576 a target, return the same value as if ELEMENT was the closest
4577 table, item or headline containing the target. In any other
4578 case, return the sequence number of ELEMENT among elements or
4579 objects of the same type."
4580 ;; Ordinal of a target object refer to the ordinal of the closest
4581 ;; table, item, or headline containing the object.
4582 (when (eq (org-element-type element
) 'target
)
4584 (org-element-lineage
4586 '(footnote-definition footnote-reference headline item table
))))
4587 (cl-case (org-element-type element
)
4588 ;; Special case 1: A headline returns its number as a list.
4589 (headline (org-export-get-headline-number element info
))
4590 ;; Special case 2: An item returns its number as a list.
4591 (item (let ((struct (org-element-property :structure element
)))
4592 (org-list-get-item-number
4593 (org-element-property :begin element
)
4595 (org-list-prevs-alist struct
)
4596 (org-list-parents-alist struct
))))
4597 ((footnote-definition footnote-reference
)
4598 (org-export-get-footnote-number element info
))
4601 ;; Increment counter until ELEMENT is found again.
4602 (org-element-map (plist-get info
:parse-tree
)
4603 (or types
(org-element-type element
))
4606 ((eq element el
) (1+ counter
))
4607 ((not predicate
) (cl-incf counter
) nil
)
4608 ((funcall predicate el info
) (cl-incf counter
) nil
)))
4609 info
'first-match
)))))
4614 ;; `org-export-get-loc' counts number of code lines accumulated in
4615 ;; src-block or example-block elements with a "+n" switch until
4616 ;; a given element, excluded. Note: "-n" switches reset that count.
4618 ;; `org-export-unravel-code' extracts source code (along with a code
4619 ;; references alist) from an `element-block' or `src-block' type
4622 ;; `org-export-format-code' applies a formatting function to each line
4623 ;; of code, providing relative line number and code reference when
4624 ;; appropriate. Since it doesn't access the original element from
4625 ;; which the source code is coming, it expects from the code calling
4626 ;; it to know if lines should be numbered and if code references
4629 ;; Eventually, `org-export-format-code-default' is a higher-level
4630 ;; function (it makes use of the two previous functions) which handles
4631 ;; line numbering and code references inclusion, and returns source
4632 ;; code in a format suitable for plain text or verbatim output.
4634 (defun org-export-get-loc (element info
)
4635 "Return count of lines of code before ELEMENT.
4637 ELEMENT is an example-block or src-block element. INFO is the
4638 plist used as a communication channel.
4640 Count includes every line of code in example-block or src-block
4641 with a \"+n\" or \"-n\" switch before block. Return nil if
4642 ELEMENT doesn't allow line numbering."
4643 (pcase (org-element-property :number-lines element
)
4647 (org-element-map (plist-get info
:parse-tree
) '(src-block example-block
)
4649 ;; ELEMENT is reached: Quit loop and return locs.
4650 (if (eq el element
) (+ loc n
)
4651 ;; Only count lines from src-block and example-block
4652 ;; elements with a "+n" or "-n" switch.
4653 (let ((linum (org-element-property :number-lines el
)))
4655 (let ((lines (org-count-lines
4656 (org-element-property :value el
))))
4657 ;; Accumulate locs or reset them.
4659 (`(new .
,n
) (setq loc
(+ n lines
)))
4660 (`(continued .
,n
) (cl-incf loc
(+ n lines
)))))))
4661 nil
)) ;Return nil to stay in the loop.
4662 info
'first-match
)))))
4664 (defun org-export-unravel-code (element)
4665 "Clean source code and extract references out of it.
4667 ELEMENT has either a `src-block' an `example-block' type.
4669 Return a cons cell whose CAR is the source code, cleaned from any
4670 reference, protective commas and spurious indentation, and CDR is
4671 an alist between relative line number (integer) and name of code
4672 reference on that line (string)."
4673 (let* ((line 0) refs
4674 (value (org-element-property :value element
))
4675 ;; Remove global indentation from code, if necessary. Also
4676 ;; remove final newline character, since it doesn't belongs
4677 ;; to the code proper.
4678 (code (replace-regexp-in-string
4680 (if (or org-src-preserve-indentation
4681 (org-element-property :preserve-indent element
))
4683 (org-remove-indentation value
))))
4684 ;; Build a regexp matching a loc with a reference.
4685 (ref-re (org-src-coderef-regexp (org-src-coderef-format element
))))
4688 ;; Code with references removed.
4692 (if (not (string-match ref-re loc
)) loc
4693 ;; Ref line: remove ref, and add its position in REFS.
4694 (push (cons line
(match-string 3 loc
)) refs
)
4695 (replace-match "" nil nil loc
1)))
4696 (split-string code
"\n") "\n")
4700 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4701 "Format CODE by applying FUN line-wise and return it.
4703 CODE is a string representing the code to format. FUN is
4704 a function. It must accept three arguments: a line of
4705 code (string), the current line number (integer) or nil and the
4706 reference associated to the current line (string) or nil.
4708 Optional argument NUM-LINES can be an integer representing the
4709 number of code lines accumulated until the current code. Line
4710 numbers passed to FUN will take it into account. If it is nil,
4711 FUN's second argument will always be nil. This number can be
4712 obtained with `org-export-get-loc' function.
4714 Optional argument REF-ALIST can be an alist between relative line
4715 number (i.e. ignoring NUM-LINES) and the name of the code
4716 reference on it. If it is nil, FUN's third argument will always
4717 be nil. It can be obtained through the use of
4718 `org-export-unravel-code' function."
4719 (let ((--locs (split-string code
"\n"))
4725 (let ((--ref (cdr (assq --line ref-alist
))))
4726 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4730 (defun org-export-format-code-default (element info
)
4731 "Return source code from ELEMENT, formatted in a standard way.
4733 ELEMENT is either a `src-block' or `example-block' element. INFO
4734 is a plist used as a communication channel.
4736 This function takes care of line numbering and code references
4737 inclusion. Line numbers, when applicable, appear at the
4738 beginning of the line, separated from the code by two white
4739 spaces. Code references, on the other hand, appear flushed to
4740 the right, separated by six white spaces from the widest line of
4742 ;; Extract code and references.
4743 (let* ((code-info (org-export-unravel-code element
))
4744 (code (car code-info
))
4745 (code-lines (split-string code
"\n")))
4746 (if (null code-lines
) ""
4747 (let* ((refs (and (org-element-property :retain-labels element
)
4749 ;; Handle line numbering.
4750 (num-start (org-export-get-loc element info
))
4754 (length (number-to-string
4755 (+ (length code-lines
) num-start
))))))
4756 ;; Prepare references display, if required. Any reference
4757 ;; should start six columns after the widest line of code,
4758 ;; wrapped with parenthesis.
4760 (+ (apply 'max
(mapcar 'length code-lines
))
4761 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4762 (org-export-format-code
4764 (lambda (loc line-num ref
)
4765 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4770 (concat (make-string (- (+ 6 max-width
)
4771 (+ (length loc
) (length number-str
)))
4773 (format "(%s)" ref
))))))
4779 ;; `org-export-table-has-special-column-p' and and
4780 ;; `org-export-table-row-is-special-p' are predicates used to look for
4781 ;; meta-information about the table structure.
4783 ;; `org-table-has-header-p' tells when the rows before the first rule
4784 ;; should be considered as table's header.
4786 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4787 ;; and `org-export-table-cell-borders' extract information from
4788 ;; a table-cell element.
4790 ;; `org-export-table-dimensions' gives the number on rows and columns
4791 ;; in the table, ignoring horizontal rules and special columns.
4792 ;; `org-export-table-cell-address', given a table-cell object, returns
4793 ;; the absolute address of a cell. On the other hand,
4794 ;; `org-export-get-table-cell-at' does the contrary.
4796 ;; `org-export-table-cell-starts-colgroup-p',
4797 ;; `org-export-table-cell-ends-colgroup-p',
4798 ;; `org-export-table-row-starts-rowgroup-p',
4799 ;; `org-export-table-row-ends-rowgroup-p',
4800 ;; `org-export-table-row-starts-header-p',
4801 ;; `org-export-table-row-ends-header-p' and
4802 ;; `org-export-table-row-in-header-p' indicate position of current row
4803 ;; or cell within the table.
4805 (defun org-export-table-has-special-column-p (table)
4806 "Non-nil when TABLE has a special column.
4807 All special columns will be ignored during export."
4808 ;; The table has a special column when every first cell of every row
4809 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4810 ;; "*" "_" and "^". Though, do not consider a first column
4811 ;; containing only empty cells as special.
4812 (let ((special-column?
'empty
))
4814 (dolist (row (org-element-contents table
))
4815 (when (eq (org-element-property :type row
) 'standard
)
4816 (let ((value (org-element-contents
4817 (car (org-element-contents row
)))))
4818 (cond ((member value
4819 '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4820 (setq special-column?
'special
))
4822 (t (throw 'exit nil
))))))
4823 (eq special-column?
'special
))))
4825 (defun org-export-table-has-header-p (table info
)
4826 "Non-nil when TABLE has a header.
4828 INFO is a plist used as a communication channel.
4830 A table has a header when it contains at least two row groups."
4831 (let* ((cache (or (plist-get info
:table-header-cache
)
4832 (let ((table (make-hash-table :test
#'eq
)))
4833 (plist-put info
:table-header-cache table
)
4835 (cached (gethash table cache
'no-cache
)))
4836 (if (not (eq cached
'no-cache
)) cached
4837 (let ((rowgroup 1) row-flag
)
4839 (org-element-map table
'table-row
4844 (eq (org-element-property :type row
) 'rule
)
4845 (not (eq (org-element-property :ruletype row
) 'csep
)))
4847 (setq row-flag nil
))
4848 ((and (not row-flag
)
4849 (eq (org-element-property :type row
) 'standard
))
4855 (defun org-export-table-row-is-special-p (table-row _
)
4856 "Non-nil if TABLE-ROW is considered special.
4857 All special rows will be ignored during export."
4858 (when (eq (org-element-property :type table-row
) 'standard
)
4859 (let ((first-cell (org-element-contents
4860 (car (org-element-contents table-row
)))))
4861 ;; A row is special either when...
4863 ;; ... it starts with a field only containing "/",
4864 (equal first-cell
'("/"))
4865 ;; ... the table contains a special column and the row start
4866 ;; with a marking character among, "^", "_", "$" or "!",
4867 (and (org-export-table-has-special-column-p
4868 (org-export-get-parent table-row
))
4869 (member first-cell
'(("^") ("_") ("$") ("!"))))
4870 ;; ... it contains only alignment cookies and empty cells.
4871 (let ((special-row-p 'empty
))
4873 (dolist (cell (org-element-contents table-row
))
4874 (let ((value (org-element-contents cell
)))
4875 ;; Since VALUE is a secondary string, the following
4876 ;; checks avoid expanding it with `org-export-data'.
4878 ((and (not (cdr value
))
4879 (stringp (car value
))
4880 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4882 (setq special-row-p
'cookie
))
4883 (t (throw 'exit nil
)))))
4884 (eq special-row-p
'cookie
)))))))
4886 (defun org-export-table-row-group (table-row info
)
4887 "Return TABLE-ROW's group number, as an integer.
4889 INFO is a plist used as the communication channel.
4891 Return value is the group number, as an integer, or nil for
4892 special rows and rows separators. First group is also table's
4894 (when (eq (org-element-property :type table-row
) 'standard
)
4895 (let* ((cache (or (plist-get info
:table-row-group-cache
)
4896 (let ((table (make-hash-table :test
#'eq
)))
4897 (plist-put info
:table-row-group-cache table
)
4899 (cached (gethash table-row cache
'no-cache
)))
4900 (if (not (eq cached
'no-cache
)) cached
4901 ;; First time a row is queried, populate cache with all the
4902 ;; rows from the table.
4903 (let ((group 0) row-flag
)
4904 (org-element-map (org-export-get-parent table-row
) 'table-row
4906 (if (and (eq (org-element-property :type row
) 'rule
)
4907 (not (eq (org-element-property :ruletype row
) 'csep
)))
4909 (unless row-flag
(cl-incf group
) (setq row-flag t
))
4910 (puthash row group cache
)))
4912 (gethash table-row cache
)))))
4914 (defun org-export-table-cell-width (table-cell info
)
4915 "Return TABLE-CELL contents width.
4917 INFO is a plist used as the communication channel.
4919 Return value is the width given by the last width cookie in the
4920 same column as TABLE-CELL, or nil."
4921 (let* ((row (org-export-get-parent table-cell
))
4922 (table (org-export-get-parent row
))
4923 (cells (org-element-contents row
))
4924 (columns (length cells
))
4925 (column (- columns
(length (memq table-cell cells
))))
4926 (cache (or (plist-get info
:table-cell-width-cache
)
4927 (let ((table (make-hash-table :test
#'eq
)))
4928 (plist-put info
:table-cell-width-cache table
)
4930 (width-vector (or (gethash table cache
)
4931 (puthash table
(make-vector columns
'empty
) cache
)))
4932 (value (aref width-vector column
)))
4933 (if (not (eq value
'empty
)) value
4935 (dolist (row (org-element-contents table
)
4936 (aset width-vector column cookie-width
))
4937 (when (org-export-table-row-is-special-p row info
)
4938 ;; In a special row, try to find a width cookie at COLUMN.
4939 (let* ((value (org-element-contents
4940 (elt (org-element-contents row
) column
)))
4941 (cookie (car value
)))
4942 ;; The following checks avoid expanding unnecessarily
4943 ;; the cell with `org-export-data'.
4947 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4948 (match-string 1 cookie
))
4950 (string-to-number (match-string 1 cookie
)))))))))))
4952 (defun org-export-table-cell-alignment (table-cell info
)
4953 "Return TABLE-CELL contents alignment.
4955 INFO is a plist used as the communication channel.
4957 Return alignment as specified by the last alignment cookie in the
4958 same column as TABLE-CELL. If no such cookie is found, a default
4959 alignment value will be deduced from fraction of numbers in the
4960 column (see `org-table-number-fraction' for more information).
4961 Possible values are `left', `right' and `center'."
4962 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4963 (require 'org-table
)
4964 (let* ((row (org-export-get-parent table-cell
))
4965 (table (org-export-get-parent row
))
4966 (cells (org-element-contents row
))
4967 (columns (length cells
))
4968 (column (- columns
(length (memq table-cell cells
))))
4969 (cache (or (plist-get info
:table-cell-alignment-cache
)
4970 (let ((table (make-hash-table :test
#'eq
)))
4971 (plist-put info
:table-cell-alignment-cache table
)
4973 (align-vector (or (gethash table cache
)
4974 (puthash table
(make-vector columns nil
) cache
))))
4975 (or (aref align-vector column
)
4976 (let ((number-cells 0)
4979 previous-cell-number-p
)
4980 (dolist (row (org-element-contents (org-export-get-parent row
)))
4982 ;; In a special row, try to find an alignment cookie at
4984 ((org-export-table-row-is-special-p row info
)
4985 (let ((value (org-element-contents
4986 (elt (org-element-contents row
) column
))))
4987 ;; Since VALUE is a secondary string, the following
4988 ;; checks avoid useless expansion through
4989 ;; `org-export-data'.
4992 (stringp (car value
))
4993 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4995 (match-string 1 (car value
)))
4996 (setq cookie-align
(match-string 1 (car value
))))))
4997 ;; Ignore table rules.
4998 ((eq (org-element-property :type row
) 'rule
))
4999 ;; In a standard row, check if cell's contents are
5000 ;; expressing some kind of number. Increase NUMBER-CELLS
5001 ;; accordingly. Though, don't bother if an alignment
5002 ;; cookie has already defined cell's alignment.
5004 (let ((value (org-export-data
5005 (org-element-contents
5006 (elt (org-element-contents row
) column
))
5008 (cl-incf total-cells
)
5009 ;; Treat an empty cell as a number if it follows
5011 (if (not (or (string-match org-table-number-regexp value
)
5012 (and (string= value
"") previous-cell-number-p
)))
5013 (setq previous-cell-number-p nil
)
5014 (setq previous-cell-number-p t
)
5015 (cl-incf number-cells
))))))
5016 ;; Return value. Alignment specified by cookies has
5017 ;; precedence over alignment deduced from cell's contents.
5020 (cond ((equal cookie-align
"l") 'left
)
5021 ((equal cookie-align
"r") 'right
)
5022 ((equal cookie-align
"c") 'center
)
5023 ((>= (/ (float number-cells
) total-cells
)
5024 org-table-number-fraction
)
5028 (defun org-export-table-cell-borders (table-cell info
)
5029 "Return TABLE-CELL borders.
5031 INFO is a plist used as a communication channel.
5033 Return value is a list of symbols, or nil. Possible values are:
5034 `top', `bottom', `above', `below', `left' and `right'. Note:
5035 `top' (resp. `bottom') only happen for a cell in the first
5036 row (resp. last row) of the table, ignoring table rules, if any.
5038 Returned borders ignore special rows."
5039 (let* ((row (org-export-get-parent table-cell
))
5040 (table (org-export-get-parent-table table-cell
))
5042 ;; Top/above border? TABLE-CELL has a border above when a rule
5043 ;; used to demarcate row groups can be found above. Hence,
5044 ;; finding a rule isn't sufficient to push `above' in BORDERS:
5045 ;; another regular row has to be found above that rule.
5048 ;; Look at every row before the current one.
5049 (dolist (row (cdr (memq row
(reverse (org-element-contents table
)))))
5050 (cond ((and (eq (org-element-property :type row
) 'rule
)
5051 (not (eq (org-element-property :ruletype row
) 'csep
)))
5053 ((not (org-export-table-row-is-special-p row info
))
5054 (if rule-flag
(throw 'exit
(push 'above borders
))
5055 (throw 'exit nil
)))))
5056 ;; No rule above, or rule found starts the table (ignoring any
5057 ;; special row): TABLE-CELL is at the top of the table.
5058 (when rule-flag
(push 'above borders
))
5059 (push 'top borders
)))
5060 ;; Bottom/below border? TABLE-CELL has a border below when next
5061 ;; non-regular row below is a rule.
5064 ;; Look at every row after the current one.
5065 (dolist (row (cdr (memq row
(org-element-contents table
))))
5066 (cond ((and (eq (org-element-property :type row
) 'rule
)
5067 (not (eq (org-element-property :ruletype row
) 'csep
)))
5069 ((not (org-export-table-row-is-special-p row info
))
5070 (if rule-flag
(throw 'exit
(push 'below borders
))
5071 (throw 'exit nil
)))))
5072 ;; No rule below, or rule found ends the table (modulo some
5073 ;; special row): TABLE-CELL is at the bottom of the table.
5074 (when rule-flag
(push 'below borders
))
5075 (push 'bottom borders
)))
5076 ;; Right/left borders? They can only be specified by column
5077 ;; groups. Column groups are defined in a row starting with "/".
5078 ;; Also a column groups row only contains "<", "<>", ">" or blank
5081 (let ((column (let ((cells (org-element-contents row
)))
5082 (- (length cells
) (length (memq table-cell cells
))))))
5083 ;; Table rows are read in reverse order so last column groups
5084 ;; row has precedence over any previous one.
5085 (dolist (row (reverse (org-element-contents table
)))
5086 (unless (eq (org-element-property :type row
) 'rule
)
5087 (when (equal (org-element-contents
5088 (car (org-element-contents row
)))
5090 (let ((column-groups
5093 (let ((value (org-element-contents cell
)))
5094 (when (member value
'(("<") ("<>") (">") nil
))
5096 (org-element-contents row
))))
5097 ;; There's a left border when previous cell, if
5098 ;; any, ends a group, or current one starts one.
5099 (when (or (and (not (zerop column
))
5100 (member (elt column-groups
(1- column
))
5102 (member (elt column-groups column
) '("<" "<>")))
5103 (push 'left borders
))
5104 ;; There's a right border when next cell, if any,
5105 ;; starts a group, or current one ends one.
5106 (when (or (and (/= (1+ column
) (length column-groups
))
5107 (member (elt column-groups
(1+ column
))
5109 (member (elt column-groups column
) '(">" "<>")))
5110 (push 'right borders
))
5111 (throw 'exit nil
)))))))
5115 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
5116 "Non-nil when TABLE-CELL is at the beginning of a column group.
5117 INFO is a plist used as a communication channel."
5118 ;; A cell starts a column group either when it is at the beginning
5119 ;; of a row (or after the special column, if any) or when it has
5121 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
5122 'identity info
'first-match
)
5124 (memq 'left
(org-export-table-cell-borders table-cell info
))))
5126 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
5127 "Non-nil when TABLE-CELL is at the end of a column group.
5128 INFO is a plist used as a communication channel."
5129 ;; A cell ends a column group either when it is at the end of a row
5130 ;; or when it has a right border.
5131 (or (eq (car (last (org-element-contents
5132 (org-export-get-parent table-cell
))))
5134 (memq 'right
(org-export-table-cell-borders table-cell info
))))
5136 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
5137 "Non-nil when TABLE-ROW is at the beginning of a row group.
5138 INFO is a plist used as a communication channel."
5139 (unless (or (and (eq (org-element-property :type table-row
) 'rule
)
5140 (not (eq (org-element-property :ruletype table-row
) 'csep
)))
5141 (org-export-table-row-is-special-p table-row info
))
5142 (let ((borders (org-export-table-cell-borders
5143 (car (org-element-contents table-row
)) info
)))
5144 (or (memq 'top borders
) (memq 'above borders
)))))
5146 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
5147 "Non-nil when TABLE-ROW is at the end of a row group.
5148 INFO is a plist used as a communication channel."
5149 (unless (or (and (eq (org-element-property :type table-row
) 'rule
)
5150 (not (eq (org-element-property :ruletype table-row
) 'csep
)))
5151 (org-export-table-row-is-special-p table-row info
))
5152 (let ((borders (org-export-table-cell-borders
5153 (car (org-element-contents table-row
)) info
)))
5154 (or (memq 'bottom borders
) (memq 'below borders
)))))
5156 (defun org-export-table-row-in-header-p (table-row info
)
5157 "Non-nil when TABLE-ROW is located within table's header.
5158 INFO is a plist used as a communication channel. Always return
5159 nil for special rows and rows separators."
5160 (and (org-export-table-has-header-p
5161 (org-export-get-parent-table table-row
) info
)
5162 (eql (org-export-table-row-group table-row info
) 1)))
5164 (defun org-export-table-row-starts-header-p (table-row info
)
5165 "Non-nil when TABLE-ROW is the first table header's row.
5166 INFO is a plist used as a communication channel."
5167 (and (org-export-table-row-in-header-p table-row info
)
5168 (org-export-table-row-starts-rowgroup-p table-row info
)))
5170 (defun org-export-table-row-ends-header-p (table-row info
)
5171 "Non-nil when TABLE-ROW is the last table header's row.
5172 INFO is a plist used as a communication channel."
5173 (and (org-export-table-row-in-header-p table-row info
)
5174 (org-export-table-row-ends-rowgroup-p table-row info
)))
5176 (defun org-export-table-row-number (table-row info
)
5177 "Return TABLE-ROW number.
5178 INFO is a plist used as a communication channel. Return value is
5179 zero-indexed and ignores separators. The function returns nil
5180 for special rows and separators."
5181 (when (eq (org-element-property :type table-row
) 'standard
)
5182 (let* ((cache (or (plist-get info
:table-row-number-cache
)
5183 (let ((table (make-hash-table :test
#'eq
)))
5184 (plist-put info
:table-row-number-cache table
)
5186 (cached (gethash table-row cache
'no-cache
)))
5187 (if (not (eq cached
'no-cache
)) cached
5188 ;; First time a row is queried, populate cache with all the
5189 ;; rows from the table.
5191 (org-element-map (org-export-get-parent-table table-row
) 'table-row
5193 (when (eq (org-element-property :type row
) 'standard
)
5194 (puthash row
(cl-incf number
) cache
)))
5196 (gethash table-row cache
)))))
5198 (defun org-export-table-dimensions (table info
)
5199 "Return TABLE dimensions.
5201 INFO is a plist used as a communication channel.
5203 Return value is a CONS like (ROWS . COLUMNS) where
5204 ROWS (resp. COLUMNS) is the number of exportable
5205 rows (resp. columns)."
5206 (let (first-row (columns 0) (rows 0))
5207 ;; Set number of rows, and extract first one.
5208 (org-element-map table
'table-row
5210 (when (eq (org-element-property :type row
) 'standard
)
5212 (unless first-row
(setq first-row row
)))) info
)
5213 ;; Set number of columns.
5214 (org-element-map first-row
'table-cell
(lambda (_) (cl-incf columns
)) info
)
5216 (cons rows columns
)))
5218 (defun org-export-table-cell-address (table-cell info
)
5219 "Return address of a regular TABLE-CELL object.
5221 TABLE-CELL is the cell considered. INFO is a plist used as
5222 a communication channel.
5224 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5225 zero-based index. Only exportable cells are considered. The
5226 function returns nil for other cells."
5227 (let* ((table-row (org-export-get-parent table-cell
))
5228 (row-number (org-export-table-row-number table-row info
)))
5231 (let ((col-count 0))
5232 (org-element-map table-row
'table-cell
5234 (if (eq cell table-cell
) col-count
(cl-incf col-count
) nil
))
5235 info
'first-match
))))))
5237 (defun org-export-get-table-cell-at (address table info
)
5238 "Return regular table-cell object at ADDRESS in TABLE.
5240 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5241 zero-based index. TABLE is a table type element. INFO is
5242 a plist used as a communication channel.
5244 If no table-cell, among exportable cells, is found at ADDRESS,
5246 (let ((column-pos (cdr address
)) (column-count 0))
5248 ;; Row at (car address) or nil.
5249 (let ((row-pos (car address
)) (row-count 0))
5250 (org-element-map table
'table-row
5252 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
5253 ((= row-count row-pos
) row
)
5254 (t (cl-incf row-count
) nil
)))
5258 (if (= column-count column-pos
) cell
5259 (cl-incf column-count
) nil
))
5260 info
'first-match
)))
5263 ;;;; For Tables of Contents
5265 ;; `org-export-collect-headlines' builds a list of all exportable
5266 ;; headline elements, maybe limited to a certain depth. One can then
5267 ;; easily parse it and transcode it.
5269 ;; Building lists of tables, figures or listings is quite similar.
5270 ;; Once the generic function `org-export-collect-elements' is defined,
5271 ;; `org-export-collect-tables', `org-export-collect-figures' and
5272 ;; `org-export-collect-listings' can be derived from it.
5274 ;; `org-export-toc-entry-backend' builds a special anonymous back-end
5275 ;; useful to export table of contents' entries.
5277 (defun org-export-collect-headlines (info &optional n scope
)
5278 "Collect headlines in order to build a table of contents.
5280 INFO is a plist used as a communication channel.
5282 When optional argument N is an integer, it specifies the depth of
5283 the table of contents. Otherwise, it is set to the value of the
5284 last headline level. See `org-export-headline-levels' for more
5287 Optional argument SCOPE, when non-nil, is an element. If it is
5288 a headline, only children of SCOPE are collected. Otherwise,
5289 collect children of the headline containing provided element. If
5290 there is no such headline, collect all headlines. In any case,
5291 argument N becomes relative to the level of that headline.
5293 Return a list of all exportable headlines as parsed elements.
5294 Footnote sections are ignored."
5295 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
5296 ((eq (org-element-type scope
) 'headline
) scope
)
5297 ((org-export-get-parent-headline scope
))
5298 (t (plist-get info
:parse-tree
))))
5299 (limit (plist-get info
:headline-levels
))
5300 (n (if (not (wholenump n
)) limit
5301 (min (if (eq (org-element-type scope
) 'org-data
) n
5302 (+ (org-export-get-relative-level scope info
) n
))
5304 (org-element-map (org-element-contents scope
) 'headline
5306 (and (not (org-element-property :footnote-section-p h
))
5308 (org-export-get-node-property :UNNUMBERED h t
)))
5309 (>= n
(org-export-get-relative-level h info
))
5313 (defun org-export-collect-elements (type info
&optional predicate
)
5314 "Collect referenceable elements of a determined type.
5316 TYPE can be a symbol or a list of symbols specifying element
5317 types to search. Only elements with a caption are collected.
5319 INFO is a plist used as a communication channel.
5321 When non-nil, optional argument PREDICATE is a function accepting
5322 one argument, an element of type TYPE. It returns a non-nil
5323 value when that element should be collected.
5325 Return a list of all elements found, in order of appearance."
5326 (org-element-map (plist-get info
:parse-tree
) type
5328 (and (org-element-property :caption element
)
5329 (or (not predicate
) (funcall predicate element
))
5333 (defun org-export-collect-tables (info)
5334 "Build a list of tables.
5335 INFO is a plist used as a communication channel.
5337 Return a list of table elements with a caption."
5338 (org-export-collect-elements 'table info
))
5340 (defun org-export-collect-figures (info predicate
)
5341 "Build a list of figures.
5343 INFO is a plist used as a communication channel. PREDICATE is
5344 a function which accepts one argument: a paragraph element and
5345 whose return value is non-nil when that element should be
5348 A figure is a paragraph type element, with a caption, verifying
5349 PREDICATE. The latter has to be provided since a \"figure\" is
5350 a vague concept that may depend on back-end.
5352 Return a list of elements recognized as figures."
5353 (org-export-collect-elements 'paragraph info predicate
))
5355 (defun org-export-collect-listings (info)
5356 "Build a list of source blocks.
5358 INFO is a plist used as a communication channel.
5360 Return a list of `src-block' elements with a caption."
5361 (org-export-collect-elements 'src-block info
))
5363 (defun org-export-excluded-from-toc-p (headline info
)
5364 "Non-nil if HEADLINE should be excluded from tables of contents.
5366 INFO is a plist used as a communication channel.
5368 Note that such headlines are already excluded from
5369 `org-export-collect-headlines'. Therefore, this function is not
5370 necessary if you only need to list headlines in the table of
5371 contents. However, it is useful if some additional processing is
5372 required on headlines excluded from table of contents."
5373 (or (org-element-property :footnote-section-p headline
)
5374 (org-export-low-level-p headline info
)
5375 (equal "notoc" (org-export-get-node-property :UNNUMBERED headline t
))))
5377 (defun org-export-toc-entry-backend (parent &rest transcoders
)
5378 "Return an export back-end appropriate for table of contents entries.
5380 PARENT is an export back-end the returned back-end should inherit
5383 By default, the back-end removes footnote references and targets.
5384 It also changes links and radio targets into regular text.
5385 TRANSCODERS optional argument, when non-nil, specifies additional
5386 transcoders. A transcoder follows the pattern (TYPE . FUNCTION)
5387 where type is an element or object type and FUNCTION the function
5389 (declare (indent 1))
5390 (org-export-create-backend
5394 `((footnote-reference .
,#'ignore
)
5395 (link .
,(lambda (l c i
)
5398 (org-element-property :raw-link l
)
5400 (radio-target .
,(lambda (_r c _
) c
))
5401 (target .
,#'ignore
)))))
5406 ;; The main function for the smart quotes sub-system is
5407 ;; `org-export-activate-smart-quotes', which replaces every quote in
5408 ;; a given string from the parse tree with its "smart" counterpart.
5410 ;; Dictionary for smart quotes is stored in
5411 ;; `org-export-smart-quotes-alist'.
5413 (defconst org-export-smart-quotes-alist
5416 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5417 :texinfo
"@guillemetleft{}")
5419 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5420 :texinfo
"@guillemetright{}")
5421 (secondary-opening :utf-8
"‹" :html
"‹" :latex
"\\guilsinglleft{}"
5422 :texinfo
"@guilsinglleft{}")
5423 (secondary-closing :utf-8
"›" :html
"›" :latex
"\\guilsinglright{}"
5424 :texinfo
"@guilsinglright{}")
5425 (apostrophe :utf-8
"’" :html
"’"))
5427 ;; one may use: »...«, "...", ›...‹, or '...'.
5428 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5429 ;; LaTeX quotes require Babel!
5431 :utf-8
"»" :html
"»" :latex
">>" :texinfo
"@guillemetright{}")
5433 :utf-8
"«" :html
"«" :latex
"<<" :texinfo
"@guillemetleft{}")
5435 :utf-8
"›" :html
"›" :latex
"\\frq{}" :texinfo
"@guilsinglright{}")
5437 :utf-8
"‹" :html
"‹" :latex
"\\flq{}" :texinfo
"@guilsingleft{}")
5438 (apostrophe :utf-8
"’" :html
"’"))
5441 :utf-8
"„" :html
"„" :latex
"\"`" :texinfo
"@quotedblbase{}")
5443 :utf-8
"“" :html
"“" :latex
"\"'" :texinfo
"@quotedblleft{}")
5445 :utf-8
"‚" :html
"‚" :latex
"\\glq{}" :texinfo
"@quotesinglbase{}")
5447 :utf-8
"‘" :html
"‘" :latex
"\\grq{}" :texinfo
"@quoteleft{}")
5448 (apostrophe :utf-8
"’" :html
"’"))
5450 (primary-opening :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5451 (primary-closing :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5452 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5453 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5454 (apostrophe :utf-8
"’" :html
"’"))
5457 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5458 :texinfo
"@guillemetleft{}")
5460 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5461 :texinfo
"@guillemetright{}")
5462 (secondary-opening :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5463 (secondary-closing :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5464 (apostrophe :utf-8
"’" :html
"’"))
5467 :utf-8
"« " :html
"« " :latex
"\\og "
5468 :texinfo
"@guillemetleft{}@tie{}")
5470 :utf-8
" »" :html
" »" :latex
"\\fg{}"
5471 :texinfo
"@tie{}@guillemetright{}")
5473 :utf-8
"« " :html
"« " :latex
"\\og "
5474 :texinfo
"@guillemetleft{}@tie{}")
5475 (secondary-closing :utf-8
" »" :html
" »" :latex
"\\fg{}"
5476 :texinfo
"@tie{}@guillemetright{}")
5477 (apostrophe :utf-8
"’" :html
"’"))
5480 :utf-8
"„" :html
"„" :latex
"\"`" :texinfo
"@quotedblbase{}")
5482 :utf-8
"“" :html
"“" :latex
"\"'" :texinfo
"@quotedblleft{}")
5484 :utf-8
"‚" :html
"‚" :latex
"\\glq{}" :texinfo
"@quotesinglbase{}")
5486 :utf-8
"‘" :html
"‘" :latex
"\\grq{}" :texinfo
"@quoteleft{}")
5487 (apostrophe :utf-8
"’" :html
"’"))
5489 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5491 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5492 :texinfo
"@guillemetleft{}")
5494 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5495 :texinfo
"@guillemetright{}")
5496 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5497 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5498 (apostrophe :utf-8
"’" :html
"’"))
5500 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5502 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5503 :texinfo
"@guillemetleft{}")
5505 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5506 :texinfo
"@guillemetright{}")
5507 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5508 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5509 (apostrophe :utf-8
"’" :html
"’"))
5511 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5513 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5514 :texinfo
"@guillemetleft{}")
5516 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5517 :texinfo
"@guillemetright{}")
5518 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5519 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5520 (apostrophe :utf-8
"’" :html
"’"))
5522 ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
5523 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5524 (primary-opening :utf-8
"«" :html
"«" :latex
"{}<<"
5525 :texinfo
"@guillemetleft{}")
5526 (primary-closing :utf-8
"»" :html
"»" :latex
">>{}"
5527 :texinfo
"@guillemetright{}")
5529 :utf-8
"„" :html
"„" :latex
"\\glqq{}" :texinfo
"@quotedblbase{}")
5531 :utf-8
"“" :html
"“" :latex
"\\grqq{}" :texinfo
"@quotedblleft{}")
5532 (apostrophe :utf-8
"’" :html
: "'"))
5534 ;; Based on https://sl.wikipedia.org/wiki/Narekovaj
5535 (primary-opening :utf-8
"«" :html
"«" :latex
"{}<<"
5536 :texinfo
"@guillemetleft{}")
5537 (primary-closing :utf-8
"»" :html
"»" :latex
">>{}"
5538 :texinfo
"@guillemetright{}")
5540 :utf-8
"„" :html
"„" :latex
"\\glqq{}" :texinfo
"@quotedblbase{}")
5542 :utf-8
"“" :html
"“" :latex
"\\grqq{}" :texinfo
"@quotedblleft{}")
5543 (apostrophe :utf-8
"’" :html
"’"))
5545 ;; Based on https://sv.wikipedia.org/wiki/Citattecken
5546 (primary-opening :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5547 (primary-closing :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5548 (secondary-opening :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5549 (secondary-closing :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5550 (apostrophe :utf-8
"’" :html
"’")))
5551 "Smart quotes translations.
5553 Alist whose CAR is a language string and CDR is an alist with
5554 quote type as key and a plist associating various encodings to
5555 their translation as value.
5557 A quote type can be any symbol among `primary-opening',
5558 `primary-closing', `secondary-opening', `secondary-closing' and
5561 Valid encodings include `:utf-8', `:html', `:latex' and
5564 If no translation is found, the quote character is left as-is.")
5566 (defun org-export--smart-quote-status (s info
)
5567 "Return smart quote status at the beginning of string S.
5568 INFO is the current export state, as a plist."
5569 (let* ((parent (org-element-property :parent s
))
5570 (cache (or (plist-get info
:smart-quote-cache
)
5571 (let ((table (make-hash-table :test
#'eq
)))
5572 (plist-put info
:smart-quote-cache table
)
5574 (value (gethash parent cache
'missing-data
)))
5575 (if (not (eq value
'missing-data
)) (cdr (assq s value
))
5576 (let (level1-open full-status
)
5578 (let ((secondary (org-element-secondary-p s
)))
5579 (if secondary
(org-element-property secondary parent
)
5580 (org-element-contents parent
)))
5583 (let ((start 0) current-status
)
5584 (while (setq start
(string-match "['\"]" text start
))
5587 ((equal (match-string 0 text
) "\"")
5588 (setf level1-open
(not level1-open
))
5589 (if level1-open
'primary-opening
'primary-closing
))
5590 ;; Not already in a level 1 quote: this is an
5592 ((not level1-open
) 'apostrophe
)
5593 ;; Extract previous char and next char. As
5594 ;; a special case, they can also be set to `blank',
5595 ;; `no-blank' or nil. Then determine if current
5596 ;; match is allowed as an opening quote or a closing
5600 (if (> start
0) (substring text
(1- start
) start
)
5601 (let ((p (org-export-get-previous-element
5604 ((stringp p
) (substring p -
1))
5605 ((memq (org-element-property :post-blank p
)
5610 (if (< (1+ start
) (length text
))
5611 (substring text
(1+ start
) (+ start
2))
5612 (let ((n (org-export-get-next-element text info
)))
5614 ((stringp n
) (substring n
0 1))
5617 (and (if (stringp previous
)
5618 (string-match "\\s\"\\|\\s-\\|\\s("
5620 (memq previous
'(blank nil
)))
5622 (string-match "\\w\\|\\s.\\|\\s_" next
)
5623 (eq next
'no-blank
))))
5625 (and (if (stringp previous
)
5626 (string-match "\\w\\|\\s.\\|\\s_" previous
)
5627 (eq previous
'no-blank
))
5629 (string-match "\\s-\\|\\s)\\|\\s.\\|\\s\""
5631 (memq next
'(blank nil
))))))
5633 ((and allow-open allow-close
) (error "Should not happen"))
5634 (allow-open 'secondary-opening
)
5635 (allow-close 'secondary-closing
)
5639 (when current-status
5640 (push (cons text
(nreverse current-status
)) full-status
))))
5641 info nil org-element-recursive-objects
)
5642 (puthash parent full-status cache
)
5643 (cdr (assq s full-status
))))))
5645 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5646 "Replace regular quotes with \"smart\" quotes in string S.
5648 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5649 `:utf-8'. INFO is a plist used as a communication channel.
5651 The function has to retrieve information about string
5652 surroundings in parse tree. It can only happen with an
5653 unmodified string. Thus, if S has already been through another
5654 process, a non-nil ORIGINAL optional argument will provide that
5657 Return the new string."
5659 (copy-sequence (org-export--smart-quote-status (or original s
) info
))))
5660 (replace-regexp-in-string
5664 (cdr (assq (pop quote-status
)
5665 (cdr (assoc (plist-get info
:language
)
5666 org-export-smart-quotes-alist
))))
5673 ;; Here are various functions to retrieve information about the
5674 ;; neighborhood of a given element or object. Neighbors of interest
5675 ;; are direct parent (`org-export-get-parent'), parent headline
5676 ;; (`org-export-get-parent-headline'), first element containing an
5677 ;; object, (`org-export-get-parent-element'), parent table
5678 ;; (`org-export-get-parent-table'), previous element or object
5679 ;; (`org-export-get-previous-element') and next element or object
5680 ;; (`org-export-get-next-element').
5682 ;; defsubst org-export-get-parent must be defined before first use
5684 (defun org-export-get-parent-headline (blob)
5685 "Return BLOB parent headline or nil.
5686 BLOB is the element or object being considered."
5687 (org-element-lineage blob
'(headline)))
5689 (defun org-export-get-parent-element (object)
5690 "Return first element containing OBJECT or nil.
5691 OBJECT is the object to consider."
5692 (org-element-lineage object org-element-all-elements
))
5694 (defun org-export-get-parent-table (object)
5695 "Return OBJECT parent table or nil.
5696 OBJECT is either a `table-cell' or `table-element' type object."
5697 (org-element-lineage object
'(table)))
5699 (defun org-export-get-previous-element (blob info
&optional n
)
5700 "Return previous element or object.
5702 BLOB is an element or object. INFO is a plist used as
5703 a communication channel. Return previous exportable element or
5704 object, a string, or nil.
5706 When optional argument N is a positive integer, return a list
5707 containing up to N siblings before BLOB, from farthest to
5708 closest. With any other non-nil value, return a list containing
5710 (let* ((secondary (org-element-secondary-p blob
))
5711 (parent (org-export-get-parent blob
))
5713 (if secondary
(org-element-property secondary parent
)
5714 (org-element-contents parent
)))
5717 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5718 (cond ((memq obj
(plist-get info
:ignore-list
)))
5719 ((null n
) (throw 'exit obj
))
5720 ((not (wholenump n
)) (push obj prev
))
5721 ((zerop n
) (throw 'exit prev
))
5722 (t (cl-decf n
) (push obj prev
)))))))
5724 (defun org-export-get-next-element (blob info
&optional n
)
5725 "Return next element or object.
5727 BLOB is an element or object. INFO is a plist used as
5728 a communication channel. Return next exportable element or
5729 object, a string, or nil.
5731 When optional argument N is a positive integer, return a list
5732 containing up to N siblings after BLOB, from closest to farthest.
5733 With any other non-nil value, return a list containing all of
5735 (let* ((secondary (org-element-secondary-p blob
))
5736 (parent (org-export-get-parent blob
))
5739 (if secondary
(org-element-property secondary parent
)
5740 (org-element-contents parent
)))))
5743 (dolist (obj siblings
(nreverse next
))
5744 (cond ((memq obj
(plist-get info
:ignore-list
)))
5745 ((null n
) (throw 'exit obj
))
5746 ((not (wholenump n
)) (push obj next
))
5747 ((zerop n
) (throw 'exit
(nreverse next
)))
5748 (t (cl-decf n
) (push obj next
)))))))
5753 ;; `org-export-translate' translates a string according to the language
5754 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5755 ;; the dictionary used for the translation.
5757 (defconst org-export-dictionary
5759 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5761 ("ar" :default
"تأليف")
5762 ("ca" :default
"Autor")
5763 ("cs" :default
"Autor")
5764 ("da" :default
"Forfatter")
5765 ("de" :default
"Autor")
5766 ("eo" :html
"Aŭtoro")
5767 ("es" :default
"Autor")
5768 ("et" :default
"Autor")
5769 ("fi" :html
"Tekijä")
5770 ("fr" :default
"Auteur")
5771 ("hu" :default
"Szerzõ")
5772 ("is" :html
"Höfundur")
5773 ("it" :default
"Autore")
5774 ("ja" :default
"著者" :html
"著者")
5775 ("nl" :default
"Auteur")
5776 ("no" :default
"Forfatter")
5777 ("nb" :default
"Forfatter")
5778 ("nn" :default
"Forfattar")
5779 ("pl" :default
"Autor")
5780 ("pt_BR" :default
"Autor")
5781 ("ru" :html
"Автор" :utf-8
"Автор")
5782 ("sl" :default
"Avtor")
5783 ("sv" :html
"Författare")
5784 ("uk" :html
"Автор" :utf-8
"Автор")
5785 ("zh-CN" :html
"作者" :utf-8
"作者")
5786 ("zh-TW" :html
"作者" :utf-8
"作者"))
5787 ("Continued from previous page"
5788 ("ar" :default
"تتمة الصفحة السابقة")
5789 ("cs" :default
"Pokračování z předchozí strany")
5790 ("de" :default
"Fortsetzung von vorheriger Seite")
5791 ("es" :html
"Continúa de la página anterior" :ascii
"Continua de la pagina anterior" :default
"Continúa de la página anterior")
5792 ("fr" :default
"Suite de la page précédente")
5793 ("it" :default
"Continua da pagina precedente")
5794 ("ja" :default
"前ページからの続き")
5795 ("nl" :default
"Vervolg van vorige pagina")
5796 ("pt" :default
"Continuação da página anterior")
5797 ("pt_BR" :html
"Continuação da página anterior" :ascii
"Continuacao da pagina anterior" :default
"Continuação da página anterior")
5798 ("ru" :html
"(Продолжение)"
5799 :utf-8
"(Продолжение)")
5800 ("sl" :default
"Nadaljevanje s prejšnje strani"))
5801 ("Continued on next page"
5802 ("ar" :default
"التتمة في الصفحة التالية")
5803 ("cs" :default
"Pokračuje na další stránce")
5804 ("de" :default
"Fortsetzung nächste Seite")
5805 ("es" :html
"Continúa en la siguiente página" :ascii
"Continua en la siguiente pagina" :default
"Continúa en la siguiente página")
5806 ("fr" :default
"Suite page suivante")
5807 ("it" :default
"Continua alla pagina successiva")
5808 ("ja" :default
"次ページに続く")
5809 ("nl" :default
"Vervolg op volgende pagina")
5810 ("pt" :default
"Continua na página seguinte")
5811 ("pt_BR" :html
"Continua na próxima página" :ascii
"Continua na proxima pagina" :default
"Continua na próxima página")
5812 ("ru" :html
"(Продолжение следует)"
5813 :utf-8
"(Продолжение следует)")
5814 ("sl" :default
"Nadaljevanje na naslednji strani"))
5816 ("cs" :default
"Vytvořeno")
5817 ("pt_BR" :default
"Criado em")
5818 ("sl" :default
"Ustvarjeno"))
5820 ("ar" :default
"بتاريخ")
5821 ("ca" :default
"Data")
5822 ("cs" :default
"Datum")
5823 ("da" :default
"Dato")
5824 ("de" :default
"Datum")
5825 ("eo" :default
"Dato")
5826 ("es" :default
"Fecha")
5827 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5828 ("fi" :html
"Päivämäärä")
5829 ("hu" :html
"Dátum")
5830 ("is" :default
"Dagsetning")
5831 ("it" :default
"Data")
5832 ("ja" :default
"日付" :html
"日付")
5833 ("nl" :default
"Datum")
5834 ("no" :default
"Dato")
5835 ("nb" :default
"Dato")
5836 ("nn" :default
"Dato")
5837 ("pl" :default
"Data")
5838 ("pt_BR" :default
"Data")
5839 ("ru" :html
"Дата" :utf-8
"Дата")
5840 ("sl" :default
"Datum")
5841 ("sv" :default
"Datum")
5842 ("uk" :html
"Дата" :utf-8
"Дата")
5843 ("zh-CN" :html
"日期" :utf-8
"日期")
5844 ("zh-TW" :html
"日期" :utf-8
"日期"))
5846 ("ar" :default
"معادلة")
5847 ("cs" :default
"Rovnice")
5848 ("da" :default
"Ligning")
5849 ("de" :default
"Gleichung")
5850 ("es" :ascii
"Ecuacion" :html
"Ecuación" :default
"Ecuación")
5851 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5852 ("fr" :ascii
"Equation" :default
"Équation")
5853 ("is" :default
"Jafna")
5854 ("ja" :default
"方程式")
5855 ("no" :default
"Ligning")
5856 ("nb" :default
"Ligning")
5857 ("nn" :default
"Likning")
5858 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5859 ("ru" :html
"Уравнение"
5861 ("sl" :default
"Enačba")
5862 ("sv" :default
"Ekvation")
5863 ("zh-CN" :html
"方程" :utf-8
"方程"))
5865 ("ar" :default
"شكل")
5866 ("cs" :default
"Obrázek")
5867 ("da" :default
"Figur")
5868 ("de" :default
"Abbildung")
5869 ("es" :default
"Figura")
5870 ("et" :default
"Joonis")
5871 ("is" :default
"Mynd")
5872 ("it" :default
"Figura")
5873 ("ja" :default
"図" :html
"図")
5874 ("no" :default
"Illustrasjon")
5875 ("nb" :default
"Illustrasjon")
5876 ("nn" :default
"Illustrasjon")
5877 ("pt_BR" :default
"Figura")
5878 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5879 ("sv" :default
"Illustration")
5880 ("zh-CN" :html
"图" :utf-8
"图"))
5882 ("ar" :default
"شكل %d:")
5883 ("cs" :default
"Obrázek %d:")
5884 ("da" :default
"Figur %d")
5885 ("de" :default
"Abbildung %d:")
5886 ("es" :default
"Figura %d:")
5887 ("et" :default
"Joonis %d:")
5888 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5889 ("is" :default
"Mynd %d")
5890 ("it" :default
"Figura %d:")
5891 ("ja" :default
"図%d: " :html
"図%d: ")
5892 ("no" :default
"Illustrasjon %d")
5893 ("nb" :default
"Illustrasjon %d")
5894 ("nn" :default
"Illustrasjon %d")
5895 ("pt_BR" :default
"Figura %d:")
5896 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5897 ("sl" :default
"Slika %d")
5898 ("sv" :default
"Illustration %d")
5899 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5901 ("ar" :default
"الهوامش")
5902 ("ca" :html
"Peus de pàgina")
5903 ("cs" :default
"Poznámky pod čarou")
5904 ("da" :default
"Fodnoter")
5905 ("de" :html
"Fußnoten" :default
"Fußnoten")
5906 ("eo" :default
"Piednotoj")
5907 ("es" :ascii
"Nota al pie de pagina" :html
"Nota al pie de página" :default
"Nota al pie de página")
5908 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5909 ("fi" :default
"Alaviitteet")
5910 ("fr" :default
"Notes de bas de page")
5911 ("hu" :html
"Lábjegyzet")
5912 ("is" :html
"Aftanmálsgreinar")
5913 ("it" :html
"Note a piè di pagina")
5914 ("ja" :default
"脚注" :html
"脚注")
5915 ("nl" :default
"Voetnoten")
5916 ("no" :default
"Fotnoter")
5917 ("nb" :default
"Fotnoter")
5918 ("nn" :default
"Fotnotar")
5919 ("pl" :default
"Przypis")
5920 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5921 ("ru" :html
"Сноски" :utf-8
"Сноски")
5922 ("sl" :default
"Opombe")
5923 ("sv" :default
"Fotnoter")
5924 ("uk" :html
"Примітки"
5926 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5927 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5929 ("ar" :default
"قائمة بالبرامج")
5930 ("cs" :default
"Seznam programů")
5931 ("da" :default
"Programmer")
5932 ("de" :default
"Programmauflistungsverzeichnis")
5933 ("es" :ascii
"Indice de Listados de programas" :html
"Índice de Listados de programas" :default
"Índice de Listados de programas")
5934 ("et" :default
"Loendite nimekiri")
5935 ("fr" :default
"Liste des programmes")
5936 ("ja" :default
"ソースコード目次")
5937 ("no" :default
"Dataprogrammer")
5938 ("nb" :default
"Dataprogrammer")
5939 ("pt_BR" :html
"Índice de Listagens" :default
"Índice de Listagens" :ascii
"Indice de Listagens")
5940 ("ru" :html
"Список распечаток"
5941 :utf-8
"Список распечаток")
5942 ("sl" :default
"Seznam programskih izpisov")
5943 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5945 ("ar" :default
"قائمة بالجداول")
5946 ("cs" :default
"Seznam tabulek")
5947 ("da" :default
"Tabeller")
5948 ("de" :default
"Tabellenverzeichnis")
5949 ("es" :ascii
"Indice de tablas" :html
"Índice de tablas" :default
"Índice de tablas")
5950 ("et" :default
"Tabelite nimekiri")
5951 ("fr" :default
"Liste des tableaux")
5952 ("is" :default
"Töfluskrá" :html
"Töfluskrá")
5953 ("it" :default
"Indice delle tabelle")
5954 ("ja" :default
"表目次")
5955 ("no" :default
"Tabeller")
5956 ("nb" :default
"Tabeller")
5957 ("nn" :default
"Tabeller")
5958 ("pt_BR" :html
"Índice de Tabelas" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5959 ("ru" :html
"Список таблиц"
5960 :utf-8
"Список таблиц")
5961 ("sl" :default
"Seznam tabel")
5962 ("sv" :default
"Tabeller")
5963 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5965 ("ar" :default
"برنامج")
5966 ("cs" :default
"Program")
5967 ("da" :default
"Program")
5968 ("de" :default
"Programmlisting")
5969 ("es" :default
"Listado de programa")
5970 ("et" :default
"Loend")
5971 ("fr" :default
"Programme" :html
"Programme")
5972 ("it" :default
"Listato")
5973 ("ja" :default
"ソースコード")
5974 ("no" :default
"Dataprogram")
5975 ("nb" :default
"Dataprogram")
5976 ("pt_BR" :default
"Listagem")
5977 ("ru" :html
"Распечатка"
5978 :utf-8
"Распечатка")
5979 ("sl" :default
"Izpis programa")
5980 ("zh-CN" :html
"代码" :utf-8
"代码"))
5982 ("ar" :default
"برنامج %d:")
5983 ("cs" :default
"Program %d:")
5984 ("da" :default
"Program %d")
5985 ("de" :default
"Programmlisting %d")
5986 ("es" :default
"Listado de programa %d")
5987 ("et" :default
"Loend %d")
5988 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5989 ("it" :default
"Listato %d :")
5990 ("ja" :default
"ソースコード%d:")
5991 ("no" :default
"Dataprogram %d")
5992 ("nb" :default
"Dataprogram %d")
5993 ("pt_BR" :default
"Listagem %d:")
5994 ("ru" :html
"Распечатка %d.:"
5995 :utf-8
"Распечатка %d.:")
5996 ("sl" :default
"Izpis programa %d")
5997 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5999 ("ar" :default
"المراجع")
6000 ("cs" :default
"Reference")
6001 ("de" :default
"Quellen")
6002 ("es" :default
"Referencias")
6003 ("fr" :ascii
"References" :default
"Références")
6004 ("it" :default
"Riferimenti")
6005 ("pt_BR" :html
"Referências" :default
"Referências" :ascii
"Referencias")
6006 ("sl" :default
"Reference"))
6008 ("cs" :default
"Viz obrázek %s")
6009 ("fr" :default
"cf. figure %s"
6010 :html
"cf. figure %s" :latex
"cf.~figure~%s")
6011 ("it" :default
"Vedi figura %s")
6012 ("pt_BR" :default
"Veja a figura %s")
6013 ("sl" :default
"Glej sliko %s"))
6015 ("cs" :default
"Viz program %s")
6016 ("fr" :default
"cf. programme %s"
6017 :html
"cf. programme %s" :latex
"cf.~programme~%s")
6018 ("pt_BR" :default
"Veja a listagem %s")
6019 ("sl" :default
"Glej izpis programa %s"))
6021 ("ar" :default
"انظر قسم %s")
6022 ("cs" :default
"Viz sekce %s")
6023 ("da" :default
"jævnfør afsnit %s")
6024 ("de" :default
"siehe Abschnitt %s")
6025 ("es" :ascii
"Vea seccion %s" :html
"Vea sección %s" :default
"Vea sección %s")
6026 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
6027 ("fr" :default
"cf. section %s")
6028 ("it" :default
"Vedi sezione %s")
6029 ("ja" :default
"セクション %s を参照")
6030 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
6031 :ascii
"Veja a secao %s")
6032 ("ru" :html
"См. раздел %s"
6033 :utf-8
"См. раздел %s")
6034 ("sl" :default
"Glej poglavje %d")
6035 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
6037 ("cs" :default
"Viz tabulka %s")
6038 ("fr" :default
"cf. tableau %s"
6039 :html
"cf. tableau %s" :latex
"cf.~tableau~%s")
6040 ("it" :default
"Vedi tabella %s")
6041 ("pt_BR" :default
"Veja a tabela %s")
6042 ("sl" :default
"Glej tabelo %s"))
6044 ("ar" :default
"جدول")
6045 ("cs" :default
"Tabulka")
6046 ("de" :default
"Tabelle")
6047 ("es" :default
"Tabla")
6048 ("et" :default
"Tabel")
6049 ("fr" :default
"Tableau")
6050 ("is" :default
"Tafla")
6051 ("it" :default
"Tabella")
6052 ("ja" :default
"表" :html
"表")
6053 ("pt_BR" :default
"Tabela")
6054 ("ru" :html
"Таблица"
6056 ("zh-CN" :html
"表" :utf-8
"表"))
6058 ("ar" :default
"جدول %d:")
6059 ("cs" :default
"Tabulka %d:")
6060 ("da" :default
"Tabel %d")
6061 ("de" :default
"Tabelle %d")
6062 ("es" :default
"Tabla %d")
6063 ("et" :default
"Tabel %d")
6064 ("fr" :default
"Tableau %d :")
6065 ("is" :default
"Tafla %d")
6066 ("it" :default
"Tabella %d:")
6067 ("ja" :default
"表%d:" :html
"表%d:")
6068 ("no" :default
"Tabell %d")
6069 ("nb" :default
"Tabell %d")
6070 ("nn" :default
"Tabell %d")
6071 ("pt_BR" :default
"Tabela %d:")
6072 ("ru" :html
"Таблица %d.:"
6073 :utf-8
"Таблица %d.:")
6074 ("sl" :default
"Tabela %d")
6075 ("sv" :default
"Tabell %d")
6076 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
6077 ("Table of Contents"
6078 ("ar" :default
"قائمة المحتويات")
6079 ("ca" :html
"Índex")
6080 ("cs" :default
"Obsah")
6081 ("da" :default
"Indhold")
6082 ("de" :default
"Inhaltsverzeichnis")
6083 ("eo" :default
"Enhavo")
6084 ("es" :ascii
"Indice" :html
"Índice" :default
"Índice")
6085 ("et" :default
"Sisukord")
6086 ("fi" :html
"Sisällysluettelo")
6087 ("fr" :ascii
"Sommaire" :default
"Table des matières")
6088 ("hu" :html
"Tartalomjegyzék")
6089 ("is" :default
"Efnisyfirlit")
6090 ("it" :default
"Indice")
6091 ("ja" :default
"目次" :html
"目次")
6092 ("nl" :default
"Inhoudsopgave")
6093 ("no" :default
"Innhold")
6094 ("nb" :default
"Innhold")
6095 ("nn" :default
"Innhald")
6096 ("pl" :html
"Spis treści")
6097 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
6098 ("ru" :html
"Содержание"
6099 :utf-8
"Содержание")
6100 ("sl" :default
"Kazalo")
6101 ("sv" :html
"Innehåll")
6102 ("uk" :html
"Зміст" :utf-8
"Зміст")
6103 ("zh-CN" :html
"目录" :utf-8
"目录")
6104 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
6105 ("Unknown reference"
6106 ("ar" :default
"مرجع غير معرّف")
6107 ("da" :default
"ukendt reference")
6108 ("de" :default
"Unbekannter Verweis")
6109 ("es" :default
"Referencia desconocida")
6110 ("et" :default
"Tundmatu viide")
6111 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
6112 ("it" :default
"Riferimento sconosciuto")
6113 ("ja" :default
"不明な参照先")
6114 ("pt_BR" :html
"Referência desconhecida" :default
"Referência desconhecida" :ascii
"Referencia desconhecida")
6115 ("ru" :html
"Неизвестная ссылка"
6116 :utf-8
"Неизвестная ссылка")
6117 ("sl" :default
"Neznana referenca")
6118 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
6119 "Dictionary for export engine.
6121 Alist whose car is the string to translate and cdr is an alist
6122 whose car is the language string and cdr is a plist whose
6123 properties are possible charsets and values translated terms.
6125 It is used as a database for `org-export-translate'. Since this
6126 function returns the string as-is if no translation was found,
6127 the variable only needs to record values different from the
6130 (defun org-export-translate (s encoding info
)
6131 "Translate string S according to language specification.
6133 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
6134 and `:utf-8'. INFO is a plist used as a communication channel.
6136 Translation depends on `:language' property. Return the
6137 translated string. If no translation is found, try to fall back
6138 to `:default' encoding. If it fails, return S."
6139 (let* ((lang (plist-get info
:language
))
6140 (translations (cdr (assoc lang
6141 (cdr (assoc s org-export-dictionary
))))))
6142 (or (plist-get translations encoding
)
6143 (plist-get translations
:default
)
6148 ;;; Asynchronous Export
6150 ;; `org-export-async-start' is the entry point for asynchronous
6151 ;; export. It recreates current buffer (including visibility,
6152 ;; narrowing and visited file) in an external Emacs process, and
6153 ;; evaluates a command there. It then applies a function on the
6154 ;; returned results in the current process.
6156 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
6157 ;; allow exporting to a buffer or a file, asynchronously or not.
6159 ;; `org-export-output-file-name' is an auxiliary function meant to be
6160 ;; used with `org-export-to-file'. With a given extension, it tries
6161 ;; to provide a canonical file name to write export output to.
6163 ;; Asynchronously generated results are never displayed directly.
6164 ;; Instead, they are stored in `org-export-stack-contents'. They can
6165 ;; then be retrieved by calling `org-export-stack'.
6167 ;; Export Stack is viewed through a dedicated major mode
6168 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
6169 ;;`org-export-stack-delete', `org-export-stack-view' and
6170 ;;`org-export-stack-clear'.
6172 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
6173 ;; It should be used whenever `org-export-async-start' is called.
6175 (defmacro org-export-async-start
(fun &rest body
)
6176 "Call function FUN on the results returned by BODY evaluation.
6178 FUN is an anonymous function of one argument. BODY evaluation
6179 happens in an asynchronous process, from a buffer which is an
6180 exact copy of the current one.
6182 Use `org-export-add-to-stack' in FUN in order to register results
6185 This is a low level function. See also `org-export-to-buffer'
6186 and `org-export-to-file' for more specialized functions."
6187 (declare (indent 1) (debug t
))
6188 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
6189 ;; Write the full sexp evaluating BODY in a copy of the current
6190 ;; buffer to a temporary file, as it may be too long for program
6191 ;; args in `start-process'.
6192 `(with-temp-message "Initializing asynchronous export process"
6193 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
6194 (,temp-file
(make-temp-file "org-export-process"))
6195 (,coding buffer-file-coding-system
))
6196 (with-temp-file ,temp-file
6198 ;; Null characters (from variable values) are inserted
6199 ;; within the file. As a consequence, coding system for
6200 ;; buffer contents will not be recognized properly. So,
6201 ;; we make sure it is the same as the one used to display
6202 ;; the original buffer.
6203 (format ";; -*- coding: %s; -*-\n%S"
6206 (when org-export-async-debug
'(setq debug-on-error t
))
6207 ;; Ignore `kill-emacs-hook' and code evaluation
6208 ;; queries from Babel as we need a truly
6209 ;; non-interactive process.
6210 (setq kill-emacs-hook nil
6211 org-babel-confirm-evaluate-answer-no t
)
6212 ;; Initialize export framework.
6214 ;; Re-create current buffer there.
6215 (funcall ,,copy-fun
)
6216 (restore-buffer-modified-p nil
)
6217 ;; Sexp to evaluate in the buffer.
6218 (print (progn ,,@body
))))))
6219 ;; Start external process.
6220 (let* ((process-connection-type nil
)
6221 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
6226 (list "org-export-process"
6228 (expand-file-name invocation-name invocation-directory
)
6230 (if org-export-async-init-file
6231 (list "-Q" "-l" org-export-async-init-file
)
6232 (list "-l" user-init-file
))
6233 (list "-l" ,temp-file
)))))
6234 ;; Register running process in stack.
6235 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
6236 ;; Set-up sentinel in order to catch results.
6237 (let ((handler ,fun
))
6238 (set-process-sentinel
6241 (let ((proc-buffer (process-buffer p
)))
6242 (when (eq (process-status p
) 'exit
)
6244 (if (zerop (process-exit-status p
))
6247 (with-current-buffer proc-buffer
6248 (goto-char (point-max))
6250 (read (current-buffer)))))
6251 (funcall ,handler results
))
6252 (unless org-export-async-debug
6253 (and (get-buffer proc-buffer
)
6254 (kill-buffer proc-buffer
))))
6255 (org-export-add-to-stack proc-buffer nil p
)
6257 (message "Process `%s' exited abnormally" p
))
6258 (unless org-export-async-debug
6259 (delete-file ,,temp-file
)))))))))))))
6262 (defun org-export-to-buffer
6264 &optional async subtreep visible-only body-only ext-plist
6266 "Call `org-export-as' with output to a specified buffer.
6268 BACKEND is either an export back-end, as returned by, e.g.,
6269 `org-export-create-backend', or a symbol referring to
6270 a registered back-end.
6272 BUFFER is the name of the output buffer. If it already exists,
6273 it will be erased first, otherwise, it will be created.
6275 A non-nil optional argument ASYNC means the process should happen
6276 asynchronously. The resulting buffer should then be accessible
6277 through the `org-export-stack' interface. When ASYNC is nil, the
6278 buffer is displayed if `org-export-show-temporary-export-buffer'
6281 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6282 EXT-PLIST are similar to those used in `org-export-as', which
6285 Optional argument POST-PROCESS is a function which should accept
6286 no argument. It is always called within the current process,
6287 from BUFFER, with point at its beginning. Export back-ends can
6288 use it to set a major mode there, e.g,
6290 (defun org-latex-export-as-latex
6291 (&optional async subtreep visible-only body-only ext-plist)
6293 (org-export-to-buffer \\='latex \"*Org LATEX Export*\"
6294 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
6296 This function returns BUFFER."
6297 (declare (indent 2))
6299 (org-export-async-start
6301 (with-current-buffer (get-buffer-create ,buffer
)
6303 (setq buffer-file-coding-system
',buffer-file-coding-system
)
6305 (goto-char (point-min))
6306 (org-export-add-to-stack (current-buffer) ',backend
)
6307 (ignore-errors (funcall ,post-process
))))
6309 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
6311 (org-export-as backend subtreep visible-only body-only ext-plist
))
6312 (buffer (get-buffer-create buffer
))
6313 (encoding buffer-file-coding-system
))
6314 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
6315 (org-kill-new output
))
6316 (with-current-buffer buffer
6318 (setq buffer-file-coding-system encoding
)
6320 (goto-char (point-min))
6321 (and (functionp post-process
) (funcall post-process
)))
6322 (when org-export-show-temporary-export-buffer
6323 (switch-to-buffer-other-window buffer
))
6327 (defun org-export-to-file
6328 (backend file
&optional async subtreep visible-only body-only ext-plist
6330 "Call `org-export-as' with output to a specified file.
6332 BACKEND is either an export back-end, as returned by, e.g.,
6333 `org-export-create-backend', or a symbol referring to
6334 a registered back-end. FILE is the name of the output file, as
6337 A non-nil optional argument ASYNC means the process should happen
6338 asynchronously. The resulting buffer will then be accessible
6339 through the `org-export-stack' interface.
6341 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6342 EXT-PLIST are similar to those used in `org-export-as', which
6345 Optional argument POST-PROCESS is called with FILE as its
6346 argument and happens asynchronously when ASYNC is non-nil. It
6347 has to return a file name, or nil. Export back-ends can use this
6348 to send the output file through additional processing, e.g,
6350 (defun org-latex-export-to-latex
6351 (&optional async subtreep visible-only body-only ext-plist)
6353 (let ((outfile (org-export-output-file-name \".tex\" subtreep)))
6354 (org-export-to-file \\='latex outfile
6355 async subtreep visible-only body-only ext-plist
6356 (lambda (file) (org-latex-compile file)))
6358 The function returns either a file name returned by POST-PROCESS,
6360 (declare (indent 2))
6361 (if (not (file-writable-p file
)) (error "Output file not writable")
6362 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
6363 (encoding (or org-export-coding-system buffer-file-coding-system
)))
6365 (org-export-async-start
6367 (org-export-add-to-stack (expand-file-name file
) ',backend
))
6370 ',backend
,subtreep
,visible-only
,body-only
6374 (let ((coding-system-for-write ',encoding
))
6375 (write-file ,file
)))
6376 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
6377 (let ((output (org-export-as
6378 backend subtreep visible-only body-only ext-plist
)))
6381 (let ((coding-system-for-write encoding
))
6383 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
6384 (org-kill-new output
))
6385 ;; Get proper return value.
6386 (or (and (functionp post-process
) (funcall post-process file
))
6389 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
6390 "Return output file's name according to buffer specifications.
6392 EXTENSION is a string representing the output file extension,
6393 with the leading dot.
6395 With a non-nil optional argument SUBTREEP, try to determine
6396 output file's name by looking for \"EXPORT_FILE_NAME\" property
6397 of subtree at point.
6399 When optional argument PUB-DIR is set, use it as the publishing
6402 Return file name as a string."
6403 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
6406 (file-name-sans-extension
6408 ;; Check EXPORT_FILE_NAME subtree property.
6409 (and subtreep
(org-entry-get nil
"EXPORT_FILE_NAME" 'selective
))
6410 ;; Check #+EXPORT_FILE_NAME keyword.
6411 (org-with-point-at (point-min)
6413 (let ((case-fold-search t
))
6414 (while (re-search-forward
6415 "^[ \t]*#\\+EXPORT_FILE_NAME:[ \t]+\\S-" nil t
)
6416 (let ((element (org-element-at-point)))
6417 (when (eq 'keyword
(org-element-type element
))
6419 (org-element-property :value element
))))))))
6420 ;; Extract from buffer's associated file, if any.
6421 (and visited-file
(file-name-nondirectory visited-file
))
6422 ;; Can't determine file name on our own: ask user.
6424 "Output file: " pub-dir nil nil nil
6425 (lambda (n) (string= extension
(file-name-extension n t
))))))
6428 ;; Build file name. Enforce EXTENSION over whatever user
6429 ;; may have come up with. PUB-DIR, if defined, always has
6430 ;; precedence over any provided path.
6432 (pub-dir (concat (file-name-as-directory pub-dir
)
6433 (file-name-nondirectory base-name
)))
6434 ((file-name-absolute-p base-name
) base-name
)
6436 ;; If writing to OUTPUT-FILE would overwrite original file, append
6437 ;; EXTENSION another time to final name.
6438 (if (and visited-file
(file-equal-p visited-file output-file
))
6439 (concat output-file extension
)
6442 (defun org-export-add-to-stack (source backend
&optional process
)
6443 "Add a new result to export stack if not present already.
6445 SOURCE is a buffer or a file name containing export results.
6446 BACKEND is a symbol representing export back-end used to generate
6449 Entries already pointing to SOURCE and unavailable entries are
6450 removed beforehand. Return the new stack."
6451 (setq org-export-stack-contents
6452 (cons (list source backend
(or process
(current-time)))
6453 (org-export-stack-remove source
))))
6455 (defun org-export-stack ()
6456 "Menu for asynchronous export results and running processes."
6458 (let ((buffer (get-buffer-create "*Org Export Stack*")))
6459 (with-current-buffer buffer
6460 (org-export-stack-mode)
6461 (tabulated-list-print t
))
6462 (pop-to-buffer buffer
))
6463 (message "Type \"q\" to quit, \"?\" for help"))
6465 (defun org-export-stack-clear ()
6466 "Remove all entries from export stack."
6468 (setq org-export-stack-contents nil
))
6470 (defun org-export-stack-refresh ()
6471 "Refresh the export stack."
6473 (tabulated-list-print t
))
6475 (defun org-export-stack-remove (&optional source
)
6476 "Remove export results at point from stack.
6477 If optional argument SOURCE is non-nil, remove it instead."
6479 (let ((source (or source
(org-export--stack-source-at-point))))
6480 (setq org-export-stack-contents
6481 (cl-remove-if (lambda (el) (equal (car el
) source
))
6482 org-export-stack-contents
))))
6484 (defun org-export-stack-view (&optional in-emacs
)
6485 "View export results at point in stack.
6486 With an optional prefix argument IN-EMACS, force viewing files
6489 (let ((source (org-export--stack-source-at-point)))
6490 (cond ((processp source
)
6491 (org-switch-to-buffer-other-window (process-buffer source
)))
6492 ((bufferp source
) (org-switch-to-buffer-other-window source
))
6493 (t (org-open-file source in-emacs
)))))
6495 (defvar org-export-stack-mode-map
6496 (let ((km (make-sparse-keymap)))
6497 (set-keymap-parent km tabulated-list-mode-map
)
6498 (define-key km
" " 'next-line
)
6499 (define-key km
"\C-n" 'next-line
)
6500 (define-key km
[down] 'next-line)
6501 (define-key km "\C-p" 'previous-line)
6502 (define-key km "\C-?" 'previous-line)
6503 (define-key km [up] 'previous-line)
6504 (define-key km "C" 'org-export-stack-clear)
6505 (define-key km "v" 'org-export-stack-view)
6506 (define-key km (kbd "RET") 'org-export-stack-view)
6507 (define-key km "d" 'org-export-stack-remove)
6509 "Keymap for Org Export Stack.")
6511 (define-derived-mode org-export-stack-mode tabulated-list-mode "Org-Stack"
6512 "Mode for displaying asynchronous export stack.
6514 Type `\\[org-export-stack]' to visualize the asynchronous export
6517 In an Org Export Stack buffer, use \
6518 \\<org-export-stack-mode-map>`\\[org-export-stack-view]' to view export output
6519 on current line, `\\[org-export-stack-remove]' to remove it from the stack and \
6520 `\\[org-export-stack-clear]' to clear
6523 Removing entries in a stack buffer does not affect files
6524 or buffers, only display.
6526 \\{org-export-stack-mode-map}"
6527 (setq tabulated-list-format
6528 (vector (list "#" 4 #'org-export--stack-num-predicate)
6529 (list "Back-End" 12 t)
6531 (list "Source" 0 nil)))
6532 (setq tabulated-list-sort-key (cons "#" nil))
6533 (setq tabulated-list-entries #'org-export--stack-generate)
6534 (add-hook 'tabulated-list-revert-hook #'org-export--stack-generate nil t)
6535 (add-hook 'post-command-hook #'org-export-stack-refresh nil t)
6536 (tabulated-list-init-header))
6538 (defun org-export--stack-generate ()
6539 "Generate the asynchronous export stack for display.
6540 Unavailable sources are removed from the list. Return a list
6541 appropriate for `tabulated-list-print'."
6542 ;; Clear stack from exited processes, dead buffers or non-existent
6544 (setq org-export-stack-contents
6547 (if (processp (nth 2 el))
6548 (buffer-live-p (process-buffer (nth 2 el)))
6549 (let ((source (car el)))
6550 (if (bufferp source) (buffer-live-p source)
6551 (file-exists-p source)))))
6552 org-export-stack-contents))
6553 ;; Update `tabulated-list-entries'.
6557 (let ((source (car entry)))
6561 (number-to-string (cl-incf counter))
6563 (if (nth 1 entry) (symbol-name (nth 1 entry)) "")
6565 (let ((info (nth 2 entry)))
6566 (if (processp info) (symbol-name (process-status info))
6567 (format-seconds "%h:%.2m" (float-time (time-since info)))))
6569 (if (stringp source) source (buffer-name source))))))
6570 org-export-stack-contents)))
6572 (defun org-export--stack-num-predicate (a b)
6573 (< (string-to-number (aref (nth 1 a) 0))
6574 (string-to-number (aref (nth 1 b) 0))))
6576 (defun org-export--stack-source-at-point ()
6577 "Return source from export results at point in stack."
6578 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
6579 (if (not source) (error "Source unavailable, please refresh buffer")
6580 (let ((source-name (if (stringp source) source (buffer-name source))))
6583 (looking-at-p (concat ".* +" (regexp-quote source-name) "$")))
6585 ;; SOURCE is not consistent with current line. The stack
6586 ;; view is outdated.
6587 (error (substitute-command-keys
6588 "Source unavailable; type `\\[org-export-stack-refresh]' \
6589 to refresh buffer")))))))
6595 ;; `org-export-dispatch' is the standard interactive way to start an
6596 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6597 ;; for its interface, which, in turn, delegates response to key
6598 ;; pressed to `org-export--dispatch-action'.
6601 (defun org-export-dispatch (&optional arg)
6602 "Export dispatcher for Org mode.
6604 It provides an access to common export related tasks in a buffer.
6605 Its interface comes in two flavors: standard and expert.
6607 While both share the same set of bindings, only the former
6608 displays the valid keys associations in a dedicated buffer.
6609 Scrolling (resp. line-wise motion) in this buffer is done with
6610 SPC and DEL (resp. C-n and C-p) keys.
6612 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6613 flavor or the other.
6615 When ARG is `\\[universal-argument]', repeat the last export action, with the\
6617 set of options used back then, on the current buffer.
6619 When ARG is `\\[universal-argument] \\[universal-argument]', display the \
6620 asynchronous export stack."
6623 (cond ((equal arg '(16)) '(stack))
6624 ((and arg org-export-dispatch-last-action))
6625 (t (save-window-excursion
6628 ;; Remember where we are
6629 (move-marker org-export-dispatch-last-position
6631 (org-base-buffer (current-buffer)))
6632 ;; Get and store an export command
6633 (setq org-export-dispatch-last-action
6634 (org-export--dispatch-ui
6635 (list org-export-initial-scope
6636 (and org-export-in-background 'async))
6638 org-export-dispatch-use-expert-ui)))
6639 (and (get-buffer "*Org Export Dispatcher*")
6640 (kill-buffer "*Org Export Dispatcher*")))))))
6641 (action (car input))
6642 (optns (cdr input)))
6643 (unless (memq 'subtree optns)
6644 (move-marker org-export-dispatch-last-position nil))
6646 ;; First handle special hard-coded actions.
6647 (template (org-export-insert-default-template nil optns))
6648 (stack (org-export-stack))
6649 (publish-current-file
6650 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6651 (publish-current-project
6652 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6653 (publish-choose-project
6654 (org-publish (assoc (completing-read
6656 org-publish-project-alist nil t)
6657 org-publish-project-alist)
6659 (memq 'async optns)))
6660 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6664 ;; Repeating command, maybe move cursor to restore subtree
6666 (if (eq (marker-buffer org-export-dispatch-last-position)
6667 (org-base-buffer (current-buffer)))
6668 (goto-char org-export-dispatch-last-position)
6669 ;; We are in a different buffer, forget position.
6670 (move-marker org-export-dispatch-last-position nil)))
6672 ;; Return a symbol instead of a list to ease
6673 ;; asynchronous export macro use.
6674 (and (memq 'async optns) t)
6675 (and (memq 'subtree optns) t)
6676 (and (memq 'visible optns) t)
6677 (and (memq 'body optns) t)))))))
6679 (defun org-export--dispatch-ui (options first-key expertp)
6680 "Handle interface for `org-export-dispatch'.
6682 OPTIONS is a list containing current interactive options set for
6683 export. It can contain any of the following symbols:
6684 `body' toggles a body-only export
6685 `subtree' restricts export to current subtree
6686 `visible' restricts export to visible part of buffer.
6687 `force' force publishing files.
6688 `async' use asynchronous export process
6690 FIRST-KEY is the key pressed to select the first level menu. It
6691 is nil when this menu hasn't been selected yet.
6693 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6694 buffer is provided, but indications about currently active
6695 options are given in the prompt. Moreover, [?] allows switching
6696 back to standard interface."
6698 (lambda (key &optional access-key)
6699 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6700 ;; non-nil is the required first-level key to activate
6701 ;; KEY. When its value is t, activate KEY independently
6702 ;; on the first key, if any. A nil value means KEY will
6703 ;; only be activated at first level.
6704 (if (or (eq access-key t) (eq access-key first-key))
6705 (propertize key 'face 'org-warning)
6709 ;; Fontify VALUE string.
6710 (propertize value 'face 'font-lock-variable-name-face)))
6711 ;; Prepare menu entries by extracting them from registered
6712 ;; back-ends and sorting them by access key and by ordinal,
6715 (sort (sort (delq nil
6716 (mapcar #'org-export-backend-menu
6717 org-export-registered-backends))
6719 (let ((key-a (nth 1 a))
6721 (cond ((and (numberp key-a) (numberp key-b))
6723 ((numberp key-b) t)))))
6724 'car-less-than-car))
6725 ;; Compute a list of allowed keys based on the first key
6726 ;; pressed, if any. Some keys
6727 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6730 (nconc (list 2 22 19 6 1)
6731 (if (not first-key) (org-uniquify (mapcar 'car entries))
6733 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6734 (when (eq (car entry) first-key)
6735 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6736 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6737 ((not first-key) (list ?P)))
6739 (when expertp (list ??))
6741 ;; Build the help menu for standard UI.
6745 ;; Options are hard-coded.
6746 (format "[%s] Body only: %s [%s] Visible only: %s
6747 \[%s] Export scope: %s [%s] Force publishing: %s
6748 \[%s] Async export: %s\n\n"
6749 (funcall fontify-key "C-b" t)
6750 (funcall fontify-value
6751 (if (memq 'body options) "On " "Off"))
6752 (funcall fontify-key "C-v" t)
6753 (funcall fontify-value
6754 (if (memq 'visible options) "On " "Off"))
6755 (funcall fontify-key "C-s" t)
6756 (funcall fontify-value
6757 (if (memq 'subtree options) "Subtree" "Buffer "))
6758 (funcall fontify-key "C-f" t)
6759 (funcall fontify-value
6760 (if (memq 'force options) "On " "Off"))
6761 (funcall fontify-key "C-a" t)
6762 (funcall fontify-value
6763 (if (memq 'async options) "On " "Off")))
6764 ;; Display registered back-end entries. When a key
6765 ;; appears for the second time, do not create another
6766 ;; entry, but append its sub-menu to existing menu.
6770 (let ((top-key (car entry)))
6772 (unless (eq top-key last-key)
6773 (setq last-key top-key)
6774 (format "\n[%s] %s\n"
6775 (funcall fontify-key (char-to-string top-key))
6777 (let ((sub-menu (nth 2 entry)))
6778 (unless (functionp sub-menu)
6779 ;; Split sub-menu into two columns.
6786 (if (zerop (mod index 2)) " [%s] %-26s"
6788 (funcall fontify-key
6789 (char-to-string (car sub-entry))
6793 (when (zerop (mod index 2)) "\n"))))))))
6795 ;; Publishing menu is hard-coded.
6796 (format "\n[%s] Publish
6797 [%s] Current file [%s] Current project
6798 [%s] Choose project [%s] All projects\n\n\n"
6799 (funcall fontify-key "P")
6800 (funcall fontify-key "f" ?P)
6801 (funcall fontify-key "p" ?P)
6802 (funcall fontify-key "x" ?P)
6803 (funcall fontify-key "a" ?P))
6804 (format "[%s] Export stack [%s] Insert template\n"
6805 (funcall fontify-key "&" t)
6806 (funcall fontify-key "#" t))
6808 (funcall fontify-key "q" t)
6809 (if first-key "Main menu" "Exit")))))
6810 ;; Build prompts for both standard and expert UI.
6811 (standard-prompt (unless expertp "Export command: "))
6815 "Export command (C-%s%s%s%s%s) [%s]: "
6816 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6817 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6818 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6819 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6820 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6821 (mapconcat (lambda (k)
6822 ;; Strip control characters.
6823 (unless (< k 27) (char-to-string k)))
6824 allowed-keys "")))))
6825 ;; With expert UI, just read key with a fancy prompt. In standard
6826 ;; UI, display an intrusive help buffer.
6828 (org-export--dispatch-action
6829 expert-prompt allowed-keys entries options first-key expertp)
6830 ;; At first call, create frame layout in order to display menu.
6831 (unless (get-buffer "*Org Export Dispatcher*")
6832 (delete-other-windows)
6833 (org-switch-to-buffer-other-window
6834 (get-buffer-create "*Org Export Dispatcher*"))
6835 (setq cursor-type nil
6836 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6837 ;; Make sure that invisible cursor will not highlight square
6839 (set-syntax-table (copy-syntax-table))
6840 (modify-syntax-entry ?\[ "w"))
6841 ;; At this point, the buffer containing the menu exists and is
6842 ;; visible in the current window. So, refresh it.
6843 (with-current-buffer "*Org Export Dispatcher*"
6844 ;; Refresh help. Maintain display continuity by re-visiting
6845 ;; previous window position.
6846 (let ((pos (window-start)))
6849 (set-window-start nil pos)))
6850 (org-fit-window-to-buffer)
6851 (org-export--dispatch-action
6852 standard-prompt allowed-keys entries options first-key expertp))))
6854 (defun org-export--dispatch-action
6855 (prompt allowed-keys entries options first-key expertp)
6856 "Read a character from command input and act accordingly.
6858 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6859 a list of characters available at a given step in the process.
6860 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6861 EXPERTP are the same as defined in `org-export--dispatch-ui',
6864 Toggle export options when required. Otherwise, return value is
6865 a list with action as CAR and a list of interactive export
6868 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6870 (while (and (setq key (read-char-exclusive prompt))
6872 (memq key '(14 16 ?\s ?\d)))
6874 (14 (if (not (pos-visible-in-window-p (point-max)))
6875 (ignore-errors (scroll-up 1))
6876 (message "End of buffer")
6878 (16 (if (not (pos-visible-in-window-p (point-min)))
6879 (ignore-errors (scroll-down 1))
6880 (message "Beginning of buffer")
6882 (?\s (if (not (pos-visible-in-window-p (point-max)))
6884 (message "End of buffer")
6886 (?\d (if (not (pos-visible-in-window-p (point-min)))
6888 (message "Beginning of buffer")
6891 ;; Ignore undefined associations.
6892 ((not (memq key allowed-keys))
6894 (unless expertp (message "Invalid key") (sit-for 1))
6895 (org-export--dispatch-ui options first-key expertp))
6896 ;; q key at first level aborts export. At second level, cancel
6897 ;; first key instead.
6898 ((eq key ?q) (if (not first-key) (error "Export aborted")
6899 (org-export--dispatch-ui options nil expertp)))
6900 ;; Help key: Switch back to standard interface if expert UI was
6902 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6903 ;; Send request for template insertion along with export scope.
6904 ((eq key ?#) (cons 'template (memq 'subtree options)))
6905 ;; Switch to asynchronous export stack.
6906 ((eq key ?&) '(stack))
6907 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6908 ((memq key '(2 22 19 6 1))
6909 (org-export--dispatch-ui
6910 (let ((option (cl-case key (2 'body) (22 'visible) (19 'subtree)
6911 (6 'force) (1 'async))))
6912 (if (memq option options) (remq option options)
6913 (cons option options)))
6915 ;; Action selected: Send key and options back to
6916 ;; `org-export-dispatch'.
6917 ((or first-key (functionp (nth 2 (assq key entries))))
6919 ((not first-key) (nth 2 (assq key entries)))
6920 ;; Publishing actions are hard-coded. Send a special
6921 ;; signal to `org-export-dispatch'.
6924 (?f 'publish-current-file)
6925 (?p 'publish-current-project)
6926 (?x 'publish-choose-project)
6928 ;; Return first action associated to FIRST-KEY + KEY
6929 ;; path. Indeed, derived backends can share the same
6932 (dolist (entry (member (assq first-key entries) entries))
6933 (let ((match (assq key (nth 2 entry))))
6934 (when match (throw 'found (nth 2 match))))))))
6936 ;; Otherwise, enter sub-menu.
6937 (t (org-export--dispatch-ui options key expertp)))))
6944 ;; generated-autoload-file: "org-loaddefs.el"