1 ;;; ox.el --- Generic Export Engine for Org Mode
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
74 (eval-when-compile (require 'cl
))
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 (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 contents
)
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 contents
)
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 (lambda (o) (cons (nth 2 o
) (car o
)))
1345 ;; Priority is given to back-end specific options.
1346 (append (and backend
(org-export-get-all-options backend
))
1347 org-export-options-alist
)))
1350 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t\n]*" options start
)
1351 (setq start
(match-end 0))
1352 (let ((property (cdr (assoc-string (match-string 1 options
) all t
))))
1355 (plist-put plist property
(read (match-string 2 options
)))))))
1358 (defun org-export--get-subtree-options (&optional backend
)
1359 "Get export options in subtree at point.
1360 Optional argument BACKEND is an export back-end, as returned by,
1361 e.g., `org-export-create-backend'. It specifies back-end used
1362 for export. Return options as a plist."
1363 ;; For each buffer keyword, create a headline property setting the
1364 ;; same property in communication channel. The name for the
1365 ;; property is the keyword with "EXPORT_" appended to it.
1366 (org-with-wide-buffer
1367 ;; Make sure point is at a heading.
1368 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1370 ;; EXPORT_OPTIONS are parsed in a non-standard way. Take
1371 ;; care of them right from the start.
1372 (let ((o (org-entry-get (point) "EXPORT_OPTIONS" 'selective
)))
1373 (and o
(org-export--parse-option-keyword o backend
))))
1374 ;; Take care of EXPORT_TITLE. If it isn't defined, use
1375 ;; headline's title (with no todo keyword, priority cookie or
1376 ;; tag) as its fallback value.
1379 (or (org-entry-get (point) "EXPORT_TITLE" 'selective
)
1380 (progn (looking-at org-complex-heading-regexp
)
1381 (org-match-string-no-properties 4))))))
1382 ;; Look for both general keywords and back-end specific
1383 ;; options, with priority given to the latter.
1384 (options (append (and backend
(org-export-get-all-options backend
))
1385 org-export-options-alist
)))
1386 ;; Handle other keywords. Then return PLIST.
1387 (dolist (option options plist
)
1388 (let ((property (car option
))
1389 (keyword (nth 1 option
)))
1392 (or (cdr (assoc keyword cache
))
1393 (let ((v (org-entry-get (point)
1394 (concat "EXPORT_" keyword
)
1396 (push (cons keyword v
) cache
) v
))))
1401 (case (nth 4 option
)
1403 (org-element-parse-secondary-string
1404 value
(org-element-restriction 'keyword
)))
1405 (split (org-split-string value
))
1406 (t value
))))))))))))
1408 (defun org-export--get-inbuffer-options (&optional backend
)
1409 "Return current buffer export options, as a plist.
1411 Optional argument BACKEND, when non-nil, is an export back-end,
1412 as returned by, e.g., `org-export-create-backend'. It specifies
1413 which back-end specific options should also be read in the
1416 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1418 get-options
; For byte-compiler.
1419 (case-fold-search t
)
1421 ;; Priority is given to back-end specific options.
1422 (and backend
(org-export-get-all-options backend
))
1423 org-export-options-alist
))
1424 (regexp (format "^[ \t]*#\\+%s:"
1425 (regexp-opt (nconc (delq nil
(mapcar #'cadr options
))
1426 org-export-special-keywords
))))
1429 ;; Return all properties associated to KEYWORD.
1431 (dolist (option options properties
)
1432 (when (equal (nth 1 option
) keyword
)
1433 (pushnew (car option
) properties
))))))
1436 (lambda (&optional files plist
)
1437 ;; Recursively read keywords in buffer. FILES is a list
1438 ;; of files read so far. PLIST is the current property
1440 (org-with-wide-buffer
1441 (goto-char (point-min))
1442 (while (re-search-forward regexp nil t
)
1443 (let ((element (org-element-at-point)))
1444 (when (eq (org-element-type element
) 'keyword
)
1445 (let ((key (org-element-property :key element
))
1446 (val (org-element-property :value element
)))
1448 ;; Options in `org-export-special-keywords'.
1449 ((equal key
"SETUPFILE")
1450 (let ((file (expand-file-name
1451 (org-remove-double-quotes (org-trim val
)))))
1452 ;; Avoid circular dependencies.
1453 (unless (member file files
)
1455 (insert (org-file-contents file
'noerror
))
1456 (let ((org-inhibit-startup t
)) (org-mode))
1457 (setq plist
(funcall get-options
1458 (cons file files
) plist
))))))
1459 ((equal key
"OPTIONS")
1463 (org-export--parse-option-keyword val backend
))))
1464 ((equal key
"FILETAGS")
1470 (append (org-split-string val
":")
1471 (plist-get plist
:filetags
)))))))
1473 ;; Options in `org-export-options-alist'.
1474 (dolist (property (funcall find-properties key
))
1479 ;; Handle value depending on specified
1481 (case (nth 4 (assq property options
))
1483 (unless (memq property to-parse
)
1484 (push property to-parse
))
1485 ;; Even if `parse' implies `space'
1486 ;; behavior, we separate line with "\n"
1487 ;; so as to preserve line-breaks.
1488 ;; However, empty lines are forbidden
1489 ;; since `parse' doesn't allow more than
1491 (let ((old (plist-get plist property
)))
1492 (cond ((not (org-string-nw-p val
)) old
)
1493 (old (concat old
"\n" val
))
1496 (if (not (plist-get plist property
))
1498 (concat (plist-get plist property
)
1503 (concat (plist-get plist property
)
1506 (split `(,@(plist-get plist property
)
1507 ,@(org-split-string val
)))
1510 (if (not (plist-member plist property
)) val
1511 (plist-get plist property
)))))))))))))
1513 ;; Read options in the current buffer and return value.
1514 (let ((options (funcall get-options
1515 (and buffer-file-name
(list buffer-file-name
))
1517 ;; Parse properties in TO-PARSE. Remove newline characters not
1518 ;; involved in line breaks to simulate `space' behavior.
1519 ;; Finally return options.
1520 (dolist (p to-parse options
)
1521 (let ((value (org-element-parse-secondary-string
1522 (plist-get options p
)
1523 (org-element-restriction 'keyword
))))
1524 (org-element-map value
'plain-text
1526 (org-element-set-element
1527 s
(replace-regexp-in-string "\n" " " s
))))
1528 (setq options
(plist-put options p value
)))))))
1530 (defun org-export--get-buffer-attributes ()
1531 "Return properties related to buffer attributes, as a plist."
1532 (list :input-buffer
(buffer-name (buffer-base-buffer))
1533 :input-file
(buffer-file-name (buffer-base-buffer))))
1535 (defun org-export--get-global-options (&optional backend
)
1536 "Return global export options as a plist.
1537 Optional argument BACKEND, if non-nil, is an export back-end, as
1538 returned by, e.g., `org-export-create-backend'. It specifies
1539 which back-end specific export options should also be read in the
1542 ;; Priority is given to back-end specific options.
1543 (all (append (and backend
(org-export-get-all-options backend
))
1544 org-export-options-alist
)))
1545 (dolist (cell all plist
)
1546 (let ((prop (car cell
)))
1547 (unless (plist-member plist prop
)
1552 ;; Evaluate default value provided.
1553 (let ((value (eval (nth 3 cell
))))
1554 (if (eq (nth 4 cell
) 'parse
)
1555 (org-element-parse-secondary-string
1556 value
(org-element-restriction 'keyword
))
1559 (defun org-export--list-bound-variables ()
1560 "Return variables bound from BIND keywords in current buffer.
1561 Also look for BIND keywords in setup files. The return value is
1562 an alist where associations are (VARIABLE-NAME VALUE)."
1563 (when org-export-allow-bind-keywords
1564 (let* (collect-bind ; For byte-compiler.
1566 (lambda (files alist
)
1567 ;; Return an alist between variable names and their
1568 ;; value. FILES is a list of setup files names read so
1569 ;; far, used to avoid circular dependencies. ALIST is
1570 ;; 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
) "BIND")
1580 (push (read (format "(%s)" val
)) alist
)
1581 ;; Enter setup file.
1582 (let ((file (expand-file-name
1583 (org-remove-double-quotes val
))))
1584 (unless (member file files
)
1586 (let ((org-inhibit-startup t
)) (org-mode))
1587 (insert (org-file-contents file
'noerror
))
1589 (funcall collect-bind
1593 ;; Return value in appropriate order of appearance.
1594 (nreverse (funcall collect-bind nil nil
)))))
1596 ;; defsubst org-export-get-parent must be defined before first use,
1597 ;; was originally defined in the topology section
1599 (defsubst org-export-get-parent
(blob)
1600 "Return BLOB parent or nil.
1601 BLOB is the element or object considered."
1602 (org-element-property :parent blob
))
1604 ;;;; Tree Properties
1606 ;; Tree properties are information extracted from parse tree. They
1607 ;; are initialized at the beginning of the transcoding process by
1608 ;; `org-export-collect-tree-properties'.
1610 ;; Dedicated functions focus on computing the value of specific tree
1611 ;; properties during initialization. Thus,
1612 ;; `org-export--populate-ignore-list' lists elements and objects that
1613 ;; should be skipped during export, `org-export--get-min-level' gets
1614 ;; the minimal exportable level, used as a basis to compute relative
1615 ;; level for headlines. Eventually
1616 ;; `org-export--collect-headline-numbering' builds an alist between
1617 ;; headlines and their numbering.
1619 (defun org-export-collect-tree-properties (data info
)
1620 "Extract tree properties from parse tree.
1622 DATA is the parse tree from which information is retrieved. INFO
1623 is a list holding export options.
1625 Following tree properties are set or updated:
1627 `:exported-data' Hash table used to memoize results from
1630 `:headline-offset' Offset between true level of headlines and
1631 local level. An offset of -1 means a headline
1632 of level 2 should be considered as a level
1633 1 headline in the context.
1635 `:headline-numbering' Alist of all headlines as key an the
1636 associated numbering as value.
1638 Return updated plist."
1639 ;; Install the parse tree in the communication channel.
1640 (setq info
(plist-put info
:parse-tree data
))
1641 ;; Compute `:headline-offset' in order to be able to use
1642 ;; `org-export-get-relative-level'.
1646 (- 1 (org-export--get-min-level data info
))))
1647 ;; Properties order doesn't matter: get the rest of the tree
1650 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1651 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1654 (defun org-export--get-min-level (data options
)
1655 "Return minimum exportable headline's level in DATA.
1656 DATA is parsed tree as returned by `org-element-parse-buffer'.
1657 OPTIONS is a plist holding export options."
1659 (let ((min-level 10000))
1662 (when (and (eq (org-element-type blob
) 'headline
)
1663 (not (org-element-property :footnote-section-p blob
))
1664 (not (memq blob
(plist-get options
:ignore-list
))))
1665 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1666 (when (= min-level
1) (throw 'exit
1)))
1667 (org-element-contents data
))
1668 ;; If no headline was found, for the sake of consistency, set
1669 ;; minimum level to 1 nonetheless.
1670 (if (= min-level
10000) 1 min-level
))))
1672 (defun org-export--collect-headline-numbering (data options
)
1673 "Return numbering of all exportable, numbered headlines in a parse tree.
1675 DATA is the parse tree. OPTIONS is the plist holding export
1678 Return an alist whose key is a headline and value is its
1679 associated numbering \(in the shape of a list of numbers\) or nil
1680 for a footnotes section."
1681 (let ((numbering (make-vector org-export-max-depth
0)))
1682 (org-element-map data
'headline
1684 (when (and (org-export-numbered-headline-p headline options
)
1685 (not (org-element-property :footnote-section-p headline
)))
1686 (let ((relative-level
1687 (1- (org-export-get-relative-level headline options
))))
1690 (loop for n across numbering
1691 for idx from
0 to org-export-max-depth
1692 when
(< idx relative-level
) collect n
1693 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1694 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1697 (defun org-export--selected-trees (data info
)
1698 "List headlines and inlinetasks with a select tag in their tree.
1699 DATA is parsed data as returned by `org-element-parse-buffer'.
1700 INFO is a plist holding export options."
1701 (let* (selected-trees
1702 walk-data
; For byte-compiler.
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 (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 tree
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 (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 (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 (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 (blob 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 (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
2612 ;; First find trees containing a select tag, if any.
2613 (selected (org-export--selected-trees data info
))
2616 ;; Prune non-exportable elements and objects from tree.
2617 ;; As a special case, special rows and cells from tables
2618 ;; are stored in IGNORE, as they still need to be accessed
2621 (let ((type (org-element-type data
)))
2622 (if (org-export--skip-p data info selected
)
2623 (if (memq type
'(table-cell table-row
)) (push data ignore
)
2624 (org-element-extract-element data
))
2625 (if (and (eq type
'headline
)
2626 (eq (plist-get info
:with-archived-trees
) 'headline
)
2627 (org-element-property :archivedp data
))
2628 ;; If headline is archived but tree below has to
2629 ;; be skipped, remove contents.
2630 (org-element-set-contents data
)
2631 ;; Move into secondary string, if any.
2633 (cdr (assq type org-element-secondary-value-alist
))))
2635 (mapc walk-data
(org-element-property sec-prop data
))))
2636 ;; Move into recursive objects/elements.
2637 (mapc walk-data
(org-element-contents data
)))))))))
2638 ;; If a select tag is active, also ignore the section before the
2639 ;; first headline, if any.
2641 (let ((first-element (car (org-element-contents data
))))
2642 (when (eq (org-element-type first-element
) 'section
)
2643 (org-element-extract-element first-element
))))
2644 ;; Prune tree and communication channel.
2645 (funcall walk-data data
)
2648 ;; Priority is given to back-end specific options.
2649 (org-export-get-all-options (plist-get info
:back-end
))
2650 org-export-options-alist
))
2651 (when (eq (nth 4 entry
) 'parse
)
2652 (funcall walk-data
(plist-get info
(car entry
)))))
2653 ;; Eventually set `:ignore-list'.
2654 (plist-put info
:ignore-list ignore
)))
2656 (defun org-export--remove-uninterpreted-data (data info
)
2657 "Change uninterpreted elements back into Org syntax.
2658 DATA is the parse tree. INFO is a plist containing export
2659 options. Each uninterpreted element or object is changed back
2660 into a string. Contents, if any, are not modified. The parse
2661 tree is modified by side effect."
2662 (org-export--remove-uninterpreted-data-1 data info
)
2663 (dolist (entry org-export-options-alist
)
2664 (when (eq (nth 4 entry
) 'parse
)
2665 (let ((p (car entry
)))
2668 (org-export--remove-uninterpreted-data-1
2672 (defun org-export--remove-uninterpreted-data-1 (data info
)
2673 "Change uninterpreted elements back into Org syntax.
2674 DATA is a parse tree or a secondary string. INFO is a plist
2675 containing export options. It is modified by side effect and
2676 returned by the function."
2677 (org-element-map data
2678 '(entity bold italic latex-environment latex-fragment strike-through
2679 subscript superscript underline
)
2682 (case (org-element-type blob
)
2685 (and (not (plist-get info
:with-entities
))
2687 (org-export-expand blob nil
)
2689 (or (org-element-property :post-blank blob
) 0)
2692 ((bold italic strike-through underline
)
2693 (and (not (plist-get info
:with-emphasize
))
2694 (let ((marker (case (org-element-type blob
)
2697 (strike-through "+")
2701 (org-element-contents blob
)
2705 (or (org-element-property :post-blank blob
)
2708 ;; ... LaTeX environments and fragments...
2709 ((latex-environment latex-fragment
)
2710 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2711 (list (org-export-expand blob nil
))))
2712 ;; ... sub/superscripts...
2713 ((subscript superscript
)
2714 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2715 (bracketp (org-element-property :use-brackets-p blob
)))
2716 (and (or (not sub
/super-p
)
2717 (and (eq sub
/super-p
'{}) (not bracketp
)))
2720 (if (eq (org-element-type blob
) 'subscript
)
2723 (and bracketp
"{")))
2724 (org-element-contents blob
)
2727 (and (org-element-property :post-blank blob
)
2729 (org-element-property :post-blank blob
)
2732 ;; Splice NEW at BLOB location in parse tree.
2733 (dolist (e new
(org-element-extract-element blob
))
2734 (unless (string= e
"") (org-element-insert-before e blob
))))))
2736 ;; Return modified parse tree.
2739 (defun org-export--merge-external-footnote-definitions (tree)
2740 "Insert footnote definitions outside parsing scope in TREE.
2742 If there is a footnote section in TREE, definitions found are
2743 appended to it. If `org-footnote-section' is non-nil, a new
2744 footnote section containing all definitions is inserted in TREE.
2745 Otherwise, definitions are appended at the end of the section
2746 containing their first reference.
2748 Only definitions actually referred to within TREE, directly or
2749 not, are considered."
2750 (let* ((collect-labels
2752 (org-element-map data
'footnote-reference
2754 (and (eq (org-element-property :type f
) 'standard
)
2755 (org-element-property :label f
))))))
2756 (referenced-labels (funcall collect-labels tree
)))
2757 (when referenced-labels
2758 (let* ((definitions)
2761 (case (org-element-type datum
)
2762 (footnote-definition
2763 (push (save-restriction
2764 (narrow-to-region (org-element-property :begin datum
)
2765 (org-element-property :end datum
))
2766 (org-element-map (org-element-parse-buffer)
2767 'footnote-definition
#'identity nil t
))
2770 (let ((label (org-element-property :label datum
))
2771 (cbeg (org-element-property :contents-begin datum
)))
2772 (when (and label cbeg
2773 (eq (org-element-property :type datum
) 'inline
))
2775 (apply #'org-element-create
2776 'footnote-definition
2777 (list :label label
:post-blank
1)
2778 (org-element-parse-secondary-string
2780 cbeg
(org-element-property :contents-end datum
))
2781 (org-element-restriction 'footnote-reference
)))
2783 ;; Collect all out of scope definitions.
2785 (goto-char (point-min))
2786 (org-with-wide-buffer
2787 (while (re-search-backward org-footnote-re nil t
)
2788 (funcall push-definition
(org-element-context))))
2789 (goto-char (point-max))
2790 (org-with-wide-buffer
2791 (while (re-search-forward org-footnote-re nil t
)
2792 (funcall push-definition
(org-element-context)))))
2793 ;; Filter out definitions referenced neither in the original
2794 ;; tree nor in the external definitions.
2795 (let* ((directly-referenced
2798 (member (org-element-property :label d
) referenced-labels
))
2801 (append (funcall collect-labels directly-referenced
)
2802 referenced-labels
)))
2806 (member (org-element-property :label d
) all-labels
))
2808 ;; Install definitions in subtree.
2810 ((null definitions
))
2811 ;; If there is a footnote section, insert them here.
2812 ((let ((footnote-section
2813 (org-element-map tree
'headline
2815 (and (org-element-property :footnote-section-p h
) h
))
2817 (and footnote-section
2818 (apply #'org-element-adopt-elements
(nreverse definitions
)))))
2819 ;; If there should be a footnote section, create one containing
2820 ;; all the definitions at the end of the tree.
2821 (org-footnote-section
2822 (org-element-adopt-elements
2824 (org-element-create 'headline
2825 (list :footnote-section-p t
2827 :title org-footnote-section
)
2828 (apply #'org-element-create
2831 (nreverse definitions
)))))
2832 ;; Otherwise add each definition at the end of the section where
2833 ;; it is first referenced.
2836 (insert-definitions) ; For byte-compiler.
2839 ;; Insert definitions in the same section as their
2840 ;; first reference in DATA.
2841 (org-element-map tree
'footnote-reference
2843 (when (eq (org-element-property :type f
) 'standard
)
2844 (let ((label (org-element-property :label f
)))
2845 (unless (member label seen
)
2849 (dolist (d definitions
)
2851 (org-element-property :label d
)
2854 (delete d definitions
))
2855 (throw 'found d
))))))
2857 (org-element-adopt-elements
2858 (org-element-lineage f
'(section))
2860 (funcall insert-definitions
2861 definition
)))))))))))
2862 (funcall insert-definitions tree
))))))))
2865 (defun org-export-as
2866 (backend &optional subtreep visible-only body-only ext-plist
)
2867 "Transcode current Org buffer into BACKEND code.
2869 BACKEND is either an export back-end, as returned by, e.g.,
2870 `org-export-create-backend', or a symbol referring to
2871 a registered back-end.
2873 If narrowing is active in the current buffer, only transcode its
2876 If a region is active, transcode that region.
2878 When optional argument SUBTREEP is non-nil, transcode the
2879 sub-tree at point, extracting information from the headline
2882 When optional argument VISIBLE-ONLY is non-nil, don't export
2883 contents of hidden elements.
2885 When optional argument BODY-ONLY is non-nil, only return body
2886 code, without surrounding template.
2888 Optional argument EXT-PLIST, when provided, is a property list
2889 with external parameters overriding Org default settings, but
2890 still inferior to file-local settings.
2892 Return code as a string."
2893 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2894 (org-export-barf-if-invalid-backend backend
)
2897 ;; Narrow buffer to an appropriate region or subtree for
2898 ;; parsing. If parsing subtree, be sure to remove main headline
2900 (cond ((org-region-active-p)
2901 (narrow-to-region (region-beginning) (region-end)))
2903 (org-narrow-to-subtree)
2904 (goto-char (point-min))
2906 (narrow-to-region (point) (point-max))))
2907 ;; Initialize communication channel with original buffer
2908 ;; attributes, unavailable in its copy.
2909 (let* ((org-export-current-backend (org-export-backend-name backend
))
2910 (info (org-combine-plists
2911 (list :export-options
2913 (list (and subtreep
'subtree
)
2914 (and visible-only
'visible-only
)
2915 (and body-only
'body-only
))))
2916 (org-export--get-buffer-attributes)))
2919 (mapcar (lambda (o) (and (eq (nth 4 o
) 'parse
) (nth 1 o
)))
2920 (append (org-export-get-all-options backend
)
2921 org-export-options-alist
))))
2923 ;; Update communication channel and get parse tree. Buffer
2924 ;; isn't parsed directly. Instead, all buffer modifications
2925 ;; and consequent parsing are undertaken in a temporary copy.
2926 (org-export-with-buffer-copy
2927 ;; Run first hook with current back-end's name as argument.
2928 (run-hook-with-args 'org-export-before-processing-hook
2929 (org-export-backend-name backend
))
2930 ;; Include files, delete comments and expand macros.
2931 (org-export-expand-include-keyword)
2932 (org-export--delete-comments)
2933 (org-macro-initialize-templates)
2934 (org-macro-replace-all org-macro-templates nil parsed-keywords
)
2935 ;; Refresh buffer properties and radio targets after
2936 ;; potentially invasive previous changes. Likewise, do it
2937 ;; again after executing Babel code.
2938 (org-set-regexps-and-options)
2939 (org-update-radio-target-regexp)
2940 (org-export-execute-babel-code)
2941 (org-set-regexps-and-options)
2942 (org-update-radio-target-regexp)
2943 ;; Run last hook with current back-end's name as argument.
2944 ;; Update buffer properties and radio targets one last time
2946 (goto-char (point-min))
2948 (run-hook-with-args 'org-export-before-parsing-hook
2949 (org-export-backend-name backend
)))
2950 (org-set-regexps-and-options)
2951 (org-update-radio-target-regexp)
2952 ;; Update communication channel with environment. Also
2953 ;; install user's and developer's filters.
2955 (org-export-install-filters
2957 info
(org-export-get-environment backend subtreep ext-plist
))))
2958 ;; Call options filters and update export options. We do not
2959 ;; use `org-export-filter-apply-functions' here since the
2960 ;; arity of such filters is different.
2961 (let ((backend-name (org-export-backend-name backend
)))
2962 (dolist (filter (plist-get info
:filter-options
))
2963 (let ((result (funcall filter info backend-name
)))
2964 (when result
(setq info result
)))))
2965 ;; Expand export-specific set of macros: {{{author}}},
2966 ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}. It must
2967 ;; be done once regular macros have been expanded, since
2968 ;; parsed keywords may contain one of them.
2969 (org-macro-replace-all
2971 (cons "author" (org-element-interpret-data (plist-get info
:author
)))
2973 (let* ((date (plist-get info
:date
))
2974 (value (or (org-element-interpret-data date
) "")))
2975 (if (and (consp date
)
2977 (eq (org-element-type (car date
)) 'timestamp
))
2978 (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
2979 (format "(org-timestamp-format '%S \"$1\")"
2980 (org-element-copy (car date
)))
2983 (cons "email" (org-element-interpret-data (plist-get info
:email
)))
2984 (cons "title" (org-element-interpret-data (plist-get info
:title
)))
2985 (cons "results" "$1"))
2989 (setq tree
(org-element-parse-buffer nil visible-only
))
2990 ;; Merge footnote definitions outside scope into parse tree.
2991 (org-export--merge-external-footnote-definitions tree
)
2992 ;; Prune tree from non-exported elements and transform
2993 ;; uninterpreted elements or objects in both parse tree and
2994 ;; communication channel.
2995 (org-export--prune-tree tree info
)
2996 (org-export--remove-uninterpreted-data tree info
)
2997 ;; Call parse tree filters.
2999 (org-export-filter-apply-functions
3000 (plist-get info
:filter-parse-tree
) tree info
))
3001 ;; Now tree is complete, compute its properties and add them
3002 ;; to communication channel.
3005 info
(org-export-collect-tree-properties tree info
)))
3006 ;; Eventually transcode TREE. Wrap the resulting string into
3008 (let* ((body (org-element-normalize-string
3009 (or (org-export-data tree info
) "")))
3010 (inner-template (cdr (assq 'inner-template
3011 (plist-get info
:translate-alist
))))
3012 (full-body (org-export-filter-apply-functions
3013 (plist-get info
:filter-body
)
3014 (if (not (functionp inner-template
)) body
3015 (funcall inner-template body info
))
3017 (template (cdr (assq 'template
3018 (plist-get info
:translate-alist
)))))
3019 ;; Remove all text properties since they cannot be
3020 ;; retrieved from an external process. Finally call
3021 ;; final-output filter and return result.
3023 (org-export-filter-apply-functions
3024 (plist-get info
:filter-final-output
)
3025 (if (or (not (functionp template
)) body-only
) full-body
3026 (funcall template full-body info
))
3030 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3031 "Transcode STRING into BACKEND code.
3033 BACKEND is either an export back-end, as returned by, e.g.,
3034 `org-export-create-backend', or a symbol referring to
3035 a registered back-end.
3037 When optional argument BODY-ONLY is non-nil, only return body
3038 code, without preamble nor postamble.
3040 Optional argument EXT-PLIST, when provided, is a property list
3041 with external parameters overriding Org default settings, but
3042 still inferior to file-local settings.
3044 Return code as a string."
3047 (let ((org-inhibit-startup t
)) (org-mode))
3048 (org-export-as backend nil nil body-only ext-plist
)))
3051 (defun org-export-replace-region-by (backend)
3052 "Replace the active region by its export to BACKEND.
3053 BACKEND is either an export back-end, as returned by, e.g.,
3054 `org-export-create-backend', or a symbol referring to
3055 a registered back-end."
3056 (unless (org-region-active-p) (user-error "No active region to replace"))
3058 (org-export-string-as
3059 (delete-and-extract-region (region-beginning) (region-end)) backend t
)))
3062 (defun org-export-insert-default-template (&optional backend subtreep
)
3063 "Insert all export keywords with default values at beginning of line.
3065 BACKEND is a symbol referring to the name of a registered export
3066 back-end, for which specific export options should be added to
3067 the template, or `default' for default template. When it is nil,
3068 the user will be prompted for a category.
3070 If SUBTREEP is non-nil, export configuration will be set up
3071 locally for the subtree through node properties."
3073 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3074 (when (and subtreep
(org-before-first-heading-p))
3075 (user-error "No subtree to set export options for"))
3076 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3080 (org-completing-read
3081 "Options category: "
3084 (symbol-name (org-export-backend-name b
)))
3085 org-export-registered-backends
))
3088 ;; Populate OPTIONS and KEYWORDS.
3089 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3090 ((org-export-backend-p backend
)
3091 (org-export-backend-options backend
))
3092 (t (org-export-backend-options
3093 (org-export-get-backend backend
)))))
3094 (let ((keyword (nth 1 entry
))
3095 (option (nth 2 entry
)))
3097 (keyword (unless (assoc keyword keywords
)
3099 (if (eq (nth 4 entry
) 'split
)
3100 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3101 (eval (nth 3 entry
)))))
3102 (push (cons keyword value
) keywords
))))
3103 (option (unless (assoc option options
)
3104 (push (cons option
(eval (nth 3 entry
))) options
))))))
3105 ;; Move to an appropriate location in order to insert options.
3106 (unless subtreep
(beginning-of-line))
3107 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3111 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3112 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3115 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3117 (insert "#+OPTIONS:")
3120 (< (+ width
(length (car items
)) 1) fill-column
))
3121 (let ((item (pop items
)))
3123 (incf width
(1+ (length item
))))))
3125 ;; Then the rest of keywords, in the order specified in either
3126 ;; `org-export-options-alist' or respective export back-ends.
3127 (dolist (key (nreverse keywords
))
3128 (let ((val (cond ((equal (car key
) "DATE")
3131 (org-insert-time-stamp (current-time)))))
3132 ((equal (car key
) "TITLE")
3133 (or (let ((visited-file
3134 (buffer-file-name (buffer-base-buffer))))
3136 (file-name-sans-extension
3137 (file-name-nondirectory visited-file
))))
3138 (buffer-name (buffer-base-buffer))))
3140 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3144 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3146 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3147 "Expand every include keyword in buffer.
3148 Optional argument INCLUDED is a list of included file names along
3149 with their line restriction, when appropriate. It is used to
3150 avoid infinite recursion. Optional argument DIR is the current
3151 working directory. It is used to properly resolve relative
3152 paths. Optional argument FOOTNOTES is a hash-table used for
3153 storing and resolving footnotes. It is created automatically."
3154 (let ((case-fold-search t
)
3155 (file-prefix (make-hash-table :test
#'equal
))
3157 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3158 (include-re "^[ \t]*#\\+INCLUDE:"))
3159 ;; If :minlevel is not set the text-property
3160 ;; `:org-include-induced-level' will be used to determine the
3161 ;; relative level when expanding INCLUDE.
3162 ;; Only affects included Org documents.
3163 (goto-char (point-min))
3164 (while (re-search-forward include-re nil t
)
3165 (put-text-property (line-beginning-position) (line-end-position)
3166 :org-include-induced-level
3167 (1+ (org-reduced-level (or (org-current-level) 0)))))
3168 ;; Expand INCLUDE keywords.
3169 (goto-char (point-min))
3170 (while (re-search-forward include-re nil t
)
3171 (let ((element (save-match-data (org-element-at-point))))
3172 (when (eq (org-element-type element
) 'keyword
)
3174 ;; Extract arguments from keyword's value.
3175 (let* ((value (org-element-property :value element
))
3176 (ind (org-get-indentation))
3180 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3183 (let ((matched (match-string 1 value
)))
3184 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
3186 (setq location
(match-string 2 matched
))
3188 (replace-match "" nil nil matched
1)))
3190 (org-remove-double-quotes
3193 (setq value
(replace-match "" nil nil value
)))))
3195 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
3197 (prog1 (org-not-nil (match-string 1 value
))
3198 (setq value
(replace-match "" nil nil value
)))))
3201 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3203 (prog1 (match-string 1 value
)
3204 (setq value
(replace-match "" nil nil value
)))))
3205 (env (cond ((string-match "\\<example\\>" value
)
3207 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3209 ;; Minimal level of included file defaults to the child
3210 ;; level of the current headline, if any, or one. It
3211 ;; only applies is the file is meant to be included as
3215 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3216 (prog1 (string-to-number (match-string 1 value
))
3217 (setq value
(replace-match "" nil nil value
)))
3218 (get-text-property (point)
3219 :org-include-induced-level
))))
3220 (src-args (and (eq env
'literal
)
3221 (match-string 1 value
)))
3222 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3223 (match-string 1 value
))))
3225 (delete-region (point) (progn (forward-line) (point)))
3228 ((not (file-readable-p file
))
3229 (error "Cannot include file %s" file
))
3230 ;; Check if files has already been parsed. Look after
3231 ;; inclusion lines too, as different parts of the same file
3232 ;; can be included too.
3233 ((member (list file lines
) included
)
3234 (error "Recursive file inclusion: %s" file
))
3239 (let ((ind-str (make-string ind ?
))
3240 (arg-str (if (stringp src-args
)
3241 (format " %s" src-args
)
3244 (org-escape-code-in-string
3245 (org-export--prepare-file-contents file lines
))))
3246 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3247 ind-str block arg-str contents ind-str block
))))
3250 (let ((ind-str (make-string ind ?
))
3252 (org-export--prepare-file-contents file lines
)))
3253 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3254 ind-str block contents ind-str block
))))
3258 (let ((org-inhibit-startup t
)
3261 (org-export--inclusion-absolute-lines
3262 file location only-contents lines
)
3266 (org-export--prepare-file-contents
3267 file lines ind minlevel
3268 (or (gethash file file-prefix
)
3269 (puthash file
(incf current-prefix
) file-prefix
))
3271 (org-export-expand-include-keyword
3272 (cons (list file lines
) included
)
3273 (file-name-directory file
)
3276 ;; Expand footnotes after all files have been included.
3277 ;; Footnotes are stored at end of buffer.
3279 (org-with-wide-buffer
3280 (goto-char (point-max))
3281 (maphash (lambda (k v
) (insert (format "\n[%s] %s\n" k v
)))
3282 footnotes
)))))))))))
3284 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3285 "Resolve absolute lines for an included file with file-link.
3287 FILE is string file-name of the file to include. LOCATION is a
3288 string name within FILE to be included (located via
3289 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3290 contents of the named element will be included, as determined
3291 Org-Element. If LINES is non-nil only those lines are included.
3293 Return a string of lines to be included in the format expected by
3294 `org-export--prepare-file-contents'."
3296 (insert-file-contents file
)
3297 (unless (eq major-mode
'org-mode
)
3298 (let ((org-inhibit-startup t
)) (org-mode)))
3300 ;; Enforce consistent search.
3301 (let ((org-link-search-must-match-exact-headline nil
))
3302 (org-link-search location
))
3304 (error "%s for %s::%s" (error-message-string err
) file location
)))
3305 (let* ((element (org-element-at-point))
3307 (and only-contents
(org-element-property :contents-begin element
))))
3309 (or contents-begin
(org-element-property :begin element
))
3310 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3311 (when (and only-contents
3312 (memq (org-element-type element
) '(headline inlinetask
)))
3313 ;; Skip planning line and property-drawer.
3314 (goto-char (point-min))
3315 (when (org-looking-at-p org-planning-line-re
) (forward-line))
3316 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3317 (unless (bolp) (forward-line))
3318 (narrow-to-region (point) (point-max))))
3320 (org-skip-whitespace)
3322 (let* ((lines (split-string lines
"-"))
3323 (lbeg (string-to-number (car lines
)))
3324 (lend (string-to-number (cadr lines
)))
3325 (beg (if (zerop lbeg
) (point-min)
3326 (goto-char (point-min))
3327 (forward-line (1- lbeg
))
3329 (end (if (zerop lend
) (point-max)
3331 (forward-line (1- lend
))
3333 (narrow-to-region beg end
)))
3334 (let ((end (point-max)))
3335 (goto-char (point-min))
3337 (let ((start-line (line-number-at-pos)))
3343 (while (< (point) end
) (incf counter
) (forward-line))
3346 (defun org-export--update-footnote-label (ref-begin digit-label id
)
3347 "Prefix footnote-label at point REF-BEGIN in buffer with ID.
3349 REF-BEGIN corresponds to the property `:begin' of objects of type
3350 footnote-definition and footnote-reference.
3352 If DIGIT-LABEL is non-nil the label is assumed to be of the form
3353 \[N] where N is one or more numbers.
3355 Return the new label."
3356 (goto-char (1+ ref-begin
))
3357 (buffer-substring (point)
3359 (if digit-label
(insert (format "fn:%d-" id
))
3361 (insert (format "%d-" id
)))
3362 (1- (search-forward "]")))))
3364 (defun org-export--prepare-file-contents
3365 (file &optional lines ind minlevel id footnotes
)
3366 "Prepare contents of FILE for inclusion and return it as a string.
3368 When optional argument LINES is a string specifying a range of
3369 lines, include only those lines.
3371 Optional argument IND, when non-nil, is an integer specifying the
3372 global indentation of returned contents. Since its purpose is to
3373 allow an included file to stay in the same environment it was
3374 created \(i.e. a list item), it doesn't apply past the first
3375 headline encountered.
3377 Optional argument MINLEVEL, when non-nil, is an integer
3378 specifying the level that any top-level headline in the included
3380 Optional argument ID is an integer that will be inserted before
3381 each footnote definition and reference if FILE is an Org file.
3382 This is useful to avoid conflicts when more than one Org file
3383 with footnotes is included in a document.
3385 Optional argument FOOTNOTES is a hash-table to store footnotes in
3386 the included document.
3389 (insert-file-contents file
)
3391 (let* ((lines (split-string lines
"-"))
3392 (lbeg (string-to-number (car lines
)))
3393 (lend (string-to-number (cadr lines
)))
3394 (beg (if (zerop lbeg
) (point-min)
3395 (goto-char (point-min))
3396 (forward-line (1- lbeg
))
3398 (end (if (zerop lend
) (point-max)
3399 (goto-char (point-min))
3400 (forward-line (1- lend
))
3402 (narrow-to-region beg end
)))
3403 ;; Remove blank lines at beginning and end of contents. The logic
3404 ;; behind that removal is that blank lines around include keyword
3405 ;; override blank lines in included file.
3406 (goto-char (point-min))
3407 (org-skip-whitespace)
3409 (delete-region (point-min) (point))
3410 (goto-char (point-max))
3411 (skip-chars-backward " \r\t\n")
3413 (delete-region (point) (point-max))
3414 ;; If IND is set, preserve indentation of include keyword until
3415 ;; the first headline encountered.
3417 (unless (eq major-mode
'org-mode
)
3418 (let ((org-inhibit-startup t
)) (org-mode)))
3419 (goto-char (point-min))
3420 (let ((ind-str (make-string ind ?
)))
3421 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3422 ;; Do not move footnote definitions out of column 0.
3423 (unless (and (looking-at org-footnote-definition-re
)
3424 (eq (org-element-type (org-element-at-point))
3425 'footnote-definition
))
3428 ;; When MINLEVEL is specified, compute minimal level for headlines
3429 ;; in the file (CUR-MIN), and remove stars to each headline so
3430 ;; that headlines with minimal level have a level of MINLEVEL.
3432 (unless (eq major-mode
'org-mode
)
3433 (let ((org-inhibit-startup t
)) (org-mode)))
3434 (org-with-limited-levels
3435 (let ((levels (org-map-entries
3436 (lambda () (org-reduced-level (org-current-level))))))
3438 (let ((offset (- minlevel
(apply 'min levels
))))
3439 (unless (zerop offset
)
3440 (when org-odd-levels-only
(setq offset
(* offset
2)))
3441 ;; Only change stars, don't bother moving whole
3444 (lambda () (if (< offset
0) (delete-char (abs offset
))
3445 (insert (make-string offset ?
*)))))))))))
3446 ;; Append ID to all footnote references and definitions, so they
3447 ;; become file specific and cannot collide with footnotes in other
3448 ;; included files. Further, collect relevant footnotes outside of
3451 (let ((marker-min (point-min-marker))
3452 (marker-max (point-max-marker)))
3453 (goto-char (point-min))
3454 (while (re-search-forward org-footnote-re nil t
)
3455 (let ((reference (org-element-context)))
3456 (when (eq (org-element-type reference
) 'footnote-reference
)
3457 (let* ((label (org-element-property :label reference
))
3459 (and label
(org-string-match-p "\\`[0-9]+\\'" label
))))
3460 ;; Update the footnote-reference at point and collect
3461 ;; the new label, which is only used for footnotes
3464 ;; If label is akin to [1] convert it to [fn:ID-1].
3465 ;; Otherwise add "ID-" after "fn:".
3466 (let ((new-label (org-export--update-footnote-label
3467 (org-element-property :begin reference
)
3469 (unless (eq (org-element-property :type reference
) 'inline
)
3470 (org-with-wide-buffer
3471 (let* ((definition (org-footnote-get-definition label
))
3472 (beginning (nth 1 definition
)))
3475 "Definition not found for footnote %s in file %s"
3477 (if (or (< beginning marker-min
)
3478 (> beginning marker-max
))
3479 ;; Store since footnote-definition is
3480 ;; outside of LINES.
3482 (org-element-normalize-string
3485 ;; Update label of definition since it is
3486 ;; included directly.
3487 (org-export--update-footnote-label
3488 beginning digit-label id
)))))))))))
3489 (set-marker marker-min nil
)
3490 (set-marker marker-max nil
)))
3491 (org-element-normalize-string (buffer-string))))
3493 (defun org-export-execute-babel-code ()
3494 "Execute every Babel code in the visible part of current buffer."
3495 ;; Get a pristine copy of current buffer so Babel references can be
3496 ;; properly resolved.
3497 (let ((reference (org-export-copy-buffer)))
3498 (unwind-protect (org-babel-exp-process-buffer reference
)
3499 (kill-buffer reference
))))
3501 (defun org-export--copy-to-kill-ring-p ()
3502 "Return a non-nil value when output should be added to the kill ring.
3503 See also `org-export-copy-to-kill-ring'."
3504 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3505 (not (or executing-kbd-macro noninteractive
))
3506 (eq org-export-copy-to-kill-ring t
)))
3510 ;;; Tools For Back-Ends
3512 ;; A whole set of tools is available to help build new exporters. Any
3513 ;; function general enough to have its use across many back-ends
3514 ;; should be added here.
3516 ;;;; For Affiliated Keywords
3518 ;; `org-export-read-attribute' reads a property from a given element
3519 ;; as a plist. It can be used to normalize affiliated keywords'
3522 ;; Since captions can span over multiple lines and accept dual values,
3523 ;; their internal representation is a bit tricky. Therefore,
3524 ;; `org-export-get-caption' transparently returns a given element's
3525 ;; caption as a secondary string.
3527 (defun org-export-read-attribute (attribute element
&optional property
)
3528 "Turn ATTRIBUTE property from ELEMENT into a plist.
3530 When optional argument PROPERTY is non-nil, return the value of
3531 that property within attributes.
3533 This function assumes attributes are defined as \":keyword
3534 value\" pairs. It is appropriate for `:attr_html' like
3537 All values will become strings except the empty string and
3538 \"nil\", which will become nil. Also, values containing only
3539 double quotes will be read as-is, which means that \"\" value
3540 will become the empty string."
3541 (let* ((prepare-value
3544 (cond ((member str
'(nil "" "nil")) nil
)
3545 ((string-match "^\"\\(\"+\\)?\"$" str
)
3546 (or (match-string 1 str
) ""))
3549 (let ((value (org-element-property attribute element
)))
3551 (let ((s (mapconcat 'identity value
" ")) result
)
3552 (while (string-match
3553 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3555 (let ((value (substring s
0 (match-beginning 0))))
3556 (push (funcall prepare-value value
) result
))
3557 (push (intern (match-string 1 s
)) result
)
3558 (setq s
(substring s
(match-end 0))))
3559 ;; Ignore any string before first property with `cdr'.
3560 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3561 (if property
(plist-get attributes property
) attributes
)))
3563 (defun org-export-get-caption (element &optional shortp
)
3564 "Return caption from ELEMENT as a secondary string.
3566 When optional argument SHORTP is non-nil, return short caption,
3567 as a secondary string, instead.
3569 Caption lines are separated by a white space."
3570 (let ((full-caption (org-element-property :caption element
)) caption
)
3571 (dolist (line full-caption
(cdr caption
))
3572 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3574 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3577 ;;;; For Derived Back-ends
3579 ;; `org-export-with-backend' is a function allowing to locally use
3580 ;; another back-end to transcode some object or element. In a derived
3581 ;; back-end, it may be used as a fall-back function once all specific
3582 ;; cases have been treated.
3584 (defun org-export-with-backend (backend data
&optional contents info
)
3585 "Call a transcoder from BACKEND on DATA.
3586 BACKEND is an export back-end, as returned by, e.g.,
3587 `org-export-create-backend', or a symbol referring to
3588 a registered back-end. DATA is an Org element, object, secondary
3589 string or string. CONTENTS, when non-nil, is the transcoded
3590 contents of DATA element, as a string. INFO, when non-nil, is
3591 the communication channel used for export, as a plist."
3592 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3593 (org-export-barf-if-invalid-backend backend
)
3594 (let ((type (org-element-type data
)))
3595 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3596 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3597 (transcoder (cdr (assq type all-transcoders
))))
3598 (if (not (functionp transcoder
))
3599 (error "No foreign transcoder available")
3601 transcoder data contents
3605 :translate-alist all-transcoders
3606 :exported-data
(make-hash-table :test
#'eq
:size
401)))))))))
3609 ;;;; For Export Snippets
3611 ;; Every export snippet is transmitted to the back-end. Though, the
3612 ;; latter will only retain one type of export-snippet, ignoring
3613 ;; others, based on the former's target back-end. The function
3614 ;; `org-export-snippet-backend' returns that back-end for a given
3617 (defun org-export-snippet-backend (export-snippet)
3618 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3619 Translation, with `org-export-snippet-translation-alist', is
3621 (let ((back-end (org-element-property :back-end export-snippet
)))
3623 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3629 ;; `org-export-collect-footnote-definitions' is a tool to list
3630 ;; actually used footnotes definitions in the whole parse tree, or in
3631 ;; a headline, in order to add footnote listings throughout the
3634 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3635 ;; back-ends, when they need to attach the footnote definition only to
3636 ;; the first occurrence of the corresponding label.
3638 ;; `org-export-get-footnote-definition' and
3639 ;; `org-export-get-footnote-number' provide easier access to
3640 ;; additional information relative to a footnote reference.
3642 (defun org-export-get-footnote-definition (footnote-reference info
)
3643 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3644 INFO is the plist used as a communication channel. If no such
3645 definition can be found, raise an error."
3646 (let ((label (org-element-property :label footnote-reference
)))
3647 (if (not label
) (org-element-contents footnote-reference
)
3648 (let ((cache (or (plist-get info
:footnote-definition-cache
)
3649 (let ((hash (make-hash-table :test
#'equal
)))
3650 (plist-put info
:footnote-definition-cache hash
)
3652 (or (gethash label cache
)
3654 (org-element-map (plist-get info
:parse-tree
)
3655 '(footnote-definition footnote-reference
)
3657 (and (equal (org-element-property :label f
) label
)
3658 (org-element-contents f
)))
3661 (error "Definition not found for footnote %s" label
))))))
3663 (defun org-export--footnote-reference-map
3664 (function data info
&optional body-first
)
3665 "Apply FUNCTION on every footnote reference in DATA.
3666 INFO is a plist containing export state. By default, as soon as
3667 a new footnote reference is encountered, FUNCTION is called onto
3668 its definition. However, if BODY-FIRST is non-nil, this step is
3669 delayed until the end of the process."
3670 (let* ((definitions)
3672 (search-ref) ; For byte-compiler.
3674 (lambda (data delayp
)
3675 ;; Search footnote references through DATA, filling
3676 ;; SEEN-REFS along the way. When DELAYP is non-nil, store
3677 ;; footnote definitions so they can be entered later.
3678 (org-element-map data
'footnote-reference
3680 (funcall function f
)
3681 (let ((--label (org-element-property :label f
)))
3682 (unless (and --label
(member --label seen-refs
))
3683 (when --label
(push --label seen-refs
))
3684 ;; Search for subsequent references in footnote
3685 ;; definition so numbering follows reading logic,
3686 ;; unless DELAYP in non-nil.
3689 (push (org-export-get-footnote-definition f info
)
3691 ;; Do not force entering inline definitions,
3692 ;; since `org-element-map' already traverses them
3693 ;; at the right time.
3694 ((eq (org-element-property :type f
) 'inline
))
3695 (t (funcall search-ref
3696 (org-export-get-footnote-definition f info
)
3699 ;; Don't enter footnote definitions since it will happen
3700 ;; when their first reference is found. Moreover, if
3701 ;; DELAYP is non-nil, make sure we postpone entering
3702 ;; definitions of inline references.
3703 (if delayp
'(footnote-definition footnote-reference
)
3704 'footnote-definition
)))))
3705 (funcall search-ref data body-first
)
3706 (funcall search-ref
(nreverse definitions
) nil
)))
3708 (defun org-export-collect-footnote-definitions (info &optional data body-first
)
3709 "Return an alist between footnote numbers, labels and definitions.
3711 INFO is the current export state, as a plist.
3713 Definitions are collected throughout the whole parse tree, or
3716 Sorting is done by order of references. As soon as a new
3717 reference is encountered, other references are searched within
3718 its definition. However, if BODY-FIRST is non-nil, this step is
3719 delayed after the whole tree is checked. This alters results
3720 when references are found in footnote definitions.
3722 Definitions either appear as Org data or as a secondary string
3723 for inlined footnotes. Unreferenced definitions are ignored."
3724 (let ((n 0) labels alist
)
3725 (org-export--footnote-reference-map
3727 ;; Collect footnote number, label and definition.
3728 (let ((l (org-element-property :label f
)))
3729 (unless (and l
(member l labels
))
3731 (push (list n l
(org-export-get-footnote-definition f info
)) alist
))
3732 (when l
(push l labels
))))
3733 (or data
(plist-get info
:parse-tree
)) info body-first
)
3736 (defun org-export-footnote-first-reference-p
3737 (footnote-reference info
&optional data body-first
)
3738 "Non-nil when a footnote reference is the first one for its label.
3740 FOOTNOTE-REFERENCE is the footnote reference being considered.
3741 INFO is a plist containing current export state.
3743 Search is done throughout the whole parse tree, or DATA when
3746 By default, as soon as a new footnote reference is encountered,
3747 other references are searched within its definition. However, if
3748 BODY-FIRST is non-nil, this step is delayed after the whole tree
3749 is checked. This alters results when references are found in
3750 footnote definitions."
3751 (let ((label (org-element-property :label footnote-reference
)))
3752 ;; Anonymous footnotes are always a first reference.
3755 (org-export--footnote-reference-map
3757 (let ((l (org-element-property :label f
)))
3758 (when (and l label
(string= label l
))
3759 (throw 'exit
(eq footnote-reference f
)))))
3760 (or data
(plist-get info
:parse-tree
)) info body-first
)))))
3762 (defun org-export-get-footnote-number (footnote info
&optional data body-first
)
3763 "Return number associated to a footnote.
3765 FOOTNOTE is either a footnote reference or a footnote definition.
3766 INFO is the plist containing export state.
3768 Number is unique throughout the whole parse tree, or DATA, when
3771 By default, as soon as a new footnote reference is encountered,
3772 counting process moves into its definition. However, if
3773 BODY-FIRST is non-nil, this step is delayed until the end of the
3774 process, leading to a different order when footnotes are nested."
3777 (label (org-element-property :label footnote
)))
3779 (org-export--footnote-reference-map
3781 (let ((l (org-element-property :label f
)))
3783 ;; Anonymous footnote match: return number.
3784 ((and (not l
) (not label
) (eq footnote f
)) (throw 'exit
(1+ count
)))
3785 ;; Labels match: return number.
3786 ((and label l
(string= label l
)) (throw 'exit
(1+ count
)))
3787 ;; Otherwise store label and increase counter if label
3788 ;; wasn't encountered yet.
3789 ((not l
) (incf count
))
3790 ((not (member l seen
)) (push l seen
) (incf count
)))))
3791 (or data
(plist-get info
:parse-tree
)) info body-first
))))
3796 ;; `org-export-get-relative-level' is a shortcut to get headline
3797 ;; level, relatively to the lower headline level in the parsed tree.
3799 ;; `org-export-get-headline-number' returns the section number of an
3800 ;; headline, while `org-export-number-to-roman' allows to convert it
3801 ;; to roman numbers. With an optional argument,
3802 ;; `org-export-get-headline-number' returns a number to unnumbered
3803 ;; headlines (used for internal id).
3805 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3806 ;; `org-export-last-sibling-p' are three useful predicates when it
3807 ;; comes to fulfill the `:headline-levels' property.
3809 ;; `org-export-get-tags', `org-export-get-category' and
3810 ;; `org-export-get-node-property' extract useful information from an
3811 ;; headline or a parent headline. They all handle inheritance.
3813 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3814 ;; as a secondary string, suitable for table of contents. It falls
3815 ;; back onto default title.
3817 (defun org-export-get-relative-level (headline info
)
3818 "Return HEADLINE relative level within current parsed tree.
3819 INFO is a plist holding contextual information."
3820 (+ (org-element-property :level headline
)
3821 (or (plist-get info
:headline-offset
) 0)))
3823 (defun org-export-low-level-p (headline info
)
3824 "Non-nil when HEADLINE is considered as low level.
3826 INFO is a plist used as a communication channel.
3828 A low level headlines has a relative level greater than
3829 `:headline-levels' property value.
3831 Return value is the difference between HEADLINE relative level
3832 and the last level being considered as high enough, or nil."
3833 (let ((limit (plist-get info
:headline-levels
)))
3834 (when (wholenump limit
)
3835 (let ((level (org-export-get-relative-level headline info
)))
3836 (and (> level limit
) (- level limit
))))))
3838 (defun org-export-get-headline-number (headline info
)
3839 "Return numbered HEADLINE numbering as a list of numbers.
3840 INFO is a plist holding contextual information."
3841 (and (org-export-numbered-headline-p headline info
)
3842 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
3844 (defun org-export-numbered-headline-p (headline info
)
3845 "Return a non-nil value if HEADLINE element should be numbered.
3846 INFO is a plist used as a communication channel."
3848 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head
)))
3849 (org-element-lineage headline nil t
))
3850 (let ((sec-num (plist-get info
:section-numbers
))
3851 (level (org-export-get-relative-level headline info
)))
3852 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
3854 (defun org-export-number-to-roman (n)
3855 "Convert integer N into a roman numeral."
3856 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3857 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3858 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3862 (number-to-string n
)
3864 (if (>= n
(caar roman
))
3865 (setq n
(- n
(caar roman
))
3866 res
(concat res
(cdar roman
)))
3870 (defun org-export-get-tags (element info
&optional tags inherited
)
3871 "Return list of tags associated to ELEMENT.
3873 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3874 is a plist used as a communication channel.
3876 Select tags (see `org-export-select-tags') and exclude tags (see
3877 `org-export-exclude-tags') are removed from the list.
3879 When non-nil, optional argument TAGS should be a list of strings.
3880 Any tag belonging to this list will also be removed.
3882 When optional argument INHERITED is non-nil, tags can also be
3883 inherited from parent headlines and FILETAGS keywords."
3885 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3886 (member tag
(plist-get info
:exclude-tags
))
3888 (if (not inherited
) (org-element-property :tags element
)
3889 ;; Build complete list of inherited tags.
3890 (let ((current-tag-list (org-element-property :tags element
)))
3891 (dolist (parent (org-element-lineage element
))
3892 (dolist (tag (org-element-property :tags parent
))
3893 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3894 (not (member tag current-tag-list
)))
3895 (push tag current-tag-list
))))
3896 ;; Add FILETAGS keywords and return results.
3897 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3899 (defun org-export-get-node-property (property blob
&optional inherited
)
3900 "Return node PROPERTY value for BLOB.
3902 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3905 If optional argument INHERITED is non-nil, the value can be
3906 inherited from a parent headline.
3908 Return value is a string or nil."
3909 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3910 (org-export-get-parent-headline blob
))))
3911 (if (not inherited
) (org-element-property property blob
)
3912 (let ((parent headline
) value
)
3915 (when (plist-member (nth 1 parent
) property
)
3916 (throw 'found
(org-element-property property parent
)))
3917 (setq parent
(org-element-property :parent parent
))))))))
3919 (defun org-export-get-category (blob info
)
3920 "Return category for element or object BLOB.
3922 INFO is a plist used as a communication channel.
3924 CATEGORY is automatically inherited from a parent headline, from
3925 #+CATEGORY: keyword or created out of original file name. If all
3926 fail, the fall-back value is \"???\"."
3927 (or (org-export-get-node-property :CATEGORY blob t
)
3928 (org-element-map (plist-get info
:parse-tree
) 'keyword
3930 (when (equal (org-element-property :key kwd
) "CATEGORY")
3931 (org-element-property :value kwd
)))
3933 (let ((file (plist-get info
:input-file
)))
3934 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3937 (defun org-export-get-alt-title (headline info
)
3938 "Return alternative title for HEADLINE, as a secondary string.
3939 INFO is a plist used as a communication channel. If no optional
3940 title is defined, fall-back to the regular title."
3941 (let ((alt (org-element-property :ALT_TITLE headline
)))
3942 (if alt
(org-element-parse-secondary-string
3943 alt
(org-element-restriction 'headline
) headline
)
3944 (org-element-property :title headline
))))
3946 (defun org-export-first-sibling-p (blob info
)
3947 "Non-nil when BLOB is the first sibling in its parent.
3948 BLOB is an element or an object. If BLOB is a headline, non-nil
3949 means it is the first sibling in the sub-tree. INFO is a plist
3950 used as a communication channel."
3951 (memq (org-element-type (org-export-get-previous-element blob info
))
3954 (defun org-export-last-sibling-p (blob info
)
3955 "Non-nil when BLOB is the last sibling in its parent.
3956 BLOB is an element or an object. INFO is a plist used as
3957 a communication channel."
3958 (not (org-export-get-next-element blob info
)))
3963 ;; `org-export-get-date' returns a date appropriate for the document
3964 ;; to about to be exported. In particular, it takes care of
3965 ;; `org-export-date-timestamp-format'.
3967 (defun org-export-get-date (info &optional fmt
)
3968 "Return date value for the current document.
3970 INFO is a plist used as a communication channel. FMT, when
3971 non-nil, is a time format string that will be applied on the date
3972 if it consists in a single timestamp object. It defaults to
3973 `org-export-date-timestamp-format' when nil.
3975 A proper date can be a secondary string, a string or nil. It is
3976 meant to be translated with `org-export-data' or alike."
3977 (let ((date (plist-get info
:date
))
3978 (fmt (or fmt org-export-date-timestamp-format
)))
3979 (cond ((not date
) nil
)
3982 (eq (org-element-type (car date
)) 'timestamp
))
3983 (org-timestamp-format (car date
) fmt
))
3989 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
3990 ;; with `org-add-link-type', which see.
3992 ;; `org-export-get-coderef-format' returns an appropriate format
3993 ;; string for coderefs.
3995 ;; `org-export-inline-image-p' returns a non-nil value when the link
3996 ;; provided should be considered as an inline image.
3998 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3999 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
4000 ;; returns an appropriate unique identifier when found, or nil.
4002 ;; `org-export-resolve-id-link' returns the first headline with
4003 ;; specified id or custom-id in parse tree, the path to the external
4004 ;; file with the id or nil when neither was found.
4006 ;; `org-export-resolve-coderef' associates a reference to a line
4007 ;; number in the element it belongs, or returns the reference itself
4008 ;; when the element isn't numbered.
4010 ;; `org-export-file-uri' expands a filename as stored in :path value
4011 ;; of a "file" link into a file URI.
4013 (defun org-export-custom-protocol-maybe (link desc backend
)
4014 "Try exporting LINK with a dedicated function.
4016 DESC is its description, as a string, or nil. BACKEND is the
4017 back-end used for export, as a symbol.
4019 Return output as a string, or nil if no protocol handles LINK.
4021 A custom protocol has precedence over regular back-end export.
4022 The function ignores links with an implicit type (e.g.,
4024 (let ((type (org-element-property :type link
)))
4025 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
4027 (let ((protocol (nth 2 (assoc type org-link-protocols
))))
4028 (and (functionp protocol
)
4030 (org-link-unescape (org-element-property :path link
))
4034 (defun org-export-get-coderef-format (path desc
)
4035 "Return format string for code reference link.
4036 PATH is the link path. DESC is its description."
4038 (cond ((not desc
) "%s")
4039 ((string-match (regexp-quote (concat "(" path
")")) desc
)
4040 (replace-match "%s" t t desc
))
4043 (defun org-export-inline-image-p (link &optional rules
)
4044 "Non-nil if LINK object points to an inline image.
4046 Optional argument is a set of RULES defining inline images. It
4047 is an alist where associations have the following shape:
4051 Applying a rule means apply REGEXP against LINK's path when its
4052 type is TYPE. The function will return a non-nil value if any of
4053 the provided rules is non-nil. The default rule is
4054 `org-export-default-inline-image-rule'.
4056 This only applies to links without a description."
4057 (and (not (org-element-contents link
))
4058 (let ((case-fold-search t
))
4060 (dolist (rule (or rules org-export-default-inline-image-rule
))
4061 (and (string= (org-element-property :type link
) (car rule
))
4062 (org-string-match-p (cdr rule
)
4063 (org-element-property :path link
))
4064 (throw 'exit t
)))))))
4066 (defun org-export-resolve-coderef (ref info
)
4067 "Resolve a code reference REF.
4069 INFO is a plist used as a communication channel.
4071 Return associated line number in source code, or REF itself,
4072 depending on src-block or example element's switches. Throw an
4073 error if no block contains REF."
4074 (or (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
4077 (insert (org-trim (org-element-property :value el
)))
4078 (let* ((label-fmt (regexp-quote
4079 (or (org-element-property :label-fmt el
)
4080 org-coderef-label-format
)))
4082 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
4083 (format label-fmt ref
))))
4084 ;; Element containing REF is found. Resolve it to
4085 ;; either a label or a line number, as needed.
4086 (when (re-search-backward ref-re nil t
)
4088 ((org-element-property :use-labels el
) ref
)
4089 ((eq (org-element-property :number-lines el
) 'continued
)
4090 (+ (org-export-get-loc el info
) (line-number-at-pos)))
4091 (t (line-number-at-pos)))))))
4093 (user-error "Unable to resolve code reference: %s" ref
)))
4095 (defun org-export-resolve-fuzzy-link (link info
)
4096 "Return LINK destination.
4098 INFO is a plist holding contextual information.
4100 Return value can be an object, an element, or nil:
4102 - If LINK path matches a target object (i.e. <<path>>) return it.
4104 - If LINK path exactly matches the name affiliated keyword
4105 \(i.e. #+NAME: path) of an element, return that element.
4107 - If LINK path exactly matches any headline name, return that
4110 - Otherwise, throw an error.
4112 Assume LINK type is \"fuzzy\". White spaces are not
4114 (let* ((raw-path (org-link-unescape (org-element-property :path link
)))
4115 (headline-only (eq (string-to-char raw-path
) ?
*))
4116 ;; Split PATH at white spaces so matches are space
4118 (path (org-split-string
4119 (if headline-only
(substring raw-path
1) raw-path
)))
4121 (or (plist-get info
:resolve-fuzzy-link-cache
)
4122 (plist-get (plist-put info
4123 :resolve-fuzzy-link-cache
4124 (make-hash-table :test
#'equal
))
4125 :resolve-fuzzy-link-cache
)))
4126 (cached (gethash path link-cache
'not-found
)))
4127 (if (not (eq cached
'not-found
)) cached
4128 (let ((ast (plist-get info
:parse-tree
)))
4132 ;; First try to find a matching "<<path>>" unless user
4133 ;; specified he was looking for a headline (path starts with
4134 ;; a "*" character).
4135 ((and (not headline-only
)
4136 (org-element-map ast
'target
4138 (and (equal (org-split-string
4139 (org-element-property :value datum
))
4142 info
'first-match
)))
4143 ;; Then try to find an element with a matching "#+NAME: path"
4144 ;; affiliated keyword.
4145 ((and (not headline-only
)
4146 (org-element-map ast org-element-all-elements
4148 (let ((name (org-element-property :name datum
)))
4149 (and name
(equal (org-split-string name
) path
) datum
)))
4150 info
'first-match
)))
4151 ;; Try to find a matching headline.
4152 ((org-element-map ast
'headline
4154 (and (equal (org-split-string
4155 (replace-regexp-in-string
4156 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4157 (org-element-property :raw-value h
)))
4161 (t (user-error "Unable to resolve link \"%s\"" raw-path
)))
4164 (defun org-export-resolve-id-link (link info
)
4165 "Return headline referenced as LINK destination.
4167 INFO is a plist used as a communication channel.
4169 Return value can be the headline element matched in current parse
4170 tree or a file name. Assume LINK type is either \"id\" or
4171 \"custom-id\". Throw an error if no match is found."
4172 (let ((id (org-element-property :path link
)))
4173 ;; First check if id is within the current parse tree.
4174 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4176 (when (or (equal (org-element-property :ID headline
) id
)
4177 (equal (org-element-property :CUSTOM_ID headline
) id
))
4180 ;; Otherwise, look for external files.
4181 (cdr (assoc id
(plist-get info
:id-alist
)))
4182 (user-error "Unable to resolve ID \"%s\"" id
))))
4184 (defun org-export-resolve-radio-link (link info
)
4185 "Return radio-target object referenced as LINK destination.
4187 INFO is a plist used as a communication channel.
4189 Return value can be a radio-target object or nil. Assume LINK
4190 has type \"radio\"."
4191 (let ((path (replace-regexp-in-string
4192 "[ \r\t\n]+" " " (org-element-property :path link
))))
4193 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4195 (and (eq (compare-strings
4196 (replace-regexp-in-string
4197 "[ \r\t\n]+" " " (org-element-property :value radio
))
4198 nil nil path nil nil t
)
4201 info
'first-match
)))
4203 (defun org-export-file-uri (filename)
4204 "Return file URI associated to FILENAME."
4205 (if (not (file-name-absolute-p filename
)) filename
4207 (and (not (org-file-remote-p filename
)) "/")
4208 (if (org-string-match-p "\\`~" filename
)
4209 (expand-file-name filename
)
4215 ;; `org-export-get-reference' associate a unique reference for any
4216 ;; object or element.
4218 ;; `org-export-get-ordinal' associates a sequence number to any object
4221 (defun org-export-get-reference (datum info
)
4222 "Return a unique reference for DATUM, as a string.
4223 DATUM is either an element or an object. INFO is the current
4224 export state, as a plist. Returned reference consists of
4225 alphanumeric characters only."
4226 (let ((type (org-element-type datum
))
4227 (cache (or (plist-get info
:internal-references
)
4228 (let ((h (make-hash-table :test
#'eq
)))
4229 (plist-put info
:internal-references h
)
4231 (or (gethash datum cache
)
4235 (replace-regexp-in-string "-" "" (symbol-name type
))
4237 (incf (gethash type cache
0)))
4240 (defun org-export-get-ordinal (element info
&optional types predicate
)
4241 "Return ordinal number of an element or object.
4243 ELEMENT is the element or object considered. INFO is the plist
4244 used as a communication channel.
4246 Optional argument TYPES, when non-nil, is a list of element or
4247 object types, as symbols, that should also be counted in.
4248 Otherwise, only provided element's type is considered.
4250 Optional argument PREDICATE is a function returning a non-nil
4251 value if the current element or object should be counted in. It
4252 accepts two arguments: the element or object being considered and
4253 the plist used as a communication channel. This allows to count
4254 only a certain type of objects (i.e. inline images).
4256 Return value is a list of numbers if ELEMENT is a headline or an
4257 item. It is nil for keywords. It represents the footnote number
4258 for footnote definitions and footnote references. If ELEMENT is
4259 a target, return the same value as if ELEMENT was the closest
4260 table, item or headline containing the target. In any other
4261 case, return the sequence number of ELEMENT among elements or
4262 objects of the same type."
4263 ;; Ordinal of a target object refer to the ordinal of the closest
4264 ;; table, item, or headline containing the object.
4265 (when (eq (org-element-type element
) 'target
)
4267 (org-element-lineage
4269 '(footnote-definition footnote-reference headline item table
))))
4270 (case (org-element-type element
)
4271 ;; Special case 1: A headline returns its number as a list.
4272 (headline (org-export-get-headline-number element info
))
4273 ;; Special case 2: An item returns its number as a list.
4274 (item (let ((struct (org-element-property :structure element
)))
4275 (org-list-get-item-number
4276 (org-element-property :begin element
)
4278 (org-list-prevs-alist struct
)
4279 (org-list-parents-alist struct
))))
4280 ((footnote-definition footnote-reference
)
4281 (org-export-get-footnote-number element info
))
4284 ;; Increment counter until ELEMENT is found again.
4285 (org-element-map (plist-get info
:parse-tree
)
4286 (or types
(org-element-type element
))
4289 ((eq element el
) (1+ counter
))
4290 ((not predicate
) (incf counter
) nil
)
4291 ((funcall predicate el info
) (incf counter
) nil
)))
4292 info
'first-match
)))))
4297 ;; `org-export-get-loc' counts number of code lines accumulated in
4298 ;; src-block or example-block elements with a "+n" switch until
4299 ;; a given element, excluded. Note: "-n" switches reset that count.
4301 ;; `org-export-unravel-code' extracts source code (along with a code
4302 ;; references alist) from an `element-block' or `src-block' type
4305 ;; `org-export-format-code' applies a formatting function to each line
4306 ;; of code, providing relative line number and code reference when
4307 ;; appropriate. Since it doesn't access the original element from
4308 ;; which the source code is coming, it expects from the code calling
4309 ;; it to know if lines should be numbered and if code references
4312 ;; Eventually, `org-export-format-code-default' is a higher-level
4313 ;; function (it makes use of the two previous functions) which handles
4314 ;; line numbering and code references inclusion, and returns source
4315 ;; code in a format suitable for plain text or verbatim output.
4317 (defun org-export-get-loc (element info
)
4318 "Return accumulated lines of code up to ELEMENT.
4320 INFO is the plist used as a communication channel.
4322 ELEMENT is excluded from count."
4324 (org-element-map (plist-get info
:parse-tree
)
4325 `(src-block example-block
,(org-element-type element
))
4328 ;; ELEMENT is reached: Quit the loop.
4330 ;; Only count lines from src-block and example-block elements
4331 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4332 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4333 ((let ((linums (org-element-property :number-lines el
)))
4335 ;; Accumulate locs or reset them.
4336 (let ((lines (org-count-lines
4337 (org-trim (org-element-property :value el
)))))
4338 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4339 ;; Return nil to stay in the loop.
4345 (defun org-export-unravel-code (element)
4346 "Clean source code and extract references out of it.
4348 ELEMENT has either a `src-block' an `example-block' type.
4350 Return a cons cell whose CAR is the source code, cleaned from any
4351 reference, protective commas and spurious indentation, and CDR is
4352 an alist between relative line number (integer) and name of code
4353 reference on that line (string)."
4354 (let* ((line 0) refs
4355 (value (org-element-property :value element
))
4356 ;; Get code and clean it. Remove blank lines at its
4357 ;; beginning and end.
4358 (code (replace-regexp-in-string
4359 "\\`\\([ \t]*\n\\)+" ""
4360 (replace-regexp-in-string
4361 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4362 (if (or org-src-preserve-indentation
4363 (org-element-property :preserve-indent element
))
4365 (org-element-remove-indentation value
)))))
4366 ;; Get format used for references.
4367 (label-fmt (regexp-quote
4368 (or (org-element-property :label-fmt element
)
4369 org-coderef-label-format
)))
4370 ;; Build a regexp matching a loc with a reference.
4372 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4373 (replace-regexp-in-string
4374 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4377 ;; Code with references removed.
4378 (org-element-normalize-string
4382 (if (not (string-match with-ref-re loc
)) loc
4383 ;; Ref line: remove ref, and signal its position in REFS.
4384 (push (cons line
(match-string 3 loc
)) refs
)
4385 (replace-match "" nil nil loc
1)))
4386 (org-split-string code
"\n") "\n"))
4390 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4391 "Format CODE by applying FUN line-wise and return it.
4393 CODE is a string representing the code to format. FUN is
4394 a function. It must accept three arguments: a line of
4395 code (string), the current line number (integer) or nil and the
4396 reference associated to the current line (string) or nil.
4398 Optional argument NUM-LINES can be an integer representing the
4399 number of code lines accumulated until the current code. Line
4400 numbers passed to FUN will take it into account. If it is nil,
4401 FUN's second argument will always be nil. This number can be
4402 obtained with `org-export-get-loc' function.
4404 Optional argument REF-ALIST can be an alist between relative line
4405 number (i.e. ignoring NUM-LINES) and the name of the code
4406 reference on it. If it is nil, FUN's third argument will always
4407 be nil. It can be obtained through the use of
4408 `org-export-unravel-code' function."
4409 (let ((--locs (org-split-string code
"\n"))
4411 (org-element-normalize-string
4415 (let ((--ref (cdr (assq --line ref-alist
))))
4416 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4419 (defun org-export-format-code-default (element info
)
4420 "Return source code from ELEMENT, formatted in a standard way.
4422 ELEMENT is either a `src-block' or `example-block' element. INFO
4423 is a plist used as a communication channel.
4425 This function takes care of line numbering and code references
4426 inclusion. Line numbers, when applicable, appear at the
4427 beginning of the line, separated from the code by two white
4428 spaces. Code references, on the other hand, appear flushed to
4429 the right, separated by six white spaces from the widest line of
4431 ;; Extract code and references.
4432 (let* ((code-info (org-export-unravel-code element
))
4433 (code (car code-info
))
4434 (code-lines (org-split-string code
"\n")))
4435 (if (null code-lines
) ""
4436 (let* ((refs (and (org-element-property :retain-labels element
)
4438 ;; Handle line numbering.
4439 (num-start (case (org-element-property :number-lines element
)
4440 (continued (org-export-get-loc element info
))
4445 (length (number-to-string
4446 (+ (length code-lines
) num-start
))))))
4447 ;; Prepare references display, if required. Any reference
4448 ;; should start six columns after the widest line of code,
4449 ;; wrapped with parenthesis.
4451 (+ (apply 'max
(mapcar 'length code-lines
))
4452 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4453 (org-export-format-code
4455 (lambda (loc line-num ref
)
4456 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4461 (concat (make-string
4463 (+ (length loc
) (length number-str
))) ?
)
4464 (format "(%s)" ref
))))))
4470 ;; `org-export-table-has-special-column-p' and and
4471 ;; `org-export-table-row-is-special-p' are predicates used to look for
4472 ;; meta-information about the table structure.
4474 ;; `org-table-has-header-p' tells when the rows before the first rule
4475 ;; should be considered as table's header.
4477 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4478 ;; and `org-export-table-cell-borders' extract information from
4479 ;; a table-cell element.
4481 ;; `org-export-table-dimensions' gives the number on rows and columns
4482 ;; in the table, ignoring horizontal rules and special columns.
4483 ;; `org-export-table-cell-address', given a table-cell object, returns
4484 ;; the absolute address of a cell. On the other hand,
4485 ;; `org-export-get-table-cell-at' does the contrary.
4487 ;; `org-export-table-cell-starts-colgroup-p',
4488 ;; `org-export-table-cell-ends-colgroup-p',
4489 ;; `org-export-table-row-starts-rowgroup-p',
4490 ;; `org-export-table-row-ends-rowgroup-p',
4491 ;; `org-export-table-row-starts-header-p',
4492 ;; `org-export-table-row-ends-header-p' and
4493 ;; `org-export-table-row-in-header-p' indicate position of current row
4494 ;; or cell within the table.
4496 (defun org-export-table-has-special-column-p (table)
4497 "Non-nil when TABLE has a special column.
4498 All special columns will be ignored during export."
4499 ;; The table has a special column when every first cell of every row
4500 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4501 ;; "*" "_" and "^". Though, do not consider a first row containing
4502 ;; only empty cells as special.
4503 (let ((special-column-p 'empty
))
4507 (when (eq (org-element-property :type row
) 'standard
)
4508 (let ((value (org-element-contents
4509 (car (org-element-contents row
)))))
4510 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4511 (setq special-column-p
'special
))
4513 (t (throw 'exit nil
))))))
4514 (org-element-contents table
))
4515 (eq special-column-p
'special
))))
4517 (defun org-export-table-has-header-p (table info
)
4518 "Non-nil when TABLE has a header.
4520 INFO is a plist used as a communication channel.
4522 A table has a header when it contains at least two row groups."
4523 (let ((cache (or (plist-get info
:table-header-cache
)
4524 (plist-get (setq info
4525 (plist-put info
:table-header-cache
4526 (make-hash-table :test
'eq
)))
4527 :table-header-cache
))))
4528 (or (gethash table cache
)
4529 (let ((rowgroup 1) row-flag
)
4532 (org-element-map table
'table-row
4536 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4537 (incf rowgroup
) (setq row-flag nil
))
4538 ((and (not row-flag
) (eq (org-element-property :type row
)
4540 (setq row-flag t
) nil
)))
4544 (defun org-export-table-row-is-special-p (table-row info
)
4545 "Non-nil if TABLE-ROW is considered special.
4547 INFO is a plist used as the communication channel.
4549 All special rows will be ignored during export."
4550 (when (eq (org-element-property :type table-row
) 'standard
)
4551 (let ((first-cell (org-element-contents
4552 (car (org-element-contents table-row
)))))
4553 ;; A row is special either when...
4555 ;; ... it starts with a field only containing "/",
4556 (equal first-cell
'("/"))
4557 ;; ... the table contains a special column and the row start
4558 ;; with a marking character among, "^", "_", "$" or "!",
4559 (and (org-export-table-has-special-column-p
4560 (org-export-get-parent table-row
))
4561 (member first-cell
'(("^") ("_") ("$") ("!"))))
4562 ;; ... it contains only alignment cookies and empty cells.
4563 (let ((special-row-p 'empty
))
4567 (let ((value (org-element-contents cell
)))
4568 ;; Since VALUE is a secondary string, the following
4569 ;; checks avoid expanding it with `org-export-data'.
4571 ((and (not (cdr value
))
4572 (stringp (car value
))
4573 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4575 (setq special-row-p
'cookie
))
4576 (t (throw 'exit nil
)))))
4577 (org-element-contents table-row
))
4578 (eq special-row-p
'cookie
)))))))
4580 (defun org-export-table-row-group (table-row info
)
4581 "Return TABLE-ROW's group number, as an integer.
4583 INFO is a plist used as the communication channel.
4585 Return value is the group number, as an integer, or nil for
4586 special rows and rows separators. First group is also table's
4588 (let ((cache (or (plist-get info
:table-row-group-cache
)
4589 (plist-get (setq info
4590 (plist-put info
:table-row-group-cache
4591 (make-hash-table :test
'eq
)))
4592 :table-row-group-cache
))))
4593 (cond ((gethash table-row cache
))
4594 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4595 (t (let ((group 0) row-flag
)
4596 (org-element-map (org-export-get-parent table-row
) 'table-row
4598 (if (eq (org-element-property :type row
) 'rule
)
4600 (unless row-flag
(incf group
) (setq row-flag t
)))
4601 (when (eq table-row row
) (puthash table-row group cache
)))
4602 info
'first-match
))))))
4604 (defun org-export-table-cell-width (table-cell info
)
4605 "Return TABLE-CELL contents width.
4607 INFO is a plist used as the communication channel.
4609 Return value is the width given by the last width cookie in the
4610 same column as TABLE-CELL, or nil."
4611 (let* ((row (org-export-get-parent table-cell
))
4612 (table (org-export-get-parent row
))
4613 (cells (org-element-contents row
))
4614 (columns (length cells
))
4615 (column (- columns
(length (memq table-cell cells
))))
4616 (cache (or (plist-get info
:table-cell-width-cache
)
4617 (plist-get (setq info
4618 (plist-put info
:table-cell-width-cache
4619 (make-hash-table :test
'eq
)))
4620 :table-cell-width-cache
)))
4621 (width-vector (or (gethash table cache
)
4622 (puthash table
(make-vector columns
'empty
) cache
)))
4623 (value (aref width-vector column
)))
4624 (if (not (eq value
'empty
)) value
4626 (dolist (row (org-element-contents table
)
4627 (aset width-vector column cookie-width
))
4628 (when (org-export-table-row-is-special-p row info
)
4629 ;; In a special row, try to find a width cookie at COLUMN.
4630 (let* ((value (org-element-contents
4631 (elt (org-element-contents row
) column
)))
4632 (cookie (car value
)))
4633 ;; The following checks avoid expanding unnecessarily
4634 ;; the cell with `org-export-data'.
4638 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4639 (match-string 1 cookie
))
4641 (string-to-number (match-string 1 cookie
)))))))))))
4643 (defun org-export-table-cell-alignment (table-cell info
)
4644 "Return TABLE-CELL contents alignment.
4646 INFO is a plist used as the communication channel.
4648 Return alignment as specified by the last alignment cookie in the
4649 same column as TABLE-CELL. If no such cookie is found, a default
4650 alignment value will be deduced from fraction of numbers in the
4651 column (see `org-table-number-fraction' for more information).
4652 Possible values are `left', `right' and `center'."
4653 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4654 (require 'org-table
)
4655 (let* ((row (org-export-get-parent table-cell
))
4656 (table (org-export-get-parent row
))
4657 (cells (org-element-contents row
))
4658 (columns (length cells
))
4659 (column (- columns
(length (memq table-cell cells
))))
4660 (cache (or (plist-get info
:table-cell-alignment-cache
)
4661 (plist-get (setq info
4662 (plist-put info
:table-cell-alignment-cache
4663 (make-hash-table :test
'eq
)))
4664 :table-cell-alignment-cache
)))
4665 (align-vector (or (gethash table cache
)
4666 (puthash table
(make-vector columns nil
) cache
))))
4667 (or (aref align-vector column
)
4668 (let ((number-cells 0)
4671 previous-cell-number-p
)
4672 (dolist (row (org-element-contents (org-export-get-parent row
)))
4674 ;; In a special row, try to find an alignment cookie at
4676 ((org-export-table-row-is-special-p row info
)
4677 (let ((value (org-element-contents
4678 (elt (org-element-contents row
) column
))))
4679 ;; Since VALUE is a secondary string, the following
4680 ;; checks avoid useless expansion through
4681 ;; `org-export-data'.
4684 (stringp (car value
))
4685 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4687 (match-string 1 (car value
)))
4688 (setq cookie-align
(match-string 1 (car value
))))))
4689 ;; Ignore table rules.
4690 ((eq (org-element-property :type row
) 'rule
))
4691 ;; In a standard row, check if cell's contents are
4692 ;; expressing some kind of number. Increase NUMBER-CELLS
4693 ;; accordingly. Though, don't bother if an alignment
4694 ;; cookie has already defined cell's alignment.
4696 (let ((value (org-export-data
4697 (org-element-contents
4698 (elt (org-element-contents row
) column
))
4701 ;; Treat an empty cell as a number if it follows
4703 (if (not (or (string-match org-table-number-regexp value
)
4704 (and (string= value
"") previous-cell-number-p
)))
4705 (setq previous-cell-number-p nil
)
4706 (setq previous-cell-number-p t
)
4707 (incf number-cells
))))))
4708 ;; Return value. Alignment specified by cookies has
4709 ;; precedence over alignment deduced from cell's contents.
4712 (cond ((equal cookie-align
"l") 'left
)
4713 ((equal cookie-align
"r") 'right
)
4714 ((equal cookie-align
"c") 'center
)
4715 ((>= (/ (float number-cells
) total-cells
)
4716 org-table-number-fraction
)
4720 (defun org-export-table-cell-borders (table-cell info
)
4721 "Return TABLE-CELL borders.
4723 INFO is a plist used as a communication channel.
4725 Return value is a list of symbols, or nil. Possible values are:
4726 `top', `bottom', `above', `below', `left' and `right'. Note:
4727 `top' (resp. `bottom') only happen for a cell in the first
4728 row (resp. last row) of the table, ignoring table rules, if any.
4730 Returned borders ignore special rows."
4731 (let* ((row (org-export-get-parent table-cell
))
4732 (table (org-export-get-parent-table table-cell
))
4734 ;; Top/above border? TABLE-CELL has a border above when a rule
4735 ;; used to demarcate row groups can be found above. Hence,
4736 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4737 ;; another regular row has to be found above that rule.
4741 (cond ((eq (org-element-property :type row
) 'rule
)
4743 ((not (org-export-table-row-is-special-p row info
))
4744 (if rule-flag
(throw 'exit
(push 'above borders
))
4745 (throw 'exit nil
)))))
4746 ;; Look at every row before the current one.
4747 (cdr (memq row
(reverse (org-element-contents table
)))))
4748 ;; No rule above, or rule found starts the table (ignoring any
4749 ;; special row): TABLE-CELL is at the top of the table.
4750 (when rule-flag
(push 'above borders
))
4751 (push 'top borders
)))
4752 ;; Bottom/below border? TABLE-CELL has a border below when next
4753 ;; non-regular row below is a rule.
4757 (cond ((eq (org-element-property :type row
) 'rule
)
4759 ((not (org-export-table-row-is-special-p row info
))
4760 (if rule-flag
(throw 'exit
(push 'below borders
))
4761 (throw 'exit nil
)))))
4762 ;; Look at every row after the current one.
4763 (cdr (memq row
(org-element-contents table
))))
4764 ;; No rule below, or rule found ends the table (modulo some
4765 ;; special row): TABLE-CELL is at the bottom of the table.
4766 (when rule-flag
(push 'below borders
))
4767 (push 'bottom borders
)))
4768 ;; Right/left borders? They can only be specified by column
4769 ;; groups. Column groups are defined in a row starting with "/".
4770 ;; Also a column groups row only contains "<", "<>", ">" or blank
4773 (let ((column (let ((cells (org-element-contents row
)))
4774 (- (length cells
) (length (memq table-cell cells
))))))
4777 (unless (eq (org-element-property :type row
) 'rule
)
4778 (when (equal (org-element-contents
4779 (car (org-element-contents row
)))
4781 (let ((column-groups
4784 (let ((value (org-element-contents cell
)))
4785 (when (member value
'(("<") ("<>") (">") nil
))
4787 (org-element-contents row
))))
4788 ;; There's a left border when previous cell, if
4789 ;; any, ends a group, or current one starts one.
4790 (when (or (and (not (zerop column
))
4791 (member (elt column-groups
(1- column
))
4793 (member (elt column-groups column
) '("<" "<>")))
4794 (push 'left borders
))
4795 ;; There's a right border when next cell, if any,
4796 ;; starts a group, or current one ends one.
4797 (when (or (and (/= (1+ column
) (length column-groups
))
4798 (member (elt column-groups
(1+ column
))
4800 (member (elt column-groups column
) '(">" "<>")))
4801 (push 'right borders
))
4802 (throw 'exit nil
)))))
4803 ;; Table rows are read in reverse order so last column groups
4804 ;; row has precedence over any previous one.
4805 (reverse (org-element-contents table
)))))
4809 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4810 "Non-nil when TABLE-CELL is at the beginning of a column group.
4811 INFO is a plist used as a communication channel."
4812 ;; A cell starts a column group either when it is at the beginning
4813 ;; of a row (or after the special column, if any) or when it has
4815 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4816 'identity info
'first-match
)
4818 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4820 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4821 "Non-nil when TABLE-CELL is at the end of a column group.
4822 INFO is a plist used as a communication channel."
4823 ;; A cell ends a column group either when it is at the end of a row
4824 ;; or when it has a right border.
4825 (or (eq (car (last (org-element-contents
4826 (org-export-get-parent table-cell
))))
4828 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4830 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4831 "Non-nil when TABLE-ROW is at the beginning of a row group.
4832 INFO is a plist used as a communication channel."
4833 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4834 (org-export-table-row-is-special-p table-row info
))
4835 (let ((borders (org-export-table-cell-borders
4836 (car (org-element-contents table-row
)) info
)))
4837 (or (memq 'top borders
) (memq 'above borders
)))))
4839 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4840 "Non-nil when TABLE-ROW is at the end of a row group.
4841 INFO is a plist used as a communication channel."
4842 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4843 (org-export-table-row-is-special-p table-row info
))
4844 (let ((borders (org-export-table-cell-borders
4845 (car (org-element-contents table-row
)) info
)))
4846 (or (memq 'bottom borders
) (memq 'below borders
)))))
4848 (defun org-export-table-row-in-header-p (table-row info
)
4849 "Non-nil when TABLE-ROW is located within table's header.
4850 INFO is a plist used as a communication channel. Always return
4851 nil for special rows and rows separators."
4852 (and (org-export-table-has-header-p
4853 (org-export-get-parent-table table-row
) info
)
4854 (eql (org-export-table-row-group table-row info
) 1)))
4856 (defun org-export-table-row-starts-header-p (table-row info
)
4857 "Non-nil when TABLE-ROW is the first table header's row.
4858 INFO is a plist used as a communication channel."
4859 (and (org-export-table-row-in-header-p table-row info
)
4860 (org-export-table-row-starts-rowgroup-p table-row info
)))
4862 (defun org-export-table-row-ends-header-p (table-row info
)
4863 "Non-nil when TABLE-ROW is the last table header's row.
4864 INFO is a plist used as a communication channel."
4865 (and (org-export-table-row-in-header-p table-row info
)
4866 (org-export-table-row-ends-rowgroup-p table-row info
)))
4868 (defun org-export-table-row-number (table-row info
)
4869 "Return TABLE-ROW number.
4870 INFO is a plist used as a communication channel. Return value is
4871 zero-based and ignores separators. The function returns nil for
4872 special columns and separators."
4873 (when (and (eq (org-element-property :type table-row
) 'standard
)
4874 (not (org-export-table-row-is-special-p table-row info
)))
4876 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4878 (cond ((eq row table-row
) number
)
4879 ((eq (org-element-property :type row
) 'standard
)
4880 (incf number
) nil
)))
4881 info
'first-match
))))
4883 (defun org-export-table-dimensions (table info
)
4884 "Return TABLE dimensions.
4886 INFO is a plist used as a communication channel.
4888 Return value is a CONS like (ROWS . COLUMNS) where
4889 ROWS (resp. COLUMNS) is the number of exportable
4890 rows (resp. columns)."
4891 (let (first-row (columns 0) (rows 0))
4892 ;; Set number of rows, and extract first one.
4893 (org-element-map table
'table-row
4895 (when (eq (org-element-property :type row
) 'standard
)
4897 (unless first-row
(setq first-row row
)))) info
)
4898 ;; Set number of columns.
4899 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4901 (cons rows columns
)))
4903 (defun org-export-table-cell-address (table-cell info
)
4904 "Return address of a regular TABLE-CELL object.
4906 TABLE-CELL is the cell considered. INFO is a plist used as
4907 a communication channel.
4909 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4910 zero-based index. Only exportable cells are considered. The
4911 function returns nil for other cells."
4912 (let* ((table-row (org-export-get-parent table-cell
))
4913 (row-number (org-export-table-row-number table-row info
)))
4916 (let ((col-count 0))
4917 (org-element-map table-row
'table-cell
4919 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4920 info
'first-match
))))))
4922 (defun org-export-get-table-cell-at (address table info
)
4923 "Return regular table-cell object at ADDRESS in TABLE.
4925 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4926 zero-based index. TABLE is a table type element. INFO is
4927 a plist used as a communication channel.
4929 If no table-cell, among exportable cells, is found at ADDRESS,
4931 (let ((column-pos (cdr address
)) (column-count 0))
4933 ;; Row at (car address) or nil.
4934 (let ((row-pos (car address
)) (row-count 0))
4935 (org-element-map table
'table-row
4937 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4938 ((= row-count row-pos
) row
)
4939 (t (incf row-count
) nil
)))
4943 (if (= column-count column-pos
) cell
4944 (incf column-count
) nil
))
4945 info
'first-match
)))
4948 ;;;; For Tables Of Contents
4950 ;; `org-export-collect-headlines' builds a list of all exportable
4951 ;; headline elements, maybe limited to a certain depth. One can then
4952 ;; easily parse it and transcode it.
4954 ;; Building lists of tables, figures or listings is quite similar.
4955 ;; Once the generic function `org-export-collect-elements' is defined,
4956 ;; `org-export-collect-tables', `org-export-collect-figures' and
4957 ;; `org-export-collect-listings' can be derived from it.
4959 (defun org-export-collect-headlines (info &optional n scope
)
4960 "Collect headlines in order to build a table of contents.
4962 INFO is a plist used as a communication channel.
4964 When optional argument N is an integer, it specifies the depth of
4965 the table of contents. Otherwise, it is set to the value of the
4966 last headline level. See `org-export-headline-levels' for more
4969 Optional argument SCOPE, when non-nil, is an element. If it is
4970 a headline, only children of SCOPE are collected. Otherwise,
4971 collect children of the headline containing provided element. If
4972 there is no such headline, collect all headlines. In any case,
4973 argument N becomes relative to the level of that headline.
4975 Return a list of all exportable headlines as parsed elements.
4976 Footnote sections are ignored."
4977 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
4978 ((eq (org-element-type scope
) 'headline
) scope
)
4979 ((org-export-get-parent-headline scope
))
4980 (t (plist-get info
:parse-tree
))))
4981 (limit (plist-get info
:headline-levels
))
4982 (n (if (not (wholenump n
)) limit
4983 (min (if (eq (org-element-type scope
) 'org-data
) n
4984 (+ (org-export-get-relative-level scope info
) n
))
4986 (org-element-map (org-element-contents scope
) 'headline
4988 (unless (org-element-property :footnote-section-p headline
)
4989 (let ((level (org-export-get-relative-level headline info
)))
4990 (and (<= level n
) headline
))))
4993 (defun org-export-collect-elements (type info
&optional predicate
)
4994 "Collect referenceable elements of a determined type.
4996 TYPE can be a symbol or a list of symbols specifying element
4997 types to search. Only elements with a caption are collected.
4999 INFO is a plist used as a communication channel.
5001 When non-nil, optional argument PREDICATE is a function accepting
5002 one argument, an element of type TYPE. It returns a non-nil
5003 value when that element should be collected.
5005 Return a list of all elements found, in order of appearance."
5006 (org-element-map (plist-get info
:parse-tree
) type
5008 (and (org-element-property :caption element
)
5009 (or (not predicate
) (funcall predicate element
))
5013 (defun org-export-collect-tables (info)
5014 "Build a list of tables.
5015 INFO is a plist used as a communication channel.
5017 Return a list of table elements with a caption."
5018 (org-export-collect-elements 'table info
))
5020 (defun org-export-collect-figures (info predicate
)
5021 "Build a list of figures.
5023 INFO is a plist used as a communication channel. PREDICATE is
5024 a function which accepts one argument: a paragraph element and
5025 whose return value is non-nil when that element should be
5028 A figure is a paragraph type element, with a caption, verifying
5029 PREDICATE. The latter has to be provided since a \"figure\" is
5030 a vague concept that may depend on back-end.
5032 Return a list of elements recognized as figures."
5033 (org-export-collect-elements 'paragraph info predicate
))
5035 (defun org-export-collect-listings (info)
5036 "Build a list of src blocks.
5038 INFO is a plist used as a communication channel.
5040 Return a list of src-block elements with a caption."
5041 (org-export-collect-elements 'src-block info
))
5046 ;; The main function for the smart quotes sub-system is
5047 ;; `org-export-activate-smart-quotes', which replaces every quote in
5048 ;; a given string from the parse tree with its "smart" counterpart.
5050 ;; Dictionary for smart quotes is stored in
5051 ;; `org-export-smart-quotes-alist'.
5053 ;; Internally, regexps matching potential smart quotes (checks at
5054 ;; string boundaries are also necessary) are defined in
5055 ;; `org-export-smart-quotes-regexps'.
5057 (defconst org-export-smart-quotes-alist
5059 ;; one may use: »...«, "...", ›...‹, or '...'.
5060 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5061 ;; LaTeX quotes require Babel!
5062 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
5063 :texinfo
"@guillemetright{}")
5064 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
5065 :texinfo
"@guillemetleft{}")
5066 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
5067 :texinfo
"@guilsinglright{}")
5068 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
5069 :texinfo
"@guilsingleft{}")
5070 (apostrophe :utf-8
"’" :html
"’"))
5072 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
5073 :texinfo
"@quotedblbase{}")
5074 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
5075 :texinfo
"@quotedblleft{}")
5076 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
5077 :texinfo
"@quotesinglbase{}")
5078 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
5079 :texinfo
"@quoteleft{}")
5080 (apostrophe :utf-8
"’" :html
"’"))
5082 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5083 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5084 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5085 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5086 (apostrophe :utf-8
"’" :html
"’"))
5088 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5089 :texinfo
"@guillemetleft{}")
5090 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5091 :texinfo
"@guillemetright{}")
5092 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5093 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5094 (apostrophe :utf-8
"’" :html
"’"))
5096 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
5097 :texinfo
"@guillemetleft{}@tie{}")
5098 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5099 :texinfo
"@tie{}@guillemetright{}")
5100 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
5101 :texinfo
"@guillemetleft{}@tie{}")
5102 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5103 :texinfo
"@tie{}@guillemetright{}")
5104 (apostrophe :utf-8
"’" :html
"’"))
5106 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5107 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5108 :texinfo
"@guillemetleft{}")
5109 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5110 :texinfo
"@guillemetright{}")
5111 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5112 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5113 (apostrophe :utf-8
"’" :html
"’"))
5115 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5116 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5117 :texinfo
"@guillemetleft{}")
5118 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5119 :texinfo
"@guillemetright{}")
5120 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5121 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5122 (apostrophe :utf-8
"’" :html
"’"))
5124 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5125 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5126 :texinfo
"@guillemetleft{}")
5127 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5128 :texinfo
"@guillemetright{}")
5129 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5130 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5131 (apostrophe :utf-8
"’" :html
"’"))
5133 ;; 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
5134 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5135 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
5136 :texinfo
"@guillemetleft{}")
5137 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
5138 :texinfo
"@guillemetright{}")
5139 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
5140 :texinfo
"@quotedblbase{}")
5141 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
5142 :texinfo
"@quotedblleft{}")
5143 (apostrophe :utf-8
"’" :html
: "'"))
5145 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5146 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5147 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5148 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5149 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5150 (apostrophe :utf-8
"’" :html
"’"))
5152 "Smart quotes translations.
5154 Alist whose CAR is a language string and CDR is an alist with
5155 quote type as key and a plist associating various encodings to
5156 their translation as value.
5158 A quote type can be any symbol among `opening-double-quote',
5159 `closing-double-quote', `opening-single-quote',
5160 `closing-single-quote' and `apostrophe'.
5162 Valid encodings include `:utf-8', `:html', `:latex' and
5165 If no translation is found, the quote character is left as-is.")
5167 (defun org-export--smart-quote-status (s info
)
5168 "Return smart quote status at the beginning of string S.
5169 INFO is the current export state, as a plist."
5170 (let* ((parent (org-element-property :parent s
))
5171 (cache (or (plist-get info
:smart-quote-cache
)
5172 (let ((table (make-hash-table :test
#'eq
)))
5173 (plist-put info
:smart-quote-cache table
)
5175 (value (gethash parent cache
'missing-data
)))
5176 (if (not (eq value
'missing-data
)) (cdr (assq s value
))
5177 (let (level1-open level2-open full-status
)
5178 (org-element-map parent
'plain-text
5180 (let ((start 0) current-status
)
5181 (while (setq start
(string-match "['\"]" text start
))
5185 ((equal (match-string 0 text
) "\"")
5186 (setf level1-open
(not level1-open
))
5187 (setf level2-open nil
)
5188 (if level1-open
'opening-double-quote
'closing-double-quote
))
5189 ;; Not already in a level 1 quote: this is an
5191 ((not level1-open
) 'apostrophe
)
5193 ((org-string-match-p "\\S-'\\S-" text
) 'apostrophe
)
5194 ;; Apostrophe at the beginning of a string. Check
5195 ;; white space at the end of the last object.
5196 ((and (org-string-match-p "\\`'\\S-" text
)
5197 (let ((p (org-export-get-previous-element text info
)))
5200 (not (org-string-match-p "[ \t]\\'" p
))
5201 (memq (org-element-property :post-blank p
)
5204 ;; Apostrophe at the end of a string. Check white
5205 ;; space at the beginning of the next object, which
5206 ;; can only happen if that object is a string.
5207 ((and (org-string-match-p "\\S-'\\'" text
)
5208 (let ((n (org-export-get-next-element text info
)))
5210 (not (and (stringp n
)
5211 (org-string-match-p "\\`[ \t]" n
))))))
5213 ;; Lonesome apostrophe. Check white space around
5215 ((and (equal text
"'")
5216 (let ((p (org-export-get-previous-element text info
)))
5219 (not (org-string-match-p "[ \t]\\'" p
))
5220 (memq (org-element-property :post-blank p
)
5222 (let ((n (org-export-get-next-element text info
)))
5224 (not (and (stringp n
)
5225 (org-string-match-p "\\`[ \t]"
5228 ;; Else, consider it as a level 2 quote.
5229 (t (setf level2-open
(not level2-open
))
5230 (if level2-open
'opening-single-quote
5231 'closing-single-quote
)))
5233 (when current-status
5234 (push (cons text
(nreverse current-status
)) full-status
))))
5235 info nil org-element-recursive-objects
)
5236 (puthash parent full-status cache
)
5237 (cdr (assq s full-status
))))))
5239 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5240 "Replace regular quotes with \"smart\" quotes in string S.
5242 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5243 `:utf-8'. INFO is a plist used as a communication channel.
5245 The function has to retrieve information about string
5246 surroundings in parse tree. It can only happen with an
5247 unmodified string. Thus, if S has already been through another
5248 process, a non-nil ORIGINAL optional argument will provide that
5251 Return the new string."
5253 (copy-sequence (org-export--smart-quote-status (or original s
) info
))))
5254 (replace-regexp-in-string
5258 (cdr (assq (pop quote-status
)
5259 (cdr (assoc (plist-get info
:language
)
5260 org-export-smart-quotes-alist
))))
5267 ;; Here are various functions to retrieve information about the
5268 ;; neighborhood of a given element or object. Neighbors of interest
5269 ;; are direct parent (`org-export-get-parent'), parent headline
5270 ;; (`org-export-get-parent-headline'), first element containing an
5271 ;; object, (`org-export-get-parent-element'), parent table
5272 ;; (`org-export-get-parent-table'), previous element or object
5273 ;; (`org-export-get-previous-element') and next element or object
5274 ;; (`org-export-get-next-element').
5276 ;; defsubst org-export-get-parent must be defined before first use
5278 (define-obsolete-function-alias
5279 'org-export-get-genealogy
'org-element-lineage
"25.1")
5281 (defun org-export-get-parent-headline (blob)
5282 "Return BLOB parent headline or nil.
5283 BLOB is the element or object being considered."
5284 (org-element-lineage blob
'(headline)))
5286 (defun org-export-get-parent-element (object)
5287 "Return first element containing OBJECT or nil.
5288 OBJECT is the object to consider."
5289 (org-element-lineage object org-element-all-elements
))
5291 (defun org-export-get-parent-table (object)
5292 "Return OBJECT parent table or nil.
5293 OBJECT is either a `table-cell' or `table-element' type object."
5294 (org-element-lineage object
'(table)))
5296 (defun org-export-get-previous-element (blob info
&optional n
)
5297 "Return previous element or object.
5299 BLOB is an element or object. INFO is a plist used as
5300 a communication channel. Return previous exportable element or
5301 object, a string, or nil.
5303 When optional argument N is a positive integer, return a list
5304 containing up to N siblings before BLOB, from farthest to
5305 closest. With any other non-nil value, return a list containing
5307 (let* ((secondary (org-element-secondary-p blob
))
5308 (parent (org-export-get-parent blob
))
5310 (if secondary
(org-element-property secondary parent
)
5311 (org-element-contents parent
)))
5314 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5315 (cond ((memq obj
(plist-get info
:ignore-list
)))
5316 ((null n
) (throw 'exit obj
))
5317 ((not (wholenump n
)) (push obj prev
))
5318 ((zerop n
) (throw 'exit prev
))
5319 (t (decf n
) (push obj prev
)))))))
5321 (defun org-export-get-next-element (blob info
&optional n
)
5322 "Return next element or object.
5324 BLOB is an element or object. INFO is a plist used as
5325 a communication channel. Return next exportable element or
5326 object, a string, or nil.
5328 When optional argument N is a positive integer, return a list
5329 containing up to N siblings after BLOB, from closest to farthest.
5330 With any other non-nil value, return a list containing all of
5332 (let* ((secondary (org-element-secondary-p blob
))
5333 (parent (org-export-get-parent blob
))
5336 (if secondary
(org-element-property secondary parent
)
5337 (org-element-contents parent
)))))
5340 (dolist (obj siblings
(nreverse next
))
5341 (cond ((memq obj
(plist-get info
:ignore-list
)))
5342 ((null n
) (throw 'exit obj
))
5343 ((not (wholenump n
)) (push obj next
))
5344 ((zerop n
) (throw 'exit
(nreverse next
)))
5345 (t (decf n
) (push obj next
)))))))
5350 ;; `org-export-translate' translates a string according to the language
5351 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5352 ;; the dictionary used for the translation.
5354 (defconst org-export-dictionary
5356 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5358 ("ca" :default
"Autor")
5359 ("cs" :default
"Autor")
5360 ("da" :default
"Forfatter")
5361 ("de" :default
"Autor")
5362 ("eo" :html
"Aŭtoro")
5363 ("es" :default
"Autor")
5364 ("et" :default
"Autor")
5365 ("fi" :html
"Tekijä")
5366 ("fr" :default
"Auteur")
5367 ("hu" :default
"Szerzõ")
5368 ("is" :html
"Höfundur")
5369 ("it" :default
"Autore")
5370 ("ja" :default
"著者" :html
"著者")
5371 ("nl" :default
"Auteur")
5372 ("no" :default
"Forfatter")
5373 ("nb" :default
"Forfatter")
5374 ("nn" :default
"Forfattar")
5375 ("pl" :default
"Autor")
5376 ("pt_BR" :default
"Autor")
5377 ("ru" :html
"Автор" :utf-8
"Автор")
5378 ("sv" :html
"Författare")
5379 ("uk" :html
"Автор" :utf-8
"Автор")
5380 ("zh-CN" :html
"作者" :utf-8
"作者")
5381 ("zh-TW" :html
"作者" :utf-8
"作者"))
5382 ("Continued from previous page"
5383 ("de" :default
"Fortsetzung von vorheriger Seite")
5384 ("es" :html
"Continúa de la página anterior" :ascii
"Continua de la pagina anterior" :default
"Continúa de la página anterior")
5385 ("fr" :default
"Suite de la page précédente")
5386 ("it" :default
"Continua da pagina precedente")
5387 ("ja" :default
"前ページからの続き")
5388 ("nl" :default
"Vervolg van vorige pagina")
5389 ("pt" :default
"Continuação da página anterior")
5390 ("ru" :html
"(Продолжение)"
5391 :utf-8
"(Продолжение)"))
5392 ("Continued on next page"
5393 ("de" :default
"Fortsetzung nächste Seite")
5394 ("es" :html
"Continúa en la siguiente página" :ascii
"Continua en la siguiente pagina" :default
"Continúa en la siguiente página")
5395 ("fr" :default
"Suite page suivante")
5396 ("it" :default
"Continua alla pagina successiva")
5397 ("ja" :default
"次ページに続く")
5398 ("nl" :default
"Vervolg op volgende pagina")
5399 ("pt" :default
"Continua na página seguinte")
5400 ("ru" :html
"(Продолжение следует)"
5401 :utf-8
"(Продолжение следует)"))
5403 ("ca" :default
"Data")
5404 ("cs" :default
"Datum")
5405 ("da" :default
"Dato")
5406 ("de" :default
"Datum")
5407 ("eo" :default
"Dato")
5408 ("es" :default
"Fecha")
5409 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5410 ("fi" :html
"Päivämäärä")
5411 ("hu" :html
"Dátum")
5412 ("is" :default
"Dagsetning")
5413 ("it" :default
"Data")
5414 ("ja" :default
"日付" :html
"日付")
5415 ("nl" :default
"Datum")
5416 ("no" :default
"Dato")
5417 ("nb" :default
"Dato")
5418 ("nn" :default
"Dato")
5419 ("pl" :default
"Data")
5420 ("pt_BR" :default
"Data")
5421 ("ru" :html
"Дата" :utf-8
"Дата")
5422 ("sv" :default
"Datum")
5423 ("uk" :html
"Дата" :utf-8
"Дата")
5424 ("zh-CN" :html
"日期" :utf-8
"日期")
5425 ("zh-TW" :html
"日期" :utf-8
"日期"))
5427 ("da" :default
"Ligning")
5428 ("de" :default
"Gleichung")
5429 ("es" :ascii
"Ecuacion" :html
"Ecuación" :default
"Ecuación")
5430 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5431 ("fr" :ascii
"Equation" :default
"Équation")
5432 ("ja" :default
"方程式")
5433 ("no" :default
"Ligning")
5434 ("nb" :default
"Ligning")
5435 ("nn" :default
"Likning")
5436 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5437 ("ru" :html
"Уравнение"
5439 ("sv" :default
"Ekvation")
5440 ("zh-CN" :html
"方程" :utf-8
"方程"))
5442 ("da" :default
"Figur")
5443 ("de" :default
"Abbildung")
5444 ("es" :default
"Figura")
5445 ("et" :default
"Joonis")
5446 ("ja" :default
"図" :html
"図")
5447 ("no" :default
"Illustrasjon")
5448 ("nb" :default
"Illustrasjon")
5449 ("nn" :default
"Illustrasjon")
5450 ("pt_BR" :default
"Figura")
5451 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5452 ("sv" :default
"Illustration")
5453 ("zh-CN" :html
"图" :utf-8
"图"))
5455 ("da" :default
"Figur %d")
5456 ("de" :default
"Abbildung %d:")
5457 ("es" :default
"Figura %d:")
5458 ("et" :default
"Joonis %d:")
5459 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5460 ("ja" :default
"図%d: " :html
"図%d: ")
5461 ("no" :default
"Illustrasjon %d")
5462 ("nb" :default
"Illustrasjon %d")
5463 ("nn" :default
"Illustrasjon %d")
5464 ("pt_BR" :default
"Figura %d:")
5465 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5466 ("sv" :default
"Illustration %d")
5467 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5469 ("ca" :html
"Peus de pàgina")
5470 ("cs" :default
"Pozn\xe1mky pod carou")
5471 ("da" :default
"Fodnoter")
5472 ("de" :html
"Fußnoten" :default
"Fußnoten")
5473 ("eo" :default
"Piednotoj")
5474 ("es" :ascii
"Nota al pie de pagina" :html
"Nota al pie de página" :default
"Nota al pie de página")
5475 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5476 ("fi" :default
"Alaviitteet")
5477 ("fr" :default
"Notes de bas de page")
5478 ("hu" :html
"Lábjegyzet")
5479 ("is" :html
"Aftanmálsgreinar")
5480 ("it" :html
"Note a piè di pagina")
5481 ("ja" :default
"脚注" :html
"脚注")
5482 ("nl" :default
"Voetnoten")
5483 ("no" :default
"Fotnoter")
5484 ("nb" :default
"Fotnoter")
5485 ("nn" :default
"Fotnotar")
5486 ("pl" :default
"Przypis")
5487 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5488 ("ru" :html
"Сноски" :utf-8
"Сноски")
5489 ("sv" :default
"Fotnoter")
5490 ("uk" :html
"Примітки"
5492 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5493 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5495 ("da" :default
"Programmer")
5496 ("de" :default
"Programmauflistungsverzeichnis")
5497 ("es" :ascii
"Indice de Listados de programas" :html
"Índice de Listados de programas" :default
"Índice de Listados de programas")
5498 ("et" :default
"Loendite nimekiri")
5499 ("fr" :default
"Liste des programmes")
5500 ("ja" :default
"ソースコード目次")
5501 ("no" :default
"Dataprogrammer")
5502 ("nb" :default
"Dataprogrammer")
5503 ("ru" :html
"Список распечаток"
5504 :utf-8
"Список распечаток")
5505 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5507 ("da" :default
"Tabeller")
5508 ("de" :default
"Tabellenverzeichnis")
5509 ("es" :ascii
"Indice de tablas" :html
"Índice de tablas" :default
"Índice de tablas")
5510 ("et" :default
"Tabelite nimekiri")
5511 ("fr" :default
"Liste des tableaux")
5512 ("ja" :default
"表目次")
5513 ("no" :default
"Tabeller")
5514 ("nb" :default
"Tabeller")
5515 ("nn" :default
"Tabeller")
5516 ("pt_BR" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5517 ("ru" :html
"Список таблиц"
5518 :utf-8
"Список таблиц")
5519 ("sv" :default
"Tabeller")
5520 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5522 ("da" :default
"Program")
5523 ("de" :default
"Programmlisting")
5524 ("es" :default
"Listado de programa")
5525 ("et" :default
"Loend")
5526 ("fr" :default
"Programme" :html
"Programme")
5527 ("ja" :default
"ソースコード")
5528 ("no" :default
"Dataprogram")
5529 ("nb" :default
"Dataprogram")
5530 ("pt_BR" :default
"Listagem")
5531 ("ru" :html
"Распечатка"
5532 :utf-8
"Распечатка")
5533 ("zh-CN" :html
"代码" :utf-8
"代码"))
5535 ("da" :default
"Program %d")
5536 ("de" :default
"Programmlisting %d")
5537 ("es" :default
"Listado de programa %d")
5538 ("et" :default
"Loend %d")
5539 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5540 ("ja" :default
"ソースコード%d:")
5541 ("no" :default
"Dataprogram %d")
5542 ("nb" :default
"Dataprogram %d")
5543 ("pt_BR" :default
"Listagem %d")
5544 ("ru" :html
"Распечатка %d.:"
5545 :utf-8
"Распечатка %d.:")
5546 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5548 ("fr" :ascii
"References" :default
"Références")
5549 ("de" :default
"Quellen")
5550 ("es" :default
"Referencias"))
5552 ("da" :default
"jævnfør afsnit %s")
5553 ("de" :default
"siehe Abschnitt %s")
5554 ("es" :ascii
"Vea seccion %s" :html
"Vea sección %s" :default
"Vea sección %s")
5555 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5556 ("fr" :default
"cf. section %s")
5557 ("ja" :default
"セクション %s を参照")
5558 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
5559 :ascii
"Veja a secao %s")
5560 ("ru" :html
"См. раздел %s"
5561 :utf-8
"См. раздел %s")
5562 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5564 ("de" :default
"Tabelle")
5565 ("es" :default
"Tabla")
5566 ("et" :default
"Tabel")
5567 ("fr" :default
"Tableau")
5568 ("ja" :default
"表" :html
"表")
5569 ("pt_BR" :default
"Tabela")
5570 ("ru" :html
"Таблица"
5572 ("zh-CN" :html
"表" :utf-8
"表"))
5574 ("da" :default
"Tabel %d")
5575 ("de" :default
"Tabelle %d")
5576 ("es" :default
"Tabla %d")
5577 ("et" :default
"Tabel %d")
5578 ("fr" :default
"Tableau %d :")
5579 ("ja" :default
"表%d:" :html
"表%d:")
5580 ("no" :default
"Tabell %d")
5581 ("nb" :default
"Tabell %d")
5582 ("nn" :default
"Tabell %d")
5583 ("pt_BR" :default
"Tabela %d")
5584 ("ru" :html
"Таблица %d.:"
5585 :utf-8
"Таблица %d.:")
5586 ("sv" :default
"Tabell %d")
5587 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5588 ("Table of Contents"
5589 ("ca" :html
"Índex")
5590 ("cs" :default
"Obsah")
5591 ("da" :default
"Indhold")
5592 ("de" :default
"Inhaltsverzeichnis")
5593 ("eo" :default
"Enhavo")
5594 ("es" :ascii
"Indice" :html
"Índice" :default
"Índice")
5595 ("et" :default
"Sisukord")
5596 ("fi" :html
"Sisällysluettelo")
5597 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5598 ("hu" :html
"Tartalomjegyzék")
5599 ("is" :default
"Efnisyfirlit")
5600 ("it" :default
"Indice")
5601 ("ja" :default
"目次" :html
"目次")
5602 ("nl" :default
"Inhoudsopgave")
5603 ("no" :default
"Innhold")
5604 ("nb" :default
"Innhold")
5605 ("nn" :default
"Innhald")
5606 ("pl" :html
"Spis treści")
5607 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
5608 ("ru" :html
"Содержание"
5609 :utf-8
"Содержание")
5610 ("sv" :html
"Innehåll")
5611 ("uk" :html
"Зміст" :utf-8
"Зміст")
5612 ("zh-CN" :html
"目录" :utf-8
"目录")
5613 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5614 ("Unknown reference"
5615 ("da" :default
"ukendt reference")
5616 ("de" :default
"Unbekannter Verweis")
5617 ("es" :default
"Referencia desconocida")
5618 ("et" :default
"Tundmatu viide")
5619 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5620 ("ja" :default
"不明な参照先")
5621 ("pt_BR" :default
"Referência desconhecida"
5622 :ascii
"Referencia desconhecida")
5623 ("ru" :html
"Неизвестная ссылка"
5624 :utf-8
"Неизвестная ссылка")
5625 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5626 "Dictionary for export engine.
5628 Alist whose car is the string to translate and cdr is an alist
5629 whose car is the language string and cdr is a plist whose
5630 properties are possible charsets and values translated terms.
5632 It is used as a database for `org-export-translate'. Since this
5633 function returns the string as-is if no translation was found,
5634 the variable only needs to record values different from the
5637 (defun org-export-translate (s encoding info
)
5638 "Translate string S according to language specification.
5640 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5641 and `:utf-8'. INFO is a plist used as a communication channel.
5643 Translation depends on `:language' property. Return the
5644 translated string. If no translation is found, try to fall back
5645 to `:default' encoding. If it fails, return S."
5646 (let* ((lang (plist-get info
:language
))
5647 (translations (cdr (assoc lang
5648 (cdr (assoc s org-export-dictionary
))))))
5649 (or (plist-get translations encoding
)
5650 (plist-get translations
:default
)
5655 ;;; Asynchronous Export
5657 ;; `org-export-async-start' is the entry point for asynchronous
5658 ;; export. It recreates current buffer (including visibility,
5659 ;; narrowing and visited file) in an external Emacs process, and
5660 ;; evaluates a command there. It then applies a function on the
5661 ;; returned results in the current process.
5663 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5664 ;; allow to export to a buffer or a file, asynchronously or not.
5666 ;; `org-export-output-file-name' is an auxiliary function meant to be
5667 ;; used with `org-export-to-file'. With a given extension, it tries
5668 ;; to provide a canonical file name to write export output to.
5670 ;; Asynchronously generated results are never displayed directly.
5671 ;; Instead, they are stored in `org-export-stack-contents'. They can
5672 ;; then be retrieved by calling `org-export-stack'.
5674 ;; Export Stack is viewed through a dedicated major mode
5675 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5676 ;;`org-export-stack-delete', `org-export-stack-view' and
5677 ;;`org-export-stack-clear'.
5679 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5680 ;; It should be used whenever `org-export-async-start' is called.
5682 (defmacro org-export-async-start
(fun &rest body
)
5683 "Call function FUN on the results returned by BODY evaluation.
5685 FUN is an anonymous function of one argument. BODY evaluation
5686 happens in an asynchronous process, from a buffer which is an
5687 exact copy of the current one.
5689 Use `org-export-add-to-stack' in FUN in order to register results
5692 This is a low level function. See also `org-export-to-buffer'
5693 and `org-export-to-file' for more specialized functions."
5694 (declare (indent 1) (debug t
))
5695 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5696 ;; Write the full sexp evaluating BODY in a copy of the current
5697 ;; buffer to a temporary file, as it may be too long for program
5698 ;; args in `start-process'.
5699 `(with-temp-message "Initializing asynchronous export process"
5700 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5701 (,temp-file
(make-temp-file "org-export-process"))
5702 (,coding buffer-file-coding-system
))
5703 (with-temp-file ,temp-file
5705 ;; Null characters (from variable values) are inserted
5706 ;; within the file. As a consequence, coding system for
5707 ;; buffer contents will not be recognized properly. So,
5708 ;; we make sure it is the same as the one used to display
5709 ;; the original buffer.
5710 (format ";; -*- coding: %s; -*-\n%S"
5713 (when org-export-async-debug
'(setq debug-on-error t
))
5714 ;; Ignore `kill-emacs-hook' and code evaluation
5715 ;; queries from Babel as we need a truly
5716 ;; non-interactive process.
5717 (setq kill-emacs-hook nil
5718 org-babel-confirm-evaluate-answer-no t
)
5719 ;; Initialize export framework.
5721 ;; Re-create current buffer there.
5722 (funcall ,,copy-fun
)
5723 (restore-buffer-modified-p nil
)
5724 ;; Sexp to evaluate in the buffer.
5725 (print (progn ,,@body
))))))
5726 ;; Start external process.
5727 (let* ((process-connection-type nil
)
5728 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5733 (list "org-export-process"
5735 (expand-file-name invocation-name invocation-directory
)
5737 (if org-export-async-init-file
5738 (list "-Q" "-l" org-export-async-init-file
)
5739 (list "-l" user-init-file
))
5740 (list "-l" ,temp-file
)))))
5741 ;; Register running process in stack.
5742 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5743 ;; Set-up sentinel in order to catch results.
5744 (let ((handler ,fun
))
5745 (set-process-sentinel
5748 (let ((proc-buffer (process-buffer p
)))
5749 (when (eq (process-status p
) 'exit
)
5751 (if (zerop (process-exit-status p
))
5754 (with-current-buffer proc-buffer
5755 (goto-char (point-max))
5757 (read (current-buffer)))))
5758 (funcall ,handler results
))
5759 (unless org-export-async-debug
5760 (and (get-buffer proc-buffer
)
5761 (kill-buffer proc-buffer
))))
5762 (org-export-add-to-stack proc-buffer nil p
)
5764 (message "Process '%s' exited abnormally" p
))
5765 (unless org-export-async-debug
5766 (delete-file ,,temp-file
)))))))))))))
5769 (defun org-export-to-buffer
5771 &optional async subtreep visible-only body-only ext-plist
5773 "Call `org-export-as' with output to a specified buffer.
5775 BACKEND is either an export back-end, as returned by, e.g.,
5776 `org-export-create-backend', or a symbol referring to
5777 a registered back-end.
5779 BUFFER is the name of the output buffer. If it already exists,
5780 it will be erased first, otherwise, it will be created.
5782 A non-nil optional argument ASYNC means the process should happen
5783 asynchronously. The resulting buffer should then be accessible
5784 through the `org-export-stack' interface. When ASYNC is nil, the
5785 buffer is displayed if `org-export-show-temporary-export-buffer'
5788 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5789 EXT-PLIST are similar to those used in `org-export-as', which
5792 Optional argument POST-PROCESS is a function which should accept
5793 no argument. It is always called within the current process,
5794 from BUFFER, with point at its beginning. Export back-ends can
5795 use it to set a major mode there, e.g,
5797 \(defun org-latex-export-as-latex
5798 \(&optional async subtreep visible-only body-only ext-plist)
5800 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5801 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5803 This function returns BUFFER."
5804 (declare (indent 2))
5806 (org-export-async-start
5808 (with-current-buffer (get-buffer-create ,buffer
)
5810 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5812 (goto-char (point-min))
5813 (org-export-add-to-stack (current-buffer) ',backend
)
5814 (ignore-errors (funcall ,post-process
))))
5816 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5818 (org-export-as backend subtreep visible-only body-only ext-plist
))
5819 (buffer (get-buffer-create buffer
))
5820 (encoding buffer-file-coding-system
))
5821 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5822 (org-kill-new output
))
5823 (with-current-buffer buffer
5825 (setq buffer-file-coding-system encoding
)
5827 (goto-char (point-min))
5828 (and (functionp post-process
) (funcall post-process
)))
5829 (when org-export-show-temporary-export-buffer
5830 (switch-to-buffer-other-window buffer
))
5834 (defun org-export-to-file
5835 (backend file
&optional async subtreep visible-only body-only ext-plist
5837 "Call `org-export-as' with output to a specified file.
5839 BACKEND is either an export back-end, as returned by, e.g.,
5840 `org-export-create-backend', or a symbol referring to
5841 a registered back-end. FILE is the name of the output file, as
5844 A non-nil optional argument ASYNC means the process should happen
5845 asynchronously. The resulting buffer will then be accessible
5846 through the `org-export-stack' interface.
5848 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5849 EXT-PLIST are similar to those used in `org-export-as', which
5852 Optional argument POST-PROCESS is called with FILE as its
5853 argument and happens asynchronously when ASYNC is non-nil. It
5854 has to return a file name, or nil. Export back-ends can use this
5855 to send the output file through additional processing, e.g,
5857 \(defun org-latex-export-to-latex
5858 \(&optional async subtreep visible-only body-only ext-plist)
5860 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5861 \(org-export-to-file 'latex outfile
5862 async subtreep visible-only body-only ext-plist
5863 \(lambda (file) (org-latex-compile file)))
5865 The function returns either a file name returned by POST-PROCESS,
5867 (declare (indent 2))
5868 (if (not (file-writable-p file
)) (error "Output file not writable")
5869 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
5870 (encoding (or org-export-coding-system buffer-file-coding-system
)))
5872 (org-export-async-start
5874 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5877 ',backend
,subtreep
,visible-only
,body-only
5881 (let ((coding-system-for-write ',encoding
))
5882 (write-file ,file
)))
5883 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5884 (let ((output (org-export-as
5885 backend subtreep visible-only body-only ext-plist
)))
5888 (let ((coding-system-for-write encoding
))
5890 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5891 (org-kill-new output
))
5892 ;; Get proper return value.
5893 (or (and (functionp post-process
) (funcall post-process file
))
5896 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5897 "Return output file's name according to buffer specifications.
5899 EXTENSION is a string representing the output file extension,
5900 with the leading dot.
5902 With a non-nil optional argument SUBTREEP, try to determine
5903 output file's name by looking for \"EXPORT_FILE_NAME\" property
5904 of subtree at point.
5906 When optional argument PUB-DIR is set, use it as the publishing
5909 Return file name as a string."
5910 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5912 ;; File name may come from EXPORT_FILE_NAME subtree
5913 ;; property, assuming point is at beginning of said
5915 (file-name-sans-extension
5919 (ignore-errors (org-back-to-heading) (point)))
5920 "EXPORT_FILE_NAME" 'selective
))
5921 ;; File name may be extracted from buffer's associated
5923 (and visited-file
(file-name-nondirectory visited-file
))
5924 ;; Can't determine file name on our own: Ask user.
5925 (let ((read-file-name-function
5926 (and org-completion-use-ido
'ido-read-file-name
)))
5928 "Output file: " pub-dir nil nil nil
5930 (string= (file-name-extension name t
) extension
)))))))
5932 ;; Build file name. Enforce EXTENSION over whatever user
5933 ;; may have come up with. PUB-DIR, if defined, always has
5934 ;; precedence over any provided path.
5937 (concat (file-name-as-directory pub-dir
)
5938 (file-name-nondirectory base-name
)
5940 ((file-name-absolute-p base-name
) (concat base-name extension
))
5941 (t (concat (file-name-as-directory ".") base-name extension
)))))
5942 ;; If writing to OUTPUT-FILE would overwrite original file, append
5943 ;; EXTENSION another time to final name.
5944 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5945 (concat output-file extension
)
5948 (defun org-export-add-to-stack (source backend
&optional process
)
5949 "Add a new result to export stack if not present already.
5951 SOURCE is a buffer or a file name containing export results.
5952 BACKEND is a symbol representing export back-end used to generate
5955 Entries already pointing to SOURCE and unavailable entries are
5956 removed beforehand. Return the new stack."
5957 (setq org-export-stack-contents
5958 (cons (list source backend
(or process
(current-time)))
5959 (org-export-stack-remove source
))))
5961 (defun org-export-stack ()
5962 "Menu for asynchronous export results and running processes."
5964 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5966 (when (zerop (buffer-size)) (org-export-stack-mode))
5967 (org-export-stack-refresh)
5968 (pop-to-buffer buffer
))
5969 (message "Type \"q\" to quit, \"?\" for help"))
5971 (defun org-export--stack-source-at-point ()
5972 "Return source from export results at point in stack."
5973 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5974 (if (not source
) (error "Source unavailable, please refresh buffer")
5975 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5978 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5980 ;; SOURCE is not consistent with current line. The stack
5981 ;; view is outdated.
5982 (error "Source unavailable; type `g' to update buffer"))))))
5984 (defun org-export-stack-clear ()
5985 "Remove all entries from export stack."
5987 (setq org-export-stack-contents nil
))
5989 (defun org-export-stack-refresh (&rest dummy
)
5990 "Refresh the asynchronous export stack.
5991 DUMMY is ignored. Unavailable sources are removed from the list.
5992 Return the new stack."
5993 (let ((inhibit-read-only t
))
6000 (let ((proc-p (processp (nth 2 entry
))))
6003 (format " %-12s " (or (nth 1 entry
) ""))
6005 (let ((data (nth 2 entry
)))
6006 (if proc-p
(format " %6s " (process-status data
))
6007 ;; Compute age of the results.
6010 (float-time (time-since data
)))))
6013 (let ((source (car entry
)))
6014 (if (stringp source
) source
6015 (buffer-name source
)))))))
6016 ;; Clear stack from exited processes, dead buffers or
6017 ;; non-existent files.
6018 (setq org-export-stack-contents
6021 (if (processp (nth 2 el
))
6022 (buffer-live-p (process-buffer (nth 2 el
)))
6023 (let ((source (car el
)))
6024 (if (bufferp source
) (buffer-live-p source
)
6025 (file-exists-p source
)))))
6026 org-export-stack-contents
)) "\n")))))))
6028 (defun org-export-stack-remove (&optional source
)
6029 "Remove export results at point from stack.
6030 If optional argument SOURCE is non-nil, remove it instead."
6032 (let ((source (or source
(org-export--stack-source-at-point))))
6033 (setq org-export-stack-contents
6034 (org-remove-if (lambda (el) (equal (car el
) source
))
6035 org-export-stack-contents
))))
6037 (defun org-export-stack-view (&optional in-emacs
)
6038 "View export results at point in stack.
6039 With an optional prefix argument IN-EMACS, force viewing files
6042 (let ((source (org-export--stack-source-at-point)))
6043 (cond ((processp source
)
6044 (org-switch-to-buffer-other-window (process-buffer source
)))
6045 ((bufferp source
) (org-switch-to-buffer-other-window source
))
6046 (t (org-open-file source in-emacs
)))))
6048 (defvar org-export-stack-mode-map
6049 (let ((km (make-sparse-keymap)))
6050 (define-key km
" " 'next-line
)
6051 (define-key km
"n" 'next-line
)
6052 (define-key km
"\C-n" 'next-line
)
6053 (define-key km
[down] 'next-line)
6054 (define-key km "p" 'previous-line)
6055 (define-key km "\C-p" 'previous-line)
6056 (define-key km "\C-?" 'previous-line)
6057 (define-key km [up] 'previous-line)
6058 (define-key km "C" 'org-export-stack-clear)
6059 (define-key km "v" 'org-export-stack-view)
6060 (define-key km (kbd "RET") 'org-export-stack-view)
6061 (define-key km "d" 'org-export-stack-remove)
6063 "Keymap for Org Export Stack.")
6065 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
6066 "Mode for displaying asynchronous export stack.
6068 Type \\[org-export-stack] to visualize the asynchronous export
6071 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6072 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6075 Removing entries in an Org Export Stack buffer doesn't affect
6076 files or buffers, only the display.
6078 \\{org-export-stack-mode-map}"
6081 (setq buffer-read-only t
6086 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6087 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6088 (set (make-local-variable 'revert-buffer-function)
6089 'org-export-stack-refresh))
6095 ;; `org-export-dispatch' is the standard interactive way to start an
6096 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6097 ;; for its interface, which, in turn, delegates response to key
6098 ;; pressed to `org-export--dispatch-action'.
6101 (defun org-export-dispatch (&optional arg)
6102 "Export dispatcher for Org mode.
6104 It provides an access to common export related tasks in a buffer.
6105 Its interface comes in two flavors: standard and expert.
6107 While both share the same set of bindings, only the former
6108 displays the valid keys associations in a dedicated buffer.
6109 Scrolling (resp. line-wise motion) in this buffer is done with
6110 SPC and DEL (resp. C-n and C-p) keys.
6112 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6113 flavor or the other.
6115 When ARG is \\[universal-argument], repeat the last export action, with the same set
6116 of options used back then, on the current buffer.
6118 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6121 (cond ((equal arg '(16)) '(stack))
6122 ((and arg org-export-dispatch-last-action))
6123 (t (save-window-excursion
6126 ;; Remember where we are
6127 (move-marker org-export-dispatch-last-position
6129 (org-base-buffer (current-buffer)))
6130 ;; Get and store an export command
6131 (setq org-export-dispatch-last-action
6132 (org-export--dispatch-ui
6133 (list org-export-initial-scope
6134 (and org-export-in-background 'async))
6136 org-export-dispatch-use-expert-ui)))
6137 (and (get-buffer "*Org Export Dispatcher*")
6138 (kill-buffer "*Org Export Dispatcher*")))))))
6139 (action (car input))
6140 (optns (cdr input)))
6141 (unless (memq 'subtree optns)
6142 (move-marker org-export-dispatch-last-position nil))
6144 ;; First handle special hard-coded actions.
6145 (template (org-export-insert-default-template nil optns))
6146 (stack (org-export-stack))
6147 (publish-current-file
6148 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6149 (publish-current-project
6150 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6151 (publish-choose-project
6152 (org-publish (assoc (org-icompleting-read
6154 org-publish-project-alist nil t)
6155 org-publish-project-alist)
6157 (memq 'async optns)))
6158 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6162 ;; Repeating command, maybe move cursor to restore subtree
6164 (if (eq (marker-buffer org-export-dispatch-last-position)
6165 (org-base-buffer (current-buffer)))
6166 (goto-char org-export-dispatch-last-position)
6167 ;; We are in a different buffer, forget position.
6168 (move-marker org-export-dispatch-last-position nil)))
6170 ;; Return a symbol instead of a list to ease
6171 ;; asynchronous export macro use.
6172 (and (memq 'async optns) t)
6173 (and (memq 'subtree optns) t)
6174 (and (memq 'visible optns) t)
6175 (and (memq 'body optns) t)))))))
6177 (defun org-export--dispatch-ui (options first-key expertp)
6178 "Handle interface for `org-export-dispatch'.
6180 OPTIONS is a list containing current interactive options set for
6181 export. It can contain any of the following symbols:
6182 `body' toggles a body-only export
6183 `subtree' restricts export to current subtree
6184 `visible' restricts export to visible part of buffer.
6185 `force' force publishing files.
6186 `async' use asynchronous export process
6188 FIRST-KEY is the key pressed to select the first level menu. It
6189 is nil when this menu hasn't been selected yet.
6191 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6192 buffer is provided, but indications about currently active
6193 options are given in the prompt. Moreover, \[?] allows to switch
6194 back to standard interface."
6196 (lambda (key &optional access-key)
6197 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6198 ;; non-nil is the required first-level key to activate
6199 ;; KEY. When its value is t, activate KEY independently
6200 ;; on the first key, if any. A nil value means KEY will
6201 ;; only be activated at first level.
6202 (if (or (eq access-key t) (eq access-key first-key))
6203 (org-propertize key 'face 'org-warning)
6207 ;; Fontify VALUE string.
6208 (org-propertize value 'face 'font-lock-variable-name-face)))
6209 ;; Prepare menu entries by extracting them from registered
6210 ;; back-ends and sorting them by access key and by ordinal,
6213 (sort (sort (delq nil
6214 (mapcar #'org-export-backend-menu
6215 org-export-registered-backends))
6217 (let ((key-a (nth 1 a))
6219 (cond ((and (numberp key-a) (numberp key-b))
6221 ((numberp key-b) t)))))
6222 'car-less-than-car))
6223 ;; Compute a list of allowed keys based on the first key
6224 ;; pressed, if any. Some keys
6225 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6228 (nconc (list 2 22 19 6 1)
6229 (if (not first-key) (org-uniquify (mapcar 'car entries))
6231 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6232 (when (eq (car entry) first-key)
6233 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6234 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6235 ((not first-key) (list ?P)))
6237 (when expertp (list ??))
6239 ;; Build the help menu for standard UI.
6243 ;; Options are hard-coded.
6244 (format "[%s] Body only: %s [%s] Visible only: %s
6245 \[%s] Export scope: %s [%s] Force publishing: %s
6246 \[%s] Async export: %s\n\n"
6247 (funcall fontify-key "C-b" t)
6248 (funcall fontify-value
6249 (if (memq 'body options) "On " "Off"))
6250 (funcall fontify-key "C-v" t)
6251 (funcall fontify-value
6252 (if (memq 'visible options) "On " "Off"))
6253 (funcall fontify-key "C-s" t)
6254 (funcall fontify-value
6255 (if (memq 'subtree options) "Subtree" "Buffer "))
6256 (funcall fontify-key "C-f" t)
6257 (funcall fontify-value
6258 (if (memq 'force options) "On " "Off"))
6259 (funcall fontify-key "C-a" t)
6260 (funcall fontify-value
6261 (if (memq 'async options) "On " "Off")))
6262 ;; Display registered back-end entries. When a key
6263 ;; appears for the second time, do not create another
6264 ;; entry, but append its sub-menu to existing menu.
6268 (let ((top-key (car entry)))
6270 (unless (eq top-key last-key)
6271 (setq last-key top-key)
6272 (format "\n[%s] %s\n"
6273 (funcall fontify-key (char-to-string top-key))
6275 (let ((sub-menu (nth 2 entry)))
6276 (unless (functionp sub-menu)
6277 ;; Split sub-menu into two columns.
6284 (if (zerop (mod index 2)) " [%s] %-26s"
6286 (funcall fontify-key
6287 (char-to-string (car sub-entry))
6291 (when (zerop (mod index 2)) "\n"))))))))
6293 ;; Publishing menu is hard-coded.
6294 (format "\n[%s] Publish
6295 [%s] Current file [%s] Current project
6296 [%s] Choose project [%s] All projects\n\n\n"
6297 (funcall fontify-key "P")
6298 (funcall fontify-key "f" ?P)
6299 (funcall fontify-key "p" ?P)
6300 (funcall fontify-key "x" ?P)
6301 (funcall fontify-key "a" ?P))
6302 (format "[%s] Export stack [%s] Insert template\n"
6303 (funcall fontify-key "&" t)
6304 (funcall fontify-key "#" t))
6306 (funcall fontify-key "q" t)
6307 (if first-key "Main menu" "Exit")))))
6308 ;; Build prompts for both standard and expert UI.
6309 (standard-prompt (unless expertp "Export command: "))
6313 "Export command (C-%s%s%s%s%s) [%s]: "
6314 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6315 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6316 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6317 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6318 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6319 (mapconcat (lambda (k)
6320 ;; Strip control characters.
6321 (unless (< k 27) (char-to-string k)))
6322 allowed-keys "")))))
6323 ;; With expert UI, just read key with a fancy prompt. In standard
6324 ;; UI, display an intrusive help buffer.
6326 (org-export--dispatch-action
6327 expert-prompt allowed-keys entries options first-key expertp)
6328 ;; At first call, create frame layout in order to display menu.
6329 (unless (get-buffer "*Org Export Dispatcher*")
6330 (delete-other-windows)
6331 (org-switch-to-buffer-other-window
6332 (get-buffer-create "*Org Export Dispatcher*"))
6333 (setq cursor-type nil
6334 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6335 ;; Make sure that invisible cursor will not highlight square
6337 (set-syntax-table (copy-syntax-table))
6338 (modify-syntax-entry ?\[ "w"))
6339 ;; At this point, the buffer containing the menu exists and is
6340 ;; visible in the current window. So, refresh it.
6341 (with-current-buffer "*Org Export Dispatcher*"
6342 ;; Refresh help. Maintain display continuity by re-visiting
6343 ;; previous window position.
6344 (let ((pos (window-start)))
6347 (set-window-start nil pos)))
6348 (org-fit-window-to-buffer)
6349 (org-export--dispatch-action
6350 standard-prompt allowed-keys entries options first-key expertp))))
6352 (defun org-export--dispatch-action
6353 (prompt allowed-keys entries options first-key expertp)
6354 "Read a character from command input and act accordingly.
6356 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6357 a list of characters available at a given step in the process.
6358 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6359 EXPERTP are the same as defined in `org-export--dispatch-ui',
6362 Toggle export options when required. Otherwise, return value is
6363 a list with action as CAR and a list of interactive export
6366 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6368 (while (and (setq key (read-char-exclusive prompt))
6370 (memq key '(14 16 ?\s ?\d)))
6372 (14 (if (not (pos-visible-in-window-p (point-max)))
6373 (ignore-errors (scroll-up 1))
6374 (message "End of buffer")
6376 (16 (if (not (pos-visible-in-window-p (point-min)))
6377 (ignore-errors (scroll-down 1))
6378 (message "Beginning of buffer")
6380 (?\s (if (not (pos-visible-in-window-p (point-max)))
6382 (message "End of buffer")
6384 (?\d (if (not (pos-visible-in-window-p (point-min)))
6386 (message "Beginning of buffer")
6389 ;; Ignore undefined associations.
6390 ((not (memq key allowed-keys))
6392 (unless expertp (message "Invalid key") (sit-for 1))
6393 (org-export--dispatch-ui options first-key expertp))
6394 ;; q key at first level aborts export. At second level, cancel
6395 ;; first key instead.
6396 ((eq key ?q) (if (not first-key) (error "Export aborted")
6397 (org-export--dispatch-ui options nil expertp)))
6398 ;; Help key: Switch back to standard interface if expert UI was
6400 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6401 ;; Send request for template insertion along with export scope.
6402 ((eq key ?#) (cons 'template (memq 'subtree options)))
6403 ;; Switch to asynchronous export stack.
6404 ((eq key ?&) '(stack))
6405 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6406 ((memq key '(2 22 19 6 1))
6407 (org-export--dispatch-ui
6408 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6409 (6 'force) (1 'async))))
6410 (if (memq option options) (remq option options)
6411 (cons option options)))
6413 ;; Action selected: Send key and options back to
6414 ;; `org-export-dispatch'.
6415 ((or first-key (functionp (nth 2 (assq key entries))))
6417 ((not first-key) (nth 2 (assq key entries)))
6418 ;; Publishing actions are hard-coded. Send a special
6419 ;; signal to `org-export-dispatch'.
6422 (?f 'publish-current-file)
6423 (?p 'publish-current-project)
6424 (?x 'publish-choose-project)
6426 ;; Return first action associated to FIRST-KEY + KEY
6427 ;; path. Indeed, derived backends can share the same
6430 (mapc (lambda (entry)
6431 (let ((match (assq key (nth 2 entry))))
6432 (when match (throw 'found (nth 2 match)))))
6433 (member (assq first-key entries) entries)))))
6435 ;; Otherwise, enter sub-menu.
6436 (t (org-export--dispatch-ui options key expertp)))))
6443 ;; generated-autoload-file: "org-loaddefs.el"