1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012-2014 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 in 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. The exhaustive list
35 ;; of properties is given in "The Communication Channel" section of
38 ;; - The transcoder walks the parse tree, ignores or treat as plain
39 ;; text elements and objects according to export options, and
40 ;; eventually calls back-end specific functions to do the real
41 ;; transcoding, concatenating their return value along the way.
43 ;; - The filter system is activated at the very beginning and the very
44 ;; end of the export process, and each time an element or an object
45 ;; has been converted. It is the entry point to fine-tune standard
46 ;; output from back-end transcoders. See "The Filter System"
47 ;; section for more information.
49 ;; The core function is `org-export-as'. It returns the transcoded
50 ;; buffer as a string.
52 ;; An export back-end is defined with `org-export-define-backend'.
53 ;; This function can also support specific buffer keywords, OPTION
54 ;; keyword's items and filters. Refer to function's documentation for
57 ;; If the new back-end shares most properties with another one,
58 ;; `org-export-define-derived-backend' can be used to simplify the
61 ;; Any back-end can define its own variables. Among them, those
62 ;; customizable should belong to the `org-export-BACKEND' group.
64 ;; Tools for common tasks across back-ends are implemented in the
65 ;; following part of the file.
67 ;; Then, a wrapper macro for asynchronous export,
68 ;; `org-export-async-start', along with tools to display results. are
69 ;; given in the penultimate part.
71 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
72 ;; back-ends is provided in the last one.
76 (eval-when-compile (require 'cl
))
77 (require 'org-element
)
81 (declare-function org-publish
"ox-publish" (project &optional force async
))
82 (declare-function org-publish-all
"ox-publish" (&optional force async
))
84 org-publish-current-file
"ox-publish" (&optional force async
))
85 (declare-function org-publish-current-project
"ox-publish"
86 (&optional force async
))
88 (defvar org-publish-project-alist
)
89 (defvar org-table-number-fraction
)
90 (defvar org-table-number-regexp
)
93 (defsubst org-export-get-parent
(blob)
94 "Return BLOB parent or nil.
95 BLOB is the element or object considered."
96 (org-element-property :parent blob
))
99 ;;; Internal Variables
101 ;; Among internal variables, the most important is
102 ;; `org-export-options-alist'. This variable define the global export
103 ;; options, shared between every exporter, and how they are acquired.
105 (defconst org-export-max-depth
19
106 "Maximum nesting depth for headlines, counting from 0.")
108 (defconst org-export-options-alist
109 '((:title
"TITLE" nil nil space
)
110 (:date
"DATE" nil nil t
)
111 (:author
"AUTHOR" nil user-full-name t
)
112 (:email
"EMAIL" nil user-mail-address t
)
113 (:description
"DESCRIPTION" nil nil newline
)
114 (:keywords
"KEYWORDS" nil nil space
)
115 (:language
"LANGUAGE" nil org-export-default-language t
)
116 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
117 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
118 (:creator
"CREATOR" nil org-export-creator-string
)
119 (:headline-levels nil
"H" org-export-headline-levels
)
120 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
121 (:section-numbers nil
"num" org-export-with-section-numbers
)
122 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
123 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
124 (:with-author nil
"author" org-export-with-author
)
125 (:with-clocks nil
"c" org-export-with-clocks
)
126 (:with-creator nil
"creator" org-export-with-creator
)
127 (:with-date nil
"date" org-export-with-date
)
128 (:with-drawers nil
"d" org-export-with-drawers
)
129 (:with-email nil
"email" org-export-with-email
)
130 (:with-emphasize nil
"*" org-export-with-emphasize
)
131 (:with-entities nil
"e" org-export-with-entities
)
132 (:with-fixed-width nil
":" org-export-with-fixed-width
)
133 (:with-footnotes nil
"f" org-export-with-footnotes
)
134 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
135 (:with-latex nil
"tex" org-export-with-latex
)
136 (:with-planning nil
"p" org-export-with-planning
)
137 (:with-priority nil
"pri" org-export-with-priority
)
138 (:with-properties nil
"prop" org-export-with-properties
)
139 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
140 (:with-special-strings nil
"-" org-export-with-special-strings
)
141 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
142 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
143 (:with-toc nil
"toc" org-export-with-toc
)
144 (:with-tables nil
"|" org-export-with-tables
)
145 (:with-tags nil
"tags" org-export-with-tags
)
146 (:with-tasks nil
"tasks" org-export-with-tasks
)
147 (:with-timestamps nil
"<" org-export-with-timestamps
)
148 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
149 "Alist between export properties and ways to set them.
151 The CAR of the alist is the property name, and the CDR is a list
152 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
154 KEYWORD is a string representing a buffer keyword, or nil. Each
155 property defined this way can also be set, during subtree
156 export, through a headline property named after the keyword
157 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
159 OPTION is a string that could be found in an #+OPTIONS: line.
160 DEFAULT is the default value for the property.
161 BEHAVIOR determines how Org should handle multiple keywords for
162 the same property. It is a symbol among:
163 nil Keep old value and discard the new one.
164 t Replace old value with the new one.
165 `space' Concatenate the values, separating them with a space.
166 `newline' Concatenate the values, separating them with
168 `split' Split values at white spaces, and cons them to the
171 Values set through KEYWORD and OPTION have precedence over
174 All these properties should be back-end agnostic. Back-end
175 specific properties are set through `org-export-define-backend'.
176 Properties redefined there have precedence over these.")
178 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
179 "List of in-buffer keywords that require special treatment.
180 These keywords are not directly associated to a property. The
181 way they are handled must be hard-coded into
182 `org-export--get-inbuffer-options' function.")
184 (defconst org-export-filters-alist
185 '((:filter-bold . org-export-filter-bold-functions
)
186 (:filter-babel-call . org-export-filter-babel-call-functions
)
187 (:filter-center-block . org-export-filter-center-block-functions
)
188 (:filter-clock . org-export-filter-clock-functions
)
189 (:filter-code . org-export-filter-code-functions
)
190 (:filter-comment . org-export-filter-comment-functions
)
191 (:filter-comment-block . org-export-filter-comment-block-functions
)
192 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
193 (:filter-drawer . org-export-filter-drawer-functions
)
194 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
195 (:filter-entity . org-export-filter-entity-functions
)
196 (:filter-example-block . org-export-filter-example-block-functions
)
197 (:filter-export-block . org-export-filter-export-block-functions
)
198 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
199 (:filter-final-output . org-export-filter-final-output-functions
)
200 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
201 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
202 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
203 (:filter-headline . org-export-filter-headline-functions
)
204 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
205 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
206 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
207 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
208 (:filter-italic . org-export-filter-italic-functions
)
209 (:filter-item . org-export-filter-item-functions
)
210 (:filter-keyword . org-export-filter-keyword-functions
)
211 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
212 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
213 (:filter-line-break . org-export-filter-line-break-functions
)
214 (:filter-link . org-export-filter-link-functions
)
215 (:filter-node-property . org-export-filter-node-property-functions
)
216 (:filter-options . org-export-filter-options-functions
)
217 (:filter-paragraph . org-export-filter-paragraph-functions
)
218 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
219 (:filter-plain-list . org-export-filter-plain-list-functions
)
220 (:filter-plain-text . org-export-filter-plain-text-functions
)
221 (:filter-planning . org-export-filter-planning-functions
)
222 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
223 (:filter-quote-block . org-export-filter-quote-block-functions
)
224 (:filter-radio-target . org-export-filter-radio-target-functions
)
225 (:filter-section . org-export-filter-section-functions
)
226 (:filter-special-block . org-export-filter-special-block-functions
)
227 (:filter-src-block . org-export-filter-src-block-functions
)
228 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
229 (:filter-strike-through . org-export-filter-strike-through-functions
)
230 (:filter-subscript . org-export-filter-subscript-functions
)
231 (:filter-superscript . org-export-filter-superscript-functions
)
232 (:filter-table . org-export-filter-table-functions
)
233 (:filter-table-cell . org-export-filter-table-cell-functions
)
234 (:filter-table-row . org-export-filter-table-row-functions
)
235 (:filter-target . org-export-filter-target-functions
)
236 (:filter-timestamp . org-export-filter-timestamp-functions
)
237 (:filter-underline . org-export-filter-underline-functions
)
238 (:filter-verbatim . org-export-filter-verbatim-functions
)
239 (:filter-verse-block . org-export-filter-verse-block-functions
))
240 "Alist between filters properties and initial values.
242 The key of each association is a property name accessible through
243 the communication channel. Its value is a configurable global
244 variable defining initial filters.
246 This list is meant to install user specified filters. Back-end
247 developers may install their own filters using
248 `org-export-define-backend'. Filters defined there will always
249 be prepended to the current list, so they always get applied
252 (defconst org-export-default-inline-image-rule
256 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
257 "xpm" "pbm" "pgm" "ppm") t
))))
258 "Default rule for link matching an inline image.
259 This rule applies to links with no description. By default, it
260 will be considered as an inline image if it targets a local file
261 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
262 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
263 See `org-export-inline-image-p' for more information about
266 (defconst org-export-ignored-local-variables
267 '(org-font-lock-keywords
268 org-element--cache org-element--cache-objects org-element--cache-sync-keys
269 org-element--cache-sync-requests org-element--cache-sync-timer
)
270 "List of variables not copied through upon buffer duplication.
271 Export process takes place on a copy of the original buffer.
272 When this copy is created, all Org related local variables not in
273 this list are copied to the new buffer. Variables with an
274 unreadable value are also ignored.")
276 (defvar org-export-async-debug nil
277 "Non-nil means asynchronous export process should leave data behind.
279 This data is found in the appropriate \"*Org Export Process*\"
280 buffer, and in files prefixed with \"org-export-process\" and
281 located in `temporary-file-directory'.
283 When non-nil, it will also set `debug-on-error' to a non-nil
284 value in the external process.")
286 (defvar org-export-stack-contents nil
287 "Record asynchronously generated export results and processes.
288 This is an alist: its CAR is the source of the
289 result (destination file or buffer for a finished process,
290 original buffer for a running one) and its CDR is a list
291 containing the back-end used, as a symbol, and either a process
292 or the time at which it finished. It is used to build the menu
293 from `org-export-stack'.")
295 (defvar org-export--registered-backends nil
296 "List of backends currently available in the exporter.
297 This variable is set with `org-export-define-backend' and
298 `org-export-define-derived-backend' functions.")
300 (defvar org-export-dispatch-last-action nil
301 "Last command called from the dispatcher.
302 The value should be a list. Its CAR is the action, as a symbol,
303 and its CDR is a list of export options.")
305 (defvar org-export-dispatch-last-position
(make-marker)
306 "The position where the last export command was created using the dispatcher.
307 This marker will be used with `C-u C-c C-e' to make sure export repetition
308 uses the same subtree if the previous command was restricted to a subtree.")
310 ;; For compatibility with Org < 8
311 (defvar org-export-current-backend nil
312 "Name, if any, of the back-end used during an export process.
314 Its value is a symbol such as `html', `latex', `ascii', or nil if
315 the back-end is anonymous (see `org-export-create-backend') or if
316 there is no export process in progress.
318 It can be used to teach Babel blocks how to act differently
319 according to the back-end used.")
323 ;;; User-configurable Variables
325 ;; Configuration for the masses.
327 ;; They should never be accessed directly, as their value is to be
328 ;; stored in a property list (cf. `org-export-options-alist').
329 ;; Back-ends will read their value from there instead.
331 (defgroup org-export nil
332 "Options for exporting Org mode files."
336 (defgroup org-export-general nil
337 "General options for export engine."
338 :tag
"Org Export General"
341 (defcustom org-export-with-archived-trees
'headline
342 "Whether sub-trees with the ARCHIVE tag should be exported.
344 This can have three different values:
345 nil Do not export, pretend this tree is not present.
346 t Do export the entire tree.
347 `headline' Only export the headline, but skip the tree below it.
349 This option can also be set with the OPTIONS keyword,
351 :group
'org-export-general
353 (const :tag
"Not at all" nil
)
354 (const :tag
"Headline only" headline
)
355 (const :tag
"Entirely" t
)))
357 (defcustom org-export-with-author t
358 "Non-nil means insert author name into the exported file.
359 This option can also be set with the OPTIONS keyword,
360 e.g. \"author:nil\"."
361 :group
'org-export-general
364 (defcustom org-export-with-clocks nil
365 "Non-nil means export CLOCK keywords.
366 This option can also be set with the OPTIONS keyword,
368 :group
'org-export-general
371 (defcustom org-export-with-creator
'comment
372 "Non-nil means the postamble should contain a creator sentence.
374 The sentence can be set in `org-export-creator-string' and
375 defaults to \"Generated by Org mode XX in Emacs XXX.\".
377 If the value is `comment' insert it as a comment."
378 :group
'org-export-general
380 (const :tag
"No creator sentence" nil
)
381 (const :tag
"Sentence as a comment" comment
)
382 (const :tag
"Insert the sentence" t
)))
384 (defcustom org-export-with-date t
385 "Non-nil means insert date in the exported document.
386 This option can also be set with the OPTIONS keyword,
388 :group
'org-export-general
391 (defcustom org-export-date-timestamp-format nil
392 "Time-stamp format string to use for DATE keyword.
394 The format string, when specified, only applies if date consists
395 in a single time-stamp. Otherwise its value will be ignored.
397 See `format-time-string' for details on how to build this
399 :group
'org-export-general
401 (string :tag
"Time-stamp format string")
402 (const :tag
"No format string" nil
)))
404 (defcustom org-export-creator-string
405 (format "Emacs %s (Org mode %s)"
407 (if (fboundp 'org-version
) (org-version) "unknown version"))
408 "Information about the creator of the document.
409 This option can also be set on with the CREATOR keyword."
410 :group
'org-export-general
411 :type
'(string :tag
"Creator string"))
413 (defcustom org-export-with-drawers
'(not "LOGBOOK")
414 "Non-nil means export contents of standard drawers.
416 When t, all drawers are exported. This may also be a list of
417 drawer names to export, as strings. If that list starts with
418 `not', only drawers with such names will be ignored.
420 This variable doesn't apply to properties drawers. See
421 `org-export-with-properties' instead.
423 This option can also be set with the OPTIONS keyword,
425 :group
'org-export-general
427 :package-version
'(Org .
"8.0")
429 (const :tag
"All drawers" t
)
430 (const :tag
"None" nil
)
431 (repeat :tag
"Selected drawers"
432 (string :tag
"Drawer name"))
433 (list :tag
"Ignored drawers"
434 (const :format
"" not
)
435 (repeat :tag
"Specify names of drawers to ignore during export"
437 (string :tag
"Drawer name")))))
439 (defcustom org-export-with-email nil
440 "Non-nil means insert author email into the exported file.
441 This option can also be set with the OPTIONS keyword,
443 :group
'org-export-general
446 (defcustom org-export-with-emphasize t
447 "Non-nil means interpret *word*, /word/, _word_ and +word+.
449 If the export target supports emphasizing text, the word will be
450 typeset in bold, italic, with an underline or strike-through,
453 This option can also be set with the OPTIONS keyword,
455 :group
'org-export-general
458 (defcustom org-export-exclude-tags
'("noexport")
459 "Tags that exclude a tree from export.
461 All trees carrying any of these tags will be excluded from
462 export. This is without condition, so even subtrees inside that
463 carry one of the `org-export-select-tags' will be removed.
465 This option can also be set with the EXCLUDE_TAGS keyword."
466 :group
'org-export-general
467 :type
'(repeat (string :tag
"Tag")))
469 (defcustom org-export-with-fixed-width t
470 "Non-nil means export lines starting with \":\".
471 This option can also be set with the OPTIONS keyword,
473 :group
'org-export-general
475 :package-version
'(Org .
"8.0")
478 (defcustom org-export-with-footnotes t
479 "Non-nil means Org footnotes should be exported.
480 This option can also be set with the OPTIONS keyword,
482 :group
'org-export-general
485 (defcustom org-export-with-latex t
486 "Non-nil means process LaTeX environments and fragments.
488 This option can also be set with the OPTIONS line,
489 e.g. \"tex:verbatim\". Allowed values are:
491 nil Ignore math snippets.
492 `verbatim' Keep everything in verbatim.
493 t Allow export of math snippets."
494 :group
'org-export-general
496 :package-version
'(Org .
"8.0")
498 (const :tag
"Do not process math in any way" nil
)
499 (const :tag
"Interpret math snippets" t
)
500 (const :tag
"Leave math verbatim" verbatim
)))
502 (defcustom org-export-headline-levels
3
503 "The last level which is still exported as a headline.
505 Inferior levels will usually produce itemize or enumerate lists
506 when exported, but back-end behaviour may differ.
508 This option can also be set with the OPTIONS keyword,
510 :group
'org-export-general
513 (defcustom org-export-default-language
"en"
514 "The default language for export and clocktable translations, as a string.
515 This may have an association in
516 `org-clock-clocktable-language-setup',
517 `org-export-smart-quotes-alist' and `org-export-dictionary'.
518 This option can also be set with the LANGUAGE keyword."
519 :group
'org-export-general
520 :type
'(string :tag
"Language"))
522 (defcustom org-export-preserve-breaks nil
523 "Non-nil means preserve all line breaks when exporting.
524 This option can also be set with the OPTIONS keyword,
526 :group
'org-export-general
529 (defcustom org-export-with-entities t
530 "Non-nil means interpret entities when exporting.
532 For example, HTML export converts \\alpha to α and \\AA to
535 For a list of supported names, see the constant `org-entities'
536 and the user option `org-entities-user'.
538 This option can also be set with the OPTIONS keyword,
540 :group
'org-export-general
543 (defcustom org-export-with-inlinetasks t
544 "Non-nil means inlinetasks should be exported.
545 This option can also be set with the OPTIONS keyword,
546 e.g. \"inline:nil\"."
547 :group
'org-export-general
549 :package-version
'(Org .
"8.0")
552 (defcustom org-export-with-planning nil
553 "Non-nil means include planning info in export.
555 Planning info is the line containing either SCHEDULED:,
556 DEADLINE:, CLOSED: time-stamps, or a combination of them.
558 This option can also be set with the OPTIONS keyword,
560 :group
'org-export-general
562 :package-version
'(Org .
"8.0")
565 (defcustom org-export-with-priority nil
566 "Non-nil means include priority cookies in export.
567 This option can also be set with the OPTIONS keyword,
569 :group
'org-export-general
572 (defcustom org-export-with-properties nil
573 "Non-nil means export contents of properties drawers.
575 When t, all properties are exported. This may also be a list of
576 properties to export, as strings.
578 This option can also be set with the OPTIONS keyword,
580 :group
'org-export-general
582 :package-version
'(Org .
"8.3")
584 (const :tag
"All properties" t
)
585 (const :tag
"None" nil
)
586 (repeat :tag
"Selected properties"
587 (string :tag
"Property name"))))
589 (defcustom org-export-with-section-numbers t
590 "Non-nil means add section numbers to headlines when exporting.
592 When set to an integer n, numbering will only happen for
593 headlines whose relative level is higher or equal to n.
595 This option can also be set with the OPTIONS keyword,
597 :group
'org-export-general
600 (defcustom org-export-select-tags
'("export")
601 "Tags that select a tree for export.
603 If any such tag is found in a buffer, all trees that do not carry
604 one of these tags will be ignored during export. Inside trees
605 that are selected like this, you can still deselect a subtree by
606 tagging it with one of the `org-export-exclude-tags'.
608 This option can also be set with the SELECT_TAGS keyword."
609 :group
'org-export-general
610 :type
'(repeat (string :tag
"Tag")))
612 (defcustom org-export-with-smart-quotes nil
613 "Non-nil means activate smart quotes during export.
614 This option can also be set with the OPTIONS keyword,
617 When setting this to non-nil, you need to take care of
618 using the correct Babel package when exporting to LaTeX.
619 E.g., you can load Babel for french like this:
621 #+LATEX_HEADER: \\usepackage[french]{babel}"
622 :group
'org-export-general
624 :package-version
'(Org .
"8.0")
627 (defcustom org-export-with-special-strings t
628 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
630 When this option is turned on, these strings will be exported as:
633 -----+----------+--------+-------
637 ... … \\ldots …
639 This option can also be set with the OPTIONS keyword,
641 :group
'org-export-general
644 (defcustom org-export-with-statistics-cookies t
645 "Non-nil means include statistics cookies in export.
646 This option can also be set with the OPTIONS keyword,
648 :group
'org-export-general
650 :package-version
'(Org .
"8.0")
653 (defcustom org-export-with-sub-superscripts t
654 "Non-nil means interpret \"_\" and \"^\" for export.
656 If you want to control how Org displays those characters, see
657 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
658 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
661 When this option is turned on, you can use TeX-like syntax for
662 sub- and superscripts and see them exported correctly.
664 You can also set the option with #+OPTIONS: ^:t
666 Several characters after \"_\" or \"^\" will be considered as a
667 single item - so grouping with {} is normally not needed. For
668 example, the following things will be parsed as single sub- or
671 10^24 or 10^tau several digits will be considered 1 item.
672 10^-12 or 10^-tau a leading sign with digits or a word
673 x^2-y^3 will be read as x^2 - y^3, because items are
674 terminated by almost any nonword/nondigit char.
675 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
677 Still, ambiguity is possible. So when in doubt, use {} to enclose
678 the sub/superscript. If you set this variable to the symbol `{}',
679 the braces are *required* in order to trigger interpretations as
680 sub/superscript. This can be helpful in documents that need \"_\"
681 frequently in plain text."
682 :group
'org-export-general
684 :package-version
'(Org .
"8.0")
686 (const :tag
"Interpret them" t
)
687 (const :tag
"Curly brackets only" {})
688 (const :tag
"Do not interpret them" nil
)))
690 (defcustom org-export-with-toc t
691 "Non-nil means create a table of contents in exported files.
693 The TOC contains headlines with levels up
694 to`org-export-headline-levels'. When an integer, include levels
695 up to N in the toc, this may then be different from
696 `org-export-headline-levels', but it will not be allowed to be
697 larger than the number of headline levels. When nil, no table of
700 This option can also be set with the OPTIONS keyword,
701 e.g. \"toc:nil\" or \"toc:3\"."
702 :group
'org-export-general
704 (const :tag
"No Table of Contents" nil
)
705 (const :tag
"Full Table of Contents" t
)
706 (integer :tag
"TOC to level")))
708 (defcustom org-export-with-tables t
709 "Non-nil means export tables.
710 This option can also be set with the OPTIONS keyword,
712 :group
'org-export-general
714 :package-version
'(Org .
"8.0")
717 (defcustom org-export-with-tags t
718 "If nil, do not export tags, just remove them from headlines.
720 If this is the symbol `not-in-toc', tags will be removed from
721 table of contents entries, but still be shown in the headlines of
724 This option can also be set with the OPTIONS keyword,
726 :group
'org-export-general
728 (const :tag
"Off" nil
)
729 (const :tag
"Not in TOC" not-in-toc
)
730 (const :tag
"On" t
)))
732 (defcustom org-export-with-tasks t
733 "Non-nil means include TODO items for export.
735 This may have the following values:
736 t include tasks independent of state.
737 `todo' include only tasks that are not yet done.
738 `done' include only tasks that are already done.
739 nil ignore all tasks.
740 list of keywords include tasks with these keywords.
742 This option can also be set with the OPTIONS keyword,
744 :group
'org-export-general
746 (const :tag
"All tasks" t
)
747 (const :tag
"No tasks" nil
)
748 (const :tag
"Not-done tasks" todo
)
749 (const :tag
"Only done tasks" done
)
750 (repeat :tag
"Specific TODO keywords"
751 (string :tag
"Keyword"))))
753 (defcustom org-export-time-stamp-file t
754 "Non-nil means insert a time stamp into the exported file.
755 The time stamp shows when the file was created. This option can
756 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
757 :group
'org-export-general
760 (defcustom org-export-with-timestamps t
761 "Non nil means allow timestamps in export.
763 It can be set to any of the following values:
764 t export all timestamps.
765 `active' export active timestamps only.
766 `inactive' export inactive timestamps only.
767 nil do not export timestamps
769 This only applies to timestamps isolated in a paragraph
770 containing only timestamps. Other timestamps are always
773 This option can also be set with the OPTIONS keyword, e.g.
775 :group
'org-export-general
777 (const :tag
"All timestamps" t
)
778 (const :tag
"Only active timestamps" active
)
779 (const :tag
"Only inactive timestamps" inactive
)
780 (const :tag
"No timestamp" nil
)))
782 (defcustom org-export-with-todo-keywords t
783 "Non-nil means include TODO keywords in export.
784 When nil, remove all these keywords from the export. This option
785 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
786 :group
'org-export-general
789 (defcustom org-export-allow-bind-keywords nil
790 "Non-nil means BIND keywords can define local variable values.
791 This is a potential security risk, which is why the default value
792 is nil. You can also allow them through local buffer variables."
793 :group
'org-export-general
795 :package-version
'(Org .
"8.0")
798 (defcustom org-export-snippet-translation-alist nil
799 "Alist between export snippets back-ends and exporter back-ends.
801 This variable allows to provide shortcuts for export snippets.
803 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
804 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
805 HTML code while every other back-end will ignore it."
806 :group
'org-export-general
808 :package-version
'(Org .
"8.0")
810 (cons (string :tag
"Shortcut")
811 (string :tag
"Back-end"))))
813 (defcustom org-export-coding-system nil
814 "Coding system for the exported file."
815 :group
'org-export-general
817 :package-version
'(Org .
"8.0")
818 :type
'coding-system
)
820 (defcustom org-export-copy-to-kill-ring nil
821 "Non-nil means pushing export output to the kill ring.
822 This variable is ignored during asynchronous export."
823 :group
'org-export-general
825 :package-version
'(Org .
"8.3")
827 (const :tag
"Always" t
)
828 (const :tag
"When export is done interactively" if-interactive
)
829 (const :tag
"Never" nil
)))
831 (defcustom org-export-initial-scope
'buffer
832 "The initial scope when exporting with `org-export-dispatch'.
833 This variable can be either set to `buffer' or `subtree'."
834 :group
'org-export-general
836 (const :tag
"Export current buffer" buffer
)
837 (const :tag
"Export current subtree" subtree
)))
839 (defcustom org-export-show-temporary-export-buffer t
840 "Non-nil means show buffer after exporting to temp buffer.
841 When Org exports to a file, the buffer visiting that file is ever
842 shown, but remains buried. However, when exporting to
843 a temporary buffer, that buffer is popped up in a second window.
844 When this variable is nil, the buffer remains buried also in
846 :group
'org-export-general
849 (defcustom org-export-in-background nil
850 "Non-nil means export and publishing commands will run in background.
851 Results from an asynchronous export are never displayed
852 automatically. But you can retrieve them with \\[org-export-stack]."
853 :group
'org-export-general
855 :package-version
'(Org .
"8.0")
858 (defcustom org-export-async-init-file nil
859 "File used to initialize external export process.
861 Value must be either nil or an absolute file name. When nil, the
862 external process is launched like a regular Emacs session,
863 loading user's initialization file and any site specific
864 configuration. If a file is provided, it, and only it, is loaded
867 Therefore, using a specific configuration makes the process to
868 load faster and the export more portable."
869 :group
'org-export-general
871 :package-version
'(Org .
"8.0")
873 (const :tag
"Regular startup" nil
)
874 (file :tag
"Specific start-up file" :must-match t
)))
876 (defcustom org-export-dispatch-use-expert-ui nil
877 "Non-nil means using a non-intrusive `org-export-dispatch'.
878 In that case, no help buffer is displayed. Though, an indicator
879 for current export scope is added to the prompt (\"b\" when
880 output is restricted to body only, \"s\" when it is restricted to
881 the current subtree, \"v\" when only visible elements are
882 considered for export, \"f\" when publishing functions should be
883 passed the FORCE argument and \"a\" when the export should be
884 asynchronous). Also, \[?] allows to switch back to standard
886 :group
'org-export-general
888 :package-version
'(Org .
"8.0")
893 ;;; Defining Back-ends
895 ;; An export back-end is a structure with `org-export-backend' type
896 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
899 ;; At the lowest level, a back-end is created with
900 ;; `org-export-create-backend' function.
902 ;; A named back-end can be registered with
903 ;; `org-export-register-backend' function. A registered back-end can
904 ;; later be referred to by its name, with `org-export-get-backend'
905 ;; function. Also, such a back-end can become the parent of a derived
906 ;; back-end from which slot values will be inherited by default.
907 ;; `org-export-derived-backend-p' can check if a given back-end is
908 ;; derived from a list of back-end names.
910 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
911 ;; `org-export-get-all-filters' return the full alist of transcoders,
912 ;; options and filters, including those inherited from ancestors.
914 ;; At a higher level, `org-export-define-backend' is the standard way
915 ;; to define an export back-end. If the new back-end is similar to
916 ;; a registered back-end, `org-export-define-derived-backend' may be
919 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
920 ;; when a given back-end hasn't been registered yet.
922 (defstruct (org-export-backend (:constructor org-export-create-backend
)
924 name parent transcoders options filters blocks menu
)
926 (defun org-export-get-backend (name)
927 "Return export back-end named after NAME.
928 NAME is a symbol. Return nil if no such back-end is found."
930 (dolist (b org-export--registered-backends
)
931 (when (eq (org-export-backend-name b
) name
)
934 (defun org-export-register-backend (backend)
935 "Register BACKEND as a known export back-end.
936 BACKEND is a structure with `org-export-backend' type."
937 ;; Refuse to register an unnamed back-end.
938 (unless (org-export-backend-name backend
)
939 (error "Cannot register a unnamed export back-end"))
940 ;; Refuse to register a back-end with an unknown parent.
941 (let ((parent (org-export-backend-parent backend
)))
942 (when (and parent
(not (org-export-get-backend parent
)))
943 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
944 ;; Register dedicated export blocks in the parser.
945 (dolist (name (org-export-backend-blocks backend
))
946 (add-to-list 'org-element-block-name-alist
947 (cons name
'org-element-export-block-parser
)))
948 ;; If a back-end with the same name as BACKEND is already
949 ;; registered, replace it with BACKEND. Otherwise, simply add
950 ;; BACKEND to the list of registered back-ends.
951 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
952 (if old
(setcar (memq old org-export--registered-backends
) backend
)
953 (push backend org-export--registered-backends
))))
955 (defun org-export-barf-if-invalid-backend (backend)
956 "Signal an error if BACKEND isn't defined."
957 (unless (org-export-backend-p backend
)
958 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
960 (defun org-export-derived-backend-p (backend &rest backends
)
961 "Non-nil if BACKEND is derived from one of BACKENDS.
962 BACKEND is an export back-end, as returned by, e.g.,
963 `org-export-create-backend', or a symbol referring to
964 a registered back-end. BACKENDS is constituted of symbols."
965 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
968 (while (org-export-backend-parent backend
)
969 (when (memq (org-export-backend-name backend
) backends
)
972 (org-export-get-backend (org-export-backend-parent backend
))))
973 (memq (org-export-backend-name backend
) backends
))))
975 (defun org-export-get-all-transcoders (backend)
976 "Return full translation table for BACKEND.
978 BACKEND is an export back-end, as return by, e.g,,
979 `org-export-create-backend'. Return value is an alist where
980 keys are element or object types, as symbols, and values are
983 Unlike to `org-export-backend-transcoders', this function
984 also returns transcoders inherited from parent back-ends,
986 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
988 (let ((transcoders (org-export-backend-transcoders backend
))
990 (while (setq parent
(org-export-backend-parent backend
))
991 (setq backend
(org-export-get-backend parent
))
993 (append transcoders
(org-export-backend-transcoders backend
))))
996 (defun org-export-get-all-options (backend)
997 "Return export options for BACKEND.
999 BACKEND is an export back-end, as return by, e.g,,
1000 `org-export-create-backend'. See `org-export-options-alist'
1001 for the shape of the return value.
1003 Unlike to `org-export-backend-options', this function also
1004 returns options inherited from parent back-ends, if any."
1005 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1007 (let ((options (org-export-backend-options backend
))
1009 (while (setq parent
(org-export-backend-parent backend
))
1010 (setq backend
(org-export-get-backend parent
))
1011 (setq options
(append options
(org-export-backend-options backend
))))
1014 (defun org-export-get-all-filters (backend)
1015 "Return complete list of filters for BACKEND.
1017 BACKEND is an export back-end, as return by, e.g,,
1018 `org-export-create-backend'. Return value is an alist where
1019 keys are symbols and values lists of functions.
1021 Unlike to `org-export-backend-filters', this function also
1022 returns filters inherited from parent back-ends, if any."
1023 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1025 (let ((filters (org-export-backend-filters backend
))
1027 (while (setq parent
(org-export-backend-parent backend
))
1028 (setq backend
(org-export-get-backend parent
))
1029 (setq filters
(append filters
(org-export-backend-filters backend
))))
1032 (defun org-export-define-backend (backend transcoders
&rest body
)
1033 "Define a new back-end BACKEND.
1035 TRANSCODERS is an alist between object or element types and
1036 functions handling them.
1038 These functions should return a string without any trailing
1039 space, or nil. They must accept three arguments: the object or
1040 element itself, its contents or nil when it isn't recursive and
1041 the property list used as a communication channel.
1043 Contents, when not nil, are stripped from any global indentation
1044 \(although the relative one is preserved). They also always end
1045 with a single newline character.
1047 If, for a given type, no function is found, that element or
1048 object type will simply be ignored, along with any blank line or
1049 white space at its end. The same will happen if the function
1050 returns the nil value. If that function returns the empty
1051 string, the type will be ignored, but the blank lines or white
1052 spaces will be kept.
1054 In addition to element and object types, one function can be
1055 associated to the `template' (or `inner-template') symbol and
1056 another one to the `plain-text' symbol.
1058 The former returns the final transcoded string, and can be used
1059 to add a preamble and a postamble to document's body. It must
1060 accept two arguments: the transcoded string and the property list
1061 containing export options. A function associated to `template'
1062 will not be applied if export has option \"body-only\".
1063 A function associated to `inner-template' is always applied.
1065 The latter, when defined, is to be called on every text not
1066 recognized as an element or an object. It must accept two
1067 arguments: the text string and the information channel. It is an
1068 appropriate place to protect special chars relative to the
1071 BODY can start with pre-defined keyword arguments. The following
1072 keywords are understood:
1076 String, or list of strings, representing block names that
1077 will not be parsed. This is used to specify blocks that will
1078 contain raw code specific to the back-end. These blocks
1079 still have to be handled by the relative `export-block' type
1084 Alist between filters and function, or list of functions,
1085 specific to the back-end. See `org-export-filters-alist' for
1086 a list of all allowed filters. Filters defined here
1087 shouldn't make a back-end test, as it may prevent back-ends
1088 derived from this one to behave properly.
1092 Menu entry for the export dispatcher. It should be a list
1095 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1099 KEY is a free character selecting the back-end.
1101 DESCRIPTION-OR-ORDINAL is either a string or a number.
1103 If it is a string, is will be used to name the back-end in
1104 its menu entry. If it is a number, the following menu will
1105 be displayed as a sub-menu of the back-end with the same
1106 KEY. Also, the number will be used to determine in which
1107 order such sub-menus will appear (lowest first).
1109 ACTION-OR-MENU is either a function or an alist.
1111 If it is an action, it will be called with four
1112 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1113 BODY-ONLY. See `org-export-as' for further explanations on
1116 If it is an alist, associations should follow the
1119 '(KEY DESCRIPTION ACTION)
1121 where KEY, DESCRIPTION and ACTION are described above.
1123 Valid values include:
1125 '(?m \"My Special Back-end\" my-special-export-function)
1129 '(?l \"Export to LaTeX\"
1130 \(?p \"As PDF file\" org-latex-export-to-pdf)
1131 \(?o \"As PDF file and open\"
1133 \(if a (org-latex-export-to-pdf t s v b)
1135 \(org-latex-export-to-pdf nil s v b)))))))
1137 or the following, which will be added to the previous
1141 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1142 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1146 Alist between back-end specific properties introduced in
1147 communication channel and how their value are acquired. See
1148 `org-export-options-alist' for more information about
1149 structure of the values."
1150 (declare (indent 1))
1151 (let (blocks filters menu-entry options contents
)
1152 (while (keywordp (car body
))
1154 (:export-block
(let ((names (pop body
)))
1155 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1156 (list (upcase names
))))))
1157 (:filters-alist
(setq filters
(pop body
)))
1158 (:menu-entry
(setq menu-entry
(pop body
)))
1159 (:options-alist
(setq options
(pop body
)))
1161 (org-export-register-backend
1162 (org-export-create-backend :name backend
1163 :transcoders transcoders
1167 :menu menu-entry
))))
1169 (defun org-export-define-derived-backend (child parent
&rest body
)
1170 "Create a new back-end as a variant of an existing one.
1172 CHILD is the name of the derived back-end. PARENT is the name of
1173 the parent back-end.
1175 BODY can start with pre-defined keyword arguments. The following
1176 keywords are understood:
1180 String, or list of strings, representing block names that
1181 will not be parsed. This is used to specify blocks that will
1182 contain raw code specific to the back-end. These blocks
1183 still have to be handled by the relative `export-block' type
1188 Alist of filters that will overwrite or complete filters
1189 defined in PARENT back-end. See `org-export-filters-alist'
1190 for a list of allowed filters.
1194 Menu entry for the export dispatcher. See
1195 `org-export-define-backend' for more information about the
1200 Alist of back-end specific properties that will overwrite or
1201 complete those defined in PARENT back-end. Refer to
1202 `org-export-options-alist' for more information about
1203 structure of the values.
1207 Alist of element and object types and transcoders that will
1208 overwrite or complete transcode table from PARENT back-end.
1209 Refer to `org-export-define-backend' for detailed information
1212 As an example, here is how one could define \"my-latex\" back-end
1213 as a variant of `latex' back-end with a custom template function:
1215 \(org-export-define-derived-backend 'my-latex 'latex
1216 :translate-alist '((template . my-latex-template-fun)))
1218 The back-end could then be called with, for example:
1220 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1221 (declare (indent 2))
1222 (let (blocks filters menu-entry options transcoders contents
)
1223 (while (keywordp (car body
))
1225 (:export-block
(let ((names (pop body
)))
1226 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1227 (list (upcase names
))))))
1228 (:filters-alist
(setq filters
(pop body
)))
1229 (:menu-entry
(setq menu-entry
(pop body
)))
1230 (:options-alist
(setq options
(pop body
)))
1231 (:translate-alist
(setq transcoders
(pop body
)))
1233 (org-export-register-backend
1234 (org-export-create-backend :name child
1236 :transcoders transcoders
1240 :menu menu-entry
))))
1244 ;;; The Communication Channel
1246 ;; During export process, every function has access to a number of
1247 ;; properties. They are of two types:
1249 ;; 1. Environment options are collected once at the very beginning of
1250 ;; the process, out of the original buffer and configuration.
1251 ;; Collecting them is handled by `org-export-get-environment'
1254 ;; Most environment options are defined through the
1255 ;; `org-export-options-alist' variable.
1257 ;; 2. Tree properties are extracted directly from the parsed tree,
1258 ;; just before export, by `org-export-collect-tree-properties'.
1260 ;; Here is the full list of properties available during transcode
1261 ;; process, with their category and their value type.
1263 ;; + `:author' :: Author's name.
1264 ;; - category :: option
1267 ;; + `:back-end' :: Current back-end used for transcoding.
1268 ;; - category :: tree
1269 ;; - type :: structure
1271 ;; + `:creator' :: String to write as creation information.
1272 ;; - category :: option
1275 ;; + `:date' :: String to use as date.
1276 ;; - category :: option
1279 ;; + `:description' :: Description text for the current data.
1280 ;; - category :: option
1283 ;; + `:email' :: Author's email.
1284 ;; - category :: option
1287 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1289 ;; - category :: option
1290 ;; - type :: list of strings
1292 ;; + `:export-options' :: List of export options available for current
1294 ;; - category :: none
1295 ;; - type :: list of symbols, among `subtree', `body-only' and
1298 ;; + `:exported-data' :: Hash table used for memoizing
1299 ;; `org-export-data'.
1300 ;; - category :: tree
1301 ;; - type :: hash table
1303 ;; + `:filetags' :: List of global tags for buffer. Used by
1304 ;; `org-export-get-tags' to get tags with inheritance.
1305 ;; - category :: option
1306 ;; - type :: list of strings
1308 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1309 ;; their definition, as parsed data. Only non-inlined footnotes
1310 ;; are represented in this alist. Also, every definition isn't
1311 ;; guaranteed to be referenced in the parse tree. The purpose of
1312 ;; this property is to preserve definitions from oblivion
1313 ;; (i.e. when the parse tree comes from a part of the original
1314 ;; buffer), it isn't meant for direct use in a back-end. To
1315 ;; retrieve a definition relative to a reference, use
1316 ;; `org-export-get-footnote-definition' instead.
1317 ;; - category :: option
1318 ;; - type :: alist (STRING . LIST)
1320 ;; + `:headline-levels' :: Maximum level being exported as an
1321 ;; headline. Comparison is done with the relative level of
1322 ;; headlines in the parse tree, not necessarily with their
1324 ;; - category :: option
1325 ;; - type :: integer
1327 ;; + `:headline-offset' :: Difference between relative and real level
1328 ;; of headlines in the parse tree. For example, a value of -1
1329 ;; means a level 2 headline should be considered as level
1330 ;; 1 (cf. `org-export-get-relative-level').
1331 ;; - category :: tree
1332 ;; - type :: integer
1334 ;; + `:headline-numbering' :: Alist between headlines and their
1335 ;; numbering, as a list of numbers
1336 ;; (cf. `org-export-get-headline-number').
1337 ;; - category :: tree
1338 ;; - type :: alist (INTEGER . LIST)
1340 ;; + `:id-alist' :: Alist between ID strings and destination file's
1341 ;; path, relative to current directory. It is used by
1342 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1344 ;; - category :: option
1345 ;; - type :: alist (STRING . STRING)
1347 ;; + `:ignore-list' :: List of elements and objects that should be
1348 ;; ignored during export.
1349 ;; - category :: tree
1350 ;; - type :: list of elements and objects
1352 ;; + `:input-buffer' :: Original buffer name.
1353 ;; - category :: option
1356 ;; + `:input-file' :: Full path to input file, if any.
1357 ;; - category :: option
1358 ;; - type :: string or nil
1360 ;; + `:keywords' :: List of keywords attached to data.
1361 ;; - category :: option
1364 ;; + `:language' :: Default language used for translations.
1365 ;; - category :: option
1368 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1370 ;; - category :: option
1371 ;; - type :: list (as returned by `org-element-parse-buffer')
1373 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1375 ;; - category :: option
1376 ;; - type :: symbol (nil, t)
1378 ;; + `:section-numbers' :: Non-nil means transcoding should add
1379 ;; section numbers to headlines.
1380 ;; - category :: option
1381 ;; - type :: symbol (nil, t)
1383 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1384 ;; in transcoding. When such a tag is present, subtrees without
1385 ;; it are de facto excluded from the process. See
1386 ;; `use-select-tags'.
1387 ;; - category :: option
1388 ;; - type :: list of strings
1390 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1391 ;; a time stamp in the output.
1392 ;; - category :: option
1393 ;; - type :: symbol (nil, t)
1395 ;; + `:translate-alist' :: Alist between element and object types and
1396 ;; transcoding functions relative to the current back-end.
1397 ;; Special keys `inner-template', `template' and `plain-text' are
1399 ;; - category :: option
1400 ;; - type :: alist (SYMBOL . FUNCTION)
1402 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1403 ;; also be transcoded. If it is set to the `headline' symbol,
1404 ;; only the archived headline's name is retained.
1405 ;; - category :: option
1406 ;; - type :: symbol (nil, t, `headline')
1408 ;; + `:with-author' :: Non-nil means author's name should be included
1410 ;; - category :: option
1411 ;; - type :: symbol (nil, t)
1413 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1414 ;; - category :: option
1415 ;; - type :: symbol (nil, t)
1417 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1418 ;; inserted at the end of the transcoded string. If the value
1419 ;; is `comment', it should be commented.
1420 ;; - category :: option
1421 ;; - type :: symbol (`comment', nil, t)
1423 ;; + `:with-date' :: Non-nil means output should contain a date.
1424 ;; - category :: option
1425 ;; - type :. symbol (nil, t)
1427 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1428 ;; its value is a list of names, only drawers with such names
1429 ;; will be transcoded. If that list starts with `not', drawer
1430 ;; with these names will be skipped.
1431 ;; - category :: option
1432 ;; - type :: symbol (nil, t) or list of strings
1434 ;; + `:with-email' :: Non-nil means output should contain author's
1436 ;; - category :: option
1437 ;; - type :: symbol (nil, t)
1439 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1441 ;; - category :: option
1442 ;; - type :: symbol (nil, t)
1444 ;; + `:with-fixed-width' :: Non-nil if transcoder should export
1445 ;; strings starting with a colon as a fixed-with (verbatim) area.
1446 ;; - category :: option
1447 ;; - type :: symbol (nil, t)
1449 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1451 ;; - category :: option
1452 ;; - type :: symbol (nil, t)
1454 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1455 ;; `latex-fragment' objects should appear in export output. When
1456 ;; this property is set to `verbatim', they will be left as-is.
1457 ;; - category :: option
1458 ;; - type :: symbol (`verbatim', nil, t)
1460 ;; + `:with-planning' :: Non-nil means transcoding should include
1462 ;; - category :: option
1463 ;; - type :: symbol (nil, t)
1465 ;; + `:with-priority' :: Non-nil means transcoding should include
1466 ;; priority cookies.
1467 ;; - category :: option
1468 ;; - type :: symbol (nil, t)
1470 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1472 ;; - category :: option
1473 ;; - type :: symbol (nil, t)
1475 ;; + `:with-special-strings' :: Non-nil means transcoding should
1476 ;; interpret special strings in plain text.
1477 ;; - category :: option
1478 ;; - type :: symbol (nil, t)
1480 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1481 ;; interpret subscript and superscript. With a value of "{}",
1482 ;; only interpret those using curly brackets.
1483 ;; - category :: option
1484 ;; - type :: symbol (nil, {}, t)
1486 ;; + `:with-tables' :: Non-nil means transcoding should export tables.
1487 ;; - category :: option
1488 ;; - type :: symbol (nil, t)
1490 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1491 ;; headlines. A `not-in-toc' value will remove them from the
1492 ;; table of contents, if any, nonetheless.
1493 ;; - category :: option
1494 ;; - type :: symbol (nil, t, `not-in-toc')
1496 ;; + `:with-tasks' :: Non-nil means transcoding should include
1497 ;; headlines with a TODO keyword. A `todo' value will only
1498 ;; include headlines with a todo type keyword while a `done'
1499 ;; value will do the contrary. If a list of strings is provided,
1500 ;; only tasks with keywords belonging to that list will be kept.
1501 ;; - category :: option
1502 ;; - type :: symbol (t, todo, done, nil) or list of strings
1504 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1505 ;; time stamps. Special value `active' (resp. `inactive') ask to
1506 ;; export only active (resp. inactive) timestamps. Otherwise,
1507 ;; completely remove them.
1508 ;; - category :: option
1509 ;; - type :: symbol: (`active', `inactive', t, nil)
1511 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1512 ;; added to the output. An integer value limits its depth.
1513 ;; - category :: option
1514 ;; - type :: symbol (nil, t or integer)
1516 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1517 ;; include TODO keywords.
1518 ;; - category :: option
1519 ;; - type :: symbol (nil, t)
1522 ;;;; Environment Options
1524 ;; Environment options encompass all parameters defined outside the
1525 ;; scope of the parsed data. They come from five sources, in
1526 ;; increasing precedence order:
1528 ;; - Global variables,
1529 ;; - Buffer's attributes,
1530 ;; - Options keyword symbols,
1531 ;; - Buffer keywords,
1532 ;; - Subtree properties.
1534 ;; The central internal function with regards to environment options
1535 ;; is `org-export-get-environment'. It updates global variables with
1536 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1537 ;; the different sources.
1539 ;; The internal functions doing the retrieval are:
1540 ;; `org-export--get-global-options',
1541 ;; `org-export--get-buffer-attributes',
1542 ;; `org-export--parse-option-keyword',
1543 ;; `org-export--get-subtree-options' and
1544 ;; `org-export--get-inbuffer-options'
1546 ;; Also, `org-export--list-bound-variables' collects bound variables
1547 ;; along with their value in order to set them as buffer local
1548 ;; variables later in the process.
1550 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1551 "Collect export options from the current buffer.
1553 Optional argument BACKEND is an export back-end, as returned by
1554 `org-export-create-backend'.
1556 When optional argument SUBTREEP is non-nil, assume the export is
1557 done against the current sub-tree.
1559 Third optional argument EXT-PLIST is a property list with
1560 external parameters overriding Org default settings, but still
1561 inferior to file-local settings."
1562 ;; First install #+BIND variables since these must be set before
1563 ;; global options are read.
1564 (dolist (pair (org-export--list-bound-variables))
1565 (org-set-local (car pair
) (nth 1 pair
)))
1566 ;; Get and prioritize export options...
1568 ;; ... from global variables...
1569 (org-export--get-global-options backend
)
1570 ;; ... from an external property list...
1572 ;; ... from in-buffer settings...
1573 (org-export--get-inbuffer-options backend
)
1574 ;; ... and from subtree, when appropriate.
1575 (and subtreep
(org-export--get-subtree-options backend
))
1576 ;; Eventually add misc. properties.
1580 :translate-alist
(org-export-get-all-transcoders backend
)
1581 :footnote-definition-alist
1582 ;; Footnotes definitions must be collected in the original
1583 ;; buffer, as there's no insurance that they will still be in
1584 ;; the parse tree, due to possible narrowing.
1586 (org-with-wide-buffer
1587 (goto-char (point-min))
1588 (while (re-search-forward org-footnote-definition-re nil t
)
1589 (let ((def (save-match-data (org-element-at-point))))
1590 (when (eq (org-element-type def
) 'footnote-definition
)
1592 (cons (org-element-property :label def
)
1593 (let ((cbeg (org-element-property :contents-begin def
)))
1595 (org-element--parse-elements
1596 cbeg
(org-element-property :contents-end def
)
1597 nil nil nil nil
(list 'org-data nil
)))))
1601 ;; Collect id references.
1603 (org-with-wide-buffer
1604 (goto-char (point-min))
1605 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1606 (let ((link (org-element-context)))
1607 (when (eq (org-element-type link
) 'link
)
1608 (let* ((id (org-element-property :path link
))
1609 (file (org-id-find-id-file id
)))
1611 (push (cons id
(file-relative-name file
)) alist
)))))))
1614 (defun org-export--parse-option-keyword (options &optional backend
)
1615 "Parse an OPTIONS line and return values as a plist.
1616 Optional argument BACKEND is an export back-end, as returned by,
1617 e.g., `org-export-create-backend'. It specifies which back-end
1618 specific items to read, if any."
1620 ;; Priority is given to back-end specific options.
1621 (append (and backend
(org-export-get-all-options backend
))
1622 org-export-options-alist
))
1624 (dolist (option all
)
1625 (let ((property (car option
))
1626 (item (nth 2 option
)))
1628 (not (plist-member plist property
))
1629 (string-match (concat "\\(\\`\\|[ \t]\\)"
1631 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1633 (setq plist
(plist-put plist
1635 (car (read-from-string
1636 (match-string 2 options
))))))))
1639 (defun org-export--get-subtree-options (&optional backend
)
1640 "Get export options in subtree at point.
1641 Optional argument BACKEND is an export back-end, as returned by,
1642 e.g., `org-export-create-backend'. It specifies back-end used
1643 for export. Return options as a plist."
1644 ;; For each buffer keyword, create a headline property setting the
1645 ;; same property in communication channel. The name for the property
1646 ;; is the keyword with "EXPORT_" appended to it.
1647 (org-with-wide-buffer
1649 ;; Make sure point is at a heading.
1650 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1651 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1652 ;; title (with no todo keyword, priority cookie or tag) as its
1654 (when (setq prop
(or (org-entry-get (point) "EXPORT_TITLE")
1655 (progn (looking-at org-complex-heading-regexp
)
1656 (org-match-string-no-properties 4))))
1660 (org-element-parse-secondary-string
1661 prop
(org-element-restriction 'keyword
)))))
1662 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1663 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1665 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1666 ;; Handle other keywords. TITLE keyword is excluded as it has
1667 ;; been handled already.
1668 (let ((seen '("TITLE")))
1671 (let ((property (car option
))
1672 (keyword (nth 1 option
)))
1673 (when (and keyword
(not (member keyword seen
)))
1674 (let* ((subtree-prop (concat "EXPORT_" keyword
))
1675 ;; Export properties are not case-sensitive.
1676 (value (let ((case-fold-search t
))
1677 (org-entry-get (point) subtree-prop
))))
1679 (when (and value
(not (plist-member plist property
)))
1685 ;; Parse VALUE if required.
1686 ((member keyword org-element-document-properties
)
1687 (org-element-parse-secondary-string
1688 value
(org-element-restriction 'keyword
)))
1689 ;; If BEHAVIOR is `split' expected value is
1690 ;; a list of strings, not a string.
1691 ((eq (nth 4 option
) 'split
) (org-split-string value
))
1693 ;; Look for both general keywords and back-end specific
1694 ;; options, with priority given to the latter.
1695 (append (and backend
(org-export-get-all-options backend
))
1696 org-export-options-alist
)))
1700 (defun org-export--get-inbuffer-options (&optional backend
)
1701 "Return current buffer export options, as a plist.
1703 Optional argument BACKEND, when non-nil, is an export back-end,
1704 as returned by, e.g., `org-export-create-backend'. It specifies
1705 which back-end specific options should also be read in the
1708 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1710 get-options
; For byte-compiler.
1711 (case-fold-search t
)
1713 ;; Priority is given to back-end specific options.
1714 (and backend
(org-export-get-all-options backend
))
1715 org-export-options-alist
))
1716 (regexp (format "^[ \t]*#\\+%s:"
1717 (regexp-opt (nconc (delq nil
(mapcar 'cadr options
))
1718 org-export-special-keywords
))))
1721 ;; Return all properties associated to KEYWORD.
1723 (dolist (option options properties
)
1724 (when (equal (nth 1 option
) keyword
)
1725 (pushnew (car option
) properties
))))))
1727 (lambda (&optional files plist
)
1728 ;; Recursively read keywords in buffer. FILES is a list
1729 ;; of files read so far. PLIST is the current property
1731 (org-with-wide-buffer
1732 (goto-char (point-min))
1733 (while (re-search-forward regexp nil t
)
1734 (let ((element (org-element-at-point)))
1735 (when (eq (org-element-type element
) 'keyword
)
1736 (let ((key (org-element-property :key element
))
1737 (val (org-element-property :value element
)))
1739 ;; Options in `org-export-special-keywords'.
1740 ((equal key
"SETUPFILE")
1741 (let ((file (expand-file-name
1742 (org-remove-double-quotes (org-trim val
)))))
1743 ;; Avoid circular dependencies.
1744 (unless (member file files
)
1746 (insert (org-file-contents file
'noerror
))
1747 (let ((org-inhibit-startup t
)) (org-mode))
1748 (setq plist
(funcall get-options
1749 (cons file files
) plist
))))))
1750 ((equal key
"OPTIONS")
1754 (org-export--parse-option-keyword val backend
))))
1755 ((equal key
"FILETAGS")
1761 (append (org-split-string val
":")
1762 (plist-get plist
:filetags
)))))))
1764 ;; Options in `org-export-options-alist'.
1765 (dolist (property (funcall find-properties key
))
1766 (let ((behaviour (nth 4 (assq property options
))))
1770 ;; Handle value depending on specified
1774 (if (not (plist-get plist property
))
1776 (concat (plist-get plist property
)
1781 (concat (plist-get plist property
)
1784 (split `(,@(plist-get plist property
)
1785 ,@(org-split-string val
)))
1788 (if (not (plist-member plist property
)) val
1789 (plist-get plist property
))))))))))))))
1790 ;; Return final value.
1792 ;; Read options in the current buffer.
1793 (setq plist
(funcall get-options
1794 (and buffer-file-name
(list buffer-file-name
)) nil
))
1795 ;; Parse keywords specified in `org-element-document-properties'
1796 ;; and return PLIST.
1797 (dolist (keyword org-element-document-properties plist
)
1798 (dolist (property (funcall find-properties keyword
))
1799 (let ((value (plist-get plist property
)))
1800 (when (stringp value
)
1802 (plist-put plist property
1803 (org-element-parse-secondary-string
1804 value
(org-element-restriction 'keyword
))))))))))
1806 (defun org-export--get-buffer-attributes ()
1807 "Return properties related to buffer attributes, as a plist."
1808 ;; Store full path of input file name, or nil. For internal use.
1809 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1810 (list :input-buffer
(buffer-name (buffer-base-buffer))
1811 :input-file visited-file
1812 :title
(if (not visited-file
) (buffer-name (buffer-base-buffer))
1813 (file-name-sans-extension
1814 (file-name-nondirectory visited-file
))))))
1816 (defun org-export--get-global-options (&optional backend
)
1817 "Return global export options as a plist.
1818 Optional argument BACKEND, if non-nil, is an export back-end, as
1819 returned by, e.g., `org-export-create-backend'. It specifies
1820 which back-end specific export options should also be read in the
1823 ;; Priority is given to back-end specific options.
1824 (all (append (and backend
(org-export-get-all-options backend
))
1825 org-export-options-alist
)))
1826 (dolist (cell all plist
)
1827 (let ((prop (car cell
))
1828 (default-value (nth 3 cell
)))
1829 (unless (or (not default-value
) (plist-member plist prop
))
1834 ;; Eval default value provided. If keyword is
1835 ;; a member of `org-element-document-properties',
1836 ;; parse it as a secondary string before storing it.
1837 (let ((value (eval (nth 3 cell
))))
1838 (if (not (stringp value
)) value
1839 (let ((keyword (nth 1 cell
)))
1840 (if (member keyword org-element-document-properties
)
1841 (org-element-parse-secondary-string
1842 value
(org-element-restriction 'keyword
))
1845 (defun org-export--list-bound-variables ()
1846 "Return variables bound from BIND keywords in current buffer.
1847 Also look for BIND keywords in setup files. The return value is
1848 an alist where associations are (VARIABLE-NAME VALUE)."
1849 (when org-export-allow-bind-keywords
1850 (let* (collect-bind ; For byte-compiler.
1852 (lambda (files alist
)
1853 ;; Return an alist between variable names and their
1854 ;; value. FILES is a list of setup files names read so
1855 ;; far, used to avoid circular dependencies. ALIST is
1856 ;; the alist collected so far.
1857 (let ((case-fold-search t
))
1858 (org-with-wide-buffer
1859 (goto-char (point-min))
1860 (while (re-search-forward
1861 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1862 (let ((element (org-element-at-point)))
1863 (when (eq (org-element-type element
) 'keyword
)
1864 (let ((val (org-element-property :value element
)))
1865 (if (equal (org-element-property :key element
) "BIND")
1866 (push (read (format "(%s)" val
)) alist
)
1867 ;; Enter setup file.
1868 (let ((file (expand-file-name
1869 (org-remove-double-quotes val
))))
1870 (unless (member file files
)
1872 (let ((org-inhibit-startup t
)) (org-mode))
1873 (insert (org-file-contents file
'noerror
))
1875 (funcall collect-bind
1879 ;; Return value in appropriate order of appearance.
1880 (nreverse (funcall collect-bind nil nil
)))))
1883 ;;;; Tree Properties
1885 ;; Tree properties are information extracted from parse tree. They
1886 ;; are initialized at the beginning of the transcoding process by
1887 ;; `org-export-collect-tree-properties'.
1889 ;; Dedicated functions focus on computing the value of specific tree
1890 ;; properties during initialization. Thus,
1891 ;; `org-export--populate-ignore-list' lists elements and objects that
1892 ;; should be skipped during export, `org-export--get-min-level' gets
1893 ;; the minimal exportable level, used as a basis to compute relative
1894 ;; level for headlines. Eventually
1895 ;; `org-export--collect-headline-numbering' builds an alist between
1896 ;; headlines and their numbering.
1898 (defun org-export-collect-tree-properties (data info
)
1899 "Extract tree properties from parse tree.
1901 DATA is the parse tree from which information is retrieved. INFO
1902 is a list holding export options.
1904 Following tree properties are set or updated:
1906 `:exported-data' Hash table used to memoize results from
1909 `:footnote-definition-alist' List of footnotes definitions in
1910 original buffer and current parse tree.
1912 `:headline-offset' Offset between true level of headlines and
1913 local level. An offset of -1 means a headline
1914 of level 2 should be considered as a level
1915 1 headline in the context.
1917 `:headline-numbering' Alist of all headlines as key an the
1918 associated numbering as value.
1920 `:ignore-list' List of elements that should be ignored during
1923 Return updated plist."
1924 ;; Install the parse tree in the communication channel, in order to
1925 ;; use `org-export-get-genealogy' and al.
1926 (setq info
(plist-put info
:parse-tree data
))
1927 ;; Get the list of elements and objects to ignore, and put it into
1928 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1929 ;; been done during parse tree filtering.
1933 (append (org-export--populate-ignore-list data info
)
1934 (plist-get info
:ignore-list
))))
1935 ;; Compute `:headline-offset' in order to be able to use
1936 ;; `org-export-get-relative-level'.
1940 (- 1 (org-export--get-min-level data info
))))
1941 ;; Update footnotes definitions list with definitions in parse tree.
1942 ;; This is required since buffer expansion might have modified
1943 ;; boundaries of footnote definitions contained in the parse tree.
1944 ;; This way, definitions in `footnote-definition-alist' are bound to
1945 ;; match those in the parse tree.
1946 (let ((defs (plist-get info
:footnote-definition-alist
)))
1947 (org-element-map data
'footnote-definition
1949 (push (cons (org-element-property :label fn
)
1950 `(org-data nil
,@(org-element-contents fn
)))
1952 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1953 ;; Properties order doesn't matter: get the rest of the tree
1956 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1957 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1960 (defun org-export--get-min-level (data options
)
1961 "Return minimum exportable headline's level in DATA.
1962 DATA is parsed tree as returned by `org-element-parse-buffer'.
1963 OPTIONS is a plist holding export options."
1965 (let ((min-level 10000))
1968 (when (and (eq (org-element-type blob
) 'headline
)
1969 (not (org-element-property :footnote-section-p blob
))
1970 (not (memq blob
(plist-get options
:ignore-list
))))
1971 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1972 (when (= min-level
1) (throw 'exit
1)))
1973 (org-element-contents data
))
1974 ;; If no headline was found, for the sake of consistency, set
1975 ;; minimum level to 1 nonetheless.
1976 (if (= min-level
10000) 1 min-level
))))
1978 (defun org-export--collect-headline-numbering (data options
)
1979 "Return numbering of all exportable headlines in a parse tree.
1981 DATA is the parse tree. OPTIONS is the plist holding export
1984 Return an alist whose key is a headline and value is its
1985 associated numbering \(in the shape of a list of numbers\) or nil
1986 for a footnotes section."
1987 (let ((numbering (make-vector org-export-max-depth
0)))
1988 (org-element-map data
'headline
1990 (unless (org-element-property :footnote-section-p headline
)
1991 (let ((relative-level
1992 (1- (org-export-get-relative-level headline options
))))
1995 (loop for n across numbering
1996 for idx from
0 to org-export-max-depth
1997 when
(< idx relative-level
) collect n
1998 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1999 when
(> idx relative-level
) do
(aset numbering idx
0))))))
2002 (defun org-export--populate-ignore-list (data options
)
2003 "Return list of elements and objects to ignore during export.
2004 DATA is the parse tree to traverse. OPTIONS is the plist holding
2007 ;; First find trees containing a select tag, if any.
2008 (selected (org-export--selected-trees data options
))
2009 ;; If a select tag is active, also ignore the section before
2010 ;; the first headline, if any.
2011 (ignore (and selected
2012 (let ((first-element (car (org-element-contents data
))))
2013 (and (eq (org-element-type first-element
) 'section
)
2017 ;; Collect ignored elements or objects into IGNORE-LIST.
2018 (let ((type (org-element-type data
)))
2019 (if (org-export--skip-p data options selected
) (push data ignore
)
2020 (if (and (eq type
'headline
)
2021 (eq (plist-get options
:with-archived-trees
) 'headline
)
2022 (org-element-property :archivedp data
))
2023 ;; If headline is archived but tree below has
2024 ;; to be skipped, add it to ignore list.
2025 (dolist (element (org-element-contents data
))
2026 (push element ignore
))
2027 ;; Move into secondary string, if any.
2029 (cdr (assq type org-element-secondary-value-alist
))))
2031 (mapc walk-data
(org-element-property sec-prop data
))))
2032 ;; Move into recursive objects/elements.
2033 (mapc walk-data
(org-element-contents data
))))))))
2035 (funcall walk-data data
)
2039 (defun org-export--selected-trees (data info
)
2040 "Return list of headlines and inlinetasks with a select tag in their tree.
2041 DATA is parsed data as returned by `org-element-parse-buffer'.
2042 INFO is a plist holding export options."
2043 (let* (selected-trees
2044 walk-data
; For byte-compiler.
2047 (lambda (data genealogy
)
2048 (let ((type (org-element-type data
)))
2050 ((memq type
'(headline inlinetask
))
2051 (let ((tags (org-element-property :tags data
)))
2052 (if (loop for tag in
(plist-get info
:select-tags
)
2053 thereis
(member tag tags
))
2054 ;; When a select tag is found, mark full
2055 ;; genealogy and every headline within the tree
2057 (setq selected-trees
2060 (org-element-map data
'(headline inlinetask
)
2063 ;; If at a headline, continue searching in tree,
2065 (when (eq type
'headline
)
2067 (funcall walk-data el
(cons data genealogy
)))
2068 (org-element-contents data
))))))
2069 ((or (eq type
'org-data
)
2070 (memq type org-element-greater-elements
))
2071 (mapc (lambda (el) (funcall walk-data el genealogy
))
2072 (org-element-contents data
)))))))))
2073 (funcall walk-data data nil
)
2076 (defun org-export--skip-p (blob options selected
)
2077 "Non-nil when element or object BLOB should be skipped during export.
2078 OPTIONS is the plist holding export options. SELECTED, when
2079 non-nil, is a list of headlines or inlinetasks belonging to
2080 a tree with a select tag."
2081 (case (org-element-type blob
)
2082 (clock (not (plist-get options
:with-clocks
)))
2084 (let ((with-drawers-p (plist-get options
:with-drawers
)))
2085 (or (not with-drawers-p
)
2086 (and (consp with-drawers-p
)
2087 ;; If `:with-drawers' value starts with `not', ignore
2088 ;; every drawer whose name belong to that list.
2089 ;; Otherwise, ignore drawers whose name isn't in that
2091 (let ((name (org-element-property :drawer-name blob
)))
2092 (if (eq (car with-drawers-p
) 'not
)
2093 (member-ignore-case name
(cdr with-drawers-p
))
2094 (not (member-ignore-case name with-drawers-p
))))))))
2095 (fixed-width (not (plist-get options
:with-fixed-width
)))
2096 ((footnote-definition footnote-reference
)
2097 (not (plist-get options
:with-footnotes
)))
2098 ((headline inlinetask
)
2099 (let ((with-tasks (plist-get options
:with-tasks
))
2100 (todo (org-element-property :todo-keyword blob
))
2101 (todo-type (org-element-property :todo-type blob
))
2102 (archived (plist-get options
:with-archived-trees
))
2103 (tags (org-element-property :tags blob
)))
2105 (and (eq (org-element-type blob
) 'inlinetask
)
2106 (not (plist-get options
:with-inlinetasks
)))
2107 ;; Ignore subtrees with an exclude tag.
2108 (loop for k in
(plist-get options
:exclude-tags
)
2109 thereis
(member k tags
))
2110 ;; When a select tag is present in the buffer, ignore any tree
2112 (and selected
(not (memq blob selected
)))
2113 ;; Ignore commented sub-trees.
2114 (org-element-property :commentedp blob
)
2115 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
2116 (and (not archived
) (org-element-property :archivedp blob
))
2117 ;; Ignore tasks, if specified by `:with-tasks' property.
2119 (or (not with-tasks
)
2120 (and (memq with-tasks
'(todo done
))
2121 (not (eq todo-type with-tasks
)))
2122 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
2123 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
2125 (let ((properties-set (plist-get options
:with-properties
)))
2126 (cond ((null properties-set
) t
)
2127 ((consp properties-set
)
2128 (not (member-ignore-case (org-element-property :key blob
)
2129 properties-set
))))))
2130 (planning (not (plist-get options
:with-planning
)))
2131 (property-drawer (not (plist-get options
:with-properties
)))
2132 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
2133 (table (not (plist-get options
:with-tables
)))
2135 (and (org-export-table-has-special-column-p
2136 (org-export-get-parent-table blob
))
2137 (not (org-export-get-previous-element blob options
))))
2138 (table-row (org-export-table-row-is-special-p blob options
))
2140 ;; `:with-timestamps' only applies to isolated timestamps
2141 ;; objects, i.e. timestamp objects in a paragraph containing only
2142 ;; timestamps and whitespaces.
2143 (when (let ((parent (org-export-get-parent-element blob
)))
2144 (and (memq (org-element-type parent
) '(paragraph verse-block
))
2145 (not (org-element-map parent
2147 (remq 'timestamp org-element-all-objects
))
2149 (or (not (stringp obj
)) (org-string-nw-p obj
)))
2151 (case (plist-get options
:with-timestamps
)
2154 (not (memq (org-element-property :type blob
) '(active active-range
))))
2156 (not (memq (org-element-property :type blob
)
2157 '(inactive inactive-range
)))))))))
2162 ;; `org-export-data' reads a parse tree (obtained with, i.e.
2163 ;; `org-element-parse-buffer') and transcodes it into a specified
2164 ;; back-end output. It takes care of filtering out elements or
2165 ;; objects according to export options and organizing the output blank
2166 ;; lines and white space are preserved. The function memoizes its
2167 ;; results, so it is cheap to call it within transcoders.
2169 ;; It is possible to modify locally the back-end used by
2170 ;; `org-export-data' or even use a temporary back-end by using
2171 ;; `org-export-data-with-backend'.
2173 ;; Internally, three functions handle the filtering of objects and
2174 ;; elements during the export. In particular,
2175 ;; `org-export-ignore-element' marks an element or object so future
2176 ;; parse tree traversals skip it and `org-export-expand' transforms
2177 ;; the others back into their original shape.
2179 ;; `org-export-transcoder' is an accessor returning appropriate
2180 ;; translator function for a given element or object.
2182 (defun org-export-transcoder (blob info
)
2183 "Return appropriate transcoder for BLOB.
2184 INFO is a plist containing export directives."
2185 (let ((type (org-element-type blob
)))
2186 ;; Return contents only for complete parse trees.
2187 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
2188 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
2189 (and (functionp transcoder
) transcoder
)))))
2191 (defun org-export-data (data info
)
2192 "Convert DATA into current back-end format.
2194 DATA is a parse tree, an element or an object or a secondary
2195 string. INFO is a plist holding export options.
2198 (or (gethash data
(plist-get info
:exported-data
))
2199 (let* ((type (org-element-type data
))
2202 ;; Ignored element/object.
2203 ((memq data
(plist-get info
:ignore-list
)) nil
)
2205 ((eq type
'plain-text
)
2206 (org-export-filter-apply-functions
2207 (plist-get info
:filter-plain-text
)
2208 (let ((transcoder (org-export-transcoder data info
)))
2209 (if transcoder
(funcall transcoder data info
) data
))
2211 ;; Secondary string.
2213 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
2214 ;; Element/Object without contents or, as a special
2215 ;; case, headline with archive tag and archived trees
2216 ;; restricted to title only.
2217 ((or (not (org-element-contents data
))
2218 (and (eq type
'headline
)
2219 (eq (plist-get info
:with-archived-trees
) 'headline
)
2220 (org-element-property :archivedp data
)))
2221 (let ((transcoder (org-export-transcoder data info
)))
2222 (or (and (functionp transcoder
)
2223 (funcall transcoder data nil info
))
2224 ;; Export snippets never return a nil value so
2225 ;; that white spaces following them are never
2227 (and (eq type
'export-snippet
) ""))))
2228 ;; Element/Object with contents.
2230 (let ((transcoder (org-export-transcoder data info
)))
2232 (let* ((greaterp (memq type org-element-greater-elements
))
2235 (memq type org-element-recursive-objects
)))
2238 (lambda (element) (org-export-data element info
))
2239 (org-element-contents
2240 (if (or greaterp objectp
) data
2241 ;; Elements directly containing
2242 ;; objects must have their indentation
2243 ;; normalized first.
2244 (org-element-normalize-contents
2246 ;; When normalizing contents of the
2247 ;; first paragraph in an item or
2248 ;; a footnote definition, ignore
2249 ;; first line's indentation: there is
2250 ;; none and it might be misleading.
2251 (when (eq type
'paragraph
)
2252 (let ((parent (org-export-get-parent data
)))
2254 (eq (car (org-element-contents parent
))
2256 (memq (org-element-type parent
)
2257 '(footnote-definition item
))))))))
2259 (funcall transcoder data
2260 (if (not greaterp
) contents
2261 (org-element-normalize-string contents
))
2263 ;; Final result will be memoized before being returned.
2268 ((memq type
'(org-data plain-text nil
)) results
)
2269 ;; Append the same white space between elements or objects
2270 ;; as in the original buffer, and call appropriate filters.
2273 (org-export-filter-apply-functions
2274 (plist-get info
(intern (format ":filter-%s" type
)))
2275 (let ((post-blank (or (org-element-property :post-blank data
)
2277 (if (memq type org-element-all-elements
)
2278 (concat (org-element-normalize-string results
)
2279 (make-string post-blank ?
\n))
2280 (concat results
(make-string post-blank ?\s
))))
2283 (plist-get info
:exported-data
)))))
2285 (defun org-export-data-with-backend (data backend info
)
2286 "Convert DATA into BACKEND format.
2288 DATA is an element, an object, a secondary string or a string.
2289 BACKEND is a symbol. INFO is a plist used as a communication
2292 Unlike to `org-export-with-backend', this function will
2293 recursively convert DATA using BACKEND translation table."
2294 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2297 ;; Set-up a new communication channel with translations defined in
2298 ;; BACKEND as the translate table and a new hash table for
2302 (list :back-end backend
2303 :translate-alist
(org-export-get-all-transcoders backend
)
2304 ;; Size of the hash table is reduced since this function
2305 ;; will probably be used on small trees.
2306 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2308 (defun org-export-remove-uninterpreted-data (data info
)
2309 "Change uninterpreted elements back into Org syntax.
2310 DATA is the parse tree. INFO is a plist containing export
2311 options. Each uninterpreted element or object is changed back
2312 into a string. Contents, if any, are not modified. The parse
2313 tree is modified by side effect and returned by the function."
2314 (org-export--remove-uninterpreted-data-1 data info
)
2315 (dolist (prop '(:author
:date
:title
))
2318 (org-export--remove-uninterpreted-data-1
2319 (plist-get info prop
)
2322 (defun org-export--remove-uninterpreted-data-1 (data info
)
2323 "Change uninterpreted elements back into Org syntax.
2324 DATA is a parse tree or a secondary string. INFO is a plist
2325 containing export options. It is modified by side effect and
2326 returned by the function."
2327 (org-element-map data
2328 '(entity bold italic latex-environment latex-fragment strike-through
2329 subscript superscript underline
)
2332 (case (org-element-type blob
)
2335 (and (not (plist-get info
:with-entities
))
2337 (org-export-expand blob nil
)
2339 (or (org-element-property :post-blank blob
) 0)
2342 ((bold italic strike-through underline
)
2343 (and (not (plist-get info
:with-emphasize
))
2344 (let ((marker (case (org-element-type blob
)
2347 (strike-through "+")
2351 (org-element-contents blob
)
2355 (or (org-element-property :post-blank blob
)
2358 ;; ... LaTeX environments and fragments...
2359 ((latex-environment latex-fragment
)
2360 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2361 (list (org-export-expand blob nil
))))
2362 ;; ... sub/superscripts...
2363 ((subscript superscript
)
2364 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2365 (bracketp (org-element-property :use-brackets-p blob
)))
2366 (and (or (not sub
/super-p
)
2367 (and (eq sub
/super-p
'{}) (not bracketp
)))
2370 (if (eq (org-element-type blob
) 'subscript
)
2373 (and bracketp
"{")))
2374 (org-element-contents blob
)
2377 (and (org-element-property :post-blank blob
)
2379 (org-element-property :post-blank blob
)
2382 ;; Splice NEW at BLOB location in parse tree.
2383 (dolist (e new
(org-element-extract-element blob
))
2384 (unless (string= e
"") (org-element-insert-before e blob
))))))
2386 ;; Return modified parse tree.
2389 (defun org-export-expand (blob contents
&optional with-affiliated
)
2390 "Expand a parsed element or object to its original state.
2392 BLOB is either an element or an object. CONTENTS is its
2393 contents, as a string or nil.
2395 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2396 keywords before output."
2397 (let ((type (org-element-type blob
)))
2398 (concat (and with-affiliated
(memq type org-element-all-elements
)
2399 (org-element--interpret-affiliated-keywords blob
))
2400 (funcall (intern (format "org-element-%s-interpreter" type
))
2403 (defun org-export-ignore-element (element info
)
2404 "Add ELEMENT to `:ignore-list' in INFO.
2406 Any element in `:ignore-list' will be skipped when using
2407 `org-element-map'. INFO is modified by side effects."
2408 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2412 ;;; The Filter System
2414 ;; Filters allow end-users to tweak easily the transcoded output.
2415 ;; They are the functional counterpart of hooks, as every filter in
2416 ;; a set is applied to the return value of the previous one.
2418 ;; Every set is back-end agnostic. Although, a filter is always
2419 ;; called, in addition to the string it applies to, with the back-end
2420 ;; used as argument, so it's easy for the end-user to add back-end
2421 ;; specific filters in the set. The communication channel, as
2422 ;; a plist, is required as the third argument.
2424 ;; From the developer side, filters sets can be installed in the
2425 ;; process with the help of `org-export-define-backend', which
2426 ;; internally stores filters as an alist. Each association has a key
2427 ;; among the following symbols and a function or a list of functions
2430 ;; - `:filter-options' applies to the property list containing export
2431 ;; options. Unlike to other filters, functions in this list accept
2432 ;; two arguments instead of three: the property list containing
2433 ;; export options and the back-end. Users can set its value through
2434 ;; `org-export-filter-options-functions' variable.
2436 ;; - `:filter-parse-tree' applies directly to the complete parsed
2437 ;; tree. Users can set it through
2438 ;; `org-export-filter-parse-tree-functions' variable.
2440 ;; - `:filter-final-output' applies to the final transcoded string.
2441 ;; Users can set it with `org-export-filter-final-output-functions'
2444 ;; - `:filter-plain-text' applies to any string not recognized as Org
2445 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2448 ;; - `:filter-TYPE' applies on the string returned after an element or
2449 ;; object of type TYPE has been transcoded. A user can modify
2450 ;; `org-export-filter-TYPE-functions'
2452 ;; All filters sets are applied with
2453 ;; `org-export-filter-apply-functions' function. Filters in a set are
2454 ;; applied in a LIFO fashion. It allows developers to be sure that
2455 ;; their filters will be applied first.
2457 ;; Filters properties are installed in communication channel with
2458 ;; `org-export-install-filters' function.
2460 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2461 ;; `org-export-before-parsing-hook') are run at the beginning of the
2462 ;; export process and just before parsing to allow for heavy structure
2468 (defvar org-export-before-processing-hook nil
2469 "Hook run at the beginning of the export process.
2471 This is run before include keywords and macros are expanded and
2472 Babel code blocks executed, on a copy of the original buffer
2473 being exported. Visibility and narrowing are preserved. Point
2474 is at the beginning of the buffer.
2476 Every function in this hook will be called with one argument: the
2477 back-end currently used, as a symbol.")
2479 (defvar org-export-before-parsing-hook nil
2480 "Hook run before parsing an export buffer.
2482 This is run after include keywords and macros have been expanded
2483 and Babel code blocks executed, on a copy of the original buffer
2484 being exported. Visibility and narrowing are preserved. Point
2485 is at the beginning of the buffer.
2487 Every function in this hook will be called with one argument: the
2488 back-end currently used, as a symbol.")
2491 ;;;; Special Filters
2493 (defvar org-export-filter-options-functions nil
2494 "List of functions applied to the export options.
2495 Each filter is called with two arguments: the export options, as
2496 a plist, and the back-end, as a symbol. It must return
2497 a property list containing export options.")
2499 (defvar org-export-filter-parse-tree-functions nil
2500 "List of functions applied to the parsed tree.
2501 Each filter is called with three arguments: the parse tree, as
2502 returned by `org-element-parse-buffer', the back-end, as
2503 a symbol, and the communication channel, as a plist. It must
2504 return the modified parse tree to transcode.")
2506 (defvar org-export-filter-plain-text-functions nil
2507 "List of functions applied to plain text.
2508 Each filter is called with three arguments: a string which
2509 contains no Org syntax, the back-end, as a symbol, and the
2510 communication channel, as a plist. It must return a string or
2513 (defvar org-export-filter-final-output-functions nil
2514 "List of functions applied to the transcoded string.
2515 Each filter is called with three arguments: the full transcoded
2516 string, the back-end, as a symbol, and the communication channel,
2517 as a plist. It must return a string that will be used as the
2518 final export output.")
2521 ;;;; Elements Filters
2523 (defvar org-export-filter-babel-call-functions nil
2524 "List of functions applied to a transcoded babel-call.
2525 Each filter is called with three arguments: the transcoded data,
2526 as a string, the back-end, as a symbol, and the communication
2527 channel, as a plist. It must return a string or nil.")
2529 (defvar org-export-filter-center-block-functions nil
2530 "List of functions applied to a transcoded center block.
2531 Each filter is called with three arguments: the transcoded data,
2532 as a string, the back-end, as a symbol, and the communication
2533 channel, as a plist. It must return a string or nil.")
2535 (defvar org-export-filter-clock-functions nil
2536 "List of functions applied to a transcoded clock.
2537 Each filter is called with three arguments: the transcoded data,
2538 as a string, the back-end, as a symbol, and the communication
2539 channel, as a plist. It must return a string or nil.")
2541 (defvar org-export-filter-comment-functions nil
2542 "List of functions applied to a transcoded comment.
2543 Each filter is called with three arguments: the transcoded data,
2544 as a string, the back-end, as a symbol, and the communication
2545 channel, as a plist. It must return a string or nil.")
2547 (defvar org-export-filter-comment-block-functions nil
2548 "List of functions applied to a transcoded comment-block.
2549 Each filter is called with three arguments: the transcoded data,
2550 as a string, the back-end, as a symbol, and the communication
2551 channel, as a plist. It must return a string or nil.")
2553 (defvar org-export-filter-diary-sexp-functions nil
2554 "List of functions applied to a transcoded diary-sexp.
2555 Each filter is called with three arguments: the transcoded data,
2556 as a string, the back-end, as a symbol, and the communication
2557 channel, as a plist. It must return a string or nil.")
2559 (defvar org-export-filter-drawer-functions nil
2560 "List of functions applied to a transcoded drawer.
2561 Each filter is called with three arguments: the transcoded data,
2562 as a string, the back-end, as a symbol, and the communication
2563 channel, as a plist. It must return a string or nil.")
2565 (defvar org-export-filter-dynamic-block-functions nil
2566 "List of functions applied to a transcoded dynamic-block.
2567 Each filter is called with three arguments: the transcoded data,
2568 as a string, the back-end, as a symbol, and the communication
2569 channel, as a plist. It must return a string or nil.")
2571 (defvar org-export-filter-example-block-functions nil
2572 "List of functions applied to a transcoded example-block.
2573 Each filter is called with three arguments: the transcoded data,
2574 as a string, the back-end, as a symbol, and the communication
2575 channel, as a plist. It must return a string or nil.")
2577 (defvar org-export-filter-export-block-functions nil
2578 "List of functions applied to a transcoded export-block.
2579 Each filter is called with three arguments: the transcoded data,
2580 as a string, the back-end, as a symbol, and the communication
2581 channel, as a plist. It must return a string or nil.")
2583 (defvar org-export-filter-fixed-width-functions nil
2584 "List of functions applied to a transcoded fixed-width.
2585 Each filter is called with three arguments: the transcoded data,
2586 as a string, the back-end, as a symbol, and the communication
2587 channel, as a plist. It must return a string or nil.")
2589 (defvar org-export-filter-footnote-definition-functions nil
2590 "List of functions applied to a transcoded footnote-definition.
2591 Each filter is called with three arguments: the transcoded data,
2592 as a string, the back-end, as a symbol, and the communication
2593 channel, as a plist. It must return a string or nil.")
2595 (defvar org-export-filter-headline-functions nil
2596 "List of functions applied to a transcoded headline.
2597 Each filter is called with three arguments: the transcoded data,
2598 as a string, the back-end, as a symbol, and the communication
2599 channel, as a plist. It must return a string or nil.")
2601 (defvar org-export-filter-horizontal-rule-functions nil
2602 "List of functions applied to a transcoded horizontal-rule.
2603 Each filter is called with three arguments: the transcoded data,
2604 as a string, the back-end, as a symbol, and the communication
2605 channel, as a plist. It must return a string or nil.")
2607 (defvar org-export-filter-inlinetask-functions nil
2608 "List of functions applied to a transcoded inlinetask.
2609 Each filter is called with three arguments: the transcoded data,
2610 as a string, the back-end, as a symbol, and the communication
2611 channel, as a plist. It must return a string or nil.")
2613 (defvar org-export-filter-item-functions nil
2614 "List of functions applied to a transcoded item.
2615 Each filter is called with three arguments: the transcoded data,
2616 as a string, the back-end, as a symbol, and the communication
2617 channel, as a plist. It must return a string or nil.")
2619 (defvar org-export-filter-keyword-functions nil
2620 "List of functions applied to a transcoded keyword.
2621 Each filter is called with three arguments: the transcoded data,
2622 as a string, the back-end, as a symbol, and the communication
2623 channel, as a plist. It must return a string or nil.")
2625 (defvar org-export-filter-latex-environment-functions nil
2626 "List of functions applied to a transcoded latex-environment.
2627 Each filter is called with three arguments: the transcoded data,
2628 as a string, the back-end, as a symbol, and the communication
2629 channel, as a plist. It must return a string or nil.")
2631 (defvar org-export-filter-node-property-functions nil
2632 "List of functions applied to a transcoded node-property.
2633 Each filter is called with three arguments: the transcoded data,
2634 as a string, the back-end, as a symbol, and the communication
2635 channel, as a plist. It must return a string or nil.")
2637 (defvar org-export-filter-paragraph-functions nil
2638 "List of functions applied to a transcoded paragraph.
2639 Each filter is called with three arguments: the transcoded data,
2640 as a string, the back-end, as a symbol, and the communication
2641 channel, as a plist. It must return a string or nil.")
2643 (defvar org-export-filter-plain-list-functions nil
2644 "List of functions applied to a transcoded plain-list.
2645 Each filter is called with three arguments: the transcoded data,
2646 as a string, the back-end, as a symbol, and the communication
2647 channel, as a plist. It must return a string or nil.")
2649 (defvar org-export-filter-planning-functions nil
2650 "List of functions applied to a transcoded planning.
2651 Each filter is called with three arguments: the transcoded data,
2652 as a string, the back-end, as a symbol, and the communication
2653 channel, as a plist. It must return a string or nil.")
2655 (defvar org-export-filter-property-drawer-functions nil
2656 "List of functions applied to a transcoded property-drawer.
2657 Each filter is called with three arguments: the transcoded data,
2658 as a string, the back-end, as a symbol, and the communication
2659 channel, as a plist. It must return a string or nil.")
2661 (defvar org-export-filter-quote-block-functions nil
2662 "List of functions applied to a transcoded quote block.
2663 Each filter is called with three arguments: the transcoded quote
2664 data, as a string, the back-end, as a symbol, and the
2665 communication channel, as a plist. It must return a string or
2668 (defvar org-export-filter-section-functions nil
2669 "List of functions applied to a transcoded section.
2670 Each filter is called with three arguments: the transcoded data,
2671 as a string, the back-end, as a symbol, and the communication
2672 channel, as a plist. It must return a string or nil.")
2674 (defvar org-export-filter-special-block-functions nil
2675 "List of functions applied to a transcoded special block.
2676 Each filter is called with three arguments: the transcoded data,
2677 as a string, the back-end, as a symbol, and the communication
2678 channel, as a plist. It must return a string or nil.")
2680 (defvar org-export-filter-src-block-functions nil
2681 "List of functions applied to a transcoded src-block.
2682 Each filter is called with three arguments: the transcoded data,
2683 as a string, the back-end, as a symbol, and the communication
2684 channel, as a plist. It must return a string or nil.")
2686 (defvar org-export-filter-table-functions nil
2687 "List of functions applied to a transcoded table.
2688 Each filter is called with three arguments: the transcoded data,
2689 as a string, the back-end, as a symbol, and the communication
2690 channel, as a plist. It must return a string or nil.")
2692 (defvar org-export-filter-table-cell-functions nil
2693 "List of functions applied to a transcoded table-cell.
2694 Each filter is called with three arguments: the transcoded data,
2695 as a string, the back-end, as a symbol, and the communication
2696 channel, as a plist. It must return a string or nil.")
2698 (defvar org-export-filter-table-row-functions nil
2699 "List of functions applied to a transcoded table-row.
2700 Each filter is called with three arguments: the transcoded data,
2701 as a string, the back-end, as a symbol, and the communication
2702 channel, as a plist. It must return a string or nil.")
2704 (defvar org-export-filter-verse-block-functions nil
2705 "List of functions applied to a transcoded verse block.
2706 Each filter is called with three arguments: the transcoded data,
2707 as a string, the back-end, as a symbol, and the communication
2708 channel, as a plist. It must return a string or nil.")
2711 ;;;; Objects Filters
2713 (defvar org-export-filter-bold-functions nil
2714 "List of functions applied to transcoded bold text.
2715 Each filter is called with three arguments: the transcoded data,
2716 as a string, the back-end, as a symbol, and the communication
2717 channel, as a plist. It must return a string or nil.")
2719 (defvar org-export-filter-code-functions nil
2720 "List of functions applied to transcoded code text.
2721 Each filter is called with three arguments: the transcoded data,
2722 as a string, the back-end, as a symbol, and the communication
2723 channel, as a plist. It must return a string or nil.")
2725 (defvar org-export-filter-entity-functions nil
2726 "List of functions applied to a transcoded entity.
2727 Each filter is called with three arguments: the transcoded data,
2728 as a string, the back-end, as a symbol, and the communication
2729 channel, as a plist. It must return a string or nil.")
2731 (defvar org-export-filter-export-snippet-functions nil
2732 "List of functions applied to a transcoded export-snippet.
2733 Each filter is called with three arguments: the transcoded data,
2734 as a string, the back-end, as a symbol, and the communication
2735 channel, as a plist. It must return a string or nil.")
2737 (defvar org-export-filter-footnote-reference-functions nil
2738 "List of functions applied to a transcoded footnote-reference.
2739 Each filter is called with three arguments: the transcoded data,
2740 as a string, the back-end, as a symbol, and the communication
2741 channel, as a plist. It must return a string or nil.")
2743 (defvar org-export-filter-inline-babel-call-functions nil
2744 "List of functions applied to a transcoded inline-babel-call.
2745 Each filter is called with three arguments: the transcoded data,
2746 as a string, the back-end, as a symbol, and the communication
2747 channel, as a plist. It must return a string or nil.")
2749 (defvar org-export-filter-inline-src-block-functions nil
2750 "List of functions applied to a transcoded inline-src-block.
2751 Each filter is called with three arguments: the transcoded data,
2752 as a string, the back-end, as a symbol, and the communication
2753 channel, as a plist. It must return a string or nil.")
2755 (defvar org-export-filter-italic-functions nil
2756 "List of functions applied to transcoded italic text.
2757 Each filter is called with three arguments: the transcoded data,
2758 as a string, the back-end, as a symbol, and the communication
2759 channel, as a plist. It must return a string or nil.")
2761 (defvar org-export-filter-latex-fragment-functions nil
2762 "List of functions applied to a transcoded latex-fragment.
2763 Each filter is called with three arguments: the transcoded data,
2764 as a string, the back-end, as a symbol, and the communication
2765 channel, as a plist. It must return a string or nil.")
2767 (defvar org-export-filter-line-break-functions nil
2768 "List of functions applied to a transcoded line-break.
2769 Each filter is called with three arguments: the transcoded data,
2770 as a string, the back-end, as a symbol, and the communication
2771 channel, as a plist. It must return a string or nil.")
2773 (defvar org-export-filter-link-functions nil
2774 "List of functions applied to a transcoded link.
2775 Each filter is called with three arguments: the transcoded data,
2776 as a string, the back-end, as a symbol, and the communication
2777 channel, as a plist. It must return a string or nil.")
2779 (defvar org-export-filter-radio-target-functions nil
2780 "List of functions applied to a transcoded radio-target.
2781 Each filter is called with three arguments: the transcoded data,
2782 as a string, the back-end, as a symbol, and the communication
2783 channel, as a plist. It must return a string or nil.")
2785 (defvar org-export-filter-statistics-cookie-functions nil
2786 "List of functions applied to a transcoded statistics-cookie.
2787 Each filter is called with three arguments: the transcoded data,
2788 as a string, the back-end, as a symbol, and the communication
2789 channel, as a plist. It must return a string or nil.")
2791 (defvar org-export-filter-strike-through-functions nil
2792 "List of functions applied to transcoded strike-through text.
2793 Each filter is called with three arguments: the transcoded data,
2794 as a string, the back-end, as a symbol, and the communication
2795 channel, as a plist. It must return a string or nil.")
2797 (defvar org-export-filter-subscript-functions nil
2798 "List of functions applied to a transcoded subscript.
2799 Each filter is called with three arguments: the transcoded data,
2800 as a string, the back-end, as a symbol, and the communication
2801 channel, as a plist. It must return a string or nil.")
2803 (defvar org-export-filter-superscript-functions nil
2804 "List of functions applied to a transcoded superscript.
2805 Each filter is called with three arguments: the transcoded data,
2806 as a string, the back-end, as a symbol, and the communication
2807 channel, as a plist. It must return a string or nil.")
2809 (defvar org-export-filter-target-functions nil
2810 "List of functions applied to a transcoded target.
2811 Each filter is called with three arguments: the transcoded data,
2812 as a string, the back-end, as a symbol, and the communication
2813 channel, as a plist. It must return a string or nil.")
2815 (defvar org-export-filter-timestamp-functions nil
2816 "List of functions applied to a transcoded timestamp.
2817 Each filter is called with three arguments: the transcoded data,
2818 as a string, the back-end, as a symbol, and the communication
2819 channel, as a plist. It must return a string or nil.")
2821 (defvar org-export-filter-underline-functions nil
2822 "List of functions applied to transcoded underline text.
2823 Each filter is called with three arguments: the transcoded data,
2824 as a string, the back-end, as a symbol, and the communication
2825 channel, as a plist. It must return a string or nil.")
2827 (defvar org-export-filter-verbatim-functions nil
2828 "List of functions applied to transcoded verbatim text.
2829 Each filter is called with three arguments: the transcoded data,
2830 as a string, the back-end, as a symbol, and the communication
2831 channel, as a plist. It must return a string or nil.")
2836 ;; Internal function `org-export-install-filters' installs filters
2837 ;; hard-coded in back-ends (developer filters) and filters from global
2838 ;; variables (user filters) in the communication channel.
2840 ;; Internal function `org-export-filter-apply-functions' takes care
2841 ;; about applying each filter in order to a given data. It ignores
2842 ;; filters returning a nil value but stops whenever a filter returns
2845 (defun org-export-filter-apply-functions (filters value info
)
2846 "Call every function in FILTERS.
2848 Functions are called with arguments VALUE, current export
2849 back-end's name and INFO. A function returning a nil value will
2850 be skipped. If it returns the empty string, the process ends and
2853 Call is done in a LIFO fashion, to be sure that developer
2854 specified filters, if any, are called first."
2856 (let* ((backend (plist-get info
:back-end
))
2857 (backend-name (and backend
(org-export-backend-name backend
))))
2858 (dolist (filter filters value
)
2859 (let ((result (funcall filter value backend-name info
)))
2860 (cond ((not result
) value
)
2861 ((equal value
"") (throw 'exit nil
))
2862 (t (setq value result
))))))))
2864 (defun org-export-install-filters (info)
2865 "Install filters properties in communication channel.
2866 INFO is a plist containing the current communication channel.
2867 Return the updated communication channel."
2869 ;; Install user-defined filters with `org-export-filters-alist'
2870 ;; and filters already in INFO (through ext-plist mechanism).
2872 (let* ((prop (car p
))
2873 (info-value (plist-get info prop
))
2874 (default-value (symbol-value (cdr p
))))
2876 (plist-put plist prop
2877 ;; Filters in INFO will be called
2878 ;; before those user provided.
2879 (append (if (listp info-value
) info-value
2882 org-export-filters-alist
)
2883 ;; Prepend back-end specific filters to that list.
2885 ;; Single values get consed, lists are appended.
2886 (let ((key (car p
)) (value (cdr p
)))
2891 (if (atom value
) (cons value
(plist-get plist key
))
2892 (append value
(plist-get plist key
))))))))
2893 (org-export-get-all-filters (plist-get info
:back-end
)))
2894 ;; Return new communication channel.
2895 (org-combine-plists info plist
)))
2901 ;; This is the room for the main function, `org-export-as', along with
2902 ;; its derivative, `org-export-string-as'.
2903 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2904 ;; function should be added to kill ring.
2906 ;; Note that `org-export-as' doesn't really parse the current buffer,
2907 ;; but a copy of it (with the same buffer-local variables and
2908 ;; visibility), where macros and include keywords are expanded and
2909 ;; Babel blocks are executed, if appropriate.
2910 ;; `org-export-with-buffer-copy' macro prepares that copy.
2912 ;; File inclusion is taken care of by
2913 ;; `org-export-expand-include-keyword' and
2914 ;; `org-export--prepare-file-contents'. Structure wise, including
2915 ;; a whole Org file in a buffer often makes little sense. For
2916 ;; example, if the file contains a headline and the include keyword
2917 ;; was within an item, the item should contain the headline. That's
2918 ;; why file inclusion should be done before any structure can be
2919 ;; associated to the file, that is before parsing.
2921 ;; `org-export-insert-default-template' is a command to insert
2922 ;; a default template (or a back-end specific template) at point or in
2925 (defun org-export-copy-buffer ()
2926 "Return a copy of the current buffer.
2927 The copy preserves Org buffer-local variables, visibility and
2929 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2930 (new-buf (generate-new-buffer (buffer-name))))
2931 (with-current-buffer new-buf
2932 (funcall copy-buffer-fun
)
2933 (set-buffer-modified-p nil
))
2936 (defmacro org-export-with-buffer-copy
(&rest body
)
2937 "Apply BODY in a copy of the current buffer.
2938 The copy preserves local variables, visibility and contents of
2939 the original buffer. Point is at the beginning of the buffer
2940 when BODY is applied."
2942 (org-with-gensyms (buf-copy)
2943 `(let ((,buf-copy
(org-export-copy-buffer)))
2945 (with-current-buffer ,buf-copy
2946 (goto-char (point-min))
2948 (and (buffer-live-p ,buf-copy
)
2949 ;; Kill copy without confirmation.
2950 (progn (with-current-buffer ,buf-copy
2951 (restore-buffer-modified-p nil
))
2952 (kill-buffer ,buf-copy
)))))))
2954 (defun org-export--generate-copy-script (buffer)
2955 "Generate a function duplicating BUFFER.
2957 The copy will preserve local variables, visibility, contents and
2958 narrowing of the original buffer. If a region was active in
2959 BUFFER, contents will be narrowed to that region instead.
2961 The resulting function can be evaluated at a later time, from
2962 another buffer, effectively cloning the original buffer there.
2964 The function assumes BUFFER's major mode is `org-mode'."
2965 (with-current-buffer buffer
2967 (let ((inhibit-modification-hooks t
))
2968 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2969 ;; already in BUFFER.
2970 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2971 ;; Copy specific buffer local variables and variables set
2972 ;; through BIND keywords.
2973 ,@(let ((bound-variables (org-export--list-bound-variables))
2975 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2977 (let ((var (car entry
))
2979 (and (not (memq var org-export-ignored-local-variables
))
2983 buffer-file-coding-system
))
2984 (assq var bound-variables
)
2985 (string-match "^\\(org-\\|orgtbl-\\)"
2987 ;; Skip unreadable values, as they cannot be
2988 ;; sent to external process.
2989 (or (not val
) (ignore-errors (read (format "%S" val
))))
2990 (push `(set (make-local-variable (quote ,var
))
2993 ;; Whole buffer contents.
2995 ,(org-with-wide-buffer
2996 (buffer-substring-no-properties
2997 (point-min) (point-max))))
2999 ,(if (org-region-active-p)
3000 `(narrow-to-region ,(region-beginning) ,(region-end))
3001 `(narrow-to-region ,(point-min) ,(point-max)))
3002 ;; Current position of point.
3003 (goto-char ,(point))
3004 ;; Overlays with invisible property.
3008 (let ((invis-prop (overlay-get ov
'invisible
)))
3011 (make-overlay ,(overlay-start ov
)
3013 'invisible
(quote ,invis-prop
))
3015 (overlays-in (point-min) (point-max)))
3019 (defun org-export-as
3020 (backend &optional subtreep visible-only body-only ext-plist
)
3021 "Transcode current Org buffer into BACKEND code.
3023 BACKEND is either an export back-end, as returned by, e.g.,
3024 `org-export-create-backend', or a symbol referring to
3025 a registered back-end.
3027 If narrowing is active in the current buffer, only transcode its
3030 If a region is active, transcode that region.
3032 When optional argument SUBTREEP is non-nil, transcode the
3033 sub-tree at point, extracting information from the headline
3036 When optional argument VISIBLE-ONLY is non-nil, don't export
3037 contents of hidden elements.
3039 When optional argument BODY-ONLY is non-nil, only return body
3040 code, without surrounding template.
3042 Optional argument EXT-PLIST, when provided, is a property list
3043 with external parameters overriding Org default settings, but
3044 still inferior to file-local settings.
3046 Return code as a string."
3047 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3048 (org-export-barf-if-invalid-backend backend
)
3051 ;; Narrow buffer to an appropriate region or subtree for
3052 ;; parsing. If parsing subtree, be sure to remove main headline
3054 (cond ((org-region-active-p)
3055 (narrow-to-region (region-beginning) (region-end)))
3057 (org-narrow-to-subtree)
3058 (goto-char (point-min))
3060 (narrow-to-region (point) (point-max))))
3061 ;; Initialize communication channel with original buffer
3062 ;; attributes, unavailable in its copy.
3063 (let* ((org-export-current-backend (org-export-backend-name backend
))
3064 (info (org-combine-plists
3065 (list :export-options
3067 (list (and subtreep
'subtree
)
3068 (and visible-only
'visible-only
)
3069 (and body-only
'body-only
))))
3070 (org-export--get-buffer-attributes)))
3072 ;; Update communication channel and get parse tree. Buffer
3073 ;; isn't parsed directly. Instead, a temporary copy is
3074 ;; created, where include keywords, macros are expanded and
3075 ;; code blocks are evaluated.
3076 (org-export-with-buffer-copy
3077 ;; Run first hook with current back-end's name as argument.
3078 (run-hook-with-args 'org-export-before-processing-hook
3079 (org-export-backend-name backend
))
3080 (org-export-expand-include-keyword)
3081 ;; Update macro templates since #+INCLUDE keywords might have
3082 ;; added some new ones.
3083 (org-macro-initialize-templates)
3084 (org-macro-replace-all org-macro-templates
)
3085 (org-export-execute-babel-code)
3086 ;; Update radio targets since keyword inclusion might have
3088 (org-update-radio-target-regexp)
3089 ;; Run last hook with current back-end's name as argument.
3090 (goto-char (point-min))
3092 (run-hook-with-args 'org-export-before-parsing-hook
3093 (org-export-backend-name backend
)))
3094 ;; Update communication channel with environment. Also
3095 ;; install user's and developer's filters.
3097 (org-export-install-filters
3099 info
(org-export-get-environment backend subtreep ext-plist
))))
3100 ;; Expand export-specific set of macros: {{{author}}},
3101 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3102 ;; once regular macros have been expanded, since document
3103 ;; keywords may contain one of them.
3104 (org-macro-replace-all
3105 (list (cons "author"
3106 (org-element-interpret-data (plist-get info
:author
)))
3108 (org-element-interpret-data (plist-get info
:date
)))
3109 ;; EMAIL is not a parsed keyword: store it as-is.
3110 (cons "email" (or (plist-get info
:email
) ""))
3112 (org-element-interpret-data (plist-get info
:title
)))))
3114 (setq tree
(org-element-parse-buffer nil visible-only
))
3115 ;; Handle left-over uninterpreted elements or objects in
3116 ;; parse tree and communication channel.
3117 (org-export-remove-uninterpreted-data tree info
)
3118 ;; Call options filters and update export options. We do not
3119 ;; use `org-export-filter-apply-functions' here since the
3120 ;; arity of such filters is different.
3121 (let ((backend-name (org-export-backend-name backend
)))
3122 (dolist (filter (plist-get info
:filter-options
))
3123 (let ((result (funcall filter info backend-name
)))
3124 (when result
(setq info result
)))))
3125 ;; Parse buffer, handle uninterpreted elements or objects,
3126 ;; then call parse-tree filters.
3128 (org-export-filter-apply-functions
3129 (plist-get info
:filter-parse-tree
) tree info
))
3130 ;; Now tree is complete, compute its properties and add them
3131 ;; to communication channel.
3134 info
(org-export-collect-tree-properties tree info
)))
3135 ;; Eventually transcode TREE. Wrap the resulting string into
3137 (let* ((body (org-element-normalize-string
3138 (or (org-export-data tree info
) "")))
3139 (inner-template (cdr (assq 'inner-template
3140 (plist-get info
:translate-alist
))))
3141 (full-body (if (not (functionp inner-template
)) body
3142 (funcall inner-template body info
)))
3143 (template (cdr (assq 'template
3144 (plist-get info
:translate-alist
)))))
3145 ;; Remove all text properties since they cannot be
3146 ;; retrieved from an external process. Finally call
3147 ;; final-output filter and return result.
3149 (org-export-filter-apply-functions
3150 (plist-get info
:filter-final-output
)
3151 (if (or (not (functionp template
)) body-only
) full-body
3152 (funcall template full-body info
))
3156 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3157 "Transcode STRING into BACKEND code.
3159 BACKEND is either an export back-end, as returned by, e.g.,
3160 `org-export-create-backend', or a symbol referring to
3161 a registered back-end.
3163 When optional argument BODY-ONLY is non-nil, only return body
3164 code, without preamble nor postamble.
3166 Optional argument EXT-PLIST, when provided, is a property list
3167 with external parameters overriding Org default settings, but
3168 still inferior to file-local settings.
3170 Return code as a string."
3173 (let ((org-inhibit-startup t
)) (org-mode))
3174 (org-export-as backend nil nil body-only ext-plist
)))
3177 (defun org-export-replace-region-by (backend)
3178 "Replace the active region by its export to BACKEND.
3179 BACKEND is either an export back-end, as returned by, e.g.,
3180 `org-export-create-backend', or a symbol referring to
3181 a registered back-end."
3182 (if (not (org-region-active-p))
3183 (user-error "No active region to replace")
3184 (let* ((beg (region-beginning))
3186 (str (buffer-substring beg end
)) rpl
)
3187 (setq rpl
(org-export-string-as str backend t
))
3188 (delete-region beg end
)
3192 (defun org-export-insert-default-template (&optional backend subtreep
)
3193 "Insert all export keywords with default values at beginning of line.
3195 BACKEND is a symbol referring to the name of a registered export
3196 back-end, for which specific export options should be added to
3197 the template, or `default' for default template. When it is nil,
3198 the user will be prompted for a category.
3200 If SUBTREEP is non-nil, export configuration will be set up
3201 locally for the subtree through node properties."
3203 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3204 (when (and subtreep
(org-before-first-heading-p))
3205 (user-error "No subtree to set export options for"))
3206 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3210 (org-completing-read
3211 "Options category: "
3213 (mapcar #'(lambda (b)
3214 (symbol-name (org-export-backend-name b
)))
3215 org-export--registered-backends
))
3218 ;; Populate OPTIONS and KEYWORDS.
3219 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3220 ((org-export-backend-p backend
)
3221 (org-export-backend-options backend
))
3222 (t (org-export-backend-options
3223 (org-export-get-backend backend
)))))
3224 (let ((keyword (nth 1 entry
))
3225 (option (nth 2 entry
)))
3227 (keyword (unless (assoc keyword keywords
)
3229 (if (eq (nth 4 entry
) 'split
)
3230 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3231 (eval (nth 3 entry
)))))
3232 (push (cons keyword value
) keywords
))))
3233 (option (unless (assoc option options
)
3234 (push (cons option
(eval (nth 3 entry
))) options
))))))
3235 ;; Move to an appropriate location in order to insert options.
3236 (unless subtreep
(beginning-of-line))
3237 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3241 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3242 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3245 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3247 (insert "#+OPTIONS:")
3250 (< (+ width
(length (car items
)) 1) fill-column
))
3251 (let ((item (pop items
)))
3253 (incf width
(1+ (length item
))))))
3255 ;; Then the rest of keywords, in the order specified in either
3256 ;; `org-export-options-alist' or respective export back-ends.
3257 (dolist (key (nreverse keywords
))
3258 (let ((val (cond ((equal (car key
) "DATE")
3261 (org-insert-time-stamp (current-time)))))
3262 ((equal (car key
) "TITLE")
3263 (or (let ((visited-file
3264 (buffer-file-name (buffer-base-buffer))))
3266 (file-name-sans-extension
3267 (file-name-nondirectory visited-file
))))
3268 (buffer-name (buffer-base-buffer))))
3270 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3274 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3276 (defun org-export-expand-include-keyword (&optional included dir
)
3277 "Expand every include keyword in buffer.
3278 Optional argument INCLUDED is a list of included file names along
3279 with their line restriction, when appropriate. It is used to
3280 avoid infinite recursion. Optional argument DIR is the current
3281 working directory. It is used to properly resolve relative
3283 (let ((case-fold-search t
)
3284 (file-prefix (make-hash-table :test
#'equal
))
3286 (goto-char (point-min))
3287 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
3288 (let ((element (save-match-data (org-element-at-point))))
3289 (when (eq (org-element-type element
) 'keyword
)
3291 ;; Extract arguments from keyword's value.
3292 (let* ((value (org-element-property :value element
))
3293 (ind (org-get-indentation))
3294 (file (and (string-match
3295 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3296 (prog1 (expand-file-name
3297 (org-remove-double-quotes
3298 (match-string 1 value
))
3300 (setq value
(replace-match "" nil nil value
)))))
3303 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3305 (prog1 (match-string 1 value
)
3306 (setq value
(replace-match "" nil nil value
)))))
3307 (env (cond ((string-match "\\<example\\>" value
) 'example
)
3308 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3309 (match-string 1 value
))))
3310 ;; Minimal level of included file defaults to the child
3311 ;; level of the current headline, if any, or one. It
3312 ;; only applies is the file is meant to be included as
3316 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3317 (prog1 (string-to-number (match-string 1 value
))
3318 (setq value
(replace-match "" nil nil value
)))
3319 (let ((cur (org-current-level)))
3320 (if cur
(1+ (org-reduced-level cur
)) 1))))))
3322 (delete-region (point) (progn (forward-line) (point)))
3325 ((not (file-readable-p file
))
3326 (error "Cannot include file %s" file
))
3327 ;; Check if files has already been parsed. Look after
3328 ;; inclusion lines too, as different parts of the same file
3329 ;; can be included too.
3330 ((member (list file lines
) included
)
3331 (error "Recursive file inclusion: %s" file
))
3336 (let ((ind-str (make-string ind ?
))
3338 (org-escape-code-in-string
3339 (org-export--prepare-file-contents file lines
))))
3340 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3341 ind-str contents ind-str
))))
3344 (let ((ind-str (make-string ind ?
))
3346 (org-escape-code-in-string
3347 (org-export--prepare-file-contents file lines
))))
3348 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3349 ind-str env contents ind-str
))))
3353 (let ((org-inhibit-startup t
)) (org-mode))
3355 (org-export--prepare-file-contents
3356 file lines ind minlevel
3357 (or (gethash file file-prefix
)
3358 (puthash file
(incf current-prefix
) file-prefix
))))
3359 (org-export-expand-include-keyword
3360 (cons (list file lines
) included
)
3361 (file-name-directory file
))
3362 (buffer-string)))))))))))))
3364 (defun org-export--prepare-file-contents (file &optional lines ind minlevel id
)
3365 "Prepare the contents of FILE for inclusion and return them as a string.
3367 When optional argument LINES is a string specifying a range of
3368 lines, include only those lines.
3370 Optional argument IND, when non-nil, is an integer specifying the
3371 global indentation of returned contents. Since its purpose is to
3372 allow an included file to stay in the same environment it was
3373 created \(i.e. a list item), it doesn't apply past the first
3374 headline encountered.
3376 Optional argument MINLEVEL, when non-nil, is an integer
3377 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 (insert-file-contents file
)
3387 (let* ((lines (split-string lines
"-"))
3388 (lbeg (string-to-number (car lines
)))
3389 (lend (string-to-number (cadr lines
)))
3390 (beg (if (zerop lbeg
) (point-min)
3391 (goto-char (point-min))
3392 (forward-line (1- lbeg
))
3394 (end (if (zerop lend
) (point-max)
3395 (goto-char (point-min))
3396 (forward-line (1- lend
))
3398 (narrow-to-region beg end
)))
3399 ;; Remove blank lines at beginning and end of contents. The logic
3400 ;; behind that removal is that blank lines around include keyword
3401 ;; override blank lines in included file.
3402 (goto-char (point-min))
3403 (org-skip-whitespace)
3405 (delete-region (point-min) (point))
3406 (goto-char (point-max))
3407 (skip-chars-backward " \r\t\n")
3409 (delete-region (point) (point-max))
3410 ;; If IND is set, preserve indentation of include keyword until
3411 ;; the first headline encountered.
3413 (unless (eq major-mode
'org-mode
)
3414 (let ((org-inhibit-startup t
)) (org-mode)))
3415 (goto-char (point-min))
3416 (let ((ind-str (make-string ind ?
)))
3417 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3418 ;; Do not move footnote definitions out of column 0.
3419 (unless (and (looking-at org-footnote-definition-re
)
3420 (eq (org-element-type (org-element-at-point))
3421 'footnote-definition
))
3424 ;; When MINLEVEL is specified, compute minimal level for headlines
3425 ;; in the file (CUR-MIN), and remove stars to each headline so
3426 ;; that headlines with minimal level have a level of MINLEVEL.
3428 (unless (eq major-mode
'org-mode
)
3429 (let ((org-inhibit-startup t
)) (org-mode)))
3430 (org-with-limited-levels
3431 (let ((levels (org-map-entries
3432 (lambda () (org-reduced-level (org-current-level))))))
3434 (let ((offset (- minlevel
(apply 'min levels
))))
3435 (unless (zerop offset
)
3436 (when org-odd-levels-only
(setq offset
(* offset
2)))
3437 ;; Only change stars, don't bother moving whole
3440 (lambda () (if (< offset
0) (delete-char (abs offset
))
3441 (insert (make-string offset ?
*)))))))))))
3442 ;; Append ID to all footnote references and definitions, so they
3443 ;; become file specific and cannot collide with footnotes in other
3446 (goto-char (point-min))
3447 (while (re-search-forward org-footnote-re nil t
)
3448 (let ((reference (org-element-context)))
3449 (when (memq (org-element-type reference
)
3450 '(footnote-reference footnote-definition
))
3451 (goto-char (org-element-property :begin reference
))
3453 (let ((label (org-element-property :label reference
)))
3455 ((org-string-match-p "\\`[0-9]+\\'" label
)
3456 (insert (format "fn:%d-" id
)))
3457 (t (forward-char 3) (insert (format "%d-" id
)))))))))
3458 (org-element-normalize-string (buffer-string))))
3460 (defun org-export-execute-babel-code ()
3461 "Execute every Babel code in the visible part of current buffer."
3462 ;; Get a pristine copy of current buffer so Babel references can be
3463 ;; properly resolved.
3464 (let ((reference (org-export-copy-buffer)))
3465 (unwind-protect (org-babel-exp-process-buffer reference
)
3466 (kill-buffer reference
))))
3468 (defun org-export--copy-to-kill-ring-p ()
3469 "Return a non-nil value when output should be added to the kill ring.
3470 See also `org-export-copy-to-kill-ring'."
3471 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3472 (not (or executing-kbd-macro noninteractive
))
3473 (eq org-export-copy-to-kill-ring t
)))
3477 ;;; Tools For Back-Ends
3479 ;; A whole set of tools is available to help build new exporters. Any
3480 ;; function general enough to have its use across many back-ends
3481 ;; should be added here.
3483 ;;;; For Affiliated Keywords
3485 ;; `org-export-read-attribute' reads a property from a given element
3486 ;; as a plist. It can be used to normalize affiliated keywords'
3489 ;; Since captions can span over multiple lines and accept dual values,
3490 ;; their internal representation is a bit tricky. Therefore,
3491 ;; `org-export-get-caption' transparently returns a given element's
3492 ;; caption as a secondary string.
3494 (defun org-export-read-attribute (attribute element
&optional property
)
3495 "Turn ATTRIBUTE property from ELEMENT into a plist.
3497 When optional argument PROPERTY is non-nil, return the value of
3498 that property within attributes.
3500 This function assumes attributes are defined as \":keyword
3501 value\" pairs. It is appropriate for `:attr_html' like
3504 All values will become strings except the empty string and
3505 \"nil\", which will become nil. Also, values containing only
3506 double quotes will be read as-is, which means that \"\" value
3507 will become the empty string."
3508 (let* ((prepare-value
3511 (cond ((member str
'(nil "" "nil")) nil
)
3512 ((string-match "^\"\\(\"+\\)?\"$" str
)
3513 (or (match-string 1 str
) ""))
3516 (let ((value (org-element-property attribute element
)))
3518 (let ((s (mapconcat 'identity value
" ")) result
)
3519 (while (string-match
3520 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3522 (let ((value (substring s
0 (match-beginning 0))))
3523 (push (funcall prepare-value value
) result
))
3524 (push (intern (match-string 1 s
)) result
)
3525 (setq s
(substring s
(match-end 0))))
3526 ;; Ignore any string before first property with `cdr'.
3527 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3528 (if property
(plist-get attributes property
) attributes
)))
3530 (defun org-export-get-caption (element &optional shortp
)
3531 "Return caption from ELEMENT as a secondary string.
3533 When optional argument SHORTP is non-nil, return short caption,
3534 as a secondary string, instead.
3536 Caption lines are separated by a white space."
3537 (let ((full-caption (org-element-property :caption element
)) caption
)
3538 (dolist (line full-caption
(cdr caption
))
3539 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3541 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3544 ;;;; For Derived Back-ends
3546 ;; `org-export-with-backend' is a function allowing to locally use
3547 ;; another back-end to transcode some object or element. In a derived
3548 ;; back-end, it may be used as a fall-back function once all specific
3549 ;; cases have been treated.
3551 (defun org-export-with-backend (backend data
&optional contents info
)
3552 "Call a transcoder from BACKEND on DATA.
3553 BACKEND is an export back-end, as returned by, e.g.,
3554 `org-export-create-backend', or a symbol referring to
3555 a registered back-end. DATA is an Org element, object, secondary
3556 string or string. CONTENTS, when non-nil, is the transcoded
3557 contents of DATA element, as a string. INFO, when non-nil, is
3558 the communication channel used for export, as a plist."
3559 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3560 (org-export-barf-if-invalid-backend backend
)
3561 (let ((type (org-element-type data
)))
3562 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3563 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3564 (transcoder (cdr (assq type all-transcoders
))))
3565 (if (not (functionp transcoder
))
3566 (error "No foreign transcoder available")
3568 transcoder data contents
3570 info
(list :back-end backend
3571 :translate-alist all-transcoders
3572 :exported-data
(make-hash-table :test
'eq
:size
401)))))))))
3575 ;;;; For Export Snippets
3577 ;; Every export snippet is transmitted to the back-end. Though, the
3578 ;; latter will only retain one type of export-snippet, ignoring
3579 ;; others, based on the former's target back-end. The function
3580 ;; `org-export-snippet-backend' returns that back-end for a given
3583 (defun org-export-snippet-backend (export-snippet)
3584 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3585 Translation, with `org-export-snippet-translation-alist', is
3587 (let ((back-end (org-element-property :back-end export-snippet
)))
3589 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3595 ;; `org-export-collect-footnote-definitions' is a tool to list
3596 ;; actually used footnotes definitions in the whole parse tree, or in
3597 ;; a headline, in order to add footnote listings throughout the
3600 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3601 ;; back-ends, when they need to attach the footnote definition only to
3602 ;; the first occurrence of the corresponding label.
3604 ;; `org-export-get-footnote-definition' and
3605 ;; `org-export-get-footnote-number' provide easier access to
3606 ;; additional information relative to a footnote reference.
3608 (defun org-export-collect-footnote-definitions (data info
)
3609 "Return an alist between footnote numbers, labels and definitions.
3611 DATA is the parse tree from which definitions are collected.
3612 INFO is the plist used as a communication channel.
3614 Definitions are sorted by order of references. They either
3615 appear as Org data or as a secondary string for inlined
3616 footnotes. Unreferenced definitions are ignored."
3618 collect-fn
; for byte-compiler.
3622 ;; Collect footnote number, label and definition in DATA.
3623 (org-element-map data
'footnote-reference
3625 (when (org-export-footnote-first-reference-p fn info
)
3626 (let ((def (org-export-get-footnote-definition fn info
)))
3628 (list (org-export-get-footnote-number fn info
)
3629 (org-element-property :label fn
)
3632 ;; Also search in definition for nested footnotes.
3633 (when (eq (org-element-property :type fn
) 'standard
)
3634 (funcall collect-fn def
)))))
3635 ;; Don't enter footnote definitions since it will happen
3636 ;; when their first reference is found.
3637 info nil
'footnote-definition
)))))
3638 (funcall collect-fn
(plist-get info
:parse-tree
))
3639 (reverse num-alist
)))
3641 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3642 "Non-nil when a footnote reference is the first one for its label.
3644 FOOTNOTE-REFERENCE is the footnote reference being considered.
3645 INFO is the plist used as a communication channel."
3646 (let ((label (org-element-property :label footnote-reference
)))
3647 ;; Anonymous footnotes are always a first reference.
3649 ;; Otherwise, return the first footnote with the same LABEL and
3650 ;; test if it is equal to FOOTNOTE-REFERENCE.
3651 (let* (search-refs ; for byte-compiler.
3655 (org-element-map data
'footnote-reference
3658 ((string= (org-element-property :label fn
) label
)
3660 ;; If FN isn't inlined, be sure to traverse its
3661 ;; definition before resuming search. See
3662 ;; comments in `org-export-get-footnote-number'
3663 ;; for more information.
3664 ((eq (org-element-property :type fn
) 'standard
)
3665 (funcall search-refs
3666 (org-export-get-footnote-definition fn info
)))))
3667 ;; Don't enter footnote definitions since it will
3668 ;; happen when their first reference is found.
3669 info
'first-match
'footnote-definition
)))))
3670 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3671 footnote-reference
)))))
3673 (defun org-export-get-footnote-definition (footnote-reference info
)
3674 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3675 INFO is the plist used as a communication channel. If no such
3676 definition can be found, return the \"DEFINITION NOT FOUND\"
3678 (let ((label (org-element-property :label footnote-reference
)))
3679 (or (org-element-property :inline-definition footnote-reference
)
3680 (cdr (assoc label
(plist-get info
:footnote-definition-alist
)))
3681 "DEFINITION NOT FOUND.")))
3683 (defun org-export-get-footnote-number (footnote info
)
3684 "Return number associated to a footnote.
3686 FOOTNOTE is either a footnote reference or a footnote definition.
3687 INFO is the plist used as a communication channel."
3688 (let* ((label (org-element-property :label footnote
))
3690 search-ref
; For byte-compiler.
3694 ;; Search footnote references through DATA, filling
3695 ;; SEEN-REFS along the way.
3696 (org-element-map data
'footnote-reference
3698 (let ((fn-lbl (org-element-property :label fn
)))
3700 ;; Anonymous footnote match: return number.
3701 ((and (not fn-lbl
) (eq fn footnote
))
3702 (throw 'exit
(1+ (length seen-refs
))))
3703 ;; Labels match: return number.
3704 ((and label
(string= label fn-lbl
))
3705 (throw 'exit
(1+ (length seen-refs
))))
3706 ;; Anonymous footnote: it's always a new one.
3707 ;; Also, be sure to return nil from the `cond' so
3708 ;; `first-match' doesn't get us out of the loop.
3709 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3710 ;; Label not seen so far: add it so SEEN-REFS.
3712 ;; Also search for subsequent references in
3713 ;; footnote definition so numbering follows
3714 ;; reading logic. Note that we don't have to care
3715 ;; about inline definitions, since
3716 ;; `org-element-map' already traverses them at the
3719 ;; Once again, return nil to stay in the loop.
3720 ((not (member fn-lbl seen-refs
))
3721 (push fn-lbl seen-refs
)
3723 (org-export-get-footnote-definition fn info
))
3725 ;; Don't enter footnote definitions since it will
3726 ;; happen when their first reference is found.
3727 info
'first-match
'footnote-definition
)))))
3728 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3733 ;; `org-export-get-relative-level' is a shortcut to get headline
3734 ;; level, relatively to the lower headline level in the parsed tree.
3736 ;; `org-export-get-headline-number' returns the section number of an
3737 ;; headline, while `org-export-number-to-roman' allows to convert it
3738 ;; to roman numbers.
3740 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3741 ;; `org-export-last-sibling-p' are three useful predicates when it
3742 ;; comes to fulfill the `:headline-levels' property.
3744 ;; `org-export-get-tags', `org-export-get-category' and
3745 ;; `org-export-get-node-property' extract useful information from an
3746 ;; headline or a parent headline. They all handle inheritance.
3748 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3749 ;; as a secondary string, suitable for table of contents. It falls
3750 ;; back onto default title.
3752 (defun org-export-get-relative-level (headline info
)
3753 "Return HEADLINE relative level within current parsed tree.
3754 INFO is a plist holding contextual information."
3755 (+ (org-element-property :level headline
)
3756 (or (plist-get info
:headline-offset
) 0)))
3758 (defun org-export-low-level-p (headline info
)
3759 "Non-nil when HEADLINE is considered as low level.
3761 INFO is a plist used as a communication channel.
3763 A low level headlines has a relative level greater than
3764 `:headline-levels' property value.
3766 Return value is the difference between HEADLINE relative level
3767 and the last level being considered as high enough, or nil."
3768 (let ((limit (plist-get info
:headline-levels
)))
3769 (when (wholenump limit
)
3770 (let ((level (org-export-get-relative-level headline info
)))
3771 (and (> level limit
) (- level limit
))))))
3773 (defun org-export-get-headline-number (headline info
)
3774 "Return HEADLINE numbering as a list of numbers.
3775 INFO is a plist holding contextual information."
3776 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3778 (defun org-export-numbered-headline-p (headline info
)
3779 "Return a non-nil value if HEADLINE element should be numbered.
3780 INFO is a plist used as a communication channel."
3781 (let ((sec-num (plist-get info
:section-numbers
))
3782 (level (org-export-get-relative-level headline info
)))
3783 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3785 (defun org-export-number-to-roman (n)
3786 "Convert integer N into a roman numeral."
3787 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3788 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3789 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3793 (number-to-string n
)
3795 (if (>= n
(caar roman
))
3796 (setq n
(- n
(caar roman
))
3797 res
(concat res
(cdar roman
)))
3801 (defun org-export-get-tags (element info
&optional tags inherited
)
3802 "Return list of tags associated to ELEMENT.
3804 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3805 is a plist used as a communication channel.
3807 Select tags (see `org-export-select-tags') and exclude tags (see
3808 `org-export-exclude-tags') are removed from the list.
3810 When non-nil, optional argument TAGS should be a list of strings.
3811 Any tag belonging to this list will also be removed.
3813 When optional argument INHERITED is non-nil, tags can also be
3814 inherited from parent headlines and FILETAGS keywords."
3816 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3817 (member tag
(plist-get info
:exclude-tags
))
3819 (if (not inherited
) (org-element-property :tags element
)
3820 ;; Build complete list of inherited tags.
3821 (let ((current-tag-list (org-element-property :tags element
)))
3826 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3827 (not (member tag current-tag-list
)))
3828 (push tag current-tag-list
)))
3829 (org-element-property :tags parent
)))
3830 (org-export-get-genealogy element
))
3831 ;; Add FILETAGS keywords and return results.
3832 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3834 (defun org-export-get-node-property (property blob
&optional inherited
)
3835 "Return node PROPERTY value for BLOB.
3837 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3840 If optional argument INHERITED is non-nil, the value can be
3841 inherited from a parent headline.
3843 Return value is a string or nil."
3844 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3845 (org-export-get-parent-headline blob
))))
3846 (if (not inherited
) (org-element-property property blob
)
3847 (let ((parent headline
) value
)
3850 (when (plist-member (nth 1 parent
) property
)
3851 (throw 'found
(org-element-property property parent
)))
3852 (setq parent
(org-element-property :parent parent
))))))))
3854 (defun org-export-get-category (blob info
)
3855 "Return category for element or object BLOB.
3857 INFO is a plist used as a communication channel.
3859 CATEGORY is automatically inherited from a parent headline, from
3860 #+CATEGORY: keyword or created out of original file name. If all
3861 fail, the fall-back value is \"???\"."
3862 (or (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3863 (org-export-get-parent-headline blob
))))
3864 ;; Almost like `org-export-node-property', but we cannot trust
3865 ;; `plist-member' as every headline has a `:CATEGORY'
3866 ;; property, would it be nil or equal to "???" (which has the
3868 (let ((parent headline
) value
)
3871 (let ((category (org-element-property :CATEGORY parent
)))
3872 (and category
(not (equal "???" category
))
3873 (throw 'found category
)))
3874 (setq parent
(org-element-property :parent parent
))))))
3875 (org-element-map (plist-get info
:parse-tree
) 'keyword
3877 (when (equal (org-element-property :key kwd
) "CATEGORY")
3878 (org-element-property :value kwd
)))
3880 (let ((file (plist-get info
:input-file
)))
3881 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3884 (defun org-export-get-alt-title (headline info
)
3885 "Return alternative title for HEADLINE, as a secondary string.
3886 INFO is a plist used as a communication channel. If no optional
3887 title is defined, fall-back to the regular title."
3888 (or (org-element-property :alt-title headline
)
3889 (org-element-property :title headline
)))
3891 (defun org-export-first-sibling-p (headline info
)
3892 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3893 INFO is a plist used as a communication channel."
3894 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3897 (defun org-export-last-sibling-p (headline info
)
3898 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3899 INFO is a plist used as a communication channel."
3900 (not (org-export-get-next-element headline info
)))
3905 ;; `org-export-get-date' returns a date appropriate for the document
3906 ;; to about to be exported. In particular, it takes care of
3907 ;; `org-export-date-timestamp-format'.
3909 (defun org-export-get-date (info &optional fmt
)
3910 "Return date value for the current document.
3912 INFO is a plist used as a communication channel. FMT, when
3913 non-nil, is a time format string that will be applied on the date
3914 if it consists in a single timestamp object. It defaults to
3915 `org-export-date-timestamp-format' when nil.
3917 A proper date can be a secondary string, a string or nil. It is
3918 meant to be translated with `org-export-data' or alike."
3919 (let ((date (plist-get info
:date
))
3920 (fmt (or fmt org-export-date-timestamp-format
)))
3921 (cond ((not date
) nil
)
3924 (eq (org-element-type (car date
)) 'timestamp
))
3925 (org-timestamp-format (car date
) fmt
))
3931 ;; `org-export-solidify-link-text' turns a string into a safer version
3932 ;; for links, replacing most non-standard characters with hyphens.
3934 ;; `org-export-get-coderef-format' returns an appropriate format
3935 ;; string for coderefs.
3937 ;; `org-export-inline-image-p' returns a non-nil value when the link
3938 ;; provided should be considered as an inline image.
3940 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3941 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3942 ;; returns an appropriate unique identifier when found, or nil.
3944 ;; `org-export-resolve-id-link' returns the first headline with
3945 ;; specified id or custom-id in parse tree, the path to the external
3946 ;; file with the id or nil when neither was found.
3948 ;; `org-export-resolve-coderef' associates a reference to a line
3949 ;; number in the element it belongs, or returns the reference itself
3950 ;; when the element isn't numbered.
3952 (defun org-export-solidify-link-text (s)
3953 "Take link text S and make a safe target out of it."
3955 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-:]+") "-")))
3957 (defun org-export-get-coderef-format (path desc
)
3958 "Return format string for code reference link.
3959 PATH is the link path. DESC is its description."
3961 (cond ((not desc
) "%s")
3962 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3963 (replace-match "%s" t t desc
))
3966 (defun org-export-inline-image-p (link &optional rules
)
3967 "Non-nil if LINK object points to an inline image.
3969 Optional argument is a set of RULES defining inline images. It
3970 is an alist where associations have the following shape:
3974 Applying a rule means apply REGEXP against LINK's path when its
3975 type is TYPE. The function will return a non-nil value if any of
3976 the provided rules is non-nil. The default rule is
3977 `org-export-default-inline-image-rule'.
3979 This only applies to links without a description."
3980 (and (not (org-element-contents link
))
3981 (let ((case-fold-search t
)
3982 (rules (or rules org-export-default-inline-image-rule
)))
3986 (and (string= (org-element-property :type link
) (car rule
))
3987 (string-match (cdr rule
)
3988 (org-element-property :path link
))
3991 ;; Return nil if no rule matched.
3994 (defun org-export-resolve-coderef (ref info
)
3995 "Resolve a code reference REF.
3997 INFO is a plist used as a communication channel.
3999 Return associated line number in source code, or REF itself,
4000 depending on src-block or example element's switches."
4001 (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
4004 (insert (org-trim (org-element-property :value el
)))
4005 (let* ((label-fmt (regexp-quote
4006 (or (org-element-property :label-fmt el
)
4007 org-coderef-label-format
)))
4009 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
4010 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
4011 ;; Element containing REF is found. Resolve it to either
4012 ;; a label or a line number, as needed.
4013 (when (re-search-backward ref-re nil t
)
4015 ((org-element-property :use-labels el
) ref
)
4016 ((eq (org-element-property :number-lines el
) 'continued
)
4017 (+ (org-export-get-loc el info
) (line-number-at-pos)))
4018 (t (line-number-at-pos)))))))
4021 (defun org-export-resolve-fuzzy-link (link info
)
4022 "Return LINK destination.
4024 INFO is a plist holding contextual information.
4026 Return value can be an object, an element, or nil:
4028 - If LINK path matches a target object (i.e. <<path>>) return it.
4030 - If LINK path exactly matches the name affiliated keyword
4031 \(i.e. #+NAME: path) of an element, return that element.
4033 - If LINK path exactly matches any headline name, return that
4034 element. If more than one headline share that name, priority
4035 will be given to the one with the closest common ancestor, if
4036 any, or the first one in the parse tree otherwise.
4038 - Otherwise, return nil.
4040 Assume LINK type is \"fuzzy\". White spaces are not
4042 (let* ((raw-path (org-element-property :path link
))
4043 (match-title-p (eq (aref raw-path
0) ?
*))
4044 ;; Split PATH at white spaces so matches are space
4046 (path (org-split-string
4047 (if match-title-p
(substring raw-path
1) raw-path
)))
4048 ;; Cache for destinations that are not position dependent.
4050 (or (plist-get info
:resolve-fuzzy-link-cache
)
4051 (plist-get (setq info
(plist-put info
:resolve-fuzzy-link-cache
4052 (make-hash-table :test
'equal
)))
4053 :resolve-fuzzy-link-cache
)))
4054 (cached (gethash path link-cache
'not-found
)))
4056 ;; Destination is not position dependent: use cached value.
4057 ((and (not match-title-p
) (not (eq cached
'not-found
))) cached
)
4058 ;; First try to find a matching "<<path>>" unless user specified
4059 ;; he was looking for a headline (path starts with a "*"
4061 ((and (not match-title-p
)
4062 (let ((match (org-element-map (plist-get info
:parse-tree
) 'target
4064 (and (equal (org-split-string
4065 (org-element-property :value blob
))
4068 info
'first-match
)))
4069 (and match
(puthash path match link-cache
)))))
4070 ;; Then try to find an element with a matching "#+NAME: path"
4071 ;; affiliated keyword.
4072 ((and (not match-title-p
)
4073 (let ((match (org-element-map (plist-get info
:parse-tree
)
4074 org-element-all-elements
4076 (let ((name (org-element-property :name el
)))
4078 (equal (org-split-string name
) path
))
4080 info
'first-match
)))
4081 (and match
(puthash path match link-cache
)))))
4082 ;; Last case: link either points to a headline or to nothingness.
4083 ;; Try to find the source, with priority given to headlines with
4084 ;; the closest common ancestor. If such candidate is found,
4085 ;; return it, otherwise return nil.
4087 (let ((find-headline
4089 ;; Return first headline whose `:raw-value' property is
4090 ;; NAME in parse tree DATA, or nil. Statistics cookies
4093 (org-element-map data
'headline
4095 (when (equal (org-split-string
4096 (replace-regexp-in-string
4097 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4098 (org-element-property :raw-value headline
)))
4101 info
'first-match
)))))
4102 ;; Search among headlines sharing an ancestor with link, from
4103 ;; closest to farthest.
4107 (let ((foundp (funcall find-headline path parent
)))
4108 (when foundp
(throw 'exit foundp
))))
4109 (let ((parent-hl (org-export-get-parent-headline link
)))
4110 (if (not parent-hl
) (list (plist-get info
:parse-tree
))
4111 (cons parent-hl
(org-export-get-genealogy parent-hl
)))))
4112 ;; No destination found: return nil.
4113 (and (not match-title-p
) (puthash path nil link-cache
))))))))
4115 (defun org-export-resolve-id-link (link info
)
4116 "Return headline referenced as LINK destination.
4118 INFO is a plist used as a communication channel.
4120 Return value can be the headline element matched in current parse
4121 tree, a file name or nil. Assume LINK type is either \"id\" or
4123 (let ((id (org-element-property :path link
)))
4124 ;; First check if id is within the current parse tree.
4125 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4127 (when (or (string= (org-element-property :ID headline
) id
)
4128 (string= (org-element-property :CUSTOM_ID headline
) id
))
4131 ;; Otherwise, look for external files.
4132 (cdr (assoc id
(plist-get info
:id-alist
))))))
4134 (defun org-export-resolve-radio-link (link info
)
4135 "Return radio-target object referenced as LINK destination.
4137 INFO is a plist used as a communication channel.
4139 Return value can be a radio-target object or nil. Assume LINK
4140 has type \"radio\"."
4141 (let ((path (replace-regexp-in-string
4142 "[ \r\t\n]+" " " (org-element-property :path link
))))
4143 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4145 (and (eq (compare-strings
4146 (replace-regexp-in-string
4147 "[ \r\t\n]+" " " (org-element-property :value radio
))
4148 nil nil path nil nil t
)
4151 info
'first-match
)))
4156 ;; `org-export-get-ordinal' associates a sequence number to any object
4159 (defun org-export-get-ordinal (element info
&optional types predicate
)
4160 "Return ordinal number of an element or object.
4162 ELEMENT is the element or object considered. INFO is the plist
4163 used as a communication channel.
4165 Optional argument TYPES, when non-nil, is a list of element or
4166 object types, as symbols, that should also be counted in.
4167 Otherwise, only provided element's type is considered.
4169 Optional argument PREDICATE is a function returning a non-nil
4170 value if the current element or object should be counted in. It
4171 accepts two arguments: the element or object being considered and
4172 the plist used as a communication channel. This allows to count
4173 only a certain type of objects (i.e. inline images).
4175 Return value is a list of numbers if ELEMENT is a headline or an
4176 item. It is nil for keywords. It represents the footnote number
4177 for footnote definitions and footnote references. If ELEMENT is
4178 a target, return the same value as if ELEMENT was the closest
4179 table, item or headline containing the target. In any other
4180 case, return the sequence number of ELEMENT among elements or
4181 objects of the same type."
4182 ;; Ordinal of a target object refer to the ordinal of the closest
4183 ;; table, item, or headline containing the object.
4184 (when (eq (org-element-type element
) 'target
)
4186 (loop for parent in
(org-export-get-genealogy element
)
4189 (org-element-type parent
)
4190 '(footnote-definition footnote-reference headline item
4193 (case (org-element-type element
)
4194 ;; Special case 1: A headline returns its number as a list.
4195 (headline (org-export-get-headline-number element info
))
4196 ;; Special case 2: An item returns its number as a list.
4197 (item (let ((struct (org-element-property :structure element
)))
4198 (org-list-get-item-number
4199 (org-element-property :begin element
)
4201 (org-list-prevs-alist struct
)
4202 (org-list-parents-alist struct
))))
4203 ((footnote-definition footnote-reference
)
4204 (org-export-get-footnote-number element info
))
4207 ;; Increment counter until ELEMENT is found again.
4208 (org-element-map (plist-get info
:parse-tree
)
4209 (or types
(org-element-type element
))
4212 ((eq element el
) (1+ counter
))
4213 ((not predicate
) (incf counter
) nil
)
4214 ((funcall predicate el info
) (incf counter
) nil
)))
4215 info
'first-match
)))))
4220 ;; `org-export-get-loc' counts number of code lines accumulated in
4221 ;; src-block or example-block elements with a "+n" switch until
4222 ;; a given element, excluded. Note: "-n" switches reset that count.
4224 ;; `org-export-unravel-code' extracts source code (along with a code
4225 ;; references alist) from an `element-block' or `src-block' type
4228 ;; `org-export-format-code' applies a formatting function to each line
4229 ;; of code, providing relative line number and code reference when
4230 ;; appropriate. Since it doesn't access the original element from
4231 ;; which the source code is coming, it expects from the code calling
4232 ;; it to know if lines should be numbered and if code references
4235 ;; Eventually, `org-export-format-code-default' is a higher-level
4236 ;; function (it makes use of the two previous functions) which handles
4237 ;; line numbering and code references inclusion, and returns source
4238 ;; code in a format suitable for plain text or verbatim output.
4240 (defun org-export-get-loc (element info
)
4241 "Return accumulated lines of code up to ELEMENT.
4243 INFO is the plist used as a communication channel.
4245 ELEMENT is excluded from count."
4247 (org-element-map (plist-get info
:parse-tree
)
4248 `(src-block example-block
,(org-element-type element
))
4251 ;; ELEMENT is reached: Quit the loop.
4253 ;; Only count lines from src-block and example-block elements
4254 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4255 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4256 ((let ((linums (org-element-property :number-lines el
)))
4258 ;; Accumulate locs or reset them.
4259 (let ((lines (org-count-lines
4260 (org-trim (org-element-property :value el
)))))
4261 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4262 ;; Return nil to stay in the loop.
4268 (defun org-export-unravel-code (element)
4269 "Clean source code and extract references out of it.
4271 ELEMENT has either a `src-block' an `example-block' type.
4273 Return a cons cell whose CAR is the source code, cleaned from any
4274 reference, protective commas and spurious indentation, and CDR is
4275 an alist between relative line number (integer) and name of code
4276 reference on that line (string)."
4277 (let* ((line 0) refs
4278 (value (org-element-property :value element
))
4279 ;; Get code and clean it. Remove blank lines at its
4280 ;; beginning and end.
4281 (code (replace-regexp-in-string
4282 "\\`\\([ \t]*\n\\)+" ""
4283 (replace-regexp-in-string
4284 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4285 (if (or org-src-preserve-indentation
4286 (org-element-property :preserve-indent element
))
4288 (org-element-remove-indentation value
)))))
4289 ;; Get format used for references.
4290 (label-fmt (regexp-quote
4291 (or (org-element-property :label-fmt element
)
4292 org-coderef-label-format
)))
4293 ;; Build a regexp matching a loc with a reference.
4295 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4296 (replace-regexp-in-string
4297 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4300 ;; Code with references removed.
4301 (org-element-normalize-string
4305 (if (not (string-match with-ref-re loc
)) loc
4306 ;; Ref line: remove ref, and signal its position in REFS.
4307 (push (cons line
(match-string 3 loc
)) refs
)
4308 (replace-match "" nil nil loc
1)))
4309 (org-split-string code
"\n") "\n"))
4313 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4314 "Format CODE by applying FUN line-wise and return it.
4316 CODE is a string representing the code to format. FUN is
4317 a function. It must accept three arguments: a line of
4318 code (string), the current line number (integer) or nil and the
4319 reference associated to the current line (string) or nil.
4321 Optional argument NUM-LINES can be an integer representing the
4322 number of code lines accumulated until the current code. Line
4323 numbers passed to FUN will take it into account. If it is nil,
4324 FUN's second argument will always be nil. This number can be
4325 obtained with `org-export-get-loc' function.
4327 Optional argument REF-ALIST can be an alist between relative line
4328 number (i.e. ignoring NUM-LINES) and the name of the code
4329 reference on it. If it is nil, FUN's third argument will always
4330 be nil. It can be obtained through the use of
4331 `org-export-unravel-code' function."
4332 (let ((--locs (org-split-string code
"\n"))
4334 (org-element-normalize-string
4338 (let ((--ref (cdr (assq --line ref-alist
))))
4339 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4342 (defun org-export-format-code-default (element info
)
4343 "Return source code from ELEMENT, formatted in a standard way.
4345 ELEMENT is either a `src-block' or `example-block' element. INFO
4346 is a plist used as a communication channel.
4348 This function takes care of line numbering and code references
4349 inclusion. Line numbers, when applicable, appear at the
4350 beginning of the line, separated from the code by two white
4351 spaces. Code references, on the other hand, appear flushed to
4352 the right, separated by six white spaces from the widest line of
4354 ;; Extract code and references.
4355 (let* ((code-info (org-export-unravel-code element
))
4356 (code (car code-info
))
4357 (code-lines (org-split-string code
"\n")))
4358 (if (null code-lines
) ""
4359 (let* ((refs (and (org-element-property :retain-labels element
)
4361 ;; Handle line numbering.
4362 (num-start (case (org-element-property :number-lines element
)
4363 (continued (org-export-get-loc element info
))
4368 (length (number-to-string
4369 (+ (length code-lines
) num-start
))))))
4370 ;; Prepare references display, if required. Any reference
4371 ;; should start six columns after the widest line of code,
4372 ;; wrapped with parenthesis.
4374 (+ (apply 'max
(mapcar 'length code-lines
))
4375 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4376 (org-export-format-code
4378 (lambda (loc line-num ref
)
4379 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4384 (concat (make-string
4386 (+ (length loc
) (length number-str
))) ?
)
4387 (format "(%s)" ref
))))))
4393 ;; `org-export-table-has-special-column-p' and and
4394 ;; `org-export-table-row-is-special-p' are predicates used to look for
4395 ;; meta-information about the table structure.
4397 ;; `org-table-has-header-p' tells when the rows before the first rule
4398 ;; should be considered as table's header.
4400 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4401 ;; and `org-export-table-cell-borders' extract information from
4402 ;; a table-cell element.
4404 ;; `org-export-table-dimensions' gives the number on rows and columns
4405 ;; in the table, ignoring horizontal rules and special columns.
4406 ;; `org-export-table-cell-address', given a table-cell object, returns
4407 ;; the absolute address of a cell. On the other hand,
4408 ;; `org-export-get-table-cell-at' does the contrary.
4410 ;; `org-export-table-cell-starts-colgroup-p',
4411 ;; `org-export-table-cell-ends-colgroup-p',
4412 ;; `org-export-table-row-starts-rowgroup-p',
4413 ;; `org-export-table-row-ends-rowgroup-p',
4414 ;; `org-export-table-row-starts-header-p' and
4415 ;; `org-export-table-row-ends-header-p' indicate position of current
4416 ;; row or cell within the table.
4418 (defun org-export-table-has-special-column-p (table)
4419 "Non-nil when TABLE has a special column.
4420 All special columns will be ignored during export."
4421 ;; The table has a special column when every first cell of every row
4422 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4423 ;; "*" "_" and "^". Though, do not consider a first row containing
4424 ;; only empty cells as special.
4425 (let ((special-column-p 'empty
))
4429 (when (eq (org-element-property :type row
) 'standard
)
4430 (let ((value (org-element-contents
4431 (car (org-element-contents row
)))))
4432 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4433 (setq special-column-p
'special
))
4435 (t (throw 'exit nil
))))))
4436 (org-element-contents table
))
4437 (eq special-column-p
'special
))))
4439 (defun org-export-table-has-header-p (table info
)
4440 "Non-nil when TABLE has a header.
4442 INFO is a plist used as a communication channel.
4444 A table has a header when it contains at least two row groups."
4445 (let ((cache (or (plist-get info
:table-header-cache
)
4446 (plist-get (setq info
4447 (plist-put info
:table-header-cache
4448 (make-hash-table :test
'eq
)))
4449 :table-header-cache
))))
4450 (or (gethash table cache
)
4451 (let ((rowgroup 1) row-flag
)
4454 (org-element-map table
'table-row
4458 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4459 (incf rowgroup
) (setq row-flag nil
))
4460 ((and (not row-flag
) (eq (org-element-property :type row
)
4462 (setq row-flag t
) nil
)))
4466 (defun org-export-table-row-is-special-p (table-row info
)
4467 "Non-nil if TABLE-ROW is considered special.
4469 INFO is a plist used as the communication channel.
4471 All special rows will be ignored during export."
4472 (when (eq (org-element-property :type table-row
) 'standard
)
4473 (let ((first-cell (org-element-contents
4474 (car (org-element-contents table-row
)))))
4475 ;; A row is special either when...
4477 ;; ... it starts with a field only containing "/",
4478 (equal first-cell
'("/"))
4479 ;; ... the table contains a special column and the row start
4480 ;; with a marking character among, "^", "_", "$" or "!",
4481 (and (org-export-table-has-special-column-p
4482 (org-export-get-parent table-row
))
4483 (member first-cell
'(("^") ("_") ("$") ("!"))))
4484 ;; ... it contains only alignment cookies and empty cells.
4485 (let ((special-row-p 'empty
))
4489 (let ((value (org-element-contents cell
)))
4490 ;; Since VALUE is a secondary string, the following
4491 ;; checks avoid expanding it with `org-export-data'.
4493 ((and (not (cdr value
))
4494 (stringp (car value
))
4495 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4497 (setq special-row-p
'cookie
))
4498 (t (throw 'exit nil
)))))
4499 (org-element-contents table-row
))
4500 (eq special-row-p
'cookie
)))))))
4502 (defun org-export-table-row-group (table-row info
)
4503 "Return TABLE-ROW's group number, as an integer.
4505 INFO is a plist used as the communication channel.
4507 Return value is the group number, as an integer, or nil for
4508 special rows and rows separators. First group is also table's
4510 (let ((cache (or (plist-get info
:table-row-group-cache
)
4511 (plist-get (setq info
4512 (plist-put info
:table-row-group-cache
4513 (make-hash-table :test
'eq
)))
4514 :table-row-group-cache
))))
4515 (cond ((gethash table-row cache
))
4516 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4517 (t (let ((group 0) row-flag
)
4518 (org-element-map (org-export-get-parent table-row
) 'table-row
4520 (if (eq (org-element-property :type row
) 'rule
)
4522 (unless row-flag
(incf group
) (setq row-flag t
)))
4523 (when (eq table-row row
) (puthash table-row group cache
)))
4524 info
'first-match
))))))
4526 (defun org-export-table-cell-width (table-cell info
)
4527 "Return TABLE-CELL contents width.
4529 INFO is a plist used as the communication channel.
4531 Return value is the width given by the last width cookie in the
4532 same column as TABLE-CELL, or nil."
4533 (let* ((row (org-export-get-parent table-cell
))
4534 (table (org-export-get-parent row
))
4535 (cells (org-element-contents row
))
4536 (columns (length cells
))
4537 (column (- columns
(length (memq table-cell cells
))))
4538 (cache (or (plist-get info
:table-cell-width-cache
)
4539 (plist-get (setq info
4540 (plist-put info
:table-cell-width-cache
4541 (make-hash-table :test
'eq
)))
4542 :table-cell-width-cache
)))
4543 (width-vector (or (gethash table cache
)
4544 (puthash table
(make-vector columns
'empty
) cache
)))
4545 (value (aref width-vector column
)))
4546 (if (not (eq value
'empty
)) value
4548 (dolist (row (org-element-contents table
)
4549 (aset width-vector column cookie-width
))
4550 (when (org-export-table-row-is-special-p row info
)
4551 ;; In a special row, try to find a width cookie at COLUMN.
4552 (let* ((value (org-element-contents
4553 (elt (org-element-contents row
) column
)))
4554 (cookie (car value
)))
4555 ;; The following checks avoid expanding unnecessarily
4556 ;; the cell with `org-export-data'.
4560 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4561 (match-string 1 cookie
))
4563 (string-to-number (match-string 1 cookie
)))))))))))
4565 (defun org-export-table-cell-alignment (table-cell info
)
4566 "Return TABLE-CELL contents alignment.
4568 INFO is a plist used as the communication channel.
4570 Return alignment as specified by the last alignment cookie in the
4571 same column as TABLE-CELL. If no such cookie is found, a default
4572 alignment value will be deduced from fraction of numbers in the
4573 column (see `org-table-number-fraction' for more information).
4574 Possible values are `left', `right' and `center'."
4575 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4576 (require 'org-table
)
4577 (let* ((row (org-export-get-parent table-cell
))
4578 (table (org-export-get-parent row
))
4579 (cells (org-element-contents row
))
4580 (columns (length cells
))
4581 (column (- columns
(length (memq table-cell cells
))))
4582 (cache (or (plist-get info
:table-cell-alignment-cache
)
4583 (plist-get (setq info
4584 (plist-put info
:table-cell-alignment-cache
4585 (make-hash-table :test
'eq
)))
4586 :table-cell-alignment-cache
)))
4587 (align-vector (or (gethash table cache
)
4588 (puthash table
(make-vector columns nil
) cache
))))
4589 (or (aref align-vector column
)
4590 (let ((number-cells 0)
4593 previous-cell-number-p
)
4594 (dolist (row (org-element-contents (org-export-get-parent row
)))
4596 ;; In a special row, try to find an alignment cookie at
4598 ((org-export-table-row-is-special-p row info
)
4599 (let ((value (org-element-contents
4600 (elt (org-element-contents row
) column
))))
4601 ;; Since VALUE is a secondary string, the following
4602 ;; checks avoid useless expansion through
4603 ;; `org-export-data'.
4606 (stringp (car value
))
4607 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4609 (match-string 1 (car value
)))
4610 (setq cookie-align
(match-string 1 (car value
))))))
4611 ;; Ignore table rules.
4612 ((eq (org-element-property :type row
) 'rule
))
4613 ;; In a standard row, check if cell's contents are
4614 ;; expressing some kind of number. Increase NUMBER-CELLS
4615 ;; accordingly. Though, don't bother if an alignment
4616 ;; cookie has already defined cell's alignment.
4618 (let ((value (org-export-data
4619 (org-element-contents
4620 (elt (org-element-contents row
) column
))
4623 ;; Treat an empty cell as a number if it follows
4625 (if (not (or (string-match org-table-number-regexp value
)
4626 (and (string= value
"") previous-cell-number-p
)))
4627 (setq previous-cell-number-p nil
)
4628 (setq previous-cell-number-p t
)
4629 (incf number-cells
))))))
4630 ;; Return value. Alignment specified by cookies has
4631 ;; precedence over alignment deduced from cell's contents.
4634 (cond ((equal cookie-align
"l") 'left
)
4635 ((equal cookie-align
"r") 'right
)
4636 ((equal cookie-align
"c") 'center
)
4637 ((>= (/ (float number-cells
) total-cells
)
4638 org-table-number-fraction
)
4642 (defun org-export-table-cell-borders (table-cell info
)
4643 "Return TABLE-CELL borders.
4645 INFO is a plist used as a communication channel.
4647 Return value is a list of symbols, or nil. Possible values are:
4648 `top', `bottom', `above', `below', `left' and `right'. Note:
4649 `top' (resp. `bottom') only happen for a cell in the first
4650 row (resp. last row) of the table, ignoring table rules, if any.
4652 Returned borders ignore special rows."
4653 (let* ((row (org-export-get-parent table-cell
))
4654 (table (org-export-get-parent-table table-cell
))
4656 ;; Top/above border? TABLE-CELL has a border above when a rule
4657 ;; used to demarcate row groups can be found above. Hence,
4658 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4659 ;; another regular row has to be found above that rule.
4663 (cond ((eq (org-element-property :type row
) 'rule
)
4665 ((not (org-export-table-row-is-special-p row info
))
4666 (if rule-flag
(throw 'exit
(push 'above borders
))
4667 (throw 'exit nil
)))))
4668 ;; Look at every row before the current one.
4669 (cdr (memq row
(reverse (org-element-contents table
)))))
4670 ;; No rule above, or rule found starts the table (ignoring any
4671 ;; special row): TABLE-CELL is at the top of the table.
4672 (when rule-flag
(push 'above borders
))
4673 (push 'top borders
)))
4674 ;; Bottom/below border? TABLE-CELL has a border below when next
4675 ;; non-regular row below is a rule.
4679 (cond ((eq (org-element-property :type row
) 'rule
)
4681 ((not (org-export-table-row-is-special-p row info
))
4682 (if rule-flag
(throw 'exit
(push 'below borders
))
4683 (throw 'exit nil
)))))
4684 ;; Look at every row after the current one.
4685 (cdr (memq row
(org-element-contents table
))))
4686 ;; No rule below, or rule found ends the table (modulo some
4687 ;; special row): TABLE-CELL is at the bottom of the table.
4688 (when rule-flag
(push 'below borders
))
4689 (push 'bottom borders
)))
4690 ;; Right/left borders? They can only be specified by column
4691 ;; groups. Column groups are defined in a row starting with "/".
4692 ;; Also a column groups row only contains "<", "<>", ">" or blank
4695 (let ((column (let ((cells (org-element-contents row
)))
4696 (- (length cells
) (length (memq table-cell cells
))))))
4699 (unless (eq (org-element-property :type row
) 'rule
)
4700 (when (equal (org-element-contents
4701 (car (org-element-contents row
)))
4703 (let ((column-groups
4706 (let ((value (org-element-contents cell
)))
4707 (when (member value
'(("<") ("<>") (">") nil
))
4709 (org-element-contents row
))))
4710 ;; There's a left border when previous cell, if
4711 ;; any, ends a group, or current one starts one.
4712 (when (or (and (not (zerop column
))
4713 (member (elt column-groups
(1- column
))
4715 (member (elt column-groups column
) '("<" "<>")))
4716 (push 'left borders
))
4717 ;; There's a right border when next cell, if any,
4718 ;; starts a group, or current one ends one.
4719 (when (or (and (/= (1+ column
) (length column-groups
))
4720 (member (elt column-groups
(1+ column
))
4722 (member (elt column-groups column
) '(">" "<>")))
4723 (push 'right borders
))
4724 (throw 'exit nil
)))))
4725 ;; Table rows are read in reverse order so last column groups
4726 ;; row has precedence over any previous one.
4727 (reverse (org-element-contents table
)))))
4731 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4732 "Non-nil when TABLE-CELL is at the beginning of a column group.
4733 INFO is a plist used as a communication channel."
4734 ;; A cell starts a column group either when it is at the beginning
4735 ;; of a row (or after the special column, if any) or when it has
4737 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4738 'identity info
'first-match
)
4740 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4742 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4743 "Non-nil when TABLE-CELL is at the end of a column group.
4744 INFO is a plist used as a communication channel."
4745 ;; A cell ends a column group either when it is at the end of a row
4746 ;; or when it has a right border.
4747 (or (eq (car (last (org-element-contents
4748 (org-export-get-parent table-cell
))))
4750 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4752 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4753 "Non-nil when TABLE-ROW is at the beginning of a row group.
4754 INFO is a plist used as a communication channel."
4755 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4756 (org-export-table-row-is-special-p table-row info
))
4757 (let ((borders (org-export-table-cell-borders
4758 (car (org-element-contents table-row
)) info
)))
4759 (or (memq 'top borders
) (memq 'above borders
)))))
4761 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4762 "Non-nil when TABLE-ROW is at the end of a row group.
4763 INFO is a plist used as a communication channel."
4764 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4765 (org-export-table-row-is-special-p table-row info
))
4766 (let ((borders (org-export-table-cell-borders
4767 (car (org-element-contents table-row
)) info
)))
4768 (or (memq 'bottom borders
) (memq 'below borders
)))))
4770 (defun org-export-table-row-starts-header-p (table-row info
)
4771 "Non-nil when TABLE-ROW is the first table header's row.
4772 INFO is a plist used as a communication channel."
4773 (and (org-export-table-has-header-p
4774 (org-export-get-parent-table table-row
) info
)
4775 (org-export-table-row-starts-rowgroup-p table-row info
)
4776 (= (org-export-table-row-group table-row info
) 1)))
4778 (defun org-export-table-row-ends-header-p (table-row info
)
4779 "Non-nil when TABLE-ROW is the last table header's row.
4780 INFO is a plist used as a communication channel."
4781 (and (org-export-table-has-header-p
4782 (org-export-get-parent-table table-row
) info
)
4783 (org-export-table-row-ends-rowgroup-p table-row info
)
4784 (= (org-export-table-row-group table-row info
) 1)))
4786 (defun org-export-table-row-number (table-row info
)
4787 "Return TABLE-ROW number.
4788 INFO is a plist used as a communication channel. Return value is
4789 zero-based and ignores separators. The function returns nil for
4790 special columns and separators."
4791 (when (and (eq (org-element-property :type table-row
) 'standard
)
4792 (not (org-export-table-row-is-special-p table-row info
)))
4794 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4796 (cond ((eq row table-row
) number
)
4797 ((eq (org-element-property :type row
) 'standard
)
4798 (incf number
) nil
)))
4799 info
'first-match
))))
4801 (defun org-export-table-dimensions (table info
)
4802 "Return TABLE dimensions.
4804 INFO is a plist used as a communication channel.
4806 Return value is a CONS like (ROWS . COLUMNS) where
4807 ROWS (resp. COLUMNS) is the number of exportable
4808 rows (resp. columns)."
4809 (let (first-row (columns 0) (rows 0))
4810 ;; Set number of rows, and extract first one.
4811 (org-element-map table
'table-row
4813 (when (eq (org-element-property :type row
) 'standard
)
4815 (unless first-row
(setq first-row row
)))) info
)
4816 ;; Set number of columns.
4817 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4819 (cons rows columns
)))
4821 (defun org-export-table-cell-address (table-cell info
)
4822 "Return address of a regular TABLE-CELL object.
4824 TABLE-CELL is the cell considered. INFO is a plist used as
4825 a communication channel.
4827 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4828 zero-based index. Only exportable cells are considered. The
4829 function returns nil for other cells."
4830 (let* ((table-row (org-export-get-parent table-cell
))
4831 (row-number (org-export-table-row-number table-row info
)))
4834 (let ((col-count 0))
4835 (org-element-map table-row
'table-cell
4837 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4838 info
'first-match
))))))
4840 (defun org-export-get-table-cell-at (address table info
)
4841 "Return regular table-cell object at ADDRESS in TABLE.
4843 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4844 zero-based index. TABLE is a table type element. INFO is
4845 a plist used as a communication channel.
4847 If no table-cell, among exportable cells, is found at ADDRESS,
4849 (let ((column-pos (cdr address
)) (column-count 0))
4851 ;; Row at (car address) or nil.
4852 (let ((row-pos (car address
)) (row-count 0))
4853 (org-element-map table
'table-row
4855 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4856 ((= row-count row-pos
) row
)
4857 (t (incf row-count
) nil
)))
4861 (if (= column-count column-pos
) cell
4862 (incf column-count
) nil
))
4863 info
'first-match
)))
4866 ;;;; For Tables Of Contents
4868 ;; `org-export-collect-headlines' builds a list of all exportable
4869 ;; headline elements, maybe limited to a certain depth. One can then
4870 ;; easily parse it and transcode it.
4872 ;; Building lists of tables, figures or listings is quite similar.
4873 ;; Once the generic function `org-export-collect-elements' is defined,
4874 ;; `org-export-collect-tables', `org-export-collect-figures' and
4875 ;; `org-export-collect-listings' can be derived from it.
4877 (defun org-export-collect-headlines (info &optional n
)
4878 "Collect headlines in order to build a table of contents.
4880 INFO is a plist used as a communication channel.
4882 When optional argument N is an integer, it specifies the depth of
4883 the table of contents. Otherwise, it is set to the value of the
4884 last headline level. See `org-export-headline-levels' for more
4887 Return a list of all exportable headlines as parsed elements.
4888 Footnote sections, if any, will be ignored."
4889 (let ((limit (plist-get info
:headline-levels
)))
4890 (setq n
(if (wholenump n
) (min n limit
) limit
))
4891 (org-element-map (plist-get info
:parse-tree
) 'headline
4892 #'(lambda (headline)
4893 (unless (org-element-property :footnote-section-p headline
)
4894 (let ((level (org-export-get-relative-level headline info
)))
4895 (and (<= level n
) headline
))))
4898 (defun org-export-collect-elements (type info
&optional predicate
)
4899 "Collect referenceable elements of a determined type.
4901 TYPE can be a symbol or a list of symbols specifying element
4902 types to search. Only elements with a caption are collected.
4904 INFO is a plist used as a communication channel.
4906 When non-nil, optional argument PREDICATE is a function accepting
4907 one argument, an element of type TYPE. It returns a non-nil
4908 value when that element should be collected.
4910 Return a list of all elements found, in order of appearance."
4911 (org-element-map (plist-get info
:parse-tree
) type
4913 (and (org-element-property :caption element
)
4914 (or (not predicate
) (funcall predicate element
))
4918 (defun org-export-collect-tables (info)
4919 "Build a list of tables.
4920 INFO is a plist used as a communication channel.
4922 Return a list of table elements with a caption."
4923 (org-export-collect-elements 'table info
))
4925 (defun org-export-collect-figures (info predicate
)
4926 "Build a list of figures.
4928 INFO is a plist used as a communication channel. PREDICATE is
4929 a function which accepts one argument: a paragraph element and
4930 whose return value is non-nil when that element should be
4933 A figure is a paragraph type element, with a caption, verifying
4934 PREDICATE. The latter has to be provided since a \"figure\" is
4935 a vague concept that may depend on back-end.
4937 Return a list of elements recognized as figures."
4938 (org-export-collect-elements 'paragraph info predicate
))
4940 (defun org-export-collect-listings (info)
4941 "Build a list of src blocks.
4943 INFO is a plist used as a communication channel.
4945 Return a list of src-block elements with a caption."
4946 (org-export-collect-elements 'src-block info
))
4951 ;; The main function for the smart quotes sub-system is
4952 ;; `org-export-activate-smart-quotes', which replaces every quote in
4953 ;; a given string from the parse tree with its "smart" counterpart.
4955 ;; Dictionary for smart quotes is stored in
4956 ;; `org-export-smart-quotes-alist'.
4958 ;; Internally, regexps matching potential smart quotes (checks at
4959 ;; string boundaries are also necessary) are defined in
4960 ;; `org-export-smart-quotes-regexps'.
4962 (defconst org-export-smart-quotes-alist
4964 ;; one may use: »...«, "...", ›...‹, or '...'.
4965 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4966 ;; LaTeX quotes require Babel!
4967 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
4968 :texinfo
"@guillemetright{}")
4969 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
4970 :texinfo
"@guillemetleft{}")
4971 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
4972 :texinfo
"@guilsinglright{}")
4973 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
4974 :texinfo
"@guilsingleft{}")
4975 (apostrophe :utf-8
"’" :html
"’"))
4977 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4978 :texinfo
"@quotedblbase{}")
4979 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4980 :texinfo
"@quotedblleft{}")
4981 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4982 :texinfo
"@quotesinglbase{}")
4983 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4984 :texinfo
"@quoteleft{}")
4985 (apostrophe :utf-8
"’" :html
"’"))
4987 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4988 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4989 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4990 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4991 (apostrophe :utf-8
"’" :html
"’"))
4993 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4994 :texinfo
"@guillemetleft{}")
4995 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4996 :texinfo
"@guillemetright{}")
4997 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4998 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4999 (apostrophe :utf-8
"’" :html
"’"))
5001 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
5002 :texinfo
"@guillemetleft{}@tie{}")
5003 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5004 :texinfo
"@tie{}@guillemetright{}")
5005 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
5006 :texinfo
"@guillemetleft{}@tie{}")
5007 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5008 :texinfo
"@tie{}@guillemetright{}")
5009 (apostrophe :utf-8
"’" :html
"’"))
5011 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5012 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5013 :texinfo
"@guillemetleft{}")
5014 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5015 :texinfo
"@guillemetright{}")
5016 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5017 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5018 (apostrophe :utf-8
"’" :html
"’"))
5020 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5021 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5022 :texinfo
"@guillemetleft{}")
5023 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5024 :texinfo
"@guillemetright{}")
5025 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5026 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5027 (apostrophe :utf-8
"’" :html
"’"))
5029 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5030 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5031 :texinfo
"@guillemetleft{}")
5032 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5033 :texinfo
"@guillemetright{}")
5034 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5035 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5036 (apostrophe :utf-8
"’" :html
"’"))
5038 ;; 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
5039 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5040 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
5041 :texinfo
"@guillemetleft{}")
5042 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
5043 :texinfo
"@guillemetright{}")
5044 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
5045 :texinfo
"@quotedblbase{}")
5046 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
5047 :texinfo
"@quotedblleft{}")
5048 (apostrophe :utf-8
"’" :html
: "'"))
5050 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5051 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5052 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5053 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5054 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5055 (apostrophe :utf-8
"’" :html
"’"))
5057 "Smart quotes translations.
5059 Alist whose CAR is a language string and CDR is an alist with
5060 quote type as key and a plist associating various encodings to
5061 their translation as value.
5063 A quote type can be any symbol among `opening-double-quote',
5064 `closing-double-quote', `opening-single-quote',
5065 `closing-single-quote' and `apostrophe'.
5067 Valid encodings include `:utf-8', `:html', `:latex' and
5070 If no translation is found, the quote character is left as-is.")
5072 (defconst org-export-smart-quotes-regexps
5074 ;; Possible opening quote at beginning of string.
5075 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
5076 ;; Possible closing quote at beginning of string.
5077 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5078 ;; Possible apostrophe at beginning of string.
5080 ;; Opening single and double quotes.
5081 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5082 ;; Closing single and double quotes.
5083 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5086 ;; Possible opening quote at end of string.
5087 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5088 ;; Possible closing quote at end of string.
5089 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5090 ;; Possible apostrophe at end of string.
5092 "List of regexps matching a quote or an apostrophe.
5093 In every regexp, quote or apostrophe matched is put in group 1.")
5095 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5096 "Replace regular quotes with \"smart\" quotes in string S.
5098 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5099 `:utf-8'. INFO is a plist used as a communication channel.
5101 The function has to retrieve information about string
5102 surroundings in parse tree. It can only happen with an
5103 unmodified string. Thus, if S has already been through another
5104 process, a non-nil ORIGINAL optional argument will provide that
5107 Return the new string."
5109 (let* ((prev (org-export-get-previous-element (or original s
) info
))
5110 ;; Try to be flexible when computing number of blanks
5111 ;; before object. The previous object may be a string
5112 ;; introduced by the back-end and not completely parsed.
5113 (pre-blank (and prev
5114 (or (org-element-property :post-blank prev
)
5115 ;; A string with missing `:post-blank'
5118 (string-match " *\\'" prev
)
5119 (length (match-string 0 prev
)))
5122 (next (org-export-get-next-element (or original s
) info
))
5125 ;; Return smart quote associated to a give quote Q, as
5126 ;; a string. TYPE is a symbol among `open', `close' and
5128 (let ((key (case type
5129 (apostrophe 'apostrophe
)
5130 (open (if (equal "'" q
) 'opening-single-quote
5131 'opening-double-quote
))
5132 (otherwise (if (equal "'" q
) 'closing-single-quote
5133 'closing-double-quote
)))))
5136 (cdr (assoc (plist-get info
:language
)
5137 org-export-smart-quotes-alist
))))
5140 (if (or (equal "\"" s
) (equal "'" s
))
5141 ;; Only a quote: no regexp can match. We have to check both
5142 ;; sides and decide what to do.
5143 (cond ((and (not prev
) (not next
)) s
)
5144 ((not prev
) (funcall get-smart-quote s
'open
))
5145 ((and (not next
) (zerop pre-blank
))
5146 (funcall get-smart-quote s
'close
))
5148 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
5149 (t (funcall get-smart-quote
'open
)))
5150 ;; 1. Replace quote character at the beginning of S.
5153 ((and prev
(zerop pre-blank
)
5154 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
5155 (setq s
(replace-match
5156 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5159 ((and prev
(zerop pre-blank
)
5160 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
5161 (setq s
(replace-match
5162 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5165 ((and (or (not prev
) (> pre-blank
0))
5166 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
5167 (setq s
(replace-match
5168 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5170 ;; 2. Replace quotes in the middle of the string.
5171 (setq s
(replace-regexp-in-string
5173 (nth 3 org-export-smart-quotes-regexps
)
5175 (funcall get-smart-quote
(match-string 1 text
) 'open
))
5177 (setq s
(replace-regexp-in-string
5179 (nth 4 org-export-smart-quotes-regexps
)
5181 (funcall get-smart-quote
(match-string 1 text
) 'close
))
5183 (setq s
(replace-regexp-in-string
5185 (nth 5 org-export-smart-quotes-regexps
)
5187 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
5189 ;; 3. Replace quote character at the end of S.
5192 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
5193 (setq s
(replace-match
5194 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5198 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
5199 (setq s
(replace-match
5200 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5203 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
5204 (setq s
(replace-match
5205 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5207 ;; Return string with smart quotes.
5212 ;; Here are various functions to retrieve information about the
5213 ;; neighborhood of a given element or object. Neighbors of interest
5214 ;; are direct parent (`org-export-get-parent'), parent headline
5215 ;; (`org-export-get-parent-headline'), first element containing an
5216 ;; object, (`org-export-get-parent-element'), parent table
5217 ;; (`org-export-get-parent-table'), previous element or object
5218 ;; (`org-export-get-previous-element') and next element or object
5219 ;; (`org-export-get-next-element').
5221 ;; `org-export-get-genealogy' returns the full genealogy of a given
5222 ;; element or object, from closest parent to full parse tree.
5224 (defun org-export-get-genealogy (blob)
5225 "Return full genealogy relative to a given element or object.
5227 BLOB is the element or object being considered.
5229 Ancestors are returned from closest to farthest, the last one
5230 being the full parse tree."
5231 (let (genealogy (parent blob
))
5232 (while (setq parent
(org-element-property :parent parent
))
5233 (push parent genealogy
))
5234 (nreverse genealogy
)))
5236 (defun org-export-get-parent-headline (blob)
5237 "Return BLOB parent headline or nil.
5238 BLOB is the element or object being considered."
5239 (let ((parent blob
))
5240 (while (and (setq parent
(org-element-property :parent parent
))
5241 (not (eq (org-element-type parent
) 'headline
))))
5244 (defun org-export-get-parent-element (object)
5245 "Return first element containing OBJECT or nil.
5246 OBJECT is the object to consider."
5247 (let ((parent object
))
5248 (while (and (setq parent
(org-element-property :parent parent
))
5249 (memq (org-element-type parent
) org-element-all-objects
)))
5252 (defun org-export-get-parent-table (object)
5253 "Return OBJECT parent table or nil.
5254 OBJECT is either a `table-cell' or `table-element' type object."
5255 (let ((parent object
))
5256 (while (and (setq parent
(org-element-property :parent parent
))
5257 (not (eq (org-element-type parent
) 'table
))))
5260 (defun org-export-get-previous-element (blob info
&optional n
)
5261 "Return previous element or object.
5263 BLOB is an element or object. INFO is a plist used as
5264 a communication channel. Return previous exportable element or
5265 object, a string, or nil.
5267 When optional argument N is a positive integer, return a list
5268 containing up to N siblings before BLOB, from farthest to
5269 closest. With any other non-nil value, return a list containing
5271 (let* ((secondary (org-element-secondary-p blob
))
5272 (parent (org-export-get-parent blob
))
5274 (if secondary
(org-element-property secondary parent
)
5275 (org-element-contents parent
)))
5278 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5279 (cond ((memq obj
(plist-get info
:ignore-list
)))
5280 ((null n
) (throw 'exit obj
))
5281 ((not (wholenump n
)) (push obj prev
))
5282 ((zerop n
) (throw 'exit prev
))
5283 (t (decf n
) (push obj prev
)))))))
5285 (defun org-export-get-next-element (blob info
&optional n
)
5286 "Return next element or object.
5288 BLOB is an element or object. INFO is a plist used as
5289 a communication channel. Return next exportable element or
5290 object, a string, or nil.
5292 When optional argument N is a positive integer, return a list
5293 containing up to N siblings after BLOB, from closest to farthest.
5294 With any other non-nil value, return a list containing all of
5296 (let* ((secondary (org-element-secondary-p blob
))
5297 (parent (org-export-get-parent blob
))
5300 (if secondary
(org-element-property secondary parent
)
5301 (org-element-contents parent
)))))
5304 (dolist (obj siblings
(nreverse next
))
5305 (cond ((memq obj
(plist-get info
:ignore-list
)))
5306 ((null n
) (throw 'exit obj
))
5307 ((not (wholenump n
)) (push obj next
))
5308 ((zerop n
) (throw 'exit
(nreverse next
)))
5309 (t (decf n
) (push obj next
)))))))
5314 ;; `org-export-translate' translates a string according to the language
5315 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5316 ;; the dictionary used for the translation.
5318 (defconst org-export-dictionary
5320 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5322 ("ca" :default
"Autor")
5323 ("cs" :default
"Autor")
5324 ("da" :default
"Forfatter")
5325 ("de" :default
"Autor")
5326 ("eo" :html
"Aŭtoro")
5327 ("es" :default
"Autor")
5328 ("et" :default
"Autor")
5329 ("fi" :html
"Tekijä")
5330 ("fr" :default
"Auteur")
5331 ("hu" :default
"Szerzõ")
5332 ("is" :html
"Höfundur")
5333 ("it" :default
"Autore")
5334 ("ja" :default
"著者" :html
"著者")
5335 ("nl" :default
"Auteur")
5336 ("no" :default
"Forfatter")
5337 ("nb" :default
"Forfatter")
5338 ("nn" :default
"Forfattar")
5339 ("pl" :default
"Autor")
5340 ("ru" :html
"Автор" :utf-8
"Автор")
5341 ("sv" :html
"Författare")
5342 ("uk" :html
"Автор" :utf-8
"Автор")
5343 ("zh-CN" :html
"作者" :utf-8
"作者")
5344 ("zh-TW" :html
"作者" :utf-8
"作者"))
5345 ("Continued from previous page"
5346 ("de" :default
"Fortsetzung von vorheriger Seite")
5347 ("es" :default
"Continúa de la página anterior")
5348 ("fr" :default
"Suite de la page précédente")
5349 ("it" :default
"Continua da pagina precedente")
5350 ("ja" :default
"前ページからの続き")
5351 ("nl" :default
"Vervolg van vorige pagina")
5352 ("pt" :default
"Continuação da página anterior")
5353 ("ru" :html
"(Продолжение)"
5354 :utf-8
"(Продолжение)"))
5355 ("Continued on next page"
5356 ("de" :default
"Fortsetzung nächste Seite")
5357 ("es" :default
"Continúa en la siguiente página")
5358 ("fr" :default
"Suite page suivante")
5359 ("it" :default
"Continua alla pagina successiva")
5360 ("ja" :default
"次ページに続く")
5361 ("nl" :default
"Vervolg op volgende pagina")
5362 ("pt" :default
"Continua na página seguinte")
5363 ("ru" :html
"(Продолжение следует)"
5364 :utf-8
"(Продолжение следует)"))
5366 ("ca" :default
"Data")
5367 ("cs" :default
"Datum")
5368 ("da" :default
"Dato")
5369 ("de" :default
"Datum")
5370 ("eo" :default
"Dato")
5371 ("es" :default
"Fecha")
5372 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5373 ("fi" :html
"Päivämäärä")
5374 ("hu" :html
"Dátum")
5375 ("is" :default
"Dagsetning")
5376 ("it" :default
"Data")
5377 ("ja" :default
"日付" :html
"日付")
5378 ("nl" :default
"Datum")
5379 ("no" :default
"Dato")
5380 ("nb" :default
"Dato")
5381 ("nn" :default
"Dato")
5382 ("pl" :default
"Data")
5383 ("ru" :html
"Дата" :utf-8
"Дата")
5384 ("sv" :default
"Datum")
5385 ("uk" :html
"Дата" :utf-8
"Дата")
5386 ("zh-CN" :html
"日期" :utf-8
"日期")
5387 ("zh-TW" :html
"日期" :utf-8
"日期"))
5389 ("da" :default
"Ligning")
5390 ("de" :default
"Gleichung")
5391 ("es" :html
"Ecuación" :default
"Ecuación")
5392 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5393 ("fr" :ascii
"Equation" :default
"Équation")
5394 ("ja" :default
"方程式")
5395 ("no" :default
"Ligning")
5396 ("nb" :default
"Ligning")
5397 ("nn" :default
"Likning")
5398 ("ru" :html
"Уравнение"
5400 ("sv" :default
"Ekvation")
5401 ("zh-CN" :html
"方程" :utf-8
"方程"))
5403 ("da" :default
"Figur")
5404 ("de" :default
"Abbildung")
5405 ("es" :default
"Figura")
5406 ("et" :default
"Joonis")
5407 ("ja" :default
"図" :html
"図")
5408 ("no" :default
"Illustrasjon")
5409 ("nb" :default
"Illustrasjon")
5410 ("nn" :default
"Illustrasjon")
5411 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5412 ("sv" :default
"Illustration")
5413 ("zh-CN" :html
"图" :utf-8
"图"))
5415 ("da" :default
"Figur %d")
5416 ("de" :default
"Abbildung %d:")
5417 ("es" :default
"Figura %d:")
5418 ("et" :default
"Joonis %d:")
5419 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5420 ("ja" :default
"図%d: " :html
"図%d: ")
5421 ("no" :default
"Illustrasjon %d")
5422 ("nb" :default
"Illustrasjon %d")
5423 ("nn" :default
"Illustrasjon %d")
5424 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5425 ("sv" :default
"Illustration %d")
5426 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5428 ("ca" :html
"Peus de pàgina")
5429 ("cs" :default
"Pozn\xe1mky pod carou")
5430 ("da" :default
"Fodnoter")
5431 ("de" :html
"Fußnoten" :default
"Fußnoten")
5432 ("eo" :default
"Piednotoj")
5433 ("es" :html
"Nota al pie de página" :default
"Nota al pie de página")
5434 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5435 ("fi" :default
"Alaviitteet")
5436 ("fr" :default
"Notes de bas de page")
5437 ("hu" :html
"Lábjegyzet")
5438 ("is" :html
"Aftanmálsgreinar")
5439 ("it" :html
"Note a piè di pagina")
5440 ("ja" :default
"脚注" :html
"脚注")
5441 ("nl" :default
"Voetnoten")
5442 ("no" :default
"Fotnoter")
5443 ("nb" :default
"Fotnoter")
5444 ("nn" :default
"Fotnotar")
5445 ("pl" :default
"Przypis")
5446 ("ru" :html
"Сноски" :utf-8
"Сноски")
5447 ("sv" :default
"Fotnoter")
5448 ("uk" :html
"Примітки"
5450 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5451 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5453 ("da" :default
"Programmer")
5454 ("de" :default
"Programmauflistungsverzeichnis")
5455 ("es" :default
"Indice de Listados de programas")
5456 ("et" :default
"Loendite nimekiri")
5457 ("fr" :default
"Liste des programmes")
5458 ("ja" :default
"ソースコード目次")
5459 ("no" :default
"Dataprogrammer")
5460 ("nb" :default
"Dataprogrammer")
5461 ("ru" :html
"Список распечаток"
5462 :utf-8
"Список распечаток")
5463 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5465 ("da" :default
"Tabeller")
5466 ("de" :default
"Tabellenverzeichnis")
5467 ("es" :default
"Indice de tablas")
5468 ("et" :default
"Tabelite nimekiri")
5469 ("fr" :default
"Liste des tableaux")
5470 ("ja" :default
"表目次")
5471 ("no" :default
"Tabeller")
5472 ("nb" :default
"Tabeller")
5473 ("nn" :default
"Tabeller")
5474 ("ru" :html
"Список таблиц"
5475 :utf-8
"Список таблиц")
5476 ("sv" :default
"Tabeller")
5477 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5479 ("da" :default
"Program %d")
5480 ("de" :default
"Programmlisting %d")
5481 ("es" :default
"Listado de programa %d")
5482 ("et" :default
"Loend %d")
5483 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5484 ("ja" :default
"ソースコード%d:")
5485 ("no" :default
"Dataprogram %d")
5486 ("nb" :default
"Dataprogram %d")
5487 ("ru" :html
"Распечатка %d.:"
5488 :utf-8
"Распечатка %d.:")
5489 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5491 ("fr" :ascii
"References" :default
"Références")
5492 ("de" :default
"Quellen"))
5494 ("da" :default
"jævnfør afsnit %s")
5495 ("de" :default
"siehe Abschnitt %s")
5496 ("es" :default
"vea seccion %s")
5497 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5498 ("fr" :default
"cf. section %s")
5499 ("ja" :default
"セクション %s を参照")
5500 ("ru" :html
"См. раздел %s"
5501 :utf-8
"См. раздел %s")
5502 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5504 ("de" :default
"Tabelle")
5505 ("es" :default
"Tabla")
5506 ("et" :default
"Tabel")
5507 ("fr" :default
"Tableau")
5508 ("ja" :default
"表" :html
"表")
5509 ("ru" :html
"Таблица" :utf-8
"Таблица")
5510 ("zh-CN" :html
"表" :utf-8
"表"))
5512 ("da" :default
"Tabel %d")
5513 ("de" :default
"Tabelle %d")
5514 ("es" :default
"Tabla %d")
5515 ("et" :default
"Tabel %d")
5516 ("fr" :default
"Tableau %d :")
5517 ("ja" :default
"表%d:" :html
"表%d:")
5518 ("no" :default
"Tabell %d")
5519 ("nb" :default
"Tabell %d")
5520 ("nn" :default
"Tabell %d")
5521 ("ru" :html
"Таблица %d.:"
5522 :utf-8
"Таблица %d.:")
5523 ("sv" :default
"Tabell %d")
5524 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5525 ("Table of Contents"
5526 ("ca" :html
"Índex")
5527 ("cs" :default
"Obsah")
5528 ("da" :default
"Indhold")
5529 ("de" :default
"Inhaltsverzeichnis")
5530 ("eo" :default
"Enhavo")
5531 ("es" :html
"Índice")
5532 ("et" :default
"Sisukord")
5533 ("fi" :html
"Sisällysluettelo")
5534 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5535 ("hu" :html
"Tartalomjegyzék")
5536 ("is" :default
"Efnisyfirlit")
5537 ("it" :default
"Indice")
5538 ("ja" :default
"目次" :html
"目次")
5539 ("nl" :default
"Inhoudsopgave")
5540 ("no" :default
"Innhold")
5541 ("nb" :default
"Innhold")
5542 ("nn" :default
"Innhald")
5543 ("pl" :html
"Spis treści")
5544 ("ru" :html
"Содержание"
5545 :utf-8
"Содержание")
5546 ("sv" :html
"Innehåll")
5547 ("uk" :html
"Зміст" :utf-8
"Зміст")
5548 ("zh-CN" :html
"目录" :utf-8
"目录")
5549 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5550 ("Unknown reference"
5551 ("da" :default
"ukendt reference")
5552 ("de" :default
"Unbekannter Verweis")
5553 ("es" :default
"referencia desconocida")
5554 ("et" :default
"Tundmatu viide")
5555 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5556 ("ja" :default
"不明な参照先")
5557 ("ru" :html
"Неизвестная ссылка"
5558 :utf-8
"Неизвестная ссылка")
5559 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5560 "Dictionary for export engine.
5562 Alist whose CAR is the string to translate and CDR is an alist
5563 whose CAR is the language string and CDR is a plist whose
5564 properties are possible charsets and values translated terms.
5566 It is used as a database for `org-export-translate'. Since this
5567 function returns the string as-is if no translation was found,
5568 the variable only needs to record values different from the
5571 (defun org-export-translate (s encoding info
)
5572 "Translate string S according to language specification.
5574 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5575 and `:utf-8'. INFO is a plist used as a communication channel.
5577 Translation depends on `:language' property. Return the
5578 translated string. If no translation is found, try to fall back
5579 to `:default' encoding. If it fails, return S."
5580 (let* ((lang (plist-get info
:language
))
5581 (translations (cdr (assoc lang
5582 (cdr (assoc s org-export-dictionary
))))))
5583 (or (plist-get translations encoding
)
5584 (plist-get translations
:default
)
5589 ;;; Asynchronous Export
5591 ;; `org-export-async-start' is the entry point for asynchronous
5592 ;; export. It recreates current buffer (including visibility,
5593 ;; narrowing and visited file) in an external Emacs process, and
5594 ;; evaluates a command there. It then applies a function on the
5595 ;; returned results in the current process.
5597 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5598 ;; allow to export to a buffer or a file, asynchronously or not.
5600 ;; `org-export-output-file-name' is an auxiliary function meant to be
5601 ;; used with `org-export-to-file'. With a given extension, it tries
5602 ;; to provide a canonical file name to write export output to.
5604 ;; Asynchronously generated results are never displayed directly.
5605 ;; Instead, they are stored in `org-export-stack-contents'. They can
5606 ;; then be retrieved by calling `org-export-stack'.
5608 ;; Export Stack is viewed through a dedicated major mode
5609 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5610 ;;`org-export-stack-delete', `org-export-stack-view' and
5611 ;;`org-export-stack-clear'.
5613 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5614 ;; It should be used whenever `org-export-async-start' is called.
5616 (defmacro org-export-async-start
(fun &rest body
)
5617 "Call function FUN on the results returned by BODY evaluation.
5619 BODY evaluation happens in an asynchronous process, from a buffer
5620 which is an exact copy of the current one.
5622 Use `org-export-add-to-stack' in FUN in order to register results
5625 This is a low level function. See also `org-export-to-buffer'
5626 and `org-export-to-file' for more specialized functions."
5627 (declare (indent 1) (debug t
))
5628 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5629 ;; Write the full sexp evaluating BODY in a copy of the current
5630 ;; buffer to a temporary file, as it may be too long for program
5631 ;; args in `start-process'.
5632 `(with-temp-message "Initializing asynchronous export process"
5633 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5634 (,temp-file
(make-temp-file "org-export-process"))
5635 (,coding buffer-file-coding-system
))
5636 (with-temp-file ,temp-file
5638 ;; Null characters (from variable values) are inserted
5639 ;; within the file. As a consequence, coding system for
5640 ;; buffer contents will not be recognized properly. So,
5641 ;; we make sure it is the same as the one used to display
5642 ;; the original buffer.
5643 (format ";; -*- coding: %s; -*-\n%S"
5646 (when org-export-async-debug
'(setq debug-on-error t
))
5647 ;; Ignore `kill-emacs-hook' and code evaluation
5648 ;; queries from Babel as we need a truly
5649 ;; non-interactive process.
5650 (setq kill-emacs-hook nil
5651 org-babel-confirm-evaluate-answer-no t
)
5652 ;; Initialize export framework.
5654 ;; Re-create current buffer there.
5655 (funcall ,,copy-fun
)
5656 (restore-buffer-modified-p nil
)
5657 ;; Sexp to evaluate in the buffer.
5658 (print (progn ,,@body
))))))
5659 ;; Start external process.
5660 (let* ((process-connection-type nil
)
5661 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5666 (list "org-export-process"
5668 (expand-file-name invocation-name invocation-directory
)
5670 (if org-export-async-init-file
5671 (list "-Q" "-l" org-export-async-init-file
)
5672 (list "-l" user-init-file
))
5673 (list "-l" ,temp-file
)))))
5674 ;; Register running process in stack.
5675 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5676 ;; Set-up sentinel in order to catch results.
5677 (let ((handler ,fun
))
5678 (set-process-sentinel
5681 (let ((proc-buffer (process-buffer p
)))
5682 (when (eq (process-status p
) 'exit
)
5684 (if (zerop (process-exit-status p
))
5687 (with-current-buffer proc-buffer
5688 (goto-char (point-max))
5690 (read (current-buffer)))))
5691 (funcall ,handler results
))
5692 (unless org-export-async-debug
5693 (and (get-buffer proc-buffer
)
5694 (kill-buffer proc-buffer
))))
5695 (org-export-add-to-stack proc-buffer nil p
)
5697 (message "Process '%s' exited abnormally" p
))
5698 (unless org-export-async-debug
5699 (delete-file ,,temp-file
)))))))))))))
5702 (defun org-export-to-buffer
5704 &optional async subtreep visible-only body-only ext-plist
5706 "Call `org-export-as' with output to a specified buffer.
5708 BACKEND is either an export back-end, as returned by, e.g.,
5709 `org-export-create-backend', or a symbol referring to
5710 a registered back-end.
5712 BUFFER is the name of the output buffer. If it already exists,
5713 it will be erased first, otherwise, it will be created.
5715 A non-nil optional argument ASYNC means the process should happen
5716 asynchronously. The resulting buffer should then be accessible
5717 through the `org-export-stack' interface. When ASYNC is nil, the
5718 buffer is displayed if `org-export-show-temporary-export-buffer'
5721 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5722 EXT-PLIST are similar to those used in `org-export-as', which
5725 Optional argument POST-PROCESS is a function which should accept
5726 no argument. It is always called within the current process,
5727 from BUFFER, with point at its beginning. Export back-ends can
5728 use it to set a major mode there, e.g,
5730 \(defun org-latex-export-as-latex
5731 \(&optional async subtreep visible-only body-only ext-plist)
5733 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5734 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5736 This function returns BUFFER."
5737 (declare (indent 2))
5739 (org-export-async-start
5741 (with-current-buffer (get-buffer-create ,buffer
)
5743 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5745 (goto-char (point-min))
5746 (org-export-add-to-stack (current-buffer) ',backend
)
5747 (ignore-errors (funcall ,post-process
))))
5749 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5751 (org-export-as backend subtreep visible-only body-only ext-plist
))
5752 (buffer (get-buffer-create buffer
))
5753 (encoding buffer-file-coding-system
))
5754 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5755 (org-kill-new output
))
5756 (with-current-buffer buffer
5758 (setq buffer-file-coding-system encoding
)
5760 (goto-char (point-min))
5761 (and (functionp post-process
) (funcall post-process
)))
5762 (when org-export-show-temporary-export-buffer
5763 (switch-to-buffer-other-window buffer
))
5767 (defun org-export-to-file
5768 (backend file
&optional async subtreep visible-only body-only ext-plist
5770 "Call `org-export-as' with output to a specified file.
5772 BACKEND is either an export back-end, as returned by, e.g.,
5773 `org-export-create-backend', or a symbol referring to
5774 a registered back-end. FILE is the name of the output file, as
5777 A non-nil optional argument ASYNC means the process should happen
5778 asynchronously. The resulting buffer will then be accessible
5779 through the `org-export-stack' interface.
5781 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5782 EXT-PLIST are similar to those used in `org-export-as', which
5785 Optional argument POST-PROCESS is called with FILE as its
5786 argument and happens asynchronously when ASYNC is non-nil. It
5787 has to return a file name, or nil. Export back-ends can use this
5788 to send the output file through additional processing, e.g,
5790 \(defun org-latex-export-to-latex
5791 \(&optional async subtreep visible-only body-only ext-plist)
5793 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5794 \(org-export-to-file 'latex outfile
5795 async subtreep visible-only body-only ext-plist
5796 \(lambda (file) (org-latex-compile file)))
5798 The function returns either a file name returned by POST-PROCESS,
5800 (declare (indent 2))
5801 (if (not (file-writable-p file
)) (error "Output file not writable")
5802 (let ((encoding (or org-export-coding-system buffer-file-coding-system
)))
5804 (org-export-async-start
5806 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5809 ',backend
,subtreep
,visible-only
,body-only
5813 (let ((coding-system-for-write ',encoding
))
5814 (write-file ,file
)))
5815 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5816 (let ((output (org-export-as
5817 backend subtreep visible-only body-only ext-plist
)))
5820 (let ((coding-system-for-write encoding
))
5822 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5823 (org-kill-new output
))
5824 ;; Get proper return value.
5825 (or (and (functionp post-process
) (funcall post-process file
))
5828 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5829 "Return output file's name according to buffer specifications.
5831 EXTENSION is a string representing the output file extension,
5832 with the leading dot.
5834 With a non-nil optional argument SUBTREEP, try to determine
5835 output file's name by looking for \"EXPORT_FILE_NAME\" property
5836 of subtree at point.
5838 When optional argument PUB-DIR is set, use it as the publishing
5841 When optional argument VISIBLE-ONLY is non-nil, don't export
5842 contents of hidden elements.
5844 Return file name as a string."
5845 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5847 ;; File name may come from EXPORT_FILE_NAME subtree
5848 ;; property, assuming point is at beginning of said
5850 (file-name-sans-extension
5854 (ignore-errors (org-back-to-heading) (point)))
5855 "EXPORT_FILE_NAME" t
))
5856 ;; File name may be extracted from buffer's associated
5858 (and visited-file
(file-name-nondirectory visited-file
))
5859 ;; Can't determine file name on our own: Ask user.
5860 (let ((read-file-name-function
5861 (and org-completion-use-ido
'ido-read-file-name
)))
5863 "Output file: " pub-dir nil nil nil
5865 (string= (file-name-extension name t
) extension
)))))))
5867 ;; Build file name. Enforce EXTENSION over whatever user
5868 ;; may have come up with. PUB-DIR, if defined, always has
5869 ;; precedence over any provided path.
5872 (concat (file-name-as-directory pub-dir
)
5873 (file-name-nondirectory base-name
)
5875 ((file-name-absolute-p base-name
) (concat base-name extension
))
5876 (t (concat (file-name-as-directory ".") base-name extension
)))))
5877 ;; If writing to OUTPUT-FILE would overwrite original file, append
5878 ;; EXTENSION another time to final name.
5879 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5880 (concat output-file extension
)
5883 (defun org-export-add-to-stack (source backend
&optional process
)
5884 "Add a new result to export stack if not present already.
5886 SOURCE is a buffer or a file name containing export results.
5887 BACKEND is a symbol representing export back-end used to generate
5890 Entries already pointing to SOURCE and unavailable entries are
5891 removed beforehand. Return the new stack."
5892 (setq org-export-stack-contents
5893 (cons (list source backend
(or process
(current-time)))
5894 (org-export-stack-remove source
))))
5896 (defun org-export-stack ()
5897 "Menu for asynchronous export results and running processes."
5899 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5901 (when (zerop (buffer-size)) (org-export-stack-mode))
5902 (org-export-stack-refresh)
5903 (pop-to-buffer buffer
))
5904 (message "Type \"q\" to quit, \"?\" for help"))
5906 (defun org-export--stack-source-at-point ()
5907 "Return source from export results at point in stack."
5908 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5909 (if (not source
) (error "Source unavailable, please refresh buffer")
5910 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5913 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5915 ;; SOURCE is not consistent with current line. The stack
5916 ;; view is outdated.
5917 (error "Source unavailable; type `g' to update buffer"))))))
5919 (defun org-export-stack-clear ()
5920 "Remove all entries from export stack."
5922 (setq org-export-stack-contents nil
))
5924 (defun org-export-stack-refresh (&rest dummy
)
5925 "Refresh the asynchronous export stack.
5926 DUMMY is ignored. Unavailable sources are removed from the list.
5927 Return the new stack."
5928 (let ((inhibit-read-only t
))
5935 (let ((proc-p (processp (nth 2 entry
))))
5938 (format " %-12s " (or (nth 1 entry
) ""))
5940 (let ((data (nth 2 entry
)))
5941 (if proc-p
(format " %6s " (process-status data
))
5942 ;; Compute age of the results.
5945 (float-time (time-since data
)))))
5948 (let ((source (car entry
)))
5949 (if (stringp source
) source
5950 (buffer-name source
)))))))
5951 ;; Clear stack from exited processes, dead buffers or
5952 ;; non-existent files.
5953 (setq org-export-stack-contents
5956 (if (processp (nth 2 el
))
5957 (buffer-live-p (process-buffer (nth 2 el
)))
5958 (let ((source (car el
)))
5959 (if (bufferp source
) (buffer-live-p source
)
5960 (file-exists-p source
)))))
5961 org-export-stack-contents
)) "\n")))))))
5963 (defun org-export-stack-remove (&optional source
)
5964 "Remove export results at point from stack.
5965 If optional argument SOURCE is non-nil, remove it instead."
5967 (let ((source (or source
(org-export--stack-source-at-point))))
5968 (setq org-export-stack-contents
5969 (org-remove-if (lambda (el) (equal (car el
) source
))
5970 org-export-stack-contents
))))
5972 (defun org-export-stack-view (&optional in-emacs
)
5973 "View export results at point in stack.
5974 With an optional prefix argument IN-EMACS, force viewing files
5977 (let ((source (org-export--stack-source-at-point)))
5978 (cond ((processp source
)
5979 (org-switch-to-buffer-other-window (process-buffer source
)))
5980 ((bufferp source
) (org-switch-to-buffer-other-window source
))
5981 (t (org-open-file source in-emacs
)))))
5983 (defvar org-export-stack-mode-map
5984 (let ((km (make-sparse-keymap)))
5985 (define-key km
" " 'next-line
)
5986 (define-key km
"n" 'next-line
)
5987 (define-key km
"\C-n" 'next-line
)
5988 (define-key km
[down] 'next-line)
5989 (define-key km "p" 'previous-line)
5990 (define-key km "\C-p" 'previous-line)
5991 (define-key km "\C-?" 'previous-line)
5992 (define-key km [up] 'previous-line)
5993 (define-key km "C" 'org-export-stack-clear)
5994 (define-key km "v" 'org-export-stack-view)
5995 (define-key km (kbd "RET") 'org-export-stack-view)
5996 (define-key km "d" 'org-export-stack-remove)
5998 "Keymap for Org Export Stack.")
6000 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
6001 "Mode for displaying asynchronous export stack.
6003 Type \\[org-export-stack] to visualize the asynchronous export
6006 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6007 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6010 Removing entries in an Org Export Stack buffer doesn't affect
6011 files or buffers, only the display.
6013 \\{org-export-stack-mode-map}"
6016 (setq buffer-read-only t
6021 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6022 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6023 (set (make-local-variable 'revert-buffer-function)
6024 'org-export-stack-refresh))
6030 ;; `org-export-dispatch' is the standard interactive way to start an
6031 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6032 ;; for its interface, which, in turn, delegates response to key
6033 ;; pressed to `org-export--dispatch-action'.
6036 (defun org-export-dispatch (&optional arg)
6037 "Export dispatcher for Org mode.
6039 It provides an access to common export related tasks in a buffer.
6040 Its interface comes in two flavors: standard and expert.
6042 While both share the same set of bindings, only the former
6043 displays the valid keys associations in a dedicated buffer.
6044 Scrolling (resp. line-wise motion) in this buffer is done with
6045 SPC and DEL (resp. C-n and C-p) keys.
6047 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6048 flavor or the other.
6050 When ARG is \\[universal-argument], repeat the last export action, with the same set
6051 of options used back then, on the current buffer.
6053 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6056 (cond ((equal arg '(16)) '(stack))
6057 ((and arg org-export-dispatch-last-action))
6058 (t (save-window-excursion
6061 ;; Remember where we are
6062 (move-marker org-export-dispatch-last-position
6064 (org-base-buffer (current-buffer)))
6065 ;; Get and store an export command
6066 (setq org-export-dispatch-last-action
6067 (org-export--dispatch-ui
6068 (list org-export-initial-scope
6069 (and org-export-in-background 'async))
6071 org-export-dispatch-use-expert-ui)))
6072 (and (get-buffer "*Org Export Dispatcher*")
6073 (kill-buffer "*Org Export Dispatcher*")))))))
6074 (action (car input))
6075 (optns (cdr input)))
6076 (unless (memq 'subtree optns)
6077 (move-marker org-export-dispatch-last-position nil))
6079 ;; First handle special hard-coded actions.
6080 (template (org-export-insert-default-template nil optns))
6081 (stack (org-export-stack))
6082 (publish-current-file
6083 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6084 (publish-current-project
6085 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6086 (publish-choose-project
6087 (org-publish (assoc (org-icompleting-read
6089 org-publish-project-alist nil t)
6090 org-publish-project-alist)
6092 (memq 'async optns)))
6093 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6097 ;; Repeating command, maybe move cursor to restore subtree
6099 (if (eq (marker-buffer org-export-dispatch-last-position)
6100 (org-base-buffer (current-buffer)))
6101 (goto-char org-export-dispatch-last-position)
6102 ;; We are in a different buffer, forget position.
6103 (move-marker org-export-dispatch-last-position nil)))
6105 ;; Return a symbol instead of a list to ease
6106 ;; asynchronous export macro use.
6107 (and (memq 'async optns) t)
6108 (and (memq 'subtree optns) t)
6109 (and (memq 'visible optns) t)
6110 (and (memq 'body optns) t)))))))
6112 (defun org-export--dispatch-ui (options first-key expertp)
6113 "Handle interface for `org-export-dispatch'.
6115 OPTIONS is a list containing current interactive options set for
6116 export. It can contain any of the following symbols:
6117 `body' toggles a body-only export
6118 `subtree' restricts export to current subtree
6119 `visible' restricts export to visible part of buffer.
6120 `force' force publishing files.
6121 `async' use asynchronous export process
6123 FIRST-KEY is the key pressed to select the first level menu. It
6124 is nil when this menu hasn't been selected yet.
6126 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6127 buffer is provided, but indications about currently active
6128 options are given in the prompt. Moreover, \[?] allows to switch
6129 back to standard interface."
6131 (lambda (key &optional access-key)
6132 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6133 ;; non-nil is the required first-level key to activate
6134 ;; KEY. When its value is t, activate KEY independently
6135 ;; on the first key, if any. A nil value means KEY will
6136 ;; only be activated at first level.
6137 (if (or (eq access-key t) (eq access-key first-key))
6138 (org-propertize key 'face 'org-warning)
6142 ;; Fontify VALUE string.
6143 (org-propertize value 'face 'font-lock-variable-name-face)))
6144 ;; Prepare menu entries by extracting them from registered
6145 ;; back-ends and sorting them by access key and by ordinal,
6148 (sort (sort (delq nil
6149 (mapcar 'org-export-backend-menu
6150 org-export--registered-backends))
6152 (let ((key-a (nth 1 a))
6154 (cond ((and (numberp key-a) (numberp key-b))
6156 ((numberp key-b) t)))))
6157 'car-less-than-car))
6158 ;; Compute a list of allowed keys based on the first key
6159 ;; pressed, if any. Some keys
6160 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6163 (nconc (list 2 22 19 6 1)
6164 (if (not first-key) (org-uniquify (mapcar 'car entries))
6166 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6167 (when (eq (car entry) first-key)
6168 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6169 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6170 ((not first-key) (list ?P)))
6172 (when expertp (list ??))
6174 ;; Build the help menu for standard UI.
6178 ;; Options are hard-coded.
6179 (format "[%s] Body only: %s [%s] Visible only: %s
6180 \[%s] Export scope: %s [%s] Force publishing: %s
6181 \[%s] Async export: %s\n\n"
6182 (funcall fontify-key "C-b" t)
6183 (funcall fontify-value
6184 (if (memq 'body options) "On " "Off"))
6185 (funcall fontify-key "C-v" t)
6186 (funcall fontify-value
6187 (if (memq 'visible options) "On " "Off"))
6188 (funcall fontify-key "C-s" t)
6189 (funcall fontify-value
6190 (if (memq 'subtree options) "Subtree" "Buffer "))
6191 (funcall fontify-key "C-f" t)
6192 (funcall fontify-value
6193 (if (memq 'force options) "On " "Off"))
6194 (funcall fontify-key "C-a" t)
6195 (funcall fontify-value
6196 (if (memq 'async options) "On " "Off")))
6197 ;; Display registered back-end entries. When a key
6198 ;; appears for the second time, do not create another
6199 ;; entry, but append its sub-menu to existing menu.
6203 (let ((top-key (car entry)))
6205 (unless (eq top-key last-key)
6206 (setq last-key top-key)
6207 (format "\n[%s] %s\n"
6208 (funcall fontify-key (char-to-string top-key))
6210 (let ((sub-menu (nth 2 entry)))
6211 (unless (functionp sub-menu)
6212 ;; Split sub-menu into two columns.
6219 (if (zerop (mod index 2)) " [%s] %-26s"
6221 (funcall fontify-key
6222 (char-to-string (car sub-entry))
6226 (when (zerop (mod index 2)) "\n"))))))))
6228 ;; Publishing menu is hard-coded.
6229 (format "\n[%s] Publish
6230 [%s] Current file [%s] Current project
6231 [%s] Choose project [%s] All projects\n\n\n"
6232 (funcall fontify-key "P")
6233 (funcall fontify-key "f" ?P)
6234 (funcall fontify-key "p" ?P)
6235 (funcall fontify-key "x" ?P)
6236 (funcall fontify-key "a" ?P))
6237 (format "[%s] Export stack [%s] Insert template\n"
6238 (funcall fontify-key "&" t)
6239 (funcall fontify-key "#" t))
6241 (funcall fontify-key "q" t)
6242 (if first-key "Main menu" "Exit")))))
6243 ;; Build prompts for both standard and expert UI.
6244 (standard-prompt (unless expertp "Export command: "))
6248 "Export command (C-%s%s%s%s%s) [%s]: "
6249 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6250 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6251 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6252 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6253 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6254 (mapconcat (lambda (k)
6255 ;; Strip control characters.
6256 (unless (< k 27) (char-to-string k)))
6257 allowed-keys "")))))
6258 ;; With expert UI, just read key with a fancy prompt. In standard
6259 ;; UI, display an intrusive help buffer.
6261 (org-export--dispatch-action
6262 expert-prompt allowed-keys entries options first-key expertp)
6263 ;; At first call, create frame layout in order to display menu.
6264 (unless (get-buffer "*Org Export Dispatcher*")
6265 (delete-other-windows)
6266 (org-switch-to-buffer-other-window
6267 (get-buffer-create "*Org Export Dispatcher*"))
6268 (setq cursor-type nil
6269 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6270 ;; Make sure that invisible cursor will not highlight square
6272 (set-syntax-table (copy-syntax-table))
6273 (modify-syntax-entry ?\[ "w"))
6274 ;; At this point, the buffer containing the menu exists and is
6275 ;; visible in the current window. So, refresh it.
6276 (with-current-buffer "*Org Export Dispatcher*"
6277 ;; Refresh help. Maintain display continuity by re-visiting
6278 ;; previous window position.
6279 (let ((pos (window-start)))
6282 (set-window-start nil pos)))
6283 (org-fit-window-to-buffer)
6284 (org-export--dispatch-action
6285 standard-prompt allowed-keys entries options first-key expertp))))
6287 (defun org-export--dispatch-action
6288 (prompt allowed-keys entries options first-key expertp)
6289 "Read a character from command input and act accordingly.
6291 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6292 a list of characters available at a given step in the process.
6293 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6294 EXPERTP are the same as defined in `org-export--dispatch-ui',
6297 Toggle export options when required. Otherwise, return value is
6298 a list with action as CAR and a list of interactive export
6301 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6303 (while (and (setq key (read-char-exclusive prompt))
6305 (memq key '(14 16 ?\s ?\d)))
6307 (14 (if (not (pos-visible-in-window-p (point-max)))
6308 (ignore-errors (scroll-up 1))
6309 (message "End of buffer")
6311 (16 (if (not (pos-visible-in-window-p (point-min)))
6312 (ignore-errors (scroll-down 1))
6313 (message "Beginning of buffer")
6315 (?\s (if (not (pos-visible-in-window-p (point-max)))
6317 (message "End of buffer")
6319 (?\d (if (not (pos-visible-in-window-p (point-min)))
6321 (message "Beginning of buffer")
6324 ;; Ignore undefined associations.
6325 ((not (memq key allowed-keys))
6327 (unless expertp (message "Invalid key") (sit-for 1))
6328 (org-export--dispatch-ui options first-key expertp))
6329 ;; q key at first level aborts export. At second level, cancel
6330 ;; first key instead.
6331 ((eq key ?q) (if (not first-key) (error "Export aborted")
6332 (org-export--dispatch-ui options nil expertp)))
6333 ;; Help key: Switch back to standard interface if expert UI was
6335 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6336 ;; Send request for template insertion along with export scope.
6337 ((eq key ?#) (cons 'template (memq 'subtree options)))
6338 ;; Switch to asynchronous export stack.
6339 ((eq key ?&) '(stack))
6340 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6341 ((memq key '(2 22 19 6 1))
6342 (org-export--dispatch-ui
6343 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6344 (6 'force) (1 'async))))
6345 (if (memq option options) (remq option options)
6346 (cons option options)))
6348 ;; Action selected: Send key and options back to
6349 ;; `org-export-dispatch'.
6350 ((or first-key (functionp (nth 2 (assq key entries))))
6352 ((not first-key) (nth 2 (assq key entries)))
6353 ;; Publishing actions are hard-coded. Send a special
6354 ;; signal to `org-export-dispatch'.
6357 (?f 'publish-current-file)
6358 (?p 'publish-current-project)
6359 (?x 'publish-choose-project)
6361 ;; Return first action associated to FIRST-KEY + KEY
6362 ;; path. Indeed, derived backends can share the same
6365 (mapc (lambda (entry)
6366 (let ((match (assq key (nth 2 entry))))
6367 (when match (throw 'found (nth 2 match)))))
6368 (member (assq first-key entries) entries)))))
6370 ;; Otherwise, enter sub-menu.
6371 (t (org-export--dispatch-ui options key expertp)))))
6378 ;; generated-autoload-file: "org-loaddefs.el"