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
"ob-exp")
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-smart-quotes nil
"'" org-export-with-smart-quotes
)
128 (:with-special-strings nil
"-" org-export-with-special-strings
)
129 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
130 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
131 (:with-toc nil
"toc" org-export-with-toc
)
132 (:with-tables nil
"|" org-export-with-tables
)
133 (:with-tags nil
"tags" org-export-with-tags
)
134 (:with-tasks nil
"tasks" org-export-with-tasks
)
135 (:with-timestamps nil
"<" org-export-with-timestamps
)
136 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
137 "Alist between export properties and ways to set them.
139 The CAR of the alist is the property name, and the CDR is a list
140 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
142 KEYWORD is a string representing a buffer keyword, or nil. Each
143 property defined this way can also be set, during subtree
144 export, through an headline property named after the keyword
145 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
147 OPTION is a string that could be found in an #+OPTIONS: line.
148 DEFAULT is the default value for the property.
149 BEHAVIOUR determine how Org should handle multiple keywords for
150 the same property. It is a symbol among:
151 nil Keep old value and discard the new one.
152 t Replace old value with the new one.
153 `space' Concatenate the values, separating them with a space.
154 `newline' Concatenate the values, separating them with
156 `split' Split values at white spaces, and cons them to the
159 KEYWORD and OPTION have precedence over DEFAULT.
161 All these properties should be back-end agnostic. Back-end
162 specific properties are set through `org-export-define-backend'.
163 Properties redefined there have precedence over these.")
165 (defconst org-export-special-keywords
'("SETUP_FILE" "OPTIONS")
166 "List of in-buffer keywords that require special treatment.
167 These keywords are not directly associated to a property. The
168 way they are handled must be hard-coded into
169 `org-export--get-inbuffer-options' function.")
171 (defconst org-export-filters-alist
172 '((:filter-bold . org-export-filter-bold-functions
)
173 (:filter-babel-call . org-export-filter-babel-call-functions
)
174 (:filter-center-block . org-export-filter-center-block-functions
)
175 (:filter-clock . org-export-filter-clock-functions
)
176 (:filter-code . org-export-filter-code-functions
)
177 (:filter-comment . org-export-filter-comment-functions
)
178 (:filter-comment-block . org-export-filter-comment-block-functions
)
179 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
180 (:filter-drawer . org-export-filter-drawer-functions
)
181 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
182 (:filter-entity . org-export-filter-entity-functions
)
183 (:filter-example-block . org-export-filter-example-block-functions
)
184 (:filter-export-block . org-export-filter-export-block-functions
)
185 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
186 (:filter-final-output . org-export-filter-final-output-functions
)
187 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
188 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
189 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
190 (:filter-headline . org-export-filter-headline-functions
)
191 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
192 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
193 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
194 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
195 (:filter-italic . org-export-filter-italic-functions
)
196 (:filter-item . org-export-filter-item-functions
)
197 (:filter-keyword . org-export-filter-keyword-functions
)
198 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
199 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
200 (:filter-line-break . org-export-filter-line-break-functions
)
201 (:filter-link . org-export-filter-link-functions
)
202 (:filter-macro . org-export-filter-macro-functions
)
203 (:filter-node-property . org-export-filter-node-property-functions
)
204 (:filter-paragraph . org-export-filter-paragraph-functions
)
205 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
206 (:filter-plain-list . org-export-filter-plain-list-functions
)
207 (:filter-plain-text . org-export-filter-plain-text-functions
)
208 (:filter-planning . org-export-filter-planning-functions
)
209 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
210 (:filter-quote-block . org-export-filter-quote-block-functions
)
211 (:filter-quote-section . org-export-filter-quote-section-functions
)
212 (:filter-radio-target . org-export-filter-radio-target-functions
)
213 (:filter-section . org-export-filter-section-functions
)
214 (:filter-special-block . org-export-filter-special-block-functions
)
215 (:filter-src-block . org-export-filter-src-block-functions
)
216 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
217 (:filter-strike-through . org-export-filter-strike-through-functions
)
218 (:filter-subscript . org-export-filter-subscript-functions
)
219 (:filter-superscript . org-export-filter-superscript-functions
)
220 (:filter-table . org-export-filter-table-functions
)
221 (:filter-table-cell . org-export-filter-table-cell-functions
)
222 (:filter-table-row . org-export-filter-table-row-functions
)
223 (:filter-target . org-export-filter-target-functions
)
224 (:filter-timestamp . org-export-filter-timestamp-functions
)
225 (:filter-underline . org-export-filter-underline-functions
)
226 (:filter-verbatim . org-export-filter-verbatim-functions
)
227 (:filter-verse-block . org-export-filter-verse-block-functions
))
228 "Alist between filters properties and initial values.
230 The key of each association is a property name accessible through
231 the communication channel. Its value is a configurable global
232 variable defining initial filters.
234 This list is meant to install user specified filters. Back-end
235 developers may install their own filters using
236 `org-export-define-backend'. Filters defined there will always
237 be prepended to the current list, so they always get applied
240 (defconst org-export-default-inline-image-rule
244 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
245 "xpm" "pbm" "pgm" "ppm") t
))))
246 "Default rule for link matching an inline image.
247 This rule applies to links with no description. By default, it
248 will be considered as an inline image if it targets a local file
249 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
250 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
251 See `org-export-inline-image-p' for more information about
256 ;;; User-configurable Variables
258 ;; Configuration for the masses.
260 ;; They should never be accessed directly, as their value is to be
261 ;; stored in a property list (cf. `org-export-options-alist').
262 ;; Back-ends will read their value from there instead.
264 (defgroup org-export nil
265 "Options for exporting Org mode files."
269 (defgroup org-export-general nil
270 "General options for export engine."
271 :tag
"Org Export General"
274 (defcustom org-export-with-archived-trees
'headline
275 "Whether sub-trees with the ARCHIVE tag should be exported.
277 This can have three different values:
278 nil Do not export, pretend this tree is not present.
279 t Do export the entire tree.
280 `headline' Only export the headline, but skip the tree below it.
282 This option can also be set with the #+OPTIONS line,
284 :group
'org-export-general
286 (const :tag
"Not at all" nil
)
287 (const :tag
"Headline only" 'headline
)
288 (const :tag
"Entirely" t
)))
290 (defcustom org-export-with-author t
291 "Non-nil means insert author name into the exported file.
292 This option can also be set with the #+OPTIONS line,
293 e.g. \"author:nil\"."
294 :group
'org-export-general
297 (defcustom org-export-with-clocks nil
298 "Non-nil means export CLOCK keywords.
299 This option can also be set with the #+OPTIONS line,
301 :group
'org-export-general
304 (defcustom org-export-with-creator
'comment
305 "Non-nil means the postamble should contain a creator sentence.
307 The sentence can be set in `org-export-creator-string' and
308 defaults to \"Generated by Org mode XX in Emacs XXX.\".
310 If the value is `comment' insert it as a comment."
311 :group
'org-export-general
313 (const :tag
"No creator sentence" nil
)
314 (const :tag
"Sentence as a comment" 'comment
)
315 (const :tag
"Insert the sentence" t
)))
317 (defcustom org-export-creator-string
318 (format "Generated by Org mode %s in Emacs %s."
319 (if (fboundp 'org-version
) (org-version) "(Unknown)")
321 "String to insert at the end of the generated document."
322 :group
'org-export-general
323 :type
'(string :tag
"Creator string"))
325 (defcustom org-export-with-drawers t
326 "Non-nil means export contents of standard drawers.
328 When t, all drawers are exported. This may also be a list of
329 drawer names to export. This variable doesn't apply to
332 This option can also be set with the #+OPTIONS line,
334 :group
'org-export-general
336 (const :tag
"All drawers" t
)
337 (const :tag
"None" nil
)
338 (repeat :tag
"Selected drawers"
339 (string :tag
"Drawer name"))))
341 (defcustom org-export-with-email nil
342 "Non-nil means insert author email into the exported file.
343 This option can also be set with the #+OPTIONS line,
345 :group
'org-export-general
348 (defcustom org-export-with-emphasize t
349 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
351 If the export target supports emphasizing text, the word will be
352 typeset in bold, italic, or underlined, respectively. Not all
353 export backends support this.
355 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
356 :group
'org-export-general
359 (defcustom org-export-exclude-tags
'("noexport")
360 "Tags that exclude a tree from export.
362 All trees carrying any of these tags will be excluded from
363 export. This is without condition, so even subtrees inside that
364 carry one of the `org-export-select-tags' will be removed.
366 This option can also be set with the #+EXCLUDE_TAGS: keyword."
367 :group
'org-export-general
368 :type
'(repeat (string :tag
"Tag")))
370 (defcustom org-export-with-fixed-width t
371 "Non-nil means lines starting with \":\" will be in fixed width font.
373 This can be used to have pre-formatted text, fragments of code
375 : ;; Some Lisp examples
378 will be looking just like this in also HTML. See also the QUOTE
379 keyword. Not all export backends support this.
381 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
382 :group
'org-export-translation
385 (defcustom org-export-with-footnotes t
386 "Non-nil means Org footnotes should be exported.
387 This option can also be set with the #+OPTIONS line,
389 :group
'org-export-general
392 (defcustom org-export-headline-levels
3
393 "The last level which is still exported as a headline.
395 Inferior levels will produce itemize lists when exported.
397 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
398 :group
'org-export-general
401 (defcustom org-export-default-language
"en"
402 "The default language for export and clocktable translations, as a string.
403 This may have an association in
404 `org-clock-clocktable-language-setup'."
405 :group
'org-export-general
406 :type
'(string :tag
"Language"))
408 (defcustom org-export-preserve-breaks nil
409 "Non-nil means preserve all line breaks when exporting.
411 Normally, in HTML output paragraphs will be reformatted.
413 This option can also be set with the #+OPTIONS line,
415 :group
'org-export-general
418 (defcustom org-export-with-entities t
419 "Non-nil means interpret entities when exporting.
421 For example, HTML export converts \\alpha to α and \\AA to
424 For a list of supported names, see the constant `org-entities'
425 and the user option `org-entities-user'.
427 This option can also be set with the #+OPTIONS line,
429 :group
'org-export-general
432 (defcustom org-export-with-inlinetasks t
433 "Non-nil means inlinetasks should be exported.
434 This option can also be set with the #+OPTIONS line,
435 e.g. \"inline:nil\"."
436 :group
'org-export-general
439 (defcustom org-export-with-planning nil
440 "Non-nil means include planning info in export.
441 This option can also be set with the #+OPTIONS: line,
443 :group
'org-export-general
446 (defcustom org-export-with-priority nil
447 "Non-nil means include priority cookies in export.
448 This option can also be set with the #+OPTIONS line,
450 :group
'org-export-general
453 (defcustom org-export-with-section-numbers t
454 "Non-nil means add section numbers to headlines when exporting.
456 When set to an integer n, numbering will only happen for
457 headlines whose relative level is higher or equal to n.
459 This option can also be set with the #+OPTIONS line,
461 :group
'org-export-general
464 (defcustom org-export-select-tags
'("export")
465 "Tags that select a tree for export.
467 If any such tag is found in a buffer, all trees that do not carry
468 one of these tags will be ignored during export. Inside trees
469 that are selected like this, you can still deselect a subtree by
470 tagging it with one of the `org-export-exclude-tags'.
472 This option can also be set with the #+SELECT_TAGS: keyword."
473 :group
'org-export-general
474 :type
'(repeat (string :tag
"Tag")))
476 (defcustom org-export-with-smart-quotes nil
477 "Non-nil means activate smart quotes during export.
478 This option can also be set with the #+OPTIONS: line,
480 :group
'org-export-general
483 (defcustom org-export-with-special-strings t
484 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
486 When this option is turned on, these strings will be exported as:
489 -----+----------+--------+-------
493 ... … \\ldots …
495 This option can also be set with the #+OPTIONS line,
497 :group
'org-export-general
500 (defcustom org-export-with-statistics-cookies t
501 "Non-nil means include statistics cookies in export.
502 This option can also be set with the #+OPTIONS: line,
504 :group
'org-export-general
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
530 :group
'org-export-general
532 (const :tag
"Interpret them" t
)
533 (const :tag
"Curly brackets only" {})
534 (const :tag
"Do not interpret them" nil
)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group
'org-export-general
550 (const :tag
"No Table of Contents" nil
)
551 (const :tag
"Full Table of Contents" t
)
552 (integer :tag
"TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group
'org-export-general
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
573 This option can also be set with the #+OPTIONS line,
575 :group
'org-export-general
577 (const :tag
"Off" nil
)
578 (const :tag
"Not in TOC" not-in-toc
)
579 (const :tag
"On" t
)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
583 This may have the following values:
584 t include tasks independent of state.
585 todo include only tasks that are not yet done.
586 done include only tasks that are already done.
587 nil remove all tasks before export
588 list of keywords keep only tasks with these keywords"
589 :group
'org-export-general
591 (const :tag
"All tasks" t
)
592 (const :tag
"No tasks" nil
)
593 (const :tag
"Not-done tasks" todo
)
594 (const :tag
"Only done tasks" done
)
595 (repeat :tag
"Specific TODO keywords"
596 (string :tag
"Keyword"))))
598 (defcustom org-export-time-stamp-file t
599 "Non-nil means insert a time stamp into the exported file.
600 The time stamp shows when the file was created.
602 This option can also be set with the #+OPTIONS line,
603 e.g. \"timestamp:nil\"."
604 :group
'org-export-general
607 (defcustom org-export-with-timestamps t
608 "Non nil means allow timestamps in export.
610 It can be set to `active', `inactive', t or nil, in order to
611 export, respectively, only active timestamps, only inactive ones,
614 This option can also be set with the #+OPTIONS line, e.g.
616 :group
'org-export-general
618 (const :tag
"All timestamps" t
)
619 (const :tag
"Only active timestamps" active
)
620 (const :tag
"Only inactive timestamps" inactive
)
621 (const :tag
"No timestamp" nil
)))
623 (defcustom org-export-with-todo-keywords t
624 "Non-nil means include TODO keywords in export.
625 When nil, remove all these keywords from the export."
626 :group
'org-export-general
629 (defcustom org-export-allow-BIND
'confirm
630 "Non-nil means allow #+BIND to define local variable values for export.
631 This is a potential security risk, which is why the user must
632 confirm the use of these lines."
633 :group
'org-export-general
635 (const :tag
"Never" nil
)
636 (const :tag
"Always" t
)
637 (const :tag
"Ask a confirmation for each file" confirm
)))
639 (defcustom org-export-snippet-translation-alist nil
640 "Alist between export snippets back-ends and exporter back-ends.
642 This variable allows to provide shortcuts for export snippets.
644 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
645 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
646 HTML code while every other back-end will ignore it."
647 :group
'org-export-general
650 (string :tag
"Shortcut")
651 (string :tag
"Back-end"))))
653 (defcustom org-export-coding-system nil
654 "Coding system for the exported file."
655 :group
'org-export-general
656 :type
'coding-system
)
658 (defcustom org-export-copy-to-kill-ring t
659 "Non-nil means exported stuff will also be pushed onto the kill ring."
660 :group
'org-export-general
663 (defcustom org-export-initial-scope
'buffer
664 "The initial scope when exporting with `org-export-dispatch'.
665 This variable can be either set to `buffer' or `subtree'."
666 :group
'org-export-general
668 (const :tag
"Export current buffer" 'buffer
)
669 (const :tag
"Export current subtree" 'subtree
)))
671 (defcustom org-export-show-temporary-export-buffer t
672 "Non-nil means show buffer after exporting to temp buffer.
673 When Org exports to a file, the buffer visiting that file is ever
674 shown, but remains buried. However, when exporting to
675 a temporary buffer, that buffer is popped up in a second window.
676 When this variable is nil, the buffer remains buried also in
678 :group
'org-export-general
681 (defcustom org-export-dispatch-use-expert-ui nil
682 "Non-nil means using a non-intrusive `org-export-dispatch'.
683 In that case, no help buffer is displayed. Though, an indicator
684 for current export scope is added to the prompt (\"b\" when
685 output is restricted to body only, \"s\" when it is restricted to
686 the current subtree, \"v\" when only visible elements are
687 considered for export and \"f\" when publishing functions should
688 be passed the FORCE argument). Also, \[?] allows to switch back
690 :group
'org-export-general
695 ;;; Defining New Back-ends
697 ;; `org-export-define-backend' is the standard way to define an export
698 ;; back-end. It allows to specify translators, filters, buffer
699 ;; options and a menu entry. If the new back-end shares translators
700 ;; with another back-end, `org-export-define-derived-backend' may be
703 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
704 ;; when a given back-end hasn't been registered yet.
706 (defmacro org-export-define-backend
(backend translators
&rest body
)
707 "Define a new back-end BACKEND.
709 TRANSLATORS is an alist between object or element types and
710 functions handling them.
712 These functions should return a string without any trailing
713 space, or nil. They must accept three arguments: the object or
714 element itself, its contents or nil when it isn't recursive and
715 the property list used as a communication channel.
717 Contents, when not nil, are stripped from any global indentation
718 \(although the relative one is preserved). They also always end
719 with a single newline character.
721 If, for a given type, no function is found, that element or
722 object type will simply be ignored, along with any blank line or
723 white space at its end. The same will happen if the function
724 returns the nil value. If that function returns the empty
725 string, the type will be ignored, but the blank lines or white
728 In addition to element and object types, one function can be
729 associated to the `template' symbol and another one to the
732 The former returns the final transcoded string, and can be used
733 to add a preamble and a postamble to document's body. It must
734 accept two arguments: the transcoded string and the property list
735 containing export options.
737 The latter, when defined, is to be called on every text not
738 recognized as an element or an object. It must accept two
739 arguments: the text string and the information channel. It is an
740 appropriate place to protect special chars relative to the
743 BODY can start with pre-defined keyword arguments. The following
744 keywords are understood:
748 String, or list of strings, representing block names that
749 will not be parsed. This is used to specify blocks that will
750 contain raw code specific to the back-end. These blocks
751 still have to be handled by the relative `export-block' type
756 Alist between filters and function, or list of functions,
757 specific to the back-end. See `org-export-filters-alist' for
758 a list of all allowed filters. Filters defined here
759 shouldn't make a back-end test, as it may prevent back-ends
760 derived from this one to behave properly.
764 Menu entry for the export dispatcher. It should be a list
767 \(KEY DESCRIPTION ACTION-OR-MENU)
771 KEY is a free character selecting the back-end.
772 DESCRIPTION is a string naming the back-end.
773 ACTION-OR-MENU is either a function or an alist.
775 If it is an action, it will be called with three arguments:
776 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
777 for further explanations.
779 If it is an alist, associations should follow the
782 \(KEY DESCRIPTION ACTION)
784 where KEY, DESCRIPTION and ACTION are described above.
786 Valid values include:
788 \(?m \"My Special Back-end\" my-special-export-function)
792 \(?l \"Export to LaTeX\"
793 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
794 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
795 \(?p \"PDF file\" org-e-latex-export-to-pdf)
796 \(?o \"PDF file and open\"
797 \(lambda (subtree visible body-only)
799 \(org-e-latex-export-to-pdf subtree visible body-only))))))
803 Alist between back-end specific properties introduced in
804 communication channel and how their value are acquired. See
805 `org-export-options-alist' for more information about
806 structure of the values."
807 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
809 (let (export-block filters menu-entry options
)
810 (while (keywordp (car body
))
812 (:export-block
(let ((names (pop body
)))
814 (if (consp names
) (mapcar 'upcase names
)
815 (list (upcase names
))))))
816 (:filters-alist
(setq filters
(pop body
)))
817 (:menu-entry
(setq menu-entry
(pop body
)))
818 (:options-alist
(setq options
(pop body
)))
821 ;; Define translators.
822 (defvar ,(intern (format "org-%s-translate-alist" backend
)) ',translators
823 "Alist between element or object types and translators.")
826 `(defconst ,(intern (format "org-%s-options-alist" backend
)) ',options
827 ,(format "Alist between %s export properties and ways to set them.
828 See `org-export-options-alist' for more information on the
829 structure of the values."
833 `(defconst ,(intern (format "org-%s-filters-alist" backend
)) ',filters
834 "Alist between filters keywords and back-end specific filters.
835 See `org-export-filters-alist' for more information."))
836 ;; Tell parser to not parse EXPORT-BLOCK blocks.
840 (add-to-list 'org-element-block-name-alist
841 `(,name . org-element-export-block-parser
)))
843 ;; Add an entry for back-end in `org-export-dispatch'.
845 `(unless (assq (car ',menu-entry
) org-export-dispatch-menu-entries
)
846 (add-to-list 'org-export-dispatch-menu-entries
',menu-entry
)))
847 ;; Splice in the body, if any.
850 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
851 "Create a new back-end as a variant of an existing one.
853 CHILD is the name of the derived back-end. PARENT is the name of
856 BODY can start with pre-defined keyword arguments. The following
857 keywords are understood:
861 String, or list of strings, representing block names that
862 will not be parsed. This is used to specify blocks that will
863 contain raw code specific to the back-end. These blocks
864 still have to be handled by the relative `export-block' type
869 Alist of filters that will overwrite or complete filters
870 defined in PARENT back-end. See `org-export-filters-alist'
871 for a list of allowed filters.
875 Menu entry for the export dispatcher. See
876 `org-export-define-backend' for more information about the
881 Alist of back-end specific properties that will overwrite or
882 complete those defined in PARENT back-end. Refer to
883 `org-export-options-alist' for more information about
884 structure of the values.
888 Append entries to an existing menu in the export dispatcher.
889 The associated value should be a list whose CAR is the
890 character selecting the menu to expand and CDR a list of
891 entries following the pattern:
893 \(KEY DESCRIPTION ACTION)
895 where KEY is a free character triggering the action,
896 DESCRIPTION is a string defining the action, and ACTION is
897 a function that will be called with three arguments:
898 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
899 for further explanations.
901 Valid values include:
903 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
904 \(?O \"As PDF file and open (Beamer)\"
906 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
910 Alist of element and object types and transcoders that will
911 overwrite or complete transcode table from PARENT back-end.
912 Refer to `org-export-define-backend' for detailed information
915 As an example, here is how one could define \"my-latex\" back-end
916 as a variant of `e-latex' back-end with a custom template
919 \(org-export-define-derived-backend my-latex e-latex
920 :translate-alist ((template . my-latex-template-fun)))
922 The back-end could then be called with, for example:
924 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
925 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] def-body
))
927 (let (export-block filters menu-entry options sub-menu-entry translate
)
928 (while (keywordp (car body
))
930 (:export-block
(let ((names (pop body
)))
932 (if (consp names
) (mapcar 'upcase names
)
933 (list (upcase names
))))))
934 (:filters-alist
(setq filters
(pop body
)))
935 (:menu-entry
(setq menu-entry
(pop body
)))
936 (:options-alist
(setq options
(pop body
)))
937 (:sub-menu-entry
(setq sub-menu-entry
(pop body
)))
938 (:translate-alist
(setq translate
(pop body
)))
941 ;; Tell parser to not parse EXPORT-BLOCK blocks.
945 (add-to-list 'org-element-block-name-alist
946 `(,name . org-element-export-block-parser
)))
949 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent
))))
950 (when (or (boundp parent-filters
) filters
)
951 `(defconst ,(intern (format "org-%s-filters-alist" child
))
953 (and (boundp parent-filters
)
954 (copy-sequence (symbol-value parent-filters
))))
955 "Alist between filters keywords and back-end specific filters.
956 See `org-export-filters-alist' for more information.")))
958 ,(let ((parent-options (intern (format "org-%s-options-alist" parent
))))
959 (when (or (boundp parent-options
) options
)
960 `(defconst ,(intern (format "org-%s-options-alist" child
))
962 (and (boundp parent-options
)
963 (copy-sequence (symbol-value parent-options
))))
964 ,(format "Alist between %s export properties and ways to set them.
965 See `org-export-options-alist' for more information on the
966 structure of the values."
968 ;; Define translators.
969 (defvar ,(intern (format "org-%s-translate-alist" child
))
973 (intern (format "org-%s-translate-alist" parent
)))))
974 "Alist between element or object types and translators.")
975 ;; Add an entry for back-end in `org-export-dispatch'.
977 `(unless (assq (car ',menu-entry
) org-export-dispatch-menu-entries
)
978 (add-to-list 'org-export-dispatch-menu-entries
',menu-entry
)))
979 ,(when sub-menu-entry
980 (let ((menu (nth 2 (assq (car sub-menu-entry
)
981 org-export-dispatch-menu-entries
))))
982 (when menu
`(nconc ',menu
983 ',(org-remove-if (lambda (e) (member e menu
))
984 (cdr sub-menu-entry
))))))
985 ;; Splice in the body, if any.
988 (defun org-export-barf-if-invalid-backend (backend)
989 "Signal an error if BACKEND isn't defined."
990 (unless (boundp (intern (format "org-%s-translate-alist" backend
)))
991 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
995 ;;; The Communication Channel
997 ;; During export process, every function has access to a number of
998 ;; properties. They are of two types:
1000 ;; 1. Environment options are collected once at the very beginning of
1001 ;; the process, out of the original buffer and configuration.
1002 ;; Collecting them is handled by `org-export-get-environment'
1005 ;; Most environment options are defined through the
1006 ;; `org-export-options-alist' variable.
1008 ;; 2. Tree properties are extracted directly from the parsed tree,
1009 ;; just before export, by `org-export-collect-tree-properties'.
1011 ;; Here is the full list of properties available during transcode
1012 ;; process, with their category and their value type.
1014 ;; + `:author' :: Author's name.
1015 ;; - category :: option
1018 ;; + `:back-end' :: Current back-end used for transcoding.
1019 ;; - category :: tree
1022 ;; + `:creator' :: String to write as creation information.
1023 ;; - category :: option
1026 ;; + `:date' :: String to use as date.
1027 ;; - category :: option
1030 ;; + `:description' :: Description text for the current data.
1031 ;; - category :: option
1034 ;; + `:email' :: Author's email.
1035 ;; - category :: option
1038 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1040 ;; - category :: option
1041 ;; - type :: list of strings
1043 ;; + `:exported-data' :: Hash table used for memoizing
1044 ;; `org-export-data'.
1045 ;; - category :: tree
1046 ;; - type :: hash table
1048 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1049 ;; their definition, as parsed data. Only non-inlined footnotes
1050 ;; are represented in this alist. Also, every definition isn't
1051 ;; guaranteed to be referenced in the parse tree. The purpose of
1052 ;; this property is to preserve definitions from oblivion
1053 ;; (i.e. when the parse tree comes from a part of the original
1054 ;; buffer), it isn't meant for direct use in a back-end. To
1055 ;; retrieve a definition relative to a reference, use
1056 ;; `org-export-get-footnote-definition' instead.
1057 ;; - category :: option
1058 ;; - type :: alist (STRING . LIST)
1060 ;; + `:headline-levels' :: Maximum level being exported as an
1061 ;; headline. Comparison is done with the relative level of
1062 ;; headlines in the parse tree, not necessarily with their
1064 ;; - category :: option
1065 ;; - type :: integer
1067 ;; + `:headline-offset' :: Difference between relative and real level
1068 ;; of headlines in the parse tree. For example, a value of -1
1069 ;; means a level 2 headline should be considered as level
1070 ;; 1 (cf. `org-export-get-relative-level').
1071 ;; - category :: tree
1072 ;; - type :: integer
1074 ;; + `:headline-numbering' :: Alist between headlines and their
1075 ;; numbering, as a list of numbers
1076 ;; (cf. `org-export-get-headline-number').
1077 ;; - category :: tree
1078 ;; - type :: alist (INTEGER . LIST)
1080 ;; + `:id-alist' :: Alist between ID strings and destination file's
1081 ;; path, relative to current directory. It is used by
1082 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1084 ;; - category :: option
1085 ;; - type :: alist (STRING . STRING)
1087 ;; + `:ignore-list' :: List of elements and objects that should be
1088 ;; ignored during export.
1089 ;; - category :: tree
1090 ;; - type :: list of elements and objects
1092 ;; + `:input-file' :: Full path to input file, if any.
1093 ;; - category :: option
1094 ;; - type :: string or nil
1096 ;; + `:keywords' :: List of keywords attached to data.
1097 ;; - category :: option
1100 ;; + `:language' :: Default language used for translations.
1101 ;; - category :: option
1104 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1106 ;; - category :: option
1107 ;; - type :: list (as returned by `org-element-parse-buffer')
1109 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1111 ;; - category :: option
1112 ;; - type :: symbol (nil, t)
1114 ;; + `:section-numbers' :: Non-nil means transcoding should add
1115 ;; section numbers to headlines.
1116 ;; - category :: option
1117 ;; - type :: symbol (nil, t)
1119 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1120 ;; in transcoding. When such a tag is present, subtrees without
1121 ;; it are de facto excluded from the process. See
1122 ;; `use-select-tags'.
1123 ;; - category :: option
1124 ;; - type :: list of strings
1126 ;; + `:target-list' :: List of targets encountered in the parse tree.
1127 ;; This is used to partly resolve "fuzzy" links
1128 ;; (cf. `org-export-resolve-fuzzy-link').
1129 ;; - category :: tree
1130 ;; - type :: list of strings
1132 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1133 ;; a time stamp in the output.
1134 ;; - category :: option
1135 ;; - type :: symbol (nil, t)
1137 ;; + `:translate-alist' :: Alist between element and object types and
1138 ;; transcoding functions relative to the current back-end.
1139 ;; Special keys `template' and `plain-text' are also possible.
1140 ;; - category :: option
1141 ;; - type :: alist (SYMBOL . FUNCTION)
1143 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1144 ;; also be transcoded. If it is set to the `headline' symbol,
1145 ;; only the archived headline's name is retained.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t, `headline')
1149 ;; + `:with-author' :: Non-nil means author's name should be included
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1155 ;; - category :: option
1156 ;; - type :: symbol (nil, t)
1158 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1159 ;; inserted at the end of the transcoded string. If the value
1160 ;; is `comment', it should be commented.
1161 ;; - category :: option
1162 ;; - type :: symbol (`comment', nil, t)
1164 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1165 ;; its value is a list of names, only drawers with such names
1166 ;; will be transcoded.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, t) or list of strings
1170 ;; + `:with-email' :: Non-nil means output should contain author's
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1177 ;; - category :: option
1178 ;; - type :: symbol (nil, t)
1180 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1181 ;; strings starting with a colon as a fixed-with (verbatim) area.
1182 ;; - category :: option
1183 ;; - type :: symbol (nil, t)
1185 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1187 ;; - category :: option
1188 ;; - type :: symbol (nil, t)
1190 ;; + `:with-plannings' :: Non-nil means transcoding should include
1192 ;; - category :: option
1193 ;; - type :: symbol (nil, t)
1195 ;; + `:with-priority' :: Non-nil means transcoding should include
1196 ;; priority cookies.
1197 ;; - category :: option
1198 ;; - type :: symbol (nil, t)
1200 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1202 ;; - category :: option
1203 ;; - type :: symbol (nil, t)
1205 ;; + `:with-special-strings' :: Non-nil means transcoding should
1206 ;; interpret special strings in plain text.
1207 ;; - category :: option
1208 ;; - type :: symbol (nil, t)
1210 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1211 ;; interpret subscript and superscript. With a value of "{}",
1212 ;; only interpret those using curly brackets.
1213 ;; - category :: option
1214 ;; - type :: symbol (nil, {}, t)
1216 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1218 ;; - category :: option
1219 ;; - type :: symbol (nil, t)
1221 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1222 ;; headlines. A `not-in-toc' value will remove them from the
1223 ;; table of contents, if any, nonetheless.
1224 ;; - category :: option
1225 ;; - type :: symbol (nil, t, `not-in-toc')
1227 ;; + `:with-tasks' :: Non-nil means transcoding should include
1228 ;; headlines with a TODO keyword. A `todo' value will only
1229 ;; include headlines with a todo type keyword while a `done'
1230 ;; value will do the contrary. If a list of strings is provided,
1231 ;; only tasks with keywords belonging to that list will be kept.
1232 ;; - category :: option
1233 ;; - type :: symbol (t, todo, done, nil) or list of strings
1235 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1236 ;; time stamps. Special value `active' (resp. `inactive') ask to
1237 ;; export only active (resp. inactive) timestamps. Otherwise,
1238 ;; completely remove them.
1239 ;; - category :: option
1240 ;; - type :: symbol: (`active', `inactive', t, nil)
1242 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1243 ;; added to the output. An integer value limits its depth.
1244 ;; - category :: option
1245 ;; - type :: symbol (nil, t or integer)
1247 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1248 ;; include TODO keywords.
1249 ;; - category :: option
1250 ;; - type :: symbol (nil, t)
1253 ;;;; Environment Options
1255 ;; Environment options encompass all parameters defined outside the
1256 ;; scope of the parsed data. They come from five sources, in
1257 ;; increasing precedence order:
1259 ;; - Global variables,
1260 ;; - Buffer's attributes,
1261 ;; - Options keyword symbols,
1262 ;; - Buffer keywords,
1263 ;; - Subtree properties.
1265 ;; The central internal function with regards to environment options
1266 ;; is `org-export-get-environment'. It updates global variables with
1267 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1268 ;; the different sources.
1270 ;; The internal functions doing the retrieval are:
1271 ;; `org-export--get-global-options',
1272 ;; `org-export--get-buffer-attributes',
1273 ;; `org-export--parse-option-keyword',
1274 ;; `org-export--get-subtree-options' and
1275 ;; `org-export--get-inbuffer-options'
1277 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1278 ;; take care of the part relative to "#+BIND:" keywords.
1280 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1281 "Collect export options from the current buffer.
1283 Optional argument BACKEND is a symbol specifying which back-end
1284 specific options to read, if any.
1286 When optional argument SUBTREEP is non-nil, assume the export is
1287 done against the current sub-tree.
1289 Third optional argument EXT-PLIST is a property list with
1290 external parameters overriding Org default settings, but still
1291 inferior to file-local settings."
1292 ;; First install #+BIND variables.
1293 (org-export--install-letbind-maybe)
1294 ;; Get and prioritize export options...
1296 ;; ... from global variables...
1297 (org-export--get-global-options backend
)
1298 ;; ... from buffer's attributes...
1299 (org-export--get-buffer-attributes)
1300 ;; ... from an external property list...
1302 ;; ... from in-buffer settings...
1303 (org-export--get-inbuffer-options
1305 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1306 ;; ... and from subtree, when appropriate.
1307 (and subtreep
(org-export--get-subtree-options backend
))
1308 ;; Eventually install back-end symbol and its translation table.
1312 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend
))))
1313 (when (boundp trans-alist
) (symbol-value trans-alist
))))))
1315 (defun org-export--parse-option-keyword (options &optional backend
)
1316 "Parse an OPTIONS line and return values as a plist.
1317 Optional argument BACKEND is a symbol specifying which back-end
1318 specific items to read, if any."
1320 (append org-export-options-alist
1323 (format "org-%s-options-alist" backend
))))
1324 (and (boundp var
) (eval var
))))))
1325 ;; Build an alist between #+OPTION: item and property-name.
1328 (when (nth 2 e
) (cons (regexp-quote (nth 2 e
))
1333 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1335 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1337 (setq plist
(plist-put plist
1339 (car (read-from-string
1340 (match-string 2 options
)))))))
1344 (defun org-export--get-subtree-options (&optional backend
)
1345 "Get export options in subtree at point.
1346 Optional argument BACKEND is a symbol specifying back-end used
1347 for export. Return options as a plist."
1348 ;; For each buffer keyword, create an headline property setting the
1349 ;; same property in communication channel. The name for the property
1350 ;; is the keyword with "EXPORT_" appended to it.
1351 (org-with-wide-buffer
1353 ;; Make sure point is at an heading.
1354 (unless (org-at-heading-p) (org-back-to-heading t
))
1355 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1356 ;; title as its fallback value.
1357 (when (setq prop
(progn (looking-at org-todo-line-regexp
)
1358 (or (save-match-data
1359 (org-entry-get (point) "EXPORT_TITLE"))
1360 (org-match-string-no-properties 3))))
1364 (org-element-parse-secondary-string
1365 prop
(org-element-restriction 'keyword
)))))
1366 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1367 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1369 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1370 ;; Handle other keywords. TITLE keyword is excluded as it has
1371 ;; already been handled already.
1372 (let ((seen '("TITLE")))
1375 (let ((property (nth 1 option
)))
1376 (when (and property
(not (member property seen
)))
1377 (let* ((subtree-prop (concat "EXPORT_" property
))
1378 ;; Export properties are not case-sensitive.
1379 (value (let ((case-fold-search t
))
1380 (org-entry-get (point) subtree-prop
))))
1381 (push property seen
)
1387 ;; Parse VALUE if required.
1388 (if (member property org-element-document-properties
)
1389 (org-element-parse-secondary-string
1390 value
(org-element-restriction 'keyword
))
1392 ;; Also look for both general keywords and back-end specific
1393 ;; options if BACKEND is provided.
1394 (append (and backend
1396 (format "org-%s-options-alist" backend
))))
1397 (and (boundp var
) (symbol-value var
))))
1398 org-export-options-alist
)))
1402 (defun org-export--get-inbuffer-options (&optional backend files
)
1403 "Return current buffer export options, as a plist.
1405 Optional argument BACKEND, when non-nil, is a symbol specifying
1406 which back-end specific options should also be read in the
1409 Optional argument FILES is a list of setup files names read so
1410 far, used to avoid circular dependencies.
1412 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1413 (org-with-wide-buffer
1414 (goto-char (point-min))
1415 (let ((case-fold-search t
) plist
)
1416 ;; 1. Special keywords, as in `org-export-special-keywords'.
1418 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords
))))
1419 (while (re-search-forward special-re nil t
)
1420 (let ((element (org-element-at-point)))
1421 (when (eq (org-element-type element
) 'keyword
)
1422 (let* ((key (org-element-property :key element
))
1423 (val (org-element-property :value element
))
1426 ((string= key
"SETUP_FILE")
1429 (org-remove-double-quotes (org-trim val
)))))
1430 ;; Avoid circular dependencies.
1431 (unless (member file files
)
1433 (insert (org-file-contents file
'noerror
))
1435 (org-export--get-inbuffer-options
1436 backend
(cons file files
))))))
1437 ((string= key
"OPTIONS")
1438 (org-export--parse-option-keyword val backend
)))))
1439 (setq plist
(org-combine-plists plist prop
)))))))
1440 ;; 2. Standard options, as in `org-export-options-alist'.
1441 (let* ((all (append org-export-options-alist
1442 ;; Also look for back-end specific options
1443 ;; if BACKEND is defined.
1447 (format "org-%s-options-alist" backend
))))
1448 (and (boundp var
) (eval var
))))))
1449 ;; Build ALIST between keyword name and property name.
1452 (lambda (e) (when (nth 1 e
) (cons (nth 1 e
) (car e
))))
1454 ;; Build regexp matching all keywords associated to export
1455 ;; options. Note: the search is case insensitive.
1456 (opt-re (format "^[ \t]*#\\+%s:"
1458 (delq nil
(mapcar (lambda (e) (nth 1 e
)) all
))))))
1459 (goto-char (point-min))
1460 (while (re-search-forward opt-re nil t
)
1461 (let ((element (org-element-at-point)))
1462 (when (eq (org-element-type element
) 'keyword
)
1463 (let* ((key (org-element-property :key element
))
1464 (val (org-element-property :value element
))
1465 (prop (cdr (assoc key alist
)))
1466 (behaviour (nth 4 (assq prop all
))))
1470 ;; Handle value depending on specified BEHAVIOUR.
1473 (if (not (plist-get plist prop
)) (org-trim val
)
1474 (concat (plist-get plist prop
) " " (org-trim val
))))
1477 (concat (plist-get plist prop
) "\n" (org-trim val
))))
1479 `(,@(plist-get plist prop
) ,@(org-split-string val
)))
1481 (otherwise (if (not (plist-member plist prop
)) val
1482 (plist-get plist prop
))))))))))
1483 ;; Parse keywords specified in
1484 ;; `org-element-document-properties'.
1487 (let* ((prop (cdr (assoc key alist
)))
1488 (value (and prop
(plist-get plist prop
))))
1489 (when (stringp value
)
1493 (org-element-parse-secondary-string
1494 value
(org-element-restriction 'keyword
)))))))
1495 org-element-document-properties
))
1496 ;; 3. Return final value.
1499 (defun org-export--get-buffer-attributes ()
1500 "Return properties related to buffer attributes, as a plist."
1501 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1503 ;; Store full path of input file name, or nil. For internal use.
1504 :input-file visited-file
1505 :title
(or (and visited-file
1506 (file-name-sans-extension
1507 (file-name-nondirectory visited-file
)))
1508 (buffer-name (buffer-base-buffer)))
1509 :footnote-definition-alist
1510 ;; Footnotes definitions must be collected in the original
1511 ;; buffer, as there's no insurance that they will still be in the
1512 ;; parse tree, due to possible narrowing.
1514 (org-with-wide-buffer
1515 (goto-char (point-min))
1516 (while (re-search-forward org-footnote-definition-re nil t
)
1517 (let ((def (save-match-data (org-element-at-point))))
1518 (when (eq (org-element-type def
) 'footnote-definition
)
1520 (cons (org-element-property :label def
)
1521 (let ((cbeg (org-element-property :contents-begin def
)))
1523 (org-element--parse-elements
1524 cbeg
(org-element-property :contents-end def
)
1525 nil nil nil nil
(list 'org-data nil
)))))
1529 ;; Collect id references.
1531 (org-with-wide-buffer
1532 (goto-char (point-min))
1533 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1534 (let ((link (org-element-context)))
1535 (when (eq (org-element-type link
) 'link
)
1536 (let* ((id (org-element-property :path link
))
1537 (file (org-id-find-id-file id
)))
1539 (push (cons id
(file-relative-name file
)) alist
)))))))
1542 (defun org-export--get-global-options (&optional backend
)
1543 "Return global export options as a plist.
1545 Optional argument BACKEND, if non-nil, is a symbol specifying
1546 which back-end specific export options should also be read in the
1548 (let ((all (append org-export-options-alist
1551 (format "org-%s-options-alist" backend
))))
1552 (and (boundp var
) (symbol-value var
))))))
1561 ;; Eval default value provided. If keyword is a member
1562 ;; of `org-element-document-properties', parse it as
1563 ;; a secondary string before storing it.
1564 (let ((value (eval (nth 3 cell
))))
1565 (if (not (stringp value
)) value
1566 (let ((keyword (nth 1 cell
)))
1567 (if (not (member keyword org-element-document-properties
))
1569 (org-element-parse-secondary-string
1570 value
(org-element-restriction 'keyword
)))))))))
1575 (defvar org-export--allow-BIND-local nil
)
1576 (defun org-export--confirm-letbind ()
1577 "Can we use #+BIND values during export?
1578 By default this will ask for confirmation by the user, to divert
1579 possible security risks."
1581 ((not org-export-allow-BIND
) nil
)
1582 ((eq org-export-allow-BIND t
) t
)
1583 ((local-variable-p 'org-export--allow-BIND-local
)
1584 org-export--allow-BIND-local
)
1585 (t (org-set-local 'org-export--allow-BIND-local
1586 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1588 (defun org-export--install-letbind-maybe ()
1589 "Install the values from #+BIND lines as local variables.
1590 Variables must be installed before in-buffer options are
1592 (let ((case-fold-search t
) letbind pair
)
1593 (org-with-wide-buffer
1594 (goto-char (point-min))
1595 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t
)
1596 (let* ((element (org-element-at-point))
1597 (value (org-element-property :value element
)))
1598 (when (and (eq (org-element-type element
) 'keyword
)
1599 (not (equal value
""))
1600 (org-export--confirm-letbind))
1601 (push (read (format "(%s)" value
)) letbind
)))))
1602 (dolist (pair (nreverse letbind
))
1603 (org-set-local (car pair
) (nth 1 pair
)))))
1606 ;;;; Tree Properties
1608 ;; Tree properties are information extracted from parse tree. They
1609 ;; are initialized at the beginning of the transcoding process by
1610 ;; `org-export-collect-tree-properties'.
1612 ;; Dedicated functions focus on computing the value of specific tree
1613 ;; properties during initialization. Thus,
1614 ;; `org-export--populate-ignore-list' lists elements and objects that
1615 ;; should be skipped during export, `org-export--get-min-level' gets
1616 ;; the minimal exportable level, used as a basis to compute relative
1617 ;; level for headlines. Eventually
1618 ;; `org-export--collect-headline-numbering' builds an alist between
1619 ;; headlines and their numbering.
1621 (defun org-export-collect-tree-properties (data info
)
1622 "Extract tree properties from parse tree.
1624 DATA is the parse tree from which information is retrieved. INFO
1625 is a list holding export options.
1627 Following tree properties are set or updated:
1629 `:exported-data' Hash table used to memoize results from
1632 `:footnote-definition-alist' List of footnotes definitions in
1633 original buffer and current parse tree.
1635 `:headline-offset' Offset between true level of headlines and
1636 local level. An offset of -1 means an headline
1637 of level 2 should be considered as a level
1638 1 headline in the context.
1640 `:headline-numbering' Alist of all headlines as key an the
1641 associated numbering as value.
1643 `:ignore-list' List of elements that should be ignored during
1646 `:target-list' List of all targets in the parse tree.
1648 Return updated plist."
1649 ;; Install the parse tree in the communication channel, in order to
1650 ;; use `org-export-get-genealogy' and al.
1651 (setq info
(plist-put info
:parse-tree data
))
1652 ;; Get the list of elements and objects to ignore, and put it into
1653 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1654 ;; been done during parse tree filtering.
1658 (append (org-export--populate-ignore-list data info
)
1659 (plist-get info
:ignore-list
))))
1660 ;; Compute `:headline-offset' in order to be able to use
1661 ;; `org-export-get-relative-level'.
1665 (- 1 (org-export--get-min-level data info
))))
1666 ;; Update footnotes definitions list with definitions in parse tree.
1667 ;; This is required since buffer expansion might have modified
1668 ;; boundaries of footnote definitions contained in the parse tree.
1669 ;; This way, definitions in `footnote-definition-alist' are bound to
1670 ;; match those in the parse tree.
1671 (let ((defs (plist-get info
:footnote-definition-alist
)))
1673 data
'footnote-definition
1675 (push (cons (org-element-property :label fn
)
1676 `(org-data nil
,@(org-element-contents fn
)))
1678 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1679 ;; Properties order doesn't matter: get the rest of the tree
1684 data
'(keyword target
)
1686 (when (or (eq (org-element-type blob
) 'target
)
1687 (string= (org-element-property :key blob
) "TARGET"))
1689 :headline-numbering
,(org-export--collect-headline-numbering data info
)
1690 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1693 (defun org-export--get-min-level (data options
)
1694 "Return minimum exportable headline's level in DATA.
1695 DATA is parsed tree as returned by `org-element-parse-buffer'.
1696 OPTIONS is a plist holding export options."
1698 (let ((min-level 10000))
1701 (when (and (eq (org-element-type blob
) 'headline
)
1702 (not (memq blob
(plist-get options
:ignore-list
))))
1704 (min (org-element-property :level blob
) min-level
)))
1705 (when (= min-level
1) (throw 'exit
1)))
1706 (org-element-contents data
))
1707 ;; If no headline was found, for the sake of consistency, set
1708 ;; minimum level to 1 nonetheless.
1709 (if (= min-level
10000) 1 min-level
))))
1711 (defun org-export--collect-headline-numbering (data options
)
1712 "Return numbering of all exportable headlines in a parse tree.
1714 DATA is the parse tree. OPTIONS is the plist holding export
1717 Return an alist whose key is an headline and value is its
1718 associated numbering \(in the shape of a list of numbers\)."
1719 (let ((numbering (make-vector org-export-max-depth
0)))
1724 (let ((relative-level
1725 (1- (org-export-get-relative-level headline options
))))
1728 (loop for n across numbering
1729 for idx from
0 to org-export-max-depth
1730 when
(< idx relative-level
) collect n
1731 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1732 when
(> idx relative-level
) do
(aset numbering idx
0)))))
1735 (defun org-export--populate-ignore-list (data options
)
1736 "Return list of elements and objects to ignore during export.
1737 DATA is the parse tree to traverse. OPTIONS is the plist holding
1741 ;; First find trees containing a select tag, if any.
1742 (selected (org-export--selected-trees data options
))
1745 ;; Collect ignored elements or objects into IGNORE-LIST.
1746 (let ((type (org-element-type data
)))
1747 (if (org-export--skip-p data options selected
) (push data ignore
)
1748 (if (and (eq type
'headline
)
1749 (eq (plist-get options
:with-archived-trees
) 'headline
)
1750 (org-element-property :archivedp data
))
1751 ;; If headline is archived but tree below has
1752 ;; to be skipped, add it to ignore list.
1753 (mapc (lambda (e) (push e ignore
))
1754 (org-element-contents data
))
1755 ;; Move into secondary string, if any.
1757 (cdr (assq type org-element-secondary-value-alist
))))
1759 (mapc walk-data
(org-element-property sec-prop data
))))
1760 ;; Move into recursive objects/elements.
1761 (mapc walk-data
(org-element-contents data
))))))))
1763 (funcall walk-data data
)
1767 (defun org-export--selected-trees (data info
)
1768 "Return list of headlines containing a select tag in their tree.
1769 DATA is parsed data as returned by `org-element-parse-buffer'.
1770 INFO is a plist holding export options."
1771 (let* (selected-trees
1772 walk-data
; for byte-compiler.
1775 (lambda (data genealogy
)
1776 (case (org-element-type data
)
1777 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1778 (org-element-contents data
)))
1780 (let ((tags (org-element-property :tags data
)))
1781 (if (loop for tag in
(plist-get info
:select-tags
)
1782 thereis
(member tag tags
))
1783 ;; When a select tag is found, mark full
1784 ;; genealogy and every headline within the tree
1786 (setq selected-trees
1789 (org-element-map data
'headline
'identity
)
1791 ;; Else, continue searching in tree, recursively.
1793 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1794 (org-element-contents data
))))))))))
1795 (funcall walk-data data nil
) selected-trees
))
1797 (defun org-export--skip-p (blob options selected
)
1798 "Non-nil when element or object BLOB should be skipped during export.
1799 OPTIONS is the plist holding export options. SELECTED, when
1800 non-nil, is a list of headlines belonging to a tree with a select
1802 (case (org-element-type blob
)
1803 (clock (not (plist-get options
:with-clocks
)))
1805 (or (not (plist-get options
:with-drawers
))
1806 (and (consp (plist-get options
:with-drawers
))
1807 (not (member (org-element-property :drawer-name blob
)
1808 (plist-get options
:with-drawers
))))))
1810 (let ((with-tasks (plist-get options
:with-tasks
))
1811 (todo (org-element-property :todo-keyword blob
))
1812 (todo-type (org-element-property :todo-type blob
))
1813 (archived (plist-get options
:with-archived-trees
))
1814 (tags (org-element-property :tags blob
)))
1816 ;; Ignore subtrees with an exclude tag.
1817 (loop for k in
(plist-get options
:exclude-tags
)
1818 thereis
(member k tags
))
1819 ;; When a select tag is present in the buffer, ignore any tree
1821 (and selected
(not (memq blob selected
)))
1822 ;; Ignore commented sub-trees.
1823 (org-element-property :commentedp blob
)
1824 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1825 (and (not archived
) (org-element-property :archivedp blob
))
1826 ;; Ignore tasks, if specified by `:with-tasks' property.
1828 (or (not with-tasks
)
1829 (and (memq with-tasks
'(todo done
))
1830 (not (eq todo-type with-tasks
)))
1831 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1832 (inlinetask (not (plist-get options
:with-inlinetasks
)))
1833 (planning (not (plist-get options
:with-plannings
)))
1834 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1836 (and (org-export-table-has-special-column-p
1837 (org-export-get-parent-table blob
))
1838 (not (org-export-get-previous-element blob options
))))
1839 (table-row (org-export-table-row-is-special-p blob options
))
1841 (case (plist-get options
:with-timestamps
)
1842 ;; No timestamp allowed.
1844 ;; Only active timestamps allowed and the current one isn't
1847 (not (memq (org-element-property :type blob
)
1848 '(active active-range
))))
1849 ;; Only inactive timestamps allowed and the current one isn't
1852 (not (memq (org-element-property :type blob
)
1853 '(inactive inactive-range
))))))))
1859 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1860 ;; `org-element-parse-buffer') and transcodes it into a specified
1861 ;; back-end output. It takes care of filtering out elements or
1862 ;; objects according to export options and organizing the output blank
1863 ;; lines and white space are preserved. The function memoizes its
1864 ;; results, so it is cheap to call it within translators.
1866 ;; Internally, three functions handle the filtering of objects and
1867 ;; elements during the export. In particular,
1868 ;; `org-export-ignore-element' marks an element or object so future
1869 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1870 ;; elements or objects should be seen as real Org syntax and
1871 ;; `org-export-expand' transforms the others back into their original
1874 ;; `org-export-transcoder' is an accessor returning appropriate
1875 ;; translator function for a given element or object.
1877 (defun org-export-transcoder (blob info
)
1878 "Return appropriate transcoder for BLOB.
1879 INFO is a plist containing export directives."
1880 (let ((type (org-element-type blob
)))
1881 ;; Return contents only for complete parse trees.
1882 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1883 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1884 (and (functionp transcoder
) transcoder
)))))
1886 (defun org-export-data (data info
)
1887 "Convert DATA into current back-end format.
1889 DATA is a parse tree, an element or an object or a secondary
1890 string. INFO is a plist holding export options.
1892 Return transcoded string."
1893 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
1894 (if (not (eq memo
'no-memo
)) memo
1895 (let* ((type (org-element-type data
))
1898 ;; Ignored element/object.
1899 ((memq data
(plist-get info
:ignore-list
)) nil
)
1900 ;; Plain text. All residual text properties from parse
1901 ;; tree (i.e. `:parent' property) are removed.
1902 ((eq type
'plain-text
)
1904 (org-export-filter-apply-functions
1905 (plist-get info
:filter-plain-text
)
1906 (let ((transcoder (org-export-transcoder data info
)))
1907 (if transcoder
(funcall transcoder data info
) data
))
1909 ;; Uninterpreted element/object: change it back to Org
1910 ;; syntax and export again resulting raw string.
1911 ((not (org-export--interpret-p data info
))
1915 (mapconcat (lambda (blob) (org-export-data blob info
))
1916 (org-element-contents data
)
1919 ;; Secondary string.
1921 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1922 ;; Element/Object without contents or, as a special case,
1923 ;; headline with archive tag and archived trees restricted
1925 ((or (not (org-element-contents data
))
1926 (and (eq type
'headline
)
1927 (eq (plist-get info
:with-archived-trees
) 'headline
)
1928 (org-element-property :archivedp data
)))
1929 (let ((transcoder (org-export-transcoder data info
)))
1930 (and (functionp transcoder
)
1931 (funcall transcoder data nil info
))))
1932 ;; Element/Object with contents.
1934 (let ((transcoder (org-export-transcoder data info
)))
1936 (let* ((greaterp (memq type org-element-greater-elements
))
1939 (memq type org-element-recursive-objects
)))
1942 (lambda (element) (org-export-data element info
))
1943 (org-element-contents
1944 (if (or greaterp objectp
) data
1945 ;; Elements directly containing objects
1946 ;; must have their indentation normalized
1948 (org-element-normalize-contents
1950 ;; When normalizing contents of the first
1951 ;; paragraph in an item or a footnote
1952 ;; definition, ignore first line's
1953 ;; indentation: there is none and it
1954 ;; might be misleading.
1955 (when (eq type
'paragraph
)
1956 (let ((parent (org-export-get-parent data
)))
1958 (eq (car (org-element-contents parent
))
1960 (memq (org-element-type parent
)
1961 '(footnote-definition item
))))))))
1963 (funcall transcoder data
1964 (if (not greaterp
) contents
1965 (org-element-normalize-string contents
))
1967 ;; Final result will be memoized before being returned.
1972 ((memq type
'(org-data plain-text nil
)) results
)
1973 ;; Append the same white space between elements or objects as in
1974 ;; the original buffer, and call appropriate filters.
1977 (org-export-filter-apply-functions
1978 (plist-get info
(intern (format ":filter-%s" type
)))
1979 (let ((post-blank (or (org-element-property :post-blank data
)
1981 (if (memq type org-element-all-elements
)
1982 (concat (org-element-normalize-string results
)
1983 (make-string post-blank ?
\n))
1984 (concat results
(make-string post-blank ?
))))
1987 (plist-get info
:exported-data
))))))
1989 (defun org-export--interpret-p (blob info
)
1990 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1991 Check is done according to export options INFO, stored as
1993 (case (org-element-type blob
)
1995 (entity (plist-get info
:with-entities
))
1997 (emphasis (plist-get info
:with-emphasize
))
1998 ;; ... fixed-width areas.
1999 (fixed-width (plist-get info
:with-fixed-width
))
2001 ((footnote-definition footnote-reference
)
2002 (plist-get info
:with-footnotes
))
2003 ;; ... sub/superscripts...
2004 ((subscript superscript
)
2005 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
2006 (if (eq sub
/super-p
'{})
2007 (org-element-property :use-brackets-p blob
)
2010 (table (plist-get info
:with-tables
))
2013 (defun org-export-expand (blob contents
)
2014 "Expand a parsed element or object to its original state.
2015 BLOB is either an element or an object. CONTENTS is its
2016 contents, as a string or nil."
2018 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
2021 (defun org-export-ignore-element (element info
)
2022 "Add ELEMENT to `:ignore-list' in INFO.
2024 Any element in `:ignore-list' will be skipped when using
2025 `org-element-map'. INFO is modified by side effects."
2026 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2030 ;;; The Filter System
2032 ;; Filters allow end-users to tweak easily the transcoded output.
2033 ;; They are the functional counterpart of hooks, as every filter in
2034 ;; a set is applied to the return value of the previous one.
2036 ;; Every set is back-end agnostic. Although, a filter is always
2037 ;; called, in addition to the string it applies to, with the back-end
2038 ;; used as argument, so it's easy for the end-user to add back-end
2039 ;; specific filters in the set. The communication channel, as
2040 ;; a plist, is required as the third argument.
2042 ;; From the developer side, filters sets can be installed in the
2043 ;; process with the help of `org-export-define-backend', which
2044 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2045 ;; association has a key among the following symbols and a function or
2046 ;; a list of functions as value.
2048 ;; - `:filter-parse-tree' applies directly on the complete parsed
2049 ;; tree. It's the only filters set that doesn't apply to a string.
2050 ;; Users can set it through `org-export-filter-parse-tree-functions'
2053 ;; - `:filter-final-output' applies to the final transcoded string.
2054 ;; Users can set it with `org-export-filter-final-output-functions'
2057 ;; - `:filter-plain-text' applies to any string not recognized as Org
2058 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2061 ;; - `:filter-TYPE' applies on the string returned after an element or
2062 ;; object of type TYPE has been transcoded. An user can modify
2063 ;; `org-export-filter-TYPE-functions'
2065 ;; All filters sets are applied with
2066 ;; `org-export-filter-apply-functions' function. Filters in a set are
2067 ;; applied in a LIFO fashion. It allows developers to be sure that
2068 ;; their filters will be applied first.
2070 ;; Filters properties are installed in communication channel with
2071 ;; `org-export-install-filters' function.
2073 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2074 ;; before parsing to allow for heavy structure modifications.
2077 ;;;; Before Parsing Hook
2079 (defvar org-export-before-parsing-hook nil
2080 "Hook run before parsing an export buffer.
2082 This is run after include keywords have been expanded and Babel
2083 code executed, on a copy of original buffer's area being
2084 exported. Visibility is the same as in the original one. Point
2085 is left at the beginning of the new one.
2087 Every function in this hook will be called with one argument: the
2088 back-end currently used, as a symbol.")
2091 ;;;; Special Filters
2093 (defvar org-export-filter-parse-tree-functions nil
2094 "List of functions applied to the parsed tree.
2095 Each filter is called with three arguments: the parse tree, as
2096 returned by `org-element-parse-buffer', the back-end, as
2097 a symbol, and the communication channel, as a plist. It must
2098 return the modified parse tree to transcode.")
2100 (defvar org-export-filter-final-output-functions nil
2101 "List of functions applied to the transcoded string.
2102 Each filter is called with three arguments: the full transcoded
2103 string, the back-end, as a symbol, and the communication channel,
2104 as a plist. It must return a string that will be used as the
2105 final export output.")
2107 (defvar org-export-filter-plain-text-functions nil
2108 "List of functions applied to plain text.
2109 Each filter is called with three arguments: a string which
2110 contains no Org syntax, the back-end, as a symbol, and the
2111 communication channel, as a plist. It must return a string or
2115 ;;;; Elements Filters
2117 (defvar org-export-filter-babel-call-functions nil
2118 "List of functions applied to a transcoded babel-call.
2119 Each filter is called with three arguments: the transcoded data,
2120 as a string, the back-end, as a symbol, and the communication
2121 channel, as a plist. It must return a string or nil.")
2123 (defvar org-export-filter-center-block-functions nil
2124 "List of functions applied to a transcoded center block.
2125 Each filter is called with three arguments: the transcoded data,
2126 as a string, the back-end, as a symbol, and the communication
2127 channel, as a plist. It must return a string or nil.")
2129 (defvar org-export-filter-clock-functions nil
2130 "List of functions applied to a transcoded clock.
2131 Each filter is called with three arguments: the transcoded data,
2132 as a string, the back-end, as a symbol, and the communication
2133 channel, as a plist. It must return a string or nil.")
2135 (defvar org-export-filter-comment-functions nil
2136 "List of functions applied to a transcoded comment.
2137 Each filter is called with three arguments: the transcoded data,
2138 as a string, the back-end, as a symbol, and the communication
2139 channel, as a plist. It must return a string or nil.")
2141 (defvar org-export-filter-comment-block-functions nil
2142 "List of functions applied to a transcoded comment-block.
2143 Each filter is called with three arguments: the transcoded data,
2144 as a string, the back-end, as a symbol, and the communication
2145 channel, as a plist. It must return a string or nil.")
2147 (defvar org-export-filter-diary-sexp-functions nil
2148 "List of functions applied to a transcoded diary-sexp.
2149 Each filter is called with three arguments: the transcoded data,
2150 as a string, the back-end, as a symbol, and the communication
2151 channel, as a plist. It must return a string or nil.")
2153 (defvar org-export-filter-drawer-functions nil
2154 "List of functions applied to a transcoded drawer.
2155 Each filter is called with three arguments: the transcoded data,
2156 as a string, the back-end, as a symbol, and the communication
2157 channel, as a plist. It must return a string or nil.")
2159 (defvar org-export-filter-dynamic-block-functions nil
2160 "List of functions applied to a transcoded dynamic-block.
2161 Each filter is called with three arguments: the transcoded data,
2162 as a string, the back-end, as a symbol, and the communication
2163 channel, as a plist. It must return a string or nil.")
2165 (defvar org-export-filter-example-block-functions nil
2166 "List of functions applied to a transcoded example-block.
2167 Each filter is called with three arguments: the transcoded data,
2168 as a string, the back-end, as a symbol, and the communication
2169 channel, as a plist. It must return a string or nil.")
2171 (defvar org-export-filter-export-block-functions nil
2172 "List of functions applied to a transcoded export-block.
2173 Each filter is called with three arguments: the transcoded data,
2174 as a string, the back-end, as a symbol, and the communication
2175 channel, as a plist. It must return a string or nil.")
2177 (defvar org-export-filter-fixed-width-functions nil
2178 "List of functions applied to a transcoded fixed-width.
2179 Each filter is called with three arguments: the transcoded data,
2180 as a string, the back-end, as a symbol, and the communication
2181 channel, as a plist. It must return a string or nil.")
2183 (defvar org-export-filter-footnote-definition-functions nil
2184 "List of functions applied to a transcoded footnote-definition.
2185 Each filter is called with three arguments: the transcoded data,
2186 as a string, the back-end, as a symbol, and the communication
2187 channel, as a plist. It must return a string or nil.")
2189 (defvar org-export-filter-headline-functions nil
2190 "List of functions applied to a transcoded headline.
2191 Each filter is called with three arguments: the transcoded data,
2192 as a string, the back-end, as a symbol, and the communication
2193 channel, as a plist. It must return a string or nil.")
2195 (defvar org-export-filter-horizontal-rule-functions nil
2196 "List of functions applied to a transcoded horizontal-rule.
2197 Each filter is called with three arguments: the transcoded data,
2198 as a string, the back-end, as a symbol, and the communication
2199 channel, as a plist. It must return a string or nil.")
2201 (defvar org-export-filter-inlinetask-functions nil
2202 "List of functions applied to a transcoded inlinetask.
2203 Each filter is called with three arguments: the transcoded data,
2204 as a string, the back-end, as a symbol, and the communication
2205 channel, as a plist. It must return a string or nil.")
2207 (defvar org-export-filter-item-functions nil
2208 "List of functions applied to a transcoded item.
2209 Each filter is called with three arguments: the transcoded data,
2210 as a string, the back-end, as a symbol, and the communication
2211 channel, as a plist. It must return a string or nil.")
2213 (defvar org-export-filter-keyword-functions nil
2214 "List of functions applied to a transcoded keyword.
2215 Each filter is called with three arguments: the transcoded data,
2216 as a string, the back-end, as a symbol, and the communication
2217 channel, as a plist. It must return a string or nil.")
2219 (defvar org-export-filter-latex-environment-functions nil
2220 "List of functions applied to a transcoded latex-environment.
2221 Each filter is called with three arguments: the transcoded data,
2222 as a string, the back-end, as a symbol, and the communication
2223 channel, as a plist. It must return a string or nil.")
2225 (defvar org-export-filter-node-property-functions nil
2226 "List of functions applied to a transcoded node-property.
2227 Each filter is called with three arguments: the transcoded data,
2228 as a string, the back-end, as a symbol, and the communication
2229 channel, as a plist. It must return a string or nil.")
2231 (defvar org-export-filter-paragraph-functions nil
2232 "List of functions applied to a transcoded paragraph.
2233 Each filter is called with three arguments: the transcoded data,
2234 as a string, the back-end, as a symbol, and the communication
2235 channel, as a plist. It must return a string or nil.")
2237 (defvar org-export-filter-plain-list-functions nil
2238 "List of functions applied to a transcoded plain-list.
2239 Each filter is called with three arguments: the transcoded data,
2240 as a string, the back-end, as a symbol, and the communication
2241 channel, as a plist. It must return a string or nil.")
2243 (defvar org-export-filter-planning-functions nil
2244 "List of functions applied to a transcoded planning.
2245 Each filter is called with three arguments: the transcoded data,
2246 as a string, the back-end, as a symbol, and the communication
2247 channel, as a plist. It must return a string or nil.")
2249 (defvar org-export-filter-property-drawer-functions nil
2250 "List of functions applied to a transcoded property-drawer.
2251 Each filter is called with three arguments: the transcoded data,
2252 as a string, the back-end, as a symbol, and the communication
2253 channel, as a plist. It must return a string or nil.")
2255 (defvar org-export-filter-quote-block-functions nil
2256 "List of functions applied to a transcoded quote block.
2257 Each filter is called with three arguments: the transcoded quote
2258 data, as a string, the back-end, as a symbol, and the
2259 communication channel, as a plist. It must return a string or
2262 (defvar org-export-filter-quote-section-functions nil
2263 "List of functions applied to a transcoded quote-section.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-section-functions nil
2269 "List of functions applied to a transcoded section.
2270 Each filter is called with three arguments: the transcoded data,
2271 as a string, the back-end, as a symbol, and the communication
2272 channel, as a plist. It must return a string or nil.")
2274 (defvar org-export-filter-special-block-functions nil
2275 "List of functions applied to a transcoded special block.
2276 Each filter is called with three arguments: the transcoded data,
2277 as a string, the back-end, as a symbol, and the communication
2278 channel, as a plist. It must return a string or nil.")
2280 (defvar org-export-filter-src-block-functions nil
2281 "List of functions applied to a transcoded src-block.
2282 Each filter is called with three arguments: the transcoded data,
2283 as a string, the back-end, as a symbol, and the communication
2284 channel, as a plist. It must return a string or nil.")
2286 (defvar org-export-filter-table-functions nil
2287 "List of functions applied to a transcoded table.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2292 (defvar org-export-filter-table-cell-functions nil
2293 "List of functions applied to a transcoded table-cell.
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-table-row-functions nil
2299 "List of functions applied to a transcoded table-row.
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-verse-block-functions nil
2305 "List of functions applied to a transcoded verse block.
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.")
2311 ;;;; Objects Filters
2313 (defvar org-export-filter-bold-functions nil
2314 "List of functions applied to transcoded bold text.
2315 Each filter is called with three arguments: the transcoded data,
2316 as a string, the back-end, as a symbol, and the communication
2317 channel, as a plist. It must return a string or nil.")
2319 (defvar org-export-filter-code-functions nil
2320 "List of functions applied to transcoded code text.
2321 Each filter is called with three arguments: the transcoded data,
2322 as a string, the back-end, as a symbol, and the communication
2323 channel, as a plist. It must return a string or nil.")
2325 (defvar org-export-filter-entity-functions nil
2326 "List of functions applied to a transcoded entity.
2327 Each filter is called with three arguments: the transcoded data,
2328 as a string, the back-end, as a symbol, and the communication
2329 channel, as a plist. It must return a string or nil.")
2331 (defvar org-export-filter-export-snippet-functions nil
2332 "List of functions applied to a transcoded export-snippet.
2333 Each filter is called with three arguments: the transcoded data,
2334 as a string, the back-end, as a symbol, and the communication
2335 channel, as a plist. It must return a string or nil.")
2337 (defvar org-export-filter-footnote-reference-functions nil
2338 "List of functions applied to a transcoded footnote-reference.
2339 Each filter is called with three arguments: the transcoded data,
2340 as a string, the back-end, as a symbol, and the communication
2341 channel, as a plist. It must return a string or nil.")
2343 (defvar org-export-filter-inline-babel-call-functions nil
2344 "List of functions applied to a transcoded inline-babel-call.
2345 Each filter is called with three arguments: the transcoded data,
2346 as a string, the back-end, as a symbol, and the communication
2347 channel, as a plist. It must return a string or nil.")
2349 (defvar org-export-filter-inline-src-block-functions nil
2350 "List of functions applied to a transcoded inline-src-block.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-italic-functions nil
2356 "List of functions applied to transcoded italic text.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-latex-fragment-functions nil
2362 "List of functions applied to a transcoded latex-fragment.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-line-break-functions nil
2368 "List of functions applied to a transcoded line-break.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-link-functions nil
2374 "List of functions applied to a transcoded link.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-macro-functions nil
2380 "List of functions applied to a transcoded macro.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-radio-target-functions nil
2386 "List of functions applied to a transcoded radio-target.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2391 (defvar org-export-filter-statistics-cookie-functions nil
2392 "List of functions applied to a transcoded statistics-cookie.
2393 Each filter is called with three arguments: the transcoded data,
2394 as a string, the back-end, as a symbol, and the communication
2395 channel, as a plist. It must return a string or nil.")
2397 (defvar org-export-filter-strike-through-functions nil
2398 "List of functions applied to transcoded strike-through text.
2399 Each filter is called with three arguments: the transcoded data,
2400 as a string, the back-end, as a symbol, and the communication
2401 channel, as a plist. It must return a string or nil.")
2403 (defvar org-export-filter-subscript-functions nil
2404 "List of functions applied to a transcoded subscript.
2405 Each filter is called with three arguments: the transcoded data,
2406 as a string, the back-end, as a symbol, and the communication
2407 channel, as a plist. It must return a string or nil.")
2409 (defvar org-export-filter-superscript-functions nil
2410 "List of functions applied to a transcoded superscript.
2411 Each filter is called with three arguments: the transcoded data,
2412 as a string, the back-end, as a symbol, and the communication
2413 channel, as a plist. It must return a string or nil.")
2415 (defvar org-export-filter-target-functions nil
2416 "List of functions applied to a transcoded target.
2417 Each filter is called with three arguments: the transcoded data,
2418 as a string, the back-end, as a symbol, and the communication
2419 channel, as a plist. It must return a string or nil.")
2421 (defvar org-export-filter-timestamp-functions nil
2422 "List of functions applied to a transcoded timestamp.
2423 Each filter is called with three arguments: the transcoded data,
2424 as a string, the back-end, as a symbol, and the communication
2425 channel, as a plist. It must return a string or nil.")
2427 (defvar org-export-filter-underline-functions nil
2428 "List of functions applied to transcoded underline text.
2429 Each filter is called with three arguments: the transcoded data,
2430 as a string, the back-end, as a symbol, and the communication
2431 channel, as a plist. It must return a string or nil.")
2433 (defvar org-export-filter-verbatim-functions nil
2434 "List of functions applied to transcoded verbatim text.
2435 Each filter is called with three arguments: the transcoded data,
2436 as a string, the back-end, as a symbol, and the communication
2437 channel, as a plist. It must return a string or nil.")
2442 ;; Internal function `org-export-install-filters' installs filters
2443 ;; hard-coded in back-ends (developer filters) and filters from global
2444 ;; variables (user filters) in the communication channel.
2446 ;; Internal function `org-export-filter-apply-functions' takes care
2447 ;; about applying each filter in order to a given data. It ignores
2448 ;; filters returning a nil value but stops whenever a filter returns
2451 (defun org-export-filter-apply-functions (filters value info
)
2452 "Call every function in FILTERS.
2454 Functions are called with arguments VALUE, current export
2455 back-end and INFO. A function returning a nil value will be
2456 skipped. If it returns the empty string, the process ends and
2459 Call is done in a LIFO fashion, to be sure that developer
2460 specified filters, if any, are called first."
2462 (dolist (filter filters value
)
2463 (let ((result (funcall filter value
(plist-get info
:back-end
) info
)))
2464 (cond ((not result
) value
)
2465 ((equal value
"") (throw 'exit nil
))
2466 (t (setq value result
)))))))
2468 (defun org-export-install-filters (info)
2469 "Install filters properties in communication channel.
2471 INFO is a plist containing the current communication channel.
2473 Return the updated communication channel."
2475 ;; Install user defined filters with `org-export-filters-alist'.
2477 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2478 org-export-filters-alist
)
2479 ;; Prepend back-end specific filters to that list.
2480 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2481 (plist-get info
:back-end
)))))
2482 (when (boundp back-end-filters
)
2484 ;; Single values get consed, lists are prepended.
2485 (let ((key (car p
)) (value (cdr p
)))
2490 (if (atom value
) (cons value
(plist-get plist key
))
2491 (append value
(plist-get plist key
))))))))
2492 (eval back-end-filters
))))
2493 ;; Return new communication channel.
2494 (org-combine-plists info plist
)))
2500 ;; This is the room for the main function, `org-export-as', along with
2501 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2502 ;; They differ only by the way they output the resulting code.
2504 ;; `org-export-output-file-name' is an auxiliary function meant to be
2505 ;; used with `org-export-to-file'. With a given extension, it tries
2506 ;; to provide a canonical file name to write export output to.
2508 ;; Note that `org-export-as' doesn't really parse the current buffer,
2509 ;; but a copy of it (with the same buffer-local variables and
2510 ;; visibility), where macros and include keywords are expanded and
2511 ;; Babel blocks are executed, if appropriate.
2512 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2514 ;; File inclusion is taken care of by
2515 ;; `org-export-expand-include-keyword' and
2516 ;; `org-export--prepare-file-contents'. Structure wise, including
2517 ;; a whole Org file in a buffer often makes little sense. For
2518 ;; example, if the file contains an headline and the include keyword
2519 ;; was within an item, the item should contain the headline. That's
2520 ;; why file inclusion should be done before any structure can be
2521 ;; associated to the file, that is before parsing.
2523 ;; Macro are expanded with `org-export-expand-macro'.
2525 (defun org-export-as
2526 (backend &optional subtreep visible-only body-only ext-plist noexpand
)
2527 "Transcode current Org buffer into BACKEND code.
2529 If narrowing is active in the current buffer, only transcode its
2532 If a region is active, transcode that region.
2534 When optional argument SUBTREEP is non-nil, transcode the
2535 sub-tree at point, extracting information from the headline
2538 When optional argument VISIBLE-ONLY is non-nil, don't export
2539 contents of hidden elements.
2541 When optional argument BODY-ONLY is non-nil, only return body
2542 code, without preamble nor postamble.
2544 Optional argument EXT-PLIST, when provided, is a property list
2545 with external parameters overriding Org default settings, but
2546 still inferior to file-local settings.
2548 Optional argument NOEXPAND, when non-nil, prevents included files
2549 to be expanded and Babel code to be executed.
2551 Return code as a string."
2552 ;; Barf if BACKEND isn't registered.
2553 (org-export-barf-if-invalid-backend backend
)
2556 ;; Narrow buffer to an appropriate region or subtree for
2557 ;; parsing. If parsing subtree, be sure to remove main headline
2559 (cond ((org-region-active-p)
2560 (narrow-to-region (region-beginning) (region-end)))
2562 (org-narrow-to-subtree)
2563 (goto-char (point-min))
2565 (narrow-to-region (point) (point-max))))
2566 ;; Install user's and developer's filters in communication
2569 (org-export-with-current-buffer-copy
2570 ;; Update communication channel and get parse tree. Buffer
2571 ;; isn't parsed directly. Instead, a temporary copy is
2572 ;; created, where include keywords, macros are expanded and
2573 ;; code blocks are evaluated.
2575 (org-export-expand-include-keyword)
2576 ;; Update macro templates since #+INCLUDE keywords might
2577 ;; have added some new ones.
2578 (org-macro-initialize-templates)
2579 (org-macro-replace-all org-macro-templates
)
2580 ;; TODO: Setting `org-current-export-file' is required by
2581 ;; Org Babel to properly resolve noweb references. Once
2582 ;; "org-exp.el" is removed, modify
2583 ;; `org-export-blocks-preprocess' so it accepts the value
2584 ;; as an argument instead.
2585 (let ((org-current-export-file (current-buffer)))
2586 (org-export-blocks-preprocess)))
2587 ;; Update radio targets since keyword inclusion might have
2589 (org-update-radio-target-regexp)
2590 ;; Run hook `org-export-before-parsing-hook'. with current
2591 ;; back-end as argument.
2592 (goto-char (point-min))
2593 (run-hook-with-args 'org-export-before-parsing-hook backend
)
2594 ;; Initialize communication channel.
2596 (org-export-install-filters
2597 (org-export-get-environment backend subtreep ext-plist
)))
2598 ;; Expand export-specific set of macros: {{{author}}},
2599 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2600 ;; once regular macros have been expanded, since document
2601 ;; keywords may contain one of them.
2603 (org-macro-replace-all
2604 (list (cons "author"
2605 (org-element-interpret-data (plist-get info
:author
)))
2607 (org-element-interpret-data (plist-get info
:date
)))
2608 ;; EMAIL is not a parsed keyword: store it as-is.
2609 (cons "email" (or (plist-get info
:email
) ""))
2611 (org-element-interpret-data (plist-get info
:title
))))))
2612 ;; Eventually parse buffer. Call parse-tree filters to get
2615 (org-export-filter-apply-functions
2616 (plist-get info
:filter-parse-tree
)
2617 (org-element-parse-buffer nil visible-only
) info
)))
2618 ;; Now tree is complete, compute its properties and add them
2619 ;; to communication channel.
2622 info
(org-export-collect-tree-properties tree info
)))
2623 ;; Eventually transcode TREE. Wrap the resulting string into
2624 ;; a template, if required. Finally call final-output filter.
2625 (let* ((body (org-element-normalize-string (org-export-data tree info
)))
2626 (template (cdr (assq 'template
2627 (plist-get info
:translate-alist
))))
2628 (output (org-export-filter-apply-functions
2629 (plist-get info
:filter-final-output
)
2630 (if (or (not (functionp template
)) body-only
) body
2631 (funcall template body info
))
2633 ;; Maybe add final OUTPUT to kill ring, then return it.
2634 (when org-export-copy-to-kill-ring
(org-kill-new output
))
2637 (defun org-export-to-buffer
2638 (backend buffer
&optional subtreep visible-only body-only ext-plist noexpand
)
2639 "Call `org-export-as' with output to a specified buffer.
2641 BACKEND is the back-end used for transcoding, as a symbol.
2643 BUFFER is the output buffer. If it already exists, it will be
2644 erased first, otherwise, it will be created.
2646 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2647 and NOEXPAND are similar to those used in `org-export-as', which
2651 (let ((out (org-export-as
2652 backend subtreep visible-only body-only ext-plist noexpand
))
2653 (buffer (get-buffer-create buffer
)))
2654 (with-current-buffer buffer
2657 (goto-char (point-min)))
2660 (defun org-export-to-file
2661 (backend file
&optional subtreep visible-only body-only ext-plist noexpand
)
2662 "Call `org-export-as' with output to a specified file.
2664 BACKEND is the back-end used for transcoding, as a symbol. FILE
2665 is the name of the output file, as a string.
2667 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2668 and NOEXPAND are similar to those used in `org-export-as', which
2671 Return output file's name."
2672 ;; Checks for FILE permissions. `write-file' would do the same, but
2673 ;; we'd rather avoid needless transcoding of parse tree.
2674 (unless (file-writable-p file
) (error "Output file not writable"))
2675 ;; Insert contents to a temporary buffer and write it to FILE.
2676 (let ((out (org-export-as
2677 backend subtreep visible-only body-only ext-plist noexpand
)))
2680 (let ((coding-system-for-write org-export-coding-system
))
2681 (write-file file
))))
2682 ;; Return full path.
2685 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2686 "Return output file's name according to buffer specifications.
2688 EXTENSION is a string representing the output file extension,
2689 with the leading dot.
2691 With a non-nil optional argument SUBTREEP, try to determine
2692 output file's name by looking for \"EXPORT_FILE_NAME\" property
2693 of subtree at point.
2695 When optional argument PUB-DIR is set, use it as the publishing
2698 When optional argument VISIBLE-ONLY is non-nil, don't export
2699 contents of hidden elements.
2701 Return file name as a string, or nil if it couldn't be
2704 ;; File name may come from EXPORT_FILE_NAME subtree property,
2705 ;; assuming point is at beginning of said sub-tree.
2706 (file-name-sans-extension
2710 (ignore-errors (org-back-to-heading) (point)))
2711 "EXPORT_FILE_NAME" t
))
2712 ;; File name may be extracted from buffer's associated
2714 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2715 (and visited-file
(file-name-nondirectory visited-file
)))
2716 ;; Can't determine file name on our own: Ask user.
2717 (let ((read-file-name-function
2718 (and org-completion-use-ido
'ido-read-file-name
)))
2720 "Output file: " pub-dir nil nil nil
2722 (string= (file-name-extension name t
) extension
))))))))
2723 ;; Build file name. Enforce EXTENSION over whatever user may have
2724 ;; come up with. PUB-DIR, if defined, always has precedence over
2725 ;; any provided path.
2728 (concat (file-name-as-directory pub-dir
)
2729 (file-name-nondirectory base-name
)
2731 ((file-name-absolute-p base-name
) (concat base-name extension
))
2732 (t (concat (file-name-as-directory ".") base-name extension
)))))
2734 (defmacro org-export-with-current-buffer-copy
(&rest body
)
2735 "Apply BODY in a copy of the current buffer.
2737 The copy preserves local variables and visibility of the original
2740 Point is at buffer's beginning when BODY is applied."
2741 (declare (debug (body)))
2742 (org-with-gensyms (original-buffer offset buffer-string overlays region
)
2743 `(let* ((,original-buffer
(current-buffer))
2744 (,region
(list (point-min) (point-max)))
2745 (,buffer-string
(org-with-wide-buffer (buffer-string)))
2746 (,overlays
(mapcar 'copy-overlay
(apply 'overlays-in
,region
))))
2748 (let ((buffer-invisibility-spec nil
))
2749 (org-clone-local-variables
2751 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2752 (insert ,buffer-string
)
2753 (apply 'narrow-to-region
,region
)
2756 ov
(overlay-start ov
) (overlay-end ov
) (current-buffer)))
2758 (goto-char (point-min))
2761 (defun org-export-expand-include-keyword (&optional included dir
)
2762 "Expand every include keyword in buffer.
2763 Optional argument INCLUDED is a list of included file names along
2764 with their line restriction, when appropriate. It is used to
2765 avoid infinite recursion. Optional argument DIR is the current
2766 working directory. It is used to properly resolve relative
2768 (let ((case-fold-search t
))
2769 (goto-char (point-min))
2770 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t
)
2771 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2774 ;; Extract arguments from keyword's value.
2775 (let* ((value (match-string 1))
2776 (ind (org-get-indentation))
2777 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
2778 (prog1 (expand-file-name (match-string 1 value
) dir
)
2779 (setq value
(replace-match "" nil nil value
)))))
2782 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
2783 (prog1 (match-string 1 value
)
2784 (setq value
(replace-match "" nil nil value
)))))
2785 (env (cond ((string-match "\\<example\\>" value
) 'example
)
2786 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
2787 (match-string 1 value
))))
2788 ;; Minimal level of included file defaults to the child
2789 ;; level of the current headline, if any, or one. It
2790 ;; only applies is the file is meant to be included as
2794 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
2795 (prog1 (string-to-number (match-string 1 value
))
2796 (setq value
(replace-match "" nil nil value
)))
2797 (let ((cur (org-current-level)))
2798 (if cur
(1+ (org-reduced-level cur
)) 1))))))
2800 (delete-region (point) (progn (forward-line) (point)))
2802 ((not file
) (error "Invalid syntax in INCLUDE keyword"))
2803 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
2804 ;; Check if files has already been parsed. Look after
2805 ;; inclusion lines too, as different parts of the same file
2806 ;; can be included too.
2807 ((member (list file lines
) included
)
2808 (error "Recursive file inclusion: %s" file
))
2813 (let ((ind-str (make-string ind ?
))
2815 (org-escape-code-in-string
2816 (org-export--prepare-file-contents file lines
))))
2817 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2818 ind-str contents ind-str
))))
2821 (let ((ind-str (make-string ind ?
))
2823 (org-escape-code-in-string
2824 (org-export--prepare-file-contents file lines
))))
2825 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2826 ind-str env contents ind-str
))))
2832 (org-export--prepare-file-contents file lines ind minlevel
))
2833 (org-export-expand-include-keyword
2834 (cons (list file lines
) included
)
2835 (file-name-directory file
))
2836 (buffer-string))))))))))))
2838 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
2839 "Prepare the contents of FILE for inclusion and return them as a string.
2841 When optional argument LINES is a string specifying a range of
2842 lines, include only those lines.
2844 Optional argument IND, when non-nil, is an integer specifying the
2845 global indentation of returned contents. Since its purpose is to
2846 allow an included file to stay in the same environment it was
2847 created \(i.e. a list item), it doesn't apply past the first
2848 headline encountered.
2850 Optional argument MINLEVEL, when non-nil, is an integer
2851 specifying the level that any top-level headline in the included
2854 (insert-file-contents file
)
2856 (let* ((lines (split-string lines
"-"))
2857 (lbeg (string-to-number (car lines
)))
2858 (lend (string-to-number (cadr lines
)))
2859 (beg (if (zerop lbeg
) (point-min)
2860 (goto-char (point-min))
2861 (forward-line (1- lbeg
))
2863 (end (if (zerop lend
) (point-max)
2864 (goto-char (point-min))
2865 (forward-line (1- lend
))
2867 (narrow-to-region beg end
)))
2868 ;; Remove blank lines at beginning and end of contents. The logic
2869 ;; behind that removal is that blank lines around include keyword
2870 ;; override blank lines in included file.
2871 (goto-char (point-min))
2872 (org-skip-whitespace)
2874 (delete-region (point-min) (point))
2875 (goto-char (point-max))
2876 (skip-chars-backward " \r\t\n")
2878 (delete-region (point) (point-max))
2879 ;; If IND is set, preserve indentation of include keyword until
2880 ;; the first headline encountered.
2882 (unless (eq major-mode
'org-mode
) (org-mode))
2883 (goto-char (point-min))
2884 (let ((ind-str (make-string ind ?
)))
2885 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
2886 ;; Do not move footnote definitions out of column 0.
2887 (unless (and (looking-at org-footnote-definition-re
)
2888 (eq (org-element-type (org-element-at-point))
2889 'footnote-definition
))
2892 ;; When MINLEVEL is specified, compute minimal level for headlines
2893 ;; in the file (CUR-MIN), and remove stars to each headline so
2894 ;; that headlines with minimal level have a level of MINLEVEL.
2896 (unless (eq major-mode
'org-mode
) (org-mode))
2897 (org-with-limited-levels
2898 (let ((levels (org-map-entries
2899 (lambda () (org-reduced-level (org-current-level))))))
2901 (let ((offset (- minlevel
(apply 'min levels
))))
2902 (unless (zerop offset
)
2903 (when org-odd-levels-only
(setq offset
(* offset
2)))
2904 ;; Only change stars, don't bother moving whole
2907 (lambda () (if (< offset
0) (delete-char (abs offset
))
2908 (insert (make-string offset ?
*)))))))))))
2912 ;;; Tools For Back-Ends
2914 ;; A whole set of tools is available to help build new exporters. Any
2915 ;; function general enough to have its use across many back-ends
2916 ;; should be added here.
2918 ;;;; For Affiliated Keywords
2920 ;; `org-export-read-attribute' reads a property from a given element
2921 ;; as a plist. It can be used to normalize affiliated keywords'
2924 ;; Since captions can span over multiple lines and accept dual values,
2925 ;; their internal representation is a bit tricky. Therefore,
2926 ;; `org-export-get-caption' transparently returns a given element's
2927 ;; caption as a secondary string.
2929 (defun org-export-read-attribute (attribute element
&optional property
)
2930 "Turn ATTRIBUTE property from ELEMENT into a plist.
2932 When optional argument PROPERTY is non-nil, return the value of
2933 that property within attributes.
2935 This function assumes attributes are defined as \":keyword
2936 value\" pairs. It is appropriate for `:attr_html' like
2939 (let ((value (org-element-property attribute element
)))
2941 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
2942 (if property
(plist-get attributes property
) attributes
)))
2944 (defun org-export-get-caption (element &optional shortp
)
2945 "Return caption from ELEMENT as a secondary string.
2947 When optional argument SHORTP is non-nil, return short caption,
2948 as a secondary string, instead.
2950 Caption lines are separated by a white space."
2951 (let ((full-caption (org-element-property :caption element
)) caption
)
2952 (dolist (line full-caption
(cdr caption
))
2953 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
2955 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
2958 ;;;; For Export Snippets
2960 ;; Every export snippet is transmitted to the back-end. Though, the
2961 ;; latter will only retain one type of export-snippet, ignoring
2962 ;; others, based on the former's target back-end. The function
2963 ;; `org-export-snippet-backend' returns that back-end for a given
2966 (defun org-export-snippet-backend (export-snippet)
2967 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2968 Translation, with `org-export-snippet-translation-alist', is
2970 (let ((back-end (org-element-property :back-end export-snippet
)))
2972 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
2978 ;; `org-export-collect-footnote-definitions' is a tool to list
2979 ;; actually used footnotes definitions in the whole parse tree, or in
2980 ;; an headline, in order to add footnote listings throughout the
2983 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2984 ;; back-ends, when they need to attach the footnote definition only to
2985 ;; the first occurrence of the corresponding label.
2987 ;; `org-export-get-footnote-definition' and
2988 ;; `org-export-get-footnote-number' provide easier access to
2989 ;; additional information relative to a footnote reference.
2991 (defun org-export-collect-footnote-definitions (data info
)
2992 "Return an alist between footnote numbers, labels and definitions.
2994 DATA is the parse tree from which definitions are collected.
2995 INFO is the plist used as a communication channel.
2997 Definitions are sorted by order of references. They either
2998 appear as Org data or as a secondary string for inlined
2999 footnotes. Unreferenced definitions are ignored."
3001 collect-fn
; for byte-compiler.
3005 ;; Collect footnote number, label and definition in DATA.
3007 data
'footnote-reference
3009 (when (org-export-footnote-first-reference-p fn info
)
3010 (let ((def (org-export-get-footnote-definition fn info
)))
3012 (list (org-export-get-footnote-number fn info
)
3013 (org-element-property :label fn
)
3016 ;; Also search in definition for nested footnotes.
3017 (when (eq (org-element-property :type fn
) 'standard
)
3018 (funcall collect-fn def
)))))
3019 ;; Don't enter footnote definitions since it will happen
3020 ;; when their first reference is found.
3021 info nil
'footnote-definition
)))))
3022 (funcall collect-fn
(plist-get info
:parse-tree
))
3023 (reverse num-alist
)))
3025 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3026 "Non-nil when a footnote reference is the first one for its label.
3028 FOOTNOTE-REFERENCE is the footnote reference being considered.
3029 INFO is the plist used as a communication channel."
3030 (let ((label (org-element-property :label footnote-reference
)))
3031 ;; Anonymous footnotes are always a first reference.
3033 ;; Otherwise, return the first footnote with the same LABEL and
3034 ;; test if it is equal to FOOTNOTE-REFERENCE.
3035 (let* (search-refs ; for byte-compiler.
3040 data
'footnote-reference
3043 ((string= (org-element-property :label fn
) label
)
3045 ;; If FN isn't inlined, be sure to traverse its
3046 ;; definition before resuming search. See
3047 ;; comments in `org-export-get-footnote-number'
3048 ;; for more information.
3049 ((eq (org-element-property :type fn
) 'standard
)
3050 (funcall search-refs
3051 (org-export-get-footnote-definition fn info
)))))
3052 ;; Don't enter footnote definitions since it will
3053 ;; happen when their first reference is found.
3054 info
'first-match
'footnote-definition
)))))
3055 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3056 footnote-reference
)))))
3058 (defun org-export-get-footnote-definition (footnote-reference info
)
3059 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3060 INFO is the plist used as a communication channel."
3061 (let ((label (org-element-property :label footnote-reference
)))
3062 (or (org-element-property :inline-definition footnote-reference
)
3063 (cdr (assoc label
(plist-get info
:footnote-definition-alist
))))))
3065 (defun org-export-get-footnote-number (footnote info
)
3066 "Return number associated to a footnote.
3068 FOOTNOTE is either a footnote reference or a footnote definition.
3069 INFO is the plist used as a communication channel."
3070 (let* ((label (org-element-property :label footnote
))
3072 search-ref
; For byte-compiler.
3076 ;; Search footnote references through DATA, filling
3077 ;; SEEN-REFS along the way.
3079 data
'footnote-reference
3081 (let ((fn-lbl (org-element-property :label fn
)))
3083 ;; Anonymous footnote match: return number.
3084 ((and (not fn-lbl
) (eq fn footnote
))
3085 (throw 'exit
(1+ (length seen-refs
))))
3086 ;; Labels match: return number.
3087 ((and label
(string= label fn-lbl
))
3088 (throw 'exit
(1+ (length seen-refs
))))
3089 ;; Anonymous footnote: it's always a new one. Also,
3090 ;; be sure to return nil from the `cond' so
3091 ;; `first-match' doesn't get us out of the loop.
3092 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3093 ;; Label not seen so far: add it so SEEN-REFS.
3095 ;; Also search for subsequent references in
3096 ;; footnote definition so numbering follows reading
3097 ;; logic. Note that we don't have to care about
3098 ;; inline definitions, since `org-element-map'
3099 ;; already traverses them at the right time.
3101 ;; Once again, return nil to stay in the loop.
3102 ((not (member fn-lbl seen-refs
))
3103 (push fn-lbl seen-refs
)
3105 (org-export-get-footnote-definition fn info
))
3107 ;; Don't enter footnote definitions since it will happen
3108 ;; when their first reference is found.
3109 info
'first-match
'footnote-definition
)))))
3110 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3115 ;; `org-export-get-relative-level' is a shortcut to get headline
3116 ;; level, relatively to the lower headline level in the parsed tree.
3118 ;; `org-export-get-headline-number' returns the section number of an
3119 ;; headline, while `org-export-number-to-roman' allows to convert it
3120 ;; to roman numbers.
3122 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3123 ;; `org-export-last-sibling-p' are three useful predicates when it
3124 ;; comes to fulfill the `:headline-levels' property.
3126 (defun org-export-get-relative-level (headline info
)
3127 "Return HEADLINE relative level within current parsed tree.
3128 INFO is a plist holding contextual information."
3129 (+ (org-element-property :level headline
)
3130 (or (plist-get info
:headline-offset
) 0)))
3132 (defun org-export-low-level-p (headline info
)
3133 "Non-nil when HEADLINE is considered as low level.
3135 INFO is a plist used as a communication channel.
3137 A low level headlines has a relative level greater than
3138 `:headline-levels' property value.
3140 Return value is the difference between HEADLINE relative level
3141 and the last level being considered as high enough, or nil."
3142 (let ((limit (plist-get info
:headline-levels
)))
3143 (when (wholenump limit
)
3144 (let ((level (org-export-get-relative-level headline info
)))
3145 (and (> level limit
) (- level limit
))))))
3147 (defun org-export-get-headline-number (headline info
)
3148 "Return HEADLINE numbering as a list of numbers.
3149 INFO is a plist holding contextual information."
3150 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3152 (defun org-export-numbered-headline-p (headline info
)
3153 "Return a non-nil value if HEADLINE element should be numbered.
3154 INFO is a plist used as a communication channel."
3155 (let ((sec-num (plist-get info
:section-numbers
))
3156 (level (org-export-get-relative-level headline info
)))
3157 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3159 (defun org-export-number-to-roman (n)
3160 "Convert integer N into a roman numeral."
3161 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3162 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3163 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3167 (number-to-string n
)
3169 (if (>= n
(caar roman
))
3170 (setq n
(- n
(caar roman
))
3171 res
(concat res
(cdar roman
)))
3175 (defun org-export-get-tags (element info
&optional tags inherited
)
3176 "Return list of tags associated to ELEMENT.
3178 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3179 is a plist used as a communication channel.
3181 Select tags (see `org-export-select-tags') and exclude tags (see
3182 `org-export-exclude-tags') are removed from the list.
3184 When non-nil, optional argument TAGS should be a list of strings.
3185 Any tag belonging to this list will also be removed.
3187 When optional argument INHERITED is non-nil, tags can also be
3188 inherited from parent headlines.."
3190 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3191 (member tag
(plist-get info
:exclude-tags
))
3193 (if (not inherited
) (org-element-property :tags element
)
3194 ;; Build complete list of inherited tags.
3195 (let ((current-tag-list (org-element-property :tags element
)))
3200 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3201 (not (member tag current-tag-list
)))
3202 (push tag current-tag-list
)))
3203 (org-element-property :tags parent
)))
3204 (org-export-get-genealogy element
))
3205 current-tag-list
))))
3207 (defun org-export-get-node-property (property blob
&optional inherited
)
3208 "Return node PROPERTY value for BLOB.
3210 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3213 If optional argument INHERITED is non-nil, the value can be
3214 inherited from a parent headline.
3216 Return value is a string or nil."
3217 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3218 (org-export-get-parent-headline blob
))))
3219 (if (not inherited
) (org-element-property property blob
)
3220 (let ((parent headline
) value
)
3223 (when (plist-member (nth 1 parent
) property
)
3224 (throw 'found
(org-element-property property parent
)))
3225 (setq parent
(org-element-property :parent parent
))))))))
3227 (defun org-export-first-sibling-p (headline info
)
3228 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3229 INFO is a plist used as a communication channel."
3230 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3233 (defun org-export-last-sibling-p (headline info
)
3234 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3235 INFO is a plist used as a communication channel."
3236 (not (org-export-get-next-element headline info
)))
3241 ;; `org-export-solidify-link-text' turns a string into a safer version
3242 ;; for links, replacing most non-standard characters with hyphens.
3244 ;; `org-export-get-coderef-format' returns an appropriate format
3245 ;; string for coderefs.
3247 ;; `org-export-inline-image-p' returns a non-nil value when the link
3248 ;; provided should be considered as an inline image.
3250 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3251 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3252 ;; returns an appropriate unique identifier when found, or nil.
3254 ;; `org-export-resolve-id-link' returns the first headline with
3255 ;; specified id or custom-id in parse tree, the path to the external
3256 ;; file with the id or nil when neither was found.
3258 ;; `org-export-resolve-coderef' associates a reference to a line
3259 ;; number in the element it belongs, or returns the reference itself
3260 ;; when the element isn't numbered.
3262 (defun org-export-solidify-link-text (s)
3263 "Take link text S and make a safe target out of it."
3265 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-]+") "-")))
3267 (defun org-export-get-coderef-format (path desc
)
3268 "Return format string for code reference link.
3269 PATH is the link path. DESC is its description."
3271 (cond ((not desc
) "%s")
3272 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3273 (replace-match "%s" t t desc
))
3276 (defun org-export-inline-image-p (link &optional rules
)
3277 "Non-nil if LINK object points to an inline image.
3279 Optional argument is a set of RULES defining inline images. It
3280 is an alist where associations have the following shape:
3284 Applying a rule means apply REGEXP against LINK's path when its
3285 type is TYPE. The function will return a non-nil value if any of
3286 the provided rules is non-nil. The default rule is
3287 `org-export-default-inline-image-rule'.
3289 This only applies to links without a description."
3290 (and (not (org-element-contents link
))
3291 (let ((case-fold-search t
)
3292 (rules (or rules org-export-default-inline-image-rule
)))
3296 (and (string= (org-element-property :type link
) (car rule
))
3297 (string-match (cdr rule
)
3298 (org-element-property :path link
))
3301 ;; Return nil if no rule matched.
3304 (defun org-export-resolve-coderef (ref info
)
3305 "Resolve a code reference REF.
3307 INFO is a plist used as a communication channel.
3309 Return associated line number in source code, or REF itself,
3310 depending on src-block or example element's switches."
3312 (plist-get info
:parse-tree
) '(example-block src-block
)
3315 (insert (org-trim (org-element-property :value el
)))
3316 (let* ((label-fmt (regexp-quote
3317 (or (org-element-property :label-fmt el
)
3318 org-coderef-label-format
)))
3320 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3321 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3322 ;; Element containing REF is found. Resolve it to either
3323 ;; a label or a line number, as needed.
3324 (when (re-search-backward ref-re nil t
)
3326 ((org-element-property :use-labels el
) ref
)
3327 ((eq (org-element-property :number-lines el
) 'continued
)
3328 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3329 (t (line-number-at-pos)))))))
3332 (defun org-export-resolve-fuzzy-link (link info
)
3333 "Return LINK destination.
3335 INFO is a plist holding contextual information.
3337 Return value can be an object, an element, or nil:
3339 - If LINK path matches a target object (i.e. <<path>>) or
3340 element (i.e. \"#+TARGET: path\"), return it.
3342 - If LINK path exactly matches the name affiliated keyword
3343 \(i.e. #+NAME: path) of an element, return that element.
3345 - If LINK path exactly matches any headline name, return that
3346 element. If more than one headline share that name, priority
3347 will be given to the one with the closest common ancestor, if
3348 any, or the first one in the parse tree otherwise.
3350 - Otherwise, return nil.
3352 Assume LINK type is \"fuzzy\"."
3353 (let* ((path (org-element-property :path link
))
3354 (match-title-p (eq (aref path
0) ?
*)))
3356 ;; First try to find a matching "<<path>>" unless user specified
3357 ;; he was looking for an headline (path starts with a *
3359 ((and (not match-title-p
)
3360 (loop for target in
(plist-get info
:target-list
)
3361 when
(string= (org-element-property :value target
) path
)
3363 ;; Then try to find an element with a matching "#+NAME: path"
3364 ;; affiliated keyword.
3365 ((and (not match-title-p
)
3367 (plist-get info
:parse-tree
) org-element-all-elements
3369 (when (string= (org-element-property :name el
) path
) el
))
3370 info
'first-match
)))
3371 ;; Last case: link either points to an headline or to
3372 ;; nothingness. Try to find the source, with priority given to
3373 ;; headlines with the closest common ancestor. If such candidate
3374 ;; is found, return it, otherwise return nil.
3376 (let ((find-headline
3378 ;; Return first headline whose `:raw-value' property
3379 ;; is NAME in parse tree DATA, or nil.
3385 (org-element-property :raw-value headline
)
3388 info
'first-match
)))))
3389 ;; Search among headlines sharing an ancestor with link,
3390 ;; from closest to farthest.
3394 (when (eq (org-element-type parent
) 'headline
)
3395 (let ((foundp (funcall find-headline path parent
)))
3396 (when foundp
(throw 'exit foundp
)))))
3397 (org-export-get-genealogy link
)) nil
)
3398 ;; No match with a common ancestor: try the full parse-tree.
3399 (funcall find-headline
3400 (if match-title-p
(substring path
1) path
)
3401 (plist-get info
:parse-tree
))))))))
3403 (defun org-export-resolve-id-link (link info
)
3404 "Return headline referenced as LINK destination.
3406 INFO is a plist used as a communication channel.
3408 Return value can be the headline element matched in current parse
3409 tree, a file name or nil. Assume LINK type is either \"id\" or
3411 (let ((id (org-element-property :path link
)))
3412 ;; First check if id is within the current parse tree.
3413 (or (org-element-map
3414 (plist-get info
:parse-tree
) 'headline
3416 (when (or (string= (org-element-property :id headline
) id
)
3417 (string= (org-element-property :custom-id headline
) id
))
3420 ;; Otherwise, look for external files.
3421 (cdr (assoc id
(plist-get info
:id-alist
))))))
3423 (defun org-export-resolve-radio-link (link info
)
3424 "Return radio-target object referenced as LINK destination.
3426 INFO is a plist used as a communication channel.
3428 Return value can be a radio-target object or nil. Assume LINK
3429 has type \"radio\"."
3430 (let ((path (org-element-property :path link
)))
3432 (plist-get info
:parse-tree
) 'radio-target
3434 (when (equal (org-element-property :value radio
) path
) radio
))
3435 info
'first-match
)))
3440 ;; `org-export-get-ordinal' associates a sequence number to any object
3443 (defun org-export-get-ordinal (element info
&optional types predicate
)
3444 "Return ordinal number of an element or object.
3446 ELEMENT is the element or object considered. INFO is the plist
3447 used as a communication channel.
3449 Optional argument TYPES, when non-nil, is a list of element or
3450 object types, as symbols, that should also be counted in.
3451 Otherwise, only provided element's type is considered.
3453 Optional argument PREDICATE is a function returning a non-nil
3454 value if the current element or object should be counted in. It
3455 accepts two arguments: the element or object being considered and
3456 the plist used as a communication channel. This allows to count
3457 only a certain type of objects (i.e. inline images).
3459 Return value is a list of numbers if ELEMENT is an headline or an
3460 item. It is nil for keywords. It represents the footnote number
3461 for footnote definitions and footnote references. If ELEMENT is
3462 a target, return the same value as if ELEMENT was the closest
3463 table, item or headline containing the target. In any other
3464 case, return the sequence number of ELEMENT among elements or
3465 objects of the same type."
3466 ;; A target keyword, representing an invisible target, never has
3467 ;; a sequence number.
3468 (unless (eq (org-element-type element
) 'keyword
)
3469 ;; Ordinal of a target object refer to the ordinal of the closest
3470 ;; table, item, or headline containing the object.
3471 (when (eq (org-element-type element
) 'target
)
3473 (loop for parent in
(org-export-get-genealogy element
)
3476 (org-element-type parent
)
3477 '(footnote-definition footnote-reference headline item
3480 (case (org-element-type element
)
3481 ;; Special case 1: An headline returns its number as a list.
3482 (headline (org-export-get-headline-number element info
))
3483 ;; Special case 2: An item returns its number as a list.
3484 (item (let ((struct (org-element-property :structure element
)))
3485 (org-list-get-item-number
3486 (org-element-property :begin element
)
3488 (org-list-prevs-alist struct
)
3489 (org-list-parents-alist struct
))))
3490 ((footnote-definition footnote-reference
)
3491 (org-export-get-footnote-number element info
))
3494 ;; Increment counter until ELEMENT is found again.
3496 (plist-get info
:parse-tree
) (or types
(org-element-type element
))
3499 ((eq element el
) (1+ counter
))
3500 ((not predicate
) (incf counter
) nil
)
3501 ((funcall predicate el info
) (incf counter
) nil
)))
3502 info
'first-match
))))))
3507 ;; `org-export-get-loc' counts number of code lines accumulated in
3508 ;; src-block or example-block elements with a "+n" switch until
3509 ;; a given element, excluded. Note: "-n" switches reset that count.
3511 ;; `org-export-unravel-code' extracts source code (along with a code
3512 ;; references alist) from an `element-block' or `src-block' type
3515 ;; `org-export-format-code' applies a formatting function to each line
3516 ;; of code, providing relative line number and code reference when
3517 ;; appropriate. Since it doesn't access the original element from
3518 ;; which the source code is coming, it expects from the code calling
3519 ;; it to know if lines should be numbered and if code references
3522 ;; Eventually, `org-export-format-code-default' is a higher-level
3523 ;; function (it makes use of the two previous functions) which handles
3524 ;; line numbering and code references inclusion, and returns source
3525 ;; code in a format suitable for plain text or verbatim output.
3527 (defun org-export-get-loc (element info
)
3528 "Return accumulated lines of code up to ELEMENT.
3530 INFO is the plist used as a communication channel.
3532 ELEMENT is excluded from count."
3535 (plist-get info
:parse-tree
)
3536 `(src-block example-block
,(org-element-type element
))
3539 ;; ELEMENT is reached: Quit the loop.
3541 ;; Only count lines from src-block and example-block elements
3542 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3543 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3544 ((let ((linums (org-element-property :number-lines el
)))
3546 ;; Accumulate locs or reset them.
3547 (let ((lines (org-count-lines
3548 (org-trim (org-element-property :value el
)))))
3549 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3550 ;; Return nil to stay in the loop.
3556 (defun org-export-unravel-code (element)
3557 "Clean source code and extract references out of it.
3559 ELEMENT has either a `src-block' an `example-block' type.
3561 Return a cons cell whose CAR is the source code, cleaned from any
3562 reference and protective comma and CDR is an alist between
3563 relative line number (integer) and name of code reference on that
3565 (let* ((line 0) refs
3566 ;; Get code and clean it. Remove blank lines at its
3567 ;; beginning and end.
3568 (code (let ((c (replace-regexp-in-string
3569 "\\`\\([ \t]*\n\\)+" ""
3570 (replace-regexp-in-string
3571 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3572 (org-element-property :value element
)))))
3573 ;; If appropriate, remove global indentation.
3574 (if (or org-src-preserve-indentation
3575 (org-element-property :preserve-indent element
))
3577 (org-remove-indentation c
))))
3578 ;; Get format used for references.
3579 (label-fmt (regexp-quote
3580 (or (org-element-property :label-fmt element
)
3581 org-coderef-label-format
)))
3582 ;; Build a regexp matching a loc with a reference.
3584 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3585 (replace-regexp-in-string
3586 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3589 ;; Code with references removed.
3590 (org-element-normalize-string
3594 (if (not (string-match with-ref-re loc
)) loc
3595 ;; Ref line: remove ref, and signal its position in REFS.
3596 (push (cons line
(match-string 3 loc
)) refs
)
3597 (replace-match "" nil nil loc
1)))
3598 (org-split-string code
"\n") "\n"))
3602 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3603 "Format CODE by applying FUN line-wise and return it.
3605 CODE is a string representing the code to format. FUN is
3606 a function. It must accept three arguments: a line of
3607 code (string), the current line number (integer) or nil and the
3608 reference associated to the current line (string) or nil.
3610 Optional argument NUM-LINES can be an integer representing the
3611 number of code lines accumulated until the current code. Line
3612 numbers passed to FUN will take it into account. If it is nil,
3613 FUN's second argument will always be nil. This number can be
3614 obtained with `org-export-get-loc' function.
3616 Optional argument REF-ALIST can be an alist between relative line
3617 number (i.e. ignoring NUM-LINES) and the name of the code
3618 reference on it. If it is nil, FUN's third argument will always
3619 be nil. It can be obtained through the use of
3620 `org-export-unravel-code' function."
3621 (let ((--locs (org-split-string code
"\n"))
3623 (org-element-normalize-string
3627 (let ((--ref (cdr (assq --line ref-alist
))))
3628 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3631 (defun org-export-format-code-default (element info
)
3632 "Return source code from ELEMENT, formatted in a standard way.
3634 ELEMENT is either a `src-block' or `example-block' element. INFO
3635 is a plist used as a communication channel.
3637 This function takes care of line numbering and code references
3638 inclusion. Line numbers, when applicable, appear at the
3639 beginning of the line, separated from the code by two white
3640 spaces. Code references, on the other hand, appear flushed to
3641 the right, separated by six white spaces from the widest line of
3643 ;; Extract code and references.
3644 (let* ((code-info (org-export-unravel-code element
))
3645 (code (car code-info
))
3646 (code-lines (org-split-string code
"\n"))
3647 (refs (and (org-element-property :retain-labels element
)
3649 ;; Handle line numbering.
3650 (num-start (case (org-element-property :number-lines element
)
3651 (continued (org-export-get-loc element info
))
3656 (length (number-to-string
3657 (+ (length code-lines
) num-start
))))))
3658 ;; Prepare references display, if required. Any reference
3659 ;; should start six columns after the widest line of code,
3660 ;; wrapped with parenthesis.
3662 (+ (apply 'max
(mapcar 'length code-lines
))
3663 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3664 (org-export-format-code
3666 (lambda (loc line-num ref
)
3667 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3672 (concat (make-string
3674 (+ (length loc
) (length number-str
))) ?
)
3675 (format "(%s)" ref
))))))
3681 ;; `org-export-table-has-special-column-p' and and
3682 ;; `org-export-table-row-is-special-p' are predicates used to look for
3683 ;; meta-information about the table structure.
3685 ;; `org-table-has-header-p' tells when the rows before the first rule
3686 ;; should be considered as table's header.
3688 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3689 ;; and `org-export-table-cell-borders' extract information from
3690 ;; a table-cell element.
3692 ;; `org-export-table-dimensions' gives the number on rows and columns
3693 ;; in the table, ignoring horizontal rules and special columns.
3694 ;; `org-export-table-cell-address', given a table-cell object, returns
3695 ;; the absolute address of a cell. On the other hand,
3696 ;; `org-export-get-table-cell-at' does the contrary.
3698 ;; `org-export-table-cell-starts-colgroup-p',
3699 ;; `org-export-table-cell-ends-colgroup-p',
3700 ;; `org-export-table-row-starts-rowgroup-p',
3701 ;; `org-export-table-row-ends-rowgroup-p',
3702 ;; `org-export-table-row-starts-header-p' and
3703 ;; `org-export-table-row-ends-header-p' indicate position of current
3704 ;; row or cell within the table.
3706 (defun org-export-table-has-special-column-p (table)
3707 "Non-nil when TABLE has a special column.
3708 All special columns will be ignored during export."
3709 ;; The table has a special column when every first cell of every row
3710 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3711 ;; "*" "_" and "^". Though, do not consider a first row containing
3712 ;; only empty cells as special.
3713 (let ((special-column-p 'empty
))
3717 (when (eq (org-element-property :type row
) 'standard
)
3718 (let ((value (org-element-contents
3719 (car (org-element-contents row
)))))
3720 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3721 (setq special-column-p
'special
))
3723 (t (throw 'exit nil
))))))
3724 (org-element-contents table
))
3725 (eq special-column-p
'special
))))
3727 (defun org-export-table-has-header-p (table info
)
3728 "Non-nil when TABLE has an header.
3730 INFO is a plist used as a communication channel.
3732 A table has an header when it contains at least two row groups."
3733 (let ((rowgroup 1) row-flag
)
3739 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
3740 (incf rowgroup
) (setq row-flag nil
))
3741 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
3742 (setq row-flag t
) nil
)))
3745 (defun org-export-table-row-is-special-p (table-row info
)
3746 "Non-nil if TABLE-ROW is considered special.
3748 INFO is a plist used as the communication channel.
3750 All special rows will be ignored during export."
3751 (when (eq (org-element-property :type table-row
) 'standard
)
3752 (let ((first-cell (org-element-contents
3753 (car (org-element-contents table-row
)))))
3754 ;; A row is special either when...
3756 ;; ... it starts with a field only containing "/",
3757 (equal first-cell
'("/"))
3758 ;; ... the table contains a special column and the row start
3759 ;; with a marking character among, "^", "_", "$" or "!",
3760 (and (org-export-table-has-special-column-p
3761 (org-export-get-parent table-row
))
3762 (member first-cell
'(("^") ("_") ("$") ("!"))))
3763 ;; ... it contains only alignment cookies and empty cells.
3764 (let ((special-row-p 'empty
))
3768 (let ((value (org-element-contents cell
)))
3769 ;; Since VALUE is a secondary string, the following
3770 ;; checks avoid expanding it with `org-export-data'.
3772 ((and (not (cdr value
))
3773 (stringp (car value
))
3774 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3776 (setq special-row-p
'cookie
))
3777 (t (throw 'exit nil
)))))
3778 (org-element-contents table-row
))
3779 (eq special-row-p
'cookie
)))))))
3781 (defun org-export-table-row-group (table-row info
)
3782 "Return TABLE-ROW's group.
3784 INFO is a plist used as the communication channel.
3786 Return value is the group number, as an integer, or nil special
3787 rows and table rules. Group 1 is also table's header."
3788 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3789 (org-export-table-row-is-special-p table-row info
))
3790 (let ((group 0) row-flag
)
3795 ((and (eq (org-element-property :type row
) 'standard
)
3796 (not (org-export-table-row-is-special-p row info
)))
3797 (unless row-flag
(incf group
) (setq row-flag t
)))
3798 ((eq (org-element-property :type row
) 'rule
)
3799 (setq row-flag nil
)))
3800 (when (eq table-row row
) (throw 'found group
)))
3801 (org-element-contents (org-export-get-parent table-row
)))))))
3803 (defun org-export-table-cell-width (table-cell info
)
3804 "Return TABLE-CELL contents width.
3806 INFO is a plist used as the communication channel.
3808 Return value is the width given by the last width cookie in the
3809 same column as TABLE-CELL, or nil."
3810 (let* ((row (org-export-get-parent table-cell
))
3811 (column (let ((cells (org-element-contents row
)))
3812 (- (length cells
) (length (memq table-cell cells
)))))
3813 (table (org-export-get-parent-table table-cell
))
3818 ;; In a special row, try to find a width cookie at COLUMN.
3819 ((org-export-table-row-is-special-p row info
)
3820 (let ((value (org-element-contents
3821 (elt (org-element-contents row
) column
))))
3822 ;; The following checks avoid expanding unnecessarily the
3823 ;; cell with `org-export-data'
3826 (stringp (car value
))
3827 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
3828 (match-string 1 (car value
)))
3830 (string-to-number (match-string 1 (car value
)))))))
3831 ;; Ignore table rules.
3832 ((eq (org-element-property :type row
) 'rule
))))
3833 (org-element-contents table
))
3837 (defun org-export-table-cell-alignment (table-cell info
)
3838 "Return TABLE-CELL contents alignment.
3840 INFO is a plist used as the communication channel.
3842 Return alignment as specified by the last alignment cookie in the
3843 same column as TABLE-CELL. If no such cookie is found, a default
3844 alignment value will be deduced from fraction of numbers in the
3845 column (see `org-table-number-fraction' for more information).
3846 Possible values are `left', `right' and `center'."
3847 (let* ((row (org-export-get-parent table-cell
))
3848 (column (let ((cells (org-element-contents row
)))
3849 (- (length cells
) (length (memq table-cell cells
)))))
3850 (table (org-export-get-parent-table table-cell
))
3857 ;; In a special row, try to find an alignment cookie at
3859 ((org-export-table-row-is-special-p row info
)
3860 (let ((value (org-element-contents
3861 (elt (org-element-contents row
) column
))))
3862 ;; Since VALUE is a secondary string, the following checks
3863 ;; avoid useless expansion through `org-export-data'.
3866 (stringp (car value
))
3867 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3869 (match-string 1 (car value
)))
3870 (setq cookie-align
(match-string 1 (car value
))))))
3871 ;; Ignore table rules.
3872 ((eq (org-element-property :type row
) 'rule
))
3873 ;; In a standard row, check if cell's contents are expressing
3874 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3875 ;; Though, don't bother if an alignment cookie has already
3876 ;; defined cell's alignment.
3878 (let ((value (org-export-data
3879 (org-element-contents
3880 (elt (org-element-contents row
) column
))
3883 (when (string-match org-table-number-regexp value
)
3884 (incf number-cells
))))))
3885 (org-element-contents table
))
3886 ;; Return value. Alignment specified by cookies has precedence
3887 ;; over alignment deduced from cells contents.
3888 (cond ((equal cookie-align
"l") 'left
)
3889 ((equal cookie-align
"r") 'right
)
3890 ((equal cookie-align
"c") 'center
)
3891 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
3895 (defun org-export-table-cell-borders (table-cell info
)
3896 "Return TABLE-CELL borders.
3898 INFO is a plist used as a communication channel.
3900 Return value is a list of symbols, or nil. Possible values are:
3901 `top', `bottom', `above', `below', `left' and `right'. Note:
3902 `top' (resp. `bottom') only happen for a cell in the first
3903 row (resp. last row) of the table, ignoring table rules, if any.
3905 Returned borders ignore special rows."
3906 (let* ((row (org-export-get-parent table-cell
))
3907 (table (org-export-get-parent-table table-cell
))
3909 ;; Top/above border? TABLE-CELL has a border above when a rule
3910 ;; used to demarcate row groups can be found above. Hence,
3911 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3912 ;; another regular row has to be found above that rule.
3916 (cond ((eq (org-element-property :type row
) 'rule
)
3918 ((not (org-export-table-row-is-special-p row info
))
3919 (if rule-flag
(throw 'exit
(push 'above borders
))
3920 (throw 'exit nil
)))))
3921 ;; Look at every row before the current one.
3922 (cdr (memq row
(reverse (org-element-contents table
)))))
3923 ;; No rule above, or rule found starts the table (ignoring any
3924 ;; special row): TABLE-CELL is at the top of the table.
3925 (when rule-flag
(push 'above borders
))
3926 (push 'top borders
)))
3927 ;; Bottom/below border? TABLE-CELL has a border below when next
3928 ;; non-regular row below is a rule.
3932 (cond ((eq (org-element-property :type row
) 'rule
)
3934 ((not (org-export-table-row-is-special-p row info
))
3935 (if rule-flag
(throw 'exit
(push 'below borders
))
3936 (throw 'exit nil
)))))
3937 ;; Look at every row after the current one.
3938 (cdr (memq row
(org-element-contents table
))))
3939 ;; No rule below, or rule found ends the table (modulo some
3940 ;; special row): TABLE-CELL is at the bottom of the table.
3941 (when rule-flag
(push 'below borders
))
3942 (push 'bottom borders
)))
3943 ;; Right/left borders? They can only be specified by column
3944 ;; groups. Column groups are defined in a row starting with "/".
3945 ;; Also a column groups row only contains "<", "<>", ">" or blank
3948 (let ((column (let ((cells (org-element-contents row
)))
3949 (- (length cells
) (length (memq table-cell cells
))))))
3952 (unless (eq (org-element-property :type row
) 'rule
)
3953 (when (equal (org-element-contents
3954 (car (org-element-contents row
)))
3956 (let ((column-groups
3959 (let ((value (org-element-contents cell
)))
3960 (when (member value
'(("<") ("<>") (">") nil
))
3962 (org-element-contents row
))))
3963 ;; There's a left border when previous cell, if
3964 ;; any, ends a group, or current one starts one.
3965 (when (or (and (not (zerop column
))
3966 (member (elt column-groups
(1- column
))
3968 (member (elt column-groups column
) '("<" "<>")))
3969 (push 'left borders
))
3970 ;; There's a right border when next cell, if any,
3971 ;; starts a group, or current one ends one.
3972 (when (or (and (/= (1+ column
) (length column-groups
))
3973 (member (elt column-groups
(1+ column
))
3975 (member (elt column-groups column
) '(">" "<>")))
3976 (push 'right borders
))
3977 (throw 'exit nil
)))))
3978 ;; Table rows are read in reverse order so last column groups
3979 ;; row has precedence over any previous one.
3980 (reverse (org-element-contents table
)))))
3984 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
3985 "Non-nil when TABLE-CELL is at the beginning of a row group.
3986 INFO is a plist used as a communication channel."
3987 ;; A cell starts a column group either when it is at the beginning
3988 ;; of a row (or after the special column, if any) or when it has
3990 (or (eq (org-element-map
3991 (org-export-get-parent table-cell
)
3992 'table-cell
'identity info
'first-match
)
3994 (memq 'left
(org-export-table-cell-borders table-cell info
))))
3996 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
3997 "Non-nil when TABLE-CELL is at the end of a row group.
3998 INFO is a plist used as a communication channel."
3999 ;; A cell ends a column group either when it is at the end of a row
4000 ;; or when it has a right border.
4001 (or (eq (car (last (org-element-contents
4002 (org-export-get-parent table-cell
))))
4004 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4006 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4007 "Non-nil when TABLE-ROW is at the beginning of a column group.
4008 INFO is a plist used as a communication channel."
4009 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4010 (org-export-table-row-is-special-p table-row info
))
4011 (let ((borders (org-export-table-cell-borders
4012 (car (org-element-contents table-row
)) info
)))
4013 (or (memq 'top borders
) (memq 'above borders
)))))
4015 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4016 "Non-nil when TABLE-ROW is at the end of a column group.
4017 INFO is a plist used as a communication channel."
4018 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4019 (org-export-table-row-is-special-p table-row info
))
4020 (let ((borders (org-export-table-cell-borders
4021 (car (org-element-contents table-row
)) info
)))
4022 (or (memq 'bottom borders
) (memq 'below borders
)))))
4024 (defun org-export-table-row-starts-header-p (table-row info
)
4025 "Non-nil when TABLE-ROW is the first table header's row.
4026 INFO is a plist used as a communication channel."
4027 (and (org-export-table-has-header-p
4028 (org-export-get-parent-table table-row
) info
)
4029 (org-export-table-row-starts-rowgroup-p table-row info
)
4030 (= (org-export-table-row-group table-row info
) 1)))
4032 (defun org-export-table-row-ends-header-p (table-row info
)
4033 "Non-nil when TABLE-ROW is the last table header's row.
4034 INFO is a plist used as a communication channel."
4035 (and (org-export-table-has-header-p
4036 (org-export-get-parent-table table-row
) info
)
4037 (org-export-table-row-ends-rowgroup-p table-row info
)
4038 (= (org-export-table-row-group table-row info
) 1)))
4040 (defun org-export-table-dimensions (table info
)
4041 "Return TABLE dimensions.
4043 INFO is a plist used as a communication channel.
4045 Return value is a CONS like (ROWS . COLUMNS) where
4046 ROWS (resp. COLUMNS) is the number of exportable
4047 rows (resp. columns)."
4048 (let (first-row (columns 0) (rows 0))
4049 ;; Set number of rows, and extract first one.
4053 (when (eq (org-element-property :type row
) 'standard
)
4055 (unless first-row
(setq first-row row
)))) info
)
4056 ;; Set number of columns.
4057 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4059 (cons rows columns
)))
4061 (defun org-export-table-cell-address (table-cell info
)
4062 "Return address of a regular TABLE-CELL object.
4064 TABLE-CELL is the cell considered. INFO is a plist used as
4065 a communication channel.
4067 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4068 zero-based index. Only exportable cells are considered. The
4069 function returns nil for other cells."
4070 (let* ((table-row (org-export-get-parent table-cell
))
4071 (table (org-export-get-parent-table table-cell
)))
4072 ;; Ignore cells in special rows or in special column.
4073 (unless (or (org-export-table-row-is-special-p table-row info
)
4074 (and (org-export-table-has-special-column-p table
)
4075 (eq (car (org-element-contents table-row
)) table-cell
)))
4078 (let ((row-count 0))
4082 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4083 ((eq row table-row
) row-count
)
4084 (t (incf row-count
) nil
)))
4087 (let ((col-count 0))
4089 table-row
'table-cell
4091 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4092 info
'first-match
))))))
4094 (defun org-export-get-table-cell-at (address table info
)
4095 "Return regular table-cell object at ADDRESS in TABLE.
4097 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4098 zero-based index. TABLE is a table type element. INFO is
4099 a plist used as a communication channel.
4101 If no table-cell, among exportable cells, is found at ADDRESS,
4103 (let ((column-pos (cdr address
)) (column-count 0))
4105 ;; Row at (car address) or nil.
4106 (let ((row-pos (car address
)) (row-count 0))
4110 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4111 ((= row-count row-pos
) row
)
4112 (t (incf row-count
) nil
)))
4116 (if (= column-count column-pos
) cell
4117 (incf column-count
) nil
))
4118 info
'first-match
)))
4121 ;;;; For Tables Of Contents
4123 ;; `org-export-collect-headlines' builds a list of all exportable
4124 ;; headline elements, maybe limited to a certain depth. One can then
4125 ;; easily parse it and transcode it.
4127 ;; Building lists of tables, figures or listings is quite similar.
4128 ;; Once the generic function `org-export-collect-elements' is defined,
4129 ;; `org-export-collect-tables', `org-export-collect-figures' and
4130 ;; `org-export-collect-listings' can be derived from it.
4132 (defun org-export-collect-headlines (info &optional n
)
4133 "Collect headlines in order to build a table of contents.
4135 INFO is a plist used as a communication channel.
4137 When optional argument N is an integer, it specifies the depth of
4138 the table of contents. Otherwise, it is set to the value of the
4139 last headline level. See `org-export-headline-levels' for more
4142 Return a list of all exportable headlines as parsed elements."
4143 (unless (wholenump n
) (setq n
(plist-get info
:headline-levels
)))
4145 (plist-get info
:parse-tree
)
4148 ;; Strip contents from HEADLINE.
4149 (let ((relative-level (org-export-get-relative-level headline info
)))
4150 (unless (> relative-level n
) headline
)))
4153 (defun org-export-collect-elements (type info
&optional predicate
)
4154 "Collect referenceable elements of a determined type.
4156 TYPE can be a symbol or a list of symbols specifying element
4157 types to search. Only elements with a caption are collected.
4159 INFO is a plist used as a communication channel.
4161 When non-nil, optional argument PREDICATE is a function accepting
4162 one argument, an element of type TYPE. It returns a non-nil
4163 value when that element should be collected.
4165 Return a list of all elements found, in order of appearance."
4167 (plist-get info
:parse-tree
) type
4169 (and (org-element-property :caption element
)
4170 (or (not predicate
) (funcall predicate element
))
4174 (defun org-export-collect-tables (info)
4175 "Build a list of tables.
4176 INFO is a plist used as a communication channel.
4178 Return a list of table elements with a caption."
4179 (org-export-collect-elements 'table info
))
4181 (defun org-export-collect-figures (info predicate
)
4182 "Build a list of figures.
4184 INFO is a plist used as a communication channel. PREDICATE is
4185 a function which accepts one argument: a paragraph element and
4186 whose return value is non-nil when that element should be
4189 A figure is a paragraph type element, with a caption, verifying
4190 PREDICATE. The latter has to be provided since a \"figure\" is
4191 a vague concept that may depend on back-end.
4193 Return a list of elements recognized as figures."
4194 (org-export-collect-elements 'paragraph info predicate
))
4196 (defun org-export-collect-listings (info)
4197 "Build a list of src blocks.
4199 INFO is a plist used as a communication channel.
4201 Return a list of src-block elements with a caption."
4202 (org-export-collect-elements 'src-block info
))
4207 (defconst org-export-smart-quotes-alist
4209 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4210 :texinfo
"@quotedblbase{}")
4211 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4212 :texinfo
"@quotedblleft{}")
4213 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4214 :texinfo
"@quotesinglbase{}")
4215 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4216 :texinfo
"@quoteleft{}")
4217 (apostrophe :utf-8
"’" :html
"’"))
4219 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4220 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4221 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4222 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4223 (apostrophe :utf-8
"’" :html
"’"))
4225 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4226 :texinfo
"@guillemetleft{}")
4227 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4228 :texinfo
"@guillemetright{}")
4229 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4230 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4231 (apostrophe :utf-8
"’" :html
"’"))
4233 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
4234 :texinfo
"@guillemetleft{}@tie{}")
4235 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4236 :texinfo
"@tie{}@guillemetright{}")
4237 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
4238 :texinfo
"@guillemetleft{}@tie{}")
4239 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4240 :texinfo
"@tie{}@guillemetright{}")
4241 (apostrophe :utf-8
"’" :html
"’")))
4242 "Smart quotes translations.
4244 Alist whose CAR is a language string and CDR is an alist with
4245 quote type as key and a plist associating various encodings to
4246 their translation as value.
4248 A quote type can be any symbol among `opening-double-quote',
4249 `closing-double-quote', `opening-single-quote',
4250 `closing-single-quote' and `apostrophe'.
4252 Valid encodings include `:utf-8', `:html', `:latex' and
4255 If no translation is found, the quote character is left as-is.")
4257 (defconst org-export-smart-quotes-regexps
4259 ;; Possible opening quote at beginning of string.
4260 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4261 ;; Possible closing quote at beginning of string.
4262 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4263 ;; Possible apostrophe at beginning of string.
4265 ;; Opening single and double quotes.
4266 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4267 ;; Closing single and double quotes.
4268 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4271 ;; Possible opening quote at end of string.
4272 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4273 ;; Possible closing quote at end of string.
4274 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4275 ;; Possible apostrophe at end of string.
4277 "List of regexps matching a quote or an apostrophe.
4278 In every regexp, quote or apostrophe matched is put in group 1.")
4280 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
4281 "Replace regular quotes with \"smart\" quotes in string S.
4283 ENCODING is a symbol among `:html', `:latex' and `:utf-8'. INFO
4284 is a plist used as a communication channel.
4286 The function has to retrieve information about string
4287 surroundings in parse tree. It can only happen with an
4288 unmodified string. Thus, if S has already been through another
4289 process, a non-nil ORIGINAL optional argument will provide that
4292 Return the new string."
4294 (let ((quotes-alist (cdr (assoc (plist-get info
:language
)
4295 org-export-smart-quotes-alist
))))
4296 ;; 1. Replace quote character at the beginning of S.
4297 (let* ((prev (org-export-get-previous-element (or original s
) info
))
4298 (pre-blank (and prev
(org-element-property :post-blank prev
))))
4301 ((and prev
(zerop pre-blank
)
4302 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
4304 (plist-get (cdr (assq 'apostrophe quotes-alist
)) encoding
)))
4306 (setq s
(replace-match smart-quote nil t s
1)))))
4308 ((and prev
(zerop pre-blank
)
4309 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
4311 (plist-get (cdr (assq (if (equal (match-string 1 s
) "'")
4312 'closing-single-quote
4313 'closing-double-quote
)
4317 (setq s
(replace-match smart-quote nil t s
1)))))
4319 ((and (or (not prev
) (> pre-blank
0))
4320 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
4322 (plist-get (cdr (assq (if (equal (match-string 1 s
) "'")
4323 'opening-single-quote
4324 'opening-double-quote
)
4328 (setq s
(replace-match smart-quote nil t s
1)))))))
4329 ;; 2. Replace quotes in the middle of the string.
4332 (replace-regexp-in-string
4333 (nth 3 org-export-smart-quotes-regexps
)
4336 (cdr (assq (if (equal (match-string 1 text
) "'")
4337 'opening-single-quote
4338 'opening-double-quote
)
4341 (match-string 1 text
)))
4344 (replace-regexp-in-string
4346 (nth 4 org-export-smart-quotes-regexps
)
4349 (cdr (assq (if (equal (match-string 1 text
) "'")
4350 'closing-single-quote
4351 'closing-double-quote
)
4354 (match-string 1 text
)))
4357 (replace-regexp-in-string
4359 (nth 5 org-export-smart-quotes-regexps
)
4361 (or (plist-get (cdr (assq 'apostrophe quotes-alist
)) encoding
)
4362 (match-string 1 text
)))
4364 ;; 3. Replace quote character at the end of S.
4365 (let ((next (org-export-get-next-element (or original s
) info
)))
4368 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
4370 (plist-get (cdr (assq 'apostrophe quotes-alist
)) encoding
)))
4371 (when smart-quote
(setq s
(replace-match smart-quote nil t s
1)))))
4374 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
4376 (plist-get (cdr (assq (if (equal (match-string 1 s
) "'")
4377 'closing-single-quote
4378 'closing-double-quote
)
4381 (when smart-quote
(setq s
(replace-match smart-quote nil t s
1)))))
4383 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
4385 (plist-get (cdr (assq (if (equal (match-string 1 s
) "'")
4386 'opening-single-quote
4387 'opening-double-quote
)
4391 (setq s
(replace-match smart-quote nil t s
1)))))))
4392 ;; Return string with smart quotes.
4398 ;; Here are various functions to retrieve information about the
4399 ;; neighbourhood of a given element or object. Neighbours of interest
4400 ;; are direct parent (`org-export-get-parent'), parent headline
4401 ;; (`org-export-get-parent-headline'), first element containing an
4402 ;; object, (`org-export-get-parent-element'), parent table
4403 ;; (`org-export-get-parent-table'), previous element or object
4404 ;; (`org-export-get-previous-element') and next element or object
4405 ;; (`org-export-get-next-element').
4407 ;; `org-export-get-genealogy' returns the full genealogy of a given
4408 ;; element or object, from closest parent to full parse tree.
4410 (defun org-export-get-parent (blob)
4411 "Return BLOB parent or nil.
4412 BLOB is the element or object considered."
4413 (org-element-property :parent blob
))
4415 (defun org-export-get-genealogy (blob)
4416 "Return full genealogy relative to a given element or object.
4418 BLOB is the element or object being considered.
4420 Ancestors are returned from closest to farthest, the last one
4421 being the full parse tree."
4422 (let (genealogy (parent blob
))
4423 (while (setq parent
(org-element-property :parent parent
))
4424 (push parent genealogy
))
4425 (nreverse genealogy
)))
4427 (defun org-export-get-parent-headline (blob)
4428 "Return BLOB parent headline or nil.
4429 BLOB is the element or object being considered."
4430 (let ((parent blob
))
4431 (while (and (setq parent
(org-element-property :parent parent
))
4432 (not (eq (org-element-type parent
) 'headline
))))
4435 (defun org-export-get-parent-element (object)
4436 "Return first element containing OBJECT or nil.
4437 OBJECT is the object to consider."
4438 (let ((parent object
))
4439 (while (and (setq parent
(org-element-property :parent parent
))
4440 (memq (org-element-type parent
) org-element-all-objects
)))
4443 (defun org-export-get-parent-table (object)
4444 "Return OBJECT parent table or nil.
4445 OBJECT is either a `table-cell' or `table-element' type object."
4446 (let ((parent object
))
4447 (while (and (setq parent
(org-element-property :parent parent
))
4448 (not (eq (org-element-type parent
) 'table
))))
4451 (defun org-export-get-previous-element (blob info
)
4452 "Return previous element or object.
4453 BLOB is an element or object. INFO is a plist used as
4454 a communication channel. Return previous exportable element or
4455 object, a string, or nil."
4459 (cond ((eq obj blob
) (throw 'exit prev
))
4460 ((memq obj
(plist-get info
:ignore-list
)))
4461 (t (setq prev obj
))))
4462 (org-element-contents (org-export-get-parent blob
))))))
4464 (defun org-export-get-next-element (blob info
)
4465 "Return next element or object.
4466 BLOB is an element or object. INFO is a plist used as
4467 a communication channel. Return next exportable element or
4468 object, a string, or nil."
4471 (unless (memq obj
(plist-get info
:ignore-list
))
4472 (throw 'found obj
)))
4473 (cdr (memq blob
(org-element-contents (org-export-get-parent blob
)))))
4479 ;; `org-export-translate' translates a string according to language
4480 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4481 ;; variable and a specified charset. `org-export-dictionary' contains
4482 ;; the dictionary used for the translation.
4484 (defconst org-export-dictionary
4486 ("ca" :default
"Autor")
4487 ("cs" :default
"Autor")
4488 ("da" :default
"Ophavsmand")
4489 ("de" :default
"Autor")
4490 ("eo" :html
"Aŭtoro")
4491 ("es" :default
"Autor")
4492 ("fi" :html
"Tekijä")
4493 ("fr" :default
"Auteur")
4494 ("hu" :default
"Szerzõ")
4495 ("is" :html
"Höfundur")
4496 ("it" :default
"Autore")
4497 ("ja" :html
"著者" :utf-8
"著者")
4498 ("nl" :default
"Auteur")
4499 ("no" :default
"Forfatter")
4500 ("nb" :default
"Forfatter")
4501 ("nn" :default
"Forfattar")
4502 ("pl" :default
"Autor")
4503 ("ru" :html
"Автор" :utf-8
"Автор")
4504 ("sv" :html
"Författare")
4505 ("uk" :html
"Автор" :utf-8
"Автор")
4506 ("zh-CN" :html
"作者" :utf-8
"作者")
4507 ("zh-TW" :html
"作者" :utf-8
"作者"))
4509 ("ca" :default
"Data")
4510 ("cs" :default
"Datum")
4511 ("da" :default
"Dato")
4512 ("de" :default
"Datum")
4513 ("eo" :default
"Dato")
4514 ("es" :default
"Fecha")
4515 ("fi" :html
"Päivämäärä")
4516 ("hu" :html
"Dátum")
4517 ("is" :default
"Dagsetning")
4518 ("it" :default
"Data")
4519 ("ja" :html
"日付" :utf-8
"日付")
4520 ("nl" :default
"Datum")
4521 ("no" :default
"Dato")
4522 ("nb" :default
"Dato")
4523 ("nn" :default
"Dato")
4524 ("pl" :default
"Data")
4525 ("ru" :html
"Дата" :utf-8
"Дата")
4526 ("sv" :default
"Datum")
4527 ("uk" :html
"Дата" :utf-8
"Дата")
4528 ("zh-CN" :html
"日期" :utf-8
"日期")
4529 ("zh-TW" :html
"日期" :utf-8
"日期"))
4531 ("fr" :ascii
"Equation" :default
"Équation"))
4534 ("ca" :html
"Peus de pàgina")
4535 ("cs" :default
"Pozn\xe1mky pod carou")
4536 ("da" :default
"Fodnoter")
4537 ("de" :html
"Fußnoten")
4538 ("eo" :default
"Piednotoj")
4539 ("es" :html
"Pies de página")
4540 ("fi" :default
"Alaviitteet")
4541 ("fr" :default
"Notes de bas de page")
4542 ("hu" :html
"Lábjegyzet")
4543 ("is" :html
"Aftanmálsgreinar")
4544 ("it" :html
"Note a piè di pagina")
4545 ("ja" :html
"脚注" :utf-8
"脚注")
4546 ("nl" :default
"Voetnoten")
4547 ("no" :default
"Fotnoter")
4548 ("nb" :default
"Fotnoter")
4549 ("nn" :default
"Fotnotar")
4550 ("pl" :default
"Przypis")
4551 ("ru" :html
"Сноски" :utf-8
"Сноски")
4552 ("sv" :default
"Fotnoter")
4553 ("uk" :html
"Примітки"
4555 ("zh-CN" :html
"脚注" :utf-8
"脚注")
4556 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
4558 ("fr" :default
"Liste des programmes"))
4560 ("fr" :default
"Liste des tableaux"))
4563 :ascii
"Programme %d :" :default
"Programme nº %d :"
4564 :latin1
"Programme %d :"))
4567 :ascii
"Programme %d : %s" :default
"Programme nº %d : %s"
4568 :latin1
"Programme %d : %s"))
4570 ("fr" :default
"cf. section %s"))
4573 :ascii
"Tableau %d :" :default
"Tableau nº %d :" :latin1
"Tableau %d :"))
4576 :ascii
"Tableau %d : %s" :default
"Tableau nº %d : %s"
4577 :latin1
"Tableau %d : %s"))
4578 ("Table of Contents"
4579 ("ca" :html
"Índex")
4580 ("cs" :default
"Obsah")
4581 ("da" :default
"Indhold")
4582 ("de" :default
"Inhaltsverzeichnis")
4583 ("eo" :default
"Enhavo")
4584 ("es" :html
"Índice")
4585 ("fi" :html
"Sisällysluettelo")
4586 ("fr" :ascii
"Sommaire" :default
"Table des matières")
4587 ("hu" :html
"Tartalomjegyzék")
4588 ("is" :default
"Efnisyfirlit")
4589 ("it" :default
"Indice")
4590 ("ja" :html
"目次" :utf-8
"目次")
4591 ("nl" :default
"Inhoudsopgave")
4592 ("no" :default
"Innhold")
4593 ("nb" :default
"Innhold")
4594 ("nn" :default
"Innhald")
4595 ("pl" :html
"Spis treści")
4596 ("ru" :html
"Содержание"
4597 :utf-8
"Содержание")
4598 ("sv" :html
"Innehåll")
4599 ("uk" :html
"Зміст" :utf-8
"Зміст")
4600 ("zh-CN" :html
"目录" :utf-8
"目录")
4601 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
4602 ("Unknown reference"
4603 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")))
4604 "Dictionary for export engine.
4606 Alist whose CAR is the string to translate and CDR is an alist
4607 whose CAR is the language string and CDR is a plist whose
4608 properties are possible charsets and values translated terms.
4610 It is used as a database for `org-export-translate'. Since this
4611 function returns the string as-is if no translation was found,
4612 the variable only needs to record values different from the
4615 (defun org-export-translate (s encoding info
)
4616 "Translate string S according to language specification.
4618 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4619 and `:utf-8'. INFO is a plist used as a communication channel.
4621 Translation depends on `:language' property. Return the
4622 translated string. If no translation is found, try to fall back
4623 to `:default' encoding. If it fails, return S."
4624 (let* ((lang (plist-get info
:language
))
4625 (translations (cdr (assoc lang
4626 (cdr (assoc s org-export-dictionary
))))))
4627 (or (plist-get translations encoding
)
4628 (plist-get translations
:default
)
4635 ;; `org-export-dispatch' is the standard interactive way to start an
4636 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4637 ;; for its interface, which, in turn, delegates response to key
4638 ;; pressed to `org-export-dispatch-action'.
4640 (defvar org-export-dispatch-menu-entries nil
4641 "List of menu entries available for `org-export-dispatch'.
4642 This variable shouldn't be set directly. Set-up :menu-entry
4643 keyword in either `org-export-define-backend' or
4644 `org-export-define-derived-backend' instead.")
4647 (defun org-export-dispatch ()
4648 "Export dispatcher for Org mode.
4650 It provides an access to common export related tasks in a buffer.
4651 Its interface comes in two flavours: standard and expert. While
4652 both share the same set of bindings, only the former displays the
4653 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4654 to switch to one or the other."
4656 (let* ((input (save-window-excursion
4658 (org-export-dispatch-ui (list org-export-initial-scope
)
4660 org-export-dispatch-use-expert-ui
)
4661 (and (get-buffer "*Org Export Dispatcher*")
4662 (kill-buffer "*Org Export Dispatcher*")))))
4663 (action (car input
))
4664 (optns (cdr input
)))
4666 ;; First handle special hard-coded actions.
4667 (publish-current-file (org-e-publish-current-file (memq 'force optns
)))
4668 (publish-current-project
4669 (org-e-publish-current-project (memq 'force optns
)))
4670 (publish-choose-project
4671 (org-e-publish (assoc (org-icompleting-read
4673 org-e-publish-project-alist nil t
)
4674 org-e-publish-project-alist
)
4675 (memq 'force optns
)))
4676 (publish-all (org-e-publish-all (memq 'force optns
)))
4679 (memq 'subtree optns
)
4680 (memq 'visible optns
)
4681 (memq 'body optns
))))))
4683 (defun org-export-dispatch-ui (options first-key expertp
)
4684 "Handle interface for `org-export-dispatch'.
4686 OPTIONS is a list containing current interactive options set for
4687 export. It can contain any of the following symbols:
4688 `body' toggles a body-only export
4689 `subtree' restricts export to current subtree
4690 `visible' restricts export to visible part of buffer.
4691 `force' force publishing files.
4693 FIRST-KEY is the key pressed to select the first level menu. It
4694 is nil when this menu hasn't been selected yet.
4696 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4697 buffer is provided, but indications about currently active
4698 options are given in the prompt. Moreover, \[?] allows to switch
4699 back to standard interface."
4701 (lambda (key &optional access-key
)
4702 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4703 ;; non-nil is the required first-level key to activate
4704 ;; KEY. When its value is t, activate KEY independently
4705 ;; on the first key, if any. A nil value means KEY will
4706 ;; only be activated at first level.
4707 (if (or (eq access-key t
) (eq access-key first-key
))
4708 (org-add-props key nil
'face
'org-warning
)
4709 (org-no-properties key
))))
4710 ;; Make sure order of menu doesn't depend on the order in
4711 ;; which back-ends are loaded.
4712 (backends (sort (copy-sequence org-export-dispatch-menu-entries
)
4713 (lambda (a b
) (< (car a
) (car b
)))))
4714 ;; Compute a list of allowed keys based on the first key
4715 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4716 ;; always available.
4718 (nconc (list ?
1 ?
2 ?
3 ?
4)
4720 (if (not first-key
) backends
4721 (nth 2 (assq first-key backends
))))
4722 (cond ((eq first-key ?P
) (list ?f ?p ?x ?a
))
4723 ((not first-key
) (list ?P
)))
4724 (when expertp
(list ??
))
4726 ;; Build the help menu for standard UI.
4730 ;; Options are hard-coded.
4732 [%s] Body only: %s [%s] Visible only: %s
4733 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4734 (funcall fontify-key
"1" t
)
4735 (if (memq 'body options
) "On " "Off")
4736 (funcall fontify-key
"2" t
)
4737 (if (memq 'visible options
) "On " "Off")
4738 (funcall fontify-key
"3" t
)
4739 (if (memq 'subtree options
) "Subtree" "Buffer ")
4740 (funcall fontify-key
"4" t
)
4741 (if (memq 'force options
) "On " "Off"))
4742 ;; Display registered back-end entries.
4745 (let ((top-key (car entry
)))
4748 (funcall fontify-key
(char-to-string top-key
))
4750 (let ((sub-menu (nth 2 entry
)))
4751 (unless (functionp sub-menu
)
4752 ;; Split sub-menu into two columns.
4758 (format (if (zerop (mod index
2)) " [%s] %-24s"
4760 (funcall fontify-key
4761 (char-to-string (car sub-entry
))
4765 (when (zerop (mod index
2)) "\n"))))))))
4767 ;; Publishing menu is hard-coded.
4768 (format "\n[%s] Publish
4769 [%s] Current file [%s] Current project
4770 [%s] Choose project [%s] All projects\n\n"
4771 (funcall fontify-key
"P")
4772 (funcall fontify-key
"f" ?P
)
4773 (funcall fontify-key
"p" ?P
)
4774 (funcall fontify-key
"x" ?P
)
4775 (funcall fontify-key
"a" ?P
))
4777 (funcall fontify-key
"q" t
)
4778 (if first-key
"Main menu" "Exit")))))
4779 ;; Build prompts for both standard and expert UI.
4780 (standard-prompt (unless expertp
"Export command: "))
4784 "Export command (Options: %s%s%s%s) [%s]: "
4785 (if (memq 'body options
) (funcall fontify-key
"b" t
) "-")
4786 (if (memq 'visible options
) (funcall fontify-key
"v" t
) "-")
4787 (if (memq 'subtree options
) (funcall fontify-key
"s" t
) "-")
4788 (if (memq 'force options
) (funcall fontify-key
"f" t
) "-")
4789 (concat allowed-keys
)))))
4790 ;; With expert UI, just read key with a fancy prompt. In standard
4791 ;; UI, display an intrusive help buffer.
4793 (org-export-dispatch-action
4794 expert-prompt allowed-keys backends options first-key expertp
)
4795 ;; At first call, create frame layout in order to display menu.
4796 (unless (get-buffer "*Org Export Dispatcher*")
4797 (delete-other-windows)
4798 (org-switch-to-buffer-other-window
4799 (get-buffer-create "*Org Export Dispatcher*"))
4800 (setq cursor-type nil
))
4801 ;; At this point, the buffer containing the menu exists and is
4802 ;; visible in the current window. So, refresh it.
4803 (with-current-buffer "*Org Export Dispatcher*"
4806 (org-fit-window-to-buffer)
4807 (org-export-dispatch-action
4808 standard-prompt allowed-keys backends options first-key expertp
))))
4810 (defun org-export-dispatch-action
4811 (prompt allowed-keys backends options first-key expertp
)
4812 "Read a character from command input and act accordingly.
4814 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4815 a list of characters available at a given step in the process.
4816 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4817 EXPERTP are the same as defined in `org-export-dispatch-ui',
4820 Toggle export options when required. Otherwise, return value is
4821 a list with action as CAR and a list of interactive export
4823 (let ((key (let ((k (read-char-exclusive prompt
)))
4824 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4825 ;; depending on user's key pressed.
4826 (if (< k
27) (+ k
96) k
))))
4828 ;; Ignore non-standard characters (i.e. "M-a") and
4829 ;; undefined associations.
4830 ((not (memq key allowed-keys
))
4832 (unless expertp
(message "Invalid key") (sit-for 1))
4833 (org-export-dispatch-ui options first-key expertp
))
4834 ;; q key at first level aborts export. At second
4835 ;; level, cancel first key instead.
4836 ((eq key ?q
) (if (not first-key
) (error "Export aborted")
4837 (org-export-dispatch-ui options nil expertp
)))
4838 ;; Help key: Switch back to standard interface if
4839 ;; expert UI was active.
4840 ((eq key ??
) (org-export-dispatch-ui options first-key nil
))
4841 ;; Toggle export options.
4842 ((memq key
'(?
1 ?
2 ?
3 ?
4))
4843 (org-export-dispatch-ui
4844 (let ((option (case key
(?
1 'body
) (?
2 'visible
) (?
3 'subtree
)
4846 (if (memq option options
) (remq option options
)
4847 (cons option options
)))
4849 ;; Action selected: Send key and options back to
4850 ;; `org-export-dispatch'.
4852 (and (eq first-key ?P
) (memq key
'(?f ?p ?x ?a
)))
4853 (functionp (nth 2 (assq key backends
))))
4855 ((not first-key
) (nth 2 (assq key backends
)))
4856 ;; Publishing actions are hard-coded. Send a special
4857 ;; signal to `org-export-dispatch'.
4860 (?f
'publish-current-file
)
4861 (?p
'publish-current-project
)
4862 (?x
'publish-choose-project
)
4864 (t (nth 2 (assq key
(nth 2 (assq first-key backends
))))))
4866 ;; Otherwise, enter sub-menu.
4867 (t (org-export-dispatch-ui options key expertp
)))))
4870 (provide 'org-export
)
4871 ;;; org-export.el ends here