1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; 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 '((:author
"AUTHOR" nil user-full-name t
)
110 (:creator
"CREATOR" nil org-export-creator-string
)
111 (:date
"DATE" nil nil t
)
112 (:description
"DESCRIPTION" nil nil newline
)
113 (:email
"EMAIL" nil user-mail-address t
)
114 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
115 (:headline-levels nil
"H" org-export-headline-levels
)
116 (:keywords
"KEYWORDS" nil nil space
)
117 (:language
"LANGUAGE" nil org-export-default-language t
)
118 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
119 (:section-numbers nil
"num" org-export-with-section-numbers
)
120 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
121 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
122 (:title
"TITLE" nil nil space
)
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 BEHAVIOUR) 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 BEHAVIOUR 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-quote-section . org-export-filter-quote-section-functions
)
225 (:filter-radio-target . org-export-filter-radio-target-functions
)
226 (:filter-section . org-export-filter-section-functions
)
227 (:filter-special-block . org-export-filter-special-block-functions
)
228 (:filter-src-block . org-export-filter-src-block-functions
)
229 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
230 (:filter-strike-through . org-export-filter-strike-through-functions
)
231 (:filter-subscript . org-export-filter-subscript-functions
)
232 (:filter-superscript . org-export-filter-superscript-functions
)
233 (:filter-table . org-export-filter-table-functions
)
234 (:filter-table-cell . org-export-filter-table-cell-functions
)
235 (:filter-table-row . org-export-filter-table-row-functions
)
236 (:filter-target . org-export-filter-target-functions
)
237 (:filter-timestamp . org-export-filter-timestamp-functions
)
238 (:filter-underline . org-export-filter-underline-functions
)
239 (:filter-verbatim . org-export-filter-verbatim-functions
)
240 (:filter-verse-block . org-export-filter-verse-block-functions
))
241 "Alist between filters properties and initial values.
243 The key of each association is a property name accessible through
244 the communication channel. Its value is a configurable global
245 variable defining initial filters.
247 This list is meant to install user specified filters. Back-end
248 developers may install their own filters using
249 `org-export-define-backend'. Filters defined there will always
250 be prepended to the current list, so they always get applied
253 (defconst org-export-default-inline-image-rule
257 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
258 "xpm" "pbm" "pgm" "ppm") t
))))
259 "Default rule for link matching an inline image.
260 This rule applies to links with no description. By default, it
261 will be considered as an inline image if it targets a local file
262 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
263 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
264 See `org-export-inline-image-p' for more information about
267 (defvar org-export-async-debug nil
268 "Non-nil means asynchronous export process should leave data behind.
270 This data is found in the appropriate \"*Org Export Process*\"
271 buffer, and in files prefixed with \"org-export-process\" and
272 located in `temporary-file-directory'.
274 When non-nil, it will also set `debug-on-error' to a non-nil
275 value in the external process.")
277 (defvar org-export-stack-contents nil
278 "Record asynchronously generated export results and processes.
279 This is an alist: its CAR is the source of the
280 result (destination file or buffer for a finished process,
281 original buffer for a running one) and its CDR is a list
282 containing the back-end used, as a symbol, and either a process
283 or the time at which it finished. It is used to build the menu
284 from `org-export-stack'.")
286 (defvar org-export--registered-backends nil
287 "List of backends currently available in the exporter.
288 This variable is set with `org-export-define-backend' and
289 `org-export-define-derived-backend' functions.")
291 (defvar org-export-dispatch-last-action nil
292 "Last command called from the dispatcher.
293 The value should be a list. Its CAR is the action, as a symbol,
294 and its CDR is a list of export options.")
296 (defvar org-export-dispatch-last-position
(make-marker)
297 "The position where the last export command was created using the dispatcher.
298 This marker will be used with `C-u C-c C-e' to make sure export repetition
299 uses the same subtree if the previous command was restricted to a subtree.")
301 ;; For compatibility with Org < 8
302 (defvar org-export-current-backend nil
303 "Name, if any, of the back-end used during an export process.
305 Its value is a symbol such as `html', `latex', `ascii', or nil if
306 the back-end is anonymous (see `org-export-create-backend') or if
307 there is no export process in progress.
309 It can be used to teach Babel blocks how to act differently
310 according to the back-end used.")
313 ;;; User-configurable Variables
315 ;; Configuration for the masses.
317 ;; They should never be accessed directly, as their value is to be
318 ;; stored in a property list (cf. `org-export-options-alist').
319 ;; Back-ends will read their value from there instead.
321 (defgroup org-export nil
322 "Options for exporting Org mode files."
326 (defgroup org-export-general nil
327 "General options for export engine."
328 :tag
"Org Export General"
331 (defcustom org-export-with-archived-trees
'headline
332 "Whether sub-trees with the ARCHIVE tag should be exported.
334 This can have three different values:
335 nil Do not export, pretend this tree is not present.
336 t Do export the entire tree.
337 `headline' Only export the headline, but skip the tree below it.
339 This option can also be set with the OPTIONS keyword,
341 :group
'org-export-general
343 (const :tag
"Not at all" nil
)
344 (const :tag
"Headline only" headline
)
345 (const :tag
"Entirely" t
)))
347 (defcustom org-export-with-author t
348 "Non-nil means insert author name into the exported file.
349 This option can also be set with the OPTIONS keyword,
350 e.g. \"author:nil\"."
351 :group
'org-export-general
354 (defcustom org-export-with-clocks nil
355 "Non-nil means export CLOCK keywords.
356 This option can also be set with the OPTIONS keyword,
358 :group
'org-export-general
361 (defcustom org-export-with-creator
'comment
362 "Non-nil means the postamble should contain a creator sentence.
364 The sentence can be set in `org-export-creator-string' and
365 defaults to \"Generated by Org mode XX in Emacs XXX.\".
367 If the value is `comment' insert it as a comment."
368 :group
'org-export-general
370 (const :tag
"No creator sentence" nil
)
371 (const :tag
"Sentence as a comment" 'comment
)
372 (const :tag
"Insert the sentence" t
)))
374 (defcustom org-export-with-date t
375 "Non-nil means insert date in the exported document.
376 This option can also be set with the OPTIONS keyword,
378 :group
'org-export-general
381 (defcustom org-export-date-timestamp-format nil
382 "Time-stamp format string to use for DATE keyword.
384 The format string, when specified, only applies if date consists
385 in a single time-stamp. Otherwise its value will be ignored.
387 See `format-time-string' for details on how to build this
389 :group
'org-export-general
391 (string :tag
"Time-stamp format string")
392 (const :tag
"No format string" nil
)))
394 (defcustom org-export-creator-string
395 (format "Emacs %s (Org mode %s)"
397 (if (fboundp 'org-version
) (org-version) "unknown version"))
398 "Information about the creator of the document.
399 This option can also be set on with the CREATOR keyword."
400 :group
'org-export-general
401 :type
'(string :tag
"Creator string"))
403 (defcustom org-export-with-drawers
'(not "LOGBOOK")
404 "Non-nil means export contents of standard drawers.
406 When t, all drawers are exported. This may also be a list of
407 drawer names to export, as strings. If that list starts with
408 `not', only drawers with such names will be ignored.
410 This variable doesn't apply to properties drawers. See
411 `org-export-with-properties' instead.
413 This option can also be set with the OPTIONS keyword,
415 :group
'org-export-general
417 :package-version
'(Org .
"8.0")
419 (const :tag
"All drawers" t
)
420 (const :tag
"None" nil
)
421 (repeat :tag
"Selected drawers"
422 (string :tag
"Drawer name"))
423 (list :tag
"Ignored drawers"
424 (const :format
"" not
)
425 (repeat :tag
"Specify names of drawers to ignore during export"
427 (string :tag
"Drawer name")))))
429 (defcustom org-export-with-email nil
430 "Non-nil means insert author email into the exported file.
431 This option can also be set with the OPTIONS keyword,
433 :group
'org-export-general
436 (defcustom org-export-with-emphasize t
437 "Non-nil means interpret *word*, /word/, _word_ and +word+.
439 If the export target supports emphasizing text, the word will be
440 typeset in bold, italic, with an underline or strike-through,
443 This option can also be set with the OPTIONS keyword,
445 :group
'org-export-general
448 (defcustom org-export-exclude-tags
'("noexport")
449 "Tags that exclude a tree from export.
451 All trees carrying any of these tags will be excluded from
452 export. This is without condition, so even subtrees inside that
453 carry one of the `org-export-select-tags' will be removed.
455 This option can also be set with the EXCLUDE_TAGS keyword."
456 :group
'org-export-general
457 :type
'(repeat (string :tag
"Tag")))
459 (defcustom org-export-with-fixed-width t
460 "Non-nil means lines starting with \":\" will be in fixed width font.
462 This can be used to have pre-formatted text, fragments of code
464 : ;; Some Lisp examples
467 will be looking just like this in also HTML. See also the QUOTE
468 keyword. Not all export backends support this.
470 This option can also be set with the OPTIONS keyword,
472 :group
'org-export-general
475 (defcustom org-export-with-footnotes t
476 "Non-nil means Org footnotes should be exported.
477 This option can also be set with the OPTIONS keyword,
479 :group
'org-export-general
482 (defcustom org-export-with-latex t
483 "Non-nil means process LaTeX environments and fragments.
485 This option can also be set with the OPTIONS line,
486 e.g. \"tex:verbatim\". Allowed values are:
488 nil Ignore math snippets.
489 `verbatim' Keep everything in verbatim.
490 t Allow export of math snippets."
491 :group
'org-export-general
493 :package-version
'(Org .
"8.0")
495 (const :tag
"Do not process math in any way" nil
)
496 (const :tag
"Interpret math snippets" t
)
497 (const :tag
"Leave math verbatim" verbatim
)))
499 (defcustom org-export-headline-levels
3
500 "The last level which is still exported as a headline.
502 Inferior levels will usually produce itemize or enumerate lists
503 when exported, but back-end behaviour may differ.
505 This option can also be set with the OPTIONS keyword,
507 :group
'org-export-general
510 (defcustom org-export-default-language
"en"
511 "The default language for export and clocktable translations, as a string.
512 This may have an association in
513 `org-clock-clocktable-language-setup',
514 `org-export-smart-quotes-alist' and `org-export-dictionary'.
515 This option can also be set with the LANGUAGE keyword."
516 :group
'org-export-general
517 :type
'(string :tag
"Language"))
519 (defcustom org-export-preserve-breaks nil
520 "Non-nil means preserve all line breaks when exporting.
521 This option can also be set with the OPTIONS keyword,
523 :group
'org-export-general
526 (defcustom org-export-with-entities t
527 "Non-nil means interpret entities when exporting.
529 For example, HTML export converts \\alpha to α and \\AA to
532 For a list of supported names, see the constant `org-entities'
533 and the user option `org-entities-user'.
535 This option can also be set with the OPTIONS keyword,
537 :group
'org-export-general
540 (defcustom org-export-with-inlinetasks t
541 "Non-nil means inlinetasks should be exported.
542 This option can also be set with the OPTIONS keyword,
543 e.g. \"inline:nil\"."
544 :group
'org-export-general
546 :package-version
'(Org .
"8.0")
549 (defcustom org-export-with-planning nil
550 "Non-nil means include planning info in export.
552 Planning info is the line containing either SCHEDULED:,
553 DEADLINE:, CLOSED: time-stamps, or a combination of them.
555 This option can also be set with the OPTIONS keyword,
557 :group
'org-export-general
559 :package-version
'(Org .
"8.0")
562 (defcustom org-export-with-priority nil
563 "Non-nil means include priority cookies in export.
564 This option can also be set with the OPTIONS keyword,
566 :group
'org-export-general
569 (defcustom org-export-with-properties nil
570 "Non-nil means export contents of properties drawers.
572 When t, all properties are exported. This may also be a list of
573 properties to export, as strings.
575 This option can also be set with the OPTIONS keyword,
577 :group
'org-export-general
579 :package-version
'(Org .
"8.3")
581 (const :tag
"All properties" t
)
582 (const :tag
"None" nil
)
583 (repeat :tag
"Selected properties"
584 (string :tag
"Property name"))))
586 (defcustom org-export-with-section-numbers t
587 "Non-nil means add section numbers to headlines when exporting.
589 When set to an integer n, numbering will only happen for
590 headlines whose relative level is higher or equal to n.
592 This option can also be set with the OPTIONS keyword,
594 :group
'org-export-general
597 (defcustom org-export-select-tags
'("export")
598 "Tags that select a tree for export.
600 If any such tag is found in a buffer, all trees that do not carry
601 one of these tags will be ignored during export. Inside trees
602 that are selected like this, you can still deselect a subtree by
603 tagging it with one of the `org-export-exclude-tags'.
605 This option can also be set with the SELECT_TAGS keyword."
606 :group
'org-export-general
607 :type
'(repeat (string :tag
"Tag")))
609 (defcustom org-export-with-smart-quotes nil
610 "Non-nil means activate smart quotes during export.
611 This option can also be set with the OPTIONS keyword,
614 When setting this to non-nil, you need to take care of
615 using the correct Babel package when exporting to LaTeX.
616 E.g., you can load Babel for french like this:
618 #+LATEX_HEADER: \\usepackage[french]{babel}"
619 :group
'org-export-general
621 :package-version
'(Org .
"8.0")
624 (defcustom org-export-with-special-strings t
625 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
627 When this option is turned on, these strings will be exported as:
630 -----+----------+--------+-------
634 ... … \\ldots …
636 This option can also be set with the OPTIONS keyword,
638 :group
'org-export-general
641 (defcustom org-export-with-statistics-cookies t
642 "Non-nil means include statistics cookies in export.
643 This option can also be set with the OPTIONS keyword,
645 :group
'org-export-general
647 :package-version
'(Org .
"8.0")
650 (defcustom org-export-with-sub-superscripts t
651 "Non-nil means interpret \"_\" and \"^\" for export.
653 If you want to control how Org displays those characters, see
654 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
655 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
658 When this option is turned on, you can use TeX-like syntax for
659 sub- and superscripts and see them exported correctly.
661 You can also set the option with #+OPTIONS: ^:t
663 Several characters after \"_\" or \"^\" will be considered as a
664 single item - so grouping with {} is normally not needed. For
665 example, the following things will be parsed as single sub- or
668 10^24 or 10^tau several digits will be considered 1 item.
669 10^-12 or 10^-tau a leading sign with digits or a word
670 x^2-y^3 will be read as x^2 - y^3, because items are
671 terminated by almost any nonword/nondigit char.
672 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
674 Still, ambiguity is possible. So when in doubt, use {} to enclose
675 the sub/superscript. If you set this variable to the symbol `{}',
676 the braces are *required* in order to trigger interpretations as
677 sub/superscript. This can be helpful in documents that need \"_\"
678 frequently in plain text."
679 :group
'org-export-general
681 :package-version
'(Org .
"8.0")
683 (const :tag
"Interpret them" t
)
684 (const :tag
"Curly brackets only" {})
685 (const :tag
"Do not interpret them" nil
)))
687 (defcustom org-export-with-toc t
688 "Non-nil means create a table of contents in exported files.
690 The TOC contains headlines with levels up
691 to`org-export-headline-levels'. When an integer, include levels
692 up to N in the toc, this may then be different from
693 `org-export-headline-levels', but it will not be allowed to be
694 larger than the number of headline levels. When nil, no table of
697 This option can also be set with the OPTIONS keyword,
698 e.g. \"toc:nil\" or \"toc:3\"."
699 :group
'org-export-general
701 (const :tag
"No Table of Contents" nil
)
702 (const :tag
"Full Table of Contents" t
)
703 (integer :tag
"TOC to level")))
705 (defcustom org-export-with-tables t
706 "If non-nil, lines starting with \"|\" define a table.
709 | Name | Address | Birthday |
710 |-------------+----------+-----------|
711 | Arthur Dent | England | 29.2.2100 |
713 This option can also be set with the OPTIONS keyword,
715 :group
'org-export-general
718 (defcustom org-export-with-tags t
719 "If nil, do not export tags, just remove them from headlines.
721 If this is the symbol `not-in-toc', tags will be removed from
722 table of contents entries, but still be shown in the headlines of
725 This option can also be set with the OPTIONS keyword,
727 :group
'org-export-general
729 (const :tag
"Off" nil
)
730 (const :tag
"Not in TOC" not-in-toc
)
731 (const :tag
"On" t
)))
733 (defcustom org-export-with-tasks t
734 "Non-nil means include TODO items for export.
736 This may have the following values:
737 t include tasks independent of state.
738 `todo' include only tasks that are not yet done.
739 `done' include only tasks that are already done.
740 nil ignore all tasks.
741 list of keywords include tasks with these keywords.
743 This option can also be set with the OPTIONS keyword,
745 :group
'org-export-general
747 (const :tag
"All tasks" t
)
748 (const :tag
"No tasks" nil
)
749 (const :tag
"Not-done tasks" todo
)
750 (const :tag
"Only done tasks" done
)
751 (repeat :tag
"Specific TODO keywords"
752 (string :tag
"Keyword"))))
754 (defcustom org-export-time-stamp-file t
755 "Non-nil means insert a time stamp into the exported file.
756 The time stamp shows when the file was created. This option can
757 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
758 :group
'org-export-general
761 (defcustom org-export-with-timestamps t
762 "Non nil means allow timestamps in export.
764 It can be set to any of the following values:
765 t export all timestamps.
766 `active' export active timestamps only.
767 `inactive' export inactive timestamps only.
768 nil do not export timestamps
770 This only applies to timestamps isolated in a paragraph
771 containing only timestamps. Other timestamps are always
774 This option can also be set with the OPTIONS keyword, e.g.
776 :group
'org-export-general
778 (const :tag
"All timestamps" t
)
779 (const :tag
"Only active timestamps" active
)
780 (const :tag
"Only inactive timestamps" inactive
)
781 (const :tag
"No timestamp" nil
)))
783 (defcustom org-export-with-todo-keywords t
784 "Non-nil means include TODO keywords in export.
785 When nil, remove all these keywords from the export. This option
786 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
787 :group
'org-export-general
790 (defcustom org-export-allow-bind-keywords nil
791 "Non-nil means BIND keywords can define local variable values.
792 This is a potential security risk, which is why the default value
793 is nil. You can also allow them through local buffer variables."
794 :group
'org-export-general
796 :package-version
'(Org .
"8.0")
799 (defcustom org-export-snippet-translation-alist nil
800 "Alist between export snippets back-ends and exporter back-ends.
802 This variable allows to provide shortcuts for export snippets.
804 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
805 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
806 HTML code while every other back-end will ignore it."
807 :group
'org-export-general
809 :package-version
'(Org .
"8.0")
811 (cons (string :tag
"Shortcut")
812 (string :tag
"Back-end"))))
814 (defcustom org-export-coding-system nil
815 "Coding system for the exported file."
816 :group
'org-export-general
818 :package-version
'(Org .
"8.0")
819 :type
'coding-system
)
821 (defcustom org-export-copy-to-kill-ring
'if-interactive
822 "Should we push exported content to the kill ring?"
823 :group
'org-export-general
826 (const :tag
"Always" t
)
827 (const :tag
"When export is done interactively" if-interactive
)
828 (const :tag
"Never" nil
)))
830 (defcustom org-export-initial-scope
'buffer
831 "The initial scope when exporting with `org-export-dispatch'.
832 This variable can be either set to `buffer' or `subtree'."
833 :group
'org-export-general
835 (const :tag
"Export current buffer" buffer
)
836 (const :tag
"Export current subtree" subtree
)))
838 (defcustom org-export-show-temporary-export-buffer t
839 "Non-nil means show buffer after exporting to temp buffer.
840 When Org exports to a file, the buffer visiting that file is ever
841 shown, but remains buried. However, when exporting to
842 a temporary buffer, that buffer is popped up in a second window.
843 When this variable is nil, the buffer remains buried also in
845 :group
'org-export-general
848 (defcustom org-export-in-background nil
849 "Non-nil means export and publishing commands will run in background.
850 Results from an asynchronous export are never displayed
851 automatically. But you can retrieve them with \\[org-export-stack]."
852 :group
'org-export-general
854 :package-version
'(Org .
"8.0")
857 (defcustom org-export-async-init-file nil
858 "File used to initialize external export process.
860 Value must be either nil or an absolute file name. When nil, the
861 external process is launched like a regular Emacs session,
862 loading user's initialization file and any site specific
863 configuration. If a file is provided, it, and only it, is loaded
866 Therefore, using a specific configuration makes the process to
867 load faster and the export more portable."
868 :group
'org-export-general
870 :package-version
'(Org .
"8.0")
872 (const :tag
"Regular startup" nil
)
873 (file :tag
"Specific start-up file" :must-match t
)))
875 (defcustom org-export-dispatch-use-expert-ui nil
876 "Non-nil means using a non-intrusive `org-export-dispatch'.
877 In that case, no help buffer is displayed. Though, an indicator
878 for current export scope is added to the prompt (\"b\" when
879 output is restricted to body only, \"s\" when it is restricted to
880 the current subtree, \"v\" when only visible elements are
881 considered for export, \"f\" when publishing functions should be
882 passed the FORCE argument and \"a\" when the export should be
883 asynchronous). Also, \[?] allows to switch back to standard
885 :group
'org-export-general
887 :package-version
'(Org .
"8.0")
892 ;;; Defining Back-ends
894 ;; An export back-end is a structure with `org-export-backend' type
895 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
898 ;; At the lowest level, a back-end is created with
899 ;; `org-export-create-backend' function.
901 ;; A named back-end can be registered with
902 ;; `org-export-register-backend' function. A registered back-end can
903 ;; later be referred to by its name, with `org-export-get-backend'
904 ;; function. Also, such a back-end can become the parent of a derived
905 ;; back-end from which slot values will be inherited by default.
906 ;; `org-export-derived-backend-p' can check if a given back-end is
907 ;; derived from a list of back-end names.
909 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
910 ;; `org-export-get-all-filters' return the full alist of transcoders,
911 ;; options and filters, including those inherited from ancestors.
913 ;; At a higher level, `org-export-define-backend' is the standard way
914 ;; to define an export back-end. If the new back-end is similar to
915 ;; a registered back-end, `org-export-define-derived-backend' may be
918 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
919 ;; when a given back-end hasn't been registered yet.
921 (defstruct (org-export-backend (:constructor org-export-create-backend
)
923 name parent transcoders options filters blocks menu
)
925 (defun org-export-get-backend (name)
926 "Return export back-end named after NAME.
927 NAME is a symbol. Return nil if no such back-end is found."
929 (dolist (b org-export--registered-backends
)
930 (when (eq (org-export-backend-name b
) name
)
933 (defun org-export-register-backend (backend)
934 "Register BACKEND as a known export back-end.
935 BACKEND is a structure with `org-export-backend' type."
936 ;; Refuse to register an unnamed back-end.
937 (unless (org-export-backend-name backend
)
938 (error "Cannot register a unnamed export back-end"))
939 ;; Refuse to register a back-end with an unknown parent.
940 (let ((parent (org-export-backend-parent backend
)))
941 (when (and parent
(not (org-export-get-backend parent
)))
942 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
943 ;; Register dedicated export blocks in the parser.
944 (dolist (name (org-export-backend-blocks backend
))
945 (add-to-list 'org-element-block-name-alist
946 (cons name
'org-element-export-block-parser
)))
947 ;; If a back-end with the same name as BACKEND is already
948 ;; registered, replace it with BACKEND. Otherwise, simply add
949 ;; BACKEND to the list of registered back-ends.
950 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
951 (if old
(setcar (memq old org-export--registered-backends
) backend
)
952 (push backend org-export--registered-backends
))))
954 (defun org-export-barf-if-invalid-backend (backend)
955 "Signal an error if BACKEND isn't defined."
956 (unless (org-export-backend-p backend
)
957 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
959 (defun org-export-derived-backend-p (backend &rest backends
)
960 "Non-nil if BACKEND is derived from one of BACKENDS.
961 BACKEND is an export back-end, as returned by, e.g.,
962 `org-export-create-backend', or a symbol referring to
963 a registered back-end. BACKENDS is constituted of symbols."
964 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
967 (while (org-export-backend-parent backend
)
968 (when (memq (org-export-backend-name backend
) backends
)
971 (org-export-get-backend (org-export-backend-parent backend
))))
972 (memq (org-export-backend-name backend
) backends
))))
974 (defun org-export-get-all-transcoders (backend)
975 "Return full translation table for BACKEND.
977 BACKEND is an export back-end, as return by, e.g,,
978 `org-export-create-backend'. Return value is an alist where
979 keys are element or object types, as symbols, and values are
982 Unlike to `org-export-backend-transcoders', this function
983 also returns transcoders inherited from parent back-ends,
985 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
987 (let ((transcoders (org-export-backend-transcoders backend
))
989 (while (setq parent
(org-export-backend-parent backend
))
990 (setq backend
(org-export-get-backend parent
))
992 (append transcoders
(org-export-backend-transcoders backend
))))
995 (defun org-export-get-all-options (backend)
996 "Return export options for BACKEND.
998 BACKEND is an export back-end, as return by, e.g,,
999 `org-export-create-backend'. See `org-export-options-alist'
1000 for the shape of the return value.
1002 Unlike to `org-export-backend-options', this function also
1003 returns options inherited from parent back-ends, if any."
1004 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1006 (let ((options (org-export-backend-options backend
))
1008 (while (setq parent
(org-export-backend-parent backend
))
1009 (setq backend
(org-export-get-backend parent
))
1010 (setq options
(append options
(org-export-backend-options backend
))))
1013 (defun org-export-get-all-filters (backend)
1014 "Return complete list of filters for BACKEND.
1016 BACKEND is an export back-end, as return by, e.g,,
1017 `org-export-create-backend'. Return value is an alist where
1018 keys are symbols and values lists of functions.
1020 Unlike to `org-export-backend-filters', this function also
1021 returns filters inherited from parent back-ends, if any."
1022 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1024 (let ((filters (org-export-backend-filters backend
))
1026 (while (setq parent
(org-export-backend-parent backend
))
1027 (setq backend
(org-export-get-backend parent
))
1028 (setq filters
(append filters
(org-export-backend-filters backend
))))
1031 (defun org-export-define-backend (backend transcoders
&rest body
)
1032 "Define a new back-end BACKEND.
1034 TRANSCODERS is an alist between object or element types and
1035 functions handling them.
1037 These functions should return a string without any trailing
1038 space, or nil. They must accept three arguments: the object or
1039 element itself, its contents or nil when it isn't recursive and
1040 the property list used as a communication channel.
1042 Contents, when not nil, are stripped from any global indentation
1043 \(although the relative one is preserved). They also always end
1044 with a single newline character.
1046 If, for a given type, no function is found, that element or
1047 object type will simply be ignored, along with any blank line or
1048 white space at its end. The same will happen if the function
1049 returns the nil value. If that function returns the empty
1050 string, the type will be ignored, but the blank lines or white
1051 spaces will be kept.
1053 In addition to element and object types, one function can be
1054 associated to the `template' (or `inner-template') symbol and
1055 another one to the `plain-text' symbol.
1057 The former returns the final transcoded string, and can be used
1058 to add a preamble and a postamble to document's body. It must
1059 accept two arguments: the transcoded string and the property list
1060 containing export options. A function associated to `template'
1061 will not be applied if export has option \"body-only\".
1062 A function associated to `inner-template' is always applied.
1064 The latter, when defined, is to be called on every text not
1065 recognized as an element or an object. It must accept two
1066 arguments: the text string and the information channel. It is an
1067 appropriate place to protect special chars relative to the
1070 BODY can start with pre-defined keyword arguments. The following
1071 keywords are understood:
1075 String, or list of strings, representing block names that
1076 will not be parsed. This is used to specify blocks that will
1077 contain raw code specific to the back-end. These blocks
1078 still have to be handled by the relative `export-block' type
1083 Alist between filters and function, or list of functions,
1084 specific to the back-end. See `org-export-filters-alist' for
1085 a list of all allowed filters. Filters defined here
1086 shouldn't make a back-end test, as it may prevent back-ends
1087 derived from this one to behave properly.
1091 Menu entry for the export dispatcher. It should be a list
1094 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1098 KEY is a free character selecting the back-end.
1100 DESCRIPTION-OR-ORDINAL is either a string or a number.
1102 If it is a string, is will be used to name the back-end in
1103 its menu entry. If it is a number, the following menu will
1104 be displayed as a sub-menu of the back-end with the same
1105 KEY. Also, the number will be used to determine in which
1106 order such sub-menus will appear (lowest first).
1108 ACTION-OR-MENU is either a function or an alist.
1110 If it is an action, it will be called with four
1111 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1112 BODY-ONLY. See `org-export-as' for further explanations on
1115 If it is an alist, associations should follow the
1118 '(KEY DESCRIPTION ACTION)
1120 where KEY, DESCRIPTION and ACTION are described above.
1122 Valid values include:
1124 '(?m \"My Special Back-end\" my-special-export-function)
1128 '(?l \"Export to LaTeX\"
1129 \(?p \"As PDF file\" org-latex-export-to-pdf)
1130 \(?o \"As PDF file and open\"
1132 \(if a (org-latex-export-to-pdf t s v b)
1134 \(org-latex-export-to-pdf nil s v b)))))))
1136 or the following, which will be added to the previous
1140 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1141 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1145 Alist between back-end specific properties introduced in
1146 communication channel and how their value are acquired. See
1147 `org-export-options-alist' for more information about
1148 structure of the values."
1149 (declare (indent 1))
1150 (let (blocks filters menu-entry options contents
)
1151 (while (keywordp (car body
))
1153 (:export-block
(let ((names (pop body
)))
1154 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1155 (list (upcase names
))))))
1156 (:filters-alist
(setq filters
(pop body
)))
1157 (:menu-entry
(setq menu-entry
(pop body
)))
1158 (:options-alist
(setq options
(pop body
)))
1160 (org-export-register-backend
1161 (org-export-create-backend :name backend
1162 :transcoders transcoders
1166 :menu menu-entry
))))
1168 (defun org-export-define-derived-backend (child parent
&rest body
)
1169 "Create a new back-end as a variant of an existing one.
1171 CHILD is the name of the derived back-end. PARENT is the name of
1172 the parent back-end.
1174 BODY can start with pre-defined keyword arguments. The following
1175 keywords are understood:
1179 String, or list of strings, representing block names that
1180 will not be parsed. This is used to specify blocks that will
1181 contain raw code specific to the back-end. These blocks
1182 still have to be handled by the relative `export-block' type
1187 Alist of filters that will overwrite or complete filters
1188 defined in PARENT back-end. See `org-export-filters-alist'
1189 for a list of allowed filters.
1193 Menu entry for the export dispatcher. See
1194 `org-export-define-backend' for more information about the
1199 Alist of back-end specific properties that will overwrite or
1200 complete those defined in PARENT back-end. Refer to
1201 `org-export-options-alist' for more information about
1202 structure of the values.
1206 Alist of element and object types and transcoders that will
1207 overwrite or complete transcode table from PARENT back-end.
1208 Refer to `org-export-define-backend' for detailed information
1211 As an example, here is how one could define \"my-latex\" back-end
1212 as a variant of `latex' back-end with a custom template function:
1214 \(org-export-define-derived-backend 'my-latex 'latex
1215 :translate-alist '((template . my-latex-template-fun)))
1217 The back-end could then be called with, for example:
1219 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1220 (declare (indent 2))
1221 (let (blocks filters menu-entry options transcoders contents
)
1222 (while (keywordp (car body
))
1224 (:export-block
(let ((names (pop body
)))
1225 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1226 (list (upcase names
))))))
1227 (:filters-alist
(setq filters
(pop body
)))
1228 (:menu-entry
(setq menu-entry
(pop body
)))
1229 (:options-alist
(setq options
(pop body
)))
1230 (:translate-alist
(setq transcoders
(pop body
)))
1232 (org-export-register-backend
1233 (org-export-create-backend :name child
1235 :transcoders transcoders
1239 :menu menu-entry
))))
1243 ;;; The Communication Channel
1245 ;; During export process, every function has access to a number of
1246 ;; properties. They are of two types:
1248 ;; 1. Environment options are collected once at the very beginning of
1249 ;; the process, out of the original buffer and configuration.
1250 ;; Collecting them is handled by `org-export-get-environment'
1253 ;; Most environment options are defined through the
1254 ;; `org-export-options-alist' variable.
1256 ;; 2. Tree properties are extracted directly from the parsed tree,
1257 ;; just before export, by `org-export-collect-tree-properties'.
1259 ;; Here is the full list of properties available during transcode
1260 ;; process, with their category and their value type.
1262 ;; + `:author' :: Author's name.
1263 ;; - category :: option
1266 ;; + `:back-end' :: Current back-end used for transcoding.
1267 ;; - category :: tree
1268 ;; - type :: structure
1270 ;; + `:creator' :: String to write as creation information.
1271 ;; - category :: option
1274 ;; + `:date' :: String to use as date.
1275 ;; - category :: option
1278 ;; + `:description' :: Description text for the current data.
1279 ;; - category :: option
1282 ;; + `:email' :: Author's email.
1283 ;; - category :: option
1286 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1288 ;; - category :: option
1289 ;; - type :: list of strings
1291 ;; + `:export-options' :: List of export options available for current
1293 ;; - category :: none
1294 ;; - type :: list of symbols, among `subtree', `body-only' and
1297 ;; + `:exported-data' :: Hash table used for memoizing
1298 ;; `org-export-data'.
1299 ;; - category :: tree
1300 ;; - type :: hash table
1302 ;; + `:filetags' :: List of global tags for buffer. Used by
1303 ;; `org-export-get-tags' to get tags with inheritance.
1304 ;; - category :: option
1305 ;; - type :: list of strings
1307 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1308 ;; their definition, as parsed data. Only non-inlined footnotes
1309 ;; are represented in this alist. Also, every definition isn't
1310 ;; guaranteed to be referenced in the parse tree. The purpose of
1311 ;; this property is to preserve definitions from oblivion
1312 ;; (i.e. when the parse tree comes from a part of the original
1313 ;; buffer), it isn't meant for direct use in a back-end. To
1314 ;; retrieve a definition relative to a reference, use
1315 ;; `org-export-get-footnote-definition' instead.
1316 ;; - category :: option
1317 ;; - type :: alist (STRING . LIST)
1319 ;; + `:headline-levels' :: Maximum level being exported as an
1320 ;; headline. Comparison is done with the relative level of
1321 ;; headlines in the parse tree, not necessarily with their
1323 ;; - category :: option
1324 ;; - type :: integer
1326 ;; + `:headline-offset' :: Difference between relative and real level
1327 ;; of headlines in the parse tree. For example, a value of -1
1328 ;; means a level 2 headline should be considered as level
1329 ;; 1 (cf. `org-export-get-relative-level').
1330 ;; - category :: tree
1331 ;; - type :: integer
1333 ;; + `:headline-numbering' :: Alist between headlines and their
1334 ;; numbering, as a list of numbers
1335 ;; (cf. `org-export-get-headline-number').
1336 ;; - category :: tree
1337 ;; - type :: alist (INTEGER . LIST)
1339 ;; + `:id-alist' :: Alist between ID strings and destination file's
1340 ;; path, relative to current directory. It is used by
1341 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1343 ;; - category :: option
1344 ;; - type :: alist (STRING . STRING)
1346 ;; + `:ignore-list' :: List of elements and objects that should be
1347 ;; ignored during export.
1348 ;; - category :: tree
1349 ;; - type :: list of elements and objects
1351 ;; + `:input-buffer' :: Original buffer name.
1352 ;; - category :: option
1355 ;; + `:input-file' :: Full path to input file, if any.
1356 ;; - category :: option
1357 ;; - type :: string or nil
1359 ;; + `:keywords' :: List of keywords attached to data.
1360 ;; - category :: option
1363 ;; + `:language' :: Default language used for translations.
1364 ;; - category :: option
1367 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1369 ;; - category :: option
1370 ;; - type :: list (as returned by `org-element-parse-buffer')
1372 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1374 ;; - category :: option
1375 ;; - type :: symbol (nil, t)
1377 ;; + `:section-numbers' :: Non-nil means transcoding should add
1378 ;; section numbers to headlines.
1379 ;; - category :: option
1380 ;; - type :: symbol (nil, t)
1382 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1383 ;; in transcoding. When such a tag is present, subtrees without
1384 ;; it are de facto excluded from the process. See
1385 ;; `use-select-tags'.
1386 ;; - category :: option
1387 ;; - type :: list of strings
1389 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1390 ;; a time stamp in the output.
1391 ;; - category :: option
1392 ;; - type :: symbol (nil, t)
1394 ;; + `:translate-alist' :: Alist between element and object types and
1395 ;; transcoding functions relative to the current back-end.
1396 ;; Special keys `inner-template', `template' and `plain-text' are
1398 ;; - category :: option
1399 ;; - type :: alist (SYMBOL . FUNCTION)
1401 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1402 ;; also be transcoded. If it is set to the `headline' symbol,
1403 ;; only the archived headline's name is retained.
1404 ;; - category :: option
1405 ;; - type :: symbol (nil, t, `headline')
1407 ;; + `:with-author' :: Non-nil means author's name should be included
1409 ;; - category :: option
1410 ;; - type :: symbol (nil, t)
1412 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1413 ;; - category :: option
1414 ;; - type :: symbol (nil, t)
1416 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1417 ;; inserted at the end of the transcoded string. If the value
1418 ;; is `comment', it should be commented.
1419 ;; - category :: option
1420 ;; - type :: symbol (`comment', nil, t)
1422 ;; + `:with-date' :: Non-nil means output should contain a date.
1423 ;; - category :: option
1424 ;; - type :. symbol (nil, t)
1426 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1427 ;; its value is a list of names, only drawers with such names
1428 ;; will be transcoded. If that list starts with `not', drawer
1429 ;; with these names will be skipped.
1430 ;; - category :: option
1431 ;; - type :: symbol (nil, t) or list of strings
1433 ;; + `:with-email' :: Non-nil means output should contain author's
1435 ;; - category :: option
1436 ;; - type :: symbol (nil, t)
1438 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1440 ;; - category :: option
1441 ;; - type :: symbol (nil, t)
1443 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1444 ;; strings starting with a colon as a fixed-with (verbatim) area.
1445 ;; - category :: option
1446 ;; - type :: symbol (nil, t)
1448 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1450 ;; - category :: option
1451 ;; - type :: symbol (nil, t)
1453 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1454 ;; `latex-fragment' objects should appear in export output. When
1455 ;; this property is set to `verbatim', they will be left as-is.
1456 ;; - category :: option
1457 ;; - type :: symbol (`verbatim', nil, t)
1459 ;; + `:with-planning' :: Non-nil means transcoding should include
1461 ;; - category :: option
1462 ;; - type :: symbol (nil, t)
1464 ;; + `:with-priority' :: Non-nil means transcoding should include
1465 ;; priority cookies.
1466 ;; - category :: option
1467 ;; - type :: symbol (nil, t)
1469 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1471 ;; - category :: option
1472 ;; - type :: symbol (nil, t)
1474 ;; + `:with-special-strings' :: Non-nil means transcoding should
1475 ;; interpret special strings in plain text.
1476 ;; - category :: option
1477 ;; - type :: symbol (nil, t)
1479 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1480 ;; interpret subscript and superscript. With a value of "{}",
1481 ;; only interpret those using curly brackets.
1482 ;; - category :: option
1483 ;; - type :: symbol (nil, {}, t)
1485 ;; + `:with-tables' :: Non-nil means transcoding should interpret
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 as its fallback value.
1653 (when (setq prop
(or (org-entry-get (point) "EXPORT_TITLE")
1654 (progn (looking-at org-todo-line-regexp
)
1655 (org-match-string-no-properties 3))))
1659 (org-element-parse-secondary-string
1660 prop
(org-element-restriction 'keyword
)))))
1661 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1662 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1664 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1665 ;; Handle other keywords. TITLE keyword is excluded as it has
1666 ;; been handled already.
1667 (let ((seen '("TITLE")))
1670 (let ((property (car option
))
1671 (keyword (nth 1 option
)))
1672 (when (and keyword
(not (member keyword seen
)))
1673 (let* ((subtree-prop (concat "EXPORT_" keyword
))
1674 ;; Export properties are not case-sensitive.
1675 (value (let ((case-fold-search t
))
1676 (org-entry-get (point) subtree-prop
))))
1678 (when (and value
(not (plist-member plist property
)))
1684 ;; Parse VALUE if required.
1685 ((member keyword org-element-document-properties
)
1686 (org-element-parse-secondary-string
1687 value
(org-element-restriction 'keyword
)))
1688 ;; If BEHAVIOUR is `split' expected value is
1689 ;; a list of strings, not a string.
1690 ((eq (nth 4 option
) 'split
) (org-split-string value
))
1692 ;; Look for both general keywords and back-end specific
1693 ;; options, with priority given to the latter.
1694 (append (and backend
(org-export-get-all-options backend
))
1695 org-export-options-alist
)))
1699 (defun org-export--get-inbuffer-options (&optional backend
)
1700 "Return current buffer export options, as a plist.
1702 Optional argument BACKEND, when non-nil, is an export back-end,
1703 as returned by, e.g., `org-export-create-backend'. It specifies
1704 which back-end specific options should also be read in the
1707 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1709 get-options
; For byte-compiler.
1710 (case-fold-search t
)
1712 ;; Priority is given to back-end specific options.
1713 (and backend
(org-export-get-all-options backend
))
1714 org-export-options-alist
))
1715 (regexp (format "^[ \t]*#\\+%s:"
1716 (regexp-opt (nconc (delq nil
(mapcar 'cadr options
))
1717 org-export-special-keywords
))))
1720 ;; Return all properties associated to KEYWORD.
1722 (dolist (option options properties
)
1723 (when (equal (nth 1 option
) keyword
)
1724 (pushnew (car option
) properties
))))))
1726 (lambda (&optional files plist
)
1727 ;; Recursively read keywords in buffer. FILES is a list
1728 ;; of files read so far. PLIST is the current property
1730 (org-with-wide-buffer
1731 (goto-char (point-min))
1732 (while (re-search-forward regexp nil t
)
1733 (let ((element (org-element-at-point)))
1734 (when (eq (org-element-type element
) 'keyword
)
1735 (let ((key (org-element-property :key element
))
1736 (val (org-element-property :value element
)))
1738 ;; Options in `org-export-special-keywords'.
1739 ((equal key
"SETUPFILE")
1740 (let ((file (expand-file-name
1741 (org-remove-double-quotes (org-trim val
)))))
1742 ;; Avoid circular dependencies.
1743 (unless (member file files
)
1745 (insert (org-file-contents file
'noerror
))
1746 (let ((org-inhibit-startup t
)) (org-mode))
1747 (setq plist
(funcall get-options
1748 (cons file files
) plist
))))))
1749 ((equal key
"OPTIONS")
1753 (org-export--parse-option-keyword val backend
))))
1754 ((equal key
"FILETAGS")
1760 (append (org-split-string val
":")
1761 (plist-get plist
:filetags
)))))))
1763 ;; Options in `org-export-options-alist'.
1764 (dolist (property (funcall find-properties key
))
1765 (let ((behaviour (nth 4 (assq property options
))))
1769 ;; Handle value depending on specified
1773 (if (not (plist-get plist property
))
1775 (concat (plist-get plist property
)
1780 (concat (plist-get plist property
)
1783 (split `(,@(plist-get plist property
)
1784 ,@(org-split-string val
)))
1787 (if (not (plist-member plist property
)) val
1788 (plist-get plist property
))))))))))))))
1789 ;; Return final value.
1791 ;; Read options in the current buffer.
1792 (setq plist
(funcall get-options
1793 (and buffer-file-name
(list buffer-file-name
)) nil
))
1794 ;; Parse keywords specified in `org-element-document-properties'
1795 ;; and return PLIST.
1796 (dolist (keyword org-element-document-properties plist
)
1797 (dolist (property (funcall find-properties keyword
))
1798 (let ((value (plist-get plist property
)))
1799 (when (stringp value
)
1801 (plist-put plist property
1802 (org-element-parse-secondary-string
1803 value
(org-element-restriction 'keyword
))))))))))
1805 (defun org-export--get-buffer-attributes ()
1806 "Return properties related to buffer attributes, as a plist."
1807 ;; Store full path of input file name, or nil. For internal use.
1808 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1809 (list :input-buffer
(buffer-name (buffer-base-buffer))
1810 :input-file visited-file
1811 :title
(if (not visited-file
) (buffer-name (buffer-base-buffer))
1812 (file-name-sans-extension
1813 (file-name-nondirectory visited-file
))))))
1815 (defun org-export--get-global-options (&optional backend
)
1816 "Return global export options as a plist.
1817 Optional argument BACKEND, if non-nil, is an export back-end, as
1818 returned by, e.g., `org-export-create-backend'. It specifies
1819 which back-end specific export options should also be read in the
1822 ;; Priority is given to back-end specific options.
1823 (all (append (and backend
(org-export-get-all-options backend
))
1824 org-export-options-alist
)))
1825 (dolist (cell all plist
)
1826 (let ((prop (car cell
))
1827 (default-value (nth 3 cell
)))
1828 (unless (or (not default-value
) (plist-member plist prop
))
1833 ;; Eval default value provided. If keyword is
1834 ;; a member of `org-element-document-properties',
1835 ;; parse it as a secondary string before storing it.
1836 (let ((value (eval (nth 3 cell
))))
1837 (if (not (stringp value
)) value
1838 (let ((keyword (nth 1 cell
)))
1839 (if (member keyword org-element-document-properties
)
1840 (org-element-parse-secondary-string
1841 value
(org-element-restriction 'keyword
))
1844 (defun org-export--list-bound-variables ()
1845 "Return variables bound from BIND keywords in current buffer.
1846 Also look for BIND keywords in setup files. The return value is
1847 an alist where associations are (VARIABLE-NAME VALUE)."
1848 (when org-export-allow-bind-keywords
1849 (let* (collect-bind ; For byte-compiler.
1851 (lambda (files alist
)
1852 ;; Return an alist between variable names and their
1853 ;; value. FILES is a list of setup files names read so
1854 ;; far, used to avoid circular dependencies. ALIST is
1855 ;; the alist collected so far.
1856 (let ((case-fold-search t
))
1857 (org-with-wide-buffer
1858 (goto-char (point-min))
1859 (while (re-search-forward
1860 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1861 (let ((element (org-element-at-point)))
1862 (when (eq (org-element-type element
) 'keyword
)
1863 (let ((val (org-element-property :value element
)))
1864 (if (equal (org-element-property :key element
) "BIND")
1865 (push (read (format "(%s)" val
)) alist
)
1866 ;; Enter setup file.
1867 (let ((file (expand-file-name
1868 (org-remove-double-quotes val
))))
1869 (unless (member file files
)
1871 (let ((org-inhibit-startup t
)) (org-mode))
1872 (insert (org-file-contents file
'noerror
))
1874 (funcall collect-bind
1878 ;; Return value in appropriate order of appearance.
1879 (nreverse (funcall collect-bind nil nil
)))))
1882 ;;;; Tree Properties
1884 ;; Tree properties are information extracted from parse tree. They
1885 ;; are initialized at the beginning of the transcoding process by
1886 ;; `org-export-collect-tree-properties'.
1888 ;; Dedicated functions focus on computing the value of specific tree
1889 ;; properties during initialization. Thus,
1890 ;; `org-export--populate-ignore-list' lists elements and objects that
1891 ;; should be skipped during export, `org-export--get-min-level' gets
1892 ;; the minimal exportable level, used as a basis to compute relative
1893 ;; level for headlines. Eventually
1894 ;; `org-export--collect-headline-numbering' builds an alist between
1895 ;; headlines and their numbering.
1897 (defun org-export-collect-tree-properties (data info
)
1898 "Extract tree properties from parse tree.
1900 DATA is the parse tree from which information is retrieved. INFO
1901 is a list holding export options.
1903 Following tree properties are set or updated:
1905 `:exported-data' Hash table used to memoize results from
1908 `:footnote-definition-alist' List of footnotes definitions in
1909 original buffer and current parse tree.
1911 `:headline-offset' Offset between true level of headlines and
1912 local level. An offset of -1 means a headline
1913 of level 2 should be considered as a level
1914 1 headline in the context.
1916 `:headline-numbering' Alist of all headlines as key an the
1917 associated numbering as value.
1919 `:ignore-list' List of elements that should be ignored during
1922 Return updated plist."
1923 ;; Install the parse tree in the communication channel, in order to
1924 ;; use `org-export-get-genealogy' and al.
1925 (setq info
(plist-put info
:parse-tree data
))
1926 ;; Get the list of elements and objects to ignore, and put it into
1927 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1928 ;; been done during parse tree filtering.
1932 (append (org-export--populate-ignore-list data info
)
1933 (plist-get info
:ignore-list
))))
1934 ;; Compute `:headline-offset' in order to be able to use
1935 ;; `org-export-get-relative-level'.
1939 (- 1 (org-export--get-min-level data info
))))
1940 ;; Update footnotes definitions list with definitions in parse tree.
1941 ;; This is required since buffer expansion might have modified
1942 ;; boundaries of footnote definitions contained in the parse tree.
1943 ;; This way, definitions in `footnote-definition-alist' are bound to
1944 ;; match those in the parse tree.
1945 (let ((defs (plist-get info
:footnote-definition-alist
)))
1946 (org-element-map data
'footnote-definition
1948 (push (cons (org-element-property :label fn
)
1949 `(org-data nil
,@(org-element-contents fn
)))
1951 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1952 ;; Properties order doesn't matter: get the rest of the tree
1955 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1956 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1959 (defun org-export--get-min-level (data options
)
1960 "Return minimum exportable headline's level in DATA.
1961 DATA is parsed tree as returned by `org-element-parse-buffer'.
1962 OPTIONS is a plist holding export options."
1964 (let ((min-level 10000))
1967 (when (and (eq (org-element-type blob
) 'headline
)
1968 (not (org-element-property :footnote-section-p blob
))
1969 (not (memq blob
(plist-get options
:ignore-list
))))
1970 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1971 (when (= min-level
1) (throw 'exit
1)))
1972 (org-element-contents data
))
1973 ;; If no headline was found, for the sake of consistency, set
1974 ;; minimum level to 1 nonetheless.
1975 (if (= min-level
10000) 1 min-level
))))
1977 (defun org-export--collect-headline-numbering (data options
)
1978 "Return numbering of all exportable headlines in a parse tree.
1980 DATA is the parse tree. OPTIONS is the plist holding export
1983 Return an alist whose key is a headline and value is its
1984 associated numbering \(in the shape of a list of numbers\) or nil
1985 for a footnotes section."
1986 (let ((numbering (make-vector org-export-max-depth
0)))
1987 (org-element-map data
'headline
1989 (unless (org-element-property :footnote-section-p headline
)
1990 (let ((relative-level
1991 (1- (org-export-get-relative-level headline options
))))
1994 (loop for n across numbering
1995 for idx from
0 to org-export-max-depth
1996 when
(< idx relative-level
) collect n
1997 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1998 when
(> idx relative-level
) do
(aset numbering idx
0))))))
2001 (defun org-export--populate-ignore-list (data options
)
2002 "Return list of elements and objects to ignore during export.
2003 DATA is the parse tree to traverse. OPTIONS is the plist holding
2006 ;; First find trees containing a select tag, if any.
2007 (selected (org-export--selected-trees data options
))
2008 ;; If a select tag is active, also ignore the section before
2009 ;; the first headline, if any.
2010 (ignore (and selected
2011 (let ((first-element (car (org-element-contents data
))))
2012 (and (eq (org-element-type first-element
) 'section
)
2016 ;; Collect ignored elements or objects into IGNORE-LIST.
2017 (let ((type (org-element-type data
)))
2018 (if (org-export--skip-p data options selected
) (push data ignore
)
2019 (if (and (eq type
'headline
)
2020 (eq (plist-get options
:with-archived-trees
) 'headline
)
2021 (org-element-property :archivedp data
))
2022 ;; If headline is archived but tree below has
2023 ;; to be skipped, add it to ignore list.
2024 (dolist (element (org-element-contents data
))
2025 (push element ignore
))
2026 ;; Move into secondary string, if any.
2028 (cdr (assq type org-element-secondary-value-alist
))))
2030 (mapc walk-data
(org-element-property sec-prop data
))))
2031 ;; Move into recursive objects/elements.
2032 (mapc walk-data
(org-element-contents data
))))))))
2034 (funcall walk-data data
)
2038 (defun org-export--selected-trees (data info
)
2039 "Return list of headlines and inlinetasks with a select tag in their tree.
2040 DATA is parsed data as returned by `org-element-parse-buffer'.
2041 INFO is a plist holding export options."
2042 (let* (selected-trees
2043 walk-data
; For byte-compiler.
2046 (lambda (data genealogy
)
2047 (let ((type (org-element-type data
)))
2049 ((memq type
'(headline inlinetask
))
2050 (let ((tags (org-element-property :tags data
)))
2051 (if (loop for tag in
(plist-get info
:select-tags
)
2052 thereis
(member tag tags
))
2053 ;; When a select tag is found, mark full
2054 ;; genealogy and every headline within the tree
2056 (setq selected-trees
2059 (org-element-map data
'(headline inlinetask
)
2062 ;; If at a headline, continue searching in tree,
2064 (when (eq type
'headline
)
2066 (funcall walk-data el
(cons data genealogy
)))
2067 (org-element-contents data
))))))
2068 ((or (eq type
'org-data
)
2069 (memq type org-element-greater-elements
))
2070 (mapc (lambda (el) (funcall walk-data el genealogy
))
2071 (org-element-contents data
)))))))))
2072 (funcall walk-data data nil
)
2075 (defun org-export--skip-p (blob options selected
)
2076 "Non-nil when element or object BLOB should be skipped during export.
2077 OPTIONS is the plist holding export options. SELECTED, when
2078 non-nil, is a list of headlines or inlinetasks belonging to
2079 a tree with a select tag."
2080 (case (org-element-type blob
)
2081 (clock (not (plist-get options
:with-clocks
)))
2083 (let ((with-drawers-p (plist-get options
:with-drawers
)))
2084 (or (not with-drawers-p
)
2085 (and (consp with-drawers-p
)
2086 ;; If `:with-drawers' value starts with `not', ignore
2087 ;; every drawer whose name belong to that list.
2088 ;; Otherwise, ignore drawers whose name isn't in that
2090 (let ((name (org-element-property :drawer-name blob
)))
2091 (if (eq (car with-drawers-p
) 'not
)
2092 (member-ignore-case name
(cdr with-drawers-p
))
2093 (not (member-ignore-case name with-drawers-p
))))))))
2094 ((footnote-definition footnote-reference
)
2095 (not (plist-get options
:with-footnotes
)))
2096 ((headline inlinetask
)
2097 (let ((with-tasks (plist-get options
:with-tasks
))
2098 (todo (org-element-property :todo-keyword blob
))
2099 (todo-type (org-element-property :todo-type blob
))
2100 (archived (plist-get options
:with-archived-trees
))
2101 (tags (org-element-property :tags blob
)))
2103 (and (eq (org-element-type blob
) 'inlinetask
)
2104 (not (plist-get options
:with-inlinetasks
)))
2105 ;; Ignore subtrees with an exclude tag.
2106 (loop for k in
(plist-get options
:exclude-tags
)
2107 thereis
(member k tags
))
2108 ;; When a select tag is present in the buffer, ignore any tree
2110 (and selected
(not (memq blob selected
)))
2111 ;; Ignore commented sub-trees.
2112 (org-element-property :commentedp blob
)
2113 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
2114 (and (not archived
) (org-element-property :archivedp blob
))
2115 ;; Ignore tasks, if specified by `:with-tasks' property.
2117 (or (not with-tasks
)
2118 (and (memq with-tasks
'(todo done
))
2119 (not (eq todo-type with-tasks
)))
2120 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
2121 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
2123 (let ((properties-set (plist-get options
:with-properties
)))
2124 (cond ((null properties-set
) t
)
2125 ((consp properties-set
)
2126 (not (member-ignore-case (org-element-property :key blob
)
2127 properties-set
))))))
2128 (planning (not (plist-get options
:with-planning
)))
2129 (property-drawer (not (plist-get options
:with-properties
)))
2130 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
2132 (and (org-export-table-has-special-column-p
2133 (org-export-get-parent-table blob
))
2134 (not (org-export-get-previous-element blob options
))))
2135 (table-row (org-export-table-row-is-special-p blob options
))
2137 ;; `:with-timestamps' only applies to isolated timestamps
2138 ;; objects, i.e. timestamp objects in a paragraph containing only
2139 ;; timestamps and whitespaces.
2140 (when (let ((parent (org-export-get-parent-element blob
)))
2141 (and (memq (org-element-type parent
) '(paragraph verse-block
))
2142 (not (org-element-map parent
2144 (remq 'timestamp org-element-all-objects
))
2146 (or (not (stringp obj
)) (org-string-nw-p obj
)))
2148 (case (plist-get options
:with-timestamps
)
2151 (not (memq (org-element-property :type blob
) '(active active-range
))))
2153 (not (memq (org-element-property :type blob
)
2154 '(inactive inactive-range
)))))))))
2159 ;; `org-export-data' reads a parse tree (obtained with, i.e.
2160 ;; `org-element-parse-buffer') and transcodes it into a specified
2161 ;; back-end output. It takes care of filtering out elements or
2162 ;; objects according to export options and organizing the output blank
2163 ;; lines and white space are preserved. The function memoizes its
2164 ;; results, so it is cheap to call it within transcoders.
2166 ;; It is possible to modify locally the back-end used by
2167 ;; `org-export-data' or even use a temporary back-end by using
2168 ;; `org-export-data-with-backend'.
2170 ;; Internally, three functions handle the filtering of objects and
2171 ;; elements during the export. In particular,
2172 ;; `org-export-ignore-element' marks an element or object so future
2173 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
2174 ;; elements or objects should be seen as real Org syntax and
2175 ;; `org-export-expand' transforms the others back into their original
2178 ;; `org-export-transcoder' is an accessor returning appropriate
2179 ;; translator function for a given element or object.
2181 (defun org-export-transcoder (blob info
)
2182 "Return appropriate transcoder for BLOB.
2183 INFO is a plist containing export directives."
2184 (let ((type (org-element-type blob
)))
2185 ;; Return contents only for complete parse trees.
2186 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
2187 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
2188 (and (functionp transcoder
) transcoder
)))))
2190 (defun org-export-data (data info
)
2191 "Convert DATA into current back-end format.
2193 DATA is a parse tree, an element or an object or a secondary
2194 string. INFO is a plist holding export options.
2196 Return transcoded string."
2197 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
2198 (if (not (eq memo
'no-memo
)) memo
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 ;; Uninterpreted element/object: change it back to Org
2212 ;; syntax and export again resulting raw string.
2213 ((not (org-export--interpret-p data info
))
2217 (mapconcat (lambda (blob) (org-export-data blob info
))
2218 (org-element-contents data
)
2221 ;; Secondary string.
2223 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
2224 ;; Element/Object without contents or, as a special case,
2225 ;; headline with archive tag and archived trees restricted
2227 ((or (not (org-element-contents data
))
2228 (and (eq type
'headline
)
2229 (eq (plist-get info
:with-archived-trees
) 'headline
)
2230 (org-element-property :archivedp data
)))
2231 (let ((transcoder (org-export-transcoder data info
)))
2232 (or (and (functionp transcoder
)
2233 (funcall transcoder data nil info
))
2234 ;; Export snippets never return a nil value so
2235 ;; that white spaces following them are never
2237 (and (eq type
'export-snippet
) ""))))
2238 ;; Element/Object with contents.
2240 (let ((transcoder (org-export-transcoder data info
)))
2242 (let* ((greaterp (memq type org-element-greater-elements
))
2245 (memq type org-element-recursive-objects
)))
2248 (lambda (element) (org-export-data element info
))
2249 (org-element-contents
2250 (if (or greaterp objectp
) data
2251 ;; Elements directly containing objects
2252 ;; must have their indentation normalized
2254 (org-element-normalize-contents
2256 ;; When normalizing contents of the first
2257 ;; paragraph in an item or a footnote
2258 ;; definition, ignore first line's
2259 ;; indentation: there is none and it
2260 ;; might be misleading.
2261 (when (eq type
'paragraph
)
2262 (let ((parent (org-export-get-parent data
)))
2264 (eq (car (org-element-contents parent
))
2266 (memq (org-element-type parent
)
2267 '(footnote-definition item
))))))))
2269 (funcall transcoder data
2270 (if (not greaterp
) contents
2271 (org-element-normalize-string contents
))
2273 ;; Final result will be memoized before being returned.
2278 ((memq type
'(org-data plain-text nil
)) results
)
2279 ;; Append the same white space between elements or objects as in
2280 ;; the original buffer, and call appropriate filters.
2283 (org-export-filter-apply-functions
2284 (plist-get info
(intern (format ":filter-%s" type
)))
2285 (let ((post-blank (or (org-element-property :post-blank data
)
2287 (if (memq type org-element-all-elements
)
2288 (concat (org-element-normalize-string results
)
2289 (make-string post-blank ?
\n))
2290 (concat results
(make-string post-blank ?
))))
2293 (plist-get info
:exported-data
))))))
2295 (defun org-export-data-with-backend (data backend info
)
2296 "Convert DATA into BACKEND format.
2298 DATA is an element, an object, a secondary string or a string.
2299 BACKEND is a symbol. INFO is a plist used as a communication
2302 Unlike to `org-export-with-backend', this function will
2303 recursively convert DATA using BACKEND translation table."
2304 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2307 ;; Set-up a new communication channel with translations defined in
2308 ;; BACKEND as the translate table and a new hash table for
2312 (list :back-end backend
2313 :translate-alist
(org-export-get-all-transcoders backend
)
2314 ;; Size of the hash table is reduced since this function
2315 ;; will probably be used on small trees.
2316 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2318 (defun org-export--interpret-p (blob info
)
2319 "Non-nil if element or object BLOB should be interpreted during export.
2320 If nil, BLOB will appear as raw Org syntax. Check is done
2321 according to export options INFO, stored as a plist."
2322 (case (org-element-type blob
)
2324 (entity (plist-get info
:with-entities
))
2326 ((bold italic strike-through underline
)
2327 (plist-get info
:with-emphasize
))
2328 ;; ... fixed-width areas.
2329 (fixed-width (plist-get info
:with-fixed-width
))
2330 ;; ... LaTeX environments and fragments...
2331 ((latex-environment latex-fragment
)
2332 (let ((with-latex-p (plist-get info
:with-latex
)))
2333 (and with-latex-p
(not (eq with-latex-p
'verbatim
)))))
2334 ;; ... sub/superscripts...
2335 ((subscript superscript
)
2336 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
2337 (if (eq sub
/super-p
'{})
2338 (org-element-property :use-brackets-p blob
)
2341 (table (plist-get info
:with-tables
))
2344 (defun org-export-expand (blob contents
&optional with-affiliated
)
2345 "Expand a parsed element or object to its original state.
2347 BLOB is either an element or an object. CONTENTS is its
2348 contents, as a string or nil.
2350 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2351 keywords before output."
2352 (let ((type (org-element-type blob
)))
2353 (concat (and with-affiliated
(memq type org-element-all-elements
)
2354 (org-element--interpret-affiliated-keywords blob
))
2355 (funcall (intern (format "org-element-%s-interpreter" type
))
2358 (defun org-export-ignore-element (element info
)
2359 "Add ELEMENT to `:ignore-list' in INFO.
2361 Any element in `:ignore-list' will be skipped when using
2362 `org-element-map'. INFO is modified by side effects."
2363 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2367 ;;; The Filter System
2369 ;; Filters allow end-users to tweak easily the transcoded output.
2370 ;; They are the functional counterpart of hooks, as every filter in
2371 ;; a set is applied to the return value of the previous one.
2373 ;; Every set is back-end agnostic. Although, a filter is always
2374 ;; called, in addition to the string it applies to, with the back-end
2375 ;; used as argument, so it's easy for the end-user to add back-end
2376 ;; specific filters in the set. The communication channel, as
2377 ;; a plist, is required as the third argument.
2379 ;; From the developer side, filters sets can be installed in the
2380 ;; process with the help of `org-export-define-backend', which
2381 ;; internally stores filters as an alist. Each association has a key
2382 ;; among the following symbols and a function or a list of functions
2385 ;; - `:filter-options' applies to the property list containing export
2386 ;; options. Unlike to other filters, functions in this list accept
2387 ;; two arguments instead of three: the property list containing
2388 ;; export options and the back-end. Users can set its value through
2389 ;; `org-export-filter-options-functions' variable.
2391 ;; - `:filter-parse-tree' applies directly to the complete parsed
2392 ;; tree. Users can set it through
2393 ;; `org-export-filter-parse-tree-functions' variable.
2395 ;; - `:filter-final-output' applies to the final transcoded string.
2396 ;; Users can set it with `org-export-filter-final-output-functions'
2399 ;; - `:filter-plain-text' applies to any string not recognized as Org
2400 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2403 ;; - `:filter-TYPE' applies on the string returned after an element or
2404 ;; object of type TYPE has been transcoded. A user can modify
2405 ;; `org-export-filter-TYPE-functions'
2407 ;; All filters sets are applied with
2408 ;; `org-export-filter-apply-functions' function. Filters in a set are
2409 ;; applied in a LIFO fashion. It allows developers to be sure that
2410 ;; their filters will be applied first.
2412 ;; Filters properties are installed in communication channel with
2413 ;; `org-export-install-filters' function.
2415 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2416 ;; `org-export-before-parsing-hook') are run at the beginning of the
2417 ;; export process and just before parsing to allow for heavy structure
2423 (defvar org-export-before-processing-hook nil
2424 "Hook run at the beginning of the export process.
2426 This is run before include keywords and macros are expanded and
2427 Babel code blocks executed, on a copy of the original buffer
2428 being exported. Visibility and narrowing are preserved. Point
2429 is at the beginning of the buffer.
2431 Every function in this hook will be called with one argument: the
2432 back-end currently used, as a symbol.")
2434 (defvar org-export-before-parsing-hook nil
2435 "Hook run before parsing an export buffer.
2437 This is run after include keywords and macros have been expanded
2438 and Babel code blocks executed, on a copy of the original buffer
2439 being exported. Visibility and narrowing are preserved. Point
2440 is at the beginning of the buffer.
2442 Every function in this hook will be called with one argument: the
2443 back-end currently used, as a symbol.")
2446 ;;;; Special Filters
2448 (defvar org-export-filter-options-functions nil
2449 "List of functions applied to the export options.
2450 Each filter is called with two arguments: the export options, as
2451 a plist, and the back-end, as a symbol. It must return
2452 a property list containing export options.")
2454 (defvar org-export-filter-parse-tree-functions nil
2455 "List of functions applied to the parsed tree.
2456 Each filter is called with three arguments: the parse tree, as
2457 returned by `org-element-parse-buffer', the back-end, as
2458 a symbol, and the communication channel, as a plist. It must
2459 return the modified parse tree to transcode.")
2461 (defvar org-export-filter-plain-text-functions nil
2462 "List of functions applied to plain text.
2463 Each filter is called with three arguments: a string which
2464 contains no Org syntax, the back-end, as a symbol, and the
2465 communication channel, as a plist. It must return a string or
2468 (defvar org-export-filter-final-output-functions nil
2469 "List of functions applied to the transcoded string.
2470 Each filter is called with three arguments: the full transcoded
2471 string, the back-end, as a symbol, and the communication channel,
2472 as a plist. It must return a string that will be used as the
2473 final export output.")
2476 ;;;; Elements Filters
2478 (defvar org-export-filter-babel-call-functions nil
2479 "List of functions applied to a transcoded babel-call.
2480 Each filter is called with three arguments: the transcoded data,
2481 as a string, the back-end, as a symbol, and the communication
2482 channel, as a plist. It must return a string or nil.")
2484 (defvar org-export-filter-center-block-functions nil
2485 "List of functions applied to a transcoded center block.
2486 Each filter is called with three arguments: the transcoded data,
2487 as a string, the back-end, as a symbol, and the communication
2488 channel, as a plist. It must return a string or nil.")
2490 (defvar org-export-filter-clock-functions nil
2491 "List of functions applied to a transcoded clock.
2492 Each filter is called with three arguments: the transcoded data,
2493 as a string, the back-end, as a symbol, and the communication
2494 channel, as a plist. It must return a string or nil.")
2496 (defvar org-export-filter-comment-functions nil
2497 "List of functions applied to a transcoded comment.
2498 Each filter is called with three arguments: the transcoded data,
2499 as a string, the back-end, as a symbol, and the communication
2500 channel, as a plist. It must return a string or nil.")
2502 (defvar org-export-filter-comment-block-functions nil
2503 "List of functions applied to a transcoded comment-block.
2504 Each filter is called with three arguments: the transcoded data,
2505 as a string, the back-end, as a symbol, and the communication
2506 channel, as a plist. It must return a string or nil.")
2508 (defvar org-export-filter-diary-sexp-functions nil
2509 "List of functions applied to a transcoded diary-sexp.
2510 Each filter is called with three arguments: the transcoded data,
2511 as a string, the back-end, as a symbol, and the communication
2512 channel, as a plist. It must return a string or nil.")
2514 (defvar org-export-filter-drawer-functions nil
2515 "List of functions applied to a transcoded drawer.
2516 Each filter is called with three arguments: the transcoded data,
2517 as a string, the back-end, as a symbol, and the communication
2518 channel, as a plist. It must return a string or nil.")
2520 (defvar org-export-filter-dynamic-block-functions nil
2521 "List of functions applied to a transcoded dynamic-block.
2522 Each filter is called with three arguments: the transcoded data,
2523 as a string, the back-end, as a symbol, and the communication
2524 channel, as a plist. It must return a string or nil.")
2526 (defvar org-export-filter-example-block-functions nil
2527 "List of functions applied to a transcoded example-block.
2528 Each filter is called with three arguments: the transcoded data,
2529 as a string, the back-end, as a symbol, and the communication
2530 channel, as a plist. It must return a string or nil.")
2532 (defvar org-export-filter-export-block-functions nil
2533 "List of functions applied to a transcoded export-block.
2534 Each filter is called with three arguments: the transcoded data,
2535 as a string, the back-end, as a symbol, and the communication
2536 channel, as a plist. It must return a string or nil.")
2538 (defvar org-export-filter-fixed-width-functions nil
2539 "List of functions applied to a transcoded fixed-width.
2540 Each filter is called with three arguments: the transcoded data,
2541 as a string, the back-end, as a symbol, and the communication
2542 channel, as a plist. It must return a string or nil.")
2544 (defvar org-export-filter-footnote-definition-functions nil
2545 "List of functions applied to a transcoded footnote-definition.
2546 Each filter is called with three arguments: the transcoded data,
2547 as a string, the back-end, as a symbol, and the communication
2548 channel, as a plist. It must return a string or nil.")
2550 (defvar org-export-filter-headline-functions nil
2551 "List of functions applied to a transcoded headline.
2552 Each filter is called with three arguments: the transcoded data,
2553 as a string, the back-end, as a symbol, and the communication
2554 channel, as a plist. It must return a string or nil.")
2556 (defvar org-export-filter-horizontal-rule-functions nil
2557 "List of functions applied to a transcoded horizontal-rule.
2558 Each filter is called with three arguments: the transcoded data,
2559 as a string, the back-end, as a symbol, and the communication
2560 channel, as a plist. It must return a string or nil.")
2562 (defvar org-export-filter-inlinetask-functions nil
2563 "List of functions applied to a transcoded inlinetask.
2564 Each filter is called with three arguments: the transcoded data,
2565 as a string, the back-end, as a symbol, and the communication
2566 channel, as a plist. It must return a string or nil.")
2568 (defvar org-export-filter-item-functions nil
2569 "List of functions applied to a transcoded item.
2570 Each filter is called with three arguments: the transcoded data,
2571 as a string, the back-end, as a symbol, and the communication
2572 channel, as a plist. It must return a string or nil.")
2574 (defvar org-export-filter-keyword-functions nil
2575 "List of functions applied to a transcoded keyword.
2576 Each filter is called with three arguments: the transcoded data,
2577 as a string, the back-end, as a symbol, and the communication
2578 channel, as a plist. It must return a string or nil.")
2580 (defvar org-export-filter-latex-environment-functions nil
2581 "List of functions applied to a transcoded latex-environment.
2582 Each filter is called with three arguments: the transcoded data,
2583 as a string, the back-end, as a symbol, and the communication
2584 channel, as a plist. It must return a string or nil.")
2586 (defvar org-export-filter-node-property-functions nil
2587 "List of functions applied to a transcoded node-property.
2588 Each filter is called with three arguments: the transcoded data,
2589 as a string, the back-end, as a symbol, and the communication
2590 channel, as a plist. It must return a string or nil.")
2592 (defvar org-export-filter-paragraph-functions nil
2593 "List of functions applied to a transcoded paragraph.
2594 Each filter is called with three arguments: the transcoded data,
2595 as a string, the back-end, as a symbol, and the communication
2596 channel, as a plist. It must return a string or nil.")
2598 (defvar org-export-filter-plain-list-functions nil
2599 "List of functions applied to a transcoded plain-list.
2600 Each filter is called with three arguments: the transcoded data,
2601 as a string, the back-end, as a symbol, and the communication
2602 channel, as a plist. It must return a string or nil.")
2604 (defvar org-export-filter-planning-functions nil
2605 "List of functions applied to a transcoded planning.
2606 Each filter is called with three arguments: the transcoded data,
2607 as a string, the back-end, as a symbol, and the communication
2608 channel, as a plist. It must return a string or nil.")
2610 (defvar org-export-filter-property-drawer-functions nil
2611 "List of functions applied to a transcoded property-drawer.
2612 Each filter is called with three arguments: the transcoded data,
2613 as a string, the back-end, as a symbol, and the communication
2614 channel, as a plist. It must return a string or nil.")
2616 (defvar org-export-filter-quote-block-functions nil
2617 "List of functions applied to a transcoded quote block.
2618 Each filter is called with three arguments: the transcoded quote
2619 data, as a string, the back-end, as a symbol, and the
2620 communication channel, as a plist. It must return a string or
2623 (defvar org-export-filter-quote-section-functions nil
2624 "List of functions applied to a transcoded quote-section.
2625 Each filter is called with three arguments: the transcoded data,
2626 as a string, the back-end, as a symbol, and the communication
2627 channel, as a plist. It must return a string or nil.")
2629 (defvar org-export-filter-section-functions nil
2630 "List of functions applied to a transcoded section.
2631 Each filter is called with three arguments: the transcoded data,
2632 as a string, the back-end, as a symbol, and the communication
2633 channel, as a plist. It must return a string or nil.")
2635 (defvar org-export-filter-special-block-functions nil
2636 "List of functions applied to a transcoded special block.
2637 Each filter is called with three arguments: the transcoded data,
2638 as a string, the back-end, as a symbol, and the communication
2639 channel, as a plist. It must return a string or nil.")
2641 (defvar org-export-filter-src-block-functions nil
2642 "List of functions applied to a transcoded src-block.
2643 Each filter is called with three arguments: the transcoded data,
2644 as a string, the back-end, as a symbol, and the communication
2645 channel, as a plist. It must return a string or nil.")
2647 (defvar org-export-filter-table-functions nil
2648 "List of functions applied to a transcoded table.
2649 Each filter is called with three arguments: the transcoded data,
2650 as a string, the back-end, as a symbol, and the communication
2651 channel, as a plist. It must return a string or nil.")
2653 (defvar org-export-filter-table-cell-functions nil
2654 "List of functions applied to a transcoded table-cell.
2655 Each filter is called with three arguments: the transcoded data,
2656 as a string, the back-end, as a symbol, and the communication
2657 channel, as a plist. It must return a string or nil.")
2659 (defvar org-export-filter-table-row-functions nil
2660 "List of functions applied to a transcoded table-row.
2661 Each filter is called with three arguments: the transcoded data,
2662 as a string, the back-end, as a symbol, and the communication
2663 channel, as a plist. It must return a string or nil.")
2665 (defvar org-export-filter-verse-block-functions nil
2666 "List of functions applied to a transcoded verse block.
2667 Each filter is called with three arguments: the transcoded data,
2668 as a string, the back-end, as a symbol, and the communication
2669 channel, as a plist. It must return a string or nil.")
2672 ;;;; Objects Filters
2674 (defvar org-export-filter-bold-functions nil
2675 "List of functions applied to transcoded bold text.
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-code-functions nil
2681 "List of functions applied to transcoded code text.
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-entity-functions nil
2687 "List of functions applied to a transcoded entity.
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-export-snippet-functions nil
2693 "List of functions applied to a transcoded export-snippet.
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-footnote-reference-functions nil
2699 "List of functions applied to a transcoded footnote-reference.
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-inline-babel-call-functions nil
2705 "List of functions applied to a transcoded inline-babel-call.
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.")
2710 (defvar org-export-filter-inline-src-block-functions nil
2711 "List of functions applied to a transcoded inline-src-block.
2712 Each filter is called with three arguments: the transcoded data,
2713 as a string, the back-end, as a symbol, and the communication
2714 channel, as a plist. It must return a string or nil.")
2716 (defvar org-export-filter-italic-functions nil
2717 "List of functions applied to transcoded italic text.
2718 Each filter is called with three arguments: the transcoded data,
2719 as a string, the back-end, as a symbol, and the communication
2720 channel, as a plist. It must return a string or nil.")
2722 (defvar org-export-filter-latex-fragment-functions nil
2723 "List of functions applied to a transcoded latex-fragment.
2724 Each filter is called with three arguments: the transcoded data,
2725 as a string, the back-end, as a symbol, and the communication
2726 channel, as a plist. It must return a string or nil.")
2728 (defvar org-export-filter-line-break-functions nil
2729 "List of functions applied to a transcoded line-break.
2730 Each filter is called with three arguments: the transcoded data,
2731 as a string, the back-end, as a symbol, and the communication
2732 channel, as a plist. It must return a string or nil.")
2734 (defvar org-export-filter-link-functions nil
2735 "List of functions applied to a transcoded link.
2736 Each filter is called with three arguments: the transcoded data,
2737 as a string, the back-end, as a symbol, and the communication
2738 channel, as a plist. It must return a string or nil.")
2740 (defvar org-export-filter-radio-target-functions nil
2741 "List of functions applied to a transcoded radio-target.
2742 Each filter is called with three arguments: the transcoded data,
2743 as a string, the back-end, as a symbol, and the communication
2744 channel, as a plist. It must return a string or nil.")
2746 (defvar org-export-filter-statistics-cookie-functions nil
2747 "List of functions applied to a transcoded statistics-cookie.
2748 Each filter is called with three arguments: the transcoded data,
2749 as a string, the back-end, as a symbol, and the communication
2750 channel, as a plist. It must return a string or nil.")
2752 (defvar org-export-filter-strike-through-functions nil
2753 "List of functions applied to transcoded strike-through text.
2754 Each filter is called with three arguments: the transcoded data,
2755 as a string, the back-end, as a symbol, and the communication
2756 channel, as a plist. It must return a string or nil.")
2758 (defvar org-export-filter-subscript-functions nil
2759 "List of functions applied to a transcoded subscript.
2760 Each filter is called with three arguments: the transcoded data,
2761 as a string, the back-end, as a symbol, and the communication
2762 channel, as a plist. It must return a string or nil.")
2764 (defvar org-export-filter-superscript-functions nil
2765 "List of functions applied to a transcoded superscript.
2766 Each filter is called with three arguments: the transcoded data,
2767 as a string, the back-end, as a symbol, and the communication
2768 channel, as a plist. It must return a string or nil.")
2770 (defvar org-export-filter-target-functions nil
2771 "List of functions applied to a transcoded target.
2772 Each filter is called with three arguments: the transcoded data,
2773 as a string, the back-end, as a symbol, and the communication
2774 channel, as a plist. It must return a string or nil.")
2776 (defvar org-export-filter-timestamp-functions nil
2777 "List of functions applied to a transcoded timestamp.
2778 Each filter is called with three arguments: the transcoded data,
2779 as a string, the back-end, as a symbol, and the communication
2780 channel, as a plist. It must return a string or nil.")
2782 (defvar org-export-filter-underline-functions nil
2783 "List of functions applied to transcoded underline text.
2784 Each filter is called with three arguments: the transcoded data,
2785 as a string, the back-end, as a symbol, and the communication
2786 channel, as a plist. It must return a string or nil.")
2788 (defvar org-export-filter-verbatim-functions nil
2789 "List of functions applied to transcoded verbatim text.
2790 Each filter is called with three arguments: the transcoded data,
2791 as a string, the back-end, as a symbol, and the communication
2792 channel, as a plist. It must return a string or nil.")
2797 ;; Internal function `org-export-install-filters' installs filters
2798 ;; hard-coded in back-ends (developer filters) and filters from global
2799 ;; variables (user filters) in the communication channel.
2801 ;; Internal function `org-export-filter-apply-functions' takes care
2802 ;; about applying each filter in order to a given data. It ignores
2803 ;; filters returning a nil value but stops whenever a filter returns
2806 (defun org-export-filter-apply-functions (filters value info
)
2807 "Call every function in FILTERS.
2809 Functions are called with arguments VALUE, current export
2810 back-end's name and INFO. A function returning a nil value will
2811 be skipped. If it returns the empty string, the process ends and
2814 Call is done in a LIFO fashion, to be sure that developer
2815 specified filters, if any, are called first."
2817 (let* ((backend (plist-get info
:back-end
))
2818 (backend-name (and backend
(org-export-backend-name backend
))))
2819 (dolist (filter filters value
)
2820 (let ((result (funcall filter value backend-name info
)))
2821 (cond ((not result
) value
)
2822 ((equal value
"") (throw 'exit nil
))
2823 (t (setq value result
))))))))
2825 (defun org-export-install-filters (info)
2826 "Install filters properties in communication channel.
2827 INFO is a plist containing the current communication channel.
2828 Return the updated communication channel."
2830 ;; Install user-defined filters with `org-export-filters-alist'
2831 ;; and filters already in INFO (through ext-plist mechanism).
2833 (let* ((prop (car p
))
2834 (info-value (plist-get info prop
))
2835 (default-value (symbol-value (cdr p
))))
2837 (plist-put plist prop
2838 ;; Filters in INFO will be called
2839 ;; before those user provided.
2840 (append (if (listp info-value
) info-value
2843 org-export-filters-alist
)
2844 ;; Prepend back-end specific filters to that list.
2846 ;; Single values get consed, lists are appended.
2847 (let ((key (car p
)) (value (cdr p
)))
2852 (if (atom value
) (cons value
(plist-get plist key
))
2853 (append value
(plist-get plist key
))))))))
2854 (org-export-get-all-filters (plist-get info
:back-end
)))
2855 ;; Return new communication channel.
2856 (org-combine-plists info plist
)))
2862 ;; This is the room for the main function, `org-export-as', along with
2863 ;; its derivative, `org-export-string-as'.
2864 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2865 ;; function should be added to kill ring.
2867 ;; Note that `org-export-as' doesn't really parse the current buffer,
2868 ;; but a copy of it (with the same buffer-local variables and
2869 ;; visibility), where macros and include keywords are expanded and
2870 ;; Babel blocks are executed, if appropriate.
2871 ;; `org-export-with-buffer-copy' macro prepares that copy.
2873 ;; File inclusion is taken care of by
2874 ;; `org-export-expand-include-keyword' and
2875 ;; `org-export--prepare-file-contents'. Structure wise, including
2876 ;; a whole Org file in a buffer often makes little sense. For
2877 ;; example, if the file contains a headline and the include keyword
2878 ;; was within an item, the item should contain the headline. That's
2879 ;; why file inclusion should be done before any structure can be
2880 ;; associated to the file, that is before parsing.
2882 ;; `org-export-insert-default-template' is a command to insert
2883 ;; a default template (or a back-end specific template) at point or in
2886 (defun org-export-copy-buffer ()
2887 "Return a copy of the current buffer.
2888 The copy preserves Org buffer-local variables, visibility and
2890 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2891 (new-buf (generate-new-buffer (buffer-name))))
2892 (with-current-buffer new-buf
2893 (funcall copy-buffer-fun
)
2894 (set-buffer-modified-p nil
))
2897 (defmacro org-export-with-buffer-copy
(&rest body
)
2898 "Apply BODY in a copy of the current buffer.
2899 The copy preserves local variables, visibility and contents of
2900 the original buffer. Point is at the beginning of the buffer
2901 when BODY is applied."
2903 (org-with-gensyms (buf-copy)
2904 `(let ((,buf-copy
(org-export-copy-buffer)))
2906 (with-current-buffer ,buf-copy
2907 (goto-char (point-min))
2909 (and (buffer-live-p ,buf-copy
)
2910 ;; Kill copy without confirmation.
2911 (progn (with-current-buffer ,buf-copy
2912 (restore-buffer-modified-p nil
))
2913 (kill-buffer ,buf-copy
)))))))
2915 (defun org-export--generate-copy-script (buffer)
2916 "Generate a function duplicating BUFFER.
2918 The copy will preserve local variables, visibility, contents and
2919 narrowing of the original buffer. If a region was active in
2920 BUFFER, contents will be narrowed to that region instead.
2922 The resulting function can be evaluated at a later time, from
2923 another buffer, effectively cloning the original buffer there.
2925 The function assumes BUFFER's major mode is `org-mode'."
2926 (with-current-buffer buffer
2928 (let ((inhibit-modification-hooks t
))
2929 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2930 ;; already in BUFFER.
2931 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2932 ;; Copy specific buffer local variables and variables set
2933 ;; through BIND keywords.
2934 ,@(let ((bound-variables (org-export--list-bound-variables))
2936 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2938 (let ((var (car entry
))
2940 (and (not (eq var
'org-font-lock-keywords
))
2944 buffer-file-coding-system
))
2945 (assq var bound-variables
)
2946 (string-match "^\\(org-\\|orgtbl-\\)"
2948 ;; Skip unreadable values, as they cannot be
2949 ;; sent to external process.
2950 (or (not val
) (ignore-errors (read (format "%S" val
))))
2951 (push `(set (make-local-variable (quote ,var
))
2954 ;; Whole buffer contents.
2956 ,(org-with-wide-buffer
2957 (buffer-substring-no-properties
2958 (point-min) (point-max))))
2960 ,(if (org-region-active-p)
2961 `(narrow-to-region ,(region-beginning) ,(region-end))
2962 `(narrow-to-region ,(point-min) ,(point-max)))
2963 ;; Current position of point.
2964 (goto-char ,(point))
2965 ;; Overlays with invisible property.
2969 (let ((invis-prop (overlay-get ov
'invisible
)))
2972 (make-overlay ,(overlay-start ov
)
2974 'invisible
(quote ,invis-prop
))
2976 (overlays-in (point-min) (point-max)))
2980 (defun org-export-as
2981 (backend &optional subtreep visible-only body-only ext-plist
)
2982 "Transcode current Org buffer into BACKEND code.
2984 BACKEND is either an export back-end, as returned by, e.g.,
2985 `org-export-create-backend', or a symbol referring to
2986 a registered back-end.
2988 If narrowing is active in the current buffer, only transcode its
2991 If a region is active, transcode that region.
2993 When optional argument SUBTREEP is non-nil, transcode the
2994 sub-tree at point, extracting information from the headline
2997 When optional argument VISIBLE-ONLY is non-nil, don't export
2998 contents of hidden elements.
3000 When optional argument BODY-ONLY is non-nil, only return body
3001 code, without surrounding template.
3003 Optional argument EXT-PLIST, when provided, is a property list
3004 with external parameters overriding Org default settings, but
3005 still inferior to file-local settings.
3007 Return code as a string."
3008 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3009 (org-export-barf-if-invalid-backend backend
)
3012 ;; Narrow buffer to an appropriate region or subtree for
3013 ;; parsing. If parsing subtree, be sure to remove main headline
3015 (cond ((org-region-active-p)
3016 (narrow-to-region (region-beginning) (region-end)))
3018 (org-narrow-to-subtree)
3019 (goto-char (point-min))
3021 (narrow-to-region (point) (point-max))))
3022 ;; Initialize communication channel with original buffer
3023 ;; attributes, unavailable in its copy.
3024 (let* ((org-export-current-backend (org-export-backend-name backend
))
3025 (info (org-combine-plists
3026 (list :export-options
3028 (list (and subtreep
'subtree
)
3029 (and visible-only
'visible-only
)
3030 (and body-only
'body-only
))))
3031 (org-export--get-buffer-attributes)))
3033 ;; Update communication channel and get parse tree. Buffer
3034 ;; isn't parsed directly. Instead, a temporary copy is
3035 ;; created, where include keywords, macros are expanded and
3036 ;; code blocks are evaluated.
3037 (org-export-with-buffer-copy
3038 ;; Run first hook with current back-end's name as argument.
3039 (run-hook-with-args 'org-export-before-processing-hook
3040 (org-export-backend-name backend
))
3041 (org-export-expand-include-keyword)
3042 ;; Update macro templates since #+INCLUDE keywords might have
3043 ;; added some new ones.
3044 (org-macro-initialize-templates)
3045 (org-macro-replace-all org-macro-templates
)
3046 (org-export-execute-babel-code)
3047 ;; Update radio targets since keyword inclusion might have
3049 (org-update-radio-target-regexp)
3050 ;; Run last hook with current back-end's name as argument.
3051 (goto-char (point-min))
3053 (run-hook-with-args 'org-export-before-parsing-hook
3054 (org-export-backend-name backend
)))
3055 ;; Update communication channel with environment. Also
3056 ;; install user's and developer's filters.
3058 (org-export-install-filters
3060 info
(org-export-get-environment backend subtreep ext-plist
))))
3061 ;; Expand export-specific set of macros: {{{author}}},
3062 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3063 ;; once regular macros have been expanded, since document
3064 ;; keywords may contain one of them.
3065 (org-macro-replace-all
3066 (list (cons "author"
3067 (org-element-interpret-data (plist-get info
:author
)))
3069 (org-element-interpret-data (plist-get info
:date
)))
3070 ;; EMAIL is not a parsed keyword: store it as-is.
3071 (cons "email" (or (plist-get info
:email
) ""))
3073 (org-element-interpret-data (plist-get info
:title
)))))
3074 ;; Call options filters and update export options. We do not
3075 ;; use `org-export-filter-apply-functions' here since the
3076 ;; arity of such filters is different.
3077 (let ((backend-name (org-export-backend-name backend
)))
3078 (dolist (filter (plist-get info
:filter-options
))
3079 (let ((result (funcall filter info backend-name
)))
3080 (when result
(setq info result
)))))
3081 ;; Parse buffer and call parse-tree filter on it.
3083 (org-export-filter-apply-functions
3084 (plist-get info
:filter-parse-tree
)
3085 (org-element-parse-buffer nil visible-only
) info
))
3086 ;; Now tree is complete, compute its properties and add them
3087 ;; to communication channel.
3090 info
(org-export-collect-tree-properties tree info
)))
3091 ;; Eventually transcode TREE. Wrap the resulting string into
3093 (let* ((body (org-element-normalize-string
3094 (or (org-export-data tree info
) "")))
3095 (inner-template (cdr (assq 'inner-template
3096 (plist-get info
:translate-alist
))))
3097 (full-body (if (not (functionp inner-template
)) body
3098 (funcall inner-template body info
)))
3099 (template (cdr (assq 'template
3100 (plist-get info
:translate-alist
)))))
3101 ;; Remove all text properties since they cannot be
3102 ;; retrieved from an external process. Finally call
3103 ;; final-output filter and return result.
3105 (org-export-filter-apply-functions
3106 (plist-get info
:filter-final-output
)
3107 (if (or (not (functionp template
)) body-only
) full-body
3108 (funcall template full-body info
))
3112 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3113 "Transcode STRING into BACKEND code.
3115 BACKEND is either an export back-end, as returned by, e.g.,
3116 `org-export-create-backend', or a symbol referring to
3117 a registered back-end.
3119 When optional argument BODY-ONLY is non-nil, only return body
3120 code, without preamble nor postamble.
3122 Optional argument EXT-PLIST, when provided, is a property list
3123 with external parameters overriding Org default settings, but
3124 still inferior to file-local settings.
3126 Return code as a string."
3129 (let ((org-inhibit-startup t
)) (org-mode))
3130 (org-export-as backend nil nil body-only ext-plist
)))
3133 (defun org-export-replace-region-by (backend)
3134 "Replace the active region by its export to BACKEND.
3135 BACKEND is either an export back-end, as returned by, e.g.,
3136 `org-export-create-backend', or a symbol referring to
3137 a registered back-end."
3138 (if (not (org-region-active-p))
3139 (user-error "No active region to replace")
3140 (let* ((beg (region-beginning))
3142 (str (buffer-substring beg end
)) rpl
)
3143 (setq rpl
(org-export-string-as str backend t
))
3144 (delete-region beg end
)
3148 (defun org-export-insert-default-template (&optional backend subtreep
)
3149 "Insert all export keywords with default values at beginning of line.
3151 BACKEND is a symbol referring to the name of a registered export
3152 back-end, for which specific export options should be added to
3153 the template, or `default' for default template. When it is nil,
3154 the user will be prompted for a category.
3156 If SUBTREEP is non-nil, export configuration will be set up
3157 locally for the subtree through node properties."
3159 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3160 (when (and subtreep
(org-before-first-heading-p))
3161 (user-error "No subtree to set export options for"))
3162 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3166 (org-completing-read
3167 "Options category: "
3170 (symbol-name (org-export-backend-name b
)))
3171 org-export--registered-backends
))))))
3173 ;; Populate OPTIONS and KEYWORDS.
3174 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3175 ((org-export-backend-p backend
)
3176 (org-export-get-all-options backend
))
3177 (t (org-export-get-all-options
3178 (org-export-get-backend backend
)))))
3179 (let ((keyword (nth 1 entry
))
3180 (option (nth 2 entry
)))
3182 (keyword (unless (assoc keyword keywords
)
3184 (if (eq (nth 4 entry
) 'split
)
3185 (mapconcat 'identity
(eval (nth 3 entry
)) " ")
3186 (eval (nth 3 entry
)))))
3187 (push (cons keyword value
) keywords
))))
3188 (option (unless (assoc option options
)
3189 (push (cons option
(eval (nth 3 entry
))) options
))))))
3190 ;; Move to an appropriate location in order to insert options.
3191 (unless subtreep
(beginning-of-line))
3192 ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
3193 ;; list of available keywords.
3194 (when (assoc "TITLE" keywords
)
3196 (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
3198 (file-name-sans-extension
3199 (file-name-nondirectory visited-file
))))
3200 (buffer-name (buffer-base-buffer)))))
3201 (if (not subtreep
) (insert (format "#+TITLE: %s\n" title
))
3202 (org-entry-put node
"EXPORT_TITLE" title
))))
3203 (when (assoc "DATE" keywords
)
3204 (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
3205 (if (not subtreep
) (insert "#+DATE: " date
"\n")
3206 (org-entry-put node
"EXPORT_DATE" date
))))
3207 (when (assoc "AUTHOR" keywords
)
3208 (let ((author (cdr (assoc "AUTHOR" keywords
))))
3209 (if subtreep
(org-entry-put node
"EXPORT_AUTHOR" author
)
3211 (format "#+AUTHOR:%s\n"
3212 (if (not (org-string-nw-p author
)) ""
3213 (concat " " author
)))))))
3214 (when (assoc "EMAIL" keywords
)
3215 (let ((email (cdr (assoc "EMAIL" keywords
))))
3216 (if subtreep
(org-entry-put node
"EXPORT_EMAIL" email
)
3218 (format "#+EMAIL:%s\n"
3219 (if (not (org-string-nw-p email
)) ""
3220 (concat " " email
)))))))
3221 ;; Then (multiple) OPTIONS lines. Never go past fill-column.
3225 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3226 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3229 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3231 (insert "#+OPTIONS:")
3234 (< (+ width
(length (car items
)) 1) fill-column
))
3235 (let ((item (pop items
)))
3237 (incf width
(1+ (length item
))))))
3239 ;; And the rest of keywords.
3240 (dolist (key (sort keywords
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))
3241 (unless (member (car key
) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
3242 (let ((val (cdr key
)))
3243 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3247 (if (org-string-nw-p val
) (format " %s" val
) "")))))))))
3249 (defun org-export-expand-include-keyword (&optional included dir
)
3250 "Expand every include keyword in buffer.
3251 Optional argument INCLUDED is a list of included file names along
3252 with their line restriction, when appropriate. It is used to
3253 avoid infinite recursion. Optional argument DIR is the current
3254 working directory. It is used to properly resolve relative
3256 (let ((case-fold-search t
))
3257 (goto-char (point-min))
3258 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
3259 (let ((element (save-match-data (org-element-at-point))))
3260 (when (eq (org-element-type element
) 'keyword
)
3262 ;; Extract arguments from keyword's value.
3263 (let* ((value (org-element-property :value element
))
3264 (ind (org-get-indentation))
3265 (file (and (string-match
3266 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3267 (prog1 (expand-file-name
3268 (org-remove-double-quotes
3269 (match-string 1 value
))
3271 (setq value
(replace-match "" nil nil value
)))))
3274 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3276 (prog1 (match-string 1 value
)
3277 (setq value
(replace-match "" nil nil value
)))))
3278 (env (cond ((string-match "\\<example\\>" value
) 'example
)
3279 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3280 (match-string 1 value
))))
3281 ;; Minimal level of included file defaults to the child
3282 ;; level of the current headline, if any, or one. It
3283 ;; only applies is the file is meant to be included as
3287 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3288 (prog1 (string-to-number (match-string 1 value
))
3289 (setq value
(replace-match "" nil nil value
)))
3290 (let ((cur (org-current-level)))
3291 (if cur
(1+ (org-reduced-level cur
)) 1))))))
3293 (delete-region (point) (progn (forward-line) (point)))
3296 ((not (file-readable-p file
))
3297 (error "Cannot include file %s" file
))
3298 ;; Check if files has already been parsed. Look after
3299 ;; inclusion lines too, as different parts of the same file
3300 ;; can be included too.
3301 ((member (list file lines
) included
)
3302 (error "Recursive file inclusion: %s" file
))
3307 (let ((ind-str (make-string ind ?
))
3309 (org-escape-code-in-string
3310 (org-export--prepare-file-contents file lines
))))
3311 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3312 ind-str contents ind-str
))))
3315 (let ((ind-str (make-string ind ?
))
3317 (org-escape-code-in-string
3318 (org-export--prepare-file-contents file lines
))))
3319 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3320 ind-str env contents ind-str
))))
3324 (let ((org-inhibit-startup t
)) (org-mode))
3326 (org-export--prepare-file-contents file lines ind minlevel
))
3327 (org-export-expand-include-keyword
3328 (cons (list file lines
) included
)
3329 (file-name-directory file
))
3330 (buffer-string)))))))))))))
3332 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
3333 "Prepare the contents of FILE for inclusion and return them as a string.
3335 When optional argument LINES is a string specifying a range of
3336 lines, include only those lines.
3338 Optional argument IND, when non-nil, is an integer specifying the
3339 global indentation of returned contents. Since its purpose is to
3340 allow an included file to stay in the same environment it was
3341 created \(i.e. a list item), it doesn't apply past the first
3342 headline encountered.
3344 Optional argument MINLEVEL, when non-nil, is an integer
3345 specifying the level that any top-level headline in the included
3348 (insert-file-contents file
)
3350 (let* ((lines (split-string lines
"-"))
3351 (lbeg (string-to-number (car lines
)))
3352 (lend (string-to-number (cadr lines
)))
3353 (beg (if (zerop lbeg
) (point-min)
3354 (goto-char (point-min))
3355 (forward-line (1- lbeg
))
3357 (end (if (zerop lend
) (point-max)
3358 (goto-char (point-min))
3359 (forward-line (1- lend
))
3361 (narrow-to-region beg end
)))
3362 ;; Remove blank lines at beginning and end of contents. The logic
3363 ;; behind that removal is that blank lines around include keyword
3364 ;; override blank lines in included file.
3365 (goto-char (point-min))
3366 (org-skip-whitespace)
3368 (delete-region (point-min) (point))
3369 (goto-char (point-max))
3370 (skip-chars-backward " \r\t\n")
3372 (delete-region (point) (point-max))
3373 ;; If IND is set, preserve indentation of include keyword until
3374 ;; the first headline encountered.
3376 (unless (eq major-mode
'org-mode
)
3377 (let ((org-inhibit-startup t
)) (org-mode)))
3378 (goto-char (point-min))
3379 (let ((ind-str (make-string ind ?
)))
3380 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3381 ;; Do not move footnote definitions out of column 0.
3382 (unless (and (looking-at org-footnote-definition-re
)
3383 (eq (org-element-type (org-element-at-point))
3384 'footnote-definition
))
3387 ;; When MINLEVEL is specified, compute minimal level for headlines
3388 ;; in the file (CUR-MIN), and remove stars to each headline so
3389 ;; that headlines with minimal level have a level of MINLEVEL.
3391 (unless (eq major-mode
'org-mode
)
3392 (let ((org-inhibit-startup t
)) (org-mode)))
3393 (org-with-limited-levels
3394 (let ((levels (org-map-entries
3395 (lambda () (org-reduced-level (org-current-level))))))
3397 (let ((offset (- minlevel
(apply 'min levels
))))
3398 (unless (zerop offset
)
3399 (when org-odd-levels-only
(setq offset
(* offset
2)))
3400 ;; Only change stars, don't bother moving whole
3403 (lambda () (if (< offset
0) (delete-char (abs offset
))
3404 (insert (make-string offset ?
*)))))))))))
3405 (org-element-normalize-string (buffer-string))))
3407 (defun org-export-execute-babel-code ()
3408 "Execute every Babel code in the visible part of current buffer."
3409 ;; Get a pristine copy of current buffer so Babel references can be
3410 ;; properly resolved.
3411 (let ((reference (org-export-copy-buffer)))
3412 (unwind-protect (let ((org-current-export-file reference
))
3413 (org-babel-exp-process-buffer))
3414 (kill-buffer reference
))))
3416 (defun org-export--copy-to-kill-ring-p ()
3417 "Return a non-nil value when output should be added to the kill ring.
3418 See also `org-export-copy-to-kill-ring'."
3419 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3420 (not (or executing-kbd-macro noninteractive
))
3421 (eq org-export-copy-to-kill-ring t
)))
3425 ;;; Tools For Back-Ends
3427 ;; A whole set of tools is available to help build new exporters. Any
3428 ;; function general enough to have its use across many back-ends
3429 ;; should be added here.
3431 ;;;; For Affiliated Keywords
3433 ;; `org-export-read-attribute' reads a property from a given element
3434 ;; as a plist. It can be used to normalize affiliated keywords'
3437 ;; Since captions can span over multiple lines and accept dual values,
3438 ;; their internal representation is a bit tricky. Therefore,
3439 ;; `org-export-get-caption' transparently returns a given element's
3440 ;; caption as a secondary string.
3442 (defun org-export-read-attribute (attribute element
&optional property
)
3443 "Turn ATTRIBUTE property from ELEMENT into a plist.
3445 When optional argument PROPERTY is non-nil, return the value of
3446 that property within attributes.
3448 This function assumes attributes are defined as \":keyword
3449 value\" pairs. It is appropriate for `:attr_html' like
3452 All values will become strings except the empty string and
3453 \"nil\", which will become nil. Also, values containing only
3454 double quotes will be read as-is, which means that \"\" value
3455 will become the empty string."
3456 (let* ((prepare-value
3459 (cond ((member str
'(nil "" "nil")) nil
)
3460 ((string-match "^\"\\(\"+\\)?\"$" str
)
3461 (or (match-string 1 str
) ""))
3464 (let ((value (org-element-property attribute element
)))
3466 (let ((s (mapconcat 'identity value
" ")) result
)
3467 (while (string-match
3468 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3470 (let ((value (substring s
0 (match-beginning 0))))
3471 (push (funcall prepare-value value
) result
))
3472 (push (intern (match-string 1 s
)) result
)
3473 (setq s
(substring s
(match-end 0))))
3474 ;; Ignore any string before first property with `cdr'.
3475 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3476 (if property
(plist-get attributes property
) attributes
)))
3478 (defun org-export-get-caption (element &optional shortp
)
3479 "Return caption from ELEMENT as a secondary string.
3481 When optional argument SHORTP is non-nil, return short caption,
3482 as a secondary string, instead.
3484 Caption lines are separated by a white space."
3485 (let ((full-caption (org-element-property :caption element
)) caption
)
3486 (dolist (line full-caption
(cdr caption
))
3487 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3489 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3492 ;;;; For Derived Back-ends
3494 ;; `org-export-with-backend' is a function allowing to locally use
3495 ;; another back-end to transcode some object or element. In a derived
3496 ;; back-end, it may be used as a fall-back function once all specific
3497 ;; cases have been treated.
3499 (defun org-export-with-backend (backend data
&optional contents info
)
3500 "Call a transcoder from BACKEND on DATA.
3501 BACKEND is an export back-end, as returned by, e.g.,
3502 `org-export-create-backend', or a symbol referring to
3503 a registered back-end. DATA is an Org element, object, secondary
3504 string or string. CONTENTS, when non-nil, is the transcoded
3505 contents of DATA element, as a string. INFO, when non-nil, is
3506 the communication channel used for export, as a plist."
3507 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3508 (org-export-barf-if-invalid-backend backend
)
3509 (let ((type (org-element-type data
)))
3510 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3511 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3512 (transcoder (cdr (assq type all-transcoders
))))
3513 (if (not (functionp transcoder
))
3514 (error "No foreign transcoder available")
3516 transcoder data contents
3518 info
(list :back-end backend
3519 :translate-alist all-transcoders
3520 :exported-data
(make-hash-table :test
'eq
:size
401)))))))))
3523 ;;;; For Export Snippets
3525 ;; Every export snippet is transmitted to the back-end. Though, the
3526 ;; latter will only retain one type of export-snippet, ignoring
3527 ;; others, based on the former's target back-end. The function
3528 ;; `org-export-snippet-backend' returns that back-end for a given
3531 (defun org-export-snippet-backend (export-snippet)
3532 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3533 Translation, with `org-export-snippet-translation-alist', is
3535 (let ((back-end (org-element-property :back-end export-snippet
)))
3537 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3543 ;; `org-export-collect-footnote-definitions' is a tool to list
3544 ;; actually used footnotes definitions in the whole parse tree, or in
3545 ;; a headline, in order to add footnote listings throughout the
3548 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3549 ;; back-ends, when they need to attach the footnote definition only to
3550 ;; the first occurrence of the corresponding label.
3552 ;; `org-export-get-footnote-definition' and
3553 ;; `org-export-get-footnote-number' provide easier access to
3554 ;; additional information relative to a footnote reference.
3556 (defun org-export-collect-footnote-definitions (data info
)
3557 "Return an alist between footnote numbers, labels and definitions.
3559 DATA is the parse tree from which definitions are collected.
3560 INFO is the plist used as a communication channel.
3562 Definitions are sorted by order of references. They either
3563 appear as Org data or as a secondary string for inlined
3564 footnotes. Unreferenced definitions are ignored."
3566 collect-fn
; for byte-compiler.
3570 ;; Collect footnote number, label and definition in DATA.
3571 (org-element-map data
'footnote-reference
3573 (when (org-export-footnote-first-reference-p fn info
)
3574 (let ((def (org-export-get-footnote-definition fn info
)))
3576 (list (org-export-get-footnote-number fn info
)
3577 (org-element-property :label fn
)
3580 ;; Also search in definition for nested footnotes.
3581 (when (eq (org-element-property :type fn
) 'standard
)
3582 (funcall collect-fn def
)))))
3583 ;; Don't enter footnote definitions since it will happen
3584 ;; when their first reference is found.
3585 info nil
'footnote-definition
)))))
3586 (funcall collect-fn
(plist-get info
:parse-tree
))
3587 (reverse num-alist
)))
3589 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3590 "Non-nil when a footnote reference is the first one for its label.
3592 FOOTNOTE-REFERENCE is the footnote reference being considered.
3593 INFO is the plist used as a communication channel."
3594 (let ((label (org-element-property :label footnote-reference
)))
3595 ;; Anonymous footnotes are always a first reference.
3597 ;; Otherwise, return the first footnote with the same LABEL and
3598 ;; test if it is equal to FOOTNOTE-REFERENCE.
3599 (let* (search-refs ; for byte-compiler.
3603 (org-element-map data
'footnote-reference
3606 ((string= (org-element-property :label fn
) label
)
3608 ;; If FN isn't inlined, be sure to traverse its
3609 ;; definition before resuming search. See
3610 ;; comments in `org-export-get-footnote-number'
3611 ;; for more information.
3612 ((eq (org-element-property :type fn
) 'standard
)
3613 (funcall search-refs
3614 (org-export-get-footnote-definition fn info
)))))
3615 ;; Don't enter footnote definitions since it will
3616 ;; happen when their first reference is found.
3617 info
'first-match
'footnote-definition
)))))
3618 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3619 footnote-reference
)))))
3621 (defun org-export-get-footnote-definition (footnote-reference info
)
3622 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3623 INFO is the plist used as a communication channel. If no such
3624 definition can be found, return the \"DEFINITION NOT FOUND\"
3626 (let ((label (org-element-property :label footnote-reference
)))
3627 (or (org-element-property :inline-definition footnote-reference
)
3628 (cdr (assoc label
(plist-get info
:footnote-definition-alist
)))
3629 "DEFINITION NOT FOUND.")))
3631 (defun org-export-get-footnote-number (footnote info
)
3632 "Return number associated to a footnote.
3634 FOOTNOTE is either a footnote reference or a footnote definition.
3635 INFO is the plist used as a communication channel."
3636 (let* ((label (org-element-property :label footnote
))
3638 search-ref
; For byte-compiler.
3642 ;; Search footnote references through DATA, filling
3643 ;; SEEN-REFS along the way.
3644 (org-element-map data
'footnote-reference
3646 (let ((fn-lbl (org-element-property :label fn
)))
3648 ;; Anonymous footnote match: return number.
3649 ((and (not fn-lbl
) (eq fn footnote
))
3650 (throw 'exit
(1+ (length seen-refs
))))
3651 ;; Labels match: return number.
3652 ((and label
(string= label fn-lbl
))
3653 (throw 'exit
(1+ (length seen-refs
))))
3654 ;; Anonymous footnote: it's always a new one.
3655 ;; Also, be sure to return nil from the `cond' so
3656 ;; `first-match' doesn't get us out of the loop.
3657 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3658 ;; Label not seen so far: add it so SEEN-REFS.
3660 ;; Also search for subsequent references in
3661 ;; footnote definition so numbering follows
3662 ;; reading logic. Note that we don't have to care
3663 ;; about inline definitions, since
3664 ;; `org-element-map' already traverses them at the
3667 ;; Once again, return nil to stay in the loop.
3668 ((not (member fn-lbl seen-refs
))
3669 (push fn-lbl seen-refs
)
3671 (org-export-get-footnote-definition fn info
))
3673 ;; Don't enter footnote definitions since it will
3674 ;; happen when their first reference is found.
3675 info
'first-match
'footnote-definition
)))))
3676 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3681 ;; `org-export-get-relative-level' is a shortcut to get headline
3682 ;; level, relatively to the lower headline level in the parsed tree.
3684 ;; `org-export-get-headline-number' returns the section number of an
3685 ;; headline, while `org-export-number-to-roman' allows to convert it
3686 ;; to roman numbers.
3688 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3689 ;; `org-export-last-sibling-p' are three useful predicates when it
3690 ;; comes to fulfill the `:headline-levels' property.
3692 ;; `org-export-get-tags', `org-export-get-category' and
3693 ;; `org-export-get-node-property' extract useful information from an
3694 ;; headline or a parent headline. They all handle inheritance.
3696 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3697 ;; as a secondary string, suitable for table of contents. It falls
3698 ;; back onto default title.
3700 (defun org-export-get-relative-level (headline info
)
3701 "Return HEADLINE relative level within current parsed tree.
3702 INFO is a plist holding contextual information."
3703 (+ (org-element-property :level headline
)
3704 (or (plist-get info
:headline-offset
) 0)))
3706 (defun org-export-low-level-p (headline info
)
3707 "Non-nil when HEADLINE is considered as low level.
3709 INFO is a plist used as a communication channel.
3711 A low level headlines has a relative level greater than
3712 `:headline-levels' property value.
3714 Return value is the difference between HEADLINE relative level
3715 and the last level being considered as high enough, or nil."
3716 (let ((limit (plist-get info
:headline-levels
)))
3717 (when (wholenump limit
)
3718 (let ((level (org-export-get-relative-level headline info
)))
3719 (and (> level limit
) (- level limit
))))))
3721 (defun org-export-get-headline-number (headline info
)
3722 "Return HEADLINE numbering as a list of numbers.
3723 INFO is a plist holding contextual information."
3724 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3726 (defun org-export-numbered-headline-p (headline info
)
3727 "Return a non-nil value if HEADLINE element should be numbered.
3728 INFO is a plist used as a communication channel."
3729 (let ((sec-num (plist-get info
:section-numbers
))
3730 (level (org-export-get-relative-level headline info
)))
3731 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3733 (defun org-export-number-to-roman (n)
3734 "Convert integer N into a roman numeral."
3735 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3736 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3737 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3741 (number-to-string n
)
3743 (if (>= n
(caar roman
))
3744 (setq n
(- n
(caar roman
))
3745 res
(concat res
(cdar roman
)))
3749 (defun org-export-get-tags (element info
&optional tags inherited
)
3750 "Return list of tags associated to ELEMENT.
3752 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3753 is a plist used as a communication channel.
3755 Select tags (see `org-export-select-tags') and exclude tags (see
3756 `org-export-exclude-tags') are removed from the list.
3758 When non-nil, optional argument TAGS should be a list of strings.
3759 Any tag belonging to this list will also be removed.
3761 When optional argument INHERITED is non-nil, tags can also be
3762 inherited from parent headlines and FILETAGS keywords."
3764 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3765 (member tag
(plist-get info
:exclude-tags
))
3767 (if (not inherited
) (org-element-property :tags element
)
3768 ;; Build complete list of inherited tags.
3769 (let ((current-tag-list (org-element-property :tags element
)))
3774 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3775 (not (member tag current-tag-list
)))
3776 (push tag current-tag-list
)))
3777 (org-element-property :tags parent
)))
3778 (org-export-get-genealogy element
))
3779 ;; Add FILETAGS keywords and return results.
3780 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3782 (defun org-export-get-node-property (property blob
&optional inherited
)
3783 "Return node PROPERTY value for BLOB.
3785 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3788 If optional argument INHERITED is non-nil, the value can be
3789 inherited from a parent headline.
3791 Return value is a string or nil."
3792 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3793 (org-export-get-parent-headline blob
))))
3794 (if (not inherited
) (org-element-property property blob
)
3795 (let ((parent headline
) value
)
3798 (when (plist-member (nth 1 parent
) property
)
3799 (throw 'found
(org-element-property property parent
)))
3800 (setq parent
(org-element-property :parent parent
))))))))
3802 (defun org-export-get-category (blob info
)
3803 "Return category for element or object BLOB.
3805 INFO is a plist used as a communication channel.
3807 CATEGORY is automatically inherited from a parent headline, from
3808 #+CATEGORY: keyword or created out of original file name. If all
3809 fail, the fall-back value is \"???\"."
3810 (or (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3811 (org-export-get-parent-headline blob
))))
3812 ;; Almost like `org-export-node-property', but we cannot trust
3813 ;; `plist-member' as every headline has a `:CATEGORY'
3814 ;; property, would it be nil or equal to "???" (which has the
3816 (let ((parent headline
) value
)
3819 (let ((category (org-element-property :CATEGORY parent
)))
3820 (and category
(not (equal "???" category
))
3821 (throw 'found category
)))
3822 (setq parent
(org-element-property :parent parent
))))))
3823 (org-element-map (plist-get info
:parse-tree
) 'keyword
3825 (when (equal (org-element-property :key kwd
) "CATEGORY")
3826 (org-element-property :value kwd
)))
3828 (let ((file (plist-get info
:input-file
)))
3829 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3832 (defun org-export-get-alt-title (headline info
)
3833 "Return alternative title for HEADLINE, as a secondary string.
3834 INFO is a plist used as a communication channel. If no optional
3835 title is defined, fall-back to the regular title."
3836 (or (org-element-property :alt-title headline
)
3837 (org-element-property :title headline
)))
3839 (defun org-export-first-sibling-p (headline info
)
3840 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3841 INFO is a plist used as a communication channel."
3842 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3845 (defun org-export-last-sibling-p (headline info
)
3846 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3847 INFO is a plist used as a communication channel."
3848 (not (org-export-get-next-element headline info
)))
3853 ;; `org-export-get-date' returns a date appropriate for the document
3854 ;; to about to be exported. In particular, it takes care of
3855 ;; `org-export-date-timestamp-format'.
3857 (defun org-export-get-date (info &optional fmt
)
3858 "Return date value for the current document.
3860 INFO is a plist used as a communication channel. FMT, when
3861 non-nil, is a time format string that will be applied on the date
3862 if it consists in a single timestamp object. It defaults to
3863 `org-export-date-timestamp-format' when nil.
3865 A proper date can be a secondary string, a string or nil. It is
3866 meant to be translated with `org-export-data' or alike."
3867 (let ((date (plist-get info
:date
))
3868 (fmt (or fmt org-export-date-timestamp-format
)))
3869 (cond ((not date
) nil
)
3872 (eq (org-element-type (car date
)) 'timestamp
))
3873 (org-timestamp-format (car date
) fmt
))
3879 ;; `org-export-solidify-link-text' turns a string into a safer version
3880 ;; for links, replacing most non-standard characters with hyphens.
3882 ;; `org-export-get-coderef-format' returns an appropriate format
3883 ;; string for coderefs.
3885 ;; `org-export-inline-image-p' returns a non-nil value when the link
3886 ;; provided should be considered as an inline image.
3888 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3889 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3890 ;; returns an appropriate unique identifier when found, or nil.
3892 ;; `org-export-resolve-id-link' returns the first headline with
3893 ;; specified id or custom-id in parse tree, the path to the external
3894 ;; file with the id or nil when neither was found.
3896 ;; `org-export-resolve-coderef' associates a reference to a line
3897 ;; number in the element it belongs, or returns the reference itself
3898 ;; when the element isn't numbered.
3900 (defun org-export-solidify-link-text (s)
3901 "Take link text S and make a safe target out of it."
3903 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-:]+") "-")))
3905 (defun org-export-get-coderef-format (path desc
)
3906 "Return format string for code reference link.
3907 PATH is the link path. DESC is its description."
3909 (cond ((not desc
) "%s")
3910 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3911 (replace-match "%s" t t desc
))
3914 (defun org-export-inline-image-p (link &optional rules
)
3915 "Non-nil if LINK object points to an inline image.
3917 Optional argument is a set of RULES defining inline images. It
3918 is an alist where associations have the following shape:
3922 Applying a rule means apply REGEXP against LINK's path when its
3923 type is TYPE. The function will return a non-nil value if any of
3924 the provided rules is non-nil. The default rule is
3925 `org-export-default-inline-image-rule'.
3927 This only applies to links without a description."
3928 (and (not (org-element-contents link
))
3929 (let ((case-fold-search t
)
3930 (rules (or rules org-export-default-inline-image-rule
)))
3934 (and (string= (org-element-property :type link
) (car rule
))
3935 (string-match (cdr rule
)
3936 (org-element-property :path link
))
3939 ;; Return nil if no rule matched.
3942 (defun org-export-resolve-coderef (ref info
)
3943 "Resolve a code reference REF.
3945 INFO is a plist used as a communication channel.
3947 Return associated line number in source code, or REF itself,
3948 depending on src-block or example element's switches."
3949 (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
3952 (insert (org-trim (org-element-property :value el
)))
3953 (let* ((label-fmt (regexp-quote
3954 (or (org-element-property :label-fmt el
)
3955 org-coderef-label-format
)))
3957 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3958 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3959 ;; Element containing REF is found. Resolve it to either
3960 ;; a label or a line number, as needed.
3961 (when (re-search-backward ref-re nil t
)
3963 ((org-element-property :use-labels el
) ref
)
3964 ((eq (org-element-property :number-lines el
) 'continued
)
3965 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3966 (t (line-number-at-pos)))))))
3969 (defun org-export-resolve-fuzzy-link (link info
)
3970 "Return LINK destination.
3972 INFO is a plist holding contextual information.
3974 Return value can be an object, an element, or nil:
3976 - If LINK path matches a target object (i.e. <<path>>) return it.
3978 - If LINK path exactly matches the name affiliated keyword
3979 \(i.e. #+NAME: path) of an element, return that element.
3981 - If LINK path exactly matches any headline name, return that
3982 element. If more than one headline share that name, priority
3983 will be given to the one with the closest common ancestor, if
3984 any, or the first one in the parse tree otherwise.
3986 - Otherwise, return nil.
3988 Assume LINK type is \"fuzzy\". White spaces are not
3990 (let* ((raw-path (org-element-property :path link
))
3991 (match-title-p (eq (aref raw-path
0) ?
*))
3992 ;; Split PATH at white spaces so matches are space
3994 (path (org-split-string
3995 (if match-title-p
(substring raw-path
1) raw-path
)))
3996 ;; Cache for destinations that are not position dependent.
3998 (or (plist-get info
:resolve-fuzzy-link-cache
)
3999 (plist-get (setq info
(plist-put info
:resolve-fuzzy-link-cache
4000 (make-hash-table :test
'equal
)))
4001 :resolve-fuzzy-link-cache
)))
4002 (cached (gethash path link-cache
'not-found
)))
4004 ;; Destination is not position dependent: use cached value.
4005 ((and (not match-title-p
) (not (eq cached
'not-found
))) cached
)
4006 ;; First try to find a matching "<<path>>" unless user specified
4007 ;; he was looking for a headline (path starts with a "*"
4009 ((and (not match-title-p
)
4010 (let ((match (org-element-map (plist-get info
:parse-tree
) 'target
4012 (and (equal (org-split-string
4013 (org-element-property :value blob
))
4016 info
'first-match
)))
4017 (and match
(puthash path match link-cache
)))))
4018 ;; Then try to find an element with a matching "#+NAME: path"
4019 ;; affiliated keyword.
4020 ((and (not match-title-p
)
4021 (let ((match (org-element-map (plist-get info
:parse-tree
)
4022 org-element-all-elements
4024 (let ((name (org-element-property :name el
)))
4026 (equal (org-split-string name
) path
))
4028 info
'first-match
)))
4029 (and match
(puthash path match link-cache
)))))
4030 ;; Last case: link either points to a headline or to nothingness.
4031 ;; Try to find the source, with priority given to headlines with
4032 ;; the closest common ancestor. If such candidate is found,
4033 ;; return it, otherwise return nil.
4035 (let ((find-headline
4037 ;; Return first headline whose `:raw-value' property is
4038 ;; NAME in parse tree DATA, or nil. Statistics cookies
4041 (org-element-map data
'headline
4043 (when (equal (org-split-string
4044 (replace-regexp-in-string
4045 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4046 (org-element-property :raw-value headline
)))
4049 info
'first-match
)))))
4050 ;; Search among headlines sharing an ancestor with link, from
4051 ;; closest to farthest.
4055 (let ((foundp (funcall find-headline path parent
)))
4056 (when foundp
(throw 'exit foundp
))))
4057 (let ((parent-hl (org-export-get-parent-headline link
)))
4058 (if (not parent-hl
) (list (plist-get info
:parse-tree
))
4059 (cons parent-hl
(org-export-get-genealogy parent-hl
)))))
4060 ;; No destination found: return nil.
4061 (and (not match-title-p
) (puthash path nil link-cache
))))))))
4063 (defun org-export-resolve-id-link (link info
)
4064 "Return headline referenced as LINK destination.
4066 INFO is a plist used as a communication channel.
4068 Return value can be the headline element matched in current parse
4069 tree, a file name or nil. Assume LINK type is either \"id\" or
4071 (let ((id (org-element-property :path link
)))
4072 ;; First check if id is within the current parse tree.
4073 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4075 (when (or (string= (org-element-property :ID headline
) id
)
4076 (string= (org-element-property :CUSTOM_ID headline
) id
))
4079 ;; Otherwise, look for external files.
4080 (cdr (assoc id
(plist-get info
:id-alist
))))))
4082 (defun org-export-resolve-radio-link (link info
)
4083 "Return radio-target object referenced as LINK destination.
4085 INFO is a plist used as a communication channel.
4087 Return value can be a radio-target object or nil. Assume LINK
4088 has type \"radio\"."
4089 (let ((path (replace-regexp-in-string
4090 "[ \r\t\n]+" " " (org-element-property :path link
))))
4091 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4093 (and (eq (compare-strings
4094 (replace-regexp-in-string
4095 "[ \r\t\n]+" " " (org-element-property :value radio
))
4096 nil nil path nil nil t
)
4099 info
'first-match
)))
4104 ;; `org-export-get-ordinal' associates a sequence number to any object
4107 (defun org-export-get-ordinal (element info
&optional types predicate
)
4108 "Return ordinal number of an element or object.
4110 ELEMENT is the element or object considered. INFO is the plist
4111 used as a communication channel.
4113 Optional argument TYPES, when non-nil, is a list of element or
4114 object types, as symbols, that should also be counted in.
4115 Otherwise, only provided element's type is considered.
4117 Optional argument PREDICATE is a function returning a non-nil
4118 value if the current element or object should be counted in. It
4119 accepts two arguments: the element or object being considered and
4120 the plist used as a communication channel. This allows to count
4121 only a certain type of objects (i.e. inline images).
4123 Return value is a list of numbers if ELEMENT is a headline or an
4124 item. It is nil for keywords. It represents the footnote number
4125 for footnote definitions and footnote references. If ELEMENT is
4126 a target, return the same value as if ELEMENT was the closest
4127 table, item or headline containing the target. In any other
4128 case, return the sequence number of ELEMENT among elements or
4129 objects of the same type."
4130 ;; Ordinal of a target object refer to the ordinal of the closest
4131 ;; table, item, or headline containing the object.
4132 (when (eq (org-element-type element
) 'target
)
4134 (loop for parent in
(org-export-get-genealogy element
)
4137 (org-element-type parent
)
4138 '(footnote-definition footnote-reference headline item
4141 (case (org-element-type element
)
4142 ;; Special case 1: A headline returns its number as a list.
4143 (headline (org-export-get-headline-number element info
))
4144 ;; Special case 2: An item returns its number as a list.
4145 (item (let ((struct (org-element-property :structure element
)))
4146 (org-list-get-item-number
4147 (org-element-property :begin element
)
4149 (org-list-prevs-alist struct
)
4150 (org-list-parents-alist struct
))))
4151 ((footnote-definition footnote-reference
)
4152 (org-export-get-footnote-number element info
))
4155 ;; Increment counter until ELEMENT is found again.
4156 (org-element-map (plist-get info
:parse-tree
)
4157 (or types
(org-element-type element
))
4160 ((eq element el
) (1+ counter
))
4161 ((not predicate
) (incf counter
) nil
)
4162 ((funcall predicate el info
) (incf counter
) nil
)))
4163 info
'first-match
)))))
4168 ;; `org-export-get-loc' counts number of code lines accumulated in
4169 ;; src-block or example-block elements with a "+n" switch until
4170 ;; a given element, excluded. Note: "-n" switches reset that count.
4172 ;; `org-export-unravel-code' extracts source code (along with a code
4173 ;; references alist) from an `element-block' or `src-block' type
4176 ;; `org-export-format-code' applies a formatting function to each line
4177 ;; of code, providing relative line number and code reference when
4178 ;; appropriate. Since it doesn't access the original element from
4179 ;; which the source code is coming, it expects from the code calling
4180 ;; it to know if lines should be numbered and if code references
4183 ;; Eventually, `org-export-format-code-default' is a higher-level
4184 ;; function (it makes use of the two previous functions) which handles
4185 ;; line numbering and code references inclusion, and returns source
4186 ;; code in a format suitable for plain text or verbatim output.
4188 (defun org-export-get-loc (element info
)
4189 "Return accumulated lines of code up to ELEMENT.
4191 INFO is the plist used as a communication channel.
4193 ELEMENT is excluded from count."
4195 (org-element-map (plist-get info
:parse-tree
)
4196 `(src-block example-block
,(org-element-type element
))
4199 ;; ELEMENT is reached: Quit the loop.
4201 ;; Only count lines from src-block and example-block elements
4202 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4203 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4204 ((let ((linums (org-element-property :number-lines el
)))
4206 ;; Accumulate locs or reset them.
4207 (let ((lines (org-count-lines
4208 (org-trim (org-element-property :value el
)))))
4209 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4210 ;; Return nil to stay in the loop.
4216 (defun org-export-unravel-code (element)
4217 "Clean source code and extract references out of it.
4219 ELEMENT has either a `src-block' an `example-block' type.
4221 Return a cons cell whose CAR is the source code, cleaned from any
4222 reference, protective commas and spurious indentation, and CDR is
4223 an alist between relative line number (integer) and name of code
4224 reference on that line (string)."
4225 (let* ((line 0) refs
4226 (value (org-element-property :value element
))
4227 ;; Get code and clean it. Remove blank lines at its
4228 ;; beginning and end.
4229 (code (replace-regexp-in-string
4230 "\\`\\([ \t]*\n\\)+" ""
4231 (replace-regexp-in-string
4232 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4233 (if (or org-src-preserve-indentation
4234 (org-element-property :preserve-indent element
))
4236 (org-element-remove-indentation value
)))))
4237 ;; Get format used for references.
4238 (label-fmt (regexp-quote
4239 (or (org-element-property :label-fmt element
)
4240 org-coderef-label-format
)))
4241 ;; Build a regexp matching a loc with a reference.
4243 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4244 (replace-regexp-in-string
4245 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4248 ;; Code with references removed.
4249 (org-element-normalize-string
4253 (if (not (string-match with-ref-re loc
)) loc
4254 ;; Ref line: remove ref, and signal its position in REFS.
4255 (push (cons line
(match-string 3 loc
)) refs
)
4256 (replace-match "" nil nil loc
1)))
4257 (org-split-string code
"\n") "\n"))
4261 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4262 "Format CODE by applying FUN line-wise and return it.
4264 CODE is a string representing the code to format. FUN is
4265 a function. It must accept three arguments: a line of
4266 code (string), the current line number (integer) or nil and the
4267 reference associated to the current line (string) or nil.
4269 Optional argument NUM-LINES can be an integer representing the
4270 number of code lines accumulated until the current code. Line
4271 numbers passed to FUN will take it into account. If it is nil,
4272 FUN's second argument will always be nil. This number can be
4273 obtained with `org-export-get-loc' function.
4275 Optional argument REF-ALIST can be an alist between relative line
4276 number (i.e. ignoring NUM-LINES) and the name of the code
4277 reference on it. If it is nil, FUN's third argument will always
4278 be nil. It can be obtained through the use of
4279 `org-export-unravel-code' function."
4280 (let ((--locs (org-split-string code
"\n"))
4282 (org-element-normalize-string
4286 (let ((--ref (cdr (assq --line ref-alist
))))
4287 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4290 (defun org-export-format-code-default (element info
)
4291 "Return source code from ELEMENT, formatted in a standard way.
4293 ELEMENT is either a `src-block' or `example-block' element. INFO
4294 is a plist used as a communication channel.
4296 This function takes care of line numbering and code references
4297 inclusion. Line numbers, when applicable, appear at the
4298 beginning of the line, separated from the code by two white
4299 spaces. Code references, on the other hand, appear flushed to
4300 the right, separated by six white spaces from the widest line of
4302 ;; Extract code and references.
4303 (let* ((code-info (org-export-unravel-code element
))
4304 (code (car code-info
))
4305 (code-lines (org-split-string code
"\n")))
4306 (if (null code-lines
) ""
4307 (let* ((refs (and (org-element-property :retain-labels element
)
4309 ;; Handle line numbering.
4310 (num-start (case (org-element-property :number-lines element
)
4311 (continued (org-export-get-loc element info
))
4316 (length (number-to-string
4317 (+ (length code-lines
) num-start
))))))
4318 ;; Prepare references display, if required. Any reference
4319 ;; should start six columns after the widest line of code,
4320 ;; wrapped with parenthesis.
4322 (+ (apply 'max
(mapcar 'length code-lines
))
4323 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4324 (org-export-format-code
4326 (lambda (loc line-num ref
)
4327 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4332 (concat (make-string
4334 (+ (length loc
) (length number-str
))) ?
)
4335 (format "(%s)" ref
))))))
4341 ;; `org-export-table-has-special-column-p' and and
4342 ;; `org-export-table-row-is-special-p' are predicates used to look for
4343 ;; meta-information about the table structure.
4345 ;; `org-table-has-header-p' tells when the rows before the first rule
4346 ;; should be considered as table's header.
4348 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4349 ;; and `org-export-table-cell-borders' extract information from
4350 ;; a table-cell element.
4352 ;; `org-export-table-dimensions' gives the number on rows and columns
4353 ;; in the table, ignoring horizontal rules and special columns.
4354 ;; `org-export-table-cell-address', given a table-cell object, returns
4355 ;; the absolute address of a cell. On the other hand,
4356 ;; `org-export-get-table-cell-at' does the contrary.
4358 ;; `org-export-table-cell-starts-colgroup-p',
4359 ;; `org-export-table-cell-ends-colgroup-p',
4360 ;; `org-export-table-row-starts-rowgroup-p',
4361 ;; `org-export-table-row-ends-rowgroup-p',
4362 ;; `org-export-table-row-starts-header-p' and
4363 ;; `org-export-table-row-ends-header-p' indicate position of current
4364 ;; row or cell within the table.
4366 (defun org-export-table-has-special-column-p (table)
4367 "Non-nil when TABLE has a special column.
4368 All special columns will be ignored during export."
4369 ;; The table has a special column when every first cell of every row
4370 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4371 ;; "*" "_" and "^". Though, do not consider a first row containing
4372 ;; only empty cells as special.
4373 (let ((special-column-p 'empty
))
4377 (when (eq (org-element-property :type row
) 'standard
)
4378 (let ((value (org-element-contents
4379 (car (org-element-contents row
)))))
4380 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4381 (setq special-column-p
'special
))
4383 (t (throw 'exit nil
))))))
4384 (org-element-contents table
))
4385 (eq special-column-p
'special
))))
4387 (defun org-export-table-has-header-p (table info
)
4388 "Non-nil when TABLE has a header.
4390 INFO is a plist used as a communication channel.
4392 A table has a header when it contains at least two row groups."
4393 (let ((cache (or (plist-get info
:table-header-cache
)
4394 (plist-get (setq info
4395 (plist-put info
:table-header-cache
4396 (make-hash-table :test
'eq
)))
4397 :table-header-cache
))))
4398 (or (gethash table cache
)
4399 (let ((rowgroup 1) row-flag
)
4402 (org-element-map table
'table-row
4406 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4407 (incf rowgroup
) (setq row-flag nil
))
4408 ((and (not row-flag
) (eq (org-element-property :type row
)
4410 (setq row-flag t
) nil
)))
4414 (defun org-export-table-row-is-special-p (table-row info
)
4415 "Non-nil if TABLE-ROW is considered special.
4417 INFO is a plist used as the communication channel.
4419 All special rows will be ignored during export."
4420 (when (eq (org-element-property :type table-row
) 'standard
)
4421 (let ((first-cell (org-element-contents
4422 (car (org-element-contents table-row
)))))
4423 ;; A row is special either when...
4425 ;; ... it starts with a field only containing "/",
4426 (equal first-cell
'("/"))
4427 ;; ... the table contains a special column and the row start
4428 ;; with a marking character among, "^", "_", "$" or "!",
4429 (and (org-export-table-has-special-column-p
4430 (org-export-get-parent table-row
))
4431 (member first-cell
'(("^") ("_") ("$") ("!"))))
4432 ;; ... it contains only alignment cookies and empty cells.
4433 (let ((special-row-p 'empty
))
4437 (let ((value (org-element-contents cell
)))
4438 ;; Since VALUE is a secondary string, the following
4439 ;; checks avoid expanding it with `org-export-data'.
4441 ((and (not (cdr value
))
4442 (stringp (car value
))
4443 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4445 (setq special-row-p
'cookie
))
4446 (t (throw 'exit nil
)))))
4447 (org-element-contents table-row
))
4448 (eq special-row-p
'cookie
)))))))
4450 (defun org-export-table-row-group (table-row info
)
4451 "Return TABLE-ROW's group number, as an integer.
4453 INFO is a plist used as the communication channel.
4455 Return value is the group number, as an integer, or nil for
4456 special rows and rows separators. First group is also table's
4458 (let ((cache (or (plist-get info
:table-row-group-cache
)
4459 (plist-get (setq info
4460 (plist-put info
:table-row-group-cache
4461 (make-hash-table :test
'eq
)))
4462 :table-row-group-cache
))))
4463 (cond ((gethash table-row cache
))
4464 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4465 (t (let ((group 0) row-flag
)
4466 (org-element-map (org-export-get-parent table-row
) 'table-row
4468 (if (eq (org-element-property :type row
) 'rule
)
4470 (unless row-flag
(incf group
) (setq row-flag t
)))
4471 (when (eq table-row row
) (puthash table-row group cache
)))
4472 info
'first-match
))))))
4474 (defun org-export-table-cell-width (table-cell info
)
4475 "Return TABLE-CELL contents width.
4477 INFO is a plist used as the communication channel.
4479 Return value is the width given by the last width cookie in the
4480 same column as TABLE-CELL, or nil."
4481 (let* ((row (org-export-get-parent table-cell
))
4482 (table (org-export-get-parent row
))
4483 (cells (org-element-contents row
))
4484 (columns (length cells
))
4485 (column (- columns
(length (memq table-cell cells
))))
4486 (cache (or (plist-get info
:table-cell-width-cache
)
4487 (plist-get (setq info
4488 (plist-put info
:table-cell-width-cache
4489 (make-hash-table :test
'eq
)))
4490 :table-cell-width-cache
)))
4491 (width-vector (or (gethash table cache
)
4492 (puthash table
(make-vector columns
'empty
) cache
)))
4493 (value (aref width-vector column
)))
4494 (if (not (eq value
'empty
)) value
4496 (dolist (row (org-element-contents table
)
4497 (aset width-vector column cookie-width
))
4498 (when (org-export-table-row-is-special-p row info
)
4499 ;; In a special row, try to find a width cookie at COLUMN.
4500 (let* ((value (org-element-contents
4501 (elt (org-element-contents row
) column
)))
4502 (cookie (car value
)))
4503 ;; The following checks avoid expanding unnecessarily
4504 ;; the cell with `org-export-data'.
4508 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4509 (match-string 1 cookie
))
4511 (string-to-number (match-string 1 cookie
)))))))))))
4513 (defun org-export-table-cell-alignment (table-cell info
)
4514 "Return TABLE-CELL contents alignment.
4516 INFO is a plist used as the communication channel.
4518 Return alignment as specified by the last alignment cookie in the
4519 same column as TABLE-CELL. If no such cookie is found, a default
4520 alignment value will be deduced from fraction of numbers in the
4521 column (see `org-table-number-fraction' for more information).
4522 Possible values are `left', `right' and `center'."
4523 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4524 (require 'org-table
)
4525 (let* ((row (org-export-get-parent table-cell
))
4526 (table (org-export-get-parent row
))
4527 (cells (org-element-contents row
))
4528 (columns (length cells
))
4529 (column (- columns
(length (memq table-cell cells
))))
4530 (cache (or (plist-get info
:table-cell-alignment-cache
)
4531 (plist-get (setq info
4532 (plist-put info
:table-cell-alignment-cache
4533 (make-hash-table :test
'eq
)))
4534 :table-cell-alignment-cache
)))
4535 (align-vector (or (gethash table cache
)
4536 (puthash table
(make-vector columns nil
) cache
))))
4537 (or (aref align-vector column
)
4538 (let ((number-cells 0)
4541 previous-cell-number-p
)
4542 (dolist (row (org-element-contents (org-export-get-parent row
)))
4544 ;; In a special row, try to find an alignment cookie at
4546 ((org-export-table-row-is-special-p row info
)
4547 (let ((value (org-element-contents
4548 (elt (org-element-contents row
) column
))))
4549 ;; Since VALUE is a secondary string, the following
4550 ;; checks avoid useless expansion through
4551 ;; `org-export-data'.
4554 (stringp (car value
))
4555 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4557 (match-string 1 (car value
)))
4558 (setq cookie-align
(match-string 1 (car value
))))))
4559 ;; Ignore table rules.
4560 ((eq (org-element-property :type row
) 'rule
))
4561 ;; In a standard row, check if cell's contents are
4562 ;; expressing some kind of number. Increase NUMBER-CELLS
4563 ;; accordingly. Though, don't bother if an alignment
4564 ;; cookie has already defined cell's alignment.
4566 (let ((value (org-export-data
4567 (org-element-contents
4568 (elt (org-element-contents row
) column
))
4571 ;; Treat an empty cell as a number if it follows
4573 (if (not (or (string-match org-table-number-regexp value
)
4574 (and (string= value
"") previous-cell-number-p
)))
4575 (setq previous-cell-number-p nil
)
4576 (setq previous-cell-number-p t
)
4577 (incf number-cells
))))))
4578 ;; Return value. Alignment specified by cookies has
4579 ;; precedence over alignment deduced from cell's contents.
4582 (cond ((equal cookie-align
"l") 'left
)
4583 ((equal cookie-align
"r") 'right
)
4584 ((equal cookie-align
"c") 'center
)
4585 ((>= (/ (float number-cells
) total-cells
)
4586 org-table-number-fraction
)
4590 (defun org-export-table-cell-borders (table-cell info
)
4591 "Return TABLE-CELL borders.
4593 INFO is a plist used as a communication channel.
4595 Return value is a list of symbols, or nil. Possible values are:
4596 `top', `bottom', `above', `below', `left' and `right'. Note:
4597 `top' (resp. `bottom') only happen for a cell in the first
4598 row (resp. last row) of the table, ignoring table rules, if any.
4600 Returned borders ignore special rows."
4601 (let* ((row (org-export-get-parent table-cell
))
4602 (table (org-export-get-parent-table table-cell
))
4604 ;; Top/above border? TABLE-CELL has a border above when a rule
4605 ;; used to demarcate row groups can be found above. Hence,
4606 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4607 ;; another regular row has to be found above that rule.
4611 (cond ((eq (org-element-property :type row
) 'rule
)
4613 ((not (org-export-table-row-is-special-p row info
))
4614 (if rule-flag
(throw 'exit
(push 'above borders
))
4615 (throw 'exit nil
)))))
4616 ;; Look at every row before the current one.
4617 (cdr (memq row
(reverse (org-element-contents table
)))))
4618 ;; No rule above, or rule found starts the table (ignoring any
4619 ;; special row): TABLE-CELL is at the top of the table.
4620 (when rule-flag
(push 'above borders
))
4621 (push 'top borders
)))
4622 ;; Bottom/below border? TABLE-CELL has a border below when next
4623 ;; non-regular row below is a rule.
4627 (cond ((eq (org-element-property :type row
) 'rule
)
4629 ((not (org-export-table-row-is-special-p row info
))
4630 (if rule-flag
(throw 'exit
(push 'below borders
))
4631 (throw 'exit nil
)))))
4632 ;; Look at every row after the current one.
4633 (cdr (memq row
(org-element-contents table
))))
4634 ;; No rule below, or rule found ends the table (modulo some
4635 ;; special row): TABLE-CELL is at the bottom of the table.
4636 (when rule-flag
(push 'below borders
))
4637 (push 'bottom borders
)))
4638 ;; Right/left borders? They can only be specified by column
4639 ;; groups. Column groups are defined in a row starting with "/".
4640 ;; Also a column groups row only contains "<", "<>", ">" or blank
4643 (let ((column (let ((cells (org-element-contents row
)))
4644 (- (length cells
) (length (memq table-cell cells
))))))
4647 (unless (eq (org-element-property :type row
) 'rule
)
4648 (when (equal (org-element-contents
4649 (car (org-element-contents row
)))
4651 (let ((column-groups
4654 (let ((value (org-element-contents cell
)))
4655 (when (member value
'(("<") ("<>") (">") nil
))
4657 (org-element-contents row
))))
4658 ;; There's a left border when previous cell, if
4659 ;; any, ends a group, or current one starts one.
4660 (when (or (and (not (zerop column
))
4661 (member (elt column-groups
(1- column
))
4663 (member (elt column-groups column
) '("<" "<>")))
4664 (push 'left borders
))
4665 ;; There's a right border when next cell, if any,
4666 ;; starts a group, or current one ends one.
4667 (when (or (and (/= (1+ column
) (length column-groups
))
4668 (member (elt column-groups
(1+ column
))
4670 (member (elt column-groups column
) '(">" "<>")))
4671 (push 'right borders
))
4672 (throw 'exit nil
)))))
4673 ;; Table rows are read in reverse order so last column groups
4674 ;; row has precedence over any previous one.
4675 (reverse (org-element-contents table
)))))
4679 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4680 "Non-nil when TABLE-CELL is at the beginning of a column group.
4681 INFO is a plist used as a communication channel."
4682 ;; A cell starts a column group either when it is at the beginning
4683 ;; of a row (or after the special column, if any) or when it has
4685 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4686 'identity info
'first-match
)
4688 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4690 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4691 "Non-nil when TABLE-CELL is at the end of a column group.
4692 INFO is a plist used as a communication channel."
4693 ;; A cell ends a column group either when it is at the end of a row
4694 ;; or when it has a right border.
4695 (or (eq (car (last (org-element-contents
4696 (org-export-get-parent table-cell
))))
4698 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4700 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4701 "Non-nil when TABLE-ROW is at the beginning of a row group.
4702 INFO is a plist used as a communication channel."
4703 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4704 (org-export-table-row-is-special-p table-row info
))
4705 (let ((borders (org-export-table-cell-borders
4706 (car (org-element-contents table-row
)) info
)))
4707 (or (memq 'top borders
) (memq 'above borders
)))))
4709 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4710 "Non-nil when TABLE-ROW is at the end of a row group.
4711 INFO is a plist used as a communication channel."
4712 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4713 (org-export-table-row-is-special-p table-row info
))
4714 (let ((borders (org-export-table-cell-borders
4715 (car (org-element-contents table-row
)) info
)))
4716 (or (memq 'bottom borders
) (memq 'below borders
)))))
4718 (defun org-export-table-row-starts-header-p (table-row info
)
4719 "Non-nil when TABLE-ROW is the first table header's row.
4720 INFO is a plist used as a communication channel."
4721 (and (org-export-table-has-header-p
4722 (org-export-get-parent-table table-row
) info
)
4723 (org-export-table-row-starts-rowgroup-p table-row info
)
4724 (= (org-export-table-row-group table-row info
) 1)))
4726 (defun org-export-table-row-ends-header-p (table-row info
)
4727 "Non-nil when TABLE-ROW is the last table header's row.
4728 INFO is a plist used as a communication channel."
4729 (and (org-export-table-has-header-p
4730 (org-export-get-parent-table table-row
) info
)
4731 (org-export-table-row-ends-rowgroup-p table-row info
)
4732 (= (org-export-table-row-group table-row info
) 1)))
4734 (defun org-export-table-row-number (table-row info
)
4735 "Return TABLE-ROW number.
4736 INFO is a plist used as a communication channel. Return value is
4737 zero-based and ignores separators. The function returns nil for
4738 special columns and separators."
4739 (when (and (eq (org-element-property :type table-row
) 'standard
)
4740 (not (org-export-table-row-is-special-p table-row info
)))
4742 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4744 (cond ((eq row table-row
) number
)
4745 ((eq (org-element-property :type row
) 'standard
)
4746 (incf number
) nil
)))
4747 info
'first-match
))))
4749 (defun org-export-table-dimensions (table info
)
4750 "Return TABLE dimensions.
4752 INFO is a plist used as a communication channel.
4754 Return value is a CONS like (ROWS . COLUMNS) where
4755 ROWS (resp. COLUMNS) is the number of exportable
4756 rows (resp. columns)."
4757 (let (first-row (columns 0) (rows 0))
4758 ;; Set number of rows, and extract first one.
4759 (org-element-map table
'table-row
4761 (when (eq (org-element-property :type row
) 'standard
)
4763 (unless first-row
(setq first-row row
)))) info
)
4764 ;; Set number of columns.
4765 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4767 (cons rows columns
)))
4769 (defun org-export-table-cell-address (table-cell info
)
4770 "Return address of a regular TABLE-CELL object.
4772 TABLE-CELL is the cell considered. INFO is a plist used as
4773 a communication channel.
4775 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4776 zero-based index. Only exportable cells are considered. The
4777 function returns nil for other cells."
4778 (let* ((table-row (org-export-get-parent table-cell
))
4779 (row-number (org-export-table-row-number table-row info
)))
4782 (let ((col-count 0))
4783 (org-element-map table-row
'table-cell
4785 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4786 info
'first-match
))))))
4788 (defun org-export-get-table-cell-at (address table info
)
4789 "Return regular table-cell object at ADDRESS in TABLE.
4791 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4792 zero-based index. TABLE is a table type element. INFO is
4793 a plist used as a communication channel.
4795 If no table-cell, among exportable cells, is found at ADDRESS,
4797 (let ((column-pos (cdr address
)) (column-count 0))
4799 ;; Row at (car address) or nil.
4800 (let ((row-pos (car address
)) (row-count 0))
4801 (org-element-map table
'table-row
4803 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4804 ((= row-count row-pos
) row
)
4805 (t (incf row-count
) nil
)))
4809 (if (= column-count column-pos
) cell
4810 (incf column-count
) nil
))
4811 info
'first-match
)))
4814 ;;;; For Tables Of Contents
4816 ;; `org-export-collect-headlines' builds a list of all exportable
4817 ;; headline elements, maybe limited to a certain depth. One can then
4818 ;; easily parse it and transcode it.
4820 ;; Building lists of tables, figures or listings is quite similar.
4821 ;; Once the generic function `org-export-collect-elements' is defined,
4822 ;; `org-export-collect-tables', `org-export-collect-figures' and
4823 ;; `org-export-collect-listings' can be derived from it.
4825 (defun org-export-collect-headlines (info &optional n
)
4826 "Collect headlines in order to build a table of contents.
4828 INFO is a plist used as a communication channel.
4830 When optional argument N is an integer, it specifies the depth of
4831 the table of contents. Otherwise, it is set to the value of the
4832 last headline level. See `org-export-headline-levels' for more
4835 Return a list of all exportable headlines as parsed elements.
4836 Footnote sections, if any, will be ignored."
4837 (let ((limit (plist-get info
:headline-levels
)))
4838 (setq n
(if (wholenump n
) (min n limit
) limit
))
4839 (org-element-map (plist-get info
:parse-tree
) 'headline
4840 #'(lambda (headline)
4841 (unless (org-element-property :footnote-section-p headline
)
4842 (let ((level (org-export-get-relative-level headline info
)))
4843 (and (<= level n
) headline
))))
4846 (defun org-export-collect-elements (type info
&optional predicate
)
4847 "Collect referenceable elements of a determined type.
4849 TYPE can be a symbol or a list of symbols specifying element
4850 types to search. Only elements with a caption are collected.
4852 INFO is a plist used as a communication channel.
4854 When non-nil, optional argument PREDICATE is a function accepting
4855 one argument, an element of type TYPE. It returns a non-nil
4856 value when that element should be collected.
4858 Return a list of all elements found, in order of appearance."
4859 (org-element-map (plist-get info
:parse-tree
) type
4861 (and (org-element-property :caption element
)
4862 (or (not predicate
) (funcall predicate element
))
4866 (defun org-export-collect-tables (info)
4867 "Build a list of tables.
4868 INFO is a plist used as a communication channel.
4870 Return a list of table elements with a caption."
4871 (org-export-collect-elements 'table info
))
4873 (defun org-export-collect-figures (info predicate
)
4874 "Build a list of figures.
4876 INFO is a plist used as a communication channel. PREDICATE is
4877 a function which accepts one argument: a paragraph element and
4878 whose return value is non-nil when that element should be
4881 A figure is a paragraph type element, with a caption, verifying
4882 PREDICATE. The latter has to be provided since a \"figure\" is
4883 a vague concept that may depend on back-end.
4885 Return a list of elements recognized as figures."
4886 (org-export-collect-elements 'paragraph info predicate
))
4888 (defun org-export-collect-listings (info)
4889 "Build a list of src blocks.
4891 INFO is a plist used as a communication channel.
4893 Return a list of src-block elements with a caption."
4894 (org-export-collect-elements 'src-block info
))
4899 ;; The main function for the smart quotes sub-system is
4900 ;; `org-export-activate-smart-quotes', which replaces every quote in
4901 ;; a given string from the parse tree with its "smart" counterpart.
4903 ;; Dictionary for smart quotes is stored in
4904 ;; `org-export-smart-quotes-alist'.
4906 ;; Internally, regexps matching potential smart quotes (checks at
4907 ;; string boundaries are also necessary) are defined in
4908 ;; `org-export-smart-quotes-regexps'.
4910 (defconst org-export-smart-quotes-alist
4912 ;; one may use: »...«, "...", ›...‹, or '...'.
4913 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4914 ;; LaTeX quotes require Babel!
4915 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
4916 :texinfo
"@guillemetright{}")
4917 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
4918 :texinfo
"@guillemetleft{}")
4919 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
4920 :texinfo
"@guilsinglright{}")
4921 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
4922 :texinfo
"@guilsingleft{}")
4923 (apostrophe :utf-8
"’" :html
"’"))
4925 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4926 :texinfo
"@quotedblbase{}")
4927 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4928 :texinfo
"@quotedblleft{}")
4929 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4930 :texinfo
"@quotesinglbase{}")
4931 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4932 :texinfo
"@quoteleft{}")
4933 (apostrophe :utf-8
"’" :html
"’"))
4935 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4936 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4937 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4938 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4939 (apostrophe :utf-8
"’" :html
"’"))
4941 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4942 :texinfo
"@guillemetleft{}")
4943 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4944 :texinfo
"@guillemetright{}")
4945 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4946 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4947 (apostrophe :utf-8
"’" :html
"’"))
4949 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
4950 :texinfo
"@guillemetleft{}@tie{}")
4951 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4952 :texinfo
"@tie{}@guillemetright{}")
4953 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
4954 :texinfo
"@guillemetleft{}@tie{}")
4955 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4956 :texinfo
"@tie{}@guillemetright{}")
4957 (apostrophe :utf-8
"’" :html
"’"))
4959 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4960 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4961 :texinfo
"@guillemetleft{}")
4962 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4963 :texinfo
"@guillemetright{}")
4964 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4965 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4966 (apostrophe :utf-8
"’" :html
"’"))
4968 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4969 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4970 :texinfo
"@guillemetleft{}")
4971 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4972 :texinfo
"@guillemetright{}")
4973 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4974 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4975 (apostrophe :utf-8
"’" :html
"’"))
4977 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4978 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4979 :texinfo
"@guillemetleft{}")
4980 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4981 :texinfo
"@guillemetright{}")
4982 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4983 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4984 (apostrophe :utf-8
"’" :html
"’"))
4986 ;; 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
4987 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
4988 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
4989 :texinfo
"@guillemetleft{}")
4990 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
4991 :texinfo
"@guillemetright{}")
4992 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
4993 :texinfo
"@quotedblbase{}")
4994 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
4995 :texinfo
"@quotedblleft{}")
4996 (apostrophe :utf-8
"’" :html
: "'"))
4998 ;; based on https://sv.wikipedia.org/wiki/Citattecken
4999 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5000 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5001 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5002 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5003 (apostrophe :utf-8
"’" :html
"’"))
5005 "Smart quotes translations.
5007 Alist whose CAR is a language string and CDR is an alist with
5008 quote type as key and a plist associating various encodings to
5009 their translation as value.
5011 A quote type can be any symbol among `opening-double-quote',
5012 `closing-double-quote', `opening-single-quote',
5013 `closing-single-quote' and `apostrophe'.
5015 Valid encodings include `:utf-8', `:html', `:latex' and
5018 If no translation is found, the quote character is left as-is.")
5020 (defconst org-export-smart-quotes-regexps
5022 ;; Possible opening quote at beginning of string.
5023 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
5024 ;; Possible closing quote at beginning of string.
5025 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5026 ;; Possible apostrophe at beginning of string.
5028 ;; Opening single and double quotes.
5029 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5030 ;; Closing single and double quotes.
5031 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5034 ;; Possible opening quote at end of string.
5035 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5036 ;; Possible closing quote at end of string.
5037 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5038 ;; Possible apostrophe at end of string.
5040 "List of regexps matching a quote or an apostrophe.
5041 In every regexp, quote or apostrophe matched is put in group 1.")
5043 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5044 "Replace regular quotes with \"smart\" quotes in string S.
5046 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5047 `:utf-8'. INFO is a plist used as a communication channel.
5049 The function has to retrieve information about string
5050 surroundings in parse tree. It can only happen with an
5051 unmodified string. Thus, if S has already been through another
5052 process, a non-nil ORIGINAL optional argument will provide that
5055 Return the new string."
5057 (let* ((prev (org-export-get-previous-element (or original s
) info
))
5058 ;; Try to be flexible when computing number of blanks
5059 ;; before object. The previous object may be a string
5060 ;; introduced by the back-end and not completely parsed.
5061 (pre-blank (and prev
5062 (or (org-element-property :post-blank prev
)
5063 ;; A string with missing `:post-blank'
5066 (string-match " *\\'" prev
)
5067 (length (match-string 0 prev
)))
5070 (next (org-export-get-next-element (or original s
) info
))
5073 ;; Return smart quote associated to a give quote Q, as
5074 ;; a string. TYPE is a symbol among `open', `close' and
5076 (let ((key (case type
5077 (apostrophe 'apostrophe
)
5078 (open (if (equal "'" q
) 'opening-single-quote
5079 'opening-double-quote
))
5080 (otherwise (if (equal "'" q
) 'closing-single-quote
5081 'closing-double-quote
)))))
5084 (cdr (assoc (plist-get info
:language
)
5085 org-export-smart-quotes-alist
))))
5088 (if (or (equal "\"" s
) (equal "'" s
))
5089 ;; Only a quote: no regexp can match. We have to check both
5090 ;; sides and decide what to do.
5091 (cond ((and (not prev
) (not next
)) s
)
5092 ((not prev
) (funcall get-smart-quote s
'open
))
5093 ((and (not next
) (zerop pre-blank
))
5094 (funcall get-smart-quote s
'close
))
5096 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
5097 (t (funcall get-smart-quote
'open
)))
5098 ;; 1. Replace quote character at the beginning of S.
5101 ((and prev
(zerop pre-blank
)
5102 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
5103 (setq s
(replace-match
5104 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5107 ((and prev
(zerop pre-blank
)
5108 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
5109 (setq s
(replace-match
5110 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5113 ((and (or (not prev
) (> pre-blank
0))
5114 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
5115 (setq s
(replace-match
5116 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5118 ;; 2. Replace quotes in the middle of the string.
5119 (setq s
(replace-regexp-in-string
5121 (nth 3 org-export-smart-quotes-regexps
)
5123 (funcall get-smart-quote
(match-string 1 text
) 'open
))
5125 (setq s
(replace-regexp-in-string
5127 (nth 4 org-export-smart-quotes-regexps
)
5129 (funcall get-smart-quote
(match-string 1 text
) 'close
))
5131 (setq s
(replace-regexp-in-string
5133 (nth 5 org-export-smart-quotes-regexps
)
5135 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
5137 ;; 3. Replace quote character at the end of S.
5140 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
5141 (setq s
(replace-match
5142 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5146 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
5147 (setq s
(replace-match
5148 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5151 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
5152 (setq s
(replace-match
5153 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5155 ;; Return string with smart quotes.
5160 ;; Here are various functions to retrieve information about the
5161 ;; neighbourhood of a given element or object. Neighbours of interest
5162 ;; are direct parent (`org-export-get-parent'), parent headline
5163 ;; (`org-export-get-parent-headline'), first element containing an
5164 ;; object, (`org-export-get-parent-element'), parent table
5165 ;; (`org-export-get-parent-table'), previous element or object
5166 ;; (`org-export-get-previous-element') and next element or object
5167 ;; (`org-export-get-next-element').
5169 ;; `org-export-get-genealogy' returns the full genealogy of a given
5170 ;; element or object, from closest parent to full parse tree.
5172 (defun org-export-get-genealogy (blob)
5173 "Return full genealogy relative to a given element or object.
5175 BLOB is the element or object being considered.
5177 Ancestors are returned from closest to farthest, the last one
5178 being the full parse tree."
5179 (let (genealogy (parent blob
))
5180 (while (setq parent
(org-element-property :parent parent
))
5181 (push parent genealogy
))
5182 (nreverse genealogy
)))
5184 (defun org-export-get-parent-headline (blob)
5185 "Return BLOB parent headline or nil.
5186 BLOB is the element or object being considered."
5187 (let ((parent blob
))
5188 (while (and (setq parent
(org-element-property :parent parent
))
5189 (not (eq (org-element-type parent
) 'headline
))))
5192 (defun org-export-get-parent-element (object)
5193 "Return first element containing OBJECT or nil.
5194 OBJECT is the object to consider."
5195 (let ((parent object
))
5196 (while (and (setq parent
(org-element-property :parent parent
))
5197 (memq (org-element-type parent
) org-element-all-objects
)))
5200 (defun org-export-get-parent-table (object)
5201 "Return OBJECT parent table or nil.
5202 OBJECT is either a `table-cell' or `table-element' type object."
5203 (let ((parent object
))
5204 (while (and (setq parent
(org-element-property :parent parent
))
5205 (not (eq (org-element-type parent
) 'table
))))
5208 (defun org-export-get-previous-element (blob info
&optional n
)
5209 "Return previous element or object.
5211 BLOB is an element or object. INFO is a plist used as
5212 a communication channel. Return previous exportable element or
5213 object, a string, or nil.
5215 When optional argument N is a positive integer, return a list
5216 containing up to N siblings before BLOB, from farthest to
5217 closest. With any other non-nil value, return a list containing
5220 ;; An object can belong to the contents of its parent or
5221 ;; to a secondary string. We check the latter option
5223 (let ((parent (org-export-get-parent blob
)))
5224 (or (let ((sec-value (org-element-property
5225 (cdr (assq (org-element-type parent
)
5226 org-element-secondary-value-alist
))
5228 (and (memq blob sec-value
) sec-value
))
5229 (org-element-contents parent
))))
5233 (cond ((memq obj
(plist-get info
:ignore-list
)))
5234 ((null n
) (throw 'exit obj
))
5235 ((not (wholenump n
)) (push obj prev
))
5236 ((zerop n
) (throw 'exit prev
))
5237 (t (decf n
) (push obj prev
))))
5238 (cdr (memq blob
(reverse siblings
))))
5241 (defun org-export-get-next-element (blob info
&optional n
)
5242 "Return next element or object.
5244 BLOB is an element or object. INFO is a plist used as
5245 a communication channel. Return next exportable element or
5246 object, a string, or nil.
5248 When optional argument N is a positive integer, return a list
5249 containing up to N siblings after BLOB, from closest to farthest.
5250 With any other non-nil value, return a list containing all of
5253 ;; An object can belong to the contents of its parent or to
5254 ;; a secondary string. We check the latter option first.
5255 (let ((parent (org-export-get-parent blob
)))
5256 (or (let ((sec-value (org-element-property
5257 (cdr (assq (org-element-type parent
)
5258 org-element-secondary-value-alist
))
5260 (cdr (memq blob sec-value
)))
5261 (cdr (memq blob
(org-element-contents parent
))))))
5265 (cond ((memq obj
(plist-get info
:ignore-list
)))
5266 ((null n
) (throw 'exit obj
))
5267 ((not (wholenump n
)) (push obj next
))
5268 ((zerop n
) (throw 'exit
(nreverse next
)))
5269 (t (decf n
) (push obj next
))))
5276 ;; `org-export-translate' translates a string according to the language
5277 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5278 ;; the dictionary used for the translation.
5280 (defconst org-export-dictionary
5282 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5284 ("ca" :default
"Autor")
5285 ("cs" :default
"Autor")
5286 ("da" :default
"Forfatter")
5287 ("de" :default
"Autor")
5288 ("eo" :html
"Aŭtoro")
5289 ("es" :default
"Autor")
5290 ("et" :default
"Autor")
5291 ("fi" :html
"Tekijä")
5292 ("fr" :default
"Auteur")
5293 ("hu" :default
"Szerzõ")
5294 ("is" :html
"Höfundur")
5295 ("it" :default
"Autore")
5296 ("ja" :html
"著者" :utf-8
"著者")
5297 ("nl" :default
"Auteur")
5298 ("no" :default
"Forfatter")
5299 ("nb" :default
"Forfatter")
5300 ("nn" :default
"Forfattar")
5301 ("pl" :default
"Autor")
5302 ("ru" :html
"Автор" :utf-8
"Автор")
5303 ("sv" :html
"Författare")
5304 ("uk" :html
"Автор" :utf-8
"Автор")
5305 ("zh-CN" :html
"作者" :utf-8
"作者")
5306 ("zh-TW" :html
"作者" :utf-8
"作者"))
5307 ("Continued from previous page"
5308 ("de" :default
"Fortsetzung von vorheriger Seite")
5309 ("es" :default
"Continúa de la página anterior")
5310 ("fr" :default
"Suite de la page précédente")
5311 ("it" :default
"Continua da pagina precedente")
5312 ("ja" :utf-8
"前ページから続く")
5313 ("nl" :default
"Vervolg van vorige pagina")
5314 ("pt" :default
"Continuação da página anterior"))
5315 ("Continued on next page"
5316 ("de" :default
"Fortsetzung nächste Seite")
5317 ("es" :default
"Continúa en la siguiente página")
5318 ("fr" :default
"Suite page suivante")
5319 ("it" :default
"Continua alla pagina successiva")
5320 ("ja" :utf-8
"次ページに続く")
5321 ("nl" :default
"Vervolg op volgende pagina")
5322 ("pt" :default
"Continua na página seguinte"))
5324 ("ca" :default
"Data")
5325 ("cs" :default
"Datum")
5326 ("da" :default
"Dato")
5327 ("de" :default
"Datum")
5328 ("eo" :default
"Dato")
5329 ("es" :default
"Fecha")
5330 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5331 ("fi" :html
"Päivämäärä")
5332 ("hu" :html
"Dátum")
5333 ("is" :default
"Dagsetning")
5334 ("it" :default
"Data")
5335 ("ja" :html
"日付" :utf-8
"日付")
5336 ("nl" :default
"Datum")
5337 ("no" :default
"Dato")
5338 ("nb" :default
"Dato")
5339 ("nn" :default
"Dato")
5340 ("pl" :default
"Data")
5341 ("ru" :html
"Дата" :utf-8
"Дата")
5342 ("sv" :default
"Datum")
5343 ("uk" :html
"Дата" :utf-8
"Дата")
5344 ("zh-CN" :html
"日期" :utf-8
"日期")
5345 ("zh-TW" :html
"日期" :utf-8
"日期"))
5347 ("da" :default
"Ligning")
5348 ("de" :default
"Gleichung")
5349 ("es" :html
"Ecuación" :default
"Ecuación")
5350 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5351 ("fr" :ascii
"Equation" :default
"Équation")
5352 ("no" :default
"Ligning")
5353 ("nb" :default
"Ligning")
5354 ("nn" :default
"Likning")
5355 ("sv" :default
"Ekvation")
5356 ("zh-CN" :html
"方程" :utf-8
"方程"))
5358 ("da" :default
"Figur")
5359 ("de" :default
"Abbildung")
5360 ("es" :default
"Figura")
5361 ("et" :default
"Joonis")
5362 ("ja" :html
"図" :utf-8
"図")
5363 ("no" :default
"Illustrasjon")
5364 ("nb" :default
"Illustrasjon")
5365 ("nn" :default
"Illustrasjon")
5366 ("sv" :default
"Illustration")
5367 ("zh-CN" :html
"图" :utf-8
"图"))
5369 ("da" :default
"Figur %d")
5370 ("de" :default
"Abbildung %d:")
5371 ("es" :default
"Figura %d:")
5372 ("et" :default
"Joonis %d:")
5373 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5374 ("ja" :html
"図%d: " :utf-8
"図%d: ")
5375 ("no" :default
"Illustrasjon %d")
5376 ("nb" :default
"Illustrasjon %d")
5377 ("nn" :default
"Illustrasjon %d")
5378 ("sv" :default
"Illustration %d")
5379 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5381 ("ca" :html
"Peus de pàgina")
5382 ("cs" :default
"Pozn\xe1mky pod carou")
5383 ("da" :default
"Fodnoter")
5384 ("de" :html
"Fußnoten" :default
"Fußnoten")
5385 ("eo" :default
"Piednotoj")
5386 ("es" :html
"Nota al pie de página" :default
"Nota al pie de página")
5387 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5388 ("fi" :default
"Alaviitteet")
5389 ("fr" :default
"Notes de bas de page")
5390 ("hu" :html
"Lábjegyzet")
5391 ("is" :html
"Aftanmálsgreinar")
5392 ("it" :html
"Note a piè di pagina")
5393 ("ja" :html
"脚注" :utf-8
"脚注")
5394 ("nl" :default
"Voetnoten")
5395 ("no" :default
"Fotnoter")
5396 ("nb" :default
"Fotnoter")
5397 ("nn" :default
"Fotnotar")
5398 ("pl" :default
"Przypis")
5399 ("ru" :html
"Сноски" :utf-8
"Сноски")
5400 ("sv" :default
"Fotnoter")
5401 ("uk" :html
"Примітки"
5403 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5404 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5406 ("da" :default
"Programmer")
5407 ("de" :default
"Programmauflistungsverzeichnis")
5408 ("es" :default
"Indice de Listados de programas")
5409 ("et" :default
"Loendite nimekiri")
5410 ("fr" :default
"Liste des programmes")
5411 ("no" :default
"Dataprogrammer")
5412 ("nb" :default
"Dataprogrammer")
5413 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5415 ("da" :default
"Tabeller")
5416 ("de" :default
"Tabellenverzeichnis")
5417 ("es" :default
"Indice de tablas")
5418 ("et" :default
"Tabelite nimekiri")
5419 ("fr" :default
"Liste des tableaux")
5420 ("no" :default
"Tabeller")
5421 ("nb" :default
"Tabeller")
5422 ("nn" :default
"Tabeller")
5423 ("sv" :default
"Tabeller")
5424 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5426 ("da" :default
"Program %d")
5427 ("de" :default
"Programmlisting %d")
5428 ("es" :default
"Listado de programa %d")
5429 ("et" :default
"Loend %d")
5430 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5431 ("no" :default
"Dataprogram %d")
5432 ("nb" :default
"Dataprogram %d")
5433 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5435 ("da" :default
"jævnfør afsnit %s")
5436 ("de" :default
"siehe Abschnitt %s")
5437 ("es" :default
"vea seccion %s")
5438 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5439 ("fr" :default
"cf. section %s")
5440 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5442 ("de" :default
"Tabelle")
5443 ("es" :default
"Tabla")
5444 ("et" :default
"Tabel")
5445 ("fr" :default
"Tableau")
5446 ("ja" :html
"表" :utf-8
"表")
5447 ("zh-CN" :html
"表" :utf-8
"表"))
5449 ("da" :default
"Tabel %d")
5450 ("de" :default
"Tabelle %d")
5451 ("es" :default
"Tabla %d")
5452 ("et" :default
"Tabel %d")
5453 ("fr" :default
"Tableau %d :")
5454 ("ja" :html
"表%d:" :utf-8
"表%d:")
5455 ("no" :default
"Tabell %d")
5456 ("nb" :default
"Tabell %d")
5457 ("nn" :default
"Tabell %d")
5458 ("sv" :default
"Tabell %d")
5459 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5460 ("Table of Contents"
5461 ("ca" :html
"Índex")
5462 ("cs" :default
"Obsah")
5463 ("da" :default
"Indhold")
5464 ("de" :default
"Inhaltsverzeichnis")
5465 ("eo" :default
"Enhavo")
5466 ("es" :html
"Índice")
5467 ("et" :default
"Sisukord")
5468 ("fi" :html
"Sisällysluettelo")
5469 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5470 ("hu" :html
"Tartalomjegyzék")
5471 ("is" :default
"Efnisyfirlit")
5472 ("it" :default
"Indice")
5473 ("ja" :html
"目次" :utf-8
"目次")
5474 ("nl" :default
"Inhoudsopgave")
5475 ("no" :default
"Innhold")
5476 ("nb" :default
"Innhold")
5477 ("nn" :default
"Innhald")
5478 ("pl" :html
"Spis treści")
5479 ("ru" :html
"Содержание"
5480 :utf-8
"Содержание")
5481 ("sv" :html
"Innehåll")
5482 ("uk" :html
"Зміст" :utf-8
"Зміст")
5483 ("zh-CN" :html
"目录" :utf-8
"目录")
5484 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5485 ("Unknown reference"
5486 ("da" :default
"ukendt reference")
5487 ("de" :default
"Unbekannter Verweis")
5488 ("es" :default
"referencia desconocida")
5489 ("et" :default
"Tundmatu viide")
5490 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5491 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5492 "Dictionary for export engine.
5494 Alist whose CAR is the string to translate and CDR is an alist
5495 whose CAR is the language string and CDR is a plist whose
5496 properties are possible charsets and values translated terms.
5498 It is used as a database for `org-export-translate'. Since this
5499 function returns the string as-is if no translation was found,
5500 the variable only needs to record values different from the
5503 (defun org-export-translate (s encoding info
)
5504 "Translate string S according to language specification.
5506 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5507 and `:utf-8'. INFO is a plist used as a communication channel.
5509 Translation depends on `:language' property. Return the
5510 translated string. If no translation is found, try to fall back
5511 to `:default' encoding. If it fails, return S."
5512 (let* ((lang (plist-get info
:language
))
5513 (translations (cdr (assoc lang
5514 (cdr (assoc s org-export-dictionary
))))))
5515 (or (plist-get translations encoding
)
5516 (plist-get translations
:default
)
5521 ;;; Asynchronous Export
5523 ;; `org-export-async-start' is the entry point for asynchronous
5524 ;; export. It recreates current buffer (including visibility,
5525 ;; narrowing and visited file) in an external Emacs process, and
5526 ;; evaluates a command there. It then applies a function on the
5527 ;; returned results in the current process.
5529 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5530 ;; allow to export to a buffer or a file, asynchronously or not.
5532 ;; `org-export-output-file-name' is an auxiliary function meant to be
5533 ;; used with `org-export-to-file'. With a given extension, it tries
5534 ;; to provide a canonical file name to write export output to.
5536 ;; Asynchronously generated results are never displayed directly.
5537 ;; Instead, they are stored in `org-export-stack-contents'. They can
5538 ;; then be retrieved by calling `org-export-stack'.
5540 ;; Export Stack is viewed through a dedicated major mode
5541 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5542 ;;`org-export-stack-delete', `org-export-stack-view' and
5543 ;;`org-export-stack-clear'.
5545 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5546 ;; It should be used whenever `org-export-async-start' is called.
5548 (defmacro org-export-async-start
(fun &rest body
)
5549 "Call function FUN on the results returned by BODY evaluation.
5551 BODY evaluation happens in an asynchronous process, from a buffer
5552 which is an exact copy of the current one.
5554 Use `org-export-add-to-stack' in FUN in order to register results
5557 This is a low level function. See also `org-export-to-buffer'
5558 and `org-export-to-file' for more specialized functions."
5559 (declare (indent 1) (debug t
))
5560 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5561 ;; Write the full sexp evaluating BODY in a copy of the current
5562 ;; buffer to a temporary file, as it may be too long for program
5563 ;; args in `start-process'.
5564 `(with-temp-message "Initializing asynchronous export process"
5565 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5566 (,temp-file
(make-temp-file "org-export-process"))
5567 (,coding buffer-file-coding-system
))
5568 (with-temp-file ,temp-file
5570 ;; Null characters (from variable values) are inserted
5571 ;; within the file. As a consequence, coding system for
5572 ;; buffer contents will not be recognized properly. So,
5573 ;; we make sure it is the same as the one used to display
5574 ;; the original buffer.
5575 (format ";; -*- coding: %s; -*-\n%S"
5578 (when org-export-async-debug
'(setq debug-on-error t
))
5579 ;; Ignore `kill-emacs-hook' and code evaluation
5580 ;; queries from Babel as we need a truly
5581 ;; non-interactive process.
5582 (setq kill-emacs-hook nil
5583 org-babel-confirm-evaluate-answer-no t
)
5584 ;; Initialize export framework.
5586 ;; Re-create current buffer there.
5587 (funcall ,,copy-fun
)
5588 (restore-buffer-modified-p nil
)
5589 ;; Sexp to evaluate in the buffer.
5590 (print (progn ,,@body
))))))
5591 ;; Start external process.
5592 (let* ((process-connection-type nil
)
5593 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5598 (list "org-export-process"
5600 (expand-file-name invocation-name invocation-directory
)
5602 (if org-export-async-init-file
5603 (list "-Q" "-l" org-export-async-init-file
)
5604 (list "-l" user-init-file
))
5605 (list "-l" ,temp-file
)))))
5606 ;; Register running process in stack.
5607 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5608 ;; Set-up sentinel in order to catch results.
5609 (let ((handler ,fun
))
5610 (set-process-sentinel
5613 (let ((proc-buffer (process-buffer p
)))
5614 (when (eq (process-status p
) 'exit
)
5616 (if (zerop (process-exit-status p
))
5619 (with-current-buffer proc-buffer
5620 (goto-char (point-max))
5622 (read (current-buffer)))))
5623 (funcall ,handler results
))
5624 (unless org-export-async-debug
5625 (and (get-buffer proc-buffer
)
5626 (kill-buffer proc-buffer
))))
5627 (org-export-add-to-stack proc-buffer nil p
)
5629 (message "Process '%s' exited abnormally" p
))
5630 (unless org-export-async-debug
5631 (delete-file ,,temp-file
)))))))))))))
5634 (defun org-export-to-buffer
5636 &optional async subtreep visible-only body-only ext-plist
5638 "Call `org-export-as' with output to a specified buffer.
5640 BACKEND is either an export back-end, as returned by, e.g.,
5641 `org-export-create-backend', or a symbol referring to
5642 a registered back-end.
5644 BUFFER is the name of the output buffer. If it already exists,
5645 it will be erased first, otherwise, it will be created.
5647 A non-nil optional argument ASYNC means the process should happen
5648 asynchronously. The resulting buffer should then be accessible
5649 through the `org-export-stack' interface. When ASYNC is nil, the
5650 buffer is displayed if `org-export-show-temporary-export-buffer'
5653 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5654 EXT-PLIST are similar to those used in `org-export-as', which
5657 Optional argument POST-PROCESS is a function which should accept
5658 no argument. It is always called within the current process,
5659 from BUFFER, with point at its beginning. Export back-ends can
5660 use it to set a major mode there, e.g,
5662 \(defun org-latex-export-as-latex
5663 \(&optional async subtreep visible-only body-only ext-plist)
5665 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5666 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5668 This function returns BUFFER."
5669 (declare (indent 2))
5671 (org-export-async-start
5673 (with-current-buffer (get-buffer-create ,buffer
)
5675 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5677 (goto-char (point-min))
5678 (org-export-add-to-stack (current-buffer) ',backend
)
5679 (ignore-errors (funcall ,post-process
))))
5681 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5683 (org-export-as backend subtreep visible-only body-only ext-plist
))
5684 (buffer (get-buffer-create buffer
))
5685 (encoding buffer-file-coding-system
))
5686 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5687 (org-kill-new output
))
5688 (with-current-buffer buffer
5690 (setq buffer-file-coding-system encoding
)
5692 (goto-char (point-min))
5693 (and (functionp post-process
) (funcall post-process
)))
5694 (when org-export-show-temporary-export-buffer
5695 (switch-to-buffer-other-window buffer
))
5699 (defun org-export-to-file
5700 (backend file
&optional async subtreep visible-only body-only ext-plist
5702 "Call `org-export-as' with output to a specified file.
5704 BACKEND is either an export back-end, as returned by, e.g.,
5705 `org-export-create-backend', or a symbol referring to
5706 a registered back-end. FILE is the name of the output file, as
5709 A non-nil optional argument ASYNC means the process should happen
5710 asynchronously. The resulting buffer file then be accessible
5711 through the `org-export-stack' interface.
5713 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5714 EXT-PLIST are similar to those used in `org-export-as', which
5717 Optional argument POST-PROCESS is called with FILE as its
5718 argument and happens asynchronously when ASYNC is non-nil. It
5719 has to return a file name, or nil. Export back-ends can use this
5720 to send the output file through additional processing, e.g,
5722 \(defun org-latex-export-to-latex
5723 \(&optional async subtreep visible-only body-only ext-plist)
5725 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5726 \(org-export-to-file 'latex outfile
5727 async subtreep visible-only body-only ext-plist
5728 \(lambda (file) (org-latex-compile file)))
5730 The function returns either a file name returned by POST-PROCESS,
5732 (declare (indent 2))
5733 (if (not (file-writable-p file
)) (error "Output file not writable")
5734 (let ((encoding (or org-export-coding-system buffer-file-coding-system
)))
5736 (org-export-async-start
5738 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5741 ',backend
,subtreep
,visible-only
,body-only
5745 (let ((coding-system-for-write ',encoding
))
5746 (write-file ,file
)))
5747 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5748 (let ((output (org-export-as
5749 backend subtreep visible-only body-only ext-plist
)))
5752 (let ((coding-system-for-write encoding
))
5754 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5755 (org-kill-new output
))
5756 ;; Get proper return value.
5757 (or (and (functionp post-process
) (funcall post-process file
))
5760 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5761 "Return output file's name according to buffer specifications.
5763 EXTENSION is a string representing the output file extension,
5764 with the leading dot.
5766 With a non-nil optional argument SUBTREEP, try to determine
5767 output file's name by looking for \"EXPORT_FILE_NAME\" property
5768 of subtree at point.
5770 When optional argument PUB-DIR is set, use it as the publishing
5773 When optional argument VISIBLE-ONLY is non-nil, don't export
5774 contents of hidden elements.
5776 Return file name as a string."
5777 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5779 ;; File name may come from EXPORT_FILE_NAME subtree
5780 ;; property, assuming point is at beginning of said
5782 (file-name-sans-extension
5786 (ignore-errors (org-back-to-heading) (point)))
5787 "EXPORT_FILE_NAME" t
))
5788 ;; File name may be extracted from buffer's associated
5790 (and visited-file
(file-name-nondirectory visited-file
))
5791 ;; Can't determine file name on our own: Ask user.
5792 (let ((read-file-name-function
5793 (and org-completion-use-ido
'ido-read-file-name
)))
5795 "Output file: " pub-dir nil nil nil
5797 (string= (file-name-extension name t
) extension
)))))))
5799 ;; Build file name. Enforce EXTENSION over whatever user
5800 ;; may have come up with. PUB-DIR, if defined, always has
5801 ;; precedence over any provided path.
5804 (concat (file-name-as-directory pub-dir
)
5805 (file-name-nondirectory base-name
)
5807 ((file-name-absolute-p base-name
) (concat base-name extension
))
5808 (t (concat (file-name-as-directory ".") base-name extension
)))))
5809 ;; If writing to OUTPUT-FILE would overwrite original file, append
5810 ;; EXTENSION another time to final name.
5811 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5812 (concat output-file extension
)
5815 (defun org-export-add-to-stack (source backend
&optional process
)
5816 "Add a new result to export stack if not present already.
5818 SOURCE is a buffer or a file name containing export results.
5819 BACKEND is a symbol representing export back-end used to generate
5822 Entries already pointing to SOURCE and unavailable entries are
5823 removed beforehand. Return the new stack."
5824 (setq org-export-stack-contents
5825 (cons (list source backend
(or process
(current-time)))
5826 (org-export-stack-remove source
))))
5828 (defun org-export-stack ()
5829 "Menu for asynchronous export results and running processes."
5831 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5833 (when (zerop (buffer-size)) (org-export-stack-mode))
5834 (org-export-stack-refresh)
5835 (pop-to-buffer buffer
))
5836 (message "Type \"q\" to quit, \"?\" for help"))
5838 (defun org-export--stack-source-at-point ()
5839 "Return source from export results at point in stack."
5840 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5841 (if (not source
) (error "Source unavailable, please refresh buffer")
5842 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5845 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5847 ;; SOURCE is not consistent with current line. The stack
5848 ;; view is outdated.
5849 (error "Source unavailable; type `g' to update buffer"))))))
5851 (defun org-export-stack-clear ()
5852 "Remove all entries from export stack."
5854 (setq org-export-stack-contents nil
))
5856 (defun org-export-stack-refresh (&rest dummy
)
5857 "Refresh the asynchronous export stack.
5858 DUMMY is ignored. Unavailable sources are removed from the list.
5859 Return the new stack."
5860 (let ((inhibit-read-only t
))
5867 (let ((proc-p (processp (nth 2 entry
))))
5870 (format " %-12s " (or (nth 1 entry
) ""))
5872 (let ((data (nth 2 entry
)))
5873 (if proc-p
(format " %6s " (process-status data
))
5874 ;; Compute age of the results.
5877 (float-time (time-since data
)))))
5880 (let ((source (car entry
)))
5881 (if (stringp source
) source
5882 (buffer-name source
)))))))
5883 ;; Clear stack from exited processes, dead buffers or
5884 ;; non-existent files.
5885 (setq org-export-stack-contents
5888 (if (processp (nth 2 el
))
5889 (buffer-live-p (process-buffer (nth 2 el
)))
5890 (let ((source (car el
)))
5891 (if (bufferp source
) (buffer-live-p source
)
5892 (file-exists-p source
)))))
5893 org-export-stack-contents
)) "\n")))))))
5895 (defun org-export-stack-remove (&optional source
)
5896 "Remove export results at point from stack.
5897 If optional argument SOURCE is non-nil, remove it instead."
5899 (let ((source (or source
(org-export--stack-source-at-point))))
5900 (setq org-export-stack-contents
5901 (org-remove-if (lambda (el) (equal (car el
) source
))
5902 org-export-stack-contents
))))
5904 (defun org-export-stack-view (&optional in-emacs
)
5905 "View export results at point in stack.
5906 With an optional prefix argument IN-EMACS, force viewing files
5909 (let ((source (org-export--stack-source-at-point)))
5910 (cond ((processp source
)
5911 (org-switch-to-buffer-other-window (process-buffer source
)))
5912 ((bufferp source
) (org-switch-to-buffer-other-window source
))
5913 (t (org-open-file source in-emacs
)))))
5915 (defvar org-export-stack-mode-map
5916 (let ((km (make-sparse-keymap)))
5917 (define-key km
" " 'next-line
)
5918 (define-key km
"n" 'next-line
)
5919 (define-key km
"\C-n" 'next-line
)
5920 (define-key km
[down] 'next-line)
5921 (define-key km "p" 'previous-line)
5922 (define-key km "\C-p" 'previous-line)
5923 (define-key km "\C-?" 'previous-line)
5924 (define-key km [up] 'previous-line)
5925 (define-key km "C" 'org-export-stack-clear)
5926 (define-key km "v" 'org-export-stack-view)
5927 (define-key km (kbd "RET") 'org-export-stack-view)
5928 (define-key km "d" 'org-export-stack-remove)
5930 "Keymap for Org Export Stack.")
5932 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5933 "Mode for displaying asynchronous export stack.
5935 Type \\[org-export-stack] to visualize the asynchronous export
5938 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5939 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5942 Removing entries in an Org Export Stack buffer doesn't affect
5943 files or buffers, only the display.
5945 \\{org-export-stack-mode-map}"
5948 (setq buffer-read-only t
5953 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5954 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5955 (set (make-local-variable 'revert-buffer-function)
5956 'org-export-stack-refresh))
5962 ;; `org-export-dispatch' is the standard interactive way to start an
5963 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5964 ;; for its interface, which, in turn, delegates response to key
5965 ;; pressed to `org-export--dispatch-action'.
5968 (defun org-export-dispatch (&optional arg)
5969 "Export dispatcher for Org mode.
5971 It provides an access to common export related tasks in a buffer.
5972 Its interface comes in two flavors: standard and expert.
5974 While both share the same set of bindings, only the former
5975 displays the valid keys associations in a dedicated buffer.
5976 Scrolling (resp. line-wise motion) in this buffer is done with
5977 SPC and DEL (resp. C-n and C-p) keys.
5979 Set variable `org-export-dispatch-use-expert-ui' to switch to one
5980 flavor or the other.
5982 When ARG is \\[universal-argument], repeat the last export action, with the same set
5983 of options used back then, on the current buffer.
5985 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
5988 (cond ((equal arg '(16)) '(stack))
5989 ((and arg org-export-dispatch-last-action))
5990 (t (save-window-excursion
5993 ;; Remember where we are
5994 (move-marker org-export-dispatch-last-position
5996 (org-base-buffer (current-buffer)))
5997 ;; Get and store an export command
5998 (setq org-export-dispatch-last-action
5999 (org-export--dispatch-ui
6000 (list org-export-initial-scope
6001 (and org-export-in-background 'async))
6003 org-export-dispatch-use-expert-ui)))
6004 (and (get-buffer "*Org Export Dispatcher*")
6005 (kill-buffer "*Org Export Dispatcher*")))))))
6006 (action (car input))
6007 (optns (cdr input)))
6008 (unless (memq 'subtree optns)
6009 (move-marker org-export-dispatch-last-position nil))
6011 ;; First handle special hard-coded actions.
6012 (template (org-export-insert-default-template nil optns))
6013 (stack (org-export-stack))
6014 (publish-current-file
6015 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6016 (publish-current-project
6017 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6018 (publish-choose-project
6019 (org-publish (assoc (org-icompleting-read
6021 org-publish-project-alist nil t)
6022 org-publish-project-alist)
6024 (memq 'async optns)))
6025 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6029 ;; Repeating command, maybe move cursor to restore subtree
6031 (if (eq (marker-buffer org-export-dispatch-last-position)
6032 (org-base-buffer (current-buffer)))
6033 (goto-char org-export-dispatch-last-position)
6034 ;; We are in a different buffer, forget position.
6035 (move-marker org-export-dispatch-last-position nil)))
6037 ;; Return a symbol instead of a list to ease
6038 ;; asynchronous export macro use.
6039 (and (memq 'async optns) t)
6040 (and (memq 'subtree optns) t)
6041 (and (memq 'visible optns) t)
6042 (and (memq 'body optns) t)))))))
6044 (defun org-export--dispatch-ui (options first-key expertp)
6045 "Handle interface for `org-export-dispatch'.
6047 OPTIONS is a list containing current interactive options set for
6048 export. It can contain any of the following symbols:
6049 `body' toggles a body-only export
6050 `subtree' restricts export to current subtree
6051 `visible' restricts export to visible part of buffer.
6052 `force' force publishing files.
6053 `async' use asynchronous export process
6055 FIRST-KEY is the key pressed to select the first level menu. It
6056 is nil when this menu hasn't been selected yet.
6058 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6059 buffer is provided, but indications about currently active
6060 options are given in the prompt. Moreover, \[?] allows to switch
6061 back to standard interface."
6063 (lambda (key &optional access-key)
6064 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6065 ;; non-nil is the required first-level key to activate
6066 ;; KEY. When its value is t, activate KEY independently
6067 ;; on the first key, if any. A nil value means KEY will
6068 ;; only be activated at first level.
6069 (if (or (eq access-key t) (eq access-key first-key))
6070 (org-propertize key 'face 'org-warning)
6074 ;; Fontify VALUE string.
6075 (org-propertize value 'face 'font-lock-variable-name-face)))
6076 ;; Prepare menu entries by extracting them from registered
6077 ;; back-ends and sorting them by access key and by ordinal,
6080 (sort (sort (delq nil
6081 (mapcar 'org-export-backend-menu
6082 org-export--registered-backends))
6084 (let ((key-a (nth 1 a))
6086 (cond ((and (numberp key-a) (numberp key-b))
6088 ((numberp key-b) t)))))
6089 'car-less-than-car))
6090 ;; Compute a list of allowed keys based on the first key
6091 ;; pressed, if any. Some keys
6092 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6095 (nconc (list 2 22 19 6 1)
6096 (if (not first-key) (org-uniquify (mapcar 'car entries))
6098 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6099 (when (eq (car entry) first-key)
6100 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6101 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6102 ((not first-key) (list ?P)))
6104 (when expertp (list ??))
6106 ;; Build the help menu for standard UI.
6110 ;; Options are hard-coded.
6111 (format "[%s] Body only: %s [%s] Visible only: %s
6112 \[%s] Export scope: %s [%s] Force publishing: %s
6113 \[%s] Async export: %s\n\n"
6114 (funcall fontify-key "C-b" t)
6115 (funcall fontify-value
6116 (if (memq 'body options) "On " "Off"))
6117 (funcall fontify-key "C-v" t)
6118 (funcall fontify-value
6119 (if (memq 'visible options) "On " "Off"))
6120 (funcall fontify-key "C-s" t)
6121 (funcall fontify-value
6122 (if (memq 'subtree options) "Subtree" "Buffer "))
6123 (funcall fontify-key "C-f" t)
6124 (funcall fontify-value
6125 (if (memq 'force options) "On " "Off"))
6126 (funcall fontify-key "C-a" t)
6127 (funcall fontify-value
6128 (if (memq 'async options) "On " "Off")))
6129 ;; Display registered back-end entries. When a key
6130 ;; appears for the second time, do not create another
6131 ;; entry, but append its sub-menu to existing menu.
6135 (let ((top-key (car entry)))
6137 (unless (eq top-key last-key)
6138 (setq last-key top-key)
6139 (format "\n[%s] %s\n"
6140 (funcall fontify-key (char-to-string top-key))
6142 (let ((sub-menu (nth 2 entry)))
6143 (unless (functionp sub-menu)
6144 ;; Split sub-menu into two columns.
6151 (if (zerop (mod index 2)) " [%s] %-26s"
6153 (funcall fontify-key
6154 (char-to-string (car sub-entry))
6158 (when (zerop (mod index 2)) "\n"))))))))
6160 ;; Publishing menu is hard-coded.
6161 (format "\n[%s] Publish
6162 [%s] Current file [%s] Current project
6163 [%s] Choose project [%s] All projects\n\n\n"
6164 (funcall fontify-key "P")
6165 (funcall fontify-key "f" ?P)
6166 (funcall fontify-key "p" ?P)
6167 (funcall fontify-key "x" ?P)
6168 (funcall fontify-key "a" ?P))
6169 (format "[%s] Export stack [%s] Insert template\n"
6170 (funcall fontify-key "&" t)
6171 (funcall fontify-key "#" t))
6173 (funcall fontify-key "q" t)
6174 (if first-key "Main menu" "Exit")))))
6175 ;; Build prompts for both standard and expert UI.
6176 (standard-prompt (unless expertp "Export command: "))
6180 "Export command (C-%s%s%s%s%s) [%s]: "
6181 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6182 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6183 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6184 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6185 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6186 (mapconcat (lambda (k)
6187 ;; Strip control characters.
6188 (unless (< k 27) (char-to-string k)))
6189 allowed-keys "")))))
6190 ;; With expert UI, just read key with a fancy prompt. In standard
6191 ;; UI, display an intrusive help buffer.
6193 (org-export--dispatch-action
6194 expert-prompt allowed-keys entries options first-key expertp)
6195 ;; At first call, create frame layout in order to display menu.
6196 (unless (get-buffer "*Org Export Dispatcher*")
6197 (delete-other-windows)
6198 (org-switch-to-buffer-other-window
6199 (get-buffer-create "*Org Export Dispatcher*"))
6200 (setq cursor-type nil
6201 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6202 ;; Make sure that invisible cursor will not highlight square
6204 (set-syntax-table (copy-syntax-table))
6205 (modify-syntax-entry ?\[ "w"))
6206 ;; At this point, the buffer containing the menu exists and is
6207 ;; visible in the current window. So, refresh it.
6208 (with-current-buffer "*Org Export Dispatcher*"
6209 ;; Refresh help. Maintain display continuity by re-visiting
6210 ;; previous window position.
6211 (let ((pos (window-start)))
6214 (set-window-start nil pos)))
6215 (org-fit-window-to-buffer)
6216 (org-export--dispatch-action
6217 standard-prompt allowed-keys entries options first-key expertp))))
6219 (defun org-export--dispatch-action
6220 (prompt allowed-keys entries options first-key expertp)
6221 "Read a character from command input and act accordingly.
6223 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6224 a list of characters available at a given step in the process.
6225 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6226 EXPERTP are the same as defined in `org-export--dispatch-ui',
6229 Toggle export options when required. Otherwise, return value is
6230 a list with action as CAR and a list of interactive export
6233 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6235 (while (and (setq key (read-char-exclusive prompt))
6237 (memq key '(14 16 ?\s ?\d)))
6239 (14 (if (not (pos-visible-in-window-p (point-max)))
6240 (ignore-errors (scroll-up 1))
6241 (message "End of buffer")
6243 (16 (if (not (pos-visible-in-window-p (point-min)))
6244 (ignore-errors (scroll-down 1))
6245 (message "Beginning of buffer")
6247 (?\s (if (not (pos-visible-in-window-p (point-max)))
6249 (message "End of buffer")
6251 (?\d (if (not (pos-visible-in-window-p (point-min)))
6253 (message "Beginning of buffer")
6256 ;; Ignore undefined associations.
6257 ((not (memq key allowed-keys))
6259 (unless expertp (message "Invalid key") (sit-for 1))
6260 (org-export--dispatch-ui options first-key expertp))
6261 ;; q key at first level aborts export. At second level, cancel
6262 ;; first key instead.
6263 ((eq key ?q) (if (not first-key) (error "Export aborted")
6264 (org-export--dispatch-ui options nil expertp)))
6265 ;; Help key: Switch back to standard interface if expert UI was
6267 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6268 ;; Send request for template insertion along with export scope.
6269 ((eq key ?#) (cons 'template (memq 'subtree options)))
6270 ;; Switch to asynchronous export stack.
6271 ((eq key ?&) '(stack))
6272 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6273 ((memq key '(2 22 19 6 1))
6274 (org-export--dispatch-ui
6275 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6276 (6 'force) (1 'async))))
6277 (if (memq option options) (remq option options)
6278 (cons option options)))
6280 ;; Action selected: Send key and options back to
6281 ;; `org-export-dispatch'.
6282 ((or first-key (functionp (nth 2 (assq key entries))))
6284 ((not first-key) (nth 2 (assq key entries)))
6285 ;; Publishing actions are hard-coded. Send a special
6286 ;; signal to `org-export-dispatch'.
6289 (?f 'publish-current-file)
6290 (?p 'publish-current-project)
6291 (?x 'publish-choose-project)
6293 ;; Return first action associated to FIRST-KEY + KEY
6294 ;; path. Indeed, derived backends can share the same
6297 (mapc (lambda (entry)
6298 (let ((match (assq key (nth 2 entry))))
6299 (when match (throw 'found (nth 2 match)))))
6300 (member (assq first-key entries) entries)))))
6302 ;; Otherwise, enter sub-menu.
6303 (t (org-export--dispatch-ui options key expertp)))))
6310 ;; generated-autoload-file: "org-loaddefs.el"