1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which sets one mandatory variable: his translation table. Its name
52 ;; is always `org-BACKEND-translate-alist' where BACKEND stands for
53 ;; the name chosen for the back-end. Its value is an alist whose keys
54 ;; are elements and objects types and values translator functions.
55 ;; See function's docstring for more information about translators.
57 ;; Optionally, `org-export-define-backend' can also support specific
58 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
59 ;; function documentation for more information.
61 ;; If the new back-end shares most properties with another one,
62 ;; `org-export-define-derived-backend' can be used to simplify the
65 ;; Any back-end can define its own variables. Among them, those
66 ;; customizable should belong to the `org-export-BACKEND' group.
68 ;; Tools for common tasks across back-ends are implemented in the
69 ;; penultimate part of this file. A dispatcher for standard back-ends
70 ;; is provided in the last one.
74 (eval-when-compile (require 'cl
))
75 (require 'org-element
)
77 (declare-function org-e-publish
"org-e-publish" (project &optional force
))
78 (declare-function org-e-publish-all
"org-e-publish" (&optional force
))
79 (declare-function org-e-publish-current-file
"org-e-publish" (&optional force
))
80 (declare-function org-e-publish-current-project
"org-e-publish"
82 (declare-function org-export-blocks-preprocess
"org-exp-blocks")
84 (defvar org-e-publish-project-alist
)
85 (defvar org-table-number-fraction
)
86 (defvar org-table-number-regexp
)
90 ;;; Internal Variables
92 ;; Among internal variables, the most important is
93 ;; `org-export-options-alist'. This variable define the global export
94 ;; options, shared between every exporter, and how they are acquired.
96 (defconst org-export-max-depth
19
97 "Maximum nesting depth for headlines, counting from 0.")
99 (defconst org-export-options-alist
100 '((:author
"AUTHOR" nil user-full-name t
)
101 (:creator
"CREATOR" nil org-export-creator-string
)
102 (:date
"DATE" nil nil t
)
103 (:description
"DESCRIPTION" nil nil newline
)
104 (:email
"EMAIL" nil user-mail-address t
)
105 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
106 (:headline-levels nil
"H" org-export-headline-levels
)
107 (:keywords
"KEYWORDS" nil nil space
)
108 (:language
"LANGUAGE" nil org-export-default-language t
)
109 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
110 (:section-numbers nil
"num" org-export-with-section-numbers
)
111 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
112 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
113 (:title
"TITLE" nil nil space
)
114 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
115 (:with-author nil
"author" org-export-with-author
)
116 (:with-clocks nil
"c" org-export-with-clocks
)
117 (:with-creator nil
"creator" org-export-with-creator
)
118 (:with-drawers nil
"d" org-export-with-drawers
)
119 (:with-email nil
"email" org-export-with-email
)
120 (:with-emphasize nil
"*" org-export-with-emphasize
)
121 (:with-entities nil
"e" org-export-with-entities
)
122 (:with-fixed-width nil
":" org-export-with-fixed-width
)
123 (:with-footnotes nil
"f" org-export-with-footnotes
)
124 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
125 (:with-plannings nil
"p" org-export-with-planning
)
126 (:with-priority nil
"pri" org-export-with-priority
)
127 (:with-special-strings nil
"-" org-export-with-special-strings
)
128 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
129 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
130 (:with-toc nil
"toc" org-export-with-toc
)
131 (:with-tables nil
"|" org-export-with-tables
)
132 (:with-tags nil
"tags" org-export-with-tags
)
133 (:with-tasks nil
"tasks" org-export-with-tasks
)
134 (:with-timestamps nil
"<" org-export-with-timestamps
)
135 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
136 "Alist between export properties and ways to set them.
138 The CAR of the alist is the property name, and the CDR is a list
139 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
141 KEYWORD is a string representing a buffer keyword, or nil. Each
142 property defined this way can also be set, during subtree
143 export, through an headline property named after the keyword
144 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
146 OPTION is a string that could be found in an #+OPTIONS: line.
147 DEFAULT is the default value for the property.
148 BEHAVIOUR determine how Org should handle multiple keywords for
149 the same property. It is a symbol among:
150 nil Keep old value and discard the new one.
151 t Replace old value with the new one.
152 `space' Concatenate the values, separating them with a space.
153 `newline' Concatenate the values, separating them with
155 `split' Split values at white spaces, and cons them to the
158 KEYWORD and OPTION have precedence over DEFAULT.
160 All these properties should be back-end agnostic. Back-end
161 specific properties are set through `org-export-define-backend'.
162 Properties redefined there have precedence over these.")
164 (defconst org-export-special-keywords
'("SETUP_FILE" "OPTIONS")
165 "List of in-buffer keywords that require special treatment.
166 These keywords are not directly associated to a property. The
167 way they are handled must be hard-coded into
168 `org-export--get-inbuffer-options' function.")
170 (defconst org-export-filters-alist
171 '((:filter-bold . org-export-filter-bold-functions
)
172 (:filter-babel-call . org-export-filter-babel-call-functions
)
173 (:filter-center-block . org-export-filter-center-block-functions
)
174 (:filter-clock . org-export-filter-clock-functions
)
175 (:filter-code . org-export-filter-code-functions
)
176 (:filter-comment . org-export-filter-comment-functions
)
177 (:filter-comment-block . org-export-filter-comment-block-functions
)
178 (:filter-drawer . org-export-filter-drawer-functions
)
179 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
180 (:filter-entity . org-export-filter-entity-functions
)
181 (:filter-example-block . org-export-filter-example-block-functions
)
182 (:filter-export-block . org-export-filter-export-block-functions
)
183 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
184 (:filter-final-output . org-export-filter-final-output-functions
)
185 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
186 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
187 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
188 (:filter-headline . org-export-filter-headline-functions
)
189 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
190 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
191 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
192 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
193 (:filter-italic . org-export-filter-italic-functions
)
194 (:filter-item . org-export-filter-item-functions
)
195 (:filter-keyword . org-export-filter-keyword-functions
)
196 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
197 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
198 (:filter-line-break . org-export-filter-line-break-functions
)
199 (:filter-link . org-export-filter-link-functions
)
200 (:filter-macro . org-export-filter-macro-functions
)
201 (:filter-node-property . org-export-filter-node-property-functions
)
202 (:filter-paragraph . org-export-filter-paragraph-functions
)
203 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
204 (:filter-plain-list . org-export-filter-plain-list-functions
)
205 (:filter-plain-text . org-export-filter-plain-text-functions
)
206 (:filter-planning . org-export-filter-planning-functions
)
207 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
208 (:filter-quote-block . org-export-filter-quote-block-functions
)
209 (:filter-quote-section . org-export-filter-quote-section-functions
)
210 (:filter-radio-target . org-export-filter-radio-target-functions
)
211 (:filter-section . org-export-filter-section-functions
)
212 (:filter-special-block . org-export-filter-special-block-functions
)
213 (:filter-src-block . org-export-filter-src-block-functions
)
214 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
215 (:filter-strike-through . org-export-filter-strike-through-functions
)
216 (:filter-subscript . org-export-filter-subscript-functions
)
217 (:filter-superscript . org-export-filter-superscript-functions
)
218 (:filter-table . org-export-filter-table-functions
)
219 (:filter-table-cell . org-export-filter-table-cell-functions
)
220 (:filter-table-row . org-export-filter-table-row-functions
)
221 (:filter-target . org-export-filter-target-functions
)
222 (:filter-timestamp . org-export-filter-timestamp-functions
)
223 (:filter-underline . org-export-filter-underline-functions
)
224 (:filter-verbatim . org-export-filter-verbatim-functions
)
225 (:filter-verse-block . org-export-filter-verse-block-functions
))
226 "Alist between filters properties and initial values.
228 The key of each association is a property name accessible through
229 the communication channel. Its value is a configurable global
230 variable defining initial filters.
232 This list is meant to install user specified filters. Back-end
233 developers may install their own filters using
234 `org-export-define-backend'. Filters defined there will always
235 be prepended to the current list, so they always get applied
238 (defconst org-export-default-inline-image-rule
242 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
243 "xpm" "pbm" "pgm" "ppm") t
))))
244 "Default rule for link matching an inline image.
245 This rule applies to links with no description. By default, it
246 will be considered as an inline image if it targets a local file
247 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
248 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
249 See `org-export-inline-image-p' for more information about
254 ;;; User-configurable Variables
256 ;; Configuration for the masses.
258 ;; They should never be accessed directly, as their value is to be
259 ;; stored in a property list (cf. `org-export-options-alist').
260 ;; Back-ends will read their value from there instead.
262 (defgroup org-export nil
263 "Options for exporting Org mode files."
267 (defgroup org-export-general nil
268 "General options for export engine."
269 :tag
"Org Export General"
272 (defcustom org-export-with-archived-trees
'headline
273 "Whether sub-trees with the ARCHIVE tag should be exported.
275 This can have three different values:
276 nil Do not export, pretend this tree is not present.
277 t Do export the entire tree.
278 `headline' Only export the headline, but skip the tree below it.
280 This option can also be set with the #+OPTIONS line,
282 :group
'org-export-general
284 (const :tag
"Not at all" nil
)
285 (const :tag
"Headline only" 'headline
)
286 (const :tag
"Entirely" t
)))
288 (defcustom org-export-with-author t
289 "Non-nil means insert author name into the exported file.
290 This option can also be set with the #+OPTIONS line,
291 e.g. \"author:nil\"."
292 :group
'org-export-general
295 (defcustom org-export-with-clocks nil
296 "Non-nil means export CLOCK keywords.
297 This option can also be set with the #+OPTIONS line,
299 :group
'org-export-general
302 (defcustom org-export-with-creator
'comment
303 "Non-nil means the postamble should contain a creator sentence.
305 The sentence can be set in `org-export-creator-string' and
306 defaults to \"Generated by Org mode XX in Emacs XXX.\".
308 If the value is `comment' insert it as a comment."
309 :group
'org-export-general
311 (const :tag
"No creator sentence" nil
)
312 (const :tag
"Sentence as a comment" 'comment
)
313 (const :tag
"Insert the sentence" t
)))
315 (defcustom org-export-creator-string
316 (format "Generated by Org mode %s in Emacs %s."
317 (if (fboundp 'org-version
) (org-version) "(Unknown)")
319 "String to insert at the end of the generated document."
320 :group
'org-export-general
321 :type
'(string :tag
"Creator string"))
323 (defcustom org-export-with-drawers t
324 "Non-nil means export contents of standard drawers.
326 When t, all drawers are exported. This may also be a list of
327 drawer names to export. This variable doesn't apply to
330 This option can also be set with the #+OPTIONS line,
332 :group
'org-export-general
334 (const :tag
"All drawers" t
)
335 (const :tag
"None" nil
)
336 (repeat :tag
"Selected drawers"
337 (string :tag
"Drawer name"))))
339 (defcustom org-export-with-email nil
340 "Non-nil means insert author email into the exported file.
341 This option can also be set with the #+OPTIONS line,
343 :group
'org-export-general
346 (defcustom org-export-with-emphasize t
347 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
349 If the export target supports emphasizing text, the word will be
350 typeset in bold, italic, or underlined, respectively. Not all
351 export backends support this.
353 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
354 :group
'org-export-general
357 (defcustom org-export-exclude-tags
'("noexport")
358 "Tags that exclude a tree from export.
360 All trees carrying any of these tags will be excluded from
361 export. This is without condition, so even subtrees inside that
362 carry one of the `org-export-select-tags' will be removed.
364 This option can also be set with the #+EXCLUDE_TAGS: keyword."
365 :group
'org-export-general
366 :type
'(repeat (string :tag
"Tag")))
368 (defcustom org-export-with-fixed-width t
369 "Non-nil means lines starting with \":\" will be in fixed width font.
371 This can be used to have pre-formatted text, fragments of code
373 : ;; Some Lisp examples
376 will be looking just like this in also HTML. See also the QUOTE
377 keyword. Not all export backends support this.
379 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
380 :group
'org-export-translation
383 (defcustom org-export-with-footnotes t
384 "Non-nil means Org footnotes should be exported.
385 This option can also be set with the #+OPTIONS line,
387 :group
'org-export-general
390 (defcustom org-export-headline-levels
3
391 "The last level which is still exported as a headline.
393 Inferior levels will produce itemize lists when exported.
395 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
396 :group
'org-export-general
399 (defcustom org-export-default-language
"en"
400 "The default language for export and clocktable translations, as a string.
401 This may have an association in
402 `org-clock-clocktable-language-setup'."
403 :group
'org-export-general
404 :type
'(string :tag
"Language"))
406 (defcustom org-export-preserve-breaks nil
407 "Non-nil means preserve all line breaks when exporting.
409 Normally, in HTML output paragraphs will be reformatted.
411 This option can also be set with the #+OPTIONS line,
413 :group
'org-export-general
416 (defcustom org-export-with-entities t
417 "Non-nil means interpret entities when exporting.
419 For example, HTML export converts \\alpha to α and \\AA to
422 For a list of supported names, see the constant `org-entities'
423 and the user option `org-entities-user'.
425 This option can also be set with the #+OPTIONS line,
427 :group
'org-export-general
430 (defcustom org-export-with-inlinetasks t
431 "Non-nil means inlinetasks should be exported.
432 This option can also be set with the #+OPTIONS line,
433 e.g. \"inline:nil\"."
434 :group
'org-export-general
437 (defcustom org-export-with-planning nil
438 "Non-nil means include planning info in export.
439 This option can also be set with the #+OPTIONS: line,
441 :group
'org-export-general
444 (defcustom org-export-with-priority nil
445 "Non-nil means include priority cookies in export.
446 This option can also be set with the #+OPTIONS line,
448 :group
'org-export-general
451 (defcustom org-export-with-section-numbers t
452 "Non-nil means add section numbers to headlines when exporting.
454 When set to an integer n, numbering will only happen for
455 headlines whose relative level is higher or equal to n.
457 This option can also be set with the #+OPTIONS line,
459 :group
'org-export-general
462 (defcustom org-export-select-tags
'("export")
463 "Tags that select a tree for export.
465 If any such tag is found in a buffer, all trees that do not carry
466 one of these tags will be ignored during export. Inside trees
467 that are selected like this, you can still deselect a subtree by
468 tagging it with one of the `org-export-exclude-tags'.
470 This option can also be set with the #+SELECT_TAGS: keyword."
471 :group
'org-export-general
472 :type
'(repeat (string :tag
"Tag")))
474 (defcustom org-export-with-special-strings t
475 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
477 When this option is turned on, these strings will be exported as:
480 -----+----------+--------+-------
484 ... … \\ldots …
486 This option can also be set with the #+OPTIONS line,
488 :group
'org-export-general
491 (defcustom org-export-with-statistics-cookies t
492 "Non-nil means include statistics cookies in export.
493 This option can also be set with the #+OPTIONS: line,
495 :group
'org-export-general
498 (defcustom org-export-with-sub-superscripts t
499 "Non-nil means interpret \"_\" and \"^\" for export.
501 When this option is turned on, you can use TeX-like syntax for
502 sub- and superscripts. Several characters after \"_\" or \"^\"
503 will be considered as a single item - so grouping with {} is
504 normally not needed. For example, the following things will be
505 parsed as single sub- or superscripts.
507 10^24 or 10^tau several digits will be considered 1 item.
508 10^-12 or 10^-tau a leading sign with digits or a word
509 x^2-y^3 will be read as x^2 - y^3, because items are
510 terminated by almost any nonword/nondigit char.
511 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
513 Still, ambiguity is possible - so when in doubt use {} to enclose
514 the sub/superscript. If you set this variable to the symbol
515 `{}', the braces are *required* in order to trigger
516 interpretations as sub/superscript. This can be helpful in
517 documents that need \"_\" frequently in plain text.
519 This option can also be set with the #+OPTIONS line,
521 :group
'org-export-general
523 (const :tag
"Interpret them" t
)
524 (const :tag
"Curly brackets only" {})
525 (const :tag
"Do not interpret them" nil
)))
527 (defcustom org-export-with-toc t
528 "Non-nil means create a table of contents in exported files.
530 The TOC contains headlines with levels up
531 to`org-export-headline-levels'. When an integer, include levels
532 up to N in the toc, this may then be different from
533 `org-export-headline-levels', but it will not be allowed to be
534 larger than the number of headline levels. When nil, no table of
537 This option can also be set with the #+OPTIONS line,
538 e.g. \"toc:nil\" or \"toc:3\"."
539 :group
'org-export-general
541 (const :tag
"No Table of Contents" nil
)
542 (const :tag
"Full Table of Contents" t
)
543 (integer :tag
"TOC to level")))
545 (defcustom org-export-with-tables t
546 "If non-nil, lines starting with \"|\" define a table.
549 | Name | Address | Birthday |
550 |-------------+----------+-----------|
551 | Arthur Dent | England | 29.2.2100 |
553 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
554 :group
'org-export-general
557 (defcustom org-export-with-tags t
558 "If nil, do not export tags, just remove them from headlines.
560 If this is the symbol `not-in-toc', tags will be removed from
561 table of contents entries, but still be shown in the headlines of
564 This option can also be set with the #+OPTIONS line,
566 :group
'org-export-general
568 (const :tag
"Off" nil
)
569 (const :tag
"Not in TOC" not-in-toc
)
570 (const :tag
"On" t
)))
572 (defcustom org-export-with-tasks t
573 "Non-nil means include TODO items for export.
574 This may have the following values:
575 t include tasks independent of state.
576 todo include only tasks that are not yet done.
577 done include only tasks that are already done.
578 nil remove all tasks before export
579 list of keywords keep only tasks with these keywords"
580 :group
'org-export-general
582 (const :tag
"All tasks" t
)
583 (const :tag
"No tasks" nil
)
584 (const :tag
"Not-done tasks" todo
)
585 (const :tag
"Only done tasks" done
)
586 (repeat :tag
"Specific TODO keywords"
587 (string :tag
"Keyword"))))
589 (defcustom org-export-time-stamp-file t
590 "Non-nil means insert a time stamp into the exported file.
591 The time stamp shows when the file was created.
593 This option can also be set with the #+OPTIONS line,
594 e.g. \"timestamp:nil\"."
595 :group
'org-export-general
598 (defcustom org-export-with-timestamps t
599 "Non nil means allow timestamps in export.
601 It can be set to `active', `inactive', t or nil, in order to
602 export, respectively, only active timestamps, only inactive ones,
605 This option can also be set with the #+OPTIONS line, e.g.
607 :group
'org-export-general
609 (const :tag
"All timestamps" t
)
610 (const :tag
"Only active timestamps" active
)
611 (const :tag
"Only inactive timestamps" inactive
)
612 (const :tag
"No timestamp" nil
)))
614 (defcustom org-export-with-todo-keywords t
615 "Non-nil means include TODO keywords in export.
616 When nil, remove all these keywords from the export."
617 :group
'org-export-general
620 (defcustom org-export-allow-BIND
'confirm
621 "Non-nil means allow #+BIND to define local variable values for export.
622 This is a potential security risk, which is why the user must
623 confirm the use of these lines."
624 :group
'org-export-general
626 (const :tag
"Never" nil
)
627 (const :tag
"Always" t
)
628 (const :tag
"Ask a confirmation for each file" confirm
)))
630 (defcustom org-export-snippet-translation-alist nil
631 "Alist between export snippets back-ends and exporter back-ends.
633 This variable allows to provide shortcuts for export snippets.
635 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
636 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
637 HTML code while every other back-end will ignore it."
638 :group
'org-export-general
641 (string :tag
"Shortcut")
642 (string :tag
"Back-end"))))
644 (defcustom org-export-coding-system nil
645 "Coding system for the exported file."
646 :group
'org-export-general
647 :type
'coding-system
)
649 (defcustom org-export-copy-to-kill-ring t
650 "Non-nil means exported stuff will also be pushed onto the kill ring."
651 :group
'org-export-general
654 (defcustom org-export-initial-scope
'buffer
655 "The initial scope when exporting with `org-export-dispatch'.
656 This variable can be either set to `buffer' or `subtree'."
657 :group
'org-export-general
659 (const :tag
"Export current buffer" 'buffer
)
660 (const :tag
"Export current subtree" 'subtree
)))
662 (defcustom org-export-show-temporary-export-buffer t
663 "Non-nil means show buffer after exporting to temp buffer.
664 When Org exports to a file, the buffer visiting that file is ever
665 shown, but remains buried. However, when exporting to
666 a temporary buffer, that buffer is popped up in a second window.
667 When this variable is nil, the buffer remains buried also in
669 :group
'org-export-general
672 (defcustom org-export-dispatch-use-expert-ui nil
673 "Non-nil means using a non-intrusive `org-export-dispatch'.
674 In that case, no help buffer is displayed. Though, an indicator
675 for current export scope is added to the prompt \(i.e. \"b\" when
676 output is restricted to body only, \"s\" when it is restricted to
677 the current subtree and \"v\" when only visible elements are
678 considered for export\). Also, \[?] allows to switch back to
680 :group
'org-export-general
685 ;;; Defining New Back-ends
687 ;; `org-export-define-backend' is the standard way to define an export
688 ;; back-end. It allows to specify translators, filters, buffer
689 ;; options and a menu entry. If the new back-end shares translators
690 ;; with another back-end, `org-export-define-derived-backend' may be
693 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
694 ;; when a given back-end hasn't been registered yet.
696 (defmacro org-export-define-backend
(backend translators
&rest body
)
697 "Define a new back-end BACKEND.
699 TRANSLATORS is an alist between object or element types and
700 functions handling them.
702 These functions should return a string without any trailing
703 space, or nil. They must accept three arguments: the object or
704 element itself, its contents or nil when it isn't recursive and
705 the property list used as a communication channel.
707 Contents, when not nil, are stripped from any global indentation
708 \(although the relative one is preserved). They also always end
709 with a single newline character.
711 If, for a given type, no function is found, that element or
712 object type will simply be ignored, along with any blank line or
713 white space at its end. The same will happen if the function
714 returns the nil value. If that function returns the empty
715 string, the type will be ignored, but the blank lines or white
718 In addition to element and object types, one function can be
719 associated to the `template' symbol and another one to the
722 The former returns the final transcoded string, and can be used
723 to add a preamble and a postamble to document's body. It must
724 accept two arguments: the transcoded string and the property list
725 containing export options.
727 The latter, when defined, is to be called on every text not
728 recognized as an element or an object. It must accept two
729 arguments: the text string and the information channel. It is an
730 appropriate place to protect special chars relative to the
733 BODY can start with pre-defined keyword arguments. The following
734 keywords are understood:
738 String, or list of strings, representing block names that
739 will not be parsed. This is used to specify blocks that will
740 contain raw code specific to the back-end. These blocks
741 still have to be handled by the relative `export-block' type
746 Alist between filters and function, or list of functions,
747 specific to the back-end. See `org-export-filters-alist' for
748 a list of all allowed filters. Filters defined here
749 shouldn't make a back-end test, as it may prevent back-ends
750 derived from this one to behave properly.
754 Menu entry for the export dispatcher. It should be a list
757 \(KEY DESCRIPTION ACTION-OR-MENU)
761 KEY is a free character selecting the back-end.
762 DESCRIPTION is a string naming the back-end.
763 ACTION-OR-MENU is either a function or an alist.
765 If it is an action, it will be called with three arguments:
766 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
767 for further explanations.
769 If it is an alist, associations should follow the
772 \(KEY DESCRIPTION ACTION)
774 where KEY, DESCRIPTION and ACTION are described above.
776 Valid values include:
778 \(?m \"My Special Back-end\" my-special-export-function)
782 \(?l \"Export to LaTeX\"
783 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
784 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
785 \(?p \"PDF file\" org-e-latex-export-to-pdf)
786 \(?o \"PDF file and open\"
787 \(lambda (subtree visible body-only)
789 \(org-e-latex-export-to-pdf subtree visible body-only))))))
793 Alist between back-end specific properties introduced in
794 communication channel and how their value are acquired. See
795 `org-export-options-alist' for more information about
796 structure of the values."
797 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
799 (let (export-block filters menu-entry options
)
800 (while (keywordp (car body
))
802 (:export-block
(let ((names (pop body
)))
804 (if (consp names
) (mapcar 'upcase names
)
805 (list (upcase names
))))))
806 (:filters-alist
(setq filters
(pop body
)))
807 (:menu-entry
(setq menu-entry
(pop body
)))
808 (:options-alist
(setq options
(pop body
)))
811 ;; Define translators.
812 (defvar ,(intern (format "org-%s-translate-alist" backend
)) ',translators
813 "Alist between element or object types and translators.")
816 `(defconst ,(intern (format "org-%s-options-alist" backend
)) ',options
817 ,(format "Alist between %s export properties and ways to set them.
818 See `org-export-options-alist' for more information on the
819 structure of the values."
823 `(defconst ,(intern (format "org-%s-filters-alist" backend
)) ',filters
824 "Alist between filters keywords and back-end specific filters.
825 See `org-export-filters-alist' for more information."))
826 ;; Tell parser to not parse EXPORT-BLOCK blocks.
830 (add-to-list 'org-element-block-name-alist
831 `(,name . org-element-export-block-parser
)))
833 ;; Add an entry for back-end in `org-export-dispatch'.
835 (let ((menu (assq (car menu-entry
) org-export-dispatch-menu-entries
)))
837 `(push ',menu-entry org-export-dispatch-menu-entries
))))
838 ;; Splice in the body, if any.
841 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
842 "Create a new back-end as a variant of an existing one.
844 CHILD is the name of the derived back-end. PARENT is the name of
847 BODY can start with pre-defined keyword arguments. The following
848 keywords are understood:
852 String, or list of strings, representing block names that
853 will not be parsed. This is used to specify blocks that will
854 contain raw code specific to the back-end. These blocks
855 still have to be handled by the relative `export-block' type
860 Alist of filters that will overwrite or complete filters
861 defined in PARENT back-end. See `org-export-filters-alist'
862 for a list of allowed filters.
866 Menu entry for the export dispatcher. See
867 `org-export-define-backend' for more information about the
872 Alist of back-end specific properties that will overwrite or
873 complete those defined in PARENT back-end. Refer to
874 `org-export-options-alist' for more information about
875 structure of the values.
879 Append entries to an existing menu in the export dispatcher.
880 The associated value should be a list whose CAR is the
881 character selecting the menu to expand and CDR a list of
882 entries following the pattern:
884 \(KEY DESCRIPTION ACTION)
886 where KEY is a free character triggering the action,
887 DESCRIPTION is a string defining the action, and ACTION is
888 a function that will be called with three arguments:
889 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
890 for further explanations.
892 Valid values include:
894 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
895 \(?O \"As PDF file and open (Beamer)\"
897 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
901 Alist of element and object types and transcoders that will
902 overwrite or complete transcode table from PARENT back-end.
903 Refer to `org-export-define-backend' for detailed information
906 As an example, here is how one could define \"my-latex\" back-end
907 as a variant of `e-latex' back-end with a custom template
910 \(org-export-define-derived-backend my-latex e-latex
911 :translate-alist ((template . my-latex-template-fun)))
913 The back-end could then be called with, for example:
915 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
916 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] def-body
))
918 (let (export-block filters menu-entry options sub-menu-entry translate
)
919 (while (keywordp (car body
))
921 (:export-block
(let ((names (pop body
)))
923 (if (consp names
) (mapcar 'upcase names
)
924 (list (upcase names
))))))
925 (:filters-alist
(setq filters
(pop body
)))
926 (:menu-entry
(setq menu-entry
(pop body
)))
927 (:options-alist
(setq options
(pop body
)))
928 (:sub-menu-entry
(setq sub-menu-entry
(pop body
)))
929 (:translate-alist
(setq translate
(pop body
)))
932 ;; Tell parser to not parse EXPORT-BLOCK blocks.
936 (add-to-list 'org-element-block-name-alist
937 `(,name . org-element-export-block-parser
)))
940 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent
))))
941 (when (or (boundp parent-filters
) filters
)
942 `(defconst ,(intern (format "org-%s-filters-alist" child
))
944 (and (boundp parent-filters
)
945 (copy-sequence (symbol-value parent-filters
))))
946 "Alist between filters keywords and back-end specific filters.
947 See `org-export-filters-alist' for more information.")))
949 ,(let ((parent-options (intern (format "org-%s-options-alist" parent
))))
950 (when (or (boundp parent-options
) options
)
951 `(defconst ,(intern (format "org-%s-options-alist" child
))
953 (and (boundp parent-options
)
954 (copy-sequence (symbol-value parent-options
))))
955 ,(format "Alist between %s export properties and ways to set them.
956 See `org-export-options-alist' for more information on the
957 structure of the values."
959 ;; Define translators.
960 (defvar ,(intern (format "org-%s-translate-alist" child
))
964 (intern (format "org-%s-translate-alist" parent
)))))
965 "Alist between element or object types and translators.")
966 ;; Add an entry for back-end in `org-export-dispatch'.
968 (let ((menu (assq (car menu-entry
) org-export-dispatch-menu-entries
)))
970 `(push ',menu-entry org-export-dispatch-menu-entries
))))
971 ,(when sub-menu-entry
972 (let ((menu (nth 2 (assq (car sub-menu-entry
)
973 org-export-dispatch-menu-entries
))))
974 (when menu
`(nconc ',menu
975 ',(org-remove-if (lambda (e) (member e menu
))
976 (cdr sub-menu-entry
))))))
977 ;; Splice in the body, if any.
980 (defun org-export-barf-if-invalid-backend (backend)
981 "Signal an error if BACKEND isn't defined."
982 (unless (boundp (intern (format "org-%s-translate-alist" backend
)))
983 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
987 ;;; The Communication Channel
989 ;; During export process, every function has access to a number of
990 ;; properties. They are of two types:
992 ;; 1. Environment options are collected once at the very beginning of
993 ;; the process, out of the original buffer and configuration.
994 ;; Collecting them is handled by `org-export-get-environment'
997 ;; Most environment options are defined through the
998 ;; `org-export-options-alist' variable.
1000 ;; 2. Tree properties are extracted directly from the parsed tree,
1001 ;; just before export, by `org-export-collect-tree-properties'.
1003 ;; Here is the full list of properties available during transcode
1004 ;; process, with their category and their value type.
1006 ;; + `:author' :: Author's name.
1007 ;; - category :: option
1010 ;; + `:back-end' :: Current back-end used for transcoding.
1011 ;; - category :: tree
1014 ;; + `:creator' :: String to write as creation information.
1015 ;; - category :: option
1018 ;; + `:date' :: String to use as date.
1019 ;; - category :: option
1022 ;; + `:description' :: Description text for the current data.
1023 ;; - category :: option
1026 ;; + `:email' :: Author's email.
1027 ;; - category :: option
1030 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1032 ;; - category :: option
1033 ;; - type :: list of strings
1035 ;; + `:exported-data' :: Hash table used for memoizing
1036 ;; `org-export-data'.
1037 ;; - category :: tree
1038 ;; - type :: hash table
1040 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1041 ;; their definition, as parsed data. Only non-inlined footnotes
1042 ;; are represented in this alist. Also, every definition isn't
1043 ;; guaranteed to be referenced in the parse tree. The purpose of
1044 ;; this property is to preserve definitions from oblivion
1045 ;; (i.e. when the parse tree comes from a part of the original
1046 ;; buffer), it isn't meant for direct use in a back-end. To
1047 ;; retrieve a definition relative to a reference, use
1048 ;; `org-export-get-footnote-definition' instead.
1049 ;; - category :: option
1050 ;; - type :: alist (STRING . LIST)
1052 ;; + `:headline-levels' :: Maximum level being exported as an
1053 ;; headline. Comparison is done with the relative level of
1054 ;; headlines in the parse tree, not necessarily with their
1056 ;; - category :: option
1057 ;; - type :: integer
1059 ;; + `:headline-offset' :: Difference between relative and real level
1060 ;; of headlines in the parse tree. For example, a value of -1
1061 ;; means a level 2 headline should be considered as level
1062 ;; 1 (cf. `org-export-get-relative-level').
1063 ;; - category :: tree
1064 ;; - type :: integer
1066 ;; + `:headline-numbering' :: Alist between headlines and their
1067 ;; numbering, as a list of numbers
1068 ;; (cf. `org-export-get-headline-number').
1069 ;; - category :: tree
1070 ;; - type :: alist (INTEGER . LIST)
1072 ;; + `:id-alist' :: Alist between ID strings and destination file's
1073 ;; path, relative to current directory. It is used by
1074 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1076 ;; - category :: option
1077 ;; - type :: alist (STRING . STRING)
1079 ;; + `:ignore-list' :: List of elements and objects that should be
1080 ;; ignored during export.
1081 ;; - category :: tree
1082 ;; - type :: list of elements and objects
1084 ;; + `:input-file' :: Full path to input file, if any.
1085 ;; - category :: option
1086 ;; - type :: string or nil
1088 ;; + `:keywords' :: List of keywords attached to data.
1089 ;; - category :: option
1092 ;; + `:language' :: Default language used for translations.
1093 ;; - category :: option
1096 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1098 ;; - category :: option
1099 ;; - type :: list (as returned by `org-element-parse-buffer')
1101 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1103 ;; - category :: option
1104 ;; - type :: symbol (nil, t)
1106 ;; + `:section-numbers' :: Non-nil means transcoding should add
1107 ;; section numbers to headlines.
1108 ;; - category :: option
1109 ;; - type :: symbol (nil, t)
1111 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1112 ;; in transcoding. When such a tag is present, subtrees without
1113 ;; it are de facto excluded from the process. See
1114 ;; `use-select-tags'.
1115 ;; - category :: option
1116 ;; - type :: list of strings
1118 ;; + `:target-list' :: List of targets encountered in the parse tree.
1119 ;; This is used to partly resolve "fuzzy" links
1120 ;; (cf. `org-export-resolve-fuzzy-link').
1121 ;; - category :: tree
1122 ;; - type :: list of strings
1124 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1125 ;; a time stamp in the output.
1126 ;; - category :: option
1127 ;; - type :: symbol (nil, t)
1129 ;; + `:translate-alist' :: Alist between element and object types and
1130 ;; transcoding functions relative to the current back-end.
1131 ;; Special keys `template' and `plain-text' are also possible.
1132 ;; - category :: option
1133 ;; - type :: alist (SYMBOL . FUNCTION)
1135 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1136 ;; also be transcoded. If it is set to the `headline' symbol,
1137 ;; only the archived headline's name is retained.
1138 ;; - category :: option
1139 ;; - type :: symbol (nil, t, `headline')
1141 ;; + `:with-author' :: Non-nil means author's name should be included
1143 ;; - category :: option
1144 ;; - type :: symbol (nil, t)
1146 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1147 ;; - category :: option
1148 ;; - type :: symbol (nil, t)
1150 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1151 ;; inserted at the end of the transcoded string. If the value
1152 ;; is `comment', it should be commented.
1153 ;; - category :: option
1154 ;; - type :: symbol (`comment', nil, t)
1156 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1157 ;; its value is a list of names, only drawers with such names
1158 ;; will be transcoded.
1159 ;; - category :: option
1160 ;; - type :: symbol (nil, t) or list of strings
1162 ;; + `:with-email' :: Non-nil means output should contain author's
1164 ;; - category :: option
1165 ;; - type :: symbol (nil, t)
1167 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1169 ;; - category :: option
1170 ;; - type :: symbol (nil, t)
1172 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1173 ;; strings starting with a colon as a fixed-with (verbatim) area.
1174 ;; - category :: option
1175 ;; - type :: symbol (nil, t)
1177 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1179 ;; - category :: option
1180 ;; - type :: symbol (nil, t)
1182 ;; + `:with-plannings' :: Non-nil means transcoding should include
1184 ;; - category :: option
1185 ;; - type :: symbol (nil, t)
1187 ;; + `:with-priority' :: Non-nil means transcoding should include
1188 ;; priority cookies.
1189 ;; - category :: option
1190 ;; - type :: symbol (nil, t)
1192 ;; + `:with-special-strings' :: Non-nil means transcoding should
1193 ;; interpret special strings in plain text.
1194 ;; - category :: option
1195 ;; - type :: symbol (nil, t)
1197 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1198 ;; interpret subscript and superscript. With a value of "{}",
1199 ;; only interpret those using curly brackets.
1200 ;; - category :: option
1201 ;; - type :: symbol (nil, {}, t)
1203 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1205 ;; - category :: option
1206 ;; - type :: symbol (nil, t)
1208 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1209 ;; headlines. A `not-in-toc' value will remove them from the
1210 ;; table of contents, if any, nonetheless.
1211 ;; - category :: option
1212 ;; - type :: symbol (nil, t, `not-in-toc')
1214 ;; + `:with-tasks' :: Non-nil means transcoding should include
1215 ;; headlines with a TODO keyword. A `todo' value will only
1216 ;; include headlines with a todo type keyword while a `done'
1217 ;; value will do the contrary. If a list of strings is provided,
1218 ;; only tasks with keywords belonging to that list will be kept.
1219 ;; - category :: option
1220 ;; - type :: symbol (t, todo, done, nil) or list of strings
1222 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1223 ;; time stamps. Special value `active' (resp. `inactive') ask to
1224 ;; export only active (resp. inactive) timestamps. Otherwise,
1225 ;; completely remove them.
1226 ;; - category :: option
1227 ;; - type :: symbol: (`active', `inactive', t, nil)
1229 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1230 ;; added to the output. An integer value limits its depth.
1231 ;; - category :: option
1232 ;; - type :: symbol (nil, t or integer)
1234 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1235 ;; include TODO keywords.
1236 ;; - category :: option
1237 ;; - type :: symbol (nil, t)
1240 ;;;; Environment Options
1242 ;; Environment options encompass all parameters defined outside the
1243 ;; scope of the parsed data. They come from five sources, in
1244 ;; increasing precedence order:
1246 ;; - Global variables,
1247 ;; - Buffer's attributes,
1248 ;; - Options keyword symbols,
1249 ;; - Buffer keywords,
1250 ;; - Subtree properties.
1252 ;; The central internal function with regards to environment options
1253 ;; is `org-export-get-environment'. It updates global variables with
1254 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1255 ;; the different sources.
1257 ;; The internal functions doing the retrieval are:
1258 ;; `org-export--get-global-options',
1259 ;; `org-export--get-buffer-attributes',
1260 ;; `org-export--parse-option-keyword',
1261 ;; `org-export--get-subtree-options' and
1262 ;; `org-export--get-inbuffer-options'
1264 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1265 ;; take care of the part relative to "#+BIND:" keywords.
1267 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1268 "Collect export options from the current buffer.
1270 Optional argument BACKEND is a symbol specifying which back-end
1271 specific options to read, if any.
1273 When optional argument SUBTREEP is non-nil, assume the export is
1274 done against the current sub-tree.
1276 Third optional argument EXT-PLIST is a property list with
1277 external parameters overriding Org default settings, but still
1278 inferior to file-local settings."
1279 ;; First install #+BIND variables.
1280 (org-export--install-letbind-maybe)
1281 ;; Get and prioritize export options...
1283 ;; ... from global variables...
1284 (org-export--get-global-options backend
)
1285 ;; ... from buffer's attributes...
1286 (org-export--get-buffer-attributes)
1287 ;; ... from an external property list...
1289 ;; ... from in-buffer settings...
1290 (org-export--get-inbuffer-options
1292 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1293 ;; ... and from subtree, when appropriate.
1294 (and subtreep
(org-export--get-subtree-options backend
))
1295 ;; Eventually install back-end symbol and its translation table.
1299 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend
))))
1300 (when (boundp trans-alist
) (symbol-value trans-alist
))))))
1302 (defun org-export--parse-option-keyword (options &optional backend
)
1303 "Parse an OPTIONS line and return values as a plist.
1304 Optional argument BACKEND is a symbol specifying which back-end
1305 specific items to read, if any."
1307 (append org-export-options-alist
1310 (format "org-%s-options-alist" backend
))))
1311 (and (boundp var
) (eval var
))))))
1312 ;; Build an alist between #+OPTION: item and property-name.
1315 (when (nth 2 e
) (cons (regexp-quote (nth 2 e
))
1320 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1322 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1324 (setq plist
(plist-put plist
1326 (car (read-from-string
1327 (match-string 2 options
)))))))
1331 (defun org-export--get-subtree-options (&optional backend
)
1332 "Get export options in subtree at point.
1333 Optional argument BACKEND is a symbol specifying back-end used
1334 for export. Return options as a plist."
1335 ;; For each buffer keyword, create an headline property setting the
1336 ;; same property in communication channel. The name for the property
1337 ;; is the keyword with "EXPORT_" appended to it.
1338 (org-with-wide-buffer
1340 ;; Make sure point is at an heading.
1341 (unless (org-at-heading-p) (org-back-to-heading t
))
1342 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1343 ;; title as its fallback value.
1344 (when (setq prop
(progn (looking-at org-todo-line-regexp
)
1345 (or (save-match-data
1346 (org-entry-get (point) "EXPORT_TITLE"))
1347 (org-match-string-no-properties 3))))
1351 (org-element-parse-secondary-string
1352 prop
(org-element-restriction 'keyword
)))))
1353 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1354 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1356 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1357 ;; Handle other keywords. TITLE keyword is excluded as it has
1358 ;; already been handled already.
1359 (let ((seen '("TITLE")))
1362 (let ((property (nth 1 option
)))
1363 (when (and property
(not (member property seen
)))
1364 (let* ((subtree-prop (concat "EXPORT_" property
))
1365 ;; Export properties are not case-sensitive.
1366 (value (let ((case-fold-search t
))
1367 (org-entry-get (point) subtree-prop
))))
1368 (push property seen
)
1374 ;; Parse VALUE if required.
1375 (if (member property org-element-document-properties
)
1376 (org-element-parse-secondary-string
1377 value
(org-element-restriction 'keyword
))
1379 ;; Also look for both general keywords and back-end specific
1380 ;; options if BACKEND is provided.
1381 (append (and backend
1383 (format "org-%s-options-alist" backend
))))
1384 (and (boundp var
) (symbol-value var
))))
1385 org-export-options-alist
)))
1389 (defun org-export--get-inbuffer-options (&optional backend files
)
1390 "Return current buffer export options, as a plist.
1392 Optional argument BACKEND, when non-nil, is a symbol specifying
1393 which back-end specific options should also be read in the
1396 Optional argument FILES is a list of setup files names read so
1397 far, used to avoid circular dependencies.
1399 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1400 (org-with-wide-buffer
1401 (goto-char (point-min))
1402 (let ((case-fold-search t
) plist
)
1403 ;; 1. Special keywords, as in `org-export-special-keywords'.
1405 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords
))))
1406 (while (re-search-forward special-re nil t
)
1407 (let ((element (org-element-at-point)))
1408 (when (eq (org-element-type element
) 'keyword
)
1409 (let* ((key (org-element-property :key element
))
1410 (val (org-element-property :value element
))
1413 ((string= key
"SETUP_FILE")
1416 (org-remove-double-quotes (org-trim val
)))))
1417 ;; Avoid circular dependencies.
1418 (unless (member file files
)
1420 (insert (org-file-contents file
'noerror
))
1422 (org-export--get-inbuffer-options
1423 backend
(cons file files
))))))
1424 ((string= key
"OPTIONS")
1425 (org-export--parse-option-keyword val backend
)))))
1426 (setq plist
(org-combine-plists plist prop
)))))))
1427 ;; 2. Standard options, as in `org-export-options-alist'.
1428 (let* ((all (append org-export-options-alist
1429 ;; Also look for back-end specific options
1430 ;; if BACKEND is defined.
1434 (format "org-%s-options-alist" backend
))))
1435 (and (boundp var
) (eval var
))))))
1436 ;; Build ALIST between keyword name and property name.
1439 (lambda (e) (when (nth 1 e
) (cons (nth 1 e
) (car e
))))
1441 ;; Build regexp matching all keywords associated to export
1442 ;; options. Note: the search is case insensitive.
1443 (opt-re (format "^[ \t]*#\\+%s:"
1445 (delq nil
(mapcar (lambda (e) (nth 1 e
)) all
))))))
1446 (goto-char (point-min))
1447 (while (re-search-forward opt-re nil t
)
1448 (let ((element (org-element-at-point)))
1449 (when (eq (org-element-type element
) 'keyword
)
1450 (let* ((key (org-element-property :key element
))
1451 (val (org-element-property :value element
))
1452 (prop (cdr (assoc key alist
)))
1453 (behaviour (nth 4 (assq prop all
))))
1457 ;; Handle value depending on specified BEHAVIOUR.
1460 (if (not (plist-get plist prop
)) (org-trim val
)
1461 (concat (plist-get plist prop
) " " (org-trim val
))))
1464 (concat (plist-get plist prop
) "\n" (org-trim val
))))
1466 `(,@(plist-get plist prop
) ,@(org-split-string val
)))
1468 (otherwise (if (not (plist-member plist prop
)) val
1469 (plist-get plist prop
))))))))))
1470 ;; Parse keywords specified in
1471 ;; `org-element-document-properties'.
1474 (let* ((prop (cdr (assoc key alist
)))
1475 (value (and prop
(plist-get plist prop
))))
1476 (when (stringp value
)
1480 (org-element-parse-secondary-string
1481 value
(org-element-restriction 'keyword
)))))))
1482 org-element-document-properties
))
1483 ;; 3. Return final value.
1486 (defun org-export--get-buffer-attributes ()
1487 "Return properties related to buffer attributes, as a plist."
1488 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1490 ;; Store full path of input file name, or nil. For internal use.
1491 :input-file visited-file
1492 :title
(or (and visited-file
1493 (file-name-sans-extension
1494 (file-name-nondirectory visited-file
)))
1495 (buffer-name (buffer-base-buffer)))
1496 :footnote-definition-alist
1497 ;; Footnotes definitions must be collected in the original
1498 ;; buffer, as there's no insurance that they will still be in the
1499 ;; parse tree, due to possible narrowing.
1501 (org-with-wide-buffer
1502 (goto-char (point-min))
1503 (while (re-search-forward org-footnote-definition-re nil t
)
1504 (let ((def (save-match-data (org-element-at-point))))
1505 (when (eq (org-element-type def
) 'footnote-definition
)
1507 (cons (org-element-property :label def
)
1508 (let ((cbeg (org-element-property :contents-begin def
)))
1510 (org-element--parse-elements
1511 cbeg
(org-element-property :contents-end def
)
1512 nil nil nil nil
(list 'org-data nil
)))))
1516 ;; Collect id references.
1518 (org-with-wide-buffer
1519 (goto-char (point-min))
1520 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1521 (let ((link (org-element-context)))
1522 (when (eq (org-element-type link
) 'link
)
1523 (let* ((id (org-element-property :path link
))
1524 (file (org-id-find-id-file id
)))
1526 (push (cons id
(file-relative-name file
)) alist
)))))))
1529 (defun org-export--get-global-options (&optional backend
)
1530 "Return global export options as a plist.
1532 Optional argument BACKEND, if non-nil, is a symbol specifying
1533 which back-end specific export options should also be read in the
1535 (let ((all (append org-export-options-alist
1538 (format "org-%s-options-alist" backend
))))
1539 (and (boundp var
) (symbol-value var
))))))
1548 ;; Eval default value provided. If keyword is a member
1549 ;; of `org-element-document-properties', parse it as
1550 ;; a secondary string before storing it.
1551 (let ((value (eval (nth 3 cell
))))
1552 (if (not (stringp value
)) value
1553 (let ((keyword (nth 1 cell
)))
1554 (if (not (member keyword org-element-document-properties
))
1556 (org-element-parse-secondary-string
1557 value
(org-element-restriction 'keyword
)))))))))
1562 (defvar org-export--allow-BIND-local nil
)
1563 (defun org-export--confirm-letbind ()
1564 "Can we use #+BIND values during export?
1565 By default this will ask for confirmation by the user, to divert
1566 possible security risks."
1568 ((not org-export-allow-BIND
) nil
)
1569 ((eq org-export-allow-BIND t
) t
)
1570 ((local-variable-p 'org-export--allow-BIND-local
)
1571 org-export--allow-BIND-local
)
1572 (t (org-set-local 'org-export--allow-BIND-local
1573 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1575 (defun org-export--install-letbind-maybe ()
1576 "Install the values from #+BIND lines as local variables.
1577 Variables must be installed before in-buffer options are
1579 (let ((case-fold-search t
) letbind pair
)
1580 (org-with-wide-buffer
1581 (goto-char (point-min))
1582 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t
)
1583 (let* ((element (org-element-at-point))
1584 (value (org-element-property :value element
)))
1585 (when (and (eq (org-element-type element
) 'keyword
)
1586 (not (equal value
""))
1587 (org-export--confirm-letbind))
1588 (push (read (format "(%s)" value
)) letbind
)))))
1589 (dolist (pair (nreverse letbind
))
1590 (org-set-local (car pair
) (nth 1 pair
)))))
1593 ;;;; Tree Properties
1595 ;; Tree properties are information extracted from parse tree. They
1596 ;; are initialized at the beginning of the transcoding process by
1597 ;; `org-export-collect-tree-properties'.
1599 ;; Dedicated functions focus on computing the value of specific tree
1600 ;; properties during initialization. Thus,
1601 ;; `org-export--populate-ignore-list' lists elements and objects that
1602 ;; should be skipped during export, `org-export--get-min-level' gets
1603 ;; the minimal exportable level, used as a basis to compute relative
1604 ;; level for headlines. Eventually
1605 ;; `org-export--collect-headline-numbering' builds an alist between
1606 ;; headlines and their numbering.
1608 (defun org-export-collect-tree-properties (data info
)
1609 "Extract tree properties from parse tree.
1611 DATA is the parse tree from which information is retrieved. INFO
1612 is a list holding export options.
1614 Following tree properties are set or updated:
1616 `:exported-data' Hash table used to memoize results from
1619 `:footnote-definition-alist' List of footnotes definitions in
1620 original buffer and current parse tree.
1622 `:headline-offset' Offset between true level of headlines and
1623 local level. An offset of -1 means an headline
1624 of level 2 should be considered as a level
1625 1 headline in the context.
1627 `:headline-numbering' Alist of all headlines as key an the
1628 associated numbering as value.
1630 `:ignore-list' List of elements that should be ignored during
1633 `:target-list' List of all targets in the parse tree.
1635 Return updated plist."
1636 ;; Install the parse tree in the communication channel, in order to
1637 ;; use `org-export-get-genealogy' and al.
1638 (setq info
(plist-put info
:parse-tree data
))
1639 ;; Get the list of elements and objects to ignore, and put it into
1640 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1641 ;; been done during parse tree filtering.
1645 (append (org-export--populate-ignore-list data info
)
1646 (plist-get info
:ignore-list
))))
1647 ;; Compute `:headline-offset' in order to be able to use
1648 ;; `org-export-get-relative-level'.
1652 (- 1 (org-export--get-min-level data info
))))
1653 ;; Update footnotes definitions list with definitions in parse tree.
1654 ;; This is required since buffer expansion might have modified
1655 ;; boundaries of footnote definitions contained in the parse tree.
1656 ;; This way, definitions in `footnote-definition-alist' are bound to
1657 ;; match those in the parse tree.
1658 (let ((defs (plist-get info
:footnote-definition-alist
)))
1660 data
'footnote-definition
1662 (push (cons (org-element-property :label fn
)
1663 `(org-data nil
,@(org-element-contents fn
)))
1665 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1666 ;; Properties order doesn't matter: get the rest of the tree
1671 data
'(keyword target
)
1673 (when (or (eq (org-element-type blob
) 'target
)
1674 (string= (org-element-property :key blob
) "TARGET"))
1676 :headline-numbering
,(org-export--collect-headline-numbering data info
)
1677 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1680 (defun org-export--get-min-level (data options
)
1681 "Return minimum exportable headline's level in DATA.
1682 DATA is parsed tree as returned by `org-element-parse-buffer'.
1683 OPTIONS is a plist holding export options."
1685 (let ((min-level 10000))
1688 (when (and (eq (org-element-type blob
) 'headline
)
1689 (not (memq blob
(plist-get options
:ignore-list
))))
1691 (min (org-element-property :level blob
) min-level
)))
1692 (when (= min-level
1) (throw 'exit
1)))
1693 (org-element-contents data
))
1694 ;; If no headline was found, for the sake of consistency, set
1695 ;; minimum level to 1 nonetheless.
1696 (if (= min-level
10000) 1 min-level
))))
1698 (defun org-export--collect-headline-numbering (data options
)
1699 "Return numbering of all exportable headlines in a parse tree.
1701 DATA is the parse tree. OPTIONS is the plist holding export
1704 Return an alist whose key is an headline and value is its
1705 associated numbering \(in the shape of a list of numbers\)."
1706 (let ((numbering (make-vector org-export-max-depth
0)))
1711 (let ((relative-level
1712 (1- (org-export-get-relative-level headline options
))))
1715 (loop for n across numbering
1716 for idx from
0 to org-export-max-depth
1717 when
(< idx relative-level
) collect n
1718 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1719 when
(> idx relative-level
) do
(aset numbering idx
0)))))
1722 (defun org-export--populate-ignore-list (data options
)
1723 "Return list of elements and objects to ignore during export.
1724 DATA is the parse tree to traverse. OPTIONS is the plist holding
1728 ;; First find trees containing a select tag, if any.
1729 (selected (org-export--selected-trees data options
))
1732 ;; Collect ignored elements or objects into IGNORE-LIST.
1733 (let ((type (org-element-type data
)))
1734 (if (org-export--skip-p data options selected
) (push data ignore
)
1735 (if (and (eq type
'headline
)
1736 (eq (plist-get options
:with-archived-trees
) 'headline
)
1737 (org-element-property :archivedp data
))
1738 ;; If headline is archived but tree below has
1739 ;; to be skipped, add it to ignore list.
1740 (mapc (lambda (e) (push e ignore
))
1741 (org-element-contents data
))
1742 ;; Move into secondary string, if any.
1744 (cdr (assq type org-element-secondary-value-alist
))))
1746 (mapc walk-data
(org-element-property sec-prop data
))))
1747 ;; Move into recursive objects/elements.
1748 (mapc walk-data
(org-element-contents data
))))))))
1750 (funcall walk-data data
)
1754 (defun org-export--selected-trees (data info
)
1755 "Return list of headlines containing a select tag in their tree.
1756 DATA is parsed data as returned by `org-element-parse-buffer'.
1757 INFO is a plist holding export options."
1758 (let* (selected-trees
1759 walk-data
; for byte-compiler.
1762 (lambda (data genealogy
)
1763 (case (org-element-type data
)
1764 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1765 (org-element-contents data
)))
1767 (let ((tags (org-element-property :tags data
)))
1768 (if (loop for tag in
(plist-get info
:select-tags
)
1769 thereis
(member tag tags
))
1770 ;; When a select tag is found, mark full
1771 ;; genealogy and every headline within the tree
1773 (setq selected-trees
1776 (org-element-map data
'headline
'identity
)
1778 ;; Else, continue searching in tree, recursively.
1780 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1781 (org-element-contents data
))))))))))
1782 (funcall walk-data data nil
) selected-trees
))
1784 (defun org-export--skip-p (blob options selected
)
1785 "Non-nil when element or object BLOB should be skipped during export.
1786 OPTIONS is the plist holding export options. SELECTED, when
1787 non-nil, is a list of headlines belonging to a tree with a select
1789 (case (org-element-type blob
)
1790 (clock (not (plist-get options
:with-clocks
)))
1792 (or (not (plist-get options
:with-drawers
))
1793 (and (consp (plist-get options
:with-drawers
))
1794 (not (member (org-element-property :drawer-name blob
)
1795 (plist-get options
:with-drawers
))))))
1797 (let ((with-tasks (plist-get options
:with-tasks
))
1798 (todo (org-element-property :todo-keyword blob
))
1799 (todo-type (org-element-property :todo-type blob
))
1800 (archived (plist-get options
:with-archived-trees
))
1801 (tags (org-element-property :tags blob
)))
1803 ;; Ignore subtrees with an exclude tag.
1804 (loop for k in
(plist-get options
:exclude-tags
)
1805 thereis
(member k tags
))
1806 ;; When a select tag is present in the buffer, ignore any tree
1808 (and selected
(not (memq blob selected
)))
1809 ;; Ignore commented sub-trees.
1810 (org-element-property :commentedp blob
)
1811 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1812 (and (not archived
) (org-element-property :archivedp blob
))
1813 ;; Ignore tasks, if specified by `:with-tasks' property.
1815 (or (not with-tasks
)
1816 (and (memq with-tasks
'(todo done
))
1817 (not (eq todo-type with-tasks
)))
1818 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1819 (inlinetask (not (plist-get options
:with-inlinetasks
)))
1820 (planning (not (plist-get options
:with-plannings
)))
1821 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1823 (and (org-export-table-has-special-column-p
1824 (org-export-get-parent-table blob
))
1825 (not (org-export-get-previous-element blob options
))))
1826 (table-row (org-export-table-row-is-special-p blob options
))
1828 (case (plist-get options
:with-timestamps
)
1829 ;; No timestamp allowed.
1831 ;; Only active timestamps allowed and the current one isn't
1834 (not (memq (org-element-property :type blob
)
1835 '(active active-range
))))
1836 ;; Only inactive timestamps allowed and the current one isn't
1839 (not (memq (org-element-property :type blob
)
1840 '(inactive inactive-range
))))))))
1846 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1847 ;; `org-element-parse-buffer') and transcodes it into a specified
1848 ;; back-end output. It takes care of filtering out elements or
1849 ;; objects according to export options and organizing the output blank
1850 ;; lines and white space are preserved. The function memoizes its
1851 ;; results, so it is cheap to call it within translators.
1853 ;; Internally, three functions handle the filtering of objects and
1854 ;; elements during the export. In particular,
1855 ;; `org-export-ignore-element' marks an element or object so future
1856 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1857 ;; elements or objects should be seen as real Org syntax and
1858 ;; `org-export-expand' transforms the others back into their original
1861 ;; `org-export-transcoder' is an accessor returning appropriate
1862 ;; translator function for a given element or object.
1864 (defun org-export-transcoder (blob info
)
1865 "Return appropriate transcoder for BLOB.
1866 INFO is a plist containing export directives."
1867 (let ((type (org-element-type blob
)))
1868 ;; Return contents only for complete parse trees.
1869 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1870 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1871 (and (functionp transcoder
) transcoder
)))))
1873 (defun org-export-data (data info
)
1874 "Convert DATA into current back-end format.
1876 DATA is a parse tree, an element or an object or a secondary
1877 string. INFO is a plist holding export options.
1879 Return transcoded string."
1880 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
1881 (if (not (eq memo
'no-memo
)) memo
1882 (let* ((type (org-element-type data
))
1885 ;; Ignored element/object.
1886 ((memq data
(plist-get info
:ignore-list
)) nil
)
1888 ((eq type
'plain-text
)
1889 (org-export-filter-apply-functions
1890 (plist-get info
:filter-plain-text
)
1891 (let ((transcoder (org-export-transcoder data info
)))
1892 (if transcoder
(funcall transcoder data info
) data
))
1894 ;; Uninterpreted element/object: change it back to Org
1895 ;; syntax and export again resulting raw string.
1896 ((not (org-export--interpret-p data info
))
1900 (mapconcat (lambda (blob) (org-export-data blob info
))
1901 (org-element-contents data
)
1904 ;; Secondary string.
1906 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1907 ;; Element/Object without contents or, as a special case,
1908 ;; headline with archive tag and archived trees restricted
1910 ((or (not (org-element-contents data
))
1911 (and (eq type
'headline
)
1912 (eq (plist-get info
:with-archived-trees
) 'headline
)
1913 (org-element-property :archivedp data
)))
1914 (let ((transcoder (org-export-transcoder data info
)))
1915 (and (functionp transcoder
)
1916 (funcall transcoder data nil info
))))
1917 ;; Element/Object with contents.
1919 (let ((transcoder (org-export-transcoder data info
)))
1921 (let* ((greaterp (memq type org-element-greater-elements
))
1924 (memq type org-element-recursive-objects
)))
1927 (lambda (element) (org-export-data element info
))
1928 (org-element-contents
1929 (if (or greaterp objectp
) data
1930 ;; Elements directly containing objects
1931 ;; must have their indentation normalized
1933 (org-element-normalize-contents
1935 ;; When normalizing contents of the first
1936 ;; paragraph in an item or a footnote
1937 ;; definition, ignore first line's
1938 ;; indentation: there is none and it
1939 ;; might be misleading.
1940 (when (eq type
'paragraph
)
1941 (let ((parent (org-export-get-parent data
)))
1943 (eq (car (org-element-contents parent
))
1945 (memq (org-element-type parent
)
1946 '(footnote-definition item
))))))))
1948 (funcall transcoder data
1949 (if (not greaterp
) contents
1950 (org-element-normalize-string contents
))
1952 ;; Final result will be memoized before being returned.
1957 ((memq type
'(org-data plain-text nil
)) results
)
1958 ;; Append the same white space between elements or objects as in
1959 ;; the original buffer, and call appropriate filters.
1962 (org-export-filter-apply-functions
1963 (plist-get info
(intern (format ":filter-%s" type
)))
1964 (let ((post-blank (or (org-element-property :post-blank data
)
1966 (if (memq type org-element-all-elements
)
1967 (concat (org-element-normalize-string results
)
1968 (make-string post-blank ?
\n))
1969 (concat results
(make-string post-blank ?
))))
1972 (plist-get info
:exported-data
))))))
1974 (defun org-export--interpret-p (blob info
)
1975 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1976 Check is done according to export options INFO, stored as
1978 (case (org-element-type blob
)
1980 (entity (plist-get info
:with-entities
))
1982 (emphasis (plist-get info
:with-emphasize
))
1983 ;; ... fixed-width areas.
1984 (fixed-width (plist-get info
:with-fixed-width
))
1986 ((footnote-definition footnote-reference
)
1987 (plist-get info
:with-footnotes
))
1988 ;; ... sub/superscripts...
1989 ((subscript superscript
)
1990 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
1991 (if (eq sub
/super-p
'{})
1992 (org-element-property :use-brackets-p blob
)
1995 (table (plist-get info
:with-tables
))
1998 (defun org-export-expand (blob contents
)
1999 "Expand a parsed element or object to its original state.
2000 BLOB is either an element or an object. CONTENTS is its
2001 contents, as a string or nil."
2003 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
2006 (defun org-export-ignore-element (element info
)
2007 "Add ELEMENT to `:ignore-list' in INFO.
2009 Any element in `:ignore-list' will be skipped when using
2010 `org-element-map'. INFO is modified by side effects."
2011 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2015 ;;; The Filter System
2017 ;; Filters allow end-users to tweak easily the transcoded output.
2018 ;; They are the functional counterpart of hooks, as every filter in
2019 ;; a set is applied to the return value of the previous one.
2021 ;; Every set is back-end agnostic. Although, a filter is always
2022 ;; called, in addition to the string it applies to, with the back-end
2023 ;; used as argument, so it's easy for the end-user to add back-end
2024 ;; specific filters in the set. The communication channel, as
2025 ;; a plist, is required as the third argument.
2027 ;; From the developer side, filters sets can be installed in the
2028 ;; process with the help of `org-export-define-backend', which
2029 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2030 ;; association has a key among the following symbols and a function or
2031 ;; a list of functions as value.
2033 ;; - `:filter-parse-tree' applies directly on the complete parsed
2034 ;; tree. It's the only filters set that doesn't apply to a string.
2035 ;; Users can set it through `org-export-filter-parse-tree-functions'
2038 ;; - `:filter-final-output' applies to the final transcoded string.
2039 ;; Users can set it with `org-export-filter-final-output-functions'
2042 ;; - `:filter-plain-text' applies to any string not recognized as Org
2043 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2046 ;; - `:filter-TYPE' applies on the string returned after an element or
2047 ;; object of type TYPE has been transcoded. An user can modify
2048 ;; `org-export-filter-TYPE-functions'
2050 ;; All filters sets are applied with
2051 ;; `org-export-filter-apply-functions' function. Filters in a set are
2052 ;; applied in a LIFO fashion. It allows developers to be sure that
2053 ;; their filters will be applied first.
2055 ;; Filters properties are installed in communication channel with
2056 ;; `org-export-install-filters' function.
2058 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2059 ;; before parsing to allow for heavy structure modifications.
2062 ;;;; Before Parsing Hook
2064 (defvar org-export-before-parsing-hook nil
2065 "Hook run before parsing an export buffer.
2067 This is run after include keywords have been expanded and Babel
2068 code executed, on a copy of original buffer's area being
2069 exported. Visibility is the same as in the original one. Point
2070 is left at the beginning of the new one.
2072 Every function in this hook will be called with one argument: the
2073 back-end currently used, as a symbol.")
2076 ;;;; Special Filters
2078 (defvar org-export-filter-parse-tree-functions nil
2079 "List of functions applied to the parsed tree.
2080 Each filter is called with three arguments: the parse tree, as
2081 returned by `org-element-parse-buffer', the back-end, as
2082 a symbol, and the communication channel, as a plist. It must
2083 return the modified parse tree to transcode.")
2085 (defvar org-export-filter-final-output-functions nil
2086 "List of functions applied to the transcoded string.
2087 Each filter is called with three arguments: the full transcoded
2088 string, the back-end, as a symbol, and the communication channel,
2089 as a plist. It must return a string that will be used as the
2090 final export output.")
2092 (defvar org-export-filter-plain-text-functions nil
2093 "List of functions applied to plain text.
2094 Each filter is called with three arguments: a string which
2095 contains no Org syntax, the back-end, as a symbol, and the
2096 communication channel, as a plist. It must return a string or
2100 ;;;; Elements Filters
2102 (defvar org-export-filter-babel-call-functions nil
2103 "List of functions applied to a transcoded babel-call.
2104 Each filter is called with three arguments: the transcoded data,
2105 as a string, the back-end, as a symbol, and the communication
2106 channel, as a plist. It must return a string or nil.")
2108 (defvar org-export-filter-center-block-functions nil
2109 "List of functions applied to a transcoded center block.
2110 Each filter is called with three arguments: the transcoded data,
2111 as a string, the back-end, as a symbol, and the communication
2112 channel, as a plist. It must return a string or nil.")
2114 (defvar org-export-filter-clock-functions nil
2115 "List of functions applied to a transcoded clock.
2116 Each filter is called with three arguments: the transcoded data,
2117 as a string, the back-end, as a symbol, and the communication
2118 channel, as a plist. It must return a string or nil.")
2120 (defvar org-export-filter-comment-functions nil
2121 "List of functions applied to a transcoded comment.
2122 Each filter is called with three arguments: the transcoded data,
2123 as a string, the back-end, as a symbol, and the communication
2124 channel, as a plist. It must return a string or nil.")
2126 (defvar org-export-filter-comment-block-functions nil
2127 "List of functions applied to a transcoded comment-comment.
2128 Each filter is called with three arguments: the transcoded data,
2129 as a string, the back-end, as a symbol, and the communication
2130 channel, as a plist. It must return a string or nil.")
2132 (defvar org-export-filter-drawer-functions nil
2133 "List of functions applied to a transcoded drawer.
2134 Each filter is called with three arguments: the transcoded data,
2135 as a string, the back-end, as a symbol, and the communication
2136 channel, as a plist. It must return a string or nil.")
2138 (defvar org-export-filter-dynamic-block-functions nil
2139 "List of functions applied to a transcoded dynamic-block.
2140 Each filter is called with three arguments: the transcoded data,
2141 as a string, the back-end, as a symbol, and the communication
2142 channel, as a plist. It must return a string or nil.")
2144 (defvar org-export-filter-example-block-functions nil
2145 "List of functions applied to a transcoded example-block.
2146 Each filter is called with three arguments: the transcoded data,
2147 as a string, the back-end, as a symbol, and the communication
2148 channel, as a plist. It must return a string or nil.")
2150 (defvar org-export-filter-export-block-functions nil
2151 "List of functions applied to a transcoded export-block.
2152 Each filter is called with three arguments: the transcoded data,
2153 as a string, the back-end, as a symbol, and the communication
2154 channel, as a plist. It must return a string or nil.")
2156 (defvar org-export-filter-fixed-width-functions nil
2157 "List of functions applied to a transcoded fixed-width.
2158 Each filter is called with three arguments: the transcoded data,
2159 as a string, the back-end, as a symbol, and the communication
2160 channel, as a plist. It must return a string or nil.")
2162 (defvar org-export-filter-footnote-definition-functions nil
2163 "List of functions applied to a transcoded footnote-definition.
2164 Each filter is called with three arguments: the transcoded data,
2165 as a string, the back-end, as a symbol, and the communication
2166 channel, as a plist. It must return a string or nil.")
2168 (defvar org-export-filter-headline-functions nil
2169 "List of functions applied to a transcoded headline.
2170 Each filter is called with three arguments: the transcoded data,
2171 as a string, the back-end, as a symbol, and the communication
2172 channel, as a plist. It must return a string or nil.")
2174 (defvar org-export-filter-horizontal-rule-functions nil
2175 "List of functions applied to a transcoded horizontal-rule.
2176 Each filter is called with three arguments: the transcoded data,
2177 as a string, the back-end, as a symbol, and the communication
2178 channel, as a plist. It must return a string or nil.")
2180 (defvar org-export-filter-inlinetask-functions nil
2181 "List of functions applied to a transcoded inlinetask.
2182 Each filter is called with three arguments: the transcoded data,
2183 as a string, the back-end, as a symbol, and the communication
2184 channel, as a plist. It must return a string or nil.")
2186 (defvar org-export-filter-item-functions nil
2187 "List of functions applied to a transcoded item.
2188 Each filter is called with three arguments: the transcoded data,
2189 as a string, the back-end, as a symbol, and the communication
2190 channel, as a plist. It must return a string or nil.")
2192 (defvar org-export-filter-keyword-functions nil
2193 "List of functions applied to a transcoded keyword.
2194 Each filter is called with three arguments: the transcoded data,
2195 as a string, the back-end, as a symbol, and the communication
2196 channel, as a plist. It must return a string or nil.")
2198 (defvar org-export-filter-latex-environment-functions nil
2199 "List of functions applied to a transcoded latex-environment.
2200 Each filter is called with three arguments: the transcoded data,
2201 as a string, the back-end, as a symbol, and the communication
2202 channel, as a plist. It must return a string or nil.")
2204 (defvar org-export-filter-node-property-functions nil
2205 "List of functions applied to a transcoded node-property.
2206 Each filter is called with three arguments: the transcoded data,
2207 as a string, the back-end, as a symbol, and the communication
2208 channel, as a plist. It must return a string or nil.")
2210 (defvar org-export-filter-paragraph-functions nil
2211 "List of functions applied to a transcoded paragraph.
2212 Each filter is called with three arguments: the transcoded data,
2213 as a string, the back-end, as a symbol, and the communication
2214 channel, as a plist. It must return a string or nil.")
2216 (defvar org-export-filter-plain-list-functions nil
2217 "List of functions applied to a transcoded plain-list.
2218 Each filter is called with three arguments: the transcoded data,
2219 as a string, the back-end, as a symbol, and the communication
2220 channel, as a plist. It must return a string or nil.")
2222 (defvar org-export-filter-planning-functions nil
2223 "List of functions applied to a transcoded planning.
2224 Each filter is called with three arguments: the transcoded data,
2225 as a string, the back-end, as a symbol, and the communication
2226 channel, as a plist. It must return a string or nil.")
2228 (defvar org-export-filter-property-drawer-functions nil
2229 "List of functions applied to a transcoded property-drawer.
2230 Each filter is called with three arguments: the transcoded data,
2231 as a string, the back-end, as a symbol, and the communication
2232 channel, as a plist. It must return a string or nil.")
2234 (defvar org-export-filter-quote-block-functions nil
2235 "List of functions applied to a transcoded quote block.
2236 Each filter is called with three arguments: the transcoded quote
2237 data, as a string, the back-end, as a symbol, and the
2238 communication channel, as a plist. It must return a string or
2241 (defvar org-export-filter-quote-section-functions nil
2242 "List of functions applied to a transcoded quote-section.
2243 Each filter is called with three arguments: the transcoded data,
2244 as a string, the back-end, as a symbol, and the communication
2245 channel, as a plist. It must return a string or nil.")
2247 (defvar org-export-filter-section-functions nil
2248 "List of functions applied to a transcoded section.
2249 Each filter is called with three arguments: the transcoded data,
2250 as a string, the back-end, as a symbol, and the communication
2251 channel, as a plist. It must return a string or nil.")
2253 (defvar org-export-filter-special-block-functions nil
2254 "List of functions applied to a transcoded special block.
2255 Each filter is called with three arguments: the transcoded data,
2256 as a string, the back-end, as a symbol, and the communication
2257 channel, as a plist. It must return a string or nil.")
2259 (defvar org-export-filter-src-block-functions nil
2260 "List of functions applied to a transcoded src-block.
2261 Each filter is called with three arguments: the transcoded data,
2262 as a string, the back-end, as a symbol, and the communication
2263 channel, as a plist. It must return a string or nil.")
2265 (defvar org-export-filter-table-functions nil
2266 "List of functions applied to a transcoded table.
2267 Each filter is called with three arguments: the transcoded data,
2268 as a string, the back-end, as a symbol, and the communication
2269 channel, as a plist. It must return a string or nil.")
2271 (defvar org-export-filter-table-cell-functions nil
2272 "List of functions applied to a transcoded table-cell.
2273 Each filter is called with three arguments: the transcoded data,
2274 as a string, the back-end, as a symbol, and the communication
2275 channel, as a plist. It must return a string or nil.")
2277 (defvar org-export-filter-table-row-functions nil
2278 "List of functions applied to a transcoded table-row.
2279 Each filter is called with three arguments: the transcoded data,
2280 as a string, the back-end, as a symbol, and the communication
2281 channel, as a plist. It must return a string or nil.")
2283 (defvar org-export-filter-verse-block-functions nil
2284 "List of functions applied to a transcoded verse block.
2285 Each filter is called with three arguments: the transcoded data,
2286 as a string, the back-end, as a symbol, and the communication
2287 channel, as a plist. It must return a string or nil.")
2290 ;;;; Objects Filters
2292 (defvar org-export-filter-bold-functions nil
2293 "List of functions applied to transcoded bold text.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2298 (defvar org-export-filter-code-functions nil
2299 "List of functions applied to transcoded code text.
2300 Each filter is called with three arguments: the transcoded data,
2301 as a string, the back-end, as a symbol, and the communication
2302 channel, as a plist. It must return a string or nil.")
2304 (defvar org-export-filter-entity-functions nil
2305 "List of functions applied to a transcoded entity.
2306 Each filter is called with three arguments: the transcoded data,
2307 as a string, the back-end, as a symbol, and the communication
2308 channel, as a plist. It must return a string or nil.")
2310 (defvar org-export-filter-export-snippet-functions nil
2311 "List of functions applied to a transcoded export-snippet.
2312 Each filter is called with three arguments: the transcoded data,
2313 as a string, the back-end, as a symbol, and the communication
2314 channel, as a plist. It must return a string or nil.")
2316 (defvar org-export-filter-footnote-reference-functions nil
2317 "List of functions applied to a transcoded footnote-reference.
2318 Each filter is called with three arguments: the transcoded data,
2319 as a string, the back-end, as a symbol, and the communication
2320 channel, as a plist. It must return a string or nil.")
2322 (defvar org-export-filter-inline-babel-call-functions nil
2323 "List of functions applied to a transcoded inline-babel-call.
2324 Each filter is called with three arguments: the transcoded data,
2325 as a string, the back-end, as a symbol, and the communication
2326 channel, as a plist. It must return a string or nil.")
2328 (defvar org-export-filter-inline-src-block-functions nil
2329 "List of functions applied to a transcoded inline-src-block.
2330 Each filter is called with three arguments: the transcoded data,
2331 as a string, the back-end, as a symbol, and the communication
2332 channel, as a plist. It must return a string or nil.")
2334 (defvar org-export-filter-italic-functions nil
2335 "List of functions applied to transcoded italic text.
2336 Each filter is called with three arguments: the transcoded data,
2337 as a string, the back-end, as a symbol, and the communication
2338 channel, as a plist. It must return a string or nil.")
2340 (defvar org-export-filter-latex-fragment-functions nil
2341 "List of functions applied to a transcoded latex-fragment.
2342 Each filter is called with three arguments: the transcoded data,
2343 as a string, the back-end, as a symbol, and the communication
2344 channel, as a plist. It must return a string or nil.")
2346 (defvar org-export-filter-line-break-functions nil
2347 "List of functions applied to a transcoded line-break.
2348 Each filter is called with three arguments: the transcoded data,
2349 as a string, the back-end, as a symbol, and the communication
2350 channel, as a plist. It must return a string or nil.")
2352 (defvar org-export-filter-link-functions nil
2353 "List of functions applied to a transcoded link.
2354 Each filter is called with three arguments: the transcoded data,
2355 as a string, the back-end, as a symbol, and the communication
2356 channel, as a plist. It must return a string or nil.")
2358 (defvar org-export-filter-macro-functions nil
2359 "List of functions applied to a transcoded macro.
2360 Each filter is called with three arguments: the transcoded data,
2361 as a string, the back-end, as a symbol, and the communication
2362 channel, as a plist. It must return a string or nil.")
2364 (defvar org-export-filter-radio-target-functions nil
2365 "List of functions applied to a transcoded radio-target.
2366 Each filter is called with three arguments: the transcoded data,
2367 as a string, the back-end, as a symbol, and the communication
2368 channel, as a plist. It must return a string or nil.")
2370 (defvar org-export-filter-statistics-cookie-functions nil
2371 "List of functions applied to a transcoded statistics-cookie.
2372 Each filter is called with three arguments: the transcoded data,
2373 as a string, the back-end, as a symbol, and the communication
2374 channel, as a plist. It must return a string or nil.")
2376 (defvar org-export-filter-strike-through-functions nil
2377 "List of functions applied to transcoded strike-through text.
2378 Each filter is called with three arguments: the transcoded data,
2379 as a string, the back-end, as a symbol, and the communication
2380 channel, as a plist. It must return a string or nil.")
2382 (defvar org-export-filter-subscript-functions nil
2383 "List of functions applied to a transcoded subscript.
2384 Each filter is called with three arguments: the transcoded data,
2385 as a string, the back-end, as a symbol, and the communication
2386 channel, as a plist. It must return a string or nil.")
2388 (defvar org-export-filter-superscript-functions nil
2389 "List of functions applied to a transcoded superscript.
2390 Each filter is called with three arguments: the transcoded data,
2391 as a string, the back-end, as a symbol, and the communication
2392 channel, as a plist. It must return a string or nil.")
2394 (defvar org-export-filter-target-functions nil
2395 "List of functions applied to a transcoded target.
2396 Each filter is called with three arguments: the transcoded data,
2397 as a string, the back-end, as a symbol, and the communication
2398 channel, as a plist. It must return a string or nil.")
2400 (defvar org-export-filter-timestamp-functions nil
2401 "List of functions applied to a transcoded timestamp.
2402 Each filter is called with three arguments: the transcoded data,
2403 as a string, the back-end, as a symbol, and the communication
2404 channel, as a plist. It must return a string or nil.")
2406 (defvar org-export-filter-underline-functions nil
2407 "List of functions applied to transcoded underline text.
2408 Each filter is called with three arguments: the transcoded data,
2409 as a string, the back-end, as a symbol, and the communication
2410 channel, as a plist. It must return a string or nil.")
2412 (defvar org-export-filter-verbatim-functions nil
2413 "List of functions applied to transcoded verbatim text.
2414 Each filter is called with three arguments: the transcoded data,
2415 as a string, the back-end, as a symbol, and the communication
2416 channel, as a plist. It must return a string or nil.")
2421 ;; Internal function `org-export-install-filters' installs filters
2422 ;; hard-coded in back-ends (developer filters) and filters from global
2423 ;; variables (user filters) in the communication channel.
2425 ;; Internal function `org-export-filter-apply-functions' takes care
2426 ;; about applying each filter in order to a given data. It ignores
2427 ;; filters returning a nil value but stops whenever a filter returns
2430 (defun org-export-filter-apply-functions (filters value info
)
2431 "Call every function in FILTERS.
2433 Functions are called with arguments VALUE, current export
2434 back-end and INFO. A function returning a nil value will be
2435 skipped. If it returns the empty string, the process ends and
2438 Call is done in a LIFO fashion, to be sure that developer
2439 specified filters, if any, are called first."
2441 (dolist (filter filters value
)
2442 (let ((result (funcall filter value
(plist-get info
:back-end
) info
)))
2443 (cond ((not result
) value
)
2444 ((equal value
"") (throw 'exit nil
))
2445 (t (setq value result
)))))))
2447 (defun org-export-install-filters (info)
2448 "Install filters properties in communication channel.
2450 INFO is a plist containing the current communication channel.
2452 Return the updated communication channel."
2454 ;; Install user defined filters with `org-export-filters-alist'.
2456 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2457 org-export-filters-alist
)
2458 ;; Prepend back-end specific filters to that list.
2459 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2460 (plist-get info
:back-end
)))))
2461 (when (boundp back-end-filters
)
2463 ;; Single values get consed, lists are prepended.
2464 (let ((key (car p
)) (value (cdr p
)))
2469 (if (atom value
) (cons value
(plist-get plist key
))
2470 (append value
(plist-get plist key
))))))))
2471 (eval back-end-filters
))))
2472 ;; Return new communication channel.
2473 (org-combine-plists info plist
)))
2479 ;; This is the room for the main function, `org-export-as', along with
2480 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2481 ;; They differ only by the way they output the resulting code.
2483 ;; `org-export-output-file-name' is an auxiliary function meant to be
2484 ;; used with `org-export-to-file'. With a given extension, it tries
2485 ;; to provide a canonical file name to write export output to.
2487 ;; Note that `org-export-as' doesn't really parse the current buffer,
2488 ;; but a copy of it (with the same buffer-local variables and
2489 ;; visibility), where macros and include keywords are expanded and
2490 ;; Babel blocks are executed, if appropriate.
2491 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2493 ;; File inclusion is taken care of by
2494 ;; `org-export-expand-include-keyword' and
2495 ;; `org-export--prepare-file-contents'. Structure wise, including
2496 ;; a whole Org file in a buffer often makes little sense. For
2497 ;; example, if the file contains an headline and the include keyword
2498 ;; was within an item, the item should contain the headline. That's
2499 ;; why file inclusion should be done before any structure can be
2500 ;; associated to the file, that is before parsing.
2502 ;; Macro are expanded with `org-export-expand-macro'.
2504 (defun org-export-as
2505 (backend &optional subtreep visible-only body-only ext-plist noexpand
)
2506 "Transcode current Org buffer into BACKEND code.
2508 If narrowing is active in the current buffer, only transcode its
2511 If a region is active, transcode that region.
2513 When optional argument SUBTREEP is non-nil, transcode the
2514 sub-tree at point, extracting information from the headline
2517 When optional argument VISIBLE-ONLY is non-nil, don't export
2518 contents of hidden elements.
2520 When optional argument BODY-ONLY is non-nil, only return body
2521 code, without preamble nor postamble.
2523 Optional argument EXT-PLIST, when provided, is a property list
2524 with external parameters overriding Org default settings, but
2525 still inferior to file-local settings.
2527 Optional argument NOEXPAND, when non-nil, prevents included files
2528 to be expanded and Babel code to be executed.
2530 Return code as a string."
2531 ;; Barf if BACKEND isn't registered.
2532 (org-export-barf-if-invalid-backend backend
)
2535 ;; Narrow buffer to an appropriate region or subtree for
2536 ;; parsing. If parsing subtree, be sure to remove main headline
2538 (cond ((org-region-active-p)
2539 (narrow-to-region (region-beginning) (region-end)))
2541 (org-narrow-to-subtree)
2542 (goto-char (point-min))
2544 (narrow-to-region (point) (point-max))))
2545 ;; 1. Get export environment from original buffer. Also install
2546 ;; user's and developer's filters.
2547 (let* ((info (org-export-install-filters
2548 (org-export-get-environment backend subtreep ext-plist
)))
2549 ;; 2. Get parse tree. Buffer isn't parsed directly.
2550 ;; Instead, a temporary copy is created, where macros
2551 ;; and include keywords are expanded and code blocks
2553 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2555 (org-export-with-current-buffer-copy
2557 (org-export-expand-macro info
)
2558 (org-export-expand-include-keyword)
2559 ;; TODO: Setting `org-current-export-file' is
2560 ;; required by Org Babel to properly resolve
2561 ;; noweb references. Once "org-exp.el" is
2563 ;; `org-export-blocks-preprocess' so it
2564 ;; accepts the value as an argument instead.
2565 (let ((org-current-export-file buf
))
2566 (org-export-blocks-preprocess)))
2567 (goto-char (point-min))
2569 ;; `org-export-before-parsing-hook'. with current
2570 ;; back-end as argument.
2572 'org-export-before-parsing-hook backend
)
2573 ;; Eventually parse buffer.
2574 (org-element-parse-buffer nil visible-only
)))))
2575 ;; 3. Call parse-tree filters to get the final tree.
2577 (org-export-filter-apply-functions
2578 (plist-get info
:filter-parse-tree
) tree info
))
2579 ;; 4. Now tree is complete, compute its properties and add
2580 ;; them to communication channel.
2583 info
(org-export-collect-tree-properties tree info
)))
2584 ;; 5. Eventually transcode TREE. Wrap the resulting string
2585 ;; into a template, if required. Eventually call
2586 ;; final-output filter.
2587 (let* ((body (org-element-normalize-string (org-export-data tree info
)))
2588 (template (cdr (assq 'template
2589 (plist-get info
:translate-alist
))))
2590 (output (org-export-filter-apply-functions
2591 (plist-get info
:filter-final-output
)
2592 (if (or (not (functionp template
)) body-only
) body
2593 (funcall template body info
))
2595 ;; Maybe add final OUTPUT to kill ring, then return it.
2596 (when org-export-copy-to-kill-ring
(org-kill-new output
))
2599 (defun org-export-to-buffer
2600 (backend buffer
&optional subtreep visible-only body-only ext-plist noexpand
)
2601 "Call `org-export-as' with output to a specified buffer.
2603 BACKEND is the back-end used for transcoding, as a symbol.
2605 BUFFER is the output buffer. If it already exists, it will be
2606 erased first, otherwise, it will be created.
2608 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2609 and NOEXPAND are similar to those used in `org-export-as', which
2613 (let ((out (org-export-as
2614 backend subtreep visible-only body-only ext-plist noexpand
))
2615 (buffer (get-buffer-create buffer
)))
2616 (with-current-buffer buffer
2619 (goto-char (point-min)))
2622 (defun org-export-to-file
2623 (backend file
&optional subtreep visible-only body-only ext-plist noexpand
)
2624 "Call `org-export-as' with output to a specified file.
2626 BACKEND is the back-end used for transcoding, as a symbol. FILE
2627 is the name of the output file, as a string.
2629 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2630 and NOEXPAND are similar to those used in `org-export-as', which
2633 Return output file's name."
2634 ;; Checks for FILE permissions. `write-file' would do the same, but
2635 ;; we'd rather avoid needless transcoding of parse tree.
2636 (unless (file-writable-p file
) (error "Output file not writable"))
2637 ;; Insert contents to a temporary buffer and write it to FILE.
2638 (let ((out (org-export-as
2639 backend subtreep visible-only body-only ext-plist noexpand
)))
2642 (let ((coding-system-for-write org-export-coding-system
))
2643 (write-file file
))))
2644 ;; Return full path.
2647 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2648 "Return output file's name according to buffer specifications.
2650 EXTENSION is a string representing the output file extension,
2651 with the leading dot.
2653 With a non-nil optional argument SUBTREEP, try to determine
2654 output file's name by looking for \"EXPORT_FILE_NAME\" property
2655 of subtree at point.
2657 When optional argument PUB-DIR is set, use it as the publishing
2660 When optional argument VISIBLE-ONLY is non-nil, don't export
2661 contents of hidden elements.
2663 Return file name as a string, or nil if it couldn't be
2666 ;; File name may come from EXPORT_FILE_NAME subtree property,
2667 ;; assuming point is at beginning of said sub-tree.
2668 (file-name-sans-extension
2672 (ignore-errors (org-back-to-heading) (point)))
2673 "EXPORT_FILE_NAME" t
))
2674 ;; File name may be extracted from buffer's associated
2676 (buffer-file-name (buffer-base-buffer))
2677 ;; Can't determine file name on our own: Ask user.
2678 (let ((read-file-name-function
2679 (and org-completion-use-ido
'ido-read-file-name
)))
2681 "Output file: " pub-dir nil nil nil
2683 (string= (file-name-extension name t
) extension
))))))))
2684 ;; Build file name. Enforce EXTENSION over whatever user may have
2685 ;; come up with. PUB-DIR, if defined, always has precedence over
2686 ;; any provided path.
2689 (concat (file-name-as-directory pub-dir
)
2690 (file-name-nondirectory base-name
)
2692 ((string= (file-name-nondirectory base-name
) base-name
)
2693 (concat (file-name-as-directory ".") base-name extension
))
2694 (t (concat base-name extension
)))))
2696 (defmacro org-export-with-current-buffer-copy
(&rest body
)
2697 "Apply BODY in a copy of the current buffer.
2699 The copy preserves local variables and visibility of the original
2702 Point is at buffer's beginning when BODY is applied."
2703 (org-with-gensyms (original-buffer offset buffer-string overlays
)
2704 `(let ((,original-buffer
(current-buffer))
2705 (,offset
(1- (point-min)))
2706 (,buffer-string
(buffer-string))
2708 'copy-overlay
(overlays-in (point-min) (point-max)))))
2710 (let ((buffer-invisibility-spec nil
))
2711 (org-clone-local-variables
2713 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2714 (insert ,buffer-string
)
2718 (- (overlay-start ov
) ,offset
)
2719 (- (overlay-end ov
) ,offset
)
2722 (goto-char (point-min))
2724 (def-edebug-spec org-export-with-current-buffer-copy
(body))
2726 (defun org-export-expand-macro (info)
2727 "Expand every macro in buffer.
2728 INFO is a plist containing export options and buffer properties."
2729 (org-macro-replace-all
2730 ;; Before expanding macros, install {{{author}}}, {{{date}}},
2731 ;; {{{email}}} and {{{title}}} templates.
2733 (list (cons "author"
2734 (org-element-interpret-data (plist-get info
:author
)))
2736 (org-element-interpret-data (plist-get info
:date
)))
2737 ;; EMAIL is not a parsed keyword: store it as-is.
2738 (cons "email" (or (plist-get info
:email
) ""))
2740 (org-element-interpret-data (plist-get info
:title
))))
2741 org-macro-templates
)))
2743 (defun org-export-expand-include-keyword (&optional included dir
)
2744 "Expand every include keyword in buffer.
2745 Optional argument INCLUDED is a list of included file names along
2746 with their line restriction, when appropriate. It is used to
2747 avoid infinite recursion. Optional argument DIR is the current
2748 working directory. It is used to properly resolve relative
2750 (let ((case-fold-search t
))
2751 (goto-char (point-min))
2752 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t
)
2753 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2756 ;; Extract arguments from keyword's value.
2757 (let* ((value (match-string 1))
2758 (ind (org-get-indentation))
2759 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
2760 (prog1 (expand-file-name (match-string 1 value
) dir
)
2761 (setq value
(replace-match "" nil nil value
)))))
2764 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
2765 (prog1 (match-string 1 value
)
2766 (setq value
(replace-match "" nil nil value
)))))
2767 (env (cond ((string-match "\\<example\\>" value
) 'example
)
2768 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
2769 (match-string 1 value
))))
2770 ;; Minimal level of included file defaults to the child
2771 ;; level of the current headline, if any, or one. It
2772 ;; only applies is the file is meant to be included as
2776 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
2777 (prog1 (string-to-number (match-string 1 value
))
2778 (setq value
(replace-match "" nil nil value
)))
2779 (let ((cur (org-current-level)))
2780 (if cur
(1+ (org-reduced-level cur
)) 1))))))
2782 (delete-region (point) (progn (forward-line) (point)))
2784 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
2785 ;; Check if files has already been parsed. Look after
2786 ;; inclusion lines too, as different parts of the same file
2787 ;; can be included too.
2788 ((member (list file lines
) included
)
2789 (error "Recursive file inclusion: %s" file
))
2794 (let ((ind-str (make-string ind ?
))
2796 (org-escape-code-in-string
2797 (org-export--prepare-file-contents file lines
))))
2798 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2799 ind-str contents ind-str
))))
2802 (let ((ind-str (make-string ind ?
))
2804 (org-escape-code-in-string
2805 (org-export--prepare-file-contents file lines
))))
2806 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2807 ind-str env contents ind-str
))))
2813 (org-export--prepare-file-contents file lines ind minlevel
))
2814 (org-export-expand-include-keyword
2815 (cons (list file lines
) included
)
2816 (file-name-directory file
))
2817 (buffer-string))))))))))))
2819 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
2820 "Prepare the contents of FILE for inclusion and return them as a string.
2822 When optional argument LINES is a string specifying a range of
2823 lines, include only those lines.
2825 Optional argument IND, when non-nil, is an integer specifying the
2826 global indentation of returned contents. Since its purpose is to
2827 allow an included file to stay in the same environment it was
2828 created \(i.e. a list item), it doesn't apply past the first
2829 headline encountered.
2831 Optional argument MINLEVEL, when non-nil, is an integer
2832 specifying the level that any top-level headline in the included
2835 (insert-file-contents file
)
2837 (let* ((lines (split-string lines
"-"))
2838 (lbeg (string-to-number (car lines
)))
2839 (lend (string-to-number (cadr lines
)))
2840 (beg (if (zerop lbeg
) (point-min)
2841 (goto-char (point-min))
2842 (forward-line (1- lbeg
))
2844 (end (if (zerop lend
) (point-max)
2845 (goto-char (point-min))
2846 (forward-line (1- lend
))
2848 (narrow-to-region beg end
)))
2849 ;; Remove blank lines at beginning and end of contents. The logic
2850 ;; behind that removal is that blank lines around include keyword
2851 ;; override blank lines in included file.
2852 (goto-char (point-min))
2853 (org-skip-whitespace)
2855 (delete-region (point-min) (point))
2856 (goto-char (point-max))
2857 (skip-chars-backward " \r\t\n")
2859 (delete-region (point) (point-max))
2860 ;; If IND is set, preserve indentation of include keyword until
2861 ;; the first headline encountered.
2863 (unless (eq major-mode
'org-mode
) (org-mode))
2864 (goto-char (point-min))
2865 (let ((ind-str (make-string ind ?
)))
2866 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
2867 ;; Do not move footnote definitions out of column 0.
2868 (unless (and (looking-at org-footnote-definition-re
)
2869 (eq (org-element-type (org-element-at-point))
2870 'footnote-definition
))
2873 ;; When MINLEVEL is specified, compute minimal level for headlines
2874 ;; in the file (CUR-MIN), and remove stars to each headline so
2875 ;; that headlines with minimal level have a level of MINLEVEL.
2877 (unless (eq major-mode
'org-mode
) (org-mode))
2878 (let ((levels (org-map-entries
2879 (lambda () (org-reduced-level (org-current-level))))))
2881 (let ((offset (- minlevel
(apply 'min levels
))))
2882 (unless (zerop offset
)
2883 (when org-odd-levels-only
(setq offset
(* offset
2)))
2884 ;; Only change stars, don't bother moving whole
2887 (lambda () (if (< offset
0) (delete-char (abs offset
))
2888 (insert (make-string offset ?
*))))))))))
2892 ;;; Tools For Back-Ends
2894 ;; A whole set of tools is available to help build new exporters. Any
2895 ;; function general enough to have its use across many back-ends
2896 ;; should be added here.
2898 ;;;; For Affiliated Keywords
2900 ;; `org-export-read-attribute' reads a property from a given element
2901 ;; as a plist. It can be used to normalize affiliated keywords'
2904 ;; Since captions can span over multiple lines and accept dual values,
2905 ;; their internal representation is a bit tricky. Therefore,
2906 ;; `org-export-get-caption' transparently returns a given element's
2907 ;; caption as a secondary string.
2909 (defun org-export-read-attribute (attribute element
&optional property
)
2910 "Turn ATTRIBUTE property from ELEMENT into a plist.
2912 When optional argument PROPERTY is non-nil, return the value of
2913 that property within attributes.
2915 This function assumes attributes are defined as \":keyword
2916 value\" pairs. It is appropriate for `:attr_html' like
2919 (let ((value (org-element-property attribute element
)))
2921 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
2922 (if property
(plist-get attributes property
) attributes
)))
2924 (defun org-export-get-caption (element &optional shortp
)
2925 "Return caption from ELEMENT as a secondary string.
2927 When optional argument SHORTP is non-nil, return short caption,
2928 as a secondary string, instead.
2930 Caption lines are separated by a white space."
2931 (let ((full-caption (org-element-property :caption element
)) caption
)
2932 (dolist (line full-caption
(cdr caption
))
2933 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
2935 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
2938 ;;;; For Export Snippets
2940 ;; Every export snippet is transmitted to the back-end. Though, the
2941 ;; latter will only retain one type of export-snippet, ignoring
2942 ;; others, based on the former's target back-end. The function
2943 ;; `org-export-snippet-backend' returns that back-end for a given
2946 (defun org-export-snippet-backend (export-snippet)
2947 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2948 Translation, with `org-export-snippet-translation-alist', is
2950 (let ((back-end (org-element-property :back-end export-snippet
)))
2952 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
2958 ;; `org-export-collect-footnote-definitions' is a tool to list
2959 ;; actually used footnotes definitions in the whole parse tree, or in
2960 ;; an headline, in order to add footnote listings throughout the
2963 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2964 ;; back-ends, when they need to attach the footnote definition only to
2965 ;; the first occurrence of the corresponding label.
2967 ;; `org-export-get-footnote-definition' and
2968 ;; `org-export-get-footnote-number' provide easier access to
2969 ;; additional information relative to a footnote reference.
2971 (defun org-export-collect-footnote-definitions (data info
)
2972 "Return an alist between footnote numbers, labels and definitions.
2974 DATA is the parse tree from which definitions are collected.
2975 INFO is the plist used as a communication channel.
2977 Definitions are sorted by order of references. They either
2978 appear as Org data or as a secondary string for inlined
2979 footnotes. Unreferenced definitions are ignored."
2981 collect-fn
; for byte-compiler.
2985 ;; Collect footnote number, label and definition in DATA.
2987 data
'footnote-reference
2989 (when (org-export-footnote-first-reference-p fn info
)
2990 (let ((def (org-export-get-footnote-definition fn info
)))
2992 (list (org-export-get-footnote-number fn info
)
2993 (org-element-property :label fn
)
2996 ;; Also search in definition for nested footnotes.
2997 (when (eq (org-element-property :type fn
) 'standard
)
2998 (funcall collect-fn def
)))))
2999 ;; Don't enter footnote definitions since it will happen
3000 ;; when their first reference is found.
3001 info nil
'footnote-definition
)))))
3002 (funcall collect-fn
(plist-get info
:parse-tree
))
3003 (reverse num-alist
)))
3005 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3006 "Non-nil when a footnote reference is the first one for its label.
3008 FOOTNOTE-REFERENCE is the footnote reference being considered.
3009 INFO is the plist used as a communication channel."
3010 (let ((label (org-element-property :label footnote-reference
)))
3011 ;; Anonymous footnotes are always a first reference.
3013 ;; Otherwise, return the first footnote with the same LABEL and
3014 ;; test if it is equal to FOOTNOTE-REFERENCE.
3015 (let* (search-refs ; for byte-compiler.
3020 data
'footnote-reference
3023 ((string= (org-element-property :label fn
) label
)
3025 ;; If FN isn't inlined, be sure to traverse its
3026 ;; definition before resuming search. See
3027 ;; comments in `org-export-get-footnote-number'
3028 ;; for more information.
3029 ((eq (org-element-property :type fn
) 'standard
)
3030 (funcall search-refs
3031 (org-export-get-footnote-definition fn info
)))))
3032 ;; Don't enter footnote definitions since it will
3033 ;; happen when their first reference is found.
3034 info
'first-match
'footnote-definition
)))))
3035 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3036 footnote-reference
)))))
3038 (defun org-export-get-footnote-definition (footnote-reference info
)
3039 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3040 INFO is the plist used as a communication channel."
3041 (let ((label (org-element-property :label footnote-reference
)))
3042 (or (org-element-property :inline-definition footnote-reference
)
3043 (cdr (assoc label
(plist-get info
:footnote-definition-alist
))))))
3045 (defun org-export-get-footnote-number (footnote info
)
3046 "Return number associated to a footnote.
3048 FOOTNOTE is either a footnote reference or a footnote definition.
3049 INFO is the plist used as a communication channel."
3050 (let* ((label (org-element-property :label footnote
))
3052 search-ref
; For byte-compiler.
3056 ;; Search footnote references through DATA, filling
3057 ;; SEEN-REFS along the way.
3059 data
'footnote-reference
3061 (let ((fn-lbl (org-element-property :label fn
)))
3063 ;; Anonymous footnote match: return number.
3064 ((and (not fn-lbl
) (eq fn footnote
))
3065 (throw 'exit
(1+ (length seen-refs
))))
3066 ;; Labels match: return number.
3067 ((and label
(string= label fn-lbl
))
3068 (throw 'exit
(1+ (length seen-refs
))))
3069 ;; Anonymous footnote: it's always a new one. Also,
3070 ;; be sure to return nil from the `cond' so
3071 ;; `first-match' doesn't get us out of the loop.
3072 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3073 ;; Label not seen so far: add it so SEEN-REFS.
3075 ;; Also search for subsequent references in
3076 ;; footnote definition so numbering follows reading
3077 ;; logic. Note that we don't have to care about
3078 ;; inline definitions, since `org-element-map'
3079 ;; already traverses them at the right time.
3081 ;; Once again, return nil to stay in the loop.
3082 ((not (member fn-lbl seen-refs
))
3083 (push fn-lbl seen-refs
)
3085 (org-export-get-footnote-definition fn info
))
3087 ;; Don't enter footnote definitions since it will happen
3088 ;; when their first reference is found.
3089 info
'first-match
'footnote-definition
)))))
3090 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3095 ;; `org-export-get-relative-level' is a shortcut to get headline
3096 ;; level, relatively to the lower headline level in the parsed tree.
3098 ;; `org-export-get-headline-number' returns the section number of an
3099 ;; headline, while `org-export-number-to-roman' allows to convert it
3100 ;; to roman numbers.
3102 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3103 ;; `org-export-last-sibling-p' are three useful predicates when it
3104 ;; comes to fulfill the `:headline-levels' property.
3106 (defun org-export-get-relative-level (headline info
)
3107 "Return HEADLINE relative level within current parsed tree.
3108 INFO is a plist holding contextual information."
3109 (+ (org-element-property :level headline
)
3110 (or (plist-get info
:headline-offset
) 0)))
3112 (defun org-export-low-level-p (headline info
)
3113 "Non-nil when HEADLINE is considered as low level.
3115 INFO is a plist used as a communication channel.
3117 A low level headlines has a relative level greater than
3118 `:headline-levels' property value.
3120 Return value is the difference between HEADLINE relative level
3121 and the last level being considered as high enough, or nil."
3122 (let ((limit (plist-get info
:headline-levels
)))
3123 (when (wholenump limit
)
3124 (let ((level (org-export-get-relative-level headline info
)))
3125 (and (> level limit
) (- level limit
))))))
3127 (defun org-export-get-headline-number (headline info
)
3128 "Return HEADLINE numbering as a list of numbers.
3129 INFO is a plist holding contextual information."
3130 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3132 (defun org-export-numbered-headline-p (headline info
)
3133 "Return a non-nil value if HEADLINE element should be numbered.
3134 INFO is a plist used as a communication channel."
3135 (let ((sec-num (plist-get info
:section-numbers
))
3136 (level (org-export-get-relative-level headline info
)))
3137 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3139 (defun org-export-number-to-roman (n)
3140 "Convert integer N into a roman numeral."
3141 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3142 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3143 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3147 (number-to-string n
)
3149 (if (>= n
(caar roman
))
3150 (setq n
(- n
(caar roman
))
3151 res
(concat res
(cdar roman
)))
3155 (defun org-export-get-tags (element info
&optional tags
)
3156 "Return list of tags associated to ELEMENT.
3158 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3159 is a plist used as a communication channel.
3161 Select tags (see `org-export-select-tags') and exclude tags (see
3162 `org-export-exclude-tags') are removed from the list.
3164 When non-nil, optional argument TAGS should be a list of strings.
3165 Any tag belonging to this list will also be removed."
3166 (org-remove-if (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3167 (member tag
(plist-get info
:exclude-tags
))
3169 (org-element-property :tags element
)))
3171 (defun org-export-get-node-property (property blob
&optional inherited
)
3172 "Return node PROPERTY value for BLOB.
3174 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3177 If optional argument INHERITED is non-nil, the value can be
3178 inherited from a parent headline.
3180 Return value is a string or nil."
3181 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3182 (org-export-get-parent-headline blob
))))
3183 (if (not inherited
) (org-element-property property blob
)
3184 (let ((parent headline
) value
)
3187 (when (plist-member (nth 1 parent
) property
)
3188 (throw 'found
(org-element-property property parent
)))
3189 (setq parent
(org-element-property :parent parent
))))))))
3191 (defun org-export-first-sibling-p (headline info
)
3192 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3193 INFO is a plist used as a communication channel."
3194 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3197 (defun org-export-last-sibling-p (headline info
)
3198 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3199 INFO is a plist used as a communication channel."
3200 (not (org-export-get-next-element headline info
)))
3205 ;; `org-export-solidify-link-text' turns a string into a safer version
3206 ;; for links, replacing most non-standard characters with hyphens.
3208 ;; `org-export-get-coderef-format' returns an appropriate format
3209 ;; string for coderefs.
3211 ;; `org-export-inline-image-p' returns a non-nil value when the link
3212 ;; provided should be considered as an inline image.
3214 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3215 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3216 ;; returns an appropriate unique identifier when found, or nil.
3218 ;; `org-export-resolve-id-link' returns the first headline with
3219 ;; specified id or custom-id in parse tree, the path to the external
3220 ;; file with the id or nil when neither was found.
3222 ;; `org-export-resolve-coderef' associates a reference to a line
3223 ;; number in the element it belongs, or returns the reference itself
3224 ;; when the element isn't numbered.
3226 (defun org-export-solidify-link-text (s)
3227 "Take link text S and make a safe target out of it."
3229 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-]+") "-")))
3231 (defun org-export-get-coderef-format (path desc
)
3232 "Return format string for code reference link.
3233 PATH is the link path. DESC is its description."
3235 (cond ((not desc
) "%s")
3236 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3237 (replace-match "%s" t t desc
))
3240 (defun org-export-inline-image-p (link &optional rules
)
3241 "Non-nil if LINK object points to an inline image.
3243 Optional argument is a set of RULES defining inline images. It
3244 is an alist where associations have the following shape:
3248 Applying a rule means apply REGEXP against LINK's path when its
3249 type is TYPE. The function will return a non-nil value if any of
3250 the provided rules is non-nil. The default rule is
3251 `org-export-default-inline-image-rule'.
3253 This only applies to links without a description."
3254 (and (not (org-element-contents link
))
3255 (let ((case-fold-search t
)
3256 (rules (or rules org-export-default-inline-image-rule
)))
3260 (and (string= (org-element-property :type link
) (car rule
))
3261 (string-match (cdr rule
)
3262 (org-element-property :path link
))
3265 ;; Return nil if no rule matched.
3268 (defun org-export-resolve-coderef (ref info
)
3269 "Resolve a code reference REF.
3271 INFO is a plist used as a communication channel.
3273 Return associated line number in source code, or REF itself,
3274 depending on src-block or example element's switches."
3276 (plist-get info
:parse-tree
) '(example-block src-block
)
3279 (insert (org-trim (org-element-property :value el
)))
3280 (let* ((label-fmt (regexp-quote
3281 (or (org-element-property :label-fmt el
)
3282 org-coderef-label-format
)))
3284 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3285 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3286 ;; Element containing REF is found. Resolve it to either
3287 ;; a label or a line number, as needed.
3288 (when (re-search-backward ref-re nil t
)
3290 ((org-element-property :use-labels el
) ref
)
3291 ((eq (org-element-property :number-lines el
) 'continued
)
3292 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3293 (t (line-number-at-pos)))))))
3296 (defun org-export-resolve-fuzzy-link (link info
)
3297 "Return LINK destination.
3299 INFO is a plist holding contextual information.
3301 Return value can be an object, an element, or nil:
3303 - If LINK path matches a target object (i.e. <<path>>) or
3304 element (i.e. \"#+TARGET: path\"), return it.
3306 - If LINK path exactly matches the name affiliated keyword
3307 \(i.e. #+NAME: path) of an element, return that element.
3309 - If LINK path exactly matches any headline name, return that
3310 element. If more than one headline share that name, priority
3311 will be given to the one with the closest common ancestor, if
3312 any, or the first one in the parse tree otherwise.
3314 - Otherwise, return nil.
3316 Assume LINK type is \"fuzzy\"."
3317 (let* ((path (org-element-property :path link
))
3318 (match-title-p (eq (aref path
0) ?
*)))
3320 ;; First try to find a matching "<<path>>" unless user specified
3321 ;; he was looking for an headline (path starts with a *
3323 ((and (not match-title-p
)
3324 (loop for target in
(plist-get info
:target-list
)
3325 when
(string= (org-element-property :value target
) path
)
3327 ;; Then try to find an element with a matching "#+NAME: path"
3328 ;; affiliated keyword.
3329 ((and (not match-title-p
)
3331 (plist-get info
:parse-tree
) org-element-all-elements
3333 (when (string= (org-element-property :name el
) path
) el
))
3334 info
'first-match
)))
3335 ;; Last case: link either points to an headline or to
3336 ;; nothingness. Try to find the source, with priority given to
3337 ;; headlines with the closest common ancestor. If such candidate
3338 ;; is found, return it, otherwise return nil.
3340 (let ((find-headline
3342 ;; Return first headline whose `:raw-value' property
3343 ;; is NAME in parse tree DATA, or nil.
3349 (org-element-property :raw-value headline
)
3352 info
'first-match
)))))
3353 ;; Search among headlines sharing an ancestor with link,
3354 ;; from closest to farthest.
3358 (when (eq (org-element-type parent
) 'headline
)
3359 (let ((foundp (funcall find-headline path parent
)))
3360 (when foundp
(throw 'exit foundp
)))))
3361 (org-export-get-genealogy link
)) nil
)
3362 ;; No match with a common ancestor: try the full parse-tree.
3363 (funcall find-headline
3364 (if match-title-p
(substring path
1) path
)
3365 (plist-get info
:parse-tree
))))))))
3367 (defun org-export-resolve-id-link (link info
)
3368 "Return headline referenced as LINK destination.
3370 INFO is a plist used as a communication channel.
3372 Return value can be the headline element matched in current parse
3373 tree, a file name or nil. Assume LINK type is either \"id\" or
3375 (let ((id (org-element-property :path link
)))
3376 ;; First check if id is within the current parse tree.
3377 (or (org-element-map
3378 (plist-get info
:parse-tree
) 'headline
3380 (when (or (string= (org-element-property :id headline
) id
)
3381 (string= (org-element-property :custom-id headline
) id
))
3384 ;; Otherwise, look for external files.
3385 (cdr (assoc id
(plist-get info
:id-alist
))))))
3387 (defun org-export-resolve-radio-link (link info
)
3388 "Return radio-target object referenced as LINK destination.
3390 INFO is a plist used as a communication channel.
3392 Return value can be a radio-target object or nil. Assume LINK
3393 has type \"radio\"."
3394 (let ((path (org-element-property :path link
)))
3396 (plist-get info
:parse-tree
) 'radio-target
3398 (when (equal (org-element-property :value radio
) path
) radio
))
3399 info
'first-match
)))
3404 ;; `org-export-get-ordinal' associates a sequence number to any object
3407 (defun org-export-get-ordinal (element info
&optional types predicate
)
3408 "Return ordinal number of an element or object.
3410 ELEMENT is the element or object considered. INFO is the plist
3411 used as a communication channel.
3413 Optional argument TYPES, when non-nil, is a list of element or
3414 object types, as symbols, that should also be counted in.
3415 Otherwise, only provided element's type is considered.
3417 Optional argument PREDICATE is a function returning a non-nil
3418 value if the current element or object should be counted in. It
3419 accepts two arguments: the element or object being considered and
3420 the plist used as a communication channel. This allows to count
3421 only a certain type of objects (i.e. inline images).
3423 Return value is a list of numbers if ELEMENT is an headline or an
3424 item. It is nil for keywords. It represents the footnote number
3425 for footnote definitions and footnote references. If ELEMENT is
3426 a target, return the same value as if ELEMENT was the closest
3427 table, item or headline containing the target. In any other
3428 case, return the sequence number of ELEMENT among elements or
3429 objects of the same type."
3430 ;; A target keyword, representing an invisible target, never has
3431 ;; a sequence number.
3432 (unless (eq (org-element-type element
) 'keyword
)
3433 ;; Ordinal of a target object refer to the ordinal of the closest
3434 ;; table, item, or headline containing the object.
3435 (when (eq (org-element-type element
) 'target
)
3437 (loop for parent in
(org-export-get-genealogy element
)
3440 (org-element-type parent
)
3441 '(footnote-definition footnote-reference headline item
3444 (case (org-element-type element
)
3445 ;; Special case 1: An headline returns its number as a list.
3446 (headline (org-export-get-headline-number element info
))
3447 ;; Special case 2: An item returns its number as a list.
3448 (item (let ((struct (org-element-property :structure element
)))
3449 (org-list-get-item-number
3450 (org-element-property :begin element
)
3452 (org-list-prevs-alist struct
)
3453 (org-list-parents-alist struct
))))
3454 ((footnote-definition footnote-reference
)
3455 (org-export-get-footnote-number element info
))
3458 ;; Increment counter until ELEMENT is found again.
3460 (plist-get info
:parse-tree
) (or types
(org-element-type element
))
3463 ((eq element el
) (1+ counter
))
3464 ((not predicate
) (incf counter
) nil
)
3465 ((funcall predicate el info
) (incf counter
) nil
)))
3466 info
'first-match
))))))
3471 ;; `org-export-get-loc' counts number of code lines accumulated in
3472 ;; src-block or example-block elements with a "+n" switch until
3473 ;; a given element, excluded. Note: "-n" switches reset that count.
3475 ;; `org-export-unravel-code' extracts source code (along with a code
3476 ;; references alist) from an `element-block' or `src-block' type
3479 ;; `org-export-format-code' applies a formatting function to each line
3480 ;; of code, providing relative line number and code reference when
3481 ;; appropriate. Since it doesn't access the original element from
3482 ;; which the source code is coming, it expects from the code calling
3483 ;; it to know if lines should be numbered and if code references
3486 ;; Eventually, `org-export-format-code-default' is a higher-level
3487 ;; function (it makes use of the two previous functions) which handles
3488 ;; line numbering and code references inclusion, and returns source
3489 ;; code in a format suitable for plain text or verbatim output.
3491 (defun org-export-get-loc (element info
)
3492 "Return accumulated lines of code up to ELEMENT.
3494 INFO is the plist used as a communication channel.
3496 ELEMENT is excluded from count."
3499 (plist-get info
:parse-tree
)
3500 `(src-block example-block
,(org-element-type element
))
3503 ;; ELEMENT is reached: Quit the loop.
3505 ;; Only count lines from src-block and example-block elements
3506 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3507 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3508 ((let ((linums (org-element-property :number-lines el
)))
3510 ;; Accumulate locs or reset them.
3511 (let ((lines (org-count-lines
3512 (org-trim (org-element-property :value el
)))))
3513 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3514 ;; Return nil to stay in the loop.
3520 (defun org-export-unravel-code (element)
3521 "Clean source code and extract references out of it.
3523 ELEMENT has either a `src-block' an `example-block' type.
3525 Return a cons cell whose CAR is the source code, cleaned from any
3526 reference and protective comma and CDR is an alist between
3527 relative line number (integer) and name of code reference on that
3529 (let* ((line 0) refs
3530 ;; Get code and clean it. Remove blank lines at its
3531 ;; beginning and end.
3532 (code (let ((c (replace-regexp-in-string
3533 "\\`\\([ \t]*\n\\)+" ""
3534 (replace-regexp-in-string
3535 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3536 (org-element-property :value element
)))))
3537 ;; If appropriate, remove global indentation.
3538 (if (or org-src-preserve-indentation
3539 (org-element-property :preserve-indent element
))
3541 (org-remove-indentation c
))))
3542 ;; Get format used for references.
3543 (label-fmt (regexp-quote
3544 (or (org-element-property :label-fmt element
)
3545 org-coderef-label-format
)))
3546 ;; Build a regexp matching a loc with a reference.
3548 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3549 (replace-regexp-in-string
3550 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3553 ;; Code with references removed.
3554 (org-element-normalize-string
3558 (if (not (string-match with-ref-re loc
)) loc
3559 ;; Ref line: remove ref, and signal its position in REFS.
3560 (push (cons line
(match-string 3 loc
)) refs
)
3561 (replace-match "" nil nil loc
1)))
3562 (org-split-string code
"\n") "\n"))
3566 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3567 "Format CODE by applying FUN line-wise and return it.
3569 CODE is a string representing the code to format. FUN is
3570 a function. It must accept three arguments: a line of
3571 code (string), the current line number (integer) or nil and the
3572 reference associated to the current line (string) or nil.
3574 Optional argument NUM-LINES can be an integer representing the
3575 number of code lines accumulated until the current code. Line
3576 numbers passed to FUN will take it into account. If it is nil,
3577 FUN's second argument will always be nil. This number can be
3578 obtained with `org-export-get-loc' function.
3580 Optional argument REF-ALIST can be an alist between relative line
3581 number (i.e. ignoring NUM-LINES) and the name of the code
3582 reference on it. If it is nil, FUN's third argument will always
3583 be nil. It can be obtained through the use of
3584 `org-export-unravel-code' function."
3585 (let ((--locs (org-split-string code
"\n"))
3587 (org-element-normalize-string
3591 (let ((--ref (cdr (assq --line ref-alist
))))
3592 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3595 (defun org-export-format-code-default (element info
)
3596 "Return source code from ELEMENT, formatted in a standard way.
3598 ELEMENT is either a `src-block' or `example-block' element. INFO
3599 is a plist used as a communication channel.
3601 This function takes care of line numbering and code references
3602 inclusion. Line numbers, when applicable, appear at the
3603 beginning of the line, separated from the code by two white
3604 spaces. Code references, on the other hand, appear flushed to
3605 the right, separated by six white spaces from the widest line of
3607 ;; Extract code and references.
3608 (let* ((code-info (org-export-unravel-code element
))
3609 (code (car code-info
))
3610 (code-lines (org-split-string code
"\n"))
3611 (refs (and (org-element-property :retain-labels element
)
3613 ;; Handle line numbering.
3614 (num-start (case (org-element-property :number-lines element
)
3615 (continued (org-export-get-loc element info
))
3620 (length (number-to-string
3621 (+ (length code-lines
) num-start
))))))
3622 ;; Prepare references display, if required. Any reference
3623 ;; should start six columns after the widest line of code,
3624 ;; wrapped with parenthesis.
3626 (+ (apply 'max
(mapcar 'length code-lines
))
3627 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3628 (org-export-format-code
3630 (lambda (loc line-num ref
)
3631 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3636 (concat (make-string
3638 (+ (length loc
) (length number-str
))) ?
)
3639 (format "(%s)" ref
))))))
3645 ;; `org-export-table-has-special-column-p' and and
3646 ;; `org-export-table-row-is-special-p' are predicates used to look for
3647 ;; meta-information about the table structure.
3649 ;; `org-table-has-header-p' tells when the rows before the first rule
3650 ;; should be considered as table's header.
3652 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3653 ;; and `org-export-table-cell-borders' extract information from
3654 ;; a table-cell element.
3656 ;; `org-export-table-dimensions' gives the number on rows and columns
3657 ;; in the table, ignoring horizontal rules and special columns.
3658 ;; `org-export-table-cell-address', given a table-cell object, returns
3659 ;; the absolute address of a cell. On the other hand,
3660 ;; `org-export-get-table-cell-at' does the contrary.
3662 ;; `org-export-table-cell-starts-colgroup-p',
3663 ;; `org-export-table-cell-ends-colgroup-p',
3664 ;; `org-export-table-row-starts-rowgroup-p',
3665 ;; `org-export-table-row-ends-rowgroup-p',
3666 ;; `org-export-table-row-starts-header-p' and
3667 ;; `org-export-table-row-ends-header-p' indicate position of current
3668 ;; row or cell within the table.
3670 (defun org-export-table-has-special-column-p (table)
3671 "Non-nil when TABLE has a special column.
3672 All special columns will be ignored during export."
3673 ;; The table has a special column when every first cell of every row
3674 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3675 ;; "*" "_" and "^". Though, do not consider a first row containing
3676 ;; only empty cells as special.
3677 (let ((special-column-p 'empty
))
3681 (when (eq (org-element-property :type row
) 'standard
)
3682 (let ((value (org-element-contents
3683 (car (org-element-contents row
)))))
3684 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3685 (setq special-column-p
'special
))
3687 (t (throw 'exit nil
))))))
3688 (org-element-contents table
))
3689 (eq special-column-p
'special
))))
3691 (defun org-export-table-has-header-p (table info
)
3692 "Non-nil when TABLE has an header.
3694 INFO is a plist used as a communication channel.
3696 A table has an header when it contains at least two row groups."
3697 (let ((rowgroup 1) row-flag
)
3703 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
3704 (incf rowgroup
) (setq row-flag nil
))
3705 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
3706 (setq row-flag t
) nil
)))
3709 (defun org-export-table-row-is-special-p (table-row info
)
3710 "Non-nil if TABLE-ROW is considered special.
3712 INFO is a plist used as the communication channel.
3714 All special rows will be ignored during export."
3715 (when (eq (org-element-property :type table-row
) 'standard
)
3716 (let ((first-cell (org-element-contents
3717 (car (org-element-contents table-row
)))))
3718 ;; A row is special either when...
3720 ;; ... it starts with a field only containing "/",
3721 (equal first-cell
'("/"))
3722 ;; ... the table contains a special column and the row start
3723 ;; with a marking character among, "^", "_", "$" or "!",
3724 (and (org-export-table-has-special-column-p
3725 (org-export-get-parent table-row
))
3726 (member first-cell
'(("^") ("_") ("$") ("!"))))
3727 ;; ... it contains only alignment cookies and empty cells.
3728 (let ((special-row-p 'empty
))
3732 (let ((value (org-element-contents cell
)))
3733 ;; Since VALUE is a secondary string, the following
3734 ;; checks avoid expanding it with `org-export-data'.
3736 ((and (not (cdr value
))
3737 (stringp (car value
))
3738 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3740 (setq special-row-p
'cookie
))
3741 (t (throw 'exit nil
)))))
3742 (org-element-contents table-row
))
3743 (eq special-row-p
'cookie
)))))))
3745 (defun org-export-table-row-group (table-row info
)
3746 "Return TABLE-ROW's group.
3748 INFO is a plist used as the communication channel.
3750 Return value is the group number, as an integer, or nil special
3751 rows and table rules. Group 1 is also table's header."
3752 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3753 (org-export-table-row-is-special-p table-row info
))
3754 (let ((group 0) row-flag
)
3759 ((and (eq (org-element-property :type row
) 'standard
)
3760 (not (org-export-table-row-is-special-p row info
)))
3761 (unless row-flag
(incf group
) (setq row-flag t
)))
3762 ((eq (org-element-property :type row
) 'rule
)
3763 (setq row-flag nil
)))
3764 (when (eq table-row row
) (throw 'found group
)))
3765 (org-element-contents (org-export-get-parent table-row
)))))))
3767 (defun org-export-table-cell-width (table-cell info
)
3768 "Return TABLE-CELL contents width.
3770 INFO is a plist used as the communication channel.
3772 Return value is the width given by the last width cookie in the
3773 same column as TABLE-CELL, or nil."
3774 (let* ((row (org-export-get-parent table-cell
))
3775 (column (let ((cells (org-element-contents row
)))
3776 (- (length cells
) (length (memq table-cell cells
)))))
3777 (table (org-export-get-parent-table table-cell
))
3782 ;; In a special row, try to find a width cookie at COLUMN.
3783 ((org-export-table-row-is-special-p row info
)
3784 (let ((value (org-element-contents
3785 (elt (org-element-contents row
) column
))))
3786 ;; The following checks avoid expanding unnecessarily the
3787 ;; cell with `org-export-data'
3790 (stringp (car value
))
3791 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
3792 (match-string 1 (car value
)))
3794 (string-to-number (match-string 1 (car value
)))))))
3795 ;; Ignore table rules.
3796 ((eq (org-element-property :type row
) 'rule
))))
3797 (org-element-contents table
))
3801 (defun org-export-table-cell-alignment (table-cell info
)
3802 "Return TABLE-CELL contents alignment.
3804 INFO is a plist used as the communication channel.
3806 Return alignment as specified by the last alignment cookie in the
3807 same column as TABLE-CELL. If no such cookie is found, a default
3808 alignment value will be deduced from fraction of numbers in the
3809 column (see `org-table-number-fraction' for more information).
3810 Possible values are `left', `right' and `center'."
3811 (let* ((row (org-export-get-parent table-cell
))
3812 (column (let ((cells (org-element-contents row
)))
3813 (- (length cells
) (length (memq table-cell cells
)))))
3814 (table (org-export-get-parent-table table-cell
))
3821 ;; In a special row, try to find an alignment cookie at
3823 ((org-export-table-row-is-special-p row info
)
3824 (let ((value (org-element-contents
3825 (elt (org-element-contents row
) column
))))
3826 ;; Since VALUE is a secondary string, the following checks
3827 ;; avoid useless expansion through `org-export-data'.
3830 (stringp (car value
))
3831 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3833 (match-string 1 (car value
)))
3834 (setq cookie-align
(match-string 1 (car value
))))))
3835 ;; Ignore table rules.
3836 ((eq (org-element-property :type row
) 'rule
))
3837 ;; In a standard row, check if cell's contents are expressing
3838 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3839 ;; Though, don't bother if an alignment cookie has already
3840 ;; defined cell's alignment.
3842 (let ((value (org-export-data
3843 (org-element-contents
3844 (elt (org-element-contents row
) column
))
3847 (when (string-match org-table-number-regexp value
)
3848 (incf number-cells
))))))
3849 (org-element-contents table
))
3850 ;; Return value. Alignment specified by cookies has precedence
3851 ;; over alignment deduced from cells contents.
3852 (cond ((equal cookie-align
"l") 'left
)
3853 ((equal cookie-align
"r") 'right
)
3854 ((equal cookie-align
"c") 'center
)
3855 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
3859 (defun org-export-table-cell-borders (table-cell info
)
3860 "Return TABLE-CELL borders.
3862 INFO is a plist used as a communication channel.
3864 Return value is a list of symbols, or nil. Possible values are:
3865 `top', `bottom', `above', `below', `left' and `right'. Note:
3866 `top' (resp. `bottom') only happen for a cell in the first
3867 row (resp. last row) of the table, ignoring table rules, if any.
3869 Returned borders ignore special rows."
3870 (let* ((row (org-export-get-parent table-cell
))
3871 (table (org-export-get-parent-table table-cell
))
3873 ;; Top/above border? TABLE-CELL has a border above when a rule
3874 ;; used to demarcate row groups can be found above. Hence,
3875 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3876 ;; another regular row has to be found above that rule.
3880 (cond ((eq (org-element-property :type row
) 'rule
)
3882 ((not (org-export-table-row-is-special-p row info
))
3883 (if rule-flag
(throw 'exit
(push 'above borders
))
3884 (throw 'exit nil
)))))
3885 ;; Look at every row before the current one.
3886 (cdr (memq row
(reverse (org-element-contents table
)))))
3887 ;; No rule above, or rule found starts the table (ignoring any
3888 ;; special row): TABLE-CELL is at the top of the table.
3889 (when rule-flag
(push 'above borders
))
3890 (push 'top borders
)))
3891 ;; Bottom/below border? TABLE-CELL has a border below when next
3892 ;; non-regular row below is a rule.
3896 (cond ((eq (org-element-property :type row
) 'rule
)
3898 ((not (org-export-table-row-is-special-p row info
))
3899 (if rule-flag
(throw 'exit
(push 'below borders
))
3900 (throw 'exit nil
)))))
3901 ;; Look at every row after the current one.
3902 (cdr (memq row
(org-element-contents table
))))
3903 ;; No rule below, or rule found ends the table (modulo some
3904 ;; special row): TABLE-CELL is at the bottom of the table.
3905 (when rule-flag
(push 'below borders
))
3906 (push 'bottom borders
)))
3907 ;; Right/left borders? They can only be specified by column
3908 ;; groups. Column groups are defined in a row starting with "/".
3909 ;; Also a column groups row only contains "<", "<>", ">" or blank
3912 (let ((column (let ((cells (org-element-contents row
)))
3913 (- (length cells
) (length (memq table-cell cells
))))))
3916 (unless (eq (org-element-property :type row
) 'rule
)
3917 (when (equal (org-element-contents
3918 (car (org-element-contents row
)))
3920 (let ((column-groups
3923 (let ((value (org-element-contents cell
)))
3924 (when (member value
'(("<") ("<>") (">") nil
))
3926 (org-element-contents row
))))
3927 ;; There's a left border when previous cell, if
3928 ;; any, ends a group, or current one starts one.
3929 (when (or (and (not (zerop column
))
3930 (member (elt column-groups
(1- column
))
3932 (member (elt column-groups column
) '("<" "<>")))
3933 (push 'left borders
))
3934 ;; There's a right border when next cell, if any,
3935 ;; starts a group, or current one ends one.
3936 (when (or (and (/= (1+ column
) (length column-groups
))
3937 (member (elt column-groups
(1+ column
))
3939 (member (elt column-groups column
) '(">" "<>")))
3940 (push 'right borders
))
3941 (throw 'exit nil
)))))
3942 ;; Table rows are read in reverse order so last column groups
3943 ;; row has precedence over any previous one.
3944 (reverse (org-element-contents table
)))))
3948 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
3949 "Non-nil when TABLE-CELL is at the beginning of a row group.
3950 INFO is a plist used as a communication channel."
3951 ;; A cell starts a column group either when it is at the beginning
3952 ;; of a row (or after the special column, if any) or when it has
3954 (or (eq (org-element-map
3955 (org-export-get-parent table-cell
)
3956 'table-cell
'identity info
'first-match
)
3958 (memq 'left
(org-export-table-cell-borders table-cell info
))))
3960 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
3961 "Non-nil when TABLE-CELL is at the end of a row group.
3962 INFO is a plist used as a communication channel."
3963 ;; A cell ends a column group either when it is at the end of a row
3964 ;; or when it has a right border.
3965 (or (eq (car (last (org-element-contents
3966 (org-export-get-parent table-cell
))))
3968 (memq 'right
(org-export-table-cell-borders table-cell info
))))
3970 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
3971 "Non-nil when TABLE-ROW is at the beginning of a column group.
3972 INFO is a plist used as a communication channel."
3973 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3974 (org-export-table-row-is-special-p table-row info
))
3975 (let ((borders (org-export-table-cell-borders
3976 (car (org-element-contents table-row
)) info
)))
3977 (or (memq 'top borders
) (memq 'above borders
)))))
3979 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
3980 "Non-nil when TABLE-ROW is at the end of a column group.
3981 INFO is a plist used as a communication channel."
3982 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3983 (org-export-table-row-is-special-p table-row info
))
3984 (let ((borders (org-export-table-cell-borders
3985 (car (org-element-contents table-row
)) info
)))
3986 (or (memq 'bottom borders
) (memq 'below borders
)))))
3988 (defun org-export-table-row-starts-header-p (table-row info
)
3989 "Non-nil when TABLE-ROW is the first table header's row.
3990 INFO is a plist used as a communication channel."
3991 (and (org-export-table-has-header-p
3992 (org-export-get-parent-table table-row
) info
)
3993 (org-export-table-row-starts-rowgroup-p table-row info
)
3994 (= (org-export-table-row-group table-row info
) 1)))
3996 (defun org-export-table-row-ends-header-p (table-row info
)
3997 "Non-nil when TABLE-ROW is the last table header's row.
3998 INFO is a plist used as a communication channel."
3999 (and (org-export-table-has-header-p
4000 (org-export-get-parent-table table-row
) info
)
4001 (org-export-table-row-ends-rowgroup-p table-row info
)
4002 (= (org-export-table-row-group table-row info
) 1)))
4004 (defun org-export-table-dimensions (table info
)
4005 "Return TABLE dimensions.
4007 INFO is a plist used as a communication channel.
4009 Return value is a CONS like (ROWS . COLUMNS) where
4010 ROWS (resp. COLUMNS) is the number of exportable
4011 rows (resp. columns)."
4012 (let (first-row (columns 0) (rows 0))
4013 ;; Set number of rows, and extract first one.
4017 (when (eq (org-element-property :type row
) 'standard
)
4019 (unless first-row
(setq first-row row
)))) info
)
4020 ;; Set number of columns.
4021 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4023 (cons rows columns
)))
4025 (defun org-export-table-cell-address (table-cell info
)
4026 "Return address of a regular TABLE-CELL object.
4028 TABLE-CELL is the cell considered. INFO is a plist used as
4029 a communication channel.
4031 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4032 zero-based index. Only exportable cells are considered. The
4033 function returns nil for other cells."
4034 (let* ((table-row (org-export-get-parent table-cell
))
4035 (table (org-export-get-parent-table table-cell
)))
4036 ;; Ignore cells in special rows or in special column.
4037 (unless (or (org-export-table-row-is-special-p table-row info
)
4038 (and (org-export-table-has-special-column-p table
)
4039 (eq (car (org-element-contents table-row
)) table-cell
)))
4042 (let ((row-count 0))
4046 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4047 ((eq row table-row
) row-count
)
4048 (t (incf row-count
) nil
)))
4051 (let ((col-count 0))
4053 table-row
'table-cell
4055 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4056 info
'first-match
))))))
4058 (defun org-export-get-table-cell-at (address table info
)
4059 "Return regular table-cell object at ADDRESS in TABLE.
4061 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4062 zero-based index. TABLE is a table type element. INFO is
4063 a plist used as a communication channel.
4065 If no table-cell, among exportable cells, is found at ADDRESS,
4067 (let ((column-pos (cdr address
)) (column-count 0))
4069 ;; Row at (car address) or nil.
4070 (let ((row-pos (car address
)) (row-count 0))
4074 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4075 ((= row-count row-pos
) row
)
4076 (t (incf row-count
) nil
)))
4080 (if (= column-count column-pos
) cell
4081 (incf column-count
) nil
))
4082 info
'first-match
)))
4085 ;;;; For Tables Of Contents
4087 ;; `org-export-collect-headlines' builds a list of all exportable
4088 ;; headline elements, maybe limited to a certain depth. One can then
4089 ;; easily parse it and transcode it.
4091 ;; Building lists of tables, figures or listings is quite similar.
4092 ;; Once the generic function `org-export-collect-elements' is defined,
4093 ;; `org-export-collect-tables', `org-export-collect-figures' and
4094 ;; `org-export-collect-listings' can be derived from it.
4096 (defun org-export-collect-headlines (info &optional n
)
4097 "Collect headlines in order to build a table of contents.
4099 INFO is a plist used as a communication channel.
4101 When optional argument N is an integer, it specifies the depth of
4102 the table of contents. Otherwise, it is set to the value of the
4103 last headline level. See `org-export-headline-levels' for more
4106 Return a list of all exportable headlines as parsed elements."
4107 (unless (wholenump n
) (setq n
(plist-get info
:headline-levels
)))
4109 (plist-get info
:parse-tree
)
4112 ;; Strip contents from HEADLINE.
4113 (let ((relative-level (org-export-get-relative-level headline info
)))
4114 (unless (> relative-level n
) headline
)))
4117 (defun org-export-collect-elements (type info
&optional predicate
)
4118 "Collect referenceable elements of a determined type.
4120 TYPE can be a symbol or a list of symbols specifying element
4121 types to search. Only elements with a caption are collected.
4123 INFO is a plist used as a communication channel.
4125 When non-nil, optional argument PREDICATE is a function accepting
4126 one argument, an element of type TYPE. It returns a non-nil
4127 value when that element should be collected.
4129 Return a list of all elements found, in order of appearance."
4131 (plist-get info
:parse-tree
) type
4133 (and (org-element-property :caption element
)
4134 (or (not predicate
) (funcall predicate element
))
4138 (defun org-export-collect-tables (info)
4139 "Build a list of tables.
4140 INFO is a plist used as a communication channel.
4142 Return a list of table elements with a caption."
4143 (org-export-collect-elements 'table info
))
4145 (defun org-export-collect-figures (info predicate
)
4146 "Build a list of figures.
4148 INFO is a plist used as a communication channel. PREDICATE is
4149 a function which accepts one argument: a paragraph element and
4150 whose return value is non-nil when that element should be
4153 A figure is a paragraph type element, with a caption, verifying
4154 PREDICATE. The latter has to be provided since a \"figure\" is
4155 a vague concept that may depend on back-end.
4157 Return a list of elements recognized as figures."
4158 (org-export-collect-elements 'paragraph info predicate
))
4160 (defun org-export-collect-listings (info)
4161 "Build a list of src blocks.
4163 INFO is a plist used as a communication channel.
4165 Return a list of src-block elements with a caption."
4166 (org-export-collect-elements 'src-block info
))
4171 ;; Here are various functions to retrieve information about the
4172 ;; neighbourhood of a given element or object. Neighbours of interest
4173 ;; are direct parent (`org-export-get-parent'), parent headline
4174 ;; (`org-export-get-parent-headline'), first element containing an
4175 ;; object, (`org-export-get-parent-element'), parent table
4176 ;; (`org-export-get-parent-table'), previous element or object
4177 ;; (`org-export-get-previous-element') and next element or object
4178 ;; (`org-export-get-next-element').
4180 ;; `org-export-get-genealogy' returns the full genealogy of a given
4181 ;; element or object, from closest parent to full parse tree.
4183 (defun org-export-get-parent (blob)
4184 "Return BLOB parent or nil.
4185 BLOB is the element or object considered."
4186 (org-element-property :parent blob
))
4188 (defun org-export-get-genealogy (blob)
4189 "Return full genealogy relative to a given element or object.
4191 BLOB is the element or object being considered.
4193 Ancestors are returned from closest to farthest, the last one
4194 being the full parse tree."
4195 (let (genealogy (parent blob
))
4196 (while (setq parent
(org-element-property :parent parent
))
4197 (push parent genealogy
))
4198 (nreverse genealogy
)))
4200 (defun org-export-get-parent-headline (blob)
4201 "Return BLOB parent headline or nil.
4202 BLOB is the element or object being considered."
4203 (let ((parent blob
))
4204 (while (and (setq parent
(org-element-property :parent parent
))
4205 (not (eq (org-element-type parent
) 'headline
))))
4208 (defun org-export-get-parent-element (object)
4209 "Return first element containing OBJECT or nil.
4210 OBJECT is the object to consider."
4211 (let ((parent object
))
4212 (while (and (setq parent
(org-element-property :parent parent
))
4213 (memq (org-element-type parent
) org-element-all-objects
)))
4216 (defun org-export-get-parent-table (object)
4217 "Return OBJECT parent table or nil.
4218 OBJECT is either a `table-cell' or `table-element' type object."
4219 (let ((parent object
))
4220 (while (and (setq parent
(org-element-property :parent parent
))
4221 (not (eq (org-element-type parent
) 'table
))))
4224 (defun org-export-get-previous-element (blob info
)
4225 "Return previous element or object.
4226 BLOB is an element or object. INFO is a plist used as
4227 a communication channel. Return previous exportable element or
4228 object, a string, or nil."
4232 (cond ((eq obj blob
) (throw 'exit prev
))
4233 ((memq obj
(plist-get info
:ignore-list
)))
4234 (t (setq prev obj
))))
4235 (org-element-contents (org-export-get-parent blob
))))))
4237 (defun org-export-get-next-element (blob info
)
4238 "Return next element or object.
4239 BLOB is an element or object. INFO is a plist used as
4240 a communication channel. Return next exportable element or
4241 object, a string, or nil."
4244 (unless (memq obj
(plist-get info
:ignore-list
))
4245 (throw 'found obj
)))
4246 (cdr (memq blob
(org-element-contents (org-export-get-parent blob
)))))
4252 ;; `org-export-translate' translates a string according to language
4253 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4254 ;; variable and a specified charset. `org-export-dictionary' contains
4255 ;; the dictionary used for the translation.
4257 (defconst org-export-dictionary
4259 ("ca" :default
"Autor")
4260 ("cs" :default
"Autor")
4261 ("da" :default
"Ophavsmand")
4262 ("de" :default
"Autor")
4263 ("eo" :html
"Aŭtoro")
4264 ("es" :default
"Autor")
4265 ("fi" :html
"Tekijä")
4266 ("fr" :default
"Auteur")
4267 ("hu" :default
"Szerzõ")
4268 ("is" :html
"Höfundur")
4269 ("it" :default
"Autore")
4270 ("ja" :html
"著者" :utf-8
"著者")
4271 ("nl" :default
"Auteur")
4272 ("no" :default
"Forfatter")
4273 ("nb" :default
"Forfatter")
4274 ("nn" :default
"Forfattar")
4275 ("pl" :default
"Autor")
4276 ("ru" :html
"Автор" :utf-8
"Автор")
4277 ("sv" :html
"Författare")
4278 ("uk" :html
"Автор" :utf-8
"Автор")
4279 ("zh-CN" :html
"作者" :utf-8
"作者")
4280 ("zh-TW" :html
"作者" :utf-8
"作者"))
4282 ("ca" :default
"Data")
4283 ("cs" :default
"Datum")
4284 ("da" :default
"Dato")
4285 ("de" :default
"Datum")
4286 ("eo" :default
"Dato")
4287 ("es" :default
"Fecha")
4288 ("fi" :html
"Päivämäärä")
4289 ("hu" :html
"Dátum")
4290 ("is" :default
"Dagsetning")
4291 ("it" :default
"Data")
4292 ("ja" :html
"日付" :utf-8
"日付")
4293 ("nl" :default
"Datum")
4294 ("no" :default
"Dato")
4295 ("nb" :default
"Dato")
4296 ("nn" :default
"Dato")
4297 ("pl" :default
"Data")
4298 ("ru" :html
"Дата" :utf-8
"Дата")
4299 ("sv" :default
"Datum")
4300 ("uk" :html
"Дата" :utf-8
"Дата")
4301 ("zh-CN" :html
"日期" :utf-8
"日期")
4302 ("zh-TW" :html
"日期" :utf-8
"日期"))
4304 ("fr" :ascii
"Equation" :default
"Équation"))
4307 ("ca" :html
"Peus de pàgina")
4308 ("cs" :default
"Pozn\xe1mky pod carou")
4309 ("da" :default
"Fodnoter")
4310 ("de" :html
"Fußnoten")
4311 ("eo" :default
"Piednotoj")
4312 ("es" :html
"Pies de página")
4313 ("fi" :default
"Alaviitteet")
4314 ("fr" :default
"Notes de bas de page")
4315 ("hu" :html
"Lábjegyzet")
4316 ("is" :html
"Aftanmálsgreinar")
4317 ("it" :html
"Note a piè di pagina")
4318 ("ja" :html
"脚注" :utf-8
"脚注")
4319 ("nl" :default
"Voetnoten")
4320 ("no" :default
"Fotnoter")
4321 ("nb" :default
"Fotnoter")
4322 ("nn" :default
"Fotnotar")
4323 ("pl" :default
"Przypis")
4324 ("ru" :html
"Сноски" :utf-8
"Сноски")
4325 ("sv" :default
"Fotnoter")
4326 ("uk" :html
"Примітки"
4328 ("zh-CN" :html
"脚注" :utf-8
"脚注")
4329 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
4331 ("fr" :default
"Liste des programmes"))
4333 ("fr" :default
"Liste des tableaux"))
4336 :ascii
"Programme %d :" :default
"Programme nº %d :"
4337 :latin1
"Programme %d :"))
4340 :ascii
"Programme %d : %s" :default
"Programme nº %d : %s"
4341 :latin1
"Programme %d : %s"))
4343 ("fr" :default
"cf. section %s"))
4346 :ascii
"Tableau %d :" :default
"Tableau nº %d :" :latin1
"Tableau %d :"))
4349 :ascii
"Tableau %d : %s" :default
"Tableau nº %d : %s"
4350 :latin1
"Tableau %d : %s"))
4351 ("Table of Contents"
4352 ("ca" :html
"Índex")
4353 ("cs" :default
"Obsah")
4354 ("da" :default
"Indhold")
4355 ("de" :default
"Inhaltsverzeichnis")
4356 ("eo" :default
"Enhavo")
4357 ("es" :html
"Índice")
4358 ("fi" :html
"Sisällysluettelo")
4359 ("fr" :ascii
"Sommaire" :default
"Table des matières")
4360 ("hu" :html
"Tartalomjegyzék")
4361 ("is" :default
"Efnisyfirlit")
4362 ("it" :default
"Indice")
4363 ("ja" :html
"目次" :utf-8
"目次")
4364 ("nl" :default
"Inhoudsopgave")
4365 ("no" :default
"Innhold")
4366 ("nb" :default
"Innhold")
4367 ("nn" :default
"Innhald")
4368 ("pl" :html
"Spis treści")
4369 ("ru" :html
"Содержание"
4370 :utf-8
"Содержание")
4371 ("sv" :html
"Innehåll")
4372 ("uk" :html
"Зміст" :utf-8
"Зміст")
4373 ("zh-CN" :html
"目录" :utf-8
"目录")
4374 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
4375 ("Unknown reference"
4376 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")))
4377 "Dictionary for export engine.
4379 Alist whose CAR is the string to translate and CDR is an alist
4380 whose CAR is the language string and CDR is a plist whose
4381 properties are possible charsets and values translated terms.
4383 It is used as a database for `org-export-translate'. Since this
4384 function returns the string as-is if no translation was found,
4385 the variable only needs to record values different from the
4388 (defun org-export-translate (s encoding info
)
4389 "Translate string S according to language specification.
4391 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4392 and `:utf-8'. INFO is a plist used as a communication channel.
4394 Translation depends on `:language' property. Return the
4395 translated string. If no translation is found, try to fall back
4396 to `:default' encoding. If it fails, return S."
4397 (let* ((lang (plist-get info
:language
))
4398 (translations (cdr (assoc lang
4399 (cdr (assoc s org-export-dictionary
))))))
4400 (or (plist-get translations encoding
)
4401 (plist-get translations
:default
)
4408 ;; `org-export-dispatch' is the standard interactive way to start an
4409 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4410 ;; for its interface, which, in turn, delegates response to key
4411 ;; pressed to `org-export-dispatch-action'.
4413 (defvar org-export-dispatch-menu-entries nil
4414 "List of menu entries available for `org-export-dispatch'.
4415 This variable shouldn't be set directly. Set-up :menu-entry
4416 keyword in either `org-export-define-backend' or
4417 `org-export-define-derived-backend' instead.")
4420 (defun org-export-dispatch ()
4421 "Export dispatcher for Org mode.
4423 It provides an access to common export related tasks in a buffer.
4424 Its interface comes in two flavours: standard and expert. While
4425 both share the same set of bindings, only the former displays the
4426 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4427 to switch to one or the other.
4429 Return an error if key pressed has no associated command."
4431 (let* ((input (org-export-dispatch-ui (list org-export-initial-scope
)
4433 org-export-dispatch-use-expert-ui
))
4434 (action (car input
))
4435 (optns (cdr input
)))
4437 ;; First handle special hard-coded actions.
4438 (publish-current-file (org-e-publish-current-file (memq 'force optns
)))
4439 (publish-current-project
4440 (org-e-publish-current-project (memq 'force optns
)))
4441 (publish-choose-project
4442 (org-e-publish (assoc (org-icompleting-read
4444 org-e-publish-project-alist nil t
)
4445 org-e-publish-project-alist
)
4446 (memq 'force optns
)))
4447 (publish-all (org-e-publish-all (memq 'force optns
)))
4450 (memq 'subtree optns
)
4451 (memq 'visible optns
)
4452 (memq 'body optns
))))))
4454 (defun org-export-dispatch-ui (options first-key expertp
)
4455 "Handle interface for `org-export-dispatch'.
4457 OPTIONS is a list containing current interactive options set for
4458 export. It can contain any of the following symbols:
4459 `body' toggles a body-only export
4460 `subtree' restricts export to current subtree
4461 `visible' restricts export to visible part of buffer.
4462 `force' force publishing files.
4464 FIRST-KEY is the key pressed to select the first level menu. It
4465 is nil when this menu hasn't been selected yet.
4467 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4468 buffer is provided, but indications about currently active
4469 options are given in the prompt. Moreover, \[?] allows to switch
4470 back to standard interface."
4472 (lambda (key &optional access-key
)
4473 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4474 ;; non-nil is the required first-level key to activate
4475 ;; KEY. When its value is t, activate KEY independently
4476 ;; on the first key, if any. A nil value means KEY will
4477 ;; only be activated at first level.
4478 (if (or (eq access-key t
) (eq access-key first-key
))
4479 (org-add-props key nil
'face
'org-warning
)
4480 (org-no-properties key
))))
4481 ;; Make sure order of menu doesn't depend on the order in
4482 ;; which back-ends are loaded.
4483 (backends (sort (copy-sequence org-export-dispatch-menu-entries
)
4484 (lambda (a b
) (< (car a
) (car b
)))))
4485 ;; Compute a list of allowed keys based on the first key
4486 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4487 ;; always available.
4489 (nconc (list ?
1 ?
2 ?
3 ?
4)
4491 (if (not first-key
) backends
4492 (nth 2 (assq first-key backends
))))
4493 (cond ((eq first-key ?P
) (list ?f ?p ?x ?a
))
4494 ((not first-key
) (list ?P
)))
4495 (when expertp
(list ??
))
4497 ;; Build the help menu for standard UI.
4501 ;; Options are hard-coded.
4503 [%s] Body only: %s [%s] Visible only: %s
4504 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4505 (funcall fontify-key
"1" t
)
4506 (if (memq 'body options
) "On " "Off")
4507 (funcall fontify-key
"2" t
)
4508 (if (memq 'visible options
) "On " "Off")
4509 (funcall fontify-key
"3" t
)
4510 (if (memq 'subtree options
) "Subtree" "Buffer ")
4511 (funcall fontify-key
"4" t
)
4512 (if (memq 'force options
) "On " "Off"))
4513 ;; Display registered back-end entries.
4516 (let ((top-key (car entry
)))
4519 (funcall fontify-key
(char-to-string top-key
))
4521 (let ((sub-menu (nth 2 entry
)))
4522 (unless (functionp sub-menu
)
4523 ;; Split sub-menu into two columns.
4529 (format (if (zerop (mod index
2)) " [%s] %-24s"
4531 (funcall fontify-key
4532 (char-to-string (car sub-entry
))
4536 (when (zerop (mod index
2)) "\n"))))))))
4538 ;; Publishing menu is hard-coded.
4539 (format "\n[%s] Publish
4540 [%s] Current file [%s] Current project
4541 [%s] Choose project [%s] All projects\n\n"
4542 (funcall fontify-key
"P")
4543 (funcall fontify-key
"f" ?P
)
4544 (funcall fontify-key
"p" ?P
)
4545 (funcall fontify-key
"x" ?P
)
4546 (funcall fontify-key
"a" ?P
))
4548 (funcall fontify-key
"q" t
)
4549 (if first-key
"Main menu" "Exit")))))
4550 ;; Build prompts for both standard and expert UI.
4551 (standard-prompt (unless expertp
"Export command: "))
4555 "Export command (Options: %s%s%s%s) [%s]: "
4556 (if (memq 'body options
) (funcall fontify-key
"b" t
) "-")
4557 (if (memq 'subtree options
) (funcall fontify-key
"s" t
) "-")
4558 (if (memq 'visible options
) (funcall fontify-key
"v" t
) "-")
4559 (if (memq 'force options
) (funcall fontify-key
"f" t
) "-")
4560 (concat allowed-keys
)))))
4561 ;; With expert UI, just read key with a fancy prompt. In standard
4562 ;; UI, display an intrusive help buffer.
4564 (org-export-dispatch-action
4565 expert-prompt allowed-keys backends options first-key expertp
)
4566 (save-window-excursion
4567 (delete-other-windows)
4570 (with-current-buffer
4571 (get-buffer-create "*Org Export Dispatcher*")
4573 (save-excursion (insert help
)))
4574 (org-fit-window-to-buffer
4575 (display-buffer "*Org Export Dispatcher*"))
4576 (org-export-dispatch-action
4577 standard-prompt allowed-keys backends options first-key expertp
))
4578 (and (get-buffer "*Org Export Dispatcher*")
4579 (kill-buffer "*Org Export Dispatcher*")))))))
4581 (defun org-export-dispatch-action
4582 (prompt allowed-keys backends options first-key expertp
)
4583 "Read a character from command input and act accordingly.
4585 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4586 a list of characters available at a given step in the process.
4587 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4588 EXPERTP are the same as defined in `org-export-dispatch-ui',
4591 Toggle export options when required. Otherwise, return value is
4592 a list with action as CAR and a list of interactive export
4594 (let ((key (let ((k (read-char-exclusive prompt
)))
4595 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4596 ;; depending on user's key pressed.
4597 (if (< k
27) (+ k
96) k
))))
4599 ;; Ignore non-standard characters (i.e. "M-a") and
4600 ;; undefined associations.
4601 ((not (memq key allowed-keys
))
4602 (org-export-dispatch-ui options first-key expertp
))
4603 ;; q key at first level aborts export. At second
4604 ;; level, cancel first key instead.
4605 ((eq key ?q
) (if (not first-key
) (error "Export aborted")
4606 (org-export-dispatch-ui options nil expertp
)))
4607 ;; Help key: Switch back to standard interface if
4608 ;; expert UI was active.
4609 ((eq key ??
) (org-export-dispatch-ui options first-key nil
))
4610 ;; Toggle export options.
4611 ((memq key
'(?
1 ?
2 ?
3 ?
4))
4612 (org-export-dispatch-ui
4613 (let ((option (case key
(?
1 'body
) (?
2 'visible
) (?
3 'subtree
)
4615 (if (memq option options
) (remq option options
)
4616 (cons option options
)))
4618 ;; Action selected: Send key and options back to
4619 ;; `org-export-dispatch'.
4621 (and (eq first-key ?P
) (memq key
'(?f ?p ?x ?a
)))
4622 (functionp (nth 2 (assq key backends
))))
4624 ((not first-key
) (nth 2 (assq key backends
)))
4625 ;; Publishing actions are hard-coded. Send a special
4626 ;; signal to `org-export-dispatch'.
4629 (?f
'publish-current-file
)
4630 (?p
'publish-current-project
)
4631 (?x
'publish-choose-project
)
4633 (t (nth 2 (assq key
(nth 2 (assq first-key backends
))))))
4635 ;; Otherwise, enter sub-menu.
4636 (t (org-export-dispatch-ui options key expertp
)))))
4639 (provide 'org-export
)
4640 ;;; org-export.el ends here