1 ;;; ox.el --- Export Framework for Org Mode -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2015 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 <http://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 <http://orgmode.org/worg/dev/org-export-reference.html> for
75 (require 'org-element
)
79 (declare-function org-publish
"ox-publish" (project &optional force async
))
80 (declare-function org-publish-all
"ox-publish" (&optional force async
))
82 org-publish-current-file
"ox-publish" (&optional force async
))
83 (declare-function org-publish-current-project
"ox-publish"
84 (&optional force async
))
86 (defvar org-publish-project-alist
)
87 (defvar org-table-number-fraction
)
88 (defvar org-table-number-regexp
)
91 ;;; Internal Variables
93 ;; Among internal variables, the most important is
94 ;; `org-export-options-alist'. This variable define the global export
95 ;; options, shared between every exporter, and how they are acquired.
97 (defconst org-export-max-depth
19
98 "Maximum nesting depth for headlines, counting from 0.")
100 (defconst org-export-options-alist
101 '((:title
"TITLE" nil nil parse
)
102 (:date
"DATE" nil nil parse
)
103 (:author
"AUTHOR" nil user-full-name parse
)
104 (:email
"EMAIL" nil user-mail-address t
)
105 (:language
"LANGUAGE" nil org-export-default-language t
)
106 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
107 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
108 (:creator
"CREATOR" nil org-export-creator-string
)
109 (:headline-levels nil
"H" org-export-headline-levels
)
110 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
111 (:section-numbers nil
"num" org-export-with-section-numbers
)
112 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
113 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
114 (:with-author nil
"author" org-export-with-author
)
115 (:with-clocks nil
"c" org-export-with-clocks
)
116 (:with-creator nil
"creator" org-export-with-creator
)
117 (:with-date nil
"date" org-export-with-date
)
118 (:with-drawers nil
"d" org-export-with-drawers
)
119 (:with-email nil
"email" org-export-with-email
)
120 (:with-emphasize nil
"*" org-export-with-emphasize
)
121 (:with-entities nil
"e" org-export-with-entities
)
122 (:with-fixed-width nil
":" org-export-with-fixed-width
)
123 (:with-footnotes nil
"f" org-export-with-footnotes
)
124 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
125 (:with-latex nil
"tex" org-export-with-latex
)
126 (:with-planning nil
"p" org-export-with-planning
)
127 (:with-priority nil
"pri" org-export-with-priority
)
128 (:with-properties nil
"prop" org-export-with-properties
)
129 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
130 (:with-special-strings nil
"-" org-export-with-special-strings
)
131 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
132 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
133 (:with-toc nil
"toc" org-export-with-toc
)
134 (:with-tables nil
"|" org-export-with-tables
)
135 (:with-tags nil
"tags" org-export-with-tags
)
136 (:with-tasks nil
"tasks" org-export-with-tasks
)
137 (:with-timestamps nil
"<" org-export-with-timestamps
)
138 (:with-title nil
"title" org-export-with-title
)
139 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
140 "Alist between export properties and ways to set them.
142 The key of the alist is the property name, and the value is a list
143 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
145 KEYWORD is a string representing a buffer keyword, or nil. Each
146 property defined this way can also be set, during subtree
147 export, through a headline property named after the keyword
148 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
150 OPTION is a string that could be found in an #+OPTIONS: line.
151 DEFAULT is the default value for the property.
152 BEHAVIOR determines how Org should handle multiple keywords for
153 the same property. It is a symbol among:
154 nil Keep old value and discard the new one.
155 t Replace old value with the new one.
156 `space' Concatenate the values, separating them with a space.
157 `newline' Concatenate the values, separating them with
159 `split' Split values at white spaces, and cons them to the
161 `parse' Parse value as a list of strings and Org objects,
162 which can then be transcoded with, e.g.,
163 `org-export-data'. It implies `space' behavior.
165 Values set through KEYWORD and OPTION have precedence over
168 All these properties should be back-end agnostic. Back-end
169 specific properties are set through `org-export-define-backend'.
170 Properties redefined there have precedence over these.")
172 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
173 "List of in-buffer keywords that require special treatment.
174 These keywords are not directly associated to a property. The
175 way they are handled must be hard-coded into
176 `org-export--get-inbuffer-options' function.")
178 (defconst org-export-filters-alist
179 '((:filter-body . org-export-filter-body-functions
)
180 (:filter-bold . org-export-filter-bold-functions
)
181 (:filter-babel-call . org-export-filter-babel-call-functions
)
182 (:filter-center-block . org-export-filter-center-block-functions
)
183 (:filter-clock . org-export-filter-clock-functions
)
184 (:filter-code . org-export-filter-code-functions
)
185 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
186 (:filter-drawer . org-export-filter-drawer-functions
)
187 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
188 (:filter-entity . org-export-filter-entity-functions
)
189 (:filter-example-block . org-export-filter-example-block-functions
)
190 (:filter-export-block . org-export-filter-export-block-functions
)
191 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
192 (:filter-final-output . org-export-filter-final-output-functions
)
193 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
194 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
195 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
196 (:filter-headline . org-export-filter-headline-functions
)
197 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
198 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
199 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
200 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
201 (:filter-italic . org-export-filter-italic-functions
)
202 (:filter-item . org-export-filter-item-functions
)
203 (:filter-keyword . org-export-filter-keyword-functions
)
204 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
205 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
206 (:filter-line-break . org-export-filter-line-break-functions
)
207 (:filter-link . org-export-filter-link-functions
)
208 (:filter-node-property . org-export-filter-node-property-functions
)
209 (:filter-options . org-export-filter-options-functions
)
210 (:filter-paragraph . org-export-filter-paragraph-functions
)
211 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
212 (:filter-plain-list . org-export-filter-plain-list-functions
)
213 (:filter-plain-text . org-export-filter-plain-text-functions
)
214 (:filter-planning . org-export-filter-planning-functions
)
215 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
216 (:filter-quote-block . org-export-filter-quote-block-functions
)
217 (:filter-radio-target . org-export-filter-radio-target-functions
)
218 (:filter-section . org-export-filter-section-functions
)
219 (:filter-special-block . org-export-filter-special-block-functions
)
220 (:filter-src-block . org-export-filter-src-block-functions
)
221 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
222 (:filter-strike-through . org-export-filter-strike-through-functions
)
223 (:filter-subscript . org-export-filter-subscript-functions
)
224 (:filter-superscript . org-export-filter-superscript-functions
)
225 (:filter-table . org-export-filter-table-functions
)
226 (:filter-table-cell . org-export-filter-table-cell-functions
)
227 (:filter-table-row . org-export-filter-table-row-functions
)
228 (:filter-target . org-export-filter-target-functions
)
229 (:filter-timestamp . org-export-filter-timestamp-functions
)
230 (:filter-underline . org-export-filter-underline-functions
)
231 (:filter-verbatim . org-export-filter-verbatim-functions
)
232 (:filter-verse-block . org-export-filter-verse-block-functions
))
233 "Alist between filters properties and initial values.
235 The key of each association is a property name accessible through
236 the communication channel. Its value is a configurable global
237 variable defining initial filters.
239 This list is meant to install user specified filters. Back-end
240 developers may install their own filters using
241 `org-export-define-backend'. Filters defined there will always
242 be prepended to the current list, so they always get applied
245 (defconst org-export-default-inline-image-rule
249 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
250 "xpm" "pbm" "pgm" "ppm") t
))))
251 "Default rule for link matching an inline image.
252 This rule applies to links with no description. By default, it
253 will be considered as an inline image if it targets a local file
254 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
255 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
256 See `org-export-inline-image-p' for more information about
259 (defconst org-export-ignored-local-variables
260 '(org-font-lock-keywords
261 org-element--cache org-element--cache-objects org-element--cache-sync-keys
262 org-element--cache-sync-requests org-element--cache-sync-timer
)
263 "List of variables not copied through upon buffer duplication.
264 Export process takes place on a copy of the original buffer.
265 When this copy is created, all Org related local variables not in
266 this list are copied to the new buffer. Variables with an
267 unreadable value are also ignored.")
269 (defvar org-export-async-debug nil
270 "Non-nil means asynchronous export process should leave data behind.
272 This data is found in the appropriate \"*Org Export Process*\"
273 buffer, and in files prefixed with \"org-export-process\" and
274 located in `temporary-file-directory'.
276 When non-nil, it will also set `debug-on-error' to a non-nil
277 value in the external process.")
279 (defvar org-export-stack-contents nil
280 "Record asynchronously generated export results and processes.
281 This is an alist: its CAR is the source of the
282 result (destination file or buffer for a finished process,
283 original buffer for a running one) and its CDR is a list
284 containing the back-end used, as a symbol, and either a process
285 or the time at which it finished. It is used to build the menu
286 from `org-export-stack'.")
288 (defvar org-export-registered-backends nil
289 "List of backends currently available in the exporter.
290 This variable is set with `org-export-define-backend' and
291 `org-export-define-derived-backend' functions.")
293 (defvar org-export-dispatch-last-action nil
294 "Last command called from the dispatcher.
295 The value should be a list. Its CAR is the action, as a symbol,
296 and its CDR is a list of export options.")
298 (defvar org-export-dispatch-last-position
(make-marker)
299 "The position where the last export command was created using the dispatcher.
300 This marker will be used with `C-u C-c C-e' to make sure export repetition
301 uses the same subtree if the previous command was restricted to a subtree.")
303 ;; For compatibility with Org < 8
304 (defvar org-export-current-backend nil
305 "Name, if any, of the back-end used during an export process.
307 Its value is a symbol such as `html', `latex', `ascii', or nil if
308 the back-end is anonymous (see `org-export-create-backend') or if
309 there is no export process in progress.
311 It can be used to teach Babel blocks how to act differently
312 according to the back-end used.")
316 ;;; User-configurable Variables
318 ;; Configuration for the masses.
320 ;; They should never be accessed directly, as their value is to be
321 ;; stored in a property list (cf. `org-export-options-alist').
322 ;; Back-ends will read their value from there instead.
324 (defgroup org-export nil
325 "Options for exporting Org mode files."
329 (defgroup org-export-general nil
330 "General options for export engine."
331 :tag
"Org Export General"
334 (defcustom org-export-with-archived-trees
'headline
335 "Whether sub-trees with the ARCHIVE tag should be exported.
337 This can have three different values:
338 nil Do not export, pretend this tree is not present.
339 t Do export the entire tree.
340 `headline' Only export the headline, but skip the tree below it.
342 This option can also be set with the OPTIONS keyword,
344 :group
'org-export-general
346 (const :tag
"Not at all" nil
)
347 (const :tag
"Headline only" headline
)
348 (const :tag
"Entirely" t
)))
350 (defcustom org-export-with-author t
351 "Non-nil means insert author name into the exported file.
352 This option can also be set with the OPTIONS keyword,
353 e.g. \"author:nil\"."
354 :group
'org-export-general
357 (defcustom org-export-with-clocks nil
358 "Non-nil means export CLOCK keywords.
359 This option can also be set with the OPTIONS keyword,
361 :group
'org-export-general
364 (defcustom org-export-with-creator nil
365 "Non-nil means the postamble should contain a creator sentence.
367 The sentence can be set in `org-export-creator-string', which
370 This option can also be set with the OPTIONS keyword, e.g.,
372 :group
'org-export-general
374 :package-version
'(Org .
"8.3")
377 (defcustom org-export-with-date t
378 "Non-nil means insert date in the exported document.
379 This option can also be set with the OPTIONS keyword,
381 :group
'org-export-general
384 (defcustom org-export-date-timestamp-format nil
385 "Time-stamp format string to use for DATE keyword.
387 The format string, when specified, only applies if date consists
388 in a single time-stamp. Otherwise its value will be ignored.
390 See `format-time-string' for details on how to build this
392 :group
'org-export-general
394 (string :tag
"Time-stamp format string")
395 (const :tag
"No format string" nil
)))
397 (defcustom org-export-creator-string
398 (format "Emacs %s (Org mode %s)"
400 (if (fboundp 'org-version
) (org-version) "unknown version"))
401 "Information about the creator of the document.
402 This option can also be set on with the CREATOR keyword."
403 :group
'org-export-general
404 :type
'(string :tag
"Creator string"))
406 (defcustom org-export-with-drawers
'(not "LOGBOOK")
407 "Non-nil means export contents of standard drawers.
409 When t, all drawers are exported. This may also be a list of
410 drawer names to export, as strings. If that list starts with
411 `not', only drawers with such names will be ignored.
413 This variable doesn't apply to properties drawers. See
414 `org-export-with-properties' instead.
416 This option can also be set with the OPTIONS keyword,
418 :group
'org-export-general
420 :package-version
'(Org .
"8.0")
422 (const :tag
"All drawers" t
)
423 (const :tag
"None" nil
)
424 (repeat :tag
"Selected drawers"
425 (string :tag
"Drawer name"))
426 (list :tag
"Ignored drawers"
427 (const :format
"" not
)
428 (repeat :tag
"Specify names of drawers to ignore during export"
430 (string :tag
"Drawer name")))))
432 (defcustom org-export-with-email nil
433 "Non-nil means insert author email into the exported file.
434 This option can also be set with the OPTIONS keyword,
436 :group
'org-export-general
439 (defcustom org-export-with-emphasize t
440 "Non-nil means interpret *word*, /word/, _word_ and +word+.
442 If the export target supports emphasizing text, the word will be
443 typeset in bold, italic, with an underline or strike-through,
446 This option can also be set with the OPTIONS keyword,
448 :group
'org-export-general
451 (defcustom org-export-exclude-tags
'("noexport")
452 "Tags that exclude a tree from export.
454 All trees carrying any of these tags will be excluded from
455 export. This is without condition, so even subtrees inside that
456 carry one of the `org-export-select-tags' will be removed.
458 This option can also be set with the EXCLUDE_TAGS keyword."
459 :group
'org-export-general
460 :type
'(repeat (string :tag
"Tag")))
462 (defcustom org-export-with-fixed-width t
463 "Non-nil means export lines starting with \":\".
464 This option can also be set with the OPTIONS keyword,
466 :group
'org-export-general
468 :package-version
'(Org .
"8.0")
471 (defcustom org-export-with-footnotes t
472 "Non-nil means Org footnotes should be exported.
473 This option can also be set with the OPTIONS keyword,
475 :group
'org-export-general
478 (defcustom org-export-with-latex t
479 "Non-nil means process LaTeX environments and fragments.
481 This option can also be set with the OPTIONS line,
482 e.g. \"tex:verbatim\". Allowed values are:
484 nil Ignore math snippets.
485 `verbatim' Keep everything in verbatim.
486 t Allow export of math snippets."
487 :group
'org-export-general
489 :package-version
'(Org .
"8.0")
491 (const :tag
"Do not process math in any way" nil
)
492 (const :tag
"Interpret math snippets" t
)
493 (const :tag
"Leave math verbatim" verbatim
)))
495 (defcustom org-export-headline-levels
3
496 "The last level which is still exported as a headline.
498 Inferior levels will usually produce itemize or enumerate lists
499 when exported, but back-end behavior may differ.
501 This option can also be set with the OPTIONS keyword,
503 :group
'org-export-general
506 (defcustom org-export-default-language
"en"
507 "The default language for export and clocktable translations, as a string.
508 This may have an association in
509 `org-clock-clocktable-language-setup',
510 `org-export-smart-quotes-alist' and `org-export-dictionary'.
511 This option can also be set with the LANGUAGE keyword."
512 :group
'org-export-general
513 :type
'(string :tag
"Language"))
515 (defcustom org-export-preserve-breaks nil
516 "Non-nil means preserve all line breaks when exporting.
517 This option can also be set with the OPTIONS keyword,
519 :group
'org-export-general
522 (defcustom org-export-with-entities t
523 "Non-nil means interpret entities when exporting.
525 For example, HTML export converts \\alpha to α and \\AA to
528 For a list of supported names, see the constant `org-entities'
529 and the user option `org-entities-user'.
531 This option can also be set with the OPTIONS keyword,
533 :group
'org-export-general
536 (defcustom org-export-with-inlinetasks t
537 "Non-nil means inlinetasks should be exported.
538 This option can also be set with the OPTIONS keyword,
539 e.g. \"inline:nil\"."
540 :group
'org-export-general
542 :package-version
'(Org .
"8.0")
545 (defcustom org-export-with-planning nil
546 "Non-nil means include planning info in export.
548 Planning info is the line containing either SCHEDULED:,
549 DEADLINE:, CLOSED: time-stamps, or a combination of them.
551 This option can also be set with the OPTIONS keyword,
553 :group
'org-export-general
555 :package-version
'(Org .
"8.0")
558 (defcustom org-export-with-priority nil
559 "Non-nil means include priority cookies in export.
560 This option can also be set with the OPTIONS keyword,
562 :group
'org-export-general
565 (defcustom org-export-with-properties nil
566 "Non-nil means export contents of properties drawers.
568 When t, all properties are exported. This may also be a list of
569 properties to export, as strings.
571 This option can also be set with the OPTIONS keyword,
573 :group
'org-export-general
575 :package-version
'(Org .
"8.3")
577 (const :tag
"All properties" t
)
578 (const :tag
"None" nil
)
579 (repeat :tag
"Selected properties"
580 (string :tag
"Property name"))))
582 (defcustom org-export-with-section-numbers t
583 "Non-nil means add section numbers to headlines when exporting.
585 When set to an integer n, numbering will only happen for
586 headlines whose relative level is higher or equal to n.
588 This option can also be set with the OPTIONS keyword,
590 :group
'org-export-general
593 (defcustom org-export-select-tags
'("export")
594 "Tags that select a tree for export.
596 If any such tag is found in a buffer, all trees that do not carry
597 one of these tags will be ignored during export. Inside trees
598 that are selected like this, you can still deselect a subtree by
599 tagging it with one of the `org-export-exclude-tags'.
601 This option can also be set with the SELECT_TAGS keyword."
602 :group
'org-export-general
603 :type
'(repeat (string :tag
"Tag")))
605 (defcustom org-export-with-smart-quotes nil
606 "Non-nil means activate smart quotes during export.
607 This option can also be set with the OPTIONS keyword,
610 When setting this to non-nil, you need to take care of
611 using the correct Babel package when exporting to LaTeX.
612 E.g., you can load Babel for french like this:
614 #+LATEX_HEADER: \\usepackage[french]{babel}"
615 :group
'org-export-general
617 :package-version
'(Org .
"8.0")
620 (defcustom org-export-with-special-strings t
621 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
623 When this option is turned on, these strings will be exported as:
626 -----+----------+--------+-------
630 ... … \\ldots …
632 This option can also be set with the OPTIONS keyword,
634 :group
'org-export-general
637 (defcustom org-export-with-statistics-cookies t
638 "Non-nil means include statistics cookies in export.
639 This option can also be set with the OPTIONS keyword,
641 :group
'org-export-general
643 :package-version
'(Org .
"8.0")
646 (defcustom org-export-with-sub-superscripts t
647 "Non-nil means interpret \"_\" and \"^\" for export.
649 If you want to control how Org displays those characters, see
650 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
651 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
654 When this option is turned on, you can use TeX-like syntax for
655 sub- and superscripts and see them exported correctly.
657 You can also set the option with #+OPTIONS: ^:t
659 Several characters after \"_\" or \"^\" will be considered as a
660 single item - so grouping with {} is normally not needed. For
661 example, the following things will be parsed as single sub- or
664 10^24 or 10^tau several digits will be considered 1 item.
665 10^-12 or 10^-tau a leading sign with digits or a word
666 x^2-y^3 will be read as x^2 - y^3, because items are
667 terminated by almost any nonword/nondigit char.
668 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
670 Still, ambiguity is possible. So when in doubt, use {} to enclose
671 the sub/superscript. If you set this variable to the symbol `{}',
672 the braces are *required* in order to trigger interpretations as
673 sub/superscript. This can be helpful in documents that need \"_\"
674 frequently in plain text."
675 :group
'org-export-general
677 :package-version
'(Org .
"8.0")
679 (const :tag
"Interpret them" t
)
680 (const :tag
"Curly brackets only" {})
681 (const :tag
"Do not interpret them" nil
)))
683 (defcustom org-export-with-toc t
684 "Non-nil means create a table of contents in exported files.
686 The TOC contains headlines with levels up
687 to`org-export-headline-levels'. When an integer, include levels
688 up to N in the toc, this may then be different from
689 `org-export-headline-levels', but it will not be allowed to be
690 larger than the number of headline levels. When nil, no table of
693 This option can also be set with the OPTIONS keyword,
694 e.g. \"toc:nil\" or \"toc:3\"."
695 :group
'org-export-general
697 (const :tag
"No Table of Contents" nil
)
698 (const :tag
"Full Table of Contents" t
)
699 (integer :tag
"TOC to level")))
701 (defcustom org-export-with-tables t
702 "Non-nil means export tables.
703 This option can also be set with the OPTIONS keyword,
705 :group
'org-export-general
707 :package-version
'(Org .
"8.0")
710 (defcustom org-export-with-tags t
711 "If nil, do not export tags, just remove them from headlines.
713 If this is the symbol `not-in-toc', tags will be removed from
714 table of contents entries, but still be shown in the headlines of
717 This option can also be set with the OPTIONS keyword,
719 :group
'org-export-general
721 (const :tag
"Off" nil
)
722 (const :tag
"Not in TOC" not-in-toc
)
723 (const :tag
"On" t
)))
725 (defcustom org-export-with-tasks t
726 "Non-nil means include TODO items for export.
728 This may have the following values:
729 t include tasks independent of state.
730 `todo' include only tasks that are not yet done.
731 `done' include only tasks that are already done.
732 nil ignore all tasks.
733 list of keywords include tasks with these keywords.
735 This option can also be set with the OPTIONS keyword,
737 :group
'org-export-general
739 (const :tag
"All tasks" t
)
740 (const :tag
"No tasks" nil
)
741 (const :tag
"Not-done tasks" todo
)
742 (const :tag
"Only done tasks" done
)
743 (repeat :tag
"Specific TODO keywords"
744 (string :tag
"Keyword"))))
746 (defcustom org-export-with-title t
747 "Non-nil means print title into the exported file.
748 This option can also be set with the OPTIONS keyword,
750 :group
'org-export-general
752 :package-version
'(Org .
"8.3")
755 (defcustom org-export-time-stamp-file t
756 "Non-nil means insert a time stamp into the exported file.
757 The time stamp shows when the file was created. This option can
758 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
759 :group
'org-export-general
762 (defcustom org-export-with-timestamps t
763 "Non nil means allow timestamps in export.
765 It can be set to any of the following values:
766 t export all timestamps.
767 `active' export active timestamps only.
768 `inactive' export inactive timestamps only.
769 nil do not export timestamps
771 This only applies to timestamps isolated in a paragraph
772 containing only timestamps. Other timestamps are always
775 This option can also be set with the OPTIONS keyword, e.g.
777 :group
'org-export-general
779 (const :tag
"All timestamps" t
)
780 (const :tag
"Only active timestamps" active
)
781 (const :tag
"Only inactive timestamps" inactive
)
782 (const :tag
"No timestamp" nil
)))
784 (defcustom org-export-with-todo-keywords t
785 "Non-nil means include TODO keywords in export.
786 When nil, remove all these keywords from the export. This option
787 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
788 :group
'org-export-general
791 (defcustom org-export-allow-bind-keywords nil
792 "Non-nil means BIND keywords can define local variable values.
793 This is a potential security risk, which is why the default value
794 is nil. You can also allow them through local buffer variables."
795 :group
'org-export-general
797 :package-version
'(Org .
"8.0")
800 (defcustom org-export-snippet-translation-alist nil
801 "Alist between export snippets back-ends and exporter back-ends.
803 This variable allows to provide shortcuts for export snippets.
805 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
806 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
807 HTML code while every other back-end will ignore it."
808 :group
'org-export-general
810 :package-version
'(Org .
"8.0")
812 (cons (string :tag
"Shortcut")
813 (string :tag
"Back-end"))))
815 (defcustom org-export-coding-system nil
816 "Coding system for the exported file."
817 :group
'org-export-general
819 :package-version
'(Org .
"8.0")
820 :type
'coding-system
)
822 (defcustom org-export-copy-to-kill-ring nil
823 "Non-nil means pushing export output to the kill ring.
824 This variable is ignored during asynchronous export."
825 :group
'org-export-general
827 :package-version
'(Org .
"8.3")
829 (const :tag
"Always" t
)
830 (const :tag
"When export is done interactively" if-interactive
)
831 (const :tag
"Never" nil
)))
833 (defcustom org-export-initial-scope
'buffer
834 "The initial scope when exporting with `org-export-dispatch'.
835 This variable can be either set to `buffer' or `subtree'."
836 :group
'org-export-general
838 (const :tag
"Export current buffer" buffer
)
839 (const :tag
"Export current subtree" subtree
)))
841 (defcustom org-export-show-temporary-export-buffer t
842 "Non-nil means show buffer after exporting to temp buffer.
843 When Org exports to a file, the buffer visiting that file is never
844 shown, but remains buried. However, when exporting to
845 a temporary buffer, that buffer is popped up in a second window.
846 When this variable is nil, the buffer remains buried also in
848 :group
'org-export-general
851 (defcustom org-export-in-background nil
852 "Non-nil means export and publishing commands will run in background.
853 Results from an asynchronous export are never displayed
854 automatically. But you can retrieve them with \\[org-export-stack]."
855 :group
'org-export-general
857 :package-version
'(Org .
"8.0")
860 (defcustom org-export-async-init-file nil
861 "File used to initialize external export process.
863 Value must be either nil or an absolute file name. When nil, the
864 external process is launched like a regular Emacs session,
865 loading user's initialization file and any site specific
866 configuration. If a file is provided, it, and only it, is loaded
869 Therefore, using a specific configuration makes the process to
870 load faster and the export more portable."
871 :group
'org-export-general
873 :package-version
'(Org .
"8.0")
875 (const :tag
"Regular startup" nil
)
876 (file :tag
"Specific start-up file" :must-match t
)))
878 (defcustom org-export-dispatch-use-expert-ui nil
879 "Non-nil means using a non-intrusive `org-export-dispatch'.
880 In that case, no help buffer is displayed. Though, an indicator
881 for current export scope is added to the prompt (\"b\" when
882 output is restricted to body only, \"s\" when it is restricted to
883 the current subtree, \"v\" when only visible elements are
884 considered for export, \"f\" when publishing functions should be
885 passed the FORCE argument and \"a\" when the export should be
886 asynchronous). Also, \[?] allows to switch back to standard
888 :group
'org-export-general
890 :package-version
'(Org .
"8.0")
895 ;;; Defining Back-ends
897 ;; An export back-end is a structure with `org-export-backend' type
898 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
901 ;; At the lowest level, a back-end is created with
902 ;; `org-export-create-backend' function.
904 ;; A named back-end can be registered with
905 ;; `org-export-register-backend' function. A registered back-end can
906 ;; later be referred to by its name, with `org-export-get-backend'
907 ;; function. Also, such a back-end can become the parent of a derived
908 ;; back-end from which slot values will be inherited by default.
909 ;; `org-export-derived-backend-p' can check if a given back-end is
910 ;; derived from a list of back-end names.
912 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
913 ;; `org-export-get-all-filters' return the full alist of transcoders,
914 ;; options and filters, including those inherited from ancestors.
916 ;; At a higher level, `org-export-define-backend' is the standard way
917 ;; to define an export back-end. If the new back-end is similar to
918 ;; a registered back-end, `org-export-define-derived-backend' may be
921 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
922 ;; when a given back-end hasn't been registered yet.
924 (cl-defstruct (org-export-backend (:constructor org-export-create-backend
)
926 name parent transcoders options filters blocks menu
)
928 (defun org-export-get-backend (name)
929 "Return export back-end named after NAME.
930 NAME is a symbol. Return nil if no such back-end is found."
932 (dolist (b org-export-registered-backends
)
933 (when (eq (org-export-backend-name b
) name
)
936 (defun org-export-register-backend (backend)
937 "Register BACKEND as a known export back-end.
938 BACKEND is a structure with `org-export-backend' type."
939 ;; Refuse to register an unnamed back-end.
940 (unless (org-export-backend-name backend
)
941 (error "Cannot register a unnamed export back-end"))
942 ;; Refuse to register a back-end with an unknown parent.
943 (let ((parent (org-export-backend-parent backend
)))
944 (when (and parent
(not (org-export-get-backend parent
)))
945 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
946 ;; Register dedicated export blocks in the parser.
947 (dolist (name (org-export-backend-blocks backend
))
948 (add-to-list 'org-element-block-name-alist
949 (cons name
'org-element-export-block-parser
)))
950 ;; If a back-end with the same name as BACKEND is already
951 ;; registered, replace it with BACKEND. Otherwise, simply add
952 ;; BACKEND to the list of registered back-ends.
953 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
954 (if old
(setcar (memq old org-export-registered-backends
) backend
)
955 (push backend org-export-registered-backends
))))
957 (defun org-export-barf-if-invalid-backend (backend)
958 "Signal an error if BACKEND isn't defined."
959 (unless (org-export-backend-p backend
)
960 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
962 (defun org-export-derived-backend-p (backend &rest backends
)
963 "Non-nil if BACKEND is derived from one of BACKENDS.
964 BACKEND is an export back-end, as returned by, e.g.,
965 `org-export-create-backend', or a symbol referring to
966 a registered back-end. BACKENDS is constituted of symbols."
967 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
970 (while (org-export-backend-parent backend
)
971 (when (memq (org-export-backend-name backend
) backends
)
974 (org-export-get-backend (org-export-backend-parent backend
))))
975 (memq (org-export-backend-name backend
) backends
))))
977 (defun org-export-get-all-transcoders (backend)
978 "Return full translation table for BACKEND.
980 BACKEND is an export back-end, as return by, e.g,,
981 `org-export-create-backend'. Return value is an alist where
982 keys are element or object types, as symbols, and values are
985 Unlike to `org-export-backend-transcoders', this function
986 also returns transcoders inherited from parent back-ends,
988 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
990 (let ((transcoders (org-export-backend-transcoders backend
))
992 (while (setq parent
(org-export-backend-parent backend
))
993 (setq backend
(org-export-get-backend parent
))
995 (append transcoders
(org-export-backend-transcoders backend
))))
998 (defun org-export-get-all-options (backend)
999 "Return export options for BACKEND.
1001 BACKEND is an export back-end, as return by, e.g,,
1002 `org-export-create-backend'. See `org-export-options-alist'
1003 for the shape of the return value.
1005 Unlike to `org-export-backend-options', this function also
1006 returns options inherited from parent back-ends, if any."
1007 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1009 (let ((options (org-export-backend-options backend
))
1011 (while (setq parent
(org-export-backend-parent backend
))
1012 (setq backend
(org-export-get-backend parent
))
1013 (setq options
(append options
(org-export-backend-options backend
))))
1016 (defun org-export-get-all-filters (backend)
1017 "Return complete list of filters for BACKEND.
1019 BACKEND is an export back-end, as return by, e.g,,
1020 `org-export-create-backend'. Return value is an alist where
1021 keys are symbols and values lists of functions.
1023 Unlike to `org-export-backend-filters', this function also
1024 returns filters inherited from parent back-ends, if any."
1025 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1027 (let ((filters (org-export-backend-filters backend
))
1029 (while (setq parent
(org-export-backend-parent backend
))
1030 (setq backend
(org-export-get-backend parent
))
1031 (setq filters
(append filters
(org-export-backend-filters backend
))))
1034 (defun org-export-define-backend (backend transcoders
&rest body
)
1035 "Define a new back-end BACKEND.
1037 TRANSCODERS is an alist between object or element types and
1038 functions handling them.
1040 These functions should return a string without any trailing
1041 space, or nil. They must accept three arguments: the object or
1042 element itself, its contents or nil when it isn't recursive and
1043 the property list used as a communication channel.
1045 Contents, when not nil, are stripped from any global indentation
1046 \(although the relative one is preserved). They also always end
1047 with a single newline character.
1049 If, for a given type, no function is found, that element or
1050 object type will simply be ignored, along with any blank line or
1051 white space at its end. The same will happen if the function
1052 returns the nil value. If that function returns the empty
1053 string, the type will be ignored, but the blank lines or white
1054 spaces will be kept.
1056 In addition to element and object types, one function can be
1057 associated to the `template' (or `inner-template') symbol and
1058 another one to the `plain-text' symbol.
1060 The former returns the final transcoded string, and can be used
1061 to add a preamble and a postamble to document's body. It must
1062 accept two arguments: the transcoded string and the property list
1063 containing export options. A function associated to `template'
1064 will not be applied if export has option \"body-only\".
1065 A function associated to `inner-template' is always applied.
1067 The latter, when defined, is to be called on every text not
1068 recognized as an element or an object. It must accept two
1069 arguments: the text string and the information channel. It is an
1070 appropriate place to protect special chars relative to the
1073 BODY can start with pre-defined keyword arguments. The following
1074 keywords are understood:
1078 String, or list of strings, representing block names that
1079 will not be parsed. This is used to specify blocks that will
1080 contain raw code specific to the back-end. These blocks
1081 still have to be handled by the relative `export-block' type
1086 Alist between filters and function, or list of functions,
1087 specific to the back-end. See `org-export-filters-alist' for
1088 a list of all allowed filters. Filters defined here
1089 shouldn't make a back-end test, as it may prevent back-ends
1090 derived from this one to behave properly.
1094 Menu entry for the export dispatcher. It should be a list
1097 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1101 KEY is a free character selecting the back-end.
1103 DESCRIPTION-OR-ORDINAL is either a string or a number.
1105 If it is a string, is will be used to name the back-end in
1106 its menu entry. If it is a number, the following menu will
1107 be displayed as a sub-menu of the back-end with the same
1108 KEY. Also, the number will be used to determine in which
1109 order such sub-menus will appear (lowest first).
1111 ACTION-OR-MENU is either a function or an alist.
1113 If it is an action, it will be called with four
1114 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1115 BODY-ONLY. See `org-export-as' for further explanations on
1118 If it is an alist, associations should follow the
1121 '(KEY DESCRIPTION ACTION)
1123 where KEY, DESCRIPTION and ACTION are described above.
1125 Valid values include:
1127 '(?m \"My Special Back-end\" my-special-export-function)
1131 '(?l \"Export to LaTeX\"
1132 \(?p \"As PDF file\" org-latex-export-to-pdf)
1133 \(?o \"As PDF file and open\"
1135 \(if a (org-latex-export-to-pdf t s v b)
1137 \(org-latex-export-to-pdf nil s v b)))))))
1139 or the following, which will be added to the previous
1143 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1144 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1148 Alist between back-end specific properties introduced in
1149 communication channel and how their value are acquired. See
1150 `org-export-options-alist' for more information about
1151 structure of the values."
1152 (declare (indent 1))
1153 (let (blocks filters menu-entry options
)
1154 (while (keywordp (car body
))
1155 (let ((keyword (pop body
)))
1157 (:export-block
(let ((names (pop body
)))
1158 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1159 (list (upcase names
))))))
1160 (:filters-alist
(setq filters
(pop body
)))
1161 (:menu-entry
(setq menu-entry
(pop body
)))
1162 (:options-alist
(setq options
(pop body
)))
1163 (t (error "Unknown keyword: %s" keyword
)))))
1164 (org-export-register-backend
1165 (org-export-create-backend :name backend
1166 :transcoders transcoders
1170 :menu menu-entry
))))
1172 (defun org-export-define-derived-backend (child parent
&rest body
)
1173 "Create a new back-end as a variant of an existing one.
1175 CHILD is the name of the derived back-end. PARENT is the name of
1176 the parent back-end.
1178 BODY can start with pre-defined keyword arguments. The following
1179 keywords are understood:
1183 String, or list of strings, representing block names that
1184 will not be parsed. This is used to specify blocks that will
1185 contain raw code specific to the back-end. These blocks
1186 still have to be handled by the relative `export-block' type
1191 Alist of filters that will overwrite or complete filters
1192 defined in PARENT back-end. See `org-export-filters-alist'
1193 for a list of allowed filters.
1197 Menu entry for the export dispatcher. See
1198 `org-export-define-backend' for more information about the
1203 Alist of back-end specific properties that will overwrite or
1204 complete those defined in PARENT back-end. Refer to
1205 `org-export-options-alist' for more information about
1206 structure of the values.
1210 Alist of element and object types and transcoders that will
1211 overwrite or complete transcode table from PARENT back-end.
1212 Refer to `org-export-define-backend' for detailed information
1215 As an example, here is how one could define \"my-latex\" back-end
1216 as a variant of `latex' back-end with a custom template function:
1218 \(org-export-define-derived-backend 'my-latex 'latex
1219 :translate-alist '((template . my-latex-template-fun)))
1221 The back-end could then be called with, for example:
1223 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1224 (declare (indent 2))
1225 (let (blocks filters menu-entry options transcoders
)
1226 (while (keywordp (car body
))
1227 (let ((keyword (pop body
)))
1229 (:export-block
(let ((names (pop body
)))
1230 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1231 (list (upcase names
))))))
1232 (:filters-alist
(setq filters
(pop body
)))
1233 (:menu-entry
(setq menu-entry
(pop body
)))
1234 (:options-alist
(setq options
(pop body
)))
1235 (:translate-alist
(setq transcoders
(pop body
)))
1236 (t (error "Unknown keyword: %s" keyword
)))))
1237 (org-export-register-backend
1238 (org-export-create-backend :name child
1240 :transcoders transcoders
1244 :menu menu-entry
))))
1248 ;;; The Communication Channel
1250 ;; During export process, every function has access to a number of
1251 ;; properties. They are of two types:
1253 ;; 1. Environment options are collected once at the very beginning of
1254 ;; the process, out of the original buffer and configuration.
1255 ;; Collecting them is handled by `org-export-get-environment'
1258 ;; Most environment options are defined through the
1259 ;; `org-export-options-alist' variable.
1261 ;; 2. Tree properties are extracted directly from the parsed tree,
1262 ;; just before export, by `org-export-collect-tree-properties'.
1264 ;;;; Environment Options
1266 ;; Environment options encompass all parameters defined outside the
1267 ;; scope of the parsed data. They come from five sources, in
1268 ;; increasing precedence order:
1270 ;; - Global variables,
1271 ;; - Buffer's attributes,
1272 ;; - Options keyword symbols,
1273 ;; - Buffer keywords,
1274 ;; - Subtree properties.
1276 ;; The central internal function with regards to environment options
1277 ;; is `org-export-get-environment'. It updates global variables with
1278 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1279 ;; the different sources.
1281 ;; The internal functions doing the retrieval are:
1282 ;; `org-export--get-global-options',
1283 ;; `org-export--get-buffer-attributes',
1284 ;; `org-export--parse-option-keyword',
1285 ;; `org-export--get-subtree-options' and
1286 ;; `org-export--get-inbuffer-options'
1288 ;; Also, `org-export--list-bound-variables' collects bound variables
1289 ;; along with their value in order to set them as buffer local
1290 ;; variables later in the process.
1292 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1293 "Collect export options from the current buffer.
1295 Optional argument BACKEND is an export back-end, as returned by
1296 `org-export-create-backend'.
1298 When optional argument SUBTREEP is non-nil, assume the export is
1299 done against the current sub-tree.
1301 Third optional argument EXT-PLIST is a property list with
1302 external parameters overriding Org default settings, but still
1303 inferior to file-local settings."
1304 ;; First install #+BIND variables since these must be set before
1305 ;; global options are read.
1306 (dolist (pair (org-export--list-bound-variables))
1307 (org-set-local (car pair
) (nth 1 pair
)))
1308 ;; Get and prioritize export options...
1310 ;; ... from global variables...
1311 (org-export--get-global-options backend
)
1312 ;; ... from an external property list...
1314 ;; ... from in-buffer settings...
1315 (org-export--get-inbuffer-options backend
)
1316 ;; ... and from subtree, when appropriate.
1317 (and subtreep
(org-export--get-subtree-options backend
))
1318 ;; Eventually add misc. properties.
1322 :translate-alist
(org-export-get-all-transcoders backend
)
1324 ;; Collect id references.
1326 (org-with-wide-buffer
1327 (goto-char (point-min))
1328 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1329 (let ((link (org-element-context)))
1330 (when (eq (org-element-type link
) 'link
)
1331 (let* ((id (org-element-property :path link
))
1332 (file (car (org-id-find id
))))
1334 (push (cons id
(file-relative-name file
)) alist
)))))))
1337 (defun org-export--parse-option-keyword (options &optional backend
)
1338 "Parse an OPTIONS line and return values as a plist.
1339 Optional argument BACKEND is an export back-end, as returned by,
1340 e.g., `org-export-create-backend'. It specifies which back-end
1341 specific items to read, if any."
1344 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*" options s
)
1345 (setq s
(match-end 0))
1346 (push (cons (match-string 1 options
)
1347 (read (match-string 2 options
)))
1350 ;; Priority is given to back-end specific options.
1351 (all (append (and backend
(org-export-get-all-options backend
))
1352 org-export-options-alist
))
1355 (dolist (entry all plist
)
1356 (let ((item (nth 2 entry
)))
1358 (let ((v (assoc-string item line t
)))
1359 (when v
(setq plist
(plist-put plist
(car entry
) (cdr v
)))))))))))
1361 (defun org-export--get-subtree-options (&optional backend
)
1362 "Get export options in subtree at point.
1363 Optional argument BACKEND is an export back-end, as returned by,
1364 e.g., `org-export-create-backend'. It specifies back-end used
1365 for export. Return options as a plist."
1366 ;; For each buffer keyword, create a headline property setting the
1367 ;; same property in communication channel. The name for the
1368 ;; property is the keyword with "EXPORT_" appended to it.
1369 (org-with-wide-buffer
1370 ;; Make sure point is at a heading.
1371 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1373 ;; EXPORT_OPTIONS are parsed in a non-standard way. Take
1374 ;; care of them right from the start.
1375 (let ((o (org-entry-get (point) "EXPORT_OPTIONS" 'selective
)))
1376 (and o
(org-export--parse-option-keyword o backend
))))
1377 ;; Take care of EXPORT_TITLE. If it isn't defined, use
1378 ;; headline's title (with no todo keyword, priority cookie or
1379 ;; tag) as its fallback value.
1382 (or (org-entry-get (point) "EXPORT_TITLE" 'selective
)
1383 (progn (looking-at org-complex-heading-regexp
)
1384 (org-match-string-no-properties 4))))))
1385 ;; Look for both general keywords and back-end specific
1386 ;; options, with priority given to the latter.
1387 (options (append (and backend
(org-export-get-all-options backend
))
1388 org-export-options-alist
)))
1389 ;; Handle other keywords. Then return PLIST.
1390 (dolist (option options plist
)
1391 (let ((property (car option
))
1392 (keyword (nth 1 option
)))
1395 (or (cdr (assoc keyword cache
))
1396 (let ((v (org-entry-get (point)
1397 (concat "EXPORT_" keyword
)
1399 (push (cons keyword v
) cache
) v
))))
1404 (cl-case (nth 4 option
)
1406 (org-element-parse-secondary-string
1407 value
(org-element-restriction 'keyword
)))
1408 (split (org-split-string value
))
1409 (t value
))))))))))))
1411 (defun org-export--get-inbuffer-options (&optional backend
)
1412 "Return current buffer export options, as a plist.
1414 Optional argument BACKEND, when non-nil, is an export back-end,
1415 as returned by, e.g., `org-export-create-backend'. It specifies
1416 which back-end specific options should also be read in the
1419 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1420 (let* ((case-fold-search t
)
1422 ;; Priority is given to back-end specific options.
1423 (and backend
(org-export-get-all-options backend
))
1424 org-export-options-alist
))
1425 (regexp (format "^[ \t]*#\\+%s:"
1426 (regexp-opt (nconc (delq nil
(mapcar #'cadr options
))
1427 org-export-special-keywords
))))
1429 (letrec ((find-properties
1431 ;; Return all properties associated to KEYWORD.
1433 (dolist (option options properties
)
1434 (when (equal (nth 1 option
) keyword
)
1435 (cl-pushnew (car option
) properties
))))))
1437 (lambda (&optional files
)
1438 ;; Recursively read keywords in buffer. FILES is
1439 ;; a list of files read so far. PLIST is the current
1440 ;; property list obtained.
1441 (org-with-wide-buffer
1442 (goto-char (point-min))
1443 (while (re-search-forward regexp nil t
)
1444 (let ((element (org-element-at-point)))
1445 (when (eq (org-element-type element
) 'keyword
)
1446 (let ((key (org-element-property :key element
))
1447 (val (org-element-property :value element
)))
1449 ;; Options in `org-export-special-keywords'.
1450 ((equal key
"SETUPFILE")
1453 (org-remove-double-quotes (org-trim val
)))))
1454 ;; Avoid circular dependencies.
1455 (unless (member file files
)
1457 (insert (org-file-contents file
'noerror
))
1458 (let ((org-inhibit-startup t
)) (org-mode))
1459 (funcall get-options
(cons file files
))))))
1460 ((equal key
"OPTIONS")
1464 (org-export--parse-option-keyword
1466 ((equal key
"FILETAGS")
1473 (org-split-string val
":")
1474 (plist-get plist
:filetags
)))))))
1476 ;; Options in `org-export-options-alist'.
1477 (dolist (property (funcall find-properties key
))
1482 ;; Handle value depending on specified
1484 (cl-case (nth 4 (assq property options
))
1486 (unless (memq property to-parse
)
1487 (push property to-parse
))
1488 ;; Even if `parse' implies `space'
1489 ;; behavior, we separate line with
1490 ;; "\n" so as to preserve
1491 ;; line-breaks. However, empty
1492 ;; lines are forbidden since `parse'
1493 ;; doesn't allow more than one
1495 (let ((old (plist-get plist property
)))
1496 (cond ((not (org-string-nw-p val
)) old
)
1497 (old (concat old
"\n" val
))
1500 (if (not (plist-get plist property
))
1502 (concat (plist-get plist property
)
1507 (concat (plist-get plist property
)
1510 (split `(,@(plist-get plist property
)
1511 ,@(org-split-string val
)))
1514 (if (not (plist-member plist property
)) val
1515 (plist-get plist property
)))))))))))))))))
1516 ;; Read options in the current buffer and return value.
1517 (funcall get-options
(and buffer-file-name
(list buffer-file-name
)))
1518 ;; Parse properties in TO-PARSE. Remove newline characters not
1519 ;; involved in line breaks to simulate `space' behavior.
1520 ;; Finally return options.
1521 (dolist (p to-parse plist
)
1522 (let ((value (org-element-parse-secondary-string
1524 (org-element-restriction 'keyword
))))
1525 (org-element-map value
'plain-text
1527 (org-element-set-element
1528 s
(replace-regexp-in-string "\n" " " s
))))
1529 (setq plist
(plist-put plist p value
)))))))
1531 (defun org-export--get-buffer-attributes ()
1532 "Return properties related to buffer attributes, as a plist."
1533 (list :input-buffer
(buffer-name (buffer-base-buffer))
1534 :input-file
(buffer-file-name (buffer-base-buffer))))
1536 (defun org-export--get-global-options (&optional backend
)
1537 "Return global export options as a plist.
1538 Optional argument BACKEND, if non-nil, is an export back-end, as
1539 returned by, e.g., `org-export-create-backend'. It specifies
1540 which back-end specific export options should also be read in the
1543 ;; Priority is given to back-end specific options.
1544 (all (append (and backend
(org-export-get-all-options backend
))
1545 org-export-options-alist
)))
1546 (dolist (cell all plist
)
1547 (let ((prop (car cell
)))
1548 (unless (plist-member plist prop
)
1553 ;; Evaluate default value provided.
1554 (let ((value (eval (nth 3 cell
))))
1555 (if (eq (nth 4 cell
) 'parse
)
1556 (org-element-parse-secondary-string
1557 value
(org-element-restriction 'keyword
))
1560 (defun org-export--list-bound-variables ()
1561 "Return variables bound from BIND keywords in current buffer.
1562 Also look for BIND keywords in setup files. The return value is
1563 an alist where associations are (VARIABLE-NAME VALUE)."
1564 (when org-export-allow-bind-keywords
1565 (letrec ((collect-bind
1566 (lambda (files alist
)
1567 ;; Return an alist between variable names and their
1568 ;; value. FILES is a list of setup files names read
1569 ;; so far, used to avoid circular dependencies. ALIST
1570 ;; is the alist collected so far.
1571 (let ((case-fold-search t
))
1572 (org-with-wide-buffer
1573 (goto-char (point-min))
1574 (while (re-search-forward
1575 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1576 (let ((element (org-element-at-point)))
1577 (when (eq (org-element-type element
) 'keyword
)
1578 (let ((val (org-element-property :value element
)))
1579 (if (equal (org-element-property :key element
)
1581 (push (read (format "(%s)" val
)) alist
)
1582 ;; Enter setup file.
1583 (let ((file (expand-file-name
1584 (org-remove-double-quotes val
))))
1585 (unless (member file files
)
1587 (let ((org-inhibit-startup t
)) (org-mode))
1588 (insert (org-file-contents file
'noerror
))
1590 (funcall collect-bind
1594 ;; Return value in appropriate order of appearance.
1595 (nreverse (funcall collect-bind nil nil
)))))
1597 ;; defsubst org-export-get-parent must be defined before first use,
1598 ;; was originally defined in the topology section
1600 (defsubst org-export-get-parent
(blob)
1601 "Return BLOB parent or nil.
1602 BLOB is the element or object considered."
1603 (org-element-property :parent blob
))
1605 ;;;; Tree Properties
1607 ;; Tree properties are information extracted from parse tree. They
1608 ;; are initialized at the beginning of the transcoding process by
1609 ;; `org-export-collect-tree-properties'.
1611 ;; Dedicated functions focus on computing the value of specific tree
1612 ;; properties during initialization. Thus,
1613 ;; `org-export--populate-ignore-list' lists elements and objects that
1614 ;; should be skipped during export, `org-export--get-min-level' gets
1615 ;; the minimal exportable level, used as a basis to compute relative
1616 ;; level for headlines. Eventually
1617 ;; `org-export--collect-headline-numbering' builds an alist between
1618 ;; headlines and their numbering.
1620 (defun org-export-collect-tree-properties (data info
)
1621 "Extract tree properties from parse tree.
1623 DATA is the parse tree from which information is retrieved. INFO
1624 is a list holding export options.
1626 Following tree properties are set or updated:
1628 `:exported-data' Hash table used to memoize results from
1631 `:headline-offset' Offset between true level of headlines and
1632 local level. An offset of -1 means a headline
1633 of level 2 should be considered as a level
1634 1 headline in the context.
1636 `:headline-numbering' Alist of all headlines as key an the
1637 associated numbering as value.
1639 Return updated plist."
1640 ;; Install the parse tree in the communication channel.
1641 (setq info
(plist-put info
:parse-tree data
))
1642 ;; Compute `:headline-offset' in order to be able to use
1643 ;; `org-export-get-relative-level'.
1647 (- 1 (org-export--get-min-level data info
))))
1648 ;; Properties order doesn't matter: get the rest of the tree
1651 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1652 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1655 (defun org-export--get-min-level (data options
)
1656 "Return minimum exportable headline's level in DATA.
1657 DATA is parsed tree as returned by `org-element-parse-buffer'.
1658 OPTIONS is a plist holding export options."
1660 (let ((min-level 10000))
1663 (when (and (eq (org-element-type blob
) 'headline
)
1664 (not (org-element-property :footnote-section-p blob
))
1665 (not (memq blob
(plist-get options
:ignore-list
))))
1666 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1667 (when (= min-level
1) (throw 'exit
1)))
1668 (org-element-contents data
))
1669 ;; If no headline was found, for the sake of consistency, set
1670 ;; minimum level to 1 nonetheless.
1671 (if (= min-level
10000) 1 min-level
))))
1673 (defun org-export--collect-headline-numbering (data options
)
1674 "Return numbering of all exportable, numbered headlines in a parse tree.
1676 DATA is the parse tree. OPTIONS is the plist holding export
1679 Return an alist whose key is a headline and value is its
1680 associated numbering \(in the shape of a list of numbers\) or nil
1681 for a footnotes section."
1682 (let ((numbering (make-vector org-export-max-depth
0)))
1683 (org-element-map data
'headline
1685 (when (and (org-export-numbered-headline-p headline options
)
1686 (not (org-element-property :footnote-section-p headline
)))
1687 (let ((relative-level
1688 (1- (org-export-get-relative-level headline options
))))
1692 for n across numbering
1693 for idx from
0 to org-export-max-depth
1694 when
(< idx relative-level
) collect n
1695 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1696 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1699 (defun org-export--selected-trees (data info
)
1700 "List headlines and inlinetasks with a select tag in their tree.
1701 DATA is parsed data as returned by `org-element-parse-buffer'.
1702 INFO is a plist holding export options."
1703 (letrec ((selected-trees)
1705 (lambda (data genealogy
)
1706 (let ((type (org-element-type data
)))
1708 ((memq type
'(headline inlinetask
))
1709 (let ((tags (org-element-property :tags data
)))
1710 (if (cl-loop for tag in
(plist-get info
:select-tags
)
1711 thereis
(member tag tags
))
1712 ;; When a select tag is found, mark full
1713 ;; genealogy and every headline within the
1714 ;; tree as acceptable.
1715 (setq selected-trees
1718 (org-element-map data
'(headline inlinetask
)
1721 ;; If at a headline, continue searching in tree,
1723 (when (eq type
'headline
)
1724 (dolist (el (org-element-contents data
))
1725 (funcall walk-data el
(cons data genealogy
)))))))
1726 ((or (eq type
'org-data
)
1727 (memq type org-element-greater-elements
))
1728 (dolist (el (org-element-contents data
))
1729 (funcall walk-data el genealogy
))))))))
1730 (funcall walk-data data nil
)
1733 (defun org-export--skip-p (blob options selected
)
1734 "Non-nil when element or object BLOB should be skipped during export.
1735 OPTIONS is the plist holding export options. SELECTED, when
1736 non-nil, is a list of headlines or inlinetasks belonging to
1737 a tree with a select tag."
1738 (cl-case (org-element-type blob
)
1739 (clock (not (plist-get options
:with-clocks
)))
1741 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1742 (or (not with-drawers-p
)
1743 (and (consp with-drawers-p
)
1744 ;; If `:with-drawers' value starts with `not', ignore
1745 ;; every drawer whose name belong to that list.
1746 ;; Otherwise, ignore drawers whose name isn't in that
1748 (let ((name (org-element-property :drawer-name blob
)))
1749 (if (eq (car with-drawers-p
) 'not
)
1750 (member-ignore-case name
(cdr with-drawers-p
))
1751 (not (member-ignore-case name with-drawers-p
))))))))
1752 (fixed-width (not (plist-get options
:with-fixed-width
)))
1753 ((footnote-definition footnote-reference
)
1754 (not (plist-get options
:with-footnotes
)))
1755 ((headline inlinetask
)
1756 (let ((with-tasks (plist-get options
:with-tasks
))
1757 (todo (org-element-property :todo-keyword blob
))
1758 (todo-type (org-element-property :todo-type blob
))
1759 (archived (plist-get options
:with-archived-trees
))
1760 (tags (org-element-property :tags blob
)))
1762 (and (eq (org-element-type blob
) 'inlinetask
)
1763 (not (plist-get options
:with-inlinetasks
)))
1764 ;; Ignore subtrees with an exclude tag.
1765 (cl-loop for k in
(plist-get options
:exclude-tags
)
1766 thereis
(member k tags
))
1767 ;; When a select tag is present in the buffer, ignore any tree
1769 (and selected
(not (memq blob selected
)))
1770 ;; Ignore commented sub-trees.
1771 (org-element-property :commentedp blob
)
1772 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1773 (and (not archived
) (org-element-property :archivedp blob
))
1774 ;; Ignore tasks, if specified by `:with-tasks' property.
1776 (or (not with-tasks
)
1777 (and (memq with-tasks
'(todo done
))
1778 (not (eq todo-type with-tasks
)))
1779 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1780 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1782 (let ((properties-set (plist-get options
:with-properties
)))
1783 (cond ((null properties-set
) t
)
1784 ((consp properties-set
)
1785 (not (member-ignore-case (org-element-property :key blob
)
1786 properties-set
))))))
1787 (planning (not (plist-get options
:with-planning
)))
1788 (property-drawer (not (plist-get options
:with-properties
)))
1789 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1790 (table (not (plist-get options
:with-tables
)))
1792 (and (org-export-table-has-special-column-p
1793 (org-export-get-parent-table blob
))
1794 (org-export-first-sibling-p blob options
)))
1795 (table-row (org-export-table-row-is-special-p blob options
))
1797 ;; `:with-timestamps' only applies to isolated timestamps
1798 ;; objects, i.e. timestamp objects in a paragraph containing only
1799 ;; timestamps and whitespaces.
1800 (when (let ((parent (org-export-get-parent-element blob
)))
1801 (and (memq (org-element-type parent
) '(paragraph verse-block
))
1802 (not (org-element-map parent
1804 (remq 'timestamp org-element-all-objects
))
1806 (or (not (stringp obj
)) (org-string-nw-p obj
)))
1808 (cl-case (plist-get options
:with-timestamps
)
1811 (not (memq (org-element-property :type blob
) '(active active-range
))))
1813 (not (memq (org-element-property :type blob
)
1814 '(inactive inactive-range
)))))))))
1819 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1820 ;; `org-element-parse-buffer') and transcodes it into a specified
1821 ;; back-end output. It takes care of filtering out elements or
1822 ;; objects according to export options and organizing the output blank
1823 ;; lines and white space are preserved. The function memoizes its
1824 ;; results, so it is cheap to call it within transcoders.
1826 ;; It is possible to modify locally the back-end used by
1827 ;; `org-export-data' or even use a temporary back-end by using
1828 ;; `org-export-data-with-backend'.
1830 ;; `org-export-transcoder' is an accessor returning appropriate
1831 ;; translator function for a given element or object.
1833 (defun org-export-transcoder (blob info
)
1834 "Return appropriate transcoder for BLOB.
1835 INFO is a plist containing export directives."
1836 (let ((type (org-element-type blob
)))
1837 ;; Return contents only for complete parse trees.
1838 (if (eq type
'org-data
) (lambda (_datum contents _info
) contents
)
1839 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1840 (and (functionp transcoder
) transcoder
)))))
1842 (defun org-export-data (data info
)
1843 "Convert DATA into current back-end format.
1845 DATA is a parse tree, an element or an object or a secondary
1846 string. INFO is a plist holding export options.
1849 (or (gethash data
(plist-get info
:exported-data
))
1850 (let* ((type (org-element-type data
))
1853 ;; Ignored element/object.
1854 ((memq data
(plist-get info
:ignore-list
)) nil
)
1856 ((eq type
'plain-text
)
1857 (org-export-filter-apply-functions
1858 (plist-get info
:filter-plain-text
)
1859 (let ((transcoder (org-export-transcoder data info
)))
1860 (if transcoder
(funcall transcoder data info
) data
))
1862 ;; Secondary string.
1864 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1865 ;; Element/Object without contents or, as a special
1866 ;; case, headline with archive tag and archived trees
1867 ;; restricted to title only.
1868 ((or (not (org-element-contents data
))
1869 (and (eq type
'headline
)
1870 (eq (plist-get info
:with-archived-trees
) 'headline
)
1871 (org-element-property :archivedp data
)))
1872 (let ((transcoder (org-export-transcoder data info
)))
1873 (or (and (functionp transcoder
)
1874 (funcall transcoder data nil info
))
1875 ;; Export snippets never return a nil value so
1876 ;; that white spaces following them are never
1878 (and (eq type
'export-snippet
) ""))))
1879 ;; Element/Object with contents.
1881 (let ((transcoder (org-export-transcoder data info
)))
1883 (let* ((greaterp (memq type org-element-greater-elements
))
1886 (memq type org-element-recursive-objects
)))
1889 (lambda (element) (org-export-data element info
))
1890 (org-element-contents
1891 (if (or greaterp objectp
) data
1892 ;; Elements directly containing
1893 ;; objects must have their indentation
1894 ;; normalized first.
1895 (org-element-normalize-contents
1897 ;; When normalizing contents of the
1898 ;; first paragraph in an item or
1899 ;; a footnote definition, ignore
1900 ;; first line's indentation: there is
1901 ;; none and it might be misleading.
1902 (when (eq type
'paragraph
)
1903 (let ((parent (org-export-get-parent data
)))
1905 (eq (car (org-element-contents parent
))
1907 (memq (org-element-type parent
)
1908 '(footnote-definition item
))))))))
1910 (funcall transcoder data
1911 (if (not greaterp
) contents
1912 (org-element-normalize-string contents
))
1914 ;; Final result will be memoized before being returned.
1919 ((memq type
'(org-data plain-text nil
)) results
)
1920 ;; Append the same white space between elements or objects
1921 ;; as in the original buffer, and call appropriate filters.
1924 (org-export-filter-apply-functions
1925 (plist-get info
(intern (format ":filter-%s" type
)))
1926 (let ((post-blank (or (org-element-property :post-blank data
)
1928 (if (memq type org-element-all-elements
)
1929 (concat (org-element-normalize-string results
)
1930 (make-string post-blank ?
\n))
1931 (concat results
(make-string post-blank ?\s
))))
1934 (plist-get info
:exported-data
)))))
1936 (defun org-export-data-with-backend (data backend info
)
1937 "Convert DATA into BACKEND format.
1939 DATA is an element, an object, a secondary string or a string.
1940 BACKEND is a symbol. INFO is a plist used as a communication
1943 Unlike to `org-export-with-backend', this function will
1944 recursively convert DATA using BACKEND translation table."
1945 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1948 ;; Set-up a new communication channel with translations defined in
1949 ;; BACKEND as the translate table and a new hash table for
1953 (list :back-end backend
1954 :translate-alist
(org-export-get-all-transcoders backend
)
1955 ;; Size of the hash table is reduced since this function
1956 ;; will probably be used on small trees.
1957 :exported-data
(make-hash-table :test
'eq
:size
401)))))
1959 (defun org-export-expand (blob contents
&optional with-affiliated
)
1960 "Expand a parsed element or object to its original state.
1962 BLOB is either an element or an object. CONTENTS is its
1963 contents, as a string or nil.
1965 When optional argument WITH-AFFILIATED is non-nil, add affiliated
1966 keywords before output."
1967 (let ((type (org-element-type blob
)))
1968 (concat (and with-affiliated
(memq type org-element-all-elements
)
1969 (org-element--interpret-affiliated-keywords blob
))
1970 (funcall (intern (format "org-element-%s-interpreter" type
))
1975 ;;; The Filter System
1977 ;; Filters allow end-users to tweak easily the transcoded output.
1978 ;; They are the functional counterpart of hooks, as every filter in
1979 ;; a set is applied to the return value of the previous one.
1981 ;; Every set is back-end agnostic. Although, a filter is always
1982 ;; called, in addition to the string it applies to, with the back-end
1983 ;; used as argument, so it's easy for the end-user to add back-end
1984 ;; specific filters in the set. The communication channel, as
1985 ;; a plist, is required as the third argument.
1987 ;; From the developer side, filters sets can be installed in the
1988 ;; process with the help of `org-export-define-backend', which
1989 ;; internally stores filters as an alist. Each association has a key
1990 ;; among the following symbols and a function or a list of functions
1993 ;; - `:filter-options' applies to the property list containing export
1994 ;; options. Unlike to other filters, functions in this list accept
1995 ;; two arguments instead of three: the property list containing
1996 ;; export options and the back-end. Users can set its value through
1997 ;; `org-export-filter-options-functions' variable.
1999 ;; - `:filter-parse-tree' applies directly to the complete parsed
2000 ;; tree. Users can set it through
2001 ;; `org-export-filter-parse-tree-functions' variable.
2003 ;; - `:filter-body' applies to the body of the output, before template
2004 ;; translator chimes in. Users can set it through
2005 ;; `org-export-filter-body-functions' variable.
2007 ;; - `:filter-final-output' applies to the final transcoded string.
2008 ;; Users can set it with `org-export-filter-final-output-functions'
2011 ;; - `:filter-plain-text' applies to any string not recognized as Org
2012 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2015 ;; - `:filter-TYPE' applies on the string returned after an element or
2016 ;; object of type TYPE has been transcoded. A user can modify
2017 ;; `org-export-filter-TYPE-functions' to install these filters.
2019 ;; All filters sets are applied with
2020 ;; `org-export-filter-apply-functions' function. Filters in a set are
2021 ;; applied in a LIFO fashion. It allows developers to be sure that
2022 ;; their filters will be applied first.
2024 ;; Filters properties are installed in communication channel with
2025 ;; `org-export-install-filters' function.
2027 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2028 ;; `org-export-before-parsing-hook') are run at the beginning of the
2029 ;; export process and just before parsing to allow for heavy structure
2035 (defvar org-export-before-processing-hook nil
2036 "Hook run at the beginning of the export process.
2038 This is run before include keywords and macros are expanded and
2039 Babel code blocks executed, on a copy of the original buffer
2040 being exported. Visibility and narrowing are preserved. Point
2041 is at the beginning of the buffer.
2043 Every function in this hook will be called with one argument: the
2044 back-end currently used, as a symbol.")
2046 (defvar org-export-before-parsing-hook nil
2047 "Hook run before parsing an export buffer.
2049 This is run after include keywords and macros have been expanded
2050 and Babel code blocks executed, on a copy of the original buffer
2051 being exported. Visibility and narrowing are preserved. Point
2052 is at the beginning of the buffer.
2054 Every function in this hook will be called with one argument: the
2055 back-end currently used, as a symbol.")
2058 ;;;; Special Filters
2060 (defvar org-export-filter-options-functions nil
2061 "List of functions applied to the export options.
2062 Each filter is called with two arguments: the export options, as
2063 a plist, and the back-end, as a symbol. It must return
2064 a property list containing export options.")
2066 (defvar org-export-filter-parse-tree-functions nil
2067 "List of functions applied to the parsed tree.
2068 Each filter is called with three arguments: the parse tree, as
2069 returned by `org-element-parse-buffer', the back-end, as
2070 a symbol, and the communication channel, as a plist. It must
2071 return the modified parse tree to transcode.")
2073 (defvar org-export-filter-plain-text-functions nil
2074 "List of functions applied to plain text.
2075 Each filter is called with three arguments: a string which
2076 contains no Org syntax, the back-end, as a symbol, and the
2077 communication channel, as a plist. It must return a string or
2080 (defvar org-export-filter-body-functions nil
2081 "List of functions applied to transcoded body.
2082 Each filter is called with three arguments: a string which
2083 contains no Org syntax, the back-end, as a symbol, and the
2084 communication channel, as a plist. It must return a string or
2087 (defvar org-export-filter-final-output-functions nil
2088 "List of functions applied to the transcoded string.
2089 Each filter is called with three arguments: the full transcoded
2090 string, the back-end, as a symbol, and the communication channel,
2091 as a plist. It must return a string that will be used as the
2092 final export output.")
2095 ;;;; Elements Filters
2097 (defvar org-export-filter-babel-call-functions nil
2098 "List of functions applied to a transcoded babel-call.
2099 Each filter is called with three arguments: the transcoded data,
2100 as a string, the back-end, as a symbol, and the communication
2101 channel, as a plist. It must return a string or nil.")
2103 (defvar org-export-filter-center-block-functions nil
2104 "List of functions applied to a transcoded center block.
2105 Each filter is called with three arguments: the transcoded data,
2106 as a string, the back-end, as a symbol, and the communication
2107 channel, as a plist. It must return a string or nil.")
2109 (defvar org-export-filter-clock-functions nil
2110 "List of functions applied to a transcoded clock.
2111 Each filter is called with three arguments: the transcoded data,
2112 as a string, the back-end, as a symbol, and the communication
2113 channel, as a plist. It must return a string or nil.")
2115 (defvar org-export-filter-diary-sexp-functions nil
2116 "List of functions applied to a transcoded diary-sexp.
2117 Each filter is called with three arguments: the transcoded data,
2118 as a string, the back-end, as a symbol, and the communication
2119 channel, as a plist. It must return a string or nil.")
2121 (defvar org-export-filter-drawer-functions nil
2122 "List of functions applied to a transcoded drawer.
2123 Each filter is called with three arguments: the transcoded data,
2124 as a string, the back-end, as a symbol, and the communication
2125 channel, as a plist. It must return a string or nil.")
2127 (defvar org-export-filter-dynamic-block-functions nil
2128 "List of functions applied to a transcoded dynamic-block.
2129 Each filter is called with three arguments: the transcoded data,
2130 as a string, the back-end, as a symbol, and the communication
2131 channel, as a plist. It must return a string or nil.")
2133 (defvar org-export-filter-example-block-functions nil
2134 "List of functions applied to a transcoded example-block.
2135 Each filter is called with three arguments: the transcoded data,
2136 as a string, the back-end, as a symbol, and the communication
2137 channel, as a plist. It must return a string or nil.")
2139 (defvar org-export-filter-export-block-functions nil
2140 "List of functions applied to a transcoded export-block.
2141 Each filter is called with three arguments: the transcoded data,
2142 as a string, the back-end, as a symbol, and the communication
2143 channel, as a plist. It must return a string or nil.")
2145 (defvar org-export-filter-fixed-width-functions nil
2146 "List of functions applied to a transcoded fixed-width.
2147 Each filter is called with three arguments: the transcoded data,
2148 as a string, the back-end, as a symbol, and the communication
2149 channel, as a plist. It must return a string or nil.")
2151 (defvar org-export-filter-footnote-definition-functions nil
2152 "List of functions applied to a transcoded footnote-definition.
2153 Each filter is called with three arguments: the transcoded data,
2154 as a string, the back-end, as a symbol, and the communication
2155 channel, as a plist. It must return a string or nil.")
2157 (defvar org-export-filter-headline-functions nil
2158 "List of functions applied to a transcoded headline.
2159 Each filter is called with three arguments: the transcoded data,
2160 as a string, the back-end, as a symbol, and the communication
2161 channel, as a plist. It must return a string or nil.")
2163 (defvar org-export-filter-horizontal-rule-functions nil
2164 "List of functions applied to a transcoded horizontal-rule.
2165 Each filter is called with three arguments: the transcoded data,
2166 as a string, the back-end, as a symbol, and the communication
2167 channel, as a plist. It must return a string or nil.")
2169 (defvar org-export-filter-inlinetask-functions nil
2170 "List of functions applied to a transcoded inlinetask.
2171 Each filter is called with three arguments: the transcoded data,
2172 as a string, the back-end, as a symbol, and the communication
2173 channel, as a plist. It must return a string or nil.")
2175 (defvar org-export-filter-item-functions nil
2176 "List of functions applied to a transcoded item.
2177 Each filter is called with three arguments: the transcoded data,
2178 as a string, the back-end, as a symbol, and the communication
2179 channel, as a plist. It must return a string or nil.")
2181 (defvar org-export-filter-keyword-functions nil
2182 "List of functions applied to a transcoded keyword.
2183 Each filter is called with three arguments: the transcoded data,
2184 as a string, the back-end, as a symbol, and the communication
2185 channel, as a plist. It must return a string or nil.")
2187 (defvar org-export-filter-latex-environment-functions nil
2188 "List of functions applied to a transcoded latex-environment.
2189 Each filter is called with three arguments: the transcoded data,
2190 as a string, the back-end, as a symbol, and the communication
2191 channel, as a plist. It must return a string or nil.")
2193 (defvar org-export-filter-node-property-functions nil
2194 "List of functions applied to a transcoded node-property.
2195 Each filter is called with three arguments: the transcoded data,
2196 as a string, the back-end, as a symbol, and the communication
2197 channel, as a plist. It must return a string or nil.")
2199 (defvar org-export-filter-paragraph-functions nil
2200 "List of functions applied to a transcoded paragraph.
2201 Each filter is called with three arguments: the transcoded data,
2202 as a string, the back-end, as a symbol, and the communication
2203 channel, as a plist. It must return a string or nil.")
2205 (defvar org-export-filter-plain-list-functions nil
2206 "List of functions applied to a transcoded plain-list.
2207 Each filter is called with three arguments: the transcoded data,
2208 as a string, the back-end, as a symbol, and the communication
2209 channel, as a plist. It must return a string or nil.")
2211 (defvar org-export-filter-planning-functions nil
2212 "List of functions applied to a transcoded planning.
2213 Each filter is called with three arguments: the transcoded data,
2214 as a string, the back-end, as a symbol, and the communication
2215 channel, as a plist. It must return a string or nil.")
2217 (defvar org-export-filter-property-drawer-functions nil
2218 "List of functions applied to a transcoded property-drawer.
2219 Each filter is called with three arguments: the transcoded data,
2220 as a string, the back-end, as a symbol, and the communication
2221 channel, as a plist. It must return a string or nil.")
2223 (defvar org-export-filter-quote-block-functions nil
2224 "List of functions applied to a transcoded quote block.
2225 Each filter is called with three arguments: the transcoded quote
2226 data, as a string, the back-end, as a symbol, and the
2227 communication channel, as a plist. It must return a string or
2230 (defvar org-export-filter-section-functions nil
2231 "List of functions applied to a transcoded section.
2232 Each filter is called with three arguments: the transcoded data,
2233 as a string, the back-end, as a symbol, and the communication
2234 channel, as a plist. It must return a string or nil.")
2236 (defvar org-export-filter-special-block-functions nil
2237 "List of functions applied to a transcoded special block.
2238 Each filter is called with three arguments: the transcoded data,
2239 as a string, the back-end, as a symbol, and the communication
2240 channel, as a plist. It must return a string or nil.")
2242 (defvar org-export-filter-src-block-functions nil
2243 "List of functions applied to a transcoded src-block.
2244 Each filter is called with three arguments: the transcoded data,
2245 as a string, the back-end, as a symbol, and the communication
2246 channel, as a plist. It must return a string or nil.")
2248 (defvar org-export-filter-table-functions nil
2249 "List of functions applied to a transcoded table.
2250 Each filter is called with three arguments: the transcoded data,
2251 as a string, the back-end, as a symbol, and the communication
2252 channel, as a plist. It must return a string or nil.")
2254 (defvar org-export-filter-table-cell-functions nil
2255 "List of functions applied to a transcoded table-cell.
2256 Each filter is called with three arguments: the transcoded data,
2257 as a string, the back-end, as a symbol, and the communication
2258 channel, as a plist. It must return a string or nil.")
2260 (defvar org-export-filter-table-row-functions nil
2261 "List of functions applied to a transcoded table-row.
2262 Each filter is called with three arguments: the transcoded data,
2263 as a string, the back-end, as a symbol, and the communication
2264 channel, as a plist. It must return a string or nil.")
2266 (defvar org-export-filter-verse-block-functions nil
2267 "List of functions applied to a transcoded verse block.
2268 Each filter is called with three arguments: the transcoded data,
2269 as a string, the back-end, as a symbol, and the communication
2270 channel, as a plist. It must return a string or nil.")
2273 ;;;; Objects Filters
2275 (defvar org-export-filter-bold-functions nil
2276 "List of functions applied to transcoded bold text.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-code-functions nil
2282 "List of functions applied to transcoded code text.
2283 Each filter is called with three arguments: the transcoded data,
2284 as a string, the back-end, as a symbol, and the communication
2285 channel, as a plist. It must return a string or nil.")
2287 (defvar org-export-filter-entity-functions nil
2288 "List of functions applied to a transcoded entity.
2289 Each filter is called with three arguments: the transcoded data,
2290 as a string, the back-end, as a symbol, and the communication
2291 channel, as a plist. It must return a string or nil.")
2293 (defvar org-export-filter-export-snippet-functions nil
2294 "List of functions applied to a transcoded export-snippet.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2299 (defvar org-export-filter-footnote-reference-functions nil
2300 "List of functions applied to a transcoded footnote-reference.
2301 Each filter is called with three arguments: the transcoded data,
2302 as a string, the back-end, as a symbol, and the communication
2303 channel, as a plist. It must return a string or nil.")
2305 (defvar org-export-filter-inline-babel-call-functions nil
2306 "List of functions applied to a transcoded inline-babel-call.
2307 Each filter is called with three arguments: the transcoded data,
2308 as a string, the back-end, as a symbol, and the communication
2309 channel, as a plist. It must return a string or nil.")
2311 (defvar org-export-filter-inline-src-block-functions nil
2312 "List of functions applied to a transcoded inline-src-block.
2313 Each filter is called with three arguments: the transcoded data,
2314 as a string, the back-end, as a symbol, and the communication
2315 channel, as a plist. It must return a string or nil.")
2317 (defvar org-export-filter-italic-functions nil
2318 "List of functions applied to transcoded italic text.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-latex-fragment-functions nil
2324 "List of functions applied to a transcoded latex-fragment.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-line-break-functions nil
2330 "List of functions applied to a transcoded line-break.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-link-functions nil
2336 "List of functions applied to a transcoded link.
2337 Each filter is called with three arguments: the transcoded data,
2338 as a string, the back-end, as a symbol, and the communication
2339 channel, as a plist. It must return a string or nil.")
2341 (defvar org-export-filter-radio-target-functions nil
2342 "List of functions applied to a transcoded radio-target.
2343 Each filter is called with three arguments: the transcoded data,
2344 as a string, the back-end, as a symbol, and the communication
2345 channel, as a plist. It must return a string or nil.")
2347 (defvar org-export-filter-statistics-cookie-functions nil
2348 "List of functions applied to a transcoded statistics-cookie.
2349 Each filter is called with three arguments: the transcoded data,
2350 as a string, the back-end, as a symbol, and the communication
2351 channel, as a plist. It must return a string or nil.")
2353 (defvar org-export-filter-strike-through-functions nil
2354 "List of functions applied to transcoded strike-through text.
2355 Each filter is called with three arguments: the transcoded data,
2356 as a string, the back-end, as a symbol, and the communication
2357 channel, as a plist. It must return a string or nil.")
2359 (defvar org-export-filter-subscript-functions nil
2360 "List of functions applied to a transcoded subscript.
2361 Each filter is called with three arguments: the transcoded data,
2362 as a string, the back-end, as a symbol, and the communication
2363 channel, as a plist. It must return a string or nil.")
2365 (defvar org-export-filter-superscript-functions nil
2366 "List of functions applied to a transcoded superscript.
2367 Each filter is called with three arguments: the transcoded data,
2368 as a string, the back-end, as a symbol, and the communication
2369 channel, as a plist. It must return a string or nil.")
2371 (defvar org-export-filter-target-functions nil
2372 "List of functions applied to a transcoded target.
2373 Each filter is called with three arguments: the transcoded data,
2374 as a string, the back-end, as a symbol, and the communication
2375 channel, as a plist. It must return a string or nil.")
2377 (defvar org-export-filter-timestamp-functions nil
2378 "List of functions applied to a transcoded timestamp.
2379 Each filter is called with three arguments: the transcoded data,
2380 as a string, the back-end, as a symbol, and the communication
2381 channel, as a plist. It must return a string or nil.")
2383 (defvar org-export-filter-underline-functions nil
2384 "List of functions applied to transcoded underline text.
2385 Each filter is called with three arguments: the transcoded data,
2386 as a string, the back-end, as a symbol, and the communication
2387 channel, as a plist. It must return a string or nil.")
2389 (defvar org-export-filter-verbatim-functions nil
2390 "List of functions applied to transcoded verbatim text.
2391 Each filter is called with three arguments: the transcoded data,
2392 as a string, the back-end, as a symbol, and the communication
2393 channel, as a plist. It must return a string or nil.")
2398 ;; Internal function `org-export-install-filters' installs filters
2399 ;; hard-coded in back-ends (developer filters) and filters from global
2400 ;; variables (user filters) in the communication channel.
2402 ;; Internal function `org-export-filter-apply-functions' takes care
2403 ;; about applying each filter in order to a given data. It ignores
2404 ;; filters returning a nil value but stops whenever a filter returns
2407 (defun org-export-filter-apply-functions (filters value info
)
2408 "Call every function in FILTERS.
2410 Functions are called with arguments VALUE, current export
2411 back-end's name and INFO. A function returning a nil value will
2412 be skipped. If it returns the empty string, the process ends and
2415 Call is done in a LIFO fashion, to be sure that developer
2416 specified filters, if any, are called first."
2418 (let* ((backend (plist-get info
:back-end
))
2419 (backend-name (and backend
(org-export-backend-name backend
))))
2420 (dolist (filter filters value
)
2421 (let ((result (funcall filter value backend-name info
)))
2422 (cond ((not result
) value
)
2423 ((equal value
"") (throw 'exit nil
))
2424 (t (setq value result
))))))))
2426 (defun org-export-install-filters (info)
2427 "Install filters properties in communication channel.
2428 INFO is a plist containing the current communication channel.
2429 Return the updated communication channel."
2431 ;; Install user-defined filters with `org-export-filters-alist'
2432 ;; and filters already in INFO (through ext-plist mechanism).
2434 (let* ((prop (car p
))
2435 (info-value (plist-get info prop
))
2436 (default-value (symbol-value (cdr p
))))
2438 (plist-put plist prop
2439 ;; Filters in INFO will be called
2440 ;; before those user provided.
2441 (append (if (listp info-value
) info-value
2444 org-export-filters-alist
)
2445 ;; Prepend back-end specific filters to that list.
2447 ;; Single values get consed, lists are appended.
2448 (let ((key (car p
)) (value (cdr p
)))
2453 (if (atom value
) (cons value
(plist-get plist key
))
2454 (append value
(plist-get plist key
))))))))
2455 (org-export-get-all-filters (plist-get info
:back-end
)))
2456 ;; Return new communication channel.
2457 (org-combine-plists info plist
)))
2463 ;; This is the room for the main function, `org-export-as', along with
2464 ;; its derivative, `org-export-string-as'.
2465 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2466 ;; function should be added to kill ring.
2468 ;; Note that `org-export-as' doesn't really parse the current buffer,
2469 ;; but a copy of it (with the same buffer-local variables and
2470 ;; visibility), where macros and include keywords are expanded and
2471 ;; Babel blocks are executed, if appropriate.
2472 ;; `org-export-with-buffer-copy' macro prepares that copy.
2474 ;; File inclusion is taken care of by
2475 ;; `org-export-expand-include-keyword' and
2476 ;; `org-export--prepare-file-contents'. Structure wise, including
2477 ;; a whole Org file in a buffer often makes little sense. For
2478 ;; example, if the file contains a headline and the include keyword
2479 ;; was within an item, the item should contain the headline. That's
2480 ;; why file inclusion should be done before any structure can be
2481 ;; associated to the file, that is before parsing.
2483 ;; `org-export-insert-default-template' is a command to insert
2484 ;; a default template (or a back-end specific template) at point or in
2487 (defun org-export-copy-buffer ()
2488 "Return a copy of the current buffer.
2489 The copy preserves Org buffer-local variables, visibility and
2491 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2492 (new-buf (generate-new-buffer (buffer-name))))
2493 (with-current-buffer new-buf
2494 (funcall copy-buffer-fun
)
2495 (set-buffer-modified-p nil
))
2498 (defmacro org-export-with-buffer-copy
(&rest body
)
2499 "Apply BODY in a copy of the current buffer.
2500 The copy preserves local variables, visibility and contents of
2501 the original buffer. Point is at the beginning of the buffer
2502 when BODY is applied."
2504 (org-with-gensyms (buf-copy)
2505 `(let ((,buf-copy
(org-export-copy-buffer)))
2507 (with-current-buffer ,buf-copy
2508 (goto-char (point-min))
2510 (and (buffer-live-p ,buf-copy
)
2511 ;; Kill copy without confirmation.
2512 (progn (with-current-buffer ,buf-copy
2513 (restore-buffer-modified-p nil
))
2514 (kill-buffer ,buf-copy
)))))))
2516 (defun org-export--generate-copy-script (buffer)
2517 "Generate a function duplicating BUFFER.
2519 The copy will preserve local variables, visibility, contents and
2520 narrowing of the original buffer. If a region was active in
2521 BUFFER, contents will be narrowed to that region instead.
2523 The resulting function can be evaluated at a later time, from
2524 another buffer, effectively cloning the original buffer there.
2526 The function assumes BUFFER's major mode is `org-mode'."
2527 (with-current-buffer buffer
2529 (let ((inhibit-modification-hooks t
))
2530 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2531 ;; already in BUFFER.
2532 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2533 ;; Copy specific buffer local variables and variables set
2534 ;; through BIND keywords.
2535 ,@(let ((bound-variables (org-export--list-bound-variables))
2537 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2539 (let ((var (car entry
))
2541 (and (not (memq var org-export-ignored-local-variables
))
2545 buffer-file-coding-system
))
2546 (assq var bound-variables
)
2547 (string-match "^\\(org-\\|orgtbl-\\)"
2549 ;; Skip unreadable values, as they cannot be
2550 ;; sent to external process.
2551 (or (not val
) (ignore-errors (read (format "%S" val
))))
2552 (push `(set (make-local-variable (quote ,var
))
2555 ;; Whole buffer contents.
2557 ,(org-with-wide-buffer
2558 (buffer-substring-no-properties
2559 (point-min) (point-max))))
2561 ,(if (org-region-active-p)
2562 `(narrow-to-region ,(region-beginning) ,(region-end))
2563 `(narrow-to-region ,(point-min) ,(point-max)))
2564 ;; Current position of point.
2565 (goto-char ,(point))
2566 ;; Overlays with invisible property.
2570 (let ((invis-prop (overlay-get ov
'invisible
)))
2573 (make-overlay ,(overlay-start ov
)
2575 'invisible
(quote ,invis-prop
))
2577 (overlays-in (point-min) (point-max)))
2580 (defun org-export--delete-comments ()
2581 "Delete commented areas in the buffer.
2582 Commented areas are comments, comment blocks, commented trees and
2583 inlinetasks. Trailing blank lines after a comment or a comment
2584 block are preserved. Narrowing, if any, is ignored."
2585 (org-with-wide-buffer
2586 (goto-char (point-min))
2587 (let ((regexp (concat org-outline-regexp-bol
".*" org-comment-string
2589 "^[ \t]*#\\(?: \\|$\\|\\+begin_comment\\)"))
2590 (case-fold-search t
))
2591 (while (re-search-forward regexp nil t
)
2592 (let ((e (org-element-at-point)))
2593 (cl-case (org-element-type e
)
2594 ((comment comment-block
)
2595 (delete-region (org-element-property :begin e
)
2596 (progn (goto-char (org-element-property :end e
))
2597 (skip-chars-backward " \r\t\n")
2598 (line-beginning-position 2))))
2599 ((headline inlinetask
)
2600 (when (org-element-property :commentedp e
)
2601 (delete-region (org-element-property :begin e
)
2602 (org-element-property :end e
))))))))))
2604 (defun org-export--prune-tree (data info
)
2605 "Prune non exportable elements from DATA.
2606 DATA is the parse tree to traverse. INFO is the plist holding
2607 export info. Also set `:ignore-list' in INFO to a list of
2608 objects which should be ignored during export, but not removed
2611 ;; First find trees containing a select tag, if any.
2612 (selected (org-export--selected-trees data info
))
2615 ;; Prune non-exportable elements and objects from tree.
2616 ;; As a special case, special rows and cells from tables
2617 ;; are stored in IGNORE, as they still need to be
2618 ;; accessed during export.
2620 (let ((type (org-element-type data
)))
2621 (if (org-export--skip-p data info selected
)
2622 (if (memq type
'(table-cell table-row
)) (push data ignore
)
2623 (org-element-extract-element data
))
2624 (if (and (eq type
'headline
)
2625 (eq (plist-get info
:with-archived-trees
)
2627 (org-element-property :archivedp data
))
2628 ;; If headline is archived but tree below has
2629 ;; to be skipped, remove contents.
2630 (org-element-set-contents data
)
2631 ;; Move into secondary string, if any.
2634 org-element-secondary-value-alist
))))
2637 (org-element-property sec-prop data
))))
2638 ;; Move into recursive objects/elements.
2639 (mapc walk-data
(org-element-contents data
)))))))))
2640 ;; If a select tag is active, also ignore the section before the
2641 ;; first headline, if any.
2643 (let ((first-element (car (org-element-contents data
))))
2644 (when (eq (org-element-type first-element
) 'section
)
2645 (org-element-extract-element first-element
))))
2646 ;; Prune tree and communication channel.
2647 (funcall walk-data data
)
2650 ;; Priority is given to back-end specific options.
2651 (org-export-get-all-options (plist-get info
:back-end
))
2652 org-export-options-alist
))
2653 (when (eq (nth 4 entry
) 'parse
)
2654 (funcall walk-data
(plist-get info
(car entry
)))))
2655 ;; Eventually set `:ignore-list'.
2656 (plist-put info
:ignore-list ignore
)))
2658 (defun org-export--remove-uninterpreted-data (data info
)
2659 "Change uninterpreted elements back into Org syntax.
2660 DATA is the parse tree. INFO is a plist containing export
2661 options. Each uninterpreted element or object is changed back
2662 into a string. Contents, if any, are not modified. The parse
2663 tree is modified by side effect."
2664 (org-export--remove-uninterpreted-data-1 data info
)
2665 (dolist (entry org-export-options-alist
)
2666 (when (eq (nth 4 entry
) 'parse
)
2667 (let ((p (car entry
)))
2670 (org-export--remove-uninterpreted-data-1
2674 (defun org-export--remove-uninterpreted-data-1 (data info
)
2675 "Change uninterpreted elements back into Org syntax.
2676 DATA is a parse tree or a secondary string. INFO is a plist
2677 containing export options. It is modified by side effect and
2678 returned by the function."
2679 (org-element-map data
2680 '(entity bold italic latex-environment latex-fragment strike-through
2681 subscript superscript underline
)
2684 (cl-case (org-element-type blob
)
2687 (and (not (plist-get info
:with-entities
))
2689 (org-export-expand blob nil
)
2691 (or (org-element-property :post-blank blob
) 0)
2694 ((bold italic strike-through underline
)
2695 (and (not (plist-get info
:with-emphasize
))
2696 (let ((marker (cl-case (org-element-type blob
)
2699 (strike-through "+")
2703 (org-element-contents blob
)
2707 (or (org-element-property :post-blank blob
)
2710 ;; ... LaTeX environments and fragments...
2711 ((latex-environment latex-fragment
)
2712 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2713 (list (org-export-expand blob nil
))))
2714 ;; ... sub/superscripts...
2715 ((subscript superscript
)
2716 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2717 (bracketp (org-element-property :use-brackets-p blob
)))
2718 (and (or (not sub
/super-p
)
2719 (and (eq sub
/super-p
'{}) (not bracketp
)))
2722 (if (eq (org-element-type blob
) 'subscript
)
2725 (and bracketp
"{")))
2726 (org-element-contents blob
)
2729 (and (org-element-property :post-blank blob
)
2731 (org-element-property :post-blank blob
)
2734 ;; Splice NEW at BLOB location in parse tree.
2735 (dolist (e new
(org-element-extract-element blob
))
2736 (unless (string= e
"") (org-element-insert-before e blob
))))))
2738 ;; Return modified parse tree.
2741 (defun org-export--merge-external-footnote-definitions (tree)
2742 "Insert footnote definitions outside parsing scope in TREE.
2744 If there is a footnote section in TREE, definitions found are
2745 appended to it. If `org-footnote-section' is non-nil, a new
2746 footnote section containing all definitions is inserted in TREE.
2747 Otherwise, definitions are appended at the end of the section
2748 containing their first reference.
2750 Only definitions actually referred to within TREE, directly or
2751 not, are considered."
2752 (let* ((collect-labels
2754 (org-element-map data
'footnote-reference
2756 (and (eq (org-element-property :type f
) 'standard
)
2757 (org-element-property :label f
))))))
2758 (referenced-labels (funcall collect-labels tree
)))
2759 (when referenced-labels
2760 (let* ((definitions)
2763 (cl-case (org-element-type datum
)
2764 (footnote-definition
2765 (push (save-restriction
2766 (narrow-to-region (org-element-property :begin datum
)
2767 (org-element-property :end datum
))
2768 (org-element-map (org-element-parse-buffer)
2769 'footnote-definition
#'identity nil t
))
2772 (let ((label (org-element-property :label datum
))
2773 (cbeg (org-element-property :contents-begin datum
)))
2774 (when (and label cbeg
2775 (eq (org-element-property :type datum
) 'inline
))
2777 (apply #'org-element-create
2778 'footnote-definition
2779 (list :label label
:post-blank
1)
2780 (org-element-parse-secondary-string
2782 cbeg
(org-element-property :contents-end datum
))
2783 (org-element-restriction 'footnote-reference
)))
2785 ;; Collect all out of scope definitions.
2787 (goto-char (point-min))
2788 (org-with-wide-buffer
2789 (while (re-search-backward org-footnote-re nil t
)
2790 (funcall push-definition
(org-element-context))))
2791 (goto-char (point-max))
2792 (org-with-wide-buffer
2793 (while (re-search-forward org-footnote-re nil t
)
2794 (funcall push-definition
(org-element-context)))))
2795 ;; Filter out definitions referenced neither in the original
2796 ;; tree nor in the external definitions.
2797 (let* ((directly-referenced
2800 (member (org-element-property :label d
) referenced-labels
))
2803 (append (funcall collect-labels directly-referenced
)
2804 referenced-labels
)))
2808 (member (org-element-property :label d
) all-labels
))
2810 ;; Install definitions in subtree.
2812 ((null definitions
))
2813 ;; If there is a footnote section, insert them here.
2814 ((let ((footnote-section
2815 (org-element-map tree
'headline
2817 (and (org-element-property :footnote-section-p h
) h
))
2819 (and footnote-section
2820 (apply #'org-element-adopt-elements
(nreverse definitions
)))))
2821 ;; If there should be a footnote section, create one containing
2822 ;; all the definitions at the end of the tree.
2823 (org-footnote-section
2824 (org-element-adopt-elements
2826 (org-element-create 'headline
2827 (list :footnote-section-p t
2829 :title org-footnote-section
)
2830 (apply #'org-element-create
2833 (nreverse definitions
)))))
2834 ;; Otherwise add each definition at the end of the section where
2835 ;; it is first referenced.
2840 ;; Insert definitions in the same section as
2841 ;; their first reference in DATA.
2842 (org-element-map data
'footnote-reference
2844 (when (eq (org-element-property :type f
) 'standard
)
2845 (let ((label (org-element-property :label f
)))
2846 (unless (member label seen
)
2850 (dolist (d definitions
)
2852 (org-element-property :label
2856 (delete d definitions
))
2857 (throw 'found d
))))))
2859 (org-element-adopt-elements
2860 (org-element-lineage f
'(section))
2862 (funcall insert-definitions
2863 definition
)))))))))))
2864 (funcall insert-definitions tree
))))))))
2867 (defun org-export-as
2868 (backend &optional subtreep visible-only body-only ext-plist
)
2869 "Transcode current Org buffer into BACKEND code.
2871 BACKEND is either an export back-end, as returned by, e.g.,
2872 `org-export-create-backend', or a symbol referring to
2873 a registered back-end.
2875 If narrowing is active in the current buffer, only transcode its
2878 If a region is active, transcode that region.
2880 When optional argument SUBTREEP is non-nil, transcode the
2881 sub-tree at point, extracting information from the headline
2884 When optional argument VISIBLE-ONLY is non-nil, don't export
2885 contents of hidden elements.
2887 When optional argument BODY-ONLY is non-nil, only return body
2888 code, without surrounding template.
2890 Optional argument EXT-PLIST, when provided, is a property list
2891 with external parameters overriding Org default settings, but
2892 still inferior to file-local settings.
2894 Return code as a string."
2895 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2896 (org-export-barf-if-invalid-backend backend
)
2899 ;; Narrow buffer to an appropriate region or subtree for
2900 ;; parsing. If parsing subtree, be sure to remove main headline
2902 (cond ((org-region-active-p)
2903 (narrow-to-region (region-beginning) (region-end)))
2905 (org-narrow-to-subtree)
2906 (goto-char (point-min))
2908 (narrow-to-region (point) (point-max))))
2909 ;; Initialize communication channel with original buffer
2910 ;; attributes, unavailable in its copy.
2911 (let* ((org-export-current-backend (org-export-backend-name backend
))
2912 (info (org-combine-plists
2913 (list :export-options
2915 (list (and subtreep
'subtree
)
2916 (and visible-only
'visible-only
)
2917 (and body-only
'body-only
))))
2918 (org-export--get-buffer-attributes)))
2921 (mapcar (lambda (o) (and (eq (nth 4 o
) 'parse
) (nth 1 o
)))
2922 (append (org-export-get-all-options backend
)
2923 org-export-options-alist
))))
2925 ;; Update communication channel and get parse tree. Buffer
2926 ;; isn't parsed directly. Instead, all buffer modifications
2927 ;; and consequent parsing are undertaken in a temporary copy.
2928 (org-export-with-buffer-copy
2929 ;; Run first hook with current back-end's name as argument.
2930 (run-hook-with-args 'org-export-before-processing-hook
2931 (org-export-backend-name backend
))
2932 ;; Include files, delete comments and expand macros.
2933 (org-export-expand-include-keyword)
2934 (org-export--delete-comments)
2935 (org-macro-initialize-templates)
2936 (org-macro-replace-all org-macro-templates nil parsed-keywords
)
2937 ;; Refresh buffer properties and radio targets after
2938 ;; potentially invasive previous changes. Likewise, do it
2939 ;; again after executing Babel code.
2940 (org-set-regexps-and-options)
2941 (org-update-radio-target-regexp)
2942 (org-export-execute-babel-code)
2943 (org-set-regexps-and-options)
2944 (org-update-radio-target-regexp)
2945 ;; Run last hook with current back-end's name as argument.
2946 ;; Update buffer properties and radio targets one last time
2948 (goto-char (point-min))
2950 (run-hook-with-args 'org-export-before-parsing-hook
2951 (org-export-backend-name backend
)))
2952 (org-set-regexps-and-options)
2953 (org-update-radio-target-regexp)
2954 ;; Update communication channel with environment. Also
2955 ;; install user's and developer's filters.
2957 (org-export-install-filters
2959 info
(org-export-get-environment backend subtreep ext-plist
))))
2960 ;; Call options filters and update export options. We do not
2961 ;; use `org-export-filter-apply-functions' here since the
2962 ;; arity of such filters is different.
2963 (let ((backend-name (org-export-backend-name backend
)))
2964 (dolist (filter (plist-get info
:filter-options
))
2965 (let ((result (funcall filter info backend-name
)))
2966 (when result
(setq info result
)))))
2967 ;; Expand export-specific set of macros: {{{author}}},
2968 ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}. It must
2969 ;; be done once regular macros have been expanded, since
2970 ;; parsed keywords may contain one of them.
2971 (org-macro-replace-all
2973 (cons "author" (org-element-interpret-data (plist-get info
:author
)))
2975 (let* ((date (plist-get info
:date
))
2976 (value (or (org-element-interpret-data date
) "")))
2977 (if (and (consp date
)
2979 (eq (org-element-type (car date
)) 'timestamp
))
2980 (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
2981 (format "(org-timestamp-format '%S \"$1\")"
2982 (org-element-copy (car date
)))
2985 (cons "email" (org-element-interpret-data (plist-get info
:email
)))
2986 (cons "title" (org-element-interpret-data (plist-get info
:title
)))
2987 (cons "results" "$1"))
2991 (setq tree
(org-element-parse-buffer nil visible-only
))
2992 ;; Merge footnote definitions outside scope into parse tree.
2993 (org-export--merge-external-footnote-definitions tree
)
2994 ;; Prune tree from non-exported elements and transform
2995 ;; uninterpreted elements or objects in both parse tree and
2996 ;; communication channel.
2997 (org-export--prune-tree tree info
)
2998 (org-export--remove-uninterpreted-data tree info
)
2999 ;; Call parse tree filters.
3001 (org-export-filter-apply-functions
3002 (plist-get info
:filter-parse-tree
) tree info
))
3003 ;; Now tree is complete, compute its properties and add them
3004 ;; to communication channel.
3007 info
(org-export-collect-tree-properties tree info
)))
3008 ;; Eventually transcode TREE. Wrap the resulting string into
3010 (let* ((body (org-element-normalize-string
3011 (or (org-export-data tree info
) "")))
3012 (inner-template (cdr (assq 'inner-template
3013 (plist-get info
:translate-alist
))))
3014 (full-body (org-export-filter-apply-functions
3015 (plist-get info
:filter-body
)
3016 (if (not (functionp inner-template
)) body
3017 (funcall inner-template body info
))
3019 (template (cdr (assq 'template
3020 (plist-get info
:translate-alist
)))))
3021 ;; Remove all text properties since they cannot be
3022 ;; retrieved from an external process. Finally call
3023 ;; final-output filter and return result.
3025 (org-export-filter-apply-functions
3026 (plist-get info
:filter-final-output
)
3027 (if (or (not (functionp template
)) body-only
) full-body
3028 (funcall template full-body info
))
3032 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3033 "Transcode STRING into BACKEND code.
3035 BACKEND is either an export back-end, as returned by, e.g.,
3036 `org-export-create-backend', or a symbol referring to
3037 a registered back-end.
3039 When optional argument BODY-ONLY is non-nil, only return body
3040 code, without preamble nor postamble.
3042 Optional argument EXT-PLIST, when provided, is a property list
3043 with external parameters overriding Org default settings, but
3044 still inferior to file-local settings.
3046 Return code as a string."
3049 (let ((org-inhibit-startup t
)) (org-mode))
3050 (org-export-as backend nil nil body-only ext-plist
)))
3053 (defun org-export-replace-region-by (backend)
3054 "Replace the active region by its export to BACKEND.
3055 BACKEND is either an export back-end, as returned by, e.g.,
3056 `org-export-create-backend', or a symbol referring to
3057 a registered back-end."
3058 (unless (org-region-active-p) (user-error "No active region to replace"))
3060 (org-export-string-as
3061 (delete-and-extract-region (region-beginning) (region-end)) backend t
)))
3064 (defun org-export-insert-default-template (&optional backend subtreep
)
3065 "Insert all export keywords with default values at beginning of line.
3067 BACKEND is a symbol referring to the name of a registered export
3068 back-end, for which specific export options should be added to
3069 the template, or `default' for default template. When it is nil,
3070 the user will be prompted for a category.
3072 If SUBTREEP is non-nil, export configuration will be set up
3073 locally for the subtree through node properties."
3075 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3076 (when (and subtreep
(org-before-first-heading-p))
3077 (user-error "No subtree to set export options for"))
3078 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3082 (org-completing-read
3083 "Options category: "
3086 (symbol-name (org-export-backend-name b
)))
3087 org-export-registered-backends
))
3090 ;; Populate OPTIONS and KEYWORDS.
3091 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3092 ((org-export-backend-p backend
)
3093 (org-export-backend-options backend
))
3094 (t (org-export-backend-options
3095 (org-export-get-backend backend
)))))
3096 (let ((keyword (nth 1 entry
))
3097 (option (nth 2 entry
)))
3099 (keyword (unless (assoc keyword keywords
)
3101 (if (eq (nth 4 entry
) 'split
)
3102 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3103 (eval (nth 3 entry
)))))
3104 (push (cons keyword value
) keywords
))))
3105 (option (unless (assoc option options
)
3106 (push (cons option
(eval (nth 3 entry
))) options
))))))
3107 ;; Move to an appropriate location in order to insert options.
3108 (unless subtreep
(beginning-of-line))
3109 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3113 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3114 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3117 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3119 (insert "#+OPTIONS:")
3122 (< (+ width
(length (car items
)) 1) fill-column
))
3123 (let ((item (pop items
)))
3125 (cl-incf width
(1+ (length item
))))))
3127 ;; Then the rest of keywords, in the order specified in either
3128 ;; `org-export-options-alist' or respective export back-ends.
3129 (dolist (key (nreverse keywords
))
3130 (let ((val (cond ((equal (car key
) "DATE")
3133 (org-insert-time-stamp (current-time)))))
3134 ((equal (car key
) "TITLE")
3135 (or (let ((visited-file
3136 (buffer-file-name (buffer-base-buffer))))
3138 (file-name-sans-extension
3139 (file-name-nondirectory visited-file
))))
3140 (buffer-name (buffer-base-buffer))))
3142 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3146 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3148 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3149 "Expand every include keyword in buffer.
3150 Optional argument INCLUDED is a list of included file names along
3151 with their line restriction, when appropriate. It is used to
3152 avoid infinite recursion. Optional argument DIR is the current
3153 working directory. It is used to properly resolve relative
3154 paths. Optional argument FOOTNOTES is a hash-table used for
3155 storing and resolving footnotes. It is created automatically."
3156 (let ((case-fold-search t
)
3157 (file-prefix (make-hash-table :test
#'equal
))
3159 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3160 (include-re "^[ \t]*#\\+INCLUDE:"))
3161 ;; If :minlevel is not set the text-property
3162 ;; `:org-include-induced-level' will be used to determine the
3163 ;; relative level when expanding INCLUDE.
3164 ;; Only affects included Org documents.
3165 (goto-char (point-min))
3166 (while (re-search-forward include-re nil t
)
3167 (put-text-property (line-beginning-position) (line-end-position)
3168 :org-include-induced-level
3169 (1+ (org-reduced-level (or (org-current-level) 0)))))
3170 ;; Expand INCLUDE keywords.
3171 (goto-char (point-min))
3172 (while (re-search-forward include-re nil t
)
3173 (let ((element (save-match-data (org-element-at-point))))
3174 (when (eq (org-element-type element
) 'keyword
)
3176 ;; Extract arguments from keyword's value.
3177 (let* ((value (org-element-property :value element
))
3178 (ind (org-get-indentation))
3182 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3185 (let ((matched (match-string 1 value
)))
3186 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
3188 (setq location
(match-string 2 matched
))
3190 (replace-match "" nil nil matched
1)))
3192 (org-remove-double-quotes
3195 (setq value
(replace-match "" nil nil value
)))))
3197 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
3199 (prog1 (org-not-nil (match-string 1 value
))
3200 (setq value
(replace-match "" nil nil value
)))))
3203 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3205 (prog1 (match-string 1 value
)
3206 (setq value
(replace-match "" nil nil value
)))))
3207 (env (cond ((string-match "\\<example\\>" value
)
3209 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3211 ;; Minimal level of included file defaults to the child
3212 ;; level of the current headline, if any, or one. It
3213 ;; only applies is the file is meant to be included as
3217 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3218 (prog1 (string-to-number (match-string 1 value
))
3219 (setq value
(replace-match "" nil nil value
)))
3220 (get-text-property (point)
3221 :org-include-induced-level
))))
3222 (src-args (and (eq env
'literal
)
3223 (match-string 1 value
)))
3224 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3225 (match-string 1 value
))))
3227 (delete-region (point) (progn (forward-line) (point)))
3230 ((not (file-readable-p file
))
3231 (error "Cannot include file %s" file
))
3232 ;; Check if files has already been parsed. Look after
3233 ;; inclusion lines too, as different parts of the same file
3234 ;; can be included too.
3235 ((member (list file lines
) included
)
3236 (error "Recursive file inclusion: %s" file
))
3241 (let ((ind-str (make-string ind ?
))
3242 (arg-str (if (stringp src-args
)
3243 (format " %s" src-args
)
3246 (org-escape-code-in-string
3247 (org-export--prepare-file-contents file lines
))))
3248 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3249 ind-str block arg-str contents ind-str block
))))
3252 (let ((ind-str (make-string ind ?
))
3254 (org-export--prepare-file-contents file lines
)))
3255 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3256 ind-str block contents ind-str block
))))
3260 (let ((org-inhibit-startup t
)
3263 (org-export--inclusion-absolute-lines
3264 file location only-contents lines
)
3268 (org-export--prepare-file-contents
3269 file lines ind minlevel
3270 (or (gethash file file-prefix
)
3271 (puthash file
(cl-incf current-prefix
) file-prefix
))
3273 (org-export-expand-include-keyword
3274 (cons (list file lines
) included
)
3275 (file-name-directory file
)
3278 ;; Expand footnotes after all files have been included.
3279 ;; Footnotes are stored at end of buffer.
3281 (org-with-wide-buffer
3282 (goto-char (point-max))
3283 (maphash (lambda (k v
) (insert (format "\n[%s] %s\n" k v
)))
3284 footnotes
)))))))))))
3286 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3287 "Resolve absolute lines for an included file with file-link.
3289 FILE is string file-name of the file to include. LOCATION is a
3290 string name within FILE to be included (located via
3291 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3292 contents of the named element will be included, as determined
3293 Org-Element. If LINES is non-nil only those lines are included.
3295 Return a string of lines to be included in the format expected by
3296 `org-export--prepare-file-contents'."
3298 (insert-file-contents file
)
3299 (unless (eq major-mode
'org-mode
)
3300 (let ((org-inhibit-startup t
)) (org-mode)))
3302 ;; Enforce consistent search.
3303 (let ((org-link-search-must-match-exact-headline nil
))
3304 (org-link-search location
))
3306 (error "%s for %s::%s" (error-message-string err
) file location
)))
3307 (let* ((element (org-element-at-point))
3309 (and only-contents
(org-element-property :contents-begin element
))))
3311 (or contents-begin
(org-element-property :begin element
))
3312 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3313 (when (and only-contents
3314 (memq (org-element-type element
) '(headline inlinetask
)))
3315 ;; Skip planning line and property-drawer.
3316 (goto-char (point-min))
3317 (when (org-looking-at-p org-planning-line-re
) (forward-line))
3318 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3319 (unless (bolp) (forward-line))
3320 (narrow-to-region (point) (point-max))))
3322 (org-skip-whitespace)
3324 (let* ((lines (split-string lines
"-"))
3325 (lbeg (string-to-number (car lines
)))
3326 (lend (string-to-number (cadr lines
)))
3327 (beg (if (zerop lbeg
) (point-min)
3328 (goto-char (point-min))
3329 (forward-line (1- lbeg
))
3331 (end (if (zerop lend
) (point-max)
3333 (forward-line (1- lend
))
3335 (narrow-to-region beg end
)))
3336 (let ((end (point-max)))
3337 (goto-char (point-min))
3339 (let ((start-line (line-number-at-pos)))
3345 (while (< (point) end
) (cl-incf counter
) (forward-line))
3348 (defun org-export--prepare-file-contents
3349 (file &optional lines ind minlevel id footnotes
)
3350 "Prepare contents of FILE for inclusion and return it as a string.
3352 When optional argument LINES is a string specifying a range of
3353 lines, include only those lines.
3355 Optional argument IND, when non-nil, is an integer specifying the
3356 global indentation of returned contents. Since its purpose is to
3357 allow an included file to stay in the same environment it was
3358 created (e.g., a list item), it doesn't apply past the first
3359 headline encountered.
3361 Optional argument MINLEVEL, when non-nil, is an integer
3362 specifying the level that any top-level headline in the included
3365 Optional argument ID is an integer that will be inserted before
3366 each footnote definition and reference if FILE is an Org file.
3367 This is useful to avoid conflicts when more than one Org file
3368 with footnotes is included in a document.
3370 Optional argument FOOTNOTES is a hash-table to store footnotes in
3371 the included document."
3373 (insert-file-contents file
)
3375 (let* ((lines (split-string lines
"-"))
3376 (lbeg (string-to-number (car lines
)))
3377 (lend (string-to-number (cadr lines
)))
3378 (beg (if (zerop lbeg
) (point-min)
3379 (goto-char (point-min))
3380 (forward-line (1- lbeg
))
3382 (end (if (zerop lend
) (point-max)
3383 (goto-char (point-min))
3384 (forward-line (1- lend
))
3386 (narrow-to-region beg end
)))
3387 ;; Remove blank lines at beginning and end of contents. The logic
3388 ;; behind that removal is that blank lines around include keyword
3389 ;; override blank lines in included file.
3390 (goto-char (point-min))
3391 (org-skip-whitespace)
3393 (delete-region (point-min) (point))
3394 (goto-char (point-max))
3395 (skip-chars-backward " \r\t\n")
3397 (delete-region (point) (point-max))
3398 ;; If IND is set, preserve indentation of include keyword until
3399 ;; the first headline encountered.
3400 (when (and ind
(> ind
0))
3401 (unless (eq major-mode
'org-mode
)
3402 (let ((org-inhibit-startup t
)) (org-mode)))
3403 (goto-char (point-min))
3404 (let ((ind-str (make-string ind ?
)))
3405 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3406 ;; Do not move footnote definitions out of column 0.
3407 (unless (and (looking-at org-footnote-definition-re
)
3408 (eq (org-element-type (org-element-at-point))
3409 'footnote-definition
))
3412 ;; When MINLEVEL is specified, compute minimal level for headlines
3413 ;; in the file (CUR-MIN), and remove stars to each headline so
3414 ;; that headlines with minimal level have a level of MINLEVEL.
3416 (unless (eq major-mode
'org-mode
)
3417 (let ((org-inhibit-startup t
)) (org-mode)))
3418 (org-with-limited-levels
3419 (let ((levels (org-map-entries
3420 (lambda () (org-reduced-level (org-current-level))))))
3422 (let ((offset (- minlevel
(apply #'min levels
))))
3423 (unless (zerop offset
)
3424 (when org-odd-levels-only
(setq offset
(* offset
2)))
3425 ;; Only change stars, don't bother moving whole
3429 (if (< offset
0) (delete-char (abs offset
))
3430 (insert (make-string offset ?
*)))))))))))
3431 ;; Append ID to all footnote references and definitions, so they
3432 ;; become file specific and cannot collide with footnotes in other
3433 ;; included files. Further, collect relevant footnote definitions
3434 ;; outside of LINES, in order to reintroduce them later.
3436 (let ((marker-min (point-min-marker))
3437 (marker-max (point-max-marker))
3440 ;; Generate new label from LABEL. If LABEL is akin to
3441 ;; [1] convert it to [fn:--ID-1]. Otherwise add "-ID-"
3443 (if (org-string-match-p "\\`[0-9]+\\'" label
)
3444 (format "fn:--%d-%s" id label
)
3445 (format "fn:-%d-%s" id
(substring label
3)))))
3448 ;; Replace OLD label with NEW in footnote F.
3450 (goto-char (1+ (org-element-property :begin f
)))
3451 (looking-at (regexp-quote old
))
3452 (replace-match new
))))
3454 (goto-char (point-min))
3455 (while (re-search-forward org-footnote-re nil t
)
3456 (let ((footnote (save-excursion
3458 (org-element-context))))
3459 (when (memq (org-element-type footnote
)
3460 '(footnote-definition footnote-reference
))
3461 (let* ((label (org-element-property :label footnote
)))
3462 ;; Update the footnote-reference at point and collect
3463 ;; the new label, which is only used for footnotes
3466 (let ((seen (cdr (assoc label seen-alist
))))
3467 (if seen
(funcall set-new-label footnote label seen
)
3468 (let ((new (funcall get-new-label label
)))
3469 (push (cons label new
) seen-alist
)
3470 (org-with-wide-buffer
3471 (let* ((def (org-footnote-get-definition label
))
3474 (or (< beg marker-min
)
3475 (>= beg marker-max
)))
3476 ;; Store since footnote-definition is
3477 ;; outside of LINES.
3479 (org-element-normalize-string (nth 3 def
))
3481 (funcall set-new-label footnote label new
)))))))))
3482 (set-marker marker-min nil
)
3483 (set-marker marker-max nil
)))
3484 (org-element-normalize-string (buffer-string))))
3486 (defun org-export-execute-babel-code ()
3487 "Execute every Babel code in the visible part of current buffer."
3488 ;; Get a pristine copy of current buffer so Babel references can be
3489 ;; properly resolved.
3490 (let ((reference (org-export-copy-buffer)))
3491 (unwind-protect (org-babel-exp-process-buffer reference
)
3492 (kill-buffer reference
))))
3494 (defun org-export--copy-to-kill-ring-p ()
3495 "Return a non-nil value when output should be added to the kill ring.
3496 See also `org-export-copy-to-kill-ring'."
3497 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3498 (not (or executing-kbd-macro noninteractive
))
3499 (eq org-export-copy-to-kill-ring t
)))
3503 ;;; Tools For Back-Ends
3505 ;; A whole set of tools is available to help build new exporters. Any
3506 ;; function general enough to have its use across many back-ends
3507 ;; should be added here.
3509 ;;;; For Affiliated Keywords
3511 ;; `org-export-read-attribute' reads a property from a given element
3512 ;; as a plist. It can be used to normalize affiliated keywords'
3515 ;; Since captions can span over multiple lines and accept dual values,
3516 ;; their internal representation is a bit tricky. Therefore,
3517 ;; `org-export-get-caption' transparently returns a given element's
3518 ;; caption as a secondary string.
3520 (defun org-export-read-attribute (attribute element
&optional property
)
3521 "Turn ATTRIBUTE property from ELEMENT into a plist.
3523 When optional argument PROPERTY is non-nil, return the value of
3524 that property within attributes.
3526 This function assumes attributes are defined as \":keyword
3527 value\" pairs. It is appropriate for `:attr_html' like
3530 All values will become strings except the empty string and
3531 \"nil\", which will become nil. Also, values containing only
3532 double quotes will be read as-is, which means that \"\" value
3533 will become the empty string."
3534 (let* ((prepare-value
3537 (cond ((member str
'(nil "" "nil")) nil
)
3538 ((string-match "^\"\\(\"+\\)?\"$" str
)
3539 (or (match-string 1 str
) ""))
3542 (let ((value (org-element-property attribute element
)))
3544 (let ((s (mapconcat 'identity value
" ")) result
)
3545 (while (string-match
3546 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3548 (let ((value (substring s
0 (match-beginning 0))))
3549 (push (funcall prepare-value value
) result
))
3550 (push (intern (match-string 1 s
)) result
)
3551 (setq s
(substring s
(match-end 0))))
3552 ;; Ignore any string before first property with `cdr'.
3553 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3554 (if property
(plist-get attributes property
) attributes
)))
3556 (defun org-export-get-caption (element &optional shortp
)
3557 "Return caption from ELEMENT as a secondary string.
3559 When optional argument SHORTP is non-nil, return short caption,
3560 as a secondary string, instead.
3562 Caption lines are separated by a white space."
3563 (let ((full-caption (org-element-property :caption element
)) caption
)
3564 (dolist (line full-caption
(cdr caption
))
3565 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3567 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3570 ;;;; For Derived Back-ends
3572 ;; `org-export-with-backend' is a function allowing to locally use
3573 ;; another back-end to transcode some object or element. In a derived
3574 ;; back-end, it may be used as a fall-back function once all specific
3575 ;; cases have been treated.
3577 (defun org-export-with-backend (backend data
&optional contents info
)
3578 "Call a transcoder from BACKEND on DATA.
3579 BACKEND is an export back-end, as returned by, e.g.,
3580 `org-export-create-backend', or a symbol referring to
3581 a registered back-end. DATA is an Org element, object, secondary
3582 string or string. CONTENTS, when non-nil, is the transcoded
3583 contents of DATA element, as a string. INFO, when non-nil, is
3584 the communication channel used for export, as a plist."
3585 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3586 (org-export-barf-if-invalid-backend backend
)
3587 (let ((type (org-element-type data
)))
3588 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3589 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3590 (transcoder (cdr (assq type all-transcoders
))))
3591 (if (not (functionp transcoder
))
3592 (error "No foreign transcoder available")
3594 transcoder data contents
3598 :translate-alist all-transcoders
3599 :exported-data
(make-hash-table :test
#'eq
:size
401)))))))))
3602 ;;;; For Export Snippets
3604 ;; Every export snippet is transmitted to the back-end. Though, the
3605 ;; latter will only retain one type of export-snippet, ignoring
3606 ;; others, based on the former's target back-end. The function
3607 ;; `org-export-snippet-backend' returns that back-end for a given
3610 (defun org-export-snippet-backend (export-snippet)
3611 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3612 Translation, with `org-export-snippet-translation-alist', is
3614 (let ((back-end (org-element-property :back-end export-snippet
)))
3616 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3622 ;; `org-export-collect-footnote-definitions' is a tool to list
3623 ;; actually used footnotes definitions in the whole parse tree, or in
3624 ;; a headline, in order to add footnote listings throughout the
3627 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3628 ;; back-ends, when they need to attach the footnote definition only to
3629 ;; the first occurrence of the corresponding label.
3631 ;; `org-export-get-footnote-definition' and
3632 ;; `org-export-get-footnote-number' provide easier access to
3633 ;; additional information relative to a footnote reference.
3635 (defun org-export-get-footnote-definition (footnote-reference info
)
3636 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3637 INFO is the plist used as a communication channel. If no such
3638 definition can be found, raise an error."
3639 (let ((label (org-element-property :label footnote-reference
)))
3640 (if (not label
) (org-element-contents footnote-reference
)
3641 (let ((cache (or (plist-get info
:footnote-definition-cache
)
3642 (let ((hash (make-hash-table :test
#'equal
)))
3643 (plist-put info
:footnote-definition-cache hash
)
3645 (or (gethash label cache
)
3647 (org-element-map (plist-get info
:parse-tree
)
3648 '(footnote-definition footnote-reference
)
3650 (and (equal (org-element-property :label f
) label
)
3651 (org-element-contents f
)))
3654 (error "Definition not found for footnote %s" label
))))))
3656 (defun org-export--footnote-reference-map
3657 (function data info
&optional body-first
)
3658 "Apply FUNCTION on every footnote reference in DATA.
3659 INFO is a plist containing export state. By default, as soon as
3660 a new footnote reference is encountered, FUNCTION is called onto
3661 its definition. However, if BODY-FIRST is non-nil, this step is
3662 delayed until the end of the process."
3663 (letrec ((definitions)
3666 (lambda (data delayp
)
3667 ;; Search footnote references through DATA, filling
3668 ;; SEEN-REFS along the way. When DELAYP is non-nil,
3669 ;; store footnote definitions so they can be entered
3671 (org-element-map data
'footnote-reference
3673 (funcall function f
)
3674 (let ((--label (org-element-property :label f
)))
3675 (unless (and --label
(member --label seen-refs
))
3676 (when --label
(push --label seen-refs
))
3677 ;; Search for subsequent references in footnote
3678 ;; definition so numbering follows reading
3679 ;; logic, unless DELAYP in non-nil.
3682 (push (org-export-get-footnote-definition f info
)
3684 ;; Do not force entering inline definitions,
3685 ;; since `org-element-map' already traverses
3686 ;; them at the right time.
3687 ((eq (org-element-property :type f
) 'inline
))
3688 (t (funcall search-ref
3689 (org-export-get-footnote-definition f info
)
3692 ;; Don't enter footnote definitions since it will
3693 ;; happen when their first reference is found.
3694 ;; Moreover, if DELAYP is non-nil, make sure we
3695 ;; postpone entering definitions of inline references.
3696 (if delayp
'(footnote-definition footnote-reference
)
3697 'footnote-definition
)))))
3698 (funcall search-ref data body-first
)
3699 (funcall search-ref
(nreverse definitions
) nil
)))
3701 (defun org-export-collect-footnote-definitions (info &optional data body-first
)
3702 "Return an alist between footnote numbers, labels and definitions.
3704 INFO is the current export state, as a plist.
3706 Definitions are collected throughout the whole parse tree, or
3709 Sorting is done by order of references. As soon as a new
3710 reference is encountered, other references are searched within
3711 its definition. However, if BODY-FIRST is non-nil, this step is
3712 delayed after the whole tree is checked. This alters results
3713 when references are found in footnote definitions.
3715 Definitions either appear as Org data or as a secondary string
3716 for inlined footnotes. Unreferenced definitions are ignored."
3717 (let ((n 0) labels alist
)
3718 (org-export--footnote-reference-map
3720 ;; Collect footnote number, label and definition.
3721 (let ((l (org-element-property :label f
)))
3722 (unless (and l
(member l labels
))
3724 (push (list n l
(org-export-get-footnote-definition f info
)) alist
))
3725 (when l
(push l labels
))))
3726 (or data
(plist-get info
:parse-tree
)) info body-first
)
3729 (defun org-export-footnote-first-reference-p
3730 (footnote-reference info
&optional data body-first
)
3731 "Non-nil when a footnote reference is the first one for its label.
3733 FOOTNOTE-REFERENCE is the footnote reference being considered.
3734 INFO is a plist containing current export state.
3736 Search is done throughout the whole parse tree, or DATA when
3739 By default, as soon as a new footnote reference is encountered,
3740 other references are searched within its definition. However, if
3741 BODY-FIRST is non-nil, this step is delayed after the whole tree
3742 is checked. This alters results when references are found in
3743 footnote definitions."
3744 (let ((label (org-element-property :label footnote-reference
)))
3745 ;; Anonymous footnotes are always a first reference.
3748 (org-export--footnote-reference-map
3750 (let ((l (org-element-property :label f
)))
3751 (when (and l label
(string= label l
))
3752 (throw 'exit
(eq footnote-reference f
)))))
3753 (or data
(plist-get info
:parse-tree
)) info body-first
)))))
3755 (defun org-export-get-footnote-number (footnote info
&optional data body-first
)
3756 "Return number associated to a footnote.
3758 FOOTNOTE is either a footnote reference or a footnote definition.
3759 INFO is the plist containing export state.
3761 Number is unique throughout the whole parse tree, or DATA, when
3764 By default, as soon as a new footnote reference is encountered,
3765 counting process moves into its definition. However, if
3766 BODY-FIRST is non-nil, this step is delayed until the end of the
3767 process, leading to a different order when footnotes are nested."
3770 (label (org-element-property :label footnote
)))
3772 (org-export--footnote-reference-map
3774 (let ((l (org-element-property :label f
)))
3776 ;; Anonymous footnote match: return number.
3777 ((and (not l
) (not label
) (eq footnote f
)) (throw 'exit
(1+ count
)))
3778 ;; Labels match: return number.
3779 ((and label l
(string= label l
)) (throw 'exit
(1+ count
)))
3780 ;; Otherwise store label and increase counter if label
3781 ;; wasn't encountered yet.
3782 ((not l
) (cl-incf count
))
3783 ((not (member l seen
)) (push l seen
) (cl-incf count
)))))
3784 (or data
(plist-get info
:parse-tree
)) info body-first
))))
3789 ;; `org-export-get-relative-level' is a shortcut to get headline
3790 ;; level, relatively to the lower headline level in the parsed tree.
3792 ;; `org-export-get-headline-number' returns the section number of an
3793 ;; headline, while `org-export-number-to-roman' allows to convert it
3794 ;; to roman numbers. With an optional argument,
3795 ;; `org-export-get-headline-number' returns a number to unnumbered
3796 ;; headlines (used for internal id).
3798 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3799 ;; `org-export-last-sibling-p' are three useful predicates when it
3800 ;; comes to fulfill the `:headline-levels' property.
3802 ;; `org-export-get-tags', `org-export-get-category' and
3803 ;; `org-export-get-node-property' extract useful information from an
3804 ;; headline or a parent headline. They all handle inheritance.
3806 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3807 ;; as a secondary string, suitable for table of contents. It falls
3808 ;; back onto default title.
3810 (defun org-export-get-relative-level (headline info
)
3811 "Return HEADLINE relative level within current parsed tree.
3812 INFO is a plist holding contextual information."
3813 (+ (org-element-property :level headline
)
3814 (or (plist-get info
:headline-offset
) 0)))
3816 (defun org-export-low-level-p (headline info
)
3817 "Non-nil when HEADLINE is considered as low level.
3819 INFO is a plist used as a communication channel.
3821 A low level headlines has a relative level greater than
3822 `:headline-levels' property value.
3824 Return value is the difference between HEADLINE relative level
3825 and the last level being considered as high enough, or nil."
3826 (let ((limit (plist-get info
:headline-levels
)))
3827 (when (wholenump limit
)
3828 (let ((level (org-export-get-relative-level headline info
)))
3829 (and (> level limit
) (- level limit
))))))
3831 (defun org-export-get-headline-number (headline info
)
3832 "Return numbered HEADLINE numbering as a list of numbers.
3833 INFO is a plist holding contextual information."
3834 (and (org-export-numbered-headline-p headline info
)
3835 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
3837 (defun org-export-numbered-headline-p (headline info
)
3838 "Return a non-nil value if HEADLINE element should be numbered.
3839 INFO is a plist used as a communication channel."
3841 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head
)))
3842 (org-element-lineage headline nil t
))
3843 (let ((sec-num (plist-get info
:section-numbers
))
3844 (level (org-export-get-relative-level headline info
)))
3845 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
3847 (defun org-export-number-to-roman (n)
3848 "Convert integer N into a roman numeral."
3849 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3850 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3851 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3855 (number-to-string n
)
3857 (if (>= n
(caar roman
))
3858 (setq n
(- n
(caar roman
))
3859 res
(concat res
(cdar roman
)))
3863 (defun org-export-get-tags (element info
&optional tags inherited
)
3864 "Return list of tags associated to ELEMENT.
3866 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3867 is a plist used as a communication channel.
3869 Select tags (see `org-export-select-tags') and exclude tags (see
3870 `org-export-exclude-tags') are removed from the list.
3872 When non-nil, optional argument TAGS should be a list of strings.
3873 Any tag belonging to this list will also be removed.
3875 When optional argument INHERITED is non-nil, tags can also be
3876 inherited from parent headlines and FILETAGS keywords."
3878 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3879 (member tag
(plist-get info
:exclude-tags
))
3881 (if (not inherited
) (org-element-property :tags element
)
3882 ;; Build complete list of inherited tags.
3883 (let ((current-tag-list (org-element-property :tags element
)))
3884 (dolist (parent (org-element-lineage element
))
3885 (dolist (tag (org-element-property :tags parent
))
3886 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3887 (not (member tag current-tag-list
)))
3888 (push tag current-tag-list
))))
3889 ;; Add FILETAGS keywords and return results.
3890 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3892 (defun org-export-get-node-property (property blob
&optional inherited
)
3893 "Return node PROPERTY value for BLOB.
3895 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3898 If optional argument INHERITED is non-nil, the value can be
3899 inherited from a parent headline.
3901 Return value is a string or nil."
3902 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3903 (org-export-get-parent-headline blob
))))
3904 (if (not inherited
) (org-element-property property blob
)
3905 (let ((parent headline
))
3908 (when (plist-member (nth 1 parent
) property
)
3909 (throw 'found
(org-element-property property parent
)))
3910 (setq parent
(org-element-property :parent parent
))))))))
3912 (defun org-export-get-category (blob info
)
3913 "Return category for element or object BLOB.
3915 INFO is a plist used as a communication channel.
3917 CATEGORY is automatically inherited from a parent headline, from
3918 #+CATEGORY: keyword or created out of original file name. If all
3919 fail, the fall-back value is \"???\"."
3920 (or (org-export-get-node-property :CATEGORY blob t
)
3921 (org-element-map (plist-get info
:parse-tree
) 'keyword
3923 (when (equal (org-element-property :key kwd
) "CATEGORY")
3924 (org-element-property :value kwd
)))
3926 (let ((file (plist-get info
:input-file
)))
3927 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3930 (defun org-export-get-alt-title (headline _
)
3931 "Return alternative title for HEADLINE, as a secondary string.
3932 If no optional title is defined, fall-back to the regular title."
3933 (let ((alt (org-element-property :ALT_TITLE headline
)))
3934 (if alt
(org-element-parse-secondary-string
3935 alt
(org-element-restriction 'headline
) headline
)
3936 (org-element-property :title headline
))))
3938 (defun org-export-first-sibling-p (blob info
)
3939 "Non-nil when BLOB is the first sibling in its parent.
3940 BLOB is an element or an object. If BLOB is a headline, non-nil
3941 means it is the first sibling in the sub-tree. INFO is a plist
3942 used as a communication channel."
3943 (memq (org-element-type (org-export-get-previous-element blob info
))
3946 (defun org-export-last-sibling-p (blob info
)
3947 "Non-nil when BLOB is the last sibling in its parent.
3948 BLOB is an element or an object. INFO is a plist used as
3949 a communication channel."
3950 (not (org-export-get-next-element blob info
)))
3955 ;; `org-export-get-date' returns a date appropriate for the document
3956 ;; to about to be exported. In particular, it takes care of
3957 ;; `org-export-date-timestamp-format'.
3959 (defun org-export-get-date (info &optional fmt
)
3960 "Return date value for the current document.
3962 INFO is a plist used as a communication channel. FMT, when
3963 non-nil, is a time format string that will be applied on the date
3964 if it consists in a single timestamp object. It defaults to
3965 `org-export-date-timestamp-format' when nil.
3967 A proper date can be a secondary string, a string or nil. It is
3968 meant to be translated with `org-export-data' or alike."
3969 (let ((date (plist-get info
:date
))
3970 (fmt (or fmt org-export-date-timestamp-format
)))
3971 (cond ((not date
) nil
)
3974 (eq (org-element-type (car date
)) 'timestamp
))
3975 (org-timestamp-format (car date
) fmt
))
3981 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
3982 ;; with `org-add-link-type', which see.
3984 ;; `org-export-get-coderef-format' returns an appropriate format
3985 ;; string for coderefs.
3987 ;; `org-export-inline-image-p' returns a non-nil value when the link
3988 ;; provided should be considered as an inline image.
3990 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3991 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3992 ;; returns an appropriate unique identifier when found, or nil.
3994 ;; `org-export-resolve-id-link' returns the first headline with
3995 ;; specified id or custom-id in parse tree, the path to the external
3996 ;; file with the id or nil when neither was found.
3998 ;; `org-export-resolve-coderef' associates a reference to a line
3999 ;; number in the element it belongs, or returns the reference itself
4000 ;; when the element isn't numbered.
4002 ;; `org-export-file-uri' expands a filename as stored in :path value
4003 ;; of a "file" link into a file URI.
4005 (defun org-export-custom-protocol-maybe (link desc backend
)
4006 "Try exporting LINK with a dedicated function.
4008 DESC is its description, as a string, or nil. BACKEND is the
4009 back-end used for export, as a symbol.
4011 Return output as a string, or nil if no protocol handles LINK.
4013 A custom protocol has precedence over regular back-end export.
4014 The function ignores links with an implicit type (e.g.,
4016 (let ((type (org-element-property :type link
)))
4017 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
4019 (let ((protocol (nth 2 (assoc type org-link-protocols
))))
4020 (and (functionp protocol
)
4022 (org-link-unescape (org-element-property :path link
))
4026 (defun org-export-get-coderef-format (path desc
)
4027 "Return format string for code reference link.
4028 PATH is the link path. DESC is its description."
4030 (cond ((not desc
) "%s")
4031 ((string-match (regexp-quote (concat "(" path
")")) desc
)
4032 (replace-match "%s" t t desc
))
4035 (defun org-export-inline-image-p (link &optional rules
)
4036 "Non-nil if LINK object points to an inline image.
4038 Optional argument is a set of RULES defining inline images. It
4039 is an alist where associations have the following shape:
4043 Applying a rule means apply REGEXP against LINK's path when its
4044 type is TYPE. The function will return a non-nil value if any of
4045 the provided rules is non-nil. The default rule is
4046 `org-export-default-inline-image-rule'.
4048 This only applies to links without a description."
4049 (and (not (org-element-contents link
))
4050 (let ((case-fold-search t
))
4052 (dolist (rule (or rules org-export-default-inline-image-rule
))
4053 (and (string= (org-element-property :type link
) (car rule
))
4054 (org-string-match-p (cdr rule
)
4055 (org-element-property :path link
))
4056 (throw 'exit t
)))))))
4058 (defun org-export-resolve-coderef (ref info
)
4059 "Resolve a code reference REF.
4061 INFO is a plist used as a communication channel.
4063 Return associated line number in source code, or REF itself,
4064 depending on src-block or example element's switches. Throw an
4065 error if no block contains REF."
4066 (or (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
4069 (insert (org-trim (org-element-property :value el
)))
4070 (let* ((label-fmt (regexp-quote
4071 (or (org-element-property :label-fmt el
)
4072 org-coderef-label-format
)))
4074 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
4075 (format label-fmt ref
))))
4076 ;; Element containing REF is found. Resolve it to
4077 ;; either a label or a line number, as needed.
4078 (when (re-search-backward ref-re nil t
)
4080 ((org-element-property :use-labels el
) ref
)
4081 ((eq (org-element-property :number-lines el
) 'continued
)
4082 (+ (org-export-get-loc el info
) (line-number-at-pos)))
4083 (t (line-number-at-pos)))))))
4085 (user-error "Unable to resolve code reference: %s" ref
)))
4087 (defun org-export-resolve-fuzzy-link (link info
)
4088 "Return LINK destination.
4090 INFO is a plist holding contextual information.
4092 Return value can be an object or an element:
4094 - If LINK path matches a target object (i.e. <<path>>) return it.
4096 - If LINK path exactly matches the name affiliated keyword
4097 \(i.e. #+NAME: path) of an element, return that element.
4099 - If LINK path exactly matches any headline name, return that
4102 - Otherwise, throw an error.
4104 Assume LINK type is \"fuzzy\". White spaces are not
4106 (let* ((raw-path (org-link-unescape (org-element-property :path link
)))
4107 (headline-only (eq (string-to-char raw-path
) ?
*))
4108 ;; Split PATH at white spaces so matches are space
4110 (path (org-split-string
4111 (if headline-only
(substring raw-path
1) raw-path
)))
4113 (or (plist-get info
:resolve-fuzzy-link-cache
)
4114 (plist-get (plist-put info
4115 :resolve-fuzzy-link-cache
4116 (make-hash-table :test
#'equal
))
4117 :resolve-fuzzy-link-cache
)))
4118 (cached (gethash path link-cache
'not-found
)))
4119 (if (not (eq cached
'not-found
)) cached
4120 (let ((ast (plist-get info
:parse-tree
)))
4124 ;; First try to find a matching "<<path>>" unless user
4125 ;; specified he was looking for a headline (path starts with
4126 ;; a "*" character).
4127 ((and (not headline-only
)
4128 (org-element-map ast
'target
4130 (and (equal (org-split-string
4131 (org-element-property :value datum
))
4134 info
'first-match
)))
4135 ;; Then try to find an element with a matching "#+NAME: path"
4136 ;; affiliated keyword.
4137 ((and (not headline-only
)
4138 (org-element-map ast org-element-all-elements
4140 (let ((name (org-element-property :name datum
)))
4141 (and name
(equal (org-split-string name
) path
) datum
)))
4142 info
'first-match
)))
4143 ;; Try to find a matching headline.
4144 ((org-element-map ast
'headline
4146 (and (equal (org-split-string
4147 (replace-regexp-in-string
4148 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4149 (org-element-property :raw-value h
)))
4153 (t (user-error "Unable to resolve link \"%s\"" raw-path
)))
4156 (defun org-export-resolve-id-link (link info
)
4157 "Return headline referenced as LINK destination.
4159 INFO is a plist used as a communication channel.
4161 Return value can be the headline element matched in current parse
4162 tree or a file name. Assume LINK type is either \"id\" or
4163 \"custom-id\". Throw an error if no match is found."
4164 (let ((id (org-element-property :path link
)))
4165 ;; First check if id is within the current parse tree.
4166 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4168 (when (or (equal (org-element-property :ID headline
) id
)
4169 (equal (org-element-property :CUSTOM_ID headline
) id
))
4172 ;; Otherwise, look for external files.
4173 (cdr (assoc id
(plist-get info
:id-alist
)))
4174 (user-error "Unable to resolve ID \"%s\"" id
))))
4176 (defun org-export-resolve-radio-link (link info
)
4177 "Return radio-target object referenced as LINK destination.
4179 INFO is a plist used as a communication channel.
4181 Return value can be a radio-target object or nil. Assume LINK
4182 has type \"radio\"."
4183 (let ((path (replace-regexp-in-string
4184 "[ \r\t\n]+" " " (org-element-property :path link
))))
4185 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4187 (and (eq (compare-strings
4188 (replace-regexp-in-string
4189 "[ \r\t\n]+" " " (org-element-property :value radio
))
4190 nil nil path nil nil t
)
4193 info
'first-match
)))
4195 (defun org-export-file-uri (filename)
4196 "Return file URI associated to FILENAME."
4197 (if (not (file-name-absolute-p filename
)) filename
4199 (and (not (org-file-remote-p filename
)) "/")
4200 (if (org-string-match-p "\\`~" filename
)
4201 (expand-file-name filename
)
4207 ;; `org-export-get-reference' associate a unique reference for any
4208 ;; object or element.
4210 ;; `org-export-get-ordinal' associates a sequence number to any object
4213 (defun org-export-get-reference (datum info
)
4214 "Return a unique reference for DATUM, as a string.
4215 DATUM is either an element or an object. INFO is the current
4216 export state, as a plist. Returned reference consists of
4217 alphanumeric characters only."
4218 (let ((type (org-element-type datum
))
4219 (cache (or (plist-get info
:internal-references
)
4220 (let ((h (make-hash-table :test
#'eq
)))
4221 (plist-put info
:internal-references h
)
4223 (or (gethash datum cache
)
4227 (replace-regexp-in-string "-" "" (symbol-name type
))
4229 (cl-incf (gethash type cache
0)))
4232 (defun org-export-get-ordinal (element info
&optional types predicate
)
4233 "Return ordinal number of an element or object.
4235 ELEMENT is the element or object considered. INFO is the plist
4236 used as a communication channel.
4238 Optional argument TYPES, when non-nil, is a list of element or
4239 object types, as symbols, that should also be counted in.
4240 Otherwise, only provided element's type is considered.
4242 Optional argument PREDICATE is a function returning a non-nil
4243 value if the current element or object should be counted in. It
4244 accepts two arguments: the element or object being considered and
4245 the plist used as a communication channel. This allows to count
4246 only a certain type of objects (i.e. inline images).
4248 Return value is a list of numbers if ELEMENT is a headline or an
4249 item. It is nil for keywords. It represents the footnote number
4250 for footnote definitions and footnote references. If ELEMENT is
4251 a target, return the same value as if ELEMENT was the closest
4252 table, item or headline containing the target. In any other
4253 case, return the sequence number of ELEMENT among elements or
4254 objects of the same type."
4255 ;; Ordinal of a target object refer to the ordinal of the closest
4256 ;; table, item, or headline containing the object.
4257 (when (eq (org-element-type element
) 'target
)
4259 (org-element-lineage
4261 '(footnote-definition footnote-reference headline item table
))))
4262 (cl-case (org-element-type element
)
4263 ;; Special case 1: A headline returns its number as a list.
4264 (headline (org-export-get-headline-number element info
))
4265 ;; Special case 2: An item returns its number as a list.
4266 (item (let ((struct (org-element-property :structure element
)))
4267 (org-list-get-item-number
4268 (org-element-property :begin element
)
4270 (org-list-prevs-alist struct
)
4271 (org-list-parents-alist struct
))))
4272 ((footnote-definition footnote-reference
)
4273 (org-export-get-footnote-number element info
))
4276 ;; Increment counter until ELEMENT is found again.
4277 (org-element-map (plist-get info
:parse-tree
)
4278 (or types
(org-element-type element
))
4281 ((eq element el
) (1+ counter
))
4282 ((not predicate
) (cl-incf counter
) nil
)
4283 ((funcall predicate el info
) (cl-incf counter
) nil
)))
4284 info
'first-match
)))))
4289 ;; `org-export-get-loc' counts number of code lines accumulated in
4290 ;; src-block or example-block elements with a "+n" switch until
4291 ;; a given element, excluded. Note: "-n" switches reset that count.
4293 ;; `org-export-unravel-code' extracts source code (along with a code
4294 ;; references alist) from an `element-block' or `src-block' type
4297 ;; `org-export-format-code' applies a formatting function to each line
4298 ;; of code, providing relative line number and code reference when
4299 ;; appropriate. Since it doesn't access the original element from
4300 ;; which the source code is coming, it expects from the code calling
4301 ;; it to know if lines should be numbered and if code references
4304 ;; Eventually, `org-export-format-code-default' is a higher-level
4305 ;; function (it makes use of the two previous functions) which handles
4306 ;; line numbering and code references inclusion, and returns source
4307 ;; code in a format suitable for plain text or verbatim output.
4309 (defun org-export-get-loc (element info
)
4310 "Return accumulated lines of code up to ELEMENT.
4312 INFO is the plist used as a communication channel.
4314 ELEMENT is excluded from count."
4316 (org-element-map (plist-get info
:parse-tree
)
4317 `(src-block example-block
,(org-element-type element
))
4320 ;; ELEMENT is reached: Quit the loop.
4322 ;; Only count lines from src-block and example-block elements
4323 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4324 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4325 ((let ((linums (org-element-property :number-lines el
)))
4327 ;; Accumulate locs or reset them.
4328 (let ((lines (org-count-lines
4329 (org-trim (org-element-property :value el
)))))
4330 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4331 ;; Return nil to stay in the loop.
4337 (defun org-export-unravel-code (element)
4338 "Clean source code and extract references out of it.
4340 ELEMENT has either a `src-block' an `example-block' type.
4342 Return a cons cell whose CAR is the source code, cleaned from any
4343 reference, protective commas and spurious indentation, and CDR is
4344 an alist between relative line number (integer) and name of code
4345 reference on that line (string)."
4346 (let* ((line 0) refs
4347 (value (org-element-property :value element
))
4348 ;; Get code and clean it. Remove blank lines at its
4349 ;; beginning and end.
4350 (code (replace-regexp-in-string
4351 "\\`\\([ \t]*\n\\)+" ""
4352 (replace-regexp-in-string
4353 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4354 (if (or org-src-preserve-indentation
4355 (org-element-property :preserve-indent element
))
4357 (org-element-remove-indentation value
)))))
4358 ;; Get format used for references.
4359 (label-fmt (regexp-quote
4360 (or (org-element-property :label-fmt element
)
4361 org-coderef-label-format
)))
4362 ;; Build a regexp matching a loc with a reference.
4364 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4365 (replace-regexp-in-string
4366 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4369 ;; Code with references removed.
4370 (org-element-normalize-string
4374 (if (not (string-match with-ref-re loc
)) loc
4375 ;; Ref line: remove ref, and signal its position in REFS.
4376 (push (cons line
(match-string 3 loc
)) refs
)
4377 (replace-match "" nil nil loc
1)))
4378 (org-split-string code
"\n") "\n"))
4382 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4383 "Format CODE by applying FUN line-wise and return it.
4385 CODE is a string representing the code to format. FUN is
4386 a function. It must accept three arguments: a line of
4387 code (string), the current line number (integer) or nil and the
4388 reference associated to the current line (string) or nil.
4390 Optional argument NUM-LINES can be an integer representing the
4391 number of code lines accumulated until the current code. Line
4392 numbers passed to FUN will take it into account. If it is nil,
4393 FUN's second argument will always be nil. This number can be
4394 obtained with `org-export-get-loc' function.
4396 Optional argument REF-ALIST can be an alist between relative line
4397 number (i.e. ignoring NUM-LINES) and the name of the code
4398 reference on it. If it is nil, FUN's third argument will always
4399 be nil. It can be obtained through the use of
4400 `org-export-unravel-code' function."
4401 (let ((--locs (org-split-string code
"\n"))
4403 (org-element-normalize-string
4407 (let ((--ref (cdr (assq --line ref-alist
))))
4408 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4411 (defun org-export-format-code-default (element info
)
4412 "Return source code from ELEMENT, formatted in a standard way.
4414 ELEMENT is either a `src-block' or `example-block' element. INFO
4415 is a plist used as a communication channel.
4417 This function takes care of line numbering and code references
4418 inclusion. Line numbers, when applicable, appear at the
4419 beginning of the line, separated from the code by two white
4420 spaces. Code references, on the other hand, appear flushed to
4421 the right, separated by six white spaces from the widest line of
4423 ;; Extract code and references.
4424 (let* ((code-info (org-export-unravel-code element
))
4425 (code (car code-info
))
4426 (code-lines (org-split-string code
"\n")))
4427 (if (null code-lines
) ""
4428 (let* ((refs (and (org-element-property :retain-labels element
)
4430 ;; Handle line numbering.
4431 (num-start (cl-case (org-element-property :number-lines element
)
4432 (continued (org-export-get-loc element info
))
4437 (length (number-to-string
4438 (+ (length code-lines
) num-start
))))))
4439 ;; Prepare references display, if required. Any reference
4440 ;; should start six columns after the widest line of code,
4441 ;; wrapped with parenthesis.
4443 (+ (apply 'max
(mapcar 'length code-lines
))
4444 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4445 (org-export-format-code
4447 (lambda (loc line-num ref
)
4448 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4453 (concat (make-string
4455 (+ (length loc
) (length number-str
))) ?
)
4456 (format "(%s)" ref
))))))
4462 ;; `org-export-table-has-special-column-p' and and
4463 ;; `org-export-table-row-is-special-p' are predicates used to look for
4464 ;; meta-information about the table structure.
4466 ;; `org-table-has-header-p' tells when the rows before the first rule
4467 ;; should be considered as table's header.
4469 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4470 ;; and `org-export-table-cell-borders' extract information from
4471 ;; a table-cell element.
4473 ;; `org-export-table-dimensions' gives the number on rows and columns
4474 ;; in the table, ignoring horizontal rules and special columns.
4475 ;; `org-export-table-cell-address', given a table-cell object, returns
4476 ;; the absolute address of a cell. On the other hand,
4477 ;; `org-export-get-table-cell-at' does the contrary.
4479 ;; `org-export-table-cell-starts-colgroup-p',
4480 ;; `org-export-table-cell-ends-colgroup-p',
4481 ;; `org-export-table-row-starts-rowgroup-p',
4482 ;; `org-export-table-row-ends-rowgroup-p',
4483 ;; `org-export-table-row-starts-header-p',
4484 ;; `org-export-table-row-ends-header-p' and
4485 ;; `org-export-table-row-in-header-p' indicate position of current row
4486 ;; or cell within the table.
4488 (defun org-export-table-has-special-column-p (table)
4489 "Non-nil when TABLE has a special column.
4490 All special columns will be ignored during export."
4491 ;; The table has a special column when every first cell of every row
4492 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4493 ;; "*" "_" and "^". Though, do not consider a first row containing
4494 ;; only empty cells as special.
4495 (let ((special-column-p 'empty
))
4499 (when (eq (org-element-property :type row
) 'standard
)
4500 (let ((value (org-element-contents
4501 (car (org-element-contents row
)))))
4502 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4503 (setq special-column-p
'special
))
4505 (t (throw 'exit nil
))))))
4506 (org-element-contents table
))
4507 (eq special-column-p
'special
))))
4509 (defun org-export-table-has-header-p (table info
)
4510 "Non-nil when TABLE has a header.
4512 INFO is a plist used as a communication channel.
4514 A table has a header when it contains at least two row groups."
4515 (let ((cache (or (plist-get info
:table-header-cache
)
4516 (plist-get (setq info
4517 (plist-put info
:table-header-cache
4518 (make-hash-table :test
'eq
)))
4519 :table-header-cache
))))
4520 (or (gethash table cache
)
4521 (let ((rowgroup 1) row-flag
)
4524 (org-element-map table
'table-row
4528 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4529 (cl-incf rowgroup
) (setq row-flag nil
))
4530 ((and (not row-flag
) (eq (org-element-property :type row
)
4532 (setq row-flag t
) nil
)))
4536 (defun org-export-table-row-is-special-p (table-row _
)
4537 "Non-nil if TABLE-ROW is considered special.
4538 All special rows will be ignored during export."
4539 (when (eq (org-element-property :type table-row
) 'standard
)
4540 (let ((first-cell (org-element-contents
4541 (car (org-element-contents table-row
)))))
4542 ;; A row is special either when...
4544 ;; ... it starts with a field only containing "/",
4545 (equal first-cell
'("/"))
4546 ;; ... the table contains a special column and the row start
4547 ;; with a marking character among, "^", "_", "$" or "!",
4548 (and (org-export-table-has-special-column-p
4549 (org-export-get-parent table-row
))
4550 (member first-cell
'(("^") ("_") ("$") ("!"))))
4551 ;; ... it contains only alignment cookies and empty cells.
4552 (let ((special-row-p 'empty
))
4556 (let ((value (org-element-contents cell
)))
4557 ;; Since VALUE is a secondary string, the following
4558 ;; checks avoid expanding it with `org-export-data'.
4560 ((and (not (cdr value
))
4561 (stringp (car value
))
4562 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4564 (setq special-row-p
'cookie
))
4565 (t (throw 'exit nil
)))))
4566 (org-element-contents table-row
))
4567 (eq special-row-p
'cookie
)))))))
4569 (defun org-export-table-row-group (table-row info
)
4570 "Return TABLE-ROW's group number, as an integer.
4572 INFO is a plist used as the communication channel.
4574 Return value is the group number, as an integer, or nil for
4575 special rows and rows separators. First group is also table's
4577 (let ((cache (or (plist-get info
:table-row-group-cache
)
4578 (plist-get (setq info
4579 (plist-put info
:table-row-group-cache
4580 (make-hash-table :test
'eq
)))
4581 :table-row-group-cache
))))
4582 (cond ((gethash table-row cache
))
4583 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4584 (t (let ((group 0) row-flag
)
4585 (org-element-map (org-export-get-parent table-row
) 'table-row
4587 (if (eq (org-element-property :type row
) 'rule
)
4589 (unless row-flag
(cl-incf group
) (setq row-flag t
)))
4590 (when (eq table-row row
) (puthash table-row group cache
)))
4591 info
'first-match
))))))
4593 (defun org-export-table-cell-width (table-cell info
)
4594 "Return TABLE-CELL contents width.
4596 INFO is a plist used as the communication channel.
4598 Return value is the width given by the last width cookie in the
4599 same column as TABLE-CELL, or nil."
4600 (let* ((row (org-export-get-parent table-cell
))
4601 (table (org-export-get-parent row
))
4602 (cells (org-element-contents row
))
4603 (columns (length cells
))
4604 (column (- columns
(length (memq table-cell cells
))))
4605 (cache (or (plist-get info
:table-cell-width-cache
)
4606 (plist-get (setq info
4607 (plist-put info
:table-cell-width-cache
4608 (make-hash-table :test
'eq
)))
4609 :table-cell-width-cache
)))
4610 (width-vector (or (gethash table cache
)
4611 (puthash table
(make-vector columns
'empty
) cache
)))
4612 (value (aref width-vector column
)))
4613 (if (not (eq value
'empty
)) value
4615 (dolist (row (org-element-contents table
)
4616 (aset width-vector column cookie-width
))
4617 (when (org-export-table-row-is-special-p row info
)
4618 ;; In a special row, try to find a width cookie at COLUMN.
4619 (let* ((value (org-element-contents
4620 (elt (org-element-contents row
) column
)))
4621 (cookie (car value
)))
4622 ;; The following checks avoid expanding unnecessarily
4623 ;; the cell with `org-export-data'.
4627 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4628 (match-string 1 cookie
))
4630 (string-to-number (match-string 1 cookie
)))))))))))
4632 (defun org-export-table-cell-alignment (table-cell info
)
4633 "Return TABLE-CELL contents alignment.
4635 INFO is a plist used as the communication channel.
4637 Return alignment as specified by the last alignment cookie in the
4638 same column as TABLE-CELL. If no such cookie is found, a default
4639 alignment value will be deduced from fraction of numbers in the
4640 column (see `org-table-number-fraction' for more information).
4641 Possible values are `left', `right' and `center'."
4642 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4643 (require 'org-table
)
4644 (let* ((row (org-export-get-parent table-cell
))
4645 (table (org-export-get-parent row
))
4646 (cells (org-element-contents row
))
4647 (columns (length cells
))
4648 (column (- columns
(length (memq table-cell cells
))))
4649 (cache (or (plist-get info
:table-cell-alignment-cache
)
4650 (plist-get (setq info
4651 (plist-put info
:table-cell-alignment-cache
4652 (make-hash-table :test
'eq
)))
4653 :table-cell-alignment-cache
)))
4654 (align-vector (or (gethash table cache
)
4655 (puthash table
(make-vector columns nil
) cache
))))
4656 (or (aref align-vector column
)
4657 (let ((number-cells 0)
4660 previous-cell-number-p
)
4661 (dolist (row (org-element-contents (org-export-get-parent row
)))
4663 ;; In a special row, try to find an alignment cookie at
4665 ((org-export-table-row-is-special-p row info
)
4666 (let ((value (org-element-contents
4667 (elt (org-element-contents row
) column
))))
4668 ;; Since VALUE is a secondary string, the following
4669 ;; checks avoid useless expansion through
4670 ;; `org-export-data'.
4673 (stringp (car value
))
4674 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4676 (match-string 1 (car value
)))
4677 (setq cookie-align
(match-string 1 (car value
))))))
4678 ;; Ignore table rules.
4679 ((eq (org-element-property :type row
) 'rule
))
4680 ;; In a standard row, check if cell's contents are
4681 ;; expressing some kind of number. Increase NUMBER-CELLS
4682 ;; accordingly. Though, don't bother if an alignment
4683 ;; cookie has already defined cell's alignment.
4685 (let ((value (org-export-data
4686 (org-element-contents
4687 (elt (org-element-contents row
) column
))
4689 (cl-incf total-cells
)
4690 ;; Treat an empty cell as a number if it follows
4692 (if (not (or (string-match org-table-number-regexp value
)
4693 (and (string= value
"") previous-cell-number-p
)))
4694 (setq previous-cell-number-p nil
)
4695 (setq previous-cell-number-p t
)
4696 (cl-incf number-cells
))))))
4697 ;; Return value. Alignment specified by cookies has
4698 ;; precedence over alignment deduced from cell's contents.
4701 (cond ((equal cookie-align
"l") 'left
)
4702 ((equal cookie-align
"r") 'right
)
4703 ((equal cookie-align
"c") 'center
)
4704 ((>= (/ (float number-cells
) total-cells
)
4705 org-table-number-fraction
)
4709 (defun org-export-table-cell-borders (table-cell info
)
4710 "Return TABLE-CELL borders.
4712 INFO is a plist used as a communication channel.
4714 Return value is a list of symbols, or nil. Possible values are:
4715 `top', `bottom', `above', `below', `left' and `right'. Note:
4716 `top' (resp. `bottom') only happen for a cell in the first
4717 row (resp. last row) of the table, ignoring table rules, if any.
4719 Returned borders ignore special rows."
4720 (let* ((row (org-export-get-parent table-cell
))
4721 (table (org-export-get-parent-table table-cell
))
4723 ;; Top/above border? TABLE-CELL has a border above when a rule
4724 ;; used to demarcate row groups can be found above. Hence,
4725 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4726 ;; another regular row has to be found above that rule.
4730 (cond ((eq (org-element-property :type row
) 'rule
)
4732 ((not (org-export-table-row-is-special-p row info
))
4733 (if rule-flag
(throw 'exit
(push 'above borders
))
4734 (throw 'exit nil
)))))
4735 ;; Look at every row before the current one.
4736 (cdr (memq row
(reverse (org-element-contents table
)))))
4737 ;; No rule above, or rule found starts the table (ignoring any
4738 ;; special row): TABLE-CELL is at the top of the table.
4739 (when rule-flag
(push 'above borders
))
4740 (push 'top borders
)))
4741 ;; Bottom/below border? TABLE-CELL has a border below when next
4742 ;; non-regular row below is a rule.
4746 (cond ((eq (org-element-property :type row
) 'rule
)
4748 ((not (org-export-table-row-is-special-p row info
))
4749 (if rule-flag
(throw 'exit
(push 'below borders
))
4750 (throw 'exit nil
)))))
4751 ;; Look at every row after the current one.
4752 (cdr (memq row
(org-element-contents table
))))
4753 ;; No rule below, or rule found ends the table (modulo some
4754 ;; special row): TABLE-CELL is at the bottom of the table.
4755 (when rule-flag
(push 'below borders
))
4756 (push 'bottom borders
)))
4757 ;; Right/left borders? They can only be specified by column
4758 ;; groups. Column groups are defined in a row starting with "/".
4759 ;; Also a column groups row only contains "<", "<>", ">" or blank
4762 (let ((column (let ((cells (org-element-contents row
)))
4763 (- (length cells
) (length (memq table-cell cells
))))))
4766 (unless (eq (org-element-property :type row
) 'rule
)
4767 (when (equal (org-element-contents
4768 (car (org-element-contents row
)))
4770 (let ((column-groups
4773 (let ((value (org-element-contents cell
)))
4774 (when (member value
'(("<") ("<>") (">") nil
))
4776 (org-element-contents row
))))
4777 ;; There's a left border when previous cell, if
4778 ;; any, ends a group, or current one starts one.
4779 (when (or (and (not (zerop column
))
4780 (member (elt column-groups
(1- column
))
4782 (member (elt column-groups column
) '("<" "<>")))
4783 (push 'left borders
))
4784 ;; There's a right border when next cell, if any,
4785 ;; starts a group, or current one ends one.
4786 (when (or (and (/= (1+ column
) (length column-groups
))
4787 (member (elt column-groups
(1+ column
))
4789 (member (elt column-groups column
) '(">" "<>")))
4790 (push 'right borders
))
4791 (throw 'exit nil
)))))
4792 ;; Table rows are read in reverse order so last column groups
4793 ;; row has precedence over any previous one.
4794 (reverse (org-element-contents table
)))))
4798 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4799 "Non-nil when TABLE-CELL is at the beginning of a column group.
4800 INFO is a plist used as a communication channel."
4801 ;; A cell starts a column group either when it is at the beginning
4802 ;; of a row (or after the special column, if any) or when it has
4804 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4805 'identity info
'first-match
)
4807 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4809 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4810 "Non-nil when TABLE-CELL is at the end of a column group.
4811 INFO is a plist used as a communication channel."
4812 ;; A cell ends a column group either when it is at the end of a row
4813 ;; or when it has a right border.
4814 (or (eq (car (last (org-element-contents
4815 (org-export-get-parent table-cell
))))
4817 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4819 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4820 "Non-nil when TABLE-ROW is at the beginning of a row group.
4821 INFO is a plist used as a communication channel."
4822 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4823 (org-export-table-row-is-special-p table-row info
))
4824 (let ((borders (org-export-table-cell-borders
4825 (car (org-element-contents table-row
)) info
)))
4826 (or (memq 'top borders
) (memq 'above borders
)))))
4828 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4829 "Non-nil when TABLE-ROW is at the end of a row group.
4830 INFO is a plist used as a communication channel."
4831 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4832 (org-export-table-row-is-special-p table-row info
))
4833 (let ((borders (org-export-table-cell-borders
4834 (car (org-element-contents table-row
)) info
)))
4835 (or (memq 'bottom borders
) (memq 'below borders
)))))
4837 (defun org-export-table-row-in-header-p (table-row info
)
4838 "Non-nil when TABLE-ROW is located within table's header.
4839 INFO is a plist used as a communication channel. Always return
4840 nil for special rows and rows separators."
4841 (and (org-export-table-has-header-p
4842 (org-export-get-parent-table table-row
) info
)
4843 (eql (org-export-table-row-group table-row info
) 1)))
4845 (defun org-export-table-row-starts-header-p (table-row info
)
4846 "Non-nil when TABLE-ROW is the first table header's row.
4847 INFO is a plist used as a communication channel."
4848 (and (org-export-table-row-in-header-p table-row info
)
4849 (org-export-table-row-starts-rowgroup-p table-row info
)))
4851 (defun org-export-table-row-ends-header-p (table-row info
)
4852 "Non-nil when TABLE-ROW is the last table header's row.
4853 INFO is a plist used as a communication channel."
4854 (and (org-export-table-row-in-header-p table-row info
)
4855 (org-export-table-row-ends-rowgroup-p table-row info
)))
4857 (defun org-export-table-row-number (table-row info
)
4858 "Return TABLE-ROW number.
4859 INFO is a plist used as a communication channel. Return value is
4860 zero-based and ignores separators. The function returns nil for
4861 special columns and separators."
4862 (when (and (eq (org-element-property :type table-row
) 'standard
)
4863 (not (org-export-table-row-is-special-p table-row info
)))
4865 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4867 (cond ((eq row table-row
) number
)
4868 ((eq (org-element-property :type row
) 'standard
)
4869 (cl-incf number
) nil
)))
4870 info
'first-match
))))
4872 (defun org-export-table-dimensions (table info
)
4873 "Return TABLE dimensions.
4875 INFO is a plist used as a communication channel.
4877 Return value is a CONS like (ROWS . COLUMNS) where
4878 ROWS (resp. COLUMNS) is the number of exportable
4879 rows (resp. columns)."
4880 (let (first-row (columns 0) (rows 0))
4881 ;; Set number of rows, and extract first one.
4882 (org-element-map table
'table-row
4884 (when (eq (org-element-property :type row
) 'standard
)
4886 (unless first-row
(setq first-row row
)))) info
)
4887 ;; Set number of columns.
4888 (org-element-map first-row
'table-cell
(lambda (_) (cl-incf columns
)) info
)
4890 (cons rows columns
)))
4892 (defun org-export-table-cell-address (table-cell info
)
4893 "Return address of a regular TABLE-CELL object.
4895 TABLE-CELL is the cell considered. INFO is a plist used as
4896 a communication channel.
4898 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4899 zero-based index. Only exportable cells are considered. The
4900 function returns nil for other cells."
4901 (let* ((table-row (org-export-get-parent table-cell
))
4902 (row-number (org-export-table-row-number table-row info
)))
4905 (let ((col-count 0))
4906 (org-element-map table-row
'table-cell
4908 (if (eq cell table-cell
) col-count
(cl-incf col-count
) nil
))
4909 info
'first-match
))))))
4911 (defun org-export-get-table-cell-at (address table info
)
4912 "Return regular table-cell object at ADDRESS in TABLE.
4914 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4915 zero-based index. TABLE is a table type element. INFO is
4916 a plist used as a communication channel.
4918 If no table-cell, among exportable cells, is found at ADDRESS,
4920 (let ((column-pos (cdr address
)) (column-count 0))
4922 ;; Row at (car address) or nil.
4923 (let ((row-pos (car address
)) (row-count 0))
4924 (org-element-map table
'table-row
4926 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4927 ((= row-count row-pos
) row
)
4928 (t (cl-incf row-count
) nil
)))
4932 (if (= column-count column-pos
) cell
4933 (cl-incf column-count
) nil
))
4934 info
'first-match
)))
4937 ;;;; For Tables Of Contents
4939 ;; `org-export-collect-headlines' builds a list of all exportable
4940 ;; headline elements, maybe limited to a certain depth. One can then
4941 ;; easily parse it and transcode it.
4943 ;; Building lists of tables, figures or listings is quite similar.
4944 ;; Once the generic function `org-export-collect-elements' is defined,
4945 ;; `org-export-collect-tables', `org-export-collect-figures' and
4946 ;; `org-export-collect-listings' can be derived from it.
4948 (defun org-export-collect-headlines (info &optional n scope
)
4949 "Collect headlines in order to build a table of contents.
4951 INFO is a plist used as a communication channel.
4953 When optional argument N is an integer, it specifies the depth of
4954 the table of contents. Otherwise, it is set to the value of the
4955 last headline level. See `org-export-headline-levels' for more
4958 Optional argument SCOPE, when non-nil, is an element. If it is
4959 a headline, only children of SCOPE are collected. Otherwise,
4960 collect children of the headline containing provided element. If
4961 there is no such headline, collect all headlines. In any case,
4962 argument N becomes relative to the level of that headline.
4964 Return a list of all exportable headlines as parsed elements.
4965 Footnote sections are ignored."
4966 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
4967 ((eq (org-element-type scope
) 'headline
) scope
)
4968 ((org-export-get-parent-headline scope
))
4969 (t (plist-get info
:parse-tree
))))
4970 (limit (plist-get info
:headline-levels
))
4971 (n (if (not (wholenump n
)) limit
4972 (min (if (eq (org-element-type scope
) 'org-data
) n
4973 (+ (org-export-get-relative-level scope info
) n
))
4975 (org-element-map (org-element-contents scope
) 'headline
4977 (unless (org-element-property :footnote-section-p headline
)
4978 (let ((level (org-export-get-relative-level headline info
)))
4979 (and (<= level n
) headline
))))
4982 (defun org-export-collect-elements (type info
&optional predicate
)
4983 "Collect referenceable elements of a determined type.
4985 TYPE can be a symbol or a list of symbols specifying element
4986 types to search. Only elements with a caption are collected.
4988 INFO is a plist used as a communication channel.
4990 When non-nil, optional argument PREDICATE is a function accepting
4991 one argument, an element of type TYPE. It returns a non-nil
4992 value when that element should be collected.
4994 Return a list of all elements found, in order of appearance."
4995 (org-element-map (plist-get info
:parse-tree
) type
4997 (and (org-element-property :caption element
)
4998 (or (not predicate
) (funcall predicate element
))
5002 (defun org-export-collect-tables (info)
5003 "Build a list of tables.
5004 INFO is a plist used as a communication channel.
5006 Return a list of table elements with a caption."
5007 (org-export-collect-elements 'table info
))
5009 (defun org-export-collect-figures (info predicate
)
5010 "Build a list of figures.
5012 INFO is a plist used as a communication channel. PREDICATE is
5013 a function which accepts one argument: a paragraph element and
5014 whose return value is non-nil when that element should be
5017 A figure is a paragraph type element, with a caption, verifying
5018 PREDICATE. The latter has to be provided since a \"figure\" is
5019 a vague concept that may depend on back-end.
5021 Return a list of elements recognized as figures."
5022 (org-export-collect-elements 'paragraph info predicate
))
5024 (defun org-export-collect-listings (info)
5025 "Build a list of src blocks.
5027 INFO is a plist used as a communication channel.
5029 Return a list of src-block elements with a caption."
5030 (org-export-collect-elements 'src-block info
))
5035 ;; The main function for the smart quotes sub-system is
5036 ;; `org-export-activate-smart-quotes', which replaces every quote in
5037 ;; a given string from the parse tree with its "smart" counterpart.
5039 ;; Dictionary for smart quotes is stored in
5040 ;; `org-export-smart-quotes-alist'.
5042 (defconst org-export-smart-quotes-alist
5044 ;; one may use: »...«, "...", ›...‹, or '...'.
5045 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5046 ;; LaTeX quotes require Babel!
5047 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
5048 :texinfo
"@guillemetright{}")
5049 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
5050 :texinfo
"@guillemetleft{}")
5051 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
5052 :texinfo
"@guilsinglright{}")
5053 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
5054 :texinfo
"@guilsingleft{}")
5055 (apostrophe :utf-8
"’" :html
"’"))
5057 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
5058 :texinfo
"@quotedblbase{}")
5059 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
5060 :texinfo
"@quotedblleft{}")
5061 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
5062 :texinfo
"@quotesinglbase{}")
5063 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
5064 :texinfo
"@quoteleft{}")
5065 (apostrophe :utf-8
"’" :html
"’"))
5067 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5068 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5069 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5070 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5071 (apostrophe :utf-8
"’" :html
"’"))
5073 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5074 :texinfo
"@guillemetleft{}")
5075 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5076 :texinfo
"@guillemetright{}")
5077 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5078 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5079 (apostrophe :utf-8
"’" :html
"’"))
5081 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
5082 :texinfo
"@guillemetleft{}@tie{}")
5083 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5084 :texinfo
"@tie{}@guillemetright{}")
5085 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
5086 :texinfo
"@guillemetleft{}@tie{}")
5087 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5088 :texinfo
"@tie{}@guillemetright{}")
5089 (apostrophe :utf-8
"’" :html
"’"))
5091 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5092 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5093 :texinfo
"@guillemetleft{}")
5094 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5095 :texinfo
"@guillemetright{}")
5096 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5097 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5098 (apostrophe :utf-8
"’" :html
"’"))
5100 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5101 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5102 :texinfo
"@guillemetleft{}")
5103 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5104 :texinfo
"@guillemetright{}")
5105 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5106 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5107 (apostrophe :utf-8
"’" :html
"’"))
5109 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5110 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5111 :texinfo
"@guillemetleft{}")
5112 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5113 :texinfo
"@guillemetright{}")
5114 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5115 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5116 (apostrophe :utf-8
"’" :html
"’"))
5118 ;; 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
5119 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5120 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
5121 :texinfo
"@guillemetleft{}")
5122 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
5123 :texinfo
"@guillemetright{}")
5124 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
5125 :texinfo
"@quotedblbase{}")
5126 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
5127 :texinfo
"@quotedblleft{}")
5128 (apostrophe :utf-8
"’" :html
: "'"))
5130 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5131 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5132 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5133 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5134 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5135 (apostrophe :utf-8
"’" :html
"’"))
5137 "Smart quotes translations.
5139 Alist whose CAR is a language string and CDR is an alist with
5140 quote type as key and a plist associating various encodings to
5141 their translation as value.
5143 A quote type can be any symbol among `opening-double-quote',
5144 `closing-double-quote', `opening-single-quote',
5145 `closing-single-quote' and `apostrophe'.
5147 Valid encodings include `:utf-8', `:html', `:latex' and
5150 If no translation is found, the quote character is left as-is.")
5152 (defun org-export--smart-quote-status (s info
)
5153 "Return smart quote status at the beginning of string S.
5154 INFO is the current export state, as a plist."
5155 (let* ((parent (org-element-property :parent s
))
5156 (cache (or (plist-get info
:smart-quote-cache
)
5157 (let ((table (make-hash-table :test
#'eq
)))
5158 (plist-put info
:smart-quote-cache table
)
5160 (value (gethash parent cache
'missing-data
)))
5161 (if (not (eq value
'missing-data
)) (cdr (assq s value
))
5162 (let (level1-open full-status
)
5163 (org-element-map parent
'plain-text
5165 (let ((start 0) current-status
)
5166 (while (setq start
(string-match "['\"]" text start
))
5169 ((equal (match-string 0 text
) "\"")
5170 (setf level1-open
(not level1-open
))
5171 (if level1-open
'opening-double-quote
'closing-double-quote
))
5172 ;; Not already in a level 1 quote: this is an
5174 ((not level1-open
) 'apostrophe
)
5175 ;; Extract previous char and next char. As
5176 ;; a special case, they can also be set to `blank',
5177 ;; `no-blank' or nil. Then determine if current
5178 ;; match is allowed as an opening quote or a closing
5182 (if (> start
0) (substring text
(1- start
) start
)
5183 (let ((p (org-export-get-previous-element
5186 ((stringp p
) (substring p
(1- (length p
))))
5187 ((memq (org-element-property :post-blank p
)
5192 (if (< (1+ start
) (length text
))
5193 (substring text
(1+ start
) (+ start
2))
5194 (let ((n (org-export-get-next-element text info
)))
5196 ((stringp n
) (substring n
0 1))
5199 (and (if (stringp previous
)
5200 (string-match "\\s\"\\|\\s-\\|\\s("
5202 (memq previous
'(blank nil
)))
5204 (string-match "\\w\\|\\s.\\|\\s_" next
)
5205 (eq next
'no-blank
))))
5207 (and (if (stringp previous
)
5208 (string-match "\\w\\|\\s.\\|\\s_" previous
)
5209 (eq previous
'no-blank
))
5211 (string-match "\\s-\\|\\s)\\|\\s.\\|\\s\""
5213 (memq next
'(blank nil
))))))
5215 ((and allow-open allow-close
) (error "Should not happen"))
5216 (allow-open 'opening-single-quote
)
5217 (allow-close 'closing-single-quote
)
5221 (when current-status
5222 (push (cons text
(nreverse current-status
)) full-status
))))
5223 info nil org-element-recursive-objects
)
5224 (puthash parent full-status cache
)
5225 (cdr (assq s full-status
))))))
5227 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5228 "Replace regular quotes with \"smart\" quotes in string S.
5230 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5231 `:utf-8'. INFO is a plist used as a communication channel.
5233 The function has to retrieve information about string
5234 surroundings in parse tree. It can only happen with an
5235 unmodified string. Thus, if S has already been through another
5236 process, a non-nil ORIGINAL optional argument will provide that
5239 Return the new string."
5241 (copy-sequence (org-export--smart-quote-status (or original s
) info
))))
5242 (replace-regexp-in-string
5246 (cdr (assq (pop quote-status
)
5247 (cdr (assoc (plist-get info
:language
)
5248 org-export-smart-quotes-alist
))))
5255 ;; Here are various functions to retrieve information about the
5256 ;; neighborhood of a given element or object. Neighbors of interest
5257 ;; are direct parent (`org-export-get-parent'), parent headline
5258 ;; (`org-export-get-parent-headline'), first element containing an
5259 ;; object, (`org-export-get-parent-element'), parent table
5260 ;; (`org-export-get-parent-table'), previous element or object
5261 ;; (`org-export-get-previous-element') and next element or object
5262 ;; (`org-export-get-next-element').
5264 ;; defsubst org-export-get-parent must be defined before first use
5266 (define-obsolete-function-alias
5267 'org-export-get-genealogy
'org-element-lineage
"25.1")
5269 (defun org-export-get-parent-headline (blob)
5270 "Return BLOB parent headline or nil.
5271 BLOB is the element or object being considered."
5272 (org-element-lineage blob
'(headline)))
5274 (defun org-export-get-parent-element (object)
5275 "Return first element containing OBJECT or nil.
5276 OBJECT is the object to consider."
5277 (org-element-lineage object org-element-all-elements
))
5279 (defun org-export-get-parent-table (object)
5280 "Return OBJECT parent table or nil.
5281 OBJECT is either a `table-cell' or `table-element' type object."
5282 (org-element-lineage object
'(table)))
5284 (defun org-export-get-previous-element (blob info
&optional n
)
5285 "Return previous element or object.
5287 BLOB is an element or object. INFO is a plist used as
5288 a communication channel. Return previous exportable element or
5289 object, a string, or nil.
5291 When optional argument N is a positive integer, return a list
5292 containing up to N siblings before BLOB, from farthest to
5293 closest. With any other non-nil value, return a list containing
5295 (let* ((secondary (org-element-secondary-p blob
))
5296 (parent (org-export-get-parent blob
))
5298 (if secondary
(org-element-property secondary parent
)
5299 (org-element-contents parent
)))
5302 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5303 (cond ((memq obj
(plist-get info
:ignore-list
)))
5304 ((null n
) (throw 'exit obj
))
5305 ((not (wholenump n
)) (push obj prev
))
5306 ((zerop n
) (throw 'exit prev
))
5307 (t (cl-decf n
) (push obj prev
)))))))
5309 (defun org-export-get-next-element (blob info
&optional n
)
5310 "Return next element or object.
5312 BLOB is an element or object. INFO is a plist used as
5313 a communication channel. Return next exportable element or
5314 object, a string, or nil.
5316 When optional argument N is a positive integer, return a list
5317 containing up to N siblings after BLOB, from closest to farthest.
5318 With any other non-nil value, return a list containing all of
5320 (let* ((secondary (org-element-secondary-p blob
))
5321 (parent (org-export-get-parent blob
))
5324 (if secondary
(org-element-property secondary parent
)
5325 (org-element-contents parent
)))))
5328 (dolist (obj siblings
(nreverse next
))
5329 (cond ((memq obj
(plist-get info
:ignore-list
)))
5330 ((null n
) (throw 'exit obj
))
5331 ((not (wholenump n
)) (push obj next
))
5332 ((zerop n
) (throw 'exit
(nreverse next
)))
5333 (t (cl-decf n
) (push obj next
)))))))
5338 ;; `org-export-translate' translates a string according to the language
5339 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5340 ;; the dictionary used for the translation.
5342 (defconst org-export-dictionary
5344 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5346 ("ca" :default
"Autor")
5347 ("cs" :default
"Autor")
5348 ("da" :default
"Forfatter")
5349 ("de" :default
"Autor")
5350 ("eo" :html
"Aŭtoro")
5351 ("es" :default
"Autor")
5352 ("et" :default
"Autor")
5353 ("fi" :html
"Tekijä")
5354 ("fr" :default
"Auteur")
5355 ("hu" :default
"Szerzõ")
5356 ("is" :html
"Höfundur")
5357 ("it" :default
"Autore")
5358 ("ja" :default
"著者" :html
"著者")
5359 ("nl" :default
"Auteur")
5360 ("no" :default
"Forfatter")
5361 ("nb" :default
"Forfatter")
5362 ("nn" :default
"Forfattar")
5363 ("pl" :default
"Autor")
5364 ("pt_BR" :default
"Autor")
5365 ("ru" :html
"Автор" :utf-8
"Автор")
5366 ("sv" :html
"Författare")
5367 ("uk" :html
"Автор" :utf-8
"Автор")
5368 ("zh-CN" :html
"作者" :utf-8
"作者")
5369 ("zh-TW" :html
"作者" :utf-8
"作者"))
5370 ("Continued from previous page"
5371 ("de" :default
"Fortsetzung von vorheriger Seite")
5372 ("es" :html
"Continúa de la página anterior" :ascii
"Continua de la pagina anterior" :default
"Continúa de la página anterior")
5373 ("fr" :default
"Suite de la page précédente")
5374 ("it" :default
"Continua da pagina precedente")
5375 ("ja" :default
"前ページからの続き")
5376 ("nl" :default
"Vervolg van vorige pagina")
5377 ("pt" :default
"Continuação da página anterior")
5378 ("ru" :html
"(Продолжение)"
5379 :utf-8
"(Продолжение)"))
5380 ("Continued on next page"
5381 ("de" :default
"Fortsetzung nächste Seite")
5382 ("es" :html
"Continúa en la siguiente página" :ascii
"Continua en la siguiente pagina" :default
"Continúa en la siguiente página")
5383 ("fr" :default
"Suite page suivante")
5384 ("it" :default
"Continua alla pagina successiva")
5385 ("ja" :default
"次ページに続く")
5386 ("nl" :default
"Vervolg op volgende pagina")
5387 ("pt" :default
"Continua na página seguinte")
5388 ("ru" :html
"(Продолжение следует)"
5389 :utf-8
"(Продолжение следует)"))
5391 ("ca" :default
"Data")
5392 ("cs" :default
"Datum")
5393 ("da" :default
"Dato")
5394 ("de" :default
"Datum")
5395 ("eo" :default
"Dato")
5396 ("es" :default
"Fecha")
5397 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5398 ("fi" :html
"Päivämäärä")
5399 ("hu" :html
"Dátum")
5400 ("is" :default
"Dagsetning")
5401 ("it" :default
"Data")
5402 ("ja" :default
"日付" :html
"日付")
5403 ("nl" :default
"Datum")
5404 ("no" :default
"Dato")
5405 ("nb" :default
"Dato")
5406 ("nn" :default
"Dato")
5407 ("pl" :default
"Data")
5408 ("pt_BR" :default
"Data")
5409 ("ru" :html
"Дата" :utf-8
"Дата")
5410 ("sv" :default
"Datum")
5411 ("uk" :html
"Дата" :utf-8
"Дата")
5412 ("zh-CN" :html
"日期" :utf-8
"日期")
5413 ("zh-TW" :html
"日期" :utf-8
"日期"))
5415 ("da" :default
"Ligning")
5416 ("de" :default
"Gleichung")
5417 ("es" :ascii
"Ecuacion" :html
"Ecuación" :default
"Ecuación")
5418 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5419 ("fr" :ascii
"Equation" :default
"Équation")
5420 ("ja" :default
"方程式")
5421 ("no" :default
"Ligning")
5422 ("nb" :default
"Ligning")
5423 ("nn" :default
"Likning")
5424 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5425 ("ru" :html
"Уравнение"
5427 ("sv" :default
"Ekvation")
5428 ("zh-CN" :html
"方程" :utf-8
"方程"))
5430 ("da" :default
"Figur")
5431 ("de" :default
"Abbildung")
5432 ("es" :default
"Figura")
5433 ("et" :default
"Joonis")
5434 ("ja" :default
"図" :html
"図")
5435 ("no" :default
"Illustrasjon")
5436 ("nb" :default
"Illustrasjon")
5437 ("nn" :default
"Illustrasjon")
5438 ("pt_BR" :default
"Figura")
5439 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5440 ("sv" :default
"Illustration")
5441 ("zh-CN" :html
"图" :utf-8
"图"))
5443 ("da" :default
"Figur %d")
5444 ("de" :default
"Abbildung %d:")
5445 ("es" :default
"Figura %d:")
5446 ("et" :default
"Joonis %d:")
5447 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5448 ("ja" :default
"図%d: " :html
"図%d: ")
5449 ("no" :default
"Illustrasjon %d")
5450 ("nb" :default
"Illustrasjon %d")
5451 ("nn" :default
"Illustrasjon %d")
5452 ("pt_BR" :default
"Figura %d:")
5453 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5454 ("sv" :default
"Illustration %d")
5455 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5457 ("ca" :html
"Peus de pàgina")
5458 ("cs" :default
"Pozn\xe1mky pod carou")
5459 ("da" :default
"Fodnoter")
5460 ("de" :html
"Fußnoten" :default
"Fußnoten")
5461 ("eo" :default
"Piednotoj")
5462 ("es" :ascii
"Nota al pie de pagina" :html
"Nota al pie de página" :default
"Nota al pie de página")
5463 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5464 ("fi" :default
"Alaviitteet")
5465 ("fr" :default
"Notes de bas de page")
5466 ("hu" :html
"Lábjegyzet")
5467 ("is" :html
"Aftanmálsgreinar")
5468 ("it" :html
"Note a piè di pagina")
5469 ("ja" :default
"脚注" :html
"脚注")
5470 ("nl" :default
"Voetnoten")
5471 ("no" :default
"Fotnoter")
5472 ("nb" :default
"Fotnoter")
5473 ("nn" :default
"Fotnotar")
5474 ("pl" :default
"Przypis")
5475 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5476 ("ru" :html
"Сноски" :utf-8
"Сноски")
5477 ("sv" :default
"Fotnoter")
5478 ("uk" :html
"Примітки"
5480 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5481 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5483 ("da" :default
"Programmer")
5484 ("de" :default
"Programmauflistungsverzeichnis")
5485 ("es" :ascii
"Indice de Listados de programas" :html
"Índice de Listados de programas" :default
"Índice de Listados de programas")
5486 ("et" :default
"Loendite nimekiri")
5487 ("fr" :default
"Liste des programmes")
5488 ("ja" :default
"ソースコード目次")
5489 ("no" :default
"Dataprogrammer")
5490 ("nb" :default
"Dataprogrammer")
5491 ("ru" :html
"Список распечаток"
5492 :utf-8
"Список распечаток")
5493 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5495 ("da" :default
"Tabeller")
5496 ("de" :default
"Tabellenverzeichnis")
5497 ("es" :ascii
"Indice de tablas" :html
"Índice de tablas" :default
"Índice de tablas")
5498 ("et" :default
"Tabelite nimekiri")
5499 ("fr" :default
"Liste des tableaux")
5500 ("ja" :default
"表目次")
5501 ("no" :default
"Tabeller")
5502 ("nb" :default
"Tabeller")
5503 ("nn" :default
"Tabeller")
5504 ("pt_BR" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5505 ("ru" :html
"Список таблиц"
5506 :utf-8
"Список таблиц")
5507 ("sv" :default
"Tabeller")
5508 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5510 ("da" :default
"Program")
5511 ("de" :default
"Programmlisting")
5512 ("es" :default
"Listado de programa")
5513 ("et" :default
"Loend")
5514 ("fr" :default
"Programme" :html
"Programme")
5515 ("ja" :default
"ソースコード")
5516 ("no" :default
"Dataprogram")
5517 ("nb" :default
"Dataprogram")
5518 ("pt_BR" :default
"Listagem")
5519 ("ru" :html
"Распечатка"
5520 :utf-8
"Распечатка")
5521 ("zh-CN" :html
"代码" :utf-8
"代码"))
5523 ("da" :default
"Program %d")
5524 ("de" :default
"Programmlisting %d")
5525 ("es" :default
"Listado de programa %d")
5526 ("et" :default
"Loend %d")
5527 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5528 ("ja" :default
"ソースコード%d:")
5529 ("no" :default
"Dataprogram %d")
5530 ("nb" :default
"Dataprogram %d")
5531 ("pt_BR" :default
"Listagem %d")
5532 ("ru" :html
"Распечатка %d.:"
5533 :utf-8
"Распечатка %d.:")
5534 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5536 ("fr" :ascii
"References" :default
"Références")
5537 ("de" :default
"Quellen")
5538 ("es" :default
"Referencias"))
5540 ("da" :default
"jævnfør afsnit %s")
5541 ("de" :default
"siehe Abschnitt %s")
5542 ("es" :ascii
"Vea seccion %s" :html
"Vea sección %s" :default
"Vea sección %s")
5543 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5544 ("fr" :default
"cf. section %s")
5545 ("ja" :default
"セクション %s を参照")
5546 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
5547 :ascii
"Veja a secao %s")
5548 ("ru" :html
"См. раздел %s"
5549 :utf-8
"См. раздел %s")
5550 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5552 ("de" :default
"Tabelle")
5553 ("es" :default
"Tabla")
5554 ("et" :default
"Tabel")
5555 ("fr" :default
"Tableau")
5556 ("ja" :default
"表" :html
"表")
5557 ("pt_BR" :default
"Tabela")
5558 ("ru" :html
"Таблица"
5560 ("zh-CN" :html
"表" :utf-8
"表"))
5562 ("da" :default
"Tabel %d")
5563 ("de" :default
"Tabelle %d")
5564 ("es" :default
"Tabla %d")
5565 ("et" :default
"Tabel %d")
5566 ("fr" :default
"Tableau %d :")
5567 ("ja" :default
"表%d:" :html
"表%d:")
5568 ("no" :default
"Tabell %d")
5569 ("nb" :default
"Tabell %d")
5570 ("nn" :default
"Tabell %d")
5571 ("pt_BR" :default
"Tabela %d")
5572 ("ru" :html
"Таблица %d.:"
5573 :utf-8
"Таблица %d.:")
5574 ("sv" :default
"Tabell %d")
5575 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5576 ("Table of Contents"
5577 ("ca" :html
"Índex")
5578 ("cs" :default
"Obsah")
5579 ("da" :default
"Indhold")
5580 ("de" :default
"Inhaltsverzeichnis")
5581 ("eo" :default
"Enhavo")
5582 ("es" :ascii
"Indice" :html
"Índice" :default
"Índice")
5583 ("et" :default
"Sisukord")
5584 ("fi" :html
"Sisällysluettelo")
5585 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5586 ("hu" :html
"Tartalomjegyzék")
5587 ("is" :default
"Efnisyfirlit")
5588 ("it" :default
"Indice")
5589 ("ja" :default
"目次" :html
"目次")
5590 ("nl" :default
"Inhoudsopgave")
5591 ("no" :default
"Innhold")
5592 ("nb" :default
"Innhold")
5593 ("nn" :default
"Innhald")
5594 ("pl" :html
"Spis treści")
5595 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
5596 ("ru" :html
"Содержание"
5597 :utf-8
"Содержание")
5598 ("sv" :html
"Innehåll")
5599 ("uk" :html
"Зміст" :utf-8
"Зміст")
5600 ("zh-CN" :html
"目录" :utf-8
"目录")
5601 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5602 ("Unknown reference"
5603 ("da" :default
"ukendt reference")
5604 ("de" :default
"Unbekannter Verweis")
5605 ("es" :default
"Referencia desconocida")
5606 ("et" :default
"Tundmatu viide")
5607 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5608 ("ja" :default
"不明な参照先")
5609 ("pt_BR" :default
"Referência desconhecida"
5610 :ascii
"Referencia desconhecida")
5611 ("ru" :html
"Неизвестная ссылка"
5612 :utf-8
"Неизвестная ссылка")
5613 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5614 "Dictionary for export engine.
5616 Alist whose car is the string to translate and cdr is an alist
5617 whose car is the language string and cdr is a plist whose
5618 properties are possible charsets and values translated terms.
5620 It is used as a database for `org-export-translate'. Since this
5621 function returns the string as-is if no translation was found,
5622 the variable only needs to record values different from the
5625 (defun org-export-translate (s encoding info
)
5626 "Translate string S according to language specification.
5628 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5629 and `:utf-8'. INFO is a plist used as a communication channel.
5631 Translation depends on `:language' property. Return the
5632 translated string. If no translation is found, try to fall back
5633 to `:default' encoding. If it fails, return S."
5634 (let* ((lang (plist-get info
:language
))
5635 (translations (cdr (assoc lang
5636 (cdr (assoc s org-export-dictionary
))))))
5637 (or (plist-get translations encoding
)
5638 (plist-get translations
:default
)
5643 ;;; Asynchronous Export
5645 ;; `org-export-async-start' is the entry point for asynchronous
5646 ;; export. It recreates current buffer (including visibility,
5647 ;; narrowing and visited file) in an external Emacs process, and
5648 ;; evaluates a command there. It then applies a function on the
5649 ;; returned results in the current process.
5651 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5652 ;; allow to export to a buffer or a file, asynchronously or not.
5654 ;; `org-export-output-file-name' is an auxiliary function meant to be
5655 ;; used with `org-export-to-file'. With a given extension, it tries
5656 ;; to provide a canonical file name to write export output to.
5658 ;; Asynchronously generated results are never displayed directly.
5659 ;; Instead, they are stored in `org-export-stack-contents'. They can
5660 ;; then be retrieved by calling `org-export-stack'.
5662 ;; Export Stack is viewed through a dedicated major mode
5663 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5664 ;;`org-export-stack-delete', `org-export-stack-view' and
5665 ;;`org-export-stack-clear'.
5667 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5668 ;; It should be used whenever `org-export-async-start' is called.
5670 (defmacro org-export-async-start
(fun &rest body
)
5671 "Call function FUN on the results returned by BODY evaluation.
5673 FUN is an anonymous function of one argument. BODY evaluation
5674 happens in an asynchronous process, from a buffer which is an
5675 exact copy of the current one.
5677 Use `org-export-add-to-stack' in FUN in order to register results
5680 This is a low level function. See also `org-export-to-buffer'
5681 and `org-export-to-file' for more specialized functions."
5682 (declare (indent 1) (debug t
))
5683 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5684 ;; Write the full sexp evaluating BODY in a copy of the current
5685 ;; buffer to a temporary file, as it may be too long for program
5686 ;; args in `start-process'.
5687 `(with-temp-message "Initializing asynchronous export process"
5688 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5689 (,temp-file
(make-temp-file "org-export-process"))
5690 (,coding buffer-file-coding-system
))
5691 (with-temp-file ,temp-file
5693 ;; Null characters (from variable values) are inserted
5694 ;; within the file. As a consequence, coding system for
5695 ;; buffer contents will not be recognized properly. So,
5696 ;; we make sure it is the same as the one used to display
5697 ;; the original buffer.
5698 (format ";; -*- coding: %s; -*-\n%S"
5701 (when org-export-async-debug
'(setq debug-on-error t
))
5702 ;; Ignore `kill-emacs-hook' and code evaluation
5703 ;; queries from Babel as we need a truly
5704 ;; non-interactive process.
5705 (setq kill-emacs-hook nil
5706 org-babel-confirm-evaluate-answer-no t
)
5707 ;; Initialize export framework.
5709 ;; Re-create current buffer there.
5710 (funcall ,,copy-fun
)
5711 (restore-buffer-modified-p nil
)
5712 ;; Sexp to evaluate in the buffer.
5713 (print (progn ,,@body
))))))
5714 ;; Start external process.
5715 (let* ((process-connection-type nil
)
5716 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5721 (list "org-export-process"
5723 (expand-file-name invocation-name invocation-directory
)
5725 (if org-export-async-init-file
5726 (list "-Q" "-l" org-export-async-init-file
)
5727 (list "-l" user-init-file
))
5728 (list "-l" ,temp-file
)))))
5729 ;; Register running process in stack.
5730 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5731 ;; Set-up sentinel in order to catch results.
5732 (let ((handler ,fun
))
5733 (set-process-sentinel
5736 (let ((proc-buffer (process-buffer p
)))
5737 (when (eq (process-status p
) 'exit
)
5739 (if (zerop (process-exit-status p
))
5742 (with-current-buffer proc-buffer
5743 (goto-char (point-max))
5745 (read (current-buffer)))))
5746 (funcall ,handler results
))
5747 (unless org-export-async-debug
5748 (and (get-buffer proc-buffer
)
5749 (kill-buffer proc-buffer
))))
5750 (org-export-add-to-stack proc-buffer nil p
)
5752 (message "Process '%s' exited abnormally" p
))
5753 (unless org-export-async-debug
5754 (delete-file ,,temp-file
)))))))))))))
5757 (defun org-export-to-buffer
5759 &optional async subtreep visible-only body-only ext-plist
5761 "Call `org-export-as' with output to a specified buffer.
5763 BACKEND is either an export back-end, as returned by, e.g.,
5764 `org-export-create-backend', or a symbol referring to
5765 a registered back-end.
5767 BUFFER is the name of the output buffer. If it already exists,
5768 it will be erased first, otherwise, it will be created.
5770 A non-nil optional argument ASYNC means the process should happen
5771 asynchronously. The resulting buffer should then be accessible
5772 through the `org-export-stack' interface. When ASYNC is nil, the
5773 buffer is displayed if `org-export-show-temporary-export-buffer'
5776 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5777 EXT-PLIST are similar to those used in `org-export-as', which
5780 Optional argument POST-PROCESS is a function which should accept
5781 no argument. It is always called within the current process,
5782 from BUFFER, with point at its beginning. Export back-ends can
5783 use it to set a major mode there, e.g,
5785 \(defun org-latex-export-as-latex
5786 \(&optional async subtreep visible-only body-only ext-plist)
5788 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5789 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5791 This function returns BUFFER."
5792 (declare (indent 2))
5794 (org-export-async-start
5796 (with-current-buffer (get-buffer-create ,buffer
)
5798 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5800 (goto-char (point-min))
5801 (org-export-add-to-stack (current-buffer) ',backend
)
5802 (ignore-errors (funcall ,post-process
))))
5804 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5806 (org-export-as backend subtreep visible-only body-only ext-plist
))
5807 (buffer (get-buffer-create buffer
))
5808 (encoding buffer-file-coding-system
))
5809 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5810 (org-kill-new output
))
5811 (with-current-buffer buffer
5813 (setq buffer-file-coding-system encoding
)
5815 (goto-char (point-min))
5816 (and (functionp post-process
) (funcall post-process
)))
5817 (when org-export-show-temporary-export-buffer
5818 (switch-to-buffer-other-window buffer
))
5822 (defun org-export-to-file
5823 (backend file
&optional async subtreep visible-only body-only ext-plist
5825 "Call `org-export-as' with output to a specified file.
5827 BACKEND is either an export back-end, as returned by, e.g.,
5828 `org-export-create-backend', or a symbol referring to
5829 a registered back-end. FILE is the name of the output file, as
5832 A non-nil optional argument ASYNC means the process should happen
5833 asynchronously. The resulting buffer will then be accessible
5834 through the `org-export-stack' interface.
5836 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5837 EXT-PLIST are similar to those used in `org-export-as', which
5840 Optional argument POST-PROCESS is called with FILE as its
5841 argument and happens asynchronously when ASYNC is non-nil. It
5842 has to return a file name, or nil. Export back-ends can use this
5843 to send the output file through additional processing, e.g,
5845 \(defun org-latex-export-to-latex
5846 \(&optional async subtreep visible-only body-only ext-plist)
5848 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5849 \(org-export-to-file 'latex outfile
5850 async subtreep visible-only body-only ext-plist
5851 \(lambda (file) (org-latex-compile file)))
5853 The function returns either a file name returned by POST-PROCESS,
5855 (declare (indent 2))
5856 (if (not (file-writable-p file
)) (error "Output file not writable")
5857 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
5858 (encoding (or org-export-coding-system buffer-file-coding-system
)))
5860 (org-export-async-start
5862 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5865 ',backend
,subtreep
,visible-only
,body-only
5869 (let ((coding-system-for-write ',encoding
))
5870 (write-file ,file
)))
5871 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5872 (let ((output (org-export-as
5873 backend subtreep visible-only body-only ext-plist
)))
5876 (let ((coding-system-for-write encoding
))
5878 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5879 (org-kill-new output
))
5880 ;; Get proper return value.
5881 (or (and (functionp post-process
) (funcall post-process file
))
5884 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5885 "Return output file's name according to buffer specifications.
5887 EXTENSION is a string representing the output file extension,
5888 with the leading dot.
5890 With a non-nil optional argument SUBTREEP, try to determine
5891 output file's name by looking for \"EXPORT_FILE_NAME\" property
5892 of subtree at point.
5894 When optional argument PUB-DIR is set, use it as the publishing
5897 Return file name as a string."
5898 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5900 ;; File name may come from EXPORT_FILE_NAME subtree
5901 ;; property, assuming point is at beginning of said
5903 (file-name-sans-extension
5907 (ignore-errors (org-back-to-heading) (point)))
5908 "EXPORT_FILE_NAME" 'selective
))
5909 ;; File name may be extracted from buffer's associated
5911 (and visited-file
(file-name-nondirectory visited-file
))
5912 ;; Can't determine file name on our own: Ask user.
5913 (let ((read-file-name-function
5914 (and org-completion-use-ido
'ido-read-file-name
)))
5916 "Output file: " pub-dir nil nil nil
5918 (string= (file-name-extension name t
) extension
)))))))
5920 ;; Build file name. Enforce EXTENSION over whatever user
5921 ;; may have come up with. PUB-DIR, if defined, always has
5922 ;; precedence over any provided path.
5925 (concat (file-name-as-directory pub-dir
)
5926 (file-name-nondirectory base-name
)
5928 ((file-name-absolute-p base-name
) (concat base-name extension
))
5929 (t (concat (file-name-as-directory ".") base-name extension
)))))
5930 ;; If writing to OUTPUT-FILE would overwrite original file, append
5931 ;; EXTENSION another time to final name.
5932 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5933 (concat output-file extension
)
5936 (defun org-export-add-to-stack (source backend
&optional process
)
5937 "Add a new result to export stack if not present already.
5939 SOURCE is a buffer or a file name containing export results.
5940 BACKEND is a symbol representing export back-end used to generate
5943 Entries already pointing to SOURCE and unavailable entries are
5944 removed beforehand. Return the new stack."
5945 (setq org-export-stack-contents
5946 (cons (list source backend
(or process
(current-time)))
5947 (org-export-stack-remove source
))))
5949 (defun org-export-stack ()
5950 "Menu for asynchronous export results and running processes."
5952 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5954 (when (zerop (buffer-size)) (org-export-stack-mode))
5955 (org-export-stack-refresh)
5956 (pop-to-buffer buffer
))
5957 (message "Type \"q\" to quit, \"?\" for help"))
5959 (defun org-export--stack-source-at-point ()
5960 "Return source from export results at point in stack."
5961 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5962 (if (not source
) (error "Source unavailable, please refresh buffer")
5963 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5966 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5968 ;; SOURCE is not consistent with current line. The stack
5969 ;; view is outdated.
5970 (error "Source unavailable; type `g' to update buffer"))))))
5972 (defun org-export-stack-clear ()
5973 "Remove all entries from export stack."
5975 (setq org-export-stack-contents nil
))
5977 (defun org-export-stack-refresh (&rest _
)
5978 "Refresh the asynchronous export stack.
5979 Unavailable sources are removed from the list. Return the new
5981 (let ((inhibit-read-only t
))
5987 (let ((proc-p (processp (nth 2 entry
))))
5990 (format " %-12s " (or (nth 1 entry
) ""))
5992 (let ((data (nth 2 entry
)))
5993 (if proc-p
(format " %6s " (process-status data
))
5994 ;; Compute age of the results.
5997 (float-time (time-since data
)))))
6000 (let ((source (car entry
)))
6001 (if (stringp source
) source
6002 (buffer-name source
)))))))
6003 ;; Clear stack from exited processes, dead buffers or
6004 ;; non-existent files.
6005 (setq org-export-stack-contents
6008 (if (processp (nth 2 el
))
6009 (buffer-live-p (process-buffer (nth 2 el
)))
6010 (let ((source (car el
)))
6011 (if (bufferp source
) (buffer-live-p source
)
6012 (file-exists-p source
)))))
6013 org-export-stack-contents
)) "\n"))))))
6015 (defun org-export-stack-remove (&optional source
)
6016 "Remove export results at point from stack.
6017 If optional argument SOURCE is non-nil, remove it instead."
6019 (let ((source (or source
(org-export--stack-source-at-point))))
6020 (setq org-export-stack-contents
6021 (org-remove-if (lambda (el) (equal (car el
) source
))
6022 org-export-stack-contents
))))
6024 (defun org-export-stack-view (&optional in-emacs
)
6025 "View export results at point in stack.
6026 With an optional prefix argument IN-EMACS, force viewing files
6029 (let ((source (org-export--stack-source-at-point)))
6030 (cond ((processp source
)
6031 (org-switch-to-buffer-other-window (process-buffer source
)))
6032 ((bufferp source
) (org-switch-to-buffer-other-window source
))
6033 (t (org-open-file source in-emacs
)))))
6035 (defvar org-export-stack-mode-map
6036 (let ((km (make-sparse-keymap)))
6037 (define-key km
" " 'next-line
)
6038 (define-key km
"n" 'next-line
)
6039 (define-key km
"\C-n" 'next-line
)
6040 (define-key km
[down] 'next-line)
6041 (define-key km "p" 'previous-line)
6042 (define-key km "\C-p" 'previous-line)
6043 (define-key km "\C-?" 'previous-line)
6044 (define-key km [up] 'previous-line)
6045 (define-key km "C" 'org-export-stack-clear)
6046 (define-key km "v" 'org-export-stack-view)
6047 (define-key km (kbd "RET") 'org-export-stack-view)
6048 (define-key km "d" 'org-export-stack-remove)
6050 "Keymap for Org Export Stack.")
6052 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
6053 "Mode for displaying asynchronous export stack.
6055 Type \\[org-export-stack] to visualize the asynchronous export
6058 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6059 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6062 Removing entries in an Org Export Stack buffer doesn't affect
6063 files or buffers, only the display.
6065 \\{org-export-stack-mode-map}"
6068 (setq buffer-read-only t
6073 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6074 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6075 (set (make-local-variable 'revert-buffer-function)
6076 'org-export-stack-refresh))
6082 ;; `org-export-dispatch' is the standard interactive way to start an
6083 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6084 ;; for its interface, which, in turn, delegates response to key
6085 ;; pressed to `org-export--dispatch-action'.
6088 (defun org-export-dispatch (&optional arg)
6089 "Export dispatcher for Org mode.
6091 It provides an access to common export related tasks in a buffer.
6092 Its interface comes in two flavors: standard and expert.
6094 While both share the same set of bindings, only the former
6095 displays the valid keys associations in a dedicated buffer.
6096 Scrolling (resp. line-wise motion) in this buffer is done with
6097 SPC and DEL (resp. C-n and C-p) keys.
6099 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6100 flavor or the other.
6102 When ARG is \\[universal-argument], repeat the last export action, with the same set
6103 of options used back then, on the current buffer.
6105 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6108 (cond ((equal arg '(16)) '(stack))
6109 ((and arg org-export-dispatch-last-action))
6110 (t (save-window-excursion
6113 ;; Remember where we are
6114 (move-marker org-export-dispatch-last-position
6116 (org-base-buffer (current-buffer)))
6117 ;; Get and store an export command
6118 (setq org-export-dispatch-last-action
6119 (org-export--dispatch-ui
6120 (list org-export-initial-scope
6121 (and org-export-in-background 'async))
6123 org-export-dispatch-use-expert-ui)))
6124 (and (get-buffer "*Org Export Dispatcher*")
6125 (kill-buffer "*Org Export Dispatcher*")))))))
6126 (action (car input))
6127 (optns (cdr input)))
6128 (unless (memq 'subtree optns)
6129 (move-marker org-export-dispatch-last-position nil))
6131 ;; First handle special hard-coded actions.
6132 (template (org-export-insert-default-template nil optns))
6133 (stack (org-export-stack))
6134 (publish-current-file
6135 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6136 (publish-current-project
6137 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6138 (publish-choose-project
6139 (org-publish (assoc (org-icompleting-read
6141 org-publish-project-alist nil t)
6142 org-publish-project-alist)
6144 (memq 'async optns)))
6145 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6149 ;; Repeating command, maybe move cursor to restore subtree
6151 (if (eq (marker-buffer org-export-dispatch-last-position)
6152 (org-base-buffer (current-buffer)))
6153 (goto-char org-export-dispatch-last-position)
6154 ;; We are in a different buffer, forget position.
6155 (move-marker org-export-dispatch-last-position nil)))
6157 ;; Return a symbol instead of a list to ease
6158 ;; asynchronous export macro use.
6159 (and (memq 'async optns) t)
6160 (and (memq 'subtree optns) t)
6161 (and (memq 'visible optns) t)
6162 (and (memq 'body optns) t)))))))
6164 (defun org-export--dispatch-ui (options first-key expertp)
6165 "Handle interface for `org-export-dispatch'.
6167 OPTIONS is a list containing current interactive options set for
6168 export. It can contain any of the following symbols:
6169 `body' toggles a body-only export
6170 `subtree' restricts export to current subtree
6171 `visible' restricts export to visible part of buffer.
6172 `force' force publishing files.
6173 `async' use asynchronous export process
6175 FIRST-KEY is the key pressed to select the first level menu. It
6176 is nil when this menu hasn't been selected yet.
6178 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6179 buffer is provided, but indications about currently active
6180 options are given in the prompt. Moreover, \[?] allows to switch
6181 back to standard interface."
6183 (lambda (key &optional access-key)
6184 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6185 ;; non-nil is the required first-level key to activate
6186 ;; KEY. When its value is t, activate KEY independently
6187 ;; on the first key, if any. A nil value means KEY will
6188 ;; only be activated at first level.
6189 (if (or (eq access-key t) (eq access-key first-key))
6190 (org-propertize key 'face 'org-warning)
6194 ;; Fontify VALUE string.
6195 (org-propertize value 'face 'font-lock-variable-name-face)))
6196 ;; Prepare menu entries by extracting them from registered
6197 ;; back-ends and sorting them by access key and by ordinal,
6200 (sort (sort (delq nil
6201 (mapcar #'org-export-backend-menu
6202 org-export-registered-backends))
6204 (let ((key-a (nth 1 a))
6206 (cond ((and (numberp key-a) (numberp key-b))
6208 ((numberp key-b) t)))))
6209 'car-less-than-car))
6210 ;; Compute a list of allowed keys based on the first key
6211 ;; pressed, if any. Some keys
6212 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6215 (nconc (list 2 22 19 6 1)
6216 (if (not first-key) (org-uniquify (mapcar 'car entries))
6218 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6219 (when (eq (car entry) first-key)
6220 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6221 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6222 ((not first-key) (list ?P)))
6224 (when expertp (list ??))
6226 ;; Build the help menu for standard UI.
6230 ;; Options are hard-coded.
6231 (format "[%s] Body only: %s [%s] Visible only: %s
6232 \[%s] Export scope: %s [%s] Force publishing: %s
6233 \[%s] Async export: %s\n\n"
6234 (funcall fontify-key "C-b" t)
6235 (funcall fontify-value
6236 (if (memq 'body options) "On " "Off"))
6237 (funcall fontify-key "C-v" t)
6238 (funcall fontify-value
6239 (if (memq 'visible options) "On " "Off"))
6240 (funcall fontify-key "C-s" t)
6241 (funcall fontify-value
6242 (if (memq 'subtree options) "Subtree" "Buffer "))
6243 (funcall fontify-key "C-f" t)
6244 (funcall fontify-value
6245 (if (memq 'force options) "On " "Off"))
6246 (funcall fontify-key "C-a" t)
6247 (funcall fontify-value
6248 (if (memq 'async options) "On " "Off")))
6249 ;; Display registered back-end entries. When a key
6250 ;; appears for the second time, do not create another
6251 ;; entry, but append its sub-menu to existing menu.
6255 (let ((top-key (car entry)))
6257 (unless (eq top-key last-key)
6258 (setq last-key top-key)
6259 (format "\n[%s] %s\n"
6260 (funcall fontify-key (char-to-string top-key))
6262 (let ((sub-menu (nth 2 entry)))
6263 (unless (functionp sub-menu)
6264 ;; Split sub-menu into two columns.
6271 (if (zerop (mod index 2)) " [%s] %-26s"
6273 (funcall fontify-key
6274 (char-to-string (car sub-entry))
6278 (when (zerop (mod index 2)) "\n"))))))))
6280 ;; Publishing menu is hard-coded.
6281 (format "\n[%s] Publish
6282 [%s] Current file [%s] Current project
6283 [%s] Choose project [%s] All projects\n\n\n"
6284 (funcall fontify-key "P")
6285 (funcall fontify-key "f" ?P)
6286 (funcall fontify-key "p" ?P)
6287 (funcall fontify-key "x" ?P)
6288 (funcall fontify-key "a" ?P))
6289 (format "[%s] Export stack [%s] Insert template\n"
6290 (funcall fontify-key "&" t)
6291 (funcall fontify-key "#" t))
6293 (funcall fontify-key "q" t)
6294 (if first-key "Main menu" "Exit")))))
6295 ;; Build prompts for both standard and expert UI.
6296 (standard-prompt (unless expertp "Export command: "))
6300 "Export command (C-%s%s%s%s%s) [%s]: "
6301 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6302 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6303 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6304 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6305 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6306 (mapconcat (lambda (k)
6307 ;; Strip control characters.
6308 (unless (< k 27) (char-to-string k)))
6309 allowed-keys "")))))
6310 ;; With expert UI, just read key with a fancy prompt. In standard
6311 ;; UI, display an intrusive help buffer.
6313 (org-export--dispatch-action
6314 expert-prompt allowed-keys entries options first-key expertp)
6315 ;; At first call, create frame layout in order to display menu.
6316 (unless (get-buffer "*Org Export Dispatcher*")
6317 (delete-other-windows)
6318 (org-switch-to-buffer-other-window
6319 (get-buffer-create "*Org Export Dispatcher*"))
6320 (setq cursor-type nil
6321 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6322 ;; Make sure that invisible cursor will not highlight square
6324 (set-syntax-table (copy-syntax-table))
6325 (modify-syntax-entry ?\[ "w"))
6326 ;; At this point, the buffer containing the menu exists and is
6327 ;; visible in the current window. So, refresh it.
6328 (with-current-buffer "*Org Export Dispatcher*"
6329 ;; Refresh help. Maintain display continuity by re-visiting
6330 ;; previous window position.
6331 (let ((pos (window-start)))
6334 (set-window-start nil pos)))
6335 (org-fit-window-to-buffer)
6336 (org-export--dispatch-action
6337 standard-prompt allowed-keys entries options first-key expertp))))
6339 (defun org-export--dispatch-action
6340 (prompt allowed-keys entries options first-key expertp)
6341 "Read a character from command input and act accordingly.
6343 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6344 a list of characters available at a given step in the process.
6345 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6346 EXPERTP are the same as defined in `org-export--dispatch-ui',
6349 Toggle export options when required. Otherwise, return value is
6350 a list with action as CAR and a list of interactive export
6353 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6355 (while (and (setq key (read-char-exclusive prompt))
6357 (memq key '(14 16 ?\s ?\d)))
6359 (14 (if (not (pos-visible-in-window-p (point-max)))
6360 (ignore-errors (scroll-up 1))
6361 (message "End of buffer")
6363 (16 (if (not (pos-visible-in-window-p (point-min)))
6364 (ignore-errors (scroll-down 1))
6365 (message "Beginning of buffer")
6367 (?\s (if (not (pos-visible-in-window-p (point-max)))
6369 (message "End of buffer")
6371 (?\d (if (not (pos-visible-in-window-p (point-min)))
6373 (message "Beginning of buffer")
6376 ;; Ignore undefined associations.
6377 ((not (memq key allowed-keys))
6379 (unless expertp (message "Invalid key") (sit-for 1))
6380 (org-export--dispatch-ui options first-key expertp))
6381 ;; q key at first level aborts export. At second level, cancel
6382 ;; first key instead.
6383 ((eq key ?q) (if (not first-key) (error "Export aborted")
6384 (org-export--dispatch-ui options nil expertp)))
6385 ;; Help key: Switch back to standard interface if expert UI was
6387 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6388 ;; Send request for template insertion along with export scope.
6389 ((eq key ?#) (cons 'template (memq 'subtree options)))
6390 ;; Switch to asynchronous export stack.
6391 ((eq key ?&) '(stack))
6392 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6393 ((memq key '(2 22 19 6 1))
6394 (org-export--dispatch-ui
6395 (let ((option (cl-case key (2 'body) (22 'visible) (19 'subtree)
6396 (6 'force) (1 'async))))
6397 (if (memq option options) (remq option options)
6398 (cons option options)))
6400 ;; Action selected: Send key and options back to
6401 ;; `org-export-dispatch'.
6402 ((or first-key (functionp (nth 2 (assq key entries))))
6404 ((not first-key) (nth 2 (assq key entries)))
6405 ;; Publishing actions are hard-coded. Send a special
6406 ;; signal to `org-export-dispatch'.
6409 (?f 'publish-current-file)
6410 (?p 'publish-current-project)
6411 (?x 'publish-choose-project)
6413 ;; Return first action associated to FIRST-KEY + KEY
6414 ;; path. Indeed, derived backends can share the same
6417 (mapc (lambda (entry)
6418 (let ((match (assq key (nth 2 entry))))
6419 (when match (throw 'found (nth 2 match)))))
6420 (member (assq first-key entries) entries)))))
6422 ;; Otherwise, enter sub-menu.
6423 (t (org-export--dispatch-ui options key expertp)))))
6430 ;; generated-autoload-file: "org-loaddefs.el"