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
)
78 (declare-function org-e-publish
"org-e-publish" (project &optional force
))
79 (declare-function org-e-publish-all
"org-e-publish" (&optional force
))
80 (declare-function org-e-publish-current-file
"org-e-publish" (&optional force
))
81 (declare-function org-e-publish-current-project
"org-e-publish"
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
'("FILETAGS" "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.
584 This may have the following values:
585 t include tasks independent of state.
586 `todo' include only tasks that are not yet done.
587 `done' include only tasks that are already done.
588 nil ignore all tasks.
589 list of keywords include tasks with these keywords.
591 This option can also be set with the #+OPTIONS line,
593 :group
'org-export-general
595 (const :tag
"All tasks" t
)
596 (const :tag
"No tasks" nil
)
597 (const :tag
"Not-done tasks" todo
)
598 (const :tag
"Only done tasks" done
)
599 (repeat :tag
"Specific TODO keywords"
600 (string :tag
"Keyword"))))
602 (defcustom org-export-time-stamp-file t
603 "Non-nil means insert a time stamp into the exported file.
604 The time stamp shows when the file was created.
606 This option can also be set with the #+OPTIONS line,
607 e.g. \"timestamp:nil\"."
608 :group
'org-export-general
611 (defcustom org-export-with-timestamps t
612 "Non nil means allow timestamps in export.
614 It can be set to `active', `inactive', t or nil, in order to
615 export, respectively, only active timestamps, only inactive ones,
618 This option can also be set with the #+OPTIONS line, e.g.
620 :group
'org-export-general
622 (const :tag
"All timestamps" t
)
623 (const :tag
"Only active timestamps" active
)
624 (const :tag
"Only inactive timestamps" inactive
)
625 (const :tag
"No timestamp" nil
)))
627 (defcustom org-export-with-todo-keywords t
628 "Non-nil means include TODO keywords in export.
629 When nil, remove all these keywords from the export."
630 :group
'org-export-general
633 (defcustom org-export-allow-BIND
'confirm
634 "Non-nil means allow #+BIND to define local variable values for export.
635 This is a potential security risk, which is why the user must
636 confirm the use of these lines."
637 :group
'org-export-general
639 (const :tag
"Never" nil
)
640 (const :tag
"Always" t
)
641 (const :tag
"Ask a confirmation for each file" confirm
)))
643 (defcustom org-export-snippet-translation-alist nil
644 "Alist between export snippets back-ends and exporter back-ends.
646 This variable allows to provide shortcuts for export snippets.
648 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
649 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
650 HTML code while every other back-end will ignore it."
651 :group
'org-export-general
654 (string :tag
"Shortcut")
655 (string :tag
"Back-end"))))
657 (defcustom org-export-coding-system nil
658 "Coding system for the exported file."
659 :group
'org-export-general
660 :type
'coding-system
)
662 (defcustom org-export-copy-to-kill-ring t
663 "Non-nil means exported stuff will also be pushed onto the kill ring."
664 :group
'org-export-general
667 (defcustom org-export-initial-scope
'buffer
668 "The initial scope when exporting with `org-export-dispatch'.
669 This variable can be either set to `buffer' or `subtree'."
670 :group
'org-export-general
672 (const :tag
"Export current buffer" 'buffer
)
673 (const :tag
"Export current subtree" 'subtree
)))
675 (defcustom org-export-show-temporary-export-buffer t
676 "Non-nil means show buffer after exporting to temp buffer.
677 When Org exports to a file, the buffer visiting that file is ever
678 shown, but remains buried. However, when exporting to
679 a temporary buffer, that buffer is popped up in a second window.
680 When this variable is nil, the buffer remains buried also in
682 :group
'org-export-general
685 (defcustom org-export-dispatch-use-expert-ui nil
686 "Non-nil means using a non-intrusive `org-export-dispatch'.
687 In that case, no help buffer is displayed. Though, an indicator
688 for current export scope is added to the prompt (\"b\" when
689 output is restricted to body only, \"s\" when it is restricted to
690 the current subtree, \"v\" when only visible elements are
691 considered for export and \"f\" when publishing functions should
692 be passed the FORCE argument). Also, \[?] allows to switch back
694 :group
'org-export-general
699 ;;; Defining New Back-ends
701 ;; `org-export-define-backend' is the standard way to define an export
702 ;; back-end. It allows to specify translators, filters, buffer
703 ;; options and a menu entry. If the new back-end shares translators
704 ;; with another back-end, `org-export-define-derived-backend' may be
707 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
708 ;; when a given back-end hasn't been registered yet.
710 (defmacro org-export-define-backend
(backend translators
&rest body
)
711 "Define a new back-end BACKEND.
713 TRANSLATORS is an alist between object or element types and
714 functions handling them.
716 These functions should return a string without any trailing
717 space, or nil. They must accept three arguments: the object or
718 element itself, its contents or nil when it isn't recursive and
719 the property list used as a communication channel.
721 Contents, when not nil, are stripped from any global indentation
722 \(although the relative one is preserved). They also always end
723 with a single newline character.
725 If, for a given type, no function is found, that element or
726 object type will simply be ignored, along with any blank line or
727 white space at its end. The same will happen if the function
728 returns the nil value. If that function returns the empty
729 string, the type will be ignored, but the blank lines or white
732 In addition to element and object types, one function can be
733 associated to the `template' symbol and another one to the
736 The former returns the final transcoded string, and can be used
737 to add a preamble and a postamble to document's body. It must
738 accept two arguments: the transcoded string and the property list
739 containing export options.
741 The latter, when defined, is to be called on every text not
742 recognized as an element or an object. It must accept two
743 arguments: the text string and the information channel. It is an
744 appropriate place to protect special chars relative to the
747 BODY can start with pre-defined keyword arguments. The following
748 keywords are understood:
752 String, or list of strings, representing block names that
753 will not be parsed. This is used to specify blocks that will
754 contain raw code specific to the back-end. These blocks
755 still have to be handled by the relative `export-block' type
760 Alist between filters and function, or list of functions,
761 specific to the back-end. See `org-export-filters-alist' for
762 a list of all allowed filters. Filters defined here
763 shouldn't make a back-end test, as it may prevent back-ends
764 derived from this one to behave properly.
768 Menu entry for the export dispatcher. It should be a list
771 \(KEY DESCRIPTION ACTION-OR-MENU)
775 KEY is a free character selecting the back-end.
776 DESCRIPTION is a string naming the back-end.
777 ACTION-OR-MENU is either a function or an alist.
779 If it is an action, it will be called with three arguments:
780 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
781 for further explanations.
783 If it is an alist, associations should follow the
786 \(KEY DESCRIPTION ACTION)
788 where KEY, DESCRIPTION and ACTION are described above.
790 Valid values include:
792 \(?m \"My Special Back-end\" my-special-export-function)
796 \(?l \"Export to LaTeX\"
797 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
798 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
799 \(?p \"PDF file\" org-e-latex-export-to-pdf)
800 \(?o \"PDF file and open\"
801 \(lambda (subtree visible body-only)
803 \(org-e-latex-export-to-pdf subtree visible body-only))))))
807 Alist between back-end specific properties introduced in
808 communication channel and how their value are acquired. See
809 `org-export-options-alist' for more information about
810 structure of the values."
811 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
813 (let (export-block filters menu-entry options
)
814 (while (keywordp (car body
))
816 (:export-block
(let ((names (pop body
)))
818 (if (consp names
) (mapcar 'upcase names
)
819 (list (upcase names
))))))
820 (:filters-alist
(setq filters
(pop body
)))
821 (:menu-entry
(setq menu-entry
(pop body
)))
822 (:options-alist
(setq options
(pop body
)))
825 ;; Define translators.
826 (defvar ,(intern (format "org-%s-translate-alist" backend
)) ',translators
827 "Alist between element or object types and translators.")
830 `(defconst ,(intern (format "org-%s-options-alist" backend
)) ',options
831 ,(format "Alist between %s export properties and ways to set them.
832 See `org-export-options-alist' for more information on the
833 structure of the values."
837 `(defconst ,(intern (format "org-%s-filters-alist" backend
)) ',filters
838 "Alist between filters keywords and back-end specific filters.
839 See `org-export-filters-alist' for more information."))
840 ;; Tell parser to not parse EXPORT-BLOCK blocks.
844 (add-to-list 'org-element-block-name-alist
845 `(,name . org-element-export-block-parser
)))
847 ;; Add an entry for back-end in `org-export-dispatch'.
849 `(unless (assq (car ',menu-entry
) org-export-dispatch-menu-entries
)
850 (add-to-list 'org-export-dispatch-menu-entries
',menu-entry
)))
851 ;; Splice in the body, if any.
854 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
855 "Create a new back-end as a variant of an existing one.
857 CHILD is the name of the derived back-end. PARENT is the name of
860 BODY can start with pre-defined keyword arguments. The following
861 keywords are understood:
865 String, or list of strings, representing block names that
866 will not be parsed. This is used to specify blocks that will
867 contain raw code specific to the back-end. These blocks
868 still have to be handled by the relative `export-block' type
873 Alist of filters that will overwrite or complete filters
874 defined in PARENT back-end. See `org-export-filters-alist'
875 for a list of allowed filters.
879 Menu entry for the export dispatcher. See
880 `org-export-define-backend' for more information about the
885 Alist of back-end specific properties that will overwrite or
886 complete those defined in PARENT back-end. Refer to
887 `org-export-options-alist' for more information about
888 structure of the values.
892 Append entries to an existing menu in the export dispatcher.
893 The associated value should be a list whose CAR is the
894 character selecting the menu to expand and CDR a list of
895 entries following the pattern:
897 \(KEY DESCRIPTION ACTION)
899 where KEY is a free character triggering the action,
900 DESCRIPTION is a string defining the action, and ACTION is
901 a function that will be called with three arguments:
902 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
903 for further explanations.
905 Valid values include:
907 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
908 \(?O \"As PDF file and open (Beamer)\"
910 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
914 Alist of element and object types and transcoders that will
915 overwrite or complete transcode table from PARENT back-end.
916 Refer to `org-export-define-backend' for detailed information
919 As an example, here is how one could define \"my-latex\" back-end
920 as a variant of `e-latex' back-end with a custom template
923 \(org-export-define-derived-backend my-latex e-latex
924 :translate-alist ((template . my-latex-template-fun)))
926 The back-end could then be called with, for example:
928 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
929 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] def-body
))
931 (let (export-block filters menu-entry options sub-menu-entry translate
)
932 (while (keywordp (car body
))
934 (:export-block
(let ((names (pop body
)))
936 (if (consp names
) (mapcar 'upcase names
)
937 (list (upcase names
))))))
938 (:filters-alist
(setq filters
(pop body
)))
939 (:menu-entry
(setq menu-entry
(pop body
)))
940 (:options-alist
(setq options
(pop body
)))
941 (:sub-menu-entry
(setq sub-menu-entry
(pop body
)))
942 (:translate-alist
(setq translate
(pop body
)))
945 ;; Tell parser to not parse EXPORT-BLOCK blocks.
949 (add-to-list 'org-element-block-name-alist
950 `(,name . org-element-export-block-parser
)))
953 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent
))))
954 (when (or (boundp parent-filters
) filters
)
955 `(defconst ,(intern (format "org-%s-filters-alist" child
))
957 (and (boundp parent-filters
)
958 (copy-sequence (symbol-value parent-filters
))))
959 "Alist between filters keywords and back-end specific filters.
960 See `org-export-filters-alist' for more information.")))
962 ,(let ((parent-options (intern (format "org-%s-options-alist" parent
))))
963 (when (or (boundp parent-options
) options
)
964 `(defconst ,(intern (format "org-%s-options-alist" child
))
966 (and (boundp parent-options
)
967 (copy-sequence (symbol-value parent-options
))))
968 ,(format "Alist between %s export properties and ways to set them.
969 See `org-export-options-alist' for more information on the
970 structure of the values."
972 ;; Define translators.
973 (defvar ,(intern (format "org-%s-translate-alist" child
))
977 (intern (format "org-%s-translate-alist" parent
)))))
978 "Alist between element or object types and translators.")
979 ;; Add an entry for back-end in `org-export-dispatch'.
981 `(unless (assq (car ',menu-entry
) org-export-dispatch-menu-entries
)
982 (add-to-list 'org-export-dispatch-menu-entries
',menu-entry
)))
983 ,(when sub-menu-entry
984 (let ((menu (nth 2 (assq (car sub-menu-entry
)
985 org-export-dispatch-menu-entries
))))
986 (when menu
`(nconc ',menu
987 ',(org-remove-if (lambda (e) (member e menu
))
988 (cdr sub-menu-entry
))))))
989 ;; Splice in the body, if any.
992 (defun org-export-barf-if-invalid-backend (backend)
993 "Signal an error if BACKEND isn't defined."
994 (unless (boundp (intern (format "org-%s-translate-alist" backend
)))
995 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
999 ;;; The Communication Channel
1001 ;; During export process, every function has access to a number of
1002 ;; properties. They are of two types:
1004 ;; 1. Environment options are collected once at the very beginning of
1005 ;; the process, out of the original buffer and configuration.
1006 ;; Collecting them is handled by `org-export-get-environment'
1009 ;; Most environment options are defined through the
1010 ;; `org-export-options-alist' variable.
1012 ;; 2. Tree properties are extracted directly from the parsed tree,
1013 ;; just before export, by `org-export-collect-tree-properties'.
1015 ;; Here is the full list of properties available during transcode
1016 ;; process, with their category and their value type.
1018 ;; + `:author' :: Author's name.
1019 ;; - category :: option
1022 ;; + `:back-end' :: Current back-end used for transcoding.
1023 ;; - category :: tree
1026 ;; + `:creator' :: String to write as creation information.
1027 ;; - category :: option
1030 ;; + `:date' :: String to use as date.
1031 ;; - category :: option
1034 ;; + `:description' :: Description text for the current data.
1035 ;; - category :: option
1038 ;; + `:email' :: Author's email.
1039 ;; - category :: option
1042 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1044 ;; - category :: option
1045 ;; - type :: list of strings
1047 ;; + `:exported-data' :: Hash table used for memoizing
1048 ;; `org-export-data'.
1049 ;; - category :: tree
1050 ;; - type :: hash table
1052 ;; + `:filetags' :: List of global tags for buffer. Used by
1053 ;; `org-export-get-tags' to get tags with inheritance.
1054 ;; - category :: option
1055 ;; - type :: list of strings
1057 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1058 ;; their definition, as parsed data. Only non-inlined footnotes
1059 ;; are represented in this alist. Also, every definition isn't
1060 ;; guaranteed to be referenced in the parse tree. The purpose of
1061 ;; this property is to preserve definitions from oblivion
1062 ;; (i.e. when the parse tree comes from a part of the original
1063 ;; buffer), it isn't meant for direct use in a back-end. To
1064 ;; retrieve a definition relative to a reference, use
1065 ;; `org-export-get-footnote-definition' instead.
1066 ;; - category :: option
1067 ;; - type :: alist (STRING . LIST)
1069 ;; + `:headline-levels' :: Maximum level being exported as an
1070 ;; headline. Comparison is done with the relative level of
1071 ;; headlines in the parse tree, not necessarily with their
1073 ;; - category :: option
1074 ;; - type :: integer
1076 ;; + `:headline-offset' :: Difference between relative and real level
1077 ;; of headlines in the parse tree. For example, a value of -1
1078 ;; means a level 2 headline should be considered as level
1079 ;; 1 (cf. `org-export-get-relative-level').
1080 ;; - category :: tree
1081 ;; - type :: integer
1083 ;; + `:headline-numbering' :: Alist between headlines and their
1084 ;; numbering, as a list of numbers
1085 ;; (cf. `org-export-get-headline-number').
1086 ;; - category :: tree
1087 ;; - type :: alist (INTEGER . LIST)
1089 ;; + `:id-alist' :: Alist between ID strings and destination file's
1090 ;; path, relative to current directory. It is used by
1091 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1093 ;; - category :: option
1094 ;; - type :: alist (STRING . STRING)
1096 ;; + `:ignore-list' :: List of elements and objects that should be
1097 ;; ignored during export.
1098 ;; - category :: tree
1099 ;; - type :: list of elements and objects
1101 ;; + `:input-file' :: Full path to input file, if any.
1102 ;; - category :: option
1103 ;; - type :: string or nil
1105 ;; + `:keywords' :: List of keywords attached to data.
1106 ;; - category :: option
1109 ;; + `:language' :: Default language used for translations.
1110 ;; - category :: option
1113 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1115 ;; - category :: option
1116 ;; - type :: list (as returned by `org-element-parse-buffer')
1118 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1120 ;; - category :: option
1121 ;; - type :: symbol (nil, t)
1123 ;; + `:section-numbers' :: Non-nil means transcoding should add
1124 ;; section numbers to headlines.
1125 ;; - category :: option
1126 ;; - type :: symbol (nil, t)
1128 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1129 ;; in transcoding. When such a tag is present, subtrees without
1130 ;; it are de facto excluded from the process. See
1131 ;; `use-select-tags'.
1132 ;; - category :: option
1133 ;; - type :: list of strings
1135 ;; + `:target-list' :: List of targets encountered in the parse tree.
1136 ;; This is used to partly resolve "fuzzy" links
1137 ;; (cf. `org-export-resolve-fuzzy-link').
1138 ;; - category :: tree
1139 ;; - type :: list of strings
1141 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1142 ;; a time stamp in the output.
1143 ;; - category :: option
1144 ;; - type :: symbol (nil, t)
1146 ;; + `:translate-alist' :: Alist between element and object types and
1147 ;; transcoding functions relative to the current back-end.
1148 ;; Special keys `template' and `plain-text' are also possible.
1149 ;; - category :: option
1150 ;; - type :: alist (SYMBOL . FUNCTION)
1152 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1153 ;; also be transcoded. If it is set to the `headline' symbol,
1154 ;; only the archived headline's name is retained.
1155 ;; - category :: option
1156 ;; - type :: symbol (nil, t, `headline')
1158 ;; + `:with-author' :: Non-nil means author's name should be included
1160 ;; - category :: option
1161 ;; - type :: symbol (nil, t)
1163 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1164 ;; - category :: option
1165 ;; - type :: symbol (nil, t)
1167 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1168 ;; inserted at the end of the transcoded string. If the value
1169 ;; is `comment', it should be commented.
1170 ;; - category :: option
1171 ;; - type :: symbol (`comment', nil, t)
1173 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1174 ;; its value is a list of names, only drawers with such names
1175 ;; will be transcoded.
1176 ;; - category :: option
1177 ;; - type :: symbol (nil, t) or list of strings
1179 ;; + `:with-email' :: Non-nil means output should contain author's
1181 ;; - category :: option
1182 ;; - type :: symbol (nil, t)
1184 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1186 ;; - category :: option
1187 ;; - type :: symbol (nil, t)
1189 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1190 ;; strings starting with a colon as a fixed-with (verbatim) area.
1191 ;; - category :: option
1192 ;; - type :: symbol (nil, t)
1194 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1196 ;; - category :: option
1197 ;; - type :: symbol (nil, t)
1199 ;; + `:with-plannings' :: Non-nil means transcoding should include
1201 ;; - category :: option
1202 ;; - type :: symbol (nil, t)
1204 ;; + `:with-priority' :: Non-nil means transcoding should include
1205 ;; priority cookies.
1206 ;; - category :: option
1207 ;; - type :: symbol (nil, t)
1209 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1211 ;; - category :: option
1212 ;; - type :: symbol (nil, t)
1214 ;; + `:with-special-strings' :: Non-nil means transcoding should
1215 ;; interpret special strings in plain text.
1216 ;; - category :: option
1217 ;; - type :: symbol (nil, t)
1219 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1220 ;; interpret subscript and superscript. With a value of "{}",
1221 ;; only interpret those using curly brackets.
1222 ;; - category :: option
1223 ;; - type :: symbol (nil, {}, t)
1225 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1227 ;; - category :: option
1228 ;; - type :: symbol (nil, t)
1230 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1231 ;; headlines. A `not-in-toc' value will remove them from the
1232 ;; table of contents, if any, nonetheless.
1233 ;; - category :: option
1234 ;; - type :: symbol (nil, t, `not-in-toc')
1236 ;; + `:with-tasks' :: Non-nil means transcoding should include
1237 ;; headlines with a TODO keyword. A `todo' value will only
1238 ;; include headlines with a todo type keyword while a `done'
1239 ;; value will do the contrary. If a list of strings is provided,
1240 ;; only tasks with keywords belonging to that list will be kept.
1241 ;; - category :: option
1242 ;; - type :: symbol (t, todo, done, nil) or list of strings
1244 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1245 ;; time stamps. Special value `active' (resp. `inactive') ask to
1246 ;; export only active (resp. inactive) timestamps. Otherwise,
1247 ;; completely remove them.
1248 ;; - category :: option
1249 ;; - type :: symbol: (`active', `inactive', t, nil)
1251 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1252 ;; added to the output. An integer value limits its depth.
1253 ;; - category :: option
1254 ;; - type :: symbol (nil, t or integer)
1256 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1257 ;; include TODO keywords.
1258 ;; - category :: option
1259 ;; - type :: symbol (nil, t)
1262 ;;;; Environment Options
1264 ;; Environment options encompass all parameters defined outside the
1265 ;; scope of the parsed data. They come from five sources, in
1266 ;; increasing precedence order:
1268 ;; - Global variables,
1269 ;; - Buffer's attributes,
1270 ;; - Options keyword symbols,
1271 ;; - Buffer keywords,
1272 ;; - Subtree properties.
1274 ;; The central internal function with regards to environment options
1275 ;; is `org-export-get-environment'. It updates global variables with
1276 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1277 ;; the different sources.
1279 ;; The internal functions doing the retrieval are:
1280 ;; `org-export--get-global-options',
1281 ;; `org-export--get-buffer-attributes',
1282 ;; `org-export--parse-option-keyword',
1283 ;; `org-export--get-subtree-options' and
1284 ;; `org-export--get-inbuffer-options'
1286 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1287 ;; take care of the part relative to "#+BIND:" keywords.
1289 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1290 "Collect export options from the current buffer.
1292 Optional argument BACKEND is a symbol specifying which back-end
1293 specific options to read, if any.
1295 When optional argument SUBTREEP is non-nil, assume the export is
1296 done against the current sub-tree.
1298 Third optional argument EXT-PLIST is a property list with
1299 external parameters overriding Org default settings, but still
1300 inferior to file-local settings."
1301 ;; First install #+BIND variables.
1302 (org-export--install-letbind-maybe)
1303 ;; Get and prioritize export options...
1305 ;; ... from global variables...
1306 (org-export--get-global-options backend
)
1307 ;; ... from an external property list...
1309 ;; ... from in-buffer settings...
1310 (org-export--get-inbuffer-options
1312 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1313 ;; ... and from subtree, when appropriate.
1314 (and subtreep
(org-export--get-subtree-options backend
))
1315 ;; Eventually add misc. properties.
1320 (let ((trans-alist (intern (format "org-%s-translate-alist" backend
))))
1321 (when (boundp trans-alist
) (symbol-value trans-alist
)))
1322 :footnote-definition-alist
1323 ;; Footnotes definitions must be collected in the original
1324 ;; buffer, as there's no insurance that they will still be in
1325 ;; the parse tree, due to possible narrowing.
1327 (org-with-wide-buffer
1328 (goto-char (point-min))
1329 (while (re-search-forward org-footnote-definition-re nil t
)
1330 (let ((def (save-match-data (org-element-at-point))))
1331 (when (eq (org-element-type def
) 'footnote-definition
)
1333 (cons (org-element-property :label def
)
1334 (let ((cbeg (org-element-property :contents-begin def
)))
1336 (org-element--parse-elements
1337 cbeg
(org-element-property :contents-end def
)
1338 nil nil nil nil
(list 'org-data nil
)))))
1342 ;; Collect id references.
1344 (org-with-wide-buffer
1345 (goto-char (point-min))
1346 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1347 (let ((link (org-element-context)))
1348 (when (eq (org-element-type link
) 'link
)
1349 (let* ((id (org-element-property :path link
))
1350 (file (org-id-find-id-file id
)))
1352 (push (cons id
(file-relative-name file
)) alist
)))))))
1355 (defun org-export--parse-option-keyword (options &optional backend
)
1356 "Parse an OPTIONS line and return values as a plist.
1357 Optional argument BACKEND is a symbol specifying which back-end
1358 specific items to read, if any."
1360 (append org-export-options-alist
1363 (format "org-%s-options-alist" backend
))))
1364 (and (boundp var
) (eval var
))))))
1365 ;; Build an alist between #+OPTION: item and property-name.
1368 (when (nth 2 e
) (cons (regexp-quote (nth 2 e
))
1373 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1375 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1377 (setq plist
(plist-put plist
1379 (car (read-from-string
1380 (match-string 2 options
)))))))
1384 (defun org-export--get-subtree-options (&optional backend
)
1385 "Get export options in subtree at point.
1386 Optional argument BACKEND is a symbol specifying back-end used
1387 for export. Return options as a plist."
1388 ;; For each buffer keyword, create an headline property setting the
1389 ;; same property in communication channel. The name for the property
1390 ;; is the keyword with "EXPORT_" appended to it.
1391 (org-with-wide-buffer
1393 ;; Make sure point is at an heading.
1394 (unless (org-at-heading-p) (org-back-to-heading t
))
1395 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1396 ;; title as its fallback value.
1397 (when (setq prop
(progn (looking-at org-todo-line-regexp
)
1398 (or (save-match-data
1399 (org-entry-get (point) "EXPORT_TITLE"))
1400 (org-match-string-no-properties 3))))
1404 (org-element-parse-secondary-string
1405 prop
(org-element-restriction 'keyword
)))))
1406 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1407 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1409 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1410 ;; Handle other keywords. TITLE keyword is excluded as it has
1411 ;; been handled already.
1412 (let ((seen '("TITLE")))
1415 (let ((property (nth 1 option
)))
1416 (when (and property
(not (member property seen
)))
1417 (let* ((subtree-prop (concat "EXPORT_" property
))
1418 ;; Export properties are not case-sensitive.
1419 (value (let ((case-fold-search t
))
1420 (org-entry-get (point) subtree-prop
))))
1421 (push property seen
)
1427 ;; Parse VALUE if required.
1428 (if (member property org-element-document-properties
)
1429 (org-element-parse-secondary-string
1430 value
(org-element-restriction 'keyword
))
1432 ;; Also look for both general keywords and back-end specific
1433 ;; options if BACKEND is provided.
1434 (append (and backend
1436 (format "org-%s-options-alist" backend
))))
1437 (and (boundp var
) (symbol-value var
))))
1438 org-export-options-alist
)))
1442 (defun org-export--get-inbuffer-options (&optional backend files
)
1443 "Return current buffer export options, as a plist.
1445 Optional argument BACKEND, when non-nil, is a symbol specifying
1446 which back-end specific options should also be read in the
1449 Optional argument FILES is a list of setup files names read so
1450 far, used to avoid circular dependencies.
1452 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1453 (org-with-wide-buffer
1454 (goto-char (point-min))
1455 (let ((case-fold-search t
) plist
)
1456 ;; 1. Special keywords, as in `org-export-special-keywords'.
1458 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords
))))
1459 (while (re-search-forward special-re nil t
)
1460 (let ((element (org-element-at-point)))
1461 (when (eq (org-element-type element
) 'keyword
)
1462 (let* ((key (org-element-property :key element
))
1463 (val (org-element-property :value element
))
1466 ((equal key
"SETUP_FILE")
1469 (org-remove-double-quotes (org-trim val
)))))
1470 ;; Avoid circular dependencies.
1471 (unless (member file files
)
1473 (insert (org-file-contents file
'noerror
))
1475 (org-export--get-inbuffer-options
1476 backend
(cons file files
))))))
1477 ((equal key
"OPTIONS")
1478 (org-export--parse-option-keyword val backend
))
1479 ((equal key
"FILETAGS")
1482 (append (org-split-string val
":")
1483 (plist-get plist
:filetags
))))))))
1484 (setq plist
(org-combine-plists plist prop
)))))))
1485 ;; 2. Standard options, as in `org-export-options-alist'.
1486 (let* ((all (append org-export-options-alist
1487 ;; Also look for back-end specific options
1488 ;; if BACKEND is defined.
1492 (format "org-%s-options-alist" backend
))))
1493 (and (boundp var
) (eval var
))))))
1494 ;; Build ALIST between keyword name and property name.
1497 (lambda (e) (when (nth 1 e
) (cons (nth 1 e
) (car e
))))
1499 ;; Build regexp matching all keywords associated to export
1500 ;; options. Note: the search is case insensitive.
1501 (opt-re (format "^[ \t]*#\\+%s:"
1503 (delq nil
(mapcar (lambda (e) (nth 1 e
)) all
))))))
1504 (goto-char (point-min))
1505 (while (re-search-forward opt-re nil t
)
1506 (let ((element (org-element-at-point)))
1507 (when (eq (org-element-type element
) 'keyword
)
1508 (let* ((key (org-element-property :key element
))
1509 (val (org-element-property :value element
))
1510 (prop (cdr (assoc key alist
)))
1511 (behaviour (nth 4 (assq prop all
))))
1515 ;; Handle value depending on specified BEHAVIOUR.
1518 (if (not (plist-get plist prop
)) (org-trim val
)
1519 (concat (plist-get plist prop
) " " (org-trim val
))))
1522 (concat (plist-get plist prop
) "\n" (org-trim val
))))
1524 `(,@(plist-get plist prop
) ,@(org-split-string val
)))
1526 (otherwise (if (not (plist-member plist prop
)) val
1527 (plist-get plist prop
))))))))))
1528 ;; Parse keywords specified in
1529 ;; `org-element-document-properties'.
1532 (let* ((prop (cdr (assoc key alist
)))
1533 (value (and prop
(plist-get plist prop
))))
1534 (when (stringp value
)
1538 (org-element-parse-secondary-string
1539 value
(org-element-restriction 'keyword
)))))))
1540 org-element-document-properties
))
1541 ;; 3. Return final value.
1544 (defun org-export--get-buffer-attributes ()
1545 "Return properties related to buffer attributes, as a plist."
1546 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1548 ;; Store full path of input file name, or nil. For internal use.
1549 :input-file visited-file
1550 :title
(or (and visited-file
1551 (file-name-sans-extension
1552 (file-name-nondirectory visited-file
)))
1553 (buffer-name (buffer-base-buffer))))))
1555 (defun org-export--get-global-options (&optional backend
)
1556 "Return global export options as a plist.
1558 Optional argument BACKEND, if non-nil, is a symbol specifying
1559 which back-end specific export options should also be read in the
1561 (let ((all (append org-export-options-alist
1564 (format "org-%s-options-alist" backend
))))
1565 (and (boundp var
) (symbol-value var
))))))
1574 ;; Eval default value provided. If keyword is a member
1575 ;; of `org-element-document-properties', parse it as
1576 ;; a secondary string before storing it.
1577 (let ((value (eval (nth 3 cell
))))
1578 (if (not (stringp value
)) value
1579 (let ((keyword (nth 1 cell
)))
1580 (if (not (member keyword org-element-document-properties
))
1582 (org-element-parse-secondary-string
1583 value
(org-element-restriction 'keyword
)))))))))
1588 (defvar org-export--allow-BIND-local nil
)
1589 (defun org-export--confirm-letbind ()
1590 "Can we use #+BIND values during export?
1591 By default this will ask for confirmation by the user, to divert
1592 possible security risks."
1594 ((not org-export-allow-BIND
) nil
)
1595 ((eq org-export-allow-BIND t
) t
)
1596 ((local-variable-p 'org-export--allow-BIND-local
)
1597 org-export--allow-BIND-local
)
1598 (t (org-set-local 'org-export--allow-BIND-local
1599 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1601 (defun org-export--install-letbind-maybe ()
1602 "Install the values from #+BIND lines as local variables.
1603 Variables must be installed before in-buffer options are
1605 (let ((case-fold-search t
) letbind pair
)
1606 (org-with-wide-buffer
1607 (goto-char (point-min))
1608 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t
)
1609 (let* ((element (org-element-at-point))
1610 (value (org-element-property :value element
)))
1611 (when (and (eq (org-element-type element
) 'keyword
)
1612 (not (equal value
""))
1613 (org-export--confirm-letbind))
1614 (push (read (format "(%s)" value
)) letbind
)))))
1615 (dolist (pair (nreverse letbind
))
1616 (org-set-local (car pair
) (nth 1 pair
)))))
1619 ;;;; Tree Properties
1621 ;; Tree properties are information extracted from parse tree. They
1622 ;; are initialized at the beginning of the transcoding process by
1623 ;; `org-export-collect-tree-properties'.
1625 ;; Dedicated functions focus on computing the value of specific tree
1626 ;; properties during initialization. Thus,
1627 ;; `org-export--populate-ignore-list' lists elements and objects that
1628 ;; should be skipped during export, `org-export--get-min-level' gets
1629 ;; the minimal exportable level, used as a basis to compute relative
1630 ;; level for headlines. Eventually
1631 ;; `org-export--collect-headline-numbering' builds an alist between
1632 ;; headlines and their numbering.
1634 (defun org-export-collect-tree-properties (data info
)
1635 "Extract tree properties from parse tree.
1637 DATA is the parse tree from which information is retrieved. INFO
1638 is a list holding export options.
1640 Following tree properties are set or updated:
1642 `:exported-data' Hash table used to memoize results from
1645 `:footnote-definition-alist' List of footnotes definitions in
1646 original buffer and current parse tree.
1648 `:headline-offset' Offset between true level of headlines and
1649 local level. An offset of -1 means an headline
1650 of level 2 should be considered as a level
1651 1 headline in the context.
1653 `:headline-numbering' Alist of all headlines as key an the
1654 associated numbering as value.
1656 `:ignore-list' List of elements that should be ignored during
1659 `:target-list' List of all targets in the parse tree.
1661 Return updated plist."
1662 ;; Install the parse tree in the communication channel, in order to
1663 ;; use `org-export-get-genealogy' and al.
1664 (setq info
(plist-put info
:parse-tree data
))
1665 ;; Get the list of elements and objects to ignore, and put it into
1666 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1667 ;; been done during parse tree filtering.
1671 (append (org-export--populate-ignore-list data info
)
1672 (plist-get info
:ignore-list
))))
1673 ;; Compute `:headline-offset' in order to be able to use
1674 ;; `org-export-get-relative-level'.
1678 (- 1 (org-export--get-min-level data info
))))
1679 ;; Update footnotes definitions list with definitions in parse tree.
1680 ;; This is required since buffer expansion might have modified
1681 ;; boundaries of footnote definitions contained in the parse tree.
1682 ;; This way, definitions in `footnote-definition-alist' are bound to
1683 ;; match those in the parse tree.
1684 (let ((defs (plist-get info
:footnote-definition-alist
)))
1686 data
'footnote-definition
1688 (push (cons (org-element-property :label fn
)
1689 `(org-data nil
,@(org-element-contents fn
)))
1691 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1692 ;; Properties order doesn't matter: get the rest of the tree
1697 data
'(keyword target
)
1699 (when (or (eq (org-element-type blob
) 'target
)
1700 (string= (org-element-property :key blob
) "TARGET"))
1702 :headline-numbering
,(org-export--collect-headline-numbering data info
)
1703 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1706 (defun org-export--get-min-level (data options
)
1707 "Return minimum exportable headline's level in DATA.
1708 DATA is parsed tree as returned by `org-element-parse-buffer'.
1709 OPTIONS is a plist holding export options."
1711 (let ((min-level 10000))
1714 (when (and (eq (org-element-type blob
) 'headline
)
1715 (not (memq blob
(plist-get options
:ignore-list
))))
1717 (min (org-element-property :level blob
) min-level
)))
1718 (when (= min-level
1) (throw 'exit
1)))
1719 (org-element-contents data
))
1720 ;; If no headline was found, for the sake of consistency, set
1721 ;; minimum level to 1 nonetheless.
1722 (if (= min-level
10000) 1 min-level
))))
1724 (defun org-export--collect-headline-numbering (data options
)
1725 "Return numbering of all exportable headlines in a parse tree.
1727 DATA is the parse tree. OPTIONS is the plist holding export
1730 Return an alist whose key is an headline and value is its
1731 associated numbering \(in the shape of a list of numbers\)."
1732 (let ((numbering (make-vector org-export-max-depth
0)))
1737 (let ((relative-level
1738 (1- (org-export-get-relative-level headline options
))))
1741 (loop for n across numbering
1742 for idx from
0 to org-export-max-depth
1743 when
(< idx relative-level
) collect n
1744 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1745 when
(> idx relative-level
) do
(aset numbering idx
0)))))
1748 (defun org-export--populate-ignore-list (data options
)
1749 "Return list of elements and objects to ignore during export.
1750 DATA is the parse tree to traverse. OPTIONS is the plist holding
1754 ;; First find trees containing a select tag, if any.
1755 (selected (org-export--selected-trees data options
))
1758 ;; Collect ignored elements or objects into IGNORE-LIST.
1759 (let ((type (org-element-type data
)))
1760 (if (org-export--skip-p data options selected
) (push data ignore
)
1761 (if (and (eq type
'headline
)
1762 (eq (plist-get options
:with-archived-trees
) 'headline
)
1763 (org-element-property :archivedp data
))
1764 ;; If headline is archived but tree below has
1765 ;; to be skipped, add it to ignore list.
1766 (mapc (lambda (e) (push e ignore
))
1767 (org-element-contents data
))
1768 ;; Move into secondary string, if any.
1770 (cdr (assq type org-element-secondary-value-alist
))))
1772 (mapc walk-data
(org-element-property sec-prop data
))))
1773 ;; Move into recursive objects/elements.
1774 (mapc walk-data
(org-element-contents data
))))))))
1776 (funcall walk-data data
)
1780 (defun org-export--selected-trees (data info
)
1781 "Return list of headlines containing a select tag in their tree.
1782 DATA is parsed data as returned by `org-element-parse-buffer'.
1783 INFO is a plist holding export options."
1784 (let* (selected-trees
1785 walk-data
; for byte-compiler.
1788 (lambda (data genealogy
)
1789 (case (org-element-type data
)
1790 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1791 (org-element-contents data
)))
1793 (let ((tags (org-element-property :tags data
)))
1794 (if (loop for tag in
(plist-get info
:select-tags
)
1795 thereis
(member tag tags
))
1796 ;; When a select tag is found, mark full
1797 ;; genealogy and every headline within the tree
1799 (setq selected-trees
1802 (org-element-map data
'headline
'identity
)
1804 ;; Else, continue searching in tree, recursively.
1806 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1807 (org-element-contents data
))))))))))
1808 (funcall walk-data data nil
) selected-trees
))
1810 (defun org-export--skip-p (blob options selected
)
1811 "Non-nil when element or object BLOB should be skipped during export.
1812 OPTIONS is the plist holding export options. SELECTED, when
1813 non-nil, is a list of headlines belonging to a tree with a select
1815 (case (org-element-type blob
)
1816 (clock (not (plist-get options
:with-clocks
)))
1818 (or (not (plist-get options
:with-drawers
))
1819 (and (consp (plist-get options
:with-drawers
))
1820 (not (member (org-element-property :drawer-name blob
)
1821 (plist-get options
:with-drawers
))))))
1823 (let ((with-tasks (plist-get options
:with-tasks
))
1824 (todo (org-element-property :todo-keyword blob
))
1825 (todo-type (org-element-property :todo-type blob
))
1826 (archived (plist-get options
:with-archived-trees
))
1827 (tags (org-element-property :tags blob
)))
1829 ;; Ignore subtrees with an exclude tag.
1830 (loop for k in
(plist-get options
:exclude-tags
)
1831 thereis
(member k tags
))
1832 ;; When a select tag is present in the buffer, ignore any tree
1834 (and selected
(not (memq blob selected
)))
1835 ;; Ignore commented sub-trees.
1836 (org-element-property :commentedp blob
)
1837 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1838 (and (not archived
) (org-element-property :archivedp blob
))
1839 ;; Ignore tasks, if specified by `:with-tasks' property.
1841 (or (not with-tasks
)
1842 (and (memq with-tasks
'(todo done
))
1843 (not (eq todo-type with-tasks
)))
1844 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1845 (inlinetask (not (plist-get options
:with-inlinetasks
)))
1846 (planning (not (plist-get options
:with-plannings
)))
1847 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1849 (and (org-export-table-has-special-column-p
1850 (org-export-get-parent-table blob
))
1851 (not (org-export-get-previous-element blob options
))))
1852 (table-row (org-export-table-row-is-special-p blob options
))
1854 (case (plist-get options
:with-timestamps
)
1855 ;; No timestamp allowed.
1857 ;; Only active timestamps allowed and the current one isn't
1860 (not (memq (org-element-property :type blob
)
1861 '(active active-range
))))
1862 ;; Only inactive timestamps allowed and the current one isn't
1865 (not (memq (org-element-property :type blob
)
1866 '(inactive inactive-range
))))))))
1872 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1873 ;; `org-element-parse-buffer') and transcodes it into a specified
1874 ;; back-end output. It takes care of filtering out elements or
1875 ;; objects according to export options and organizing the output blank
1876 ;; lines and white space are preserved. The function memoizes its
1877 ;; results, so it is cheap to call it within translators.
1879 ;; Internally, three functions handle the filtering of objects and
1880 ;; elements during the export. In particular,
1881 ;; `org-export-ignore-element' marks an element or object so future
1882 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1883 ;; elements or objects should be seen as real Org syntax and
1884 ;; `org-export-expand' transforms the others back into their original
1887 ;; `org-export-transcoder' is an accessor returning appropriate
1888 ;; translator function for a given element or object.
1890 (defun org-export-transcoder (blob info
)
1891 "Return appropriate transcoder for BLOB.
1892 INFO is a plist containing export directives."
1893 (let ((type (org-element-type blob
)))
1894 ;; Return contents only for complete parse trees.
1895 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1896 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1897 (and (functionp transcoder
) transcoder
)))))
1899 (defun org-export-data (data info
)
1900 "Convert DATA into current back-end format.
1902 DATA is a parse tree, an element or an object or a secondary
1903 string. INFO is a plist holding export options.
1905 Return transcoded string."
1906 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
1907 (if (not (eq memo
'no-memo
)) memo
1908 (let* ((type (org-element-type data
))
1911 ;; Ignored element/object.
1912 ((memq data
(plist-get info
:ignore-list
)) nil
)
1913 ;; Plain text. All residual text properties from parse
1914 ;; tree (i.e. `:parent' property) are removed.
1915 ((eq type
'plain-text
)
1917 (org-export-filter-apply-functions
1918 (plist-get info
:filter-plain-text
)
1919 (let ((transcoder (org-export-transcoder data info
)))
1920 (if transcoder
(funcall transcoder data info
) data
))
1922 ;; Uninterpreted element/object: change it back to Org
1923 ;; syntax and export again resulting raw string.
1924 ((not (org-export--interpret-p data info
))
1928 (mapconcat (lambda (blob) (org-export-data blob info
))
1929 (org-element-contents data
)
1932 ;; Secondary string.
1934 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1935 ;; Element/Object without contents or, as a special case,
1936 ;; headline with archive tag and archived trees restricted
1938 ((or (not (org-element-contents data
))
1939 (and (eq type
'headline
)
1940 (eq (plist-get info
:with-archived-trees
) 'headline
)
1941 (org-element-property :archivedp data
)))
1942 (let ((transcoder (org-export-transcoder data info
)))
1943 (and (functionp transcoder
)
1944 (funcall transcoder data nil info
))))
1945 ;; Element/Object with contents.
1947 (let ((transcoder (org-export-transcoder data info
)))
1949 (let* ((greaterp (memq type org-element-greater-elements
))
1952 (memq type org-element-recursive-objects
)))
1955 (lambda (element) (org-export-data element info
))
1956 (org-element-contents
1957 (if (or greaterp objectp
) data
1958 ;; Elements directly containing objects
1959 ;; must have their indentation normalized
1961 (org-element-normalize-contents
1963 ;; When normalizing contents of the first
1964 ;; paragraph in an item or a footnote
1965 ;; definition, ignore first line's
1966 ;; indentation: there is none and it
1967 ;; might be misleading.
1968 (when (eq type
'paragraph
)
1969 (let ((parent (org-export-get-parent data
)))
1971 (eq (car (org-element-contents parent
))
1973 (memq (org-element-type parent
)
1974 '(footnote-definition item
))))))))
1976 (funcall transcoder data
1977 (if (not greaterp
) contents
1978 (org-element-normalize-string contents
))
1980 ;; Final result will be memoized before being returned.
1985 ((memq type
'(org-data plain-text nil
)) results
)
1986 ;; Append the same white space between elements or objects as in
1987 ;; the original buffer, and call appropriate filters.
1990 (org-export-filter-apply-functions
1991 (plist-get info
(intern (format ":filter-%s" type
)))
1992 (let ((post-blank (or (org-element-property :post-blank data
)
1994 (if (memq type org-element-all-elements
)
1995 (concat (org-element-normalize-string results
)
1996 (make-string post-blank ?
\n))
1997 (concat results
(make-string post-blank ?
))))
2000 (plist-get info
:exported-data
))))))
2002 (defun org-export--interpret-p (blob info
)
2003 "Non-nil if element or object BLOB should be interpreted as Org syntax.
2004 Check is done according to export options INFO, stored as
2006 (case (org-element-type blob
)
2008 (entity (plist-get info
:with-entities
))
2010 (emphasis (plist-get info
:with-emphasize
))
2011 ;; ... fixed-width areas.
2012 (fixed-width (plist-get info
:with-fixed-width
))
2014 ((footnote-definition footnote-reference
)
2015 (plist-get info
:with-footnotes
))
2016 ;; ... sub/superscripts...
2017 ((subscript superscript
)
2018 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
2019 (if (eq sub
/super-p
'{})
2020 (org-element-property :use-brackets-p blob
)
2023 (table (plist-get info
:with-tables
))
2026 (defun org-export-expand (blob contents
)
2027 "Expand a parsed element or object to its original state.
2028 BLOB is either an element or an object. CONTENTS is its
2029 contents, as a string or nil."
2031 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
2034 (defun org-export-ignore-element (element info
)
2035 "Add ELEMENT to `:ignore-list' in INFO.
2037 Any element in `:ignore-list' will be skipped when using
2038 `org-element-map'. INFO is modified by side effects."
2039 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2043 ;;; The Filter System
2045 ;; Filters allow end-users to tweak easily the transcoded output.
2046 ;; They are the functional counterpart of hooks, as every filter in
2047 ;; a set is applied to the return value of the previous one.
2049 ;; Every set is back-end agnostic. Although, a filter is always
2050 ;; called, in addition to the string it applies to, with the back-end
2051 ;; used as argument, so it's easy for the end-user to add back-end
2052 ;; specific filters in the set. The communication channel, as
2053 ;; a plist, is required as the third argument.
2055 ;; From the developer side, filters sets can be installed in the
2056 ;; process with the help of `org-export-define-backend', which
2057 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2058 ;; association has a key among the following symbols and a function or
2059 ;; a list of functions as value.
2061 ;; - `:filter-parse-tree' applies directly on the complete parsed
2062 ;; tree. It's the only filters set that doesn't apply to a string.
2063 ;; Users can set it through `org-export-filter-parse-tree-functions'
2066 ;; - `:filter-final-output' applies to the final transcoded string.
2067 ;; Users can set it with `org-export-filter-final-output-functions'
2070 ;; - `:filter-plain-text' applies to any string not recognized as Org
2071 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2074 ;; - `:filter-TYPE' applies on the string returned after an element or
2075 ;; object of type TYPE has been transcoded. An user can modify
2076 ;; `org-export-filter-TYPE-functions'
2078 ;; All filters sets are applied with
2079 ;; `org-export-filter-apply-functions' function. Filters in a set are
2080 ;; applied in a LIFO fashion. It allows developers to be sure that
2081 ;; their filters will be applied first.
2083 ;; Filters properties are installed in communication channel with
2084 ;; `org-export-install-filters' function.
2086 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2087 ;; `org-export-before-parsing-hook') are run at the beginning of the
2088 ;; export process and just before parsing to allow for heavy structure
2094 (defvar org-export-before-processing-hook nil
2095 "Hook run at the beginning of the export process.
2097 This is run before include keywords and macros are expanded and
2098 Babel code blocks executed, on a copy of the original buffer
2099 being exported. Visibility and narrowing are preserved. Point
2100 is at the beginning of the buffer.
2102 Every function in this hook will be called with one argument: the
2103 back-end currently used, as a symbol.")
2105 (defvar org-export-before-parsing-hook nil
2106 "Hook run before parsing an export buffer.
2108 This is run after include keywords and macros have been expanded
2109 and Babel code blocks executed, on a copy of the original buffer
2110 being exported. Visibility and narrowing are preserved. Point
2111 is at the beginning of the buffer.
2113 Every function in this hook will be called with one argument: the
2114 back-end currently used, as a symbol.")
2117 ;;;; Special Filters
2119 (defvar org-export-filter-parse-tree-functions nil
2120 "List of functions applied to the parsed tree.
2121 Each filter is called with three arguments: the parse tree, as
2122 returned by `org-element-parse-buffer', the back-end, as
2123 a symbol, and the communication channel, as a plist. It must
2124 return the modified parse tree to transcode.")
2126 (defvar org-export-filter-final-output-functions nil
2127 "List of functions applied to the transcoded string.
2128 Each filter is called with three arguments: the full transcoded
2129 string, the back-end, as a symbol, and the communication channel,
2130 as a plist. It must return a string that will be used as the
2131 final export output.")
2133 (defvar org-export-filter-plain-text-functions nil
2134 "List of functions applied to plain text.
2135 Each filter is called with three arguments: a string which
2136 contains no Org syntax, the back-end, as a symbol, and the
2137 communication channel, as a plist. It must return a string or
2141 ;;;; Elements Filters
2143 (defvar org-export-filter-babel-call-functions nil
2144 "List of functions applied to a transcoded babel-call.
2145 Each filter is called with three arguments: the transcoded data,
2146 as a string, the back-end, as a symbol, and the communication
2147 channel, as a plist. It must return a string or nil.")
2149 (defvar org-export-filter-center-block-functions nil
2150 "List of functions applied to a transcoded center block.
2151 Each filter is called with three arguments: the transcoded data,
2152 as a string, the back-end, as a symbol, and the communication
2153 channel, as a plist. It must return a string or nil.")
2155 (defvar org-export-filter-clock-functions nil
2156 "List of functions applied to a transcoded clock.
2157 Each filter is called with three arguments: the transcoded data,
2158 as a string, the back-end, as a symbol, and the communication
2159 channel, as a plist. It must return a string or nil.")
2161 (defvar org-export-filter-comment-functions nil
2162 "List of functions applied to a transcoded comment.
2163 Each filter is called with three arguments: the transcoded data,
2164 as a string, the back-end, as a symbol, and the communication
2165 channel, as a plist. It must return a string or nil.")
2167 (defvar org-export-filter-comment-block-functions nil
2168 "List of functions applied to a transcoded comment-block.
2169 Each filter is called with three arguments: the transcoded data,
2170 as a string, the back-end, as a symbol, and the communication
2171 channel, as a plist. It must return a string or nil.")
2173 (defvar org-export-filter-diary-sexp-functions nil
2174 "List of functions applied to a transcoded diary-sexp.
2175 Each filter is called with three arguments: the transcoded data,
2176 as a string, the back-end, as a symbol, and the communication
2177 channel, as a plist. It must return a string or nil.")
2179 (defvar org-export-filter-drawer-functions nil
2180 "List of functions applied to a transcoded drawer.
2181 Each filter is called with three arguments: the transcoded data,
2182 as a string, the back-end, as a symbol, and the communication
2183 channel, as a plist. It must return a string or nil.")
2185 (defvar org-export-filter-dynamic-block-functions nil
2186 "List of functions applied to a transcoded dynamic-block.
2187 Each filter is called with three arguments: the transcoded data,
2188 as a string, the back-end, as a symbol, and the communication
2189 channel, as a plist. It must return a string or nil.")
2191 (defvar org-export-filter-example-block-functions nil
2192 "List of functions applied to a transcoded example-block.
2193 Each filter is called with three arguments: the transcoded data,
2194 as a string, the back-end, as a symbol, and the communication
2195 channel, as a plist. It must return a string or nil.")
2197 (defvar org-export-filter-export-block-functions nil
2198 "List of functions applied to a transcoded export-block.
2199 Each filter is called with three arguments: the transcoded data,
2200 as a string, the back-end, as a symbol, and the communication
2201 channel, as a plist. It must return a string or nil.")
2203 (defvar org-export-filter-fixed-width-functions nil
2204 "List of functions applied to a transcoded fixed-width.
2205 Each filter is called with three arguments: the transcoded data,
2206 as a string, the back-end, as a symbol, and the communication
2207 channel, as a plist. It must return a string or nil.")
2209 (defvar org-export-filter-footnote-definition-functions nil
2210 "List of functions applied to a transcoded footnote-definition.
2211 Each filter is called with three arguments: the transcoded data,
2212 as a string, the back-end, as a symbol, and the communication
2213 channel, as a plist. It must return a string or nil.")
2215 (defvar org-export-filter-headline-functions nil
2216 "List of functions applied to a transcoded headline.
2217 Each filter is called with three arguments: the transcoded data,
2218 as a string, the back-end, as a symbol, and the communication
2219 channel, as a plist. It must return a string or nil.")
2221 (defvar org-export-filter-horizontal-rule-functions nil
2222 "List of functions applied to a transcoded horizontal-rule.
2223 Each filter is called with three arguments: the transcoded data,
2224 as a string, the back-end, as a symbol, and the communication
2225 channel, as a plist. It must return a string or nil.")
2227 (defvar org-export-filter-inlinetask-functions nil
2228 "List of functions applied to a transcoded inlinetask.
2229 Each filter is called with three arguments: the transcoded data,
2230 as a string, the back-end, as a symbol, and the communication
2231 channel, as a plist. It must return a string or nil.")
2233 (defvar org-export-filter-item-functions nil
2234 "List of functions applied to a transcoded item.
2235 Each filter is called with three arguments: the transcoded data,
2236 as a string, the back-end, as a symbol, and the communication
2237 channel, as a plist. It must return a string or nil.")
2239 (defvar org-export-filter-keyword-functions nil
2240 "List of functions applied to a transcoded keyword.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2245 (defvar org-export-filter-latex-environment-functions nil
2246 "List of functions applied to a transcoded latex-environment.
2247 Each filter is called with three arguments: the transcoded data,
2248 as a string, the back-end, as a symbol, and the communication
2249 channel, as a plist. It must return a string or nil.")
2251 (defvar org-export-filter-node-property-functions nil
2252 "List of functions applied to a transcoded node-property.
2253 Each filter is called with three arguments: the transcoded data,
2254 as a string, the back-end, as a symbol, and the communication
2255 channel, as a plist. It must return a string or nil.")
2257 (defvar org-export-filter-paragraph-functions nil
2258 "List of functions applied to a transcoded paragraph.
2259 Each filter is called with three arguments: the transcoded data,
2260 as a string, the back-end, as a symbol, and the communication
2261 channel, as a plist. It must return a string or nil.")
2263 (defvar org-export-filter-plain-list-functions nil
2264 "List of functions applied to a transcoded plain-list.
2265 Each filter is called with three arguments: the transcoded data,
2266 as a string, the back-end, as a symbol, and the communication
2267 channel, as a plist. It must return a string or nil.")
2269 (defvar org-export-filter-planning-functions nil
2270 "List of functions applied to a transcoded planning.
2271 Each filter is called with three arguments: the transcoded data,
2272 as a string, the back-end, as a symbol, and the communication
2273 channel, as a plist. It must return a string or nil.")
2275 (defvar org-export-filter-property-drawer-functions nil
2276 "List of functions applied to a transcoded property-drawer.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-quote-block-functions nil
2282 "List of functions applied to a transcoded quote block.
2283 Each filter is called with three arguments: the transcoded quote
2284 data, as a string, the back-end, as a symbol, and the
2285 communication channel, as a plist. It must return a string or
2288 (defvar org-export-filter-quote-section-functions nil
2289 "List of functions applied to a transcoded quote-section.
2290 Each filter is called with three arguments: the transcoded data,
2291 as a string, the back-end, as a symbol, and the communication
2292 channel, as a plist. It must return a string or nil.")
2294 (defvar org-export-filter-section-functions nil
2295 "List of functions applied to a transcoded section.
2296 Each filter is called with three arguments: the transcoded data,
2297 as a string, the back-end, as a symbol, and the communication
2298 channel, as a plist. It must return a string or nil.")
2300 (defvar org-export-filter-special-block-functions nil
2301 "List of functions applied to a transcoded special block.
2302 Each filter is called with three arguments: the transcoded data,
2303 as a string, the back-end, as a symbol, and the communication
2304 channel, as a plist. It must return a string or nil.")
2306 (defvar org-export-filter-src-block-functions nil
2307 "List of functions applied to a transcoded src-block.
2308 Each filter is called with three arguments: the transcoded data,
2309 as a string, the back-end, as a symbol, and the communication
2310 channel, as a plist. It must return a string or nil.")
2312 (defvar org-export-filter-table-functions nil
2313 "List of functions applied to a transcoded table.
2314 Each filter is called with three arguments: the transcoded data,
2315 as a string, the back-end, as a symbol, and the communication
2316 channel, as a plist. It must return a string or nil.")
2318 (defvar org-export-filter-table-cell-functions nil
2319 "List of functions applied to a transcoded table-cell.
2320 Each filter is called with three arguments: the transcoded data,
2321 as a string, the back-end, as a symbol, and the communication
2322 channel, as a plist. It must return a string or nil.")
2324 (defvar org-export-filter-table-row-functions nil
2325 "List of functions applied to a transcoded table-row.
2326 Each filter is called with three arguments: the transcoded data,
2327 as a string, the back-end, as a symbol, and the communication
2328 channel, as a plist. It must return a string or nil.")
2330 (defvar org-export-filter-verse-block-functions nil
2331 "List of functions applied to a transcoded verse block.
2332 Each filter is called with three arguments: the transcoded data,
2333 as a string, the back-end, as a symbol, and the communication
2334 channel, as a plist. It must return a string or nil.")
2337 ;;;; Objects Filters
2339 (defvar org-export-filter-bold-functions nil
2340 "List of functions applied to transcoded bold text.
2341 Each filter is called with three arguments: the transcoded data,
2342 as a string, the back-end, as a symbol, and the communication
2343 channel, as a plist. It must return a string or nil.")
2345 (defvar org-export-filter-code-functions nil
2346 "List of functions applied to transcoded code text.
2347 Each filter is called with three arguments: the transcoded data,
2348 as a string, the back-end, as a symbol, and the communication
2349 channel, as a plist. It must return a string or nil.")
2351 (defvar org-export-filter-entity-functions nil
2352 "List of functions applied to a transcoded entity.
2353 Each filter is called with three arguments: the transcoded data,
2354 as a string, the back-end, as a symbol, and the communication
2355 channel, as a plist. It must return a string or nil.")
2357 (defvar org-export-filter-export-snippet-functions nil
2358 "List of functions applied to a transcoded export-snippet.
2359 Each filter is called with three arguments: the transcoded data,
2360 as a string, the back-end, as a symbol, and the communication
2361 channel, as a plist. It must return a string or nil.")
2363 (defvar org-export-filter-footnote-reference-functions nil
2364 "List of functions applied to a transcoded footnote-reference.
2365 Each filter is called with three arguments: the transcoded data,
2366 as a string, the back-end, as a symbol, and the communication
2367 channel, as a plist. It must return a string or nil.")
2369 (defvar org-export-filter-inline-babel-call-functions nil
2370 "List of functions applied to a transcoded inline-babel-call.
2371 Each filter is called with three arguments: the transcoded data,
2372 as a string, the back-end, as a symbol, and the communication
2373 channel, as a plist. It must return a string or nil.")
2375 (defvar org-export-filter-inline-src-block-functions nil
2376 "List of functions applied to a transcoded inline-src-block.
2377 Each filter is called with three arguments: the transcoded data,
2378 as a string, the back-end, as a symbol, and the communication
2379 channel, as a plist. It must return a string or nil.")
2381 (defvar org-export-filter-italic-functions nil
2382 "List of functions applied to transcoded italic text.
2383 Each filter is called with three arguments: the transcoded data,
2384 as a string, the back-end, as a symbol, and the communication
2385 channel, as a plist. It must return a string or nil.")
2387 (defvar org-export-filter-latex-fragment-functions nil
2388 "List of functions applied to a transcoded latex-fragment.
2389 Each filter is called with three arguments: the transcoded data,
2390 as a string, the back-end, as a symbol, and the communication
2391 channel, as a plist. It must return a string or nil.")
2393 (defvar org-export-filter-line-break-functions nil
2394 "List of functions applied to a transcoded line-break.
2395 Each filter is called with three arguments: the transcoded data,
2396 as a string, the back-end, as a symbol, and the communication
2397 channel, as a plist. It must return a string or nil.")
2399 (defvar org-export-filter-link-functions nil
2400 "List of functions applied to a transcoded link.
2401 Each filter is called with three arguments: the transcoded data,
2402 as a string, the back-end, as a symbol, and the communication
2403 channel, as a plist. It must return a string or nil.")
2405 (defvar org-export-filter-macro-functions nil
2406 "List of functions applied to a transcoded macro.
2407 Each filter is called with three arguments: the transcoded data,
2408 as a string, the back-end, as a symbol, and the communication
2409 channel, as a plist. It must return a string or nil.")
2411 (defvar org-export-filter-radio-target-functions nil
2412 "List of functions applied to a transcoded radio-target.
2413 Each filter is called with three arguments: the transcoded data,
2414 as a string, the back-end, as a symbol, and the communication
2415 channel, as a plist. It must return a string or nil.")
2417 (defvar org-export-filter-statistics-cookie-functions nil
2418 "List of functions applied to a transcoded statistics-cookie.
2419 Each filter is called with three arguments: the transcoded data,
2420 as a string, the back-end, as a symbol, and the communication
2421 channel, as a plist. It must return a string or nil.")
2423 (defvar org-export-filter-strike-through-functions nil
2424 "List of functions applied to transcoded strike-through text.
2425 Each filter is called with three arguments: the transcoded data,
2426 as a string, the back-end, as a symbol, and the communication
2427 channel, as a plist. It must return a string or nil.")
2429 (defvar org-export-filter-subscript-functions nil
2430 "List of functions applied to a transcoded subscript.
2431 Each filter is called with three arguments: the transcoded data,
2432 as a string, the back-end, as a symbol, and the communication
2433 channel, as a plist. It must return a string or nil.")
2435 (defvar org-export-filter-superscript-functions nil
2436 "List of functions applied to a transcoded superscript.
2437 Each filter is called with three arguments: the transcoded data,
2438 as a string, the back-end, as a symbol, and the communication
2439 channel, as a plist. It must return a string or nil.")
2441 (defvar org-export-filter-target-functions nil
2442 "List of functions applied to a transcoded target.
2443 Each filter is called with three arguments: the transcoded data,
2444 as a string, the back-end, as a symbol, and the communication
2445 channel, as a plist. It must return a string or nil.")
2447 (defvar org-export-filter-timestamp-functions nil
2448 "List of functions applied to a transcoded timestamp.
2449 Each filter is called with three arguments: the transcoded data,
2450 as a string, the back-end, as a symbol, and the communication
2451 channel, as a plist. It must return a string or nil.")
2453 (defvar org-export-filter-underline-functions nil
2454 "List of functions applied to transcoded underline text.
2455 Each filter is called with three arguments: the transcoded data,
2456 as a string, the back-end, as a symbol, and the communication
2457 channel, as a plist. It must return a string or nil.")
2459 (defvar org-export-filter-verbatim-functions nil
2460 "List of functions applied to transcoded verbatim text.
2461 Each filter is called with three arguments: the transcoded data,
2462 as a string, the back-end, as a symbol, and the communication
2463 channel, as a plist. It must return a string or nil.")
2468 ;; Internal function `org-export-install-filters' installs filters
2469 ;; hard-coded in back-ends (developer filters) and filters from global
2470 ;; variables (user filters) in the communication channel.
2472 ;; Internal function `org-export-filter-apply-functions' takes care
2473 ;; about applying each filter in order to a given data. It ignores
2474 ;; filters returning a nil value but stops whenever a filter returns
2477 (defun org-export-filter-apply-functions (filters value info
)
2478 "Call every function in FILTERS.
2480 Functions are called with arguments VALUE, current export
2481 back-end and INFO. A function returning a nil value will be
2482 skipped. If it returns the empty string, the process ends and
2485 Call is done in a LIFO fashion, to be sure that developer
2486 specified filters, if any, are called first."
2488 (dolist (filter filters value
)
2489 (let ((result (funcall filter value
(plist-get info
:back-end
) info
)))
2490 (cond ((not result
) value
)
2491 ((equal value
"") (throw 'exit nil
))
2492 (t (setq value result
)))))))
2494 (defun org-export-install-filters (info)
2495 "Install filters properties in communication channel.
2497 INFO is a plist containing the current communication channel.
2499 Return the updated communication channel."
2501 ;; Install user defined filters with `org-export-filters-alist'.
2503 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2504 org-export-filters-alist
)
2505 ;; Prepend back-end specific filters to that list.
2506 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2507 (plist-get info
:back-end
)))))
2508 (when (boundp back-end-filters
)
2510 ;; Single values get consed, lists are prepended.
2511 (let ((key (car p
)) (value (cdr p
)))
2516 (if (atom value
) (cons value
(plist-get plist key
))
2517 (append value
(plist-get plist key
))))))))
2518 (eval back-end-filters
))))
2519 ;; Return new communication channel.
2520 (org-combine-plists info plist
)))
2526 ;; This is the room for the main function, `org-export-as', along with
2527 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2528 ;; They differ only by the way they output the resulting code.
2530 ;; `org-export-output-file-name' is an auxiliary function meant to be
2531 ;; used with `org-export-to-file'. With a given extension, it tries
2532 ;; to provide a canonical file name to write export output to.
2534 ;; Note that `org-export-as' doesn't really parse the current buffer,
2535 ;; but a copy of it (with the same buffer-local variables and
2536 ;; visibility), where macros and include keywords are expanded and
2537 ;; Babel blocks are executed, if appropriate.
2538 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2540 ;; File inclusion is taken care of by
2541 ;; `org-export-expand-include-keyword' and
2542 ;; `org-export--prepare-file-contents'. Structure wise, including
2543 ;; a whole Org file in a buffer often makes little sense. For
2544 ;; example, if the file contains an headline and the include keyword
2545 ;; was within an item, the item should contain the headline. That's
2546 ;; why file inclusion should be done before any structure can be
2547 ;; associated to the file, that is before parsing.
2549 ;; Macro are expanded with `org-export-expand-macro'.
2551 (defun org-export-as
2552 (backend &optional subtreep visible-only body-only ext-plist noexpand
)
2553 "Transcode current Org buffer into BACKEND code.
2555 If narrowing is active in the current buffer, only transcode its
2558 If a region is active, transcode that region.
2560 When optional argument SUBTREEP is non-nil, transcode the
2561 sub-tree at point, extracting information from the headline
2564 When optional argument VISIBLE-ONLY is non-nil, don't export
2565 contents of hidden elements.
2567 When optional argument BODY-ONLY is non-nil, only return body
2568 code, without preamble nor postamble.
2570 Optional argument EXT-PLIST, when provided, is a property list
2571 with external parameters overriding Org default settings, but
2572 still inferior to file-local settings.
2574 Optional argument NOEXPAND, when non-nil, prevents included files
2575 to be expanded and Babel code to be executed.
2577 Return code as a string."
2578 ;; Barf if BACKEND isn't registered.
2579 (org-export-barf-if-invalid-backend backend
)
2582 ;; Narrow buffer to an appropriate region or subtree for
2583 ;; parsing. If parsing subtree, be sure to remove main headline
2585 (cond ((org-region-active-p)
2586 (narrow-to-region (region-beginning) (region-end)))
2588 (org-narrow-to-subtree)
2589 (goto-char (point-min))
2591 (narrow-to-region (point) (point-max))))
2592 ;; Initialize communication channel with original buffer
2593 ;; attributes, unavailable in its copy.
2594 (let ((info (org-export--get-buffer-attributes)) tree
)
2595 (org-export-with-current-buffer-copy
2596 ;; Run first hook with current back-end as argument.
2597 (run-hook-with-args 'org-export-before-processing-hook backend
)
2598 ;; Update communication channel and get parse tree. Buffer
2599 ;; isn't parsed directly. Instead, a temporary copy is
2600 ;; created, where include keywords, macros are expanded and
2601 ;; code blocks are evaluated.
2603 (org-export-expand-include-keyword)
2604 ;; Update macro templates since #+INCLUDE keywords might
2605 ;; have added some new ones.
2606 (org-macro-initialize-templates)
2607 (org-macro-replace-all org-macro-templates
)
2608 ;; TODO: Setting `org-current-export-file' is required by
2609 ;; Org Babel to properly resolve noweb references. Once
2610 ;; "org-exp.el" is removed, modify
2611 ;; `org-export-blocks-preprocess' so it accepts the value
2612 ;; as an argument instead.
2613 (let ((org-current-export-file (current-buffer)))
2614 (org-export-blocks-preprocess)))
2615 ;; Update radio targets since keyword inclusion might have
2617 (org-update-radio-target-regexp)
2618 ;; Run last hook with current back-end as argument.
2619 (goto-char (point-min))
2620 (run-hook-with-args 'org-export-before-parsing-hook backend
)
2621 ;; Update communication channel with environment. Also
2622 ;; install user's and developer's filters.
2624 (org-export-install-filters
2626 info
(org-export-get-environment backend subtreep ext-plist
))))
2627 ;; Expand export-specific set of macros: {{{author}}},
2628 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2629 ;; once regular macros have been expanded, since document
2630 ;; keywords may contain one of them.
2632 (org-macro-replace-all
2633 (list (cons "author"
2634 (org-element-interpret-data (plist-get info
:author
)))
2636 (org-element-interpret-data (plist-get info
:date
)))
2637 ;; EMAIL is not a parsed keyword: store it as-is.
2638 (cons "email" (or (plist-get info
:email
) ""))
2640 (org-element-interpret-data (plist-get info
:title
))))))
2641 ;; Eventually parse buffer. Call parse-tree filters to get
2644 (org-export-filter-apply-functions
2645 (plist-get info
:filter-parse-tree
)
2646 (org-element-parse-buffer nil visible-only
) info
)))
2647 ;; Now tree is complete, compute its properties and add them
2648 ;; to communication channel.
2651 info
(org-export-collect-tree-properties tree info
)))
2652 ;; Eventually transcode TREE. Wrap the resulting string into
2653 ;; a template, if required. Finally call final-output filter.
2654 (let* ((body (org-element-normalize-string
2655 (or (org-export-data tree info
) "")))
2656 (template (cdr (assq 'template
2657 (plist-get info
:translate-alist
))))
2658 (output (org-export-filter-apply-functions
2659 (plist-get info
:filter-final-output
)
2660 (if (or (not (functionp template
)) body-only
) body
2661 (funcall template body info
))
2663 ;; Maybe add final OUTPUT to kill ring, then return it.
2664 (when (and org-export-copy-to-kill-ring
(org-string-nw-p output
))
2665 (org-kill-new output
))
2668 (defun org-export-to-buffer
2669 (backend buffer
&optional subtreep visible-only body-only ext-plist noexpand
)
2670 "Call `org-export-as' with output to a specified buffer.
2672 BACKEND is the back-end used for transcoding, as a symbol.
2674 BUFFER is the output buffer. If it already exists, it will be
2675 erased first, otherwise, it will be created.
2677 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2678 and NOEXPAND are similar to those used in `org-export-as', which
2682 (let ((out (org-export-as
2683 backend subtreep visible-only body-only ext-plist noexpand
))
2684 (buffer (get-buffer-create buffer
)))
2685 (with-current-buffer buffer
2688 (goto-char (point-min)))
2691 (defun org-export-to-file
2692 (backend file
&optional subtreep visible-only body-only ext-plist noexpand
)
2693 "Call `org-export-as' with output to a specified file.
2695 BACKEND is the back-end used for transcoding, as a symbol. FILE
2696 is the name of the output file, as a string.
2698 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2699 and NOEXPAND are similar to those used in `org-export-as', which
2702 Return output file's name."
2703 ;; Checks for FILE permissions. `write-file' would do the same, but
2704 ;; we'd rather avoid needless transcoding of parse tree.
2705 (unless (file-writable-p file
) (error "Output file not writable"))
2706 ;; Insert contents to a temporary buffer and write it to FILE.
2707 (let ((out (org-export-as
2708 backend subtreep visible-only body-only ext-plist noexpand
)))
2711 (let ((coding-system-for-write org-export-coding-system
))
2712 (write-file file
))))
2713 ;; Return full path.
2716 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2717 "Return output file's name according to buffer specifications.
2719 EXTENSION is a string representing the output file extension,
2720 with the leading dot.
2722 With a non-nil optional argument SUBTREEP, try to determine
2723 output file's name by looking for \"EXPORT_FILE_NAME\" property
2724 of subtree at point.
2726 When optional argument PUB-DIR is set, use it as the publishing
2729 When optional argument VISIBLE-ONLY is non-nil, don't export
2730 contents of hidden elements.
2732 Return file name as a string, or nil if it couldn't be
2735 ;; File name may come from EXPORT_FILE_NAME subtree property,
2736 ;; assuming point is at beginning of said sub-tree.
2737 (file-name-sans-extension
2741 (ignore-errors (org-back-to-heading) (point)))
2742 "EXPORT_FILE_NAME" t
))
2743 ;; File name may be extracted from buffer's associated
2745 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2746 (and visited-file
(file-name-nondirectory visited-file
)))
2747 ;; Can't determine file name on our own: Ask user.
2748 (let ((read-file-name-function
2749 (and org-completion-use-ido
'ido-read-file-name
)))
2751 "Output file: " pub-dir nil nil nil
2753 (string= (file-name-extension name t
) extension
))))))))
2754 ;; Build file name. Enforce EXTENSION over whatever user may have
2755 ;; come up with. PUB-DIR, if defined, always has precedence over
2756 ;; any provided path.
2759 (concat (file-name-as-directory pub-dir
)
2760 (file-name-nondirectory base-name
)
2762 ((file-name-absolute-p base-name
) (concat base-name extension
))
2763 (t (concat (file-name-as-directory ".") base-name extension
)))))
2765 (defmacro org-export-with-current-buffer-copy
(&rest body
)
2766 "Apply BODY in a copy of the current buffer.
2768 The copy preserves local variables and visibility of the original
2771 Point is at buffer's beginning when BODY is applied."
2772 (declare (debug (body)))
2773 (org-with-gensyms (original-buffer offset buffer-string overlays region
)
2774 `(let* ((,original-buffer
(current-buffer))
2775 (,region
(list (point-min) (point-max)))
2776 (,buffer-string
(org-with-wide-buffer (buffer-string)))
2777 (,overlays
(mapcar 'copy-overlay
(apply 'overlays-in
,region
))))
2779 (let ((buffer-invisibility-spec nil
))
2780 (org-clone-local-variables
2782 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2783 (insert ,buffer-string
)
2784 (apply 'narrow-to-region
,region
)
2787 ov
(overlay-start ov
) (overlay-end ov
) (current-buffer)))
2789 (goto-char (point-min))
2792 (defun org-export-expand-include-keyword (&optional included dir
)
2793 "Expand every include keyword in buffer.
2794 Optional argument INCLUDED is a list of included file names along
2795 with their line restriction, when appropriate. It is used to
2796 avoid infinite recursion. Optional argument DIR is the current
2797 working directory. It is used to properly resolve relative
2799 (let ((case-fold-search t
))
2800 (goto-char (point-min))
2801 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t
)
2802 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2805 ;; Extract arguments from keyword's value.
2806 (let* ((value (match-string 1))
2807 (ind (org-get-indentation))
2808 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
2809 (prog1 (expand-file-name (match-string 1 value
) dir
)
2810 (setq value
(replace-match "" nil nil value
)))))
2813 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
2814 (prog1 (match-string 1 value
)
2815 (setq value
(replace-match "" nil nil value
)))))
2816 (env (cond ((string-match "\\<example\\>" value
) 'example
)
2817 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
2818 (match-string 1 value
))))
2819 ;; Minimal level of included file defaults to the child
2820 ;; level of the current headline, if any, or one. It
2821 ;; only applies is the file is meant to be included as
2825 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
2826 (prog1 (string-to-number (match-string 1 value
))
2827 (setq value
(replace-match "" nil nil value
)))
2828 (let ((cur (org-current-level)))
2829 (if cur
(1+ (org-reduced-level cur
)) 1))))))
2831 (delete-region (point) (progn (forward-line) (point)))
2833 ((not file
) (error "Invalid syntax in INCLUDE keyword"))
2834 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
2835 ;; Check if files has already been parsed. Look after
2836 ;; inclusion lines too, as different parts of the same file
2837 ;; can be included too.
2838 ((member (list file lines
) included
)
2839 (error "Recursive file inclusion: %s" file
))
2844 (let ((ind-str (make-string ind ?
))
2846 (org-escape-code-in-string
2847 (org-export--prepare-file-contents file lines
))))
2848 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2849 ind-str contents ind-str
))))
2852 (let ((ind-str (make-string ind ?
))
2854 (org-escape-code-in-string
2855 (org-export--prepare-file-contents file lines
))))
2856 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2857 ind-str env contents ind-str
))))
2863 (org-export--prepare-file-contents file lines ind minlevel
))
2864 (org-export-expand-include-keyword
2865 (cons (list file lines
) included
)
2866 (file-name-directory file
))
2867 (buffer-string))))))))))))
2869 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
2870 "Prepare the contents of FILE for inclusion and return them as a string.
2872 When optional argument LINES is a string specifying a range of
2873 lines, include only those lines.
2875 Optional argument IND, when non-nil, is an integer specifying the
2876 global indentation of returned contents. Since its purpose is to
2877 allow an included file to stay in the same environment it was
2878 created \(i.e. a list item), it doesn't apply past the first
2879 headline encountered.
2881 Optional argument MINLEVEL, when non-nil, is an integer
2882 specifying the level that any top-level headline in the included
2885 (insert-file-contents file
)
2887 (let* ((lines (split-string lines
"-"))
2888 (lbeg (string-to-number (car lines
)))
2889 (lend (string-to-number (cadr lines
)))
2890 (beg (if (zerop lbeg
) (point-min)
2891 (goto-char (point-min))
2892 (forward-line (1- lbeg
))
2894 (end (if (zerop lend
) (point-max)
2895 (goto-char (point-min))
2896 (forward-line (1- lend
))
2898 (narrow-to-region beg end
)))
2899 ;; Remove blank lines at beginning and end of contents. The logic
2900 ;; behind that removal is that blank lines around include keyword
2901 ;; override blank lines in included file.
2902 (goto-char (point-min))
2903 (org-skip-whitespace)
2905 (delete-region (point-min) (point))
2906 (goto-char (point-max))
2907 (skip-chars-backward " \r\t\n")
2909 (delete-region (point) (point-max))
2910 ;; If IND is set, preserve indentation of include keyword until
2911 ;; the first headline encountered.
2913 (unless (eq major-mode
'org-mode
) (org-mode))
2914 (goto-char (point-min))
2915 (let ((ind-str (make-string ind ?
)))
2916 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
2917 ;; Do not move footnote definitions out of column 0.
2918 (unless (and (looking-at org-footnote-definition-re
)
2919 (eq (org-element-type (org-element-at-point))
2920 'footnote-definition
))
2923 ;; When MINLEVEL is specified, compute minimal level for headlines
2924 ;; in the file (CUR-MIN), and remove stars to each headline so
2925 ;; that headlines with minimal level have a level of MINLEVEL.
2927 (unless (eq major-mode
'org-mode
) (org-mode))
2928 (org-with-limited-levels
2929 (let ((levels (org-map-entries
2930 (lambda () (org-reduced-level (org-current-level))))))
2932 (let ((offset (- minlevel
(apply 'min levels
))))
2933 (unless (zerop offset
)
2934 (when org-odd-levels-only
(setq offset
(* offset
2)))
2935 ;; Only change stars, don't bother moving whole
2938 (lambda () (if (< offset
0) (delete-char (abs offset
))
2939 (insert (make-string offset ?
*)))))))))))
2940 (org-element-normalize-string (buffer-string))))
2943 ;;; Tools For Back-Ends
2945 ;; A whole set of tools is available to help build new exporters. Any
2946 ;; function general enough to have its use across many back-ends
2947 ;; should be added here.
2949 ;;;; For Affiliated Keywords
2951 ;; `org-export-read-attribute' reads a property from a given element
2952 ;; as a plist. It can be used to normalize affiliated keywords'
2955 ;; Since captions can span over multiple lines and accept dual values,
2956 ;; their internal representation is a bit tricky. Therefore,
2957 ;; `org-export-get-caption' transparently returns a given element's
2958 ;; caption as a secondary string.
2960 (defun org-export-read-attribute (attribute element
&optional property
)
2961 "Turn ATTRIBUTE property from ELEMENT into a plist.
2963 When optional argument PROPERTY is non-nil, return the value of
2964 that property within attributes.
2966 This function assumes attributes are defined as \":keyword
2967 value\" pairs. It is appropriate for `:attr_html' like
2970 (let ((value (org-element-property attribute element
)))
2972 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
2973 (if property
(plist-get attributes property
) attributes
)))
2975 (defun org-export-get-caption (element &optional shortp
)
2976 "Return caption from ELEMENT as a secondary string.
2978 When optional argument SHORTP is non-nil, return short caption,
2979 as a secondary string, instead.
2981 Caption lines are separated by a white space."
2982 (let ((full-caption (org-element-property :caption element
)) caption
)
2983 (dolist (line full-caption
(cdr caption
))
2984 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
2986 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
2989 ;;;; For Export Snippets
2991 ;; Every export snippet is transmitted to the back-end. Though, the
2992 ;; latter will only retain one type of export-snippet, ignoring
2993 ;; others, based on the former's target back-end. The function
2994 ;; `org-export-snippet-backend' returns that back-end for a given
2997 (defun org-export-snippet-backend (export-snippet)
2998 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2999 Translation, with `org-export-snippet-translation-alist', is
3001 (let ((back-end (org-element-property :back-end export-snippet
)))
3003 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3009 ;; `org-export-collect-footnote-definitions' is a tool to list
3010 ;; actually used footnotes definitions in the whole parse tree, or in
3011 ;; an headline, in order to add footnote listings throughout the
3014 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3015 ;; back-ends, when they need to attach the footnote definition only to
3016 ;; the first occurrence of the corresponding label.
3018 ;; `org-export-get-footnote-definition' and
3019 ;; `org-export-get-footnote-number' provide easier access to
3020 ;; additional information relative to a footnote reference.
3022 (defun org-export-collect-footnote-definitions (data info
)
3023 "Return an alist between footnote numbers, labels and definitions.
3025 DATA is the parse tree from which definitions are collected.
3026 INFO is the plist used as a communication channel.
3028 Definitions are sorted by order of references. They either
3029 appear as Org data or as a secondary string for inlined
3030 footnotes. Unreferenced definitions are ignored."
3032 collect-fn
; for byte-compiler.
3036 ;; Collect footnote number, label and definition in DATA.
3038 data
'footnote-reference
3040 (when (org-export-footnote-first-reference-p fn info
)
3041 (let ((def (org-export-get-footnote-definition fn info
)))
3043 (list (org-export-get-footnote-number fn info
)
3044 (org-element-property :label fn
)
3047 ;; Also search in definition for nested footnotes.
3048 (when (eq (org-element-property :type fn
) 'standard
)
3049 (funcall collect-fn def
)))))
3050 ;; Don't enter footnote definitions since it will happen
3051 ;; when their first reference is found.
3052 info nil
'footnote-definition
)))))
3053 (funcall collect-fn
(plist-get info
:parse-tree
))
3054 (reverse num-alist
)))
3056 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3057 "Non-nil when a footnote reference is the first one for its label.
3059 FOOTNOTE-REFERENCE is the footnote reference being considered.
3060 INFO is the plist used as a communication channel."
3061 (let ((label (org-element-property :label footnote-reference
)))
3062 ;; Anonymous footnotes are always a first reference.
3064 ;; Otherwise, return the first footnote with the same LABEL and
3065 ;; test if it is equal to FOOTNOTE-REFERENCE.
3066 (let* (search-refs ; for byte-compiler.
3071 data
'footnote-reference
3074 ((string= (org-element-property :label fn
) label
)
3076 ;; If FN isn't inlined, be sure to traverse its
3077 ;; definition before resuming search. See
3078 ;; comments in `org-export-get-footnote-number'
3079 ;; for more information.
3080 ((eq (org-element-property :type fn
) 'standard
)
3081 (funcall search-refs
3082 (org-export-get-footnote-definition fn info
)))))
3083 ;; Don't enter footnote definitions since it will
3084 ;; happen when their first reference is found.
3085 info
'first-match
'footnote-definition
)))))
3086 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3087 footnote-reference
)))))
3089 (defun org-export-get-footnote-definition (footnote-reference info
)
3090 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3091 INFO is the plist used as a communication channel."
3092 (let ((label (org-element-property :label footnote-reference
)))
3093 (or (org-element-property :inline-definition footnote-reference
)
3094 (cdr (assoc label
(plist-get info
:footnote-definition-alist
))))))
3096 (defun org-export-get-footnote-number (footnote info
)
3097 "Return number associated to a footnote.
3099 FOOTNOTE is either a footnote reference or a footnote definition.
3100 INFO is the plist used as a communication channel."
3101 (let* ((label (org-element-property :label footnote
))
3103 search-ref
; For byte-compiler.
3107 ;; Search footnote references through DATA, filling
3108 ;; SEEN-REFS along the way.
3110 data
'footnote-reference
3112 (let ((fn-lbl (org-element-property :label fn
)))
3114 ;; Anonymous footnote match: return number.
3115 ((and (not fn-lbl
) (eq fn footnote
))
3116 (throw 'exit
(1+ (length seen-refs
))))
3117 ;; Labels match: return number.
3118 ((and label
(string= label fn-lbl
))
3119 (throw 'exit
(1+ (length seen-refs
))))
3120 ;; Anonymous footnote: it's always a new one. Also,
3121 ;; be sure to return nil from the `cond' so
3122 ;; `first-match' doesn't get us out of the loop.
3123 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3124 ;; Label not seen so far: add it so SEEN-REFS.
3126 ;; Also search for subsequent references in
3127 ;; footnote definition so numbering follows reading
3128 ;; logic. Note that we don't have to care about
3129 ;; inline definitions, since `org-element-map'
3130 ;; already traverses them at the right time.
3132 ;; Once again, return nil to stay in the loop.
3133 ((not (member fn-lbl seen-refs
))
3134 (push fn-lbl seen-refs
)
3136 (org-export-get-footnote-definition fn info
))
3138 ;; Don't enter footnote definitions since it will happen
3139 ;; when their first reference is found.
3140 info
'first-match
'footnote-definition
)))))
3141 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3146 ;; `org-export-get-relative-level' is a shortcut to get headline
3147 ;; level, relatively to the lower headline level in the parsed tree.
3149 ;; `org-export-get-headline-number' returns the section number of an
3150 ;; headline, while `org-export-number-to-roman' allows to convert it
3151 ;; to roman numbers.
3153 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3154 ;; `org-export-last-sibling-p' are three useful predicates when it
3155 ;; comes to fulfill the `:headline-levels' property.
3157 (defun org-export-get-relative-level (headline info
)
3158 "Return HEADLINE relative level within current parsed tree.
3159 INFO is a plist holding contextual information."
3160 (+ (org-element-property :level headline
)
3161 (or (plist-get info
:headline-offset
) 0)))
3163 (defun org-export-low-level-p (headline info
)
3164 "Non-nil when HEADLINE is considered as low level.
3166 INFO is a plist used as a communication channel.
3168 A low level headlines has a relative level greater than
3169 `:headline-levels' property value.
3171 Return value is the difference between HEADLINE relative level
3172 and the last level being considered as high enough, or nil."
3173 (let ((limit (plist-get info
:headline-levels
)))
3174 (when (wholenump limit
)
3175 (let ((level (org-export-get-relative-level headline info
)))
3176 (and (> level limit
) (- level limit
))))))
3178 (defun org-export-get-headline-number (headline info
)
3179 "Return HEADLINE numbering as a list of numbers.
3180 INFO is a plist holding contextual information."
3181 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3183 (defun org-export-numbered-headline-p (headline info
)
3184 "Return a non-nil value if HEADLINE element should be numbered.
3185 INFO is a plist used as a communication channel."
3186 (let ((sec-num (plist-get info
:section-numbers
))
3187 (level (org-export-get-relative-level headline info
)))
3188 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3190 (defun org-export-number-to-roman (n)
3191 "Convert integer N into a roman numeral."
3192 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3193 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3194 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3198 (number-to-string n
)
3200 (if (>= n
(caar roman
))
3201 (setq n
(- n
(caar roman
))
3202 res
(concat res
(cdar roman
)))
3206 (defun org-export-get-tags (element info
&optional tags inherited
)
3207 "Return list of tags associated to ELEMENT.
3209 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3210 is a plist used as a communication channel.
3212 Select tags (see `org-export-select-tags') and exclude tags (see
3213 `org-export-exclude-tags') are removed from the list.
3215 When non-nil, optional argument TAGS should be a list of strings.
3216 Any tag belonging to this list will also be removed.
3218 When optional argument INHERITED is non-nil, tags can also be
3219 inherited from parent headlines and FILETAGS keywords."
3221 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3222 (member tag
(plist-get info
:exclude-tags
))
3224 (if (not inherited
) (org-element-property :tags element
)
3225 ;; Build complete list of inherited tags.
3226 (let ((current-tag-list (org-element-property :tags element
)))
3231 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3232 (not (member tag current-tag-list
)))
3233 (push tag current-tag-list
)))
3234 (org-element-property :tags parent
)))
3235 (org-export-get-genealogy element
))
3236 ;; Add FILETAGS keywords and return results.
3237 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3239 (defun org-export-get-node-property (property blob
&optional inherited
)
3240 "Return node PROPERTY value for BLOB.
3242 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3245 If optional argument INHERITED is non-nil, the value can be
3246 inherited from a parent headline.
3248 Return value is a string or nil."
3249 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3250 (org-export-get-parent-headline blob
))))
3251 (if (not inherited
) (org-element-property property blob
)
3252 (let ((parent headline
) value
)
3255 (when (plist-member (nth 1 parent
) property
)
3256 (throw 'found
(org-element-property property parent
)))
3257 (setq parent
(org-element-property :parent parent
))))))))
3259 (defun org-export-get-category (blob info
)
3260 "Return category for element or object BLOB.
3262 INFO is a plist used as a communication channel.
3264 CATEGORY is automatically inherited from a parent headline, from
3265 #+CATEGORY: keyword or created out of original file name. If all
3266 fail, the fall-back value is \"???\"."
3267 (or (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3268 (org-export-get-parent-headline blob
))))
3269 ;; Almost like `org-export-node-property', but we cannot trust
3270 ;; `plist-member' as every headline has a `:category'
3271 ;; property, would it be nil or equal to "???" (which has the
3273 (let ((parent headline
) value
)
3276 (let ((category (org-element-property :category parent
)))
3277 (and category
(not (equal "???" category
))
3278 (throw 'found category
)))
3279 (setq parent
(org-element-property :parent parent
))))))
3281 (plist-get info
:parse-tree
) 'keyword
3283 (when (equal (org-element-property :key kwd
) "CATEGORY")
3284 (org-element-property :value kwd
)))
3286 (let ((file (plist-get info
:input-file
)))
3287 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3290 (defun org-export-first-sibling-p (headline info
)
3291 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3292 INFO is a plist used as a communication channel."
3293 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3296 (defun org-export-last-sibling-p (headline info
)
3297 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3298 INFO is a plist used as a communication channel."
3299 (not (org-export-get-next-element headline info
)))
3304 ;; `org-export-solidify-link-text' turns a string into a safer version
3305 ;; for links, replacing most non-standard characters with hyphens.
3307 ;; `org-export-get-coderef-format' returns an appropriate format
3308 ;; string for coderefs.
3310 ;; `org-export-inline-image-p' returns a non-nil value when the link
3311 ;; provided should be considered as an inline image.
3313 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3314 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3315 ;; returns an appropriate unique identifier when found, or nil.
3317 ;; `org-export-resolve-id-link' returns the first headline with
3318 ;; specified id or custom-id in parse tree, the path to the external
3319 ;; file with the id or nil when neither was found.
3321 ;; `org-export-resolve-coderef' associates a reference to a line
3322 ;; number in the element it belongs, or returns the reference itself
3323 ;; when the element isn't numbered.
3325 (defun org-export-solidify-link-text (s)
3326 "Take link text S and make a safe target out of it."
3328 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-]+") "-")))
3330 (defun org-export-get-coderef-format (path desc
)
3331 "Return format string for code reference link.
3332 PATH is the link path. DESC is its description."
3334 (cond ((not desc
) "%s")
3335 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3336 (replace-match "%s" t t desc
))
3339 (defun org-export-inline-image-p (link &optional rules
)
3340 "Non-nil if LINK object points to an inline image.
3342 Optional argument is a set of RULES defining inline images. It
3343 is an alist where associations have the following shape:
3347 Applying a rule means apply REGEXP against LINK's path when its
3348 type is TYPE. The function will return a non-nil value if any of
3349 the provided rules is non-nil. The default rule is
3350 `org-export-default-inline-image-rule'.
3352 This only applies to links without a description."
3353 (and (not (org-element-contents link
))
3354 (let ((case-fold-search t
)
3355 (rules (or rules org-export-default-inline-image-rule
)))
3359 (and (string= (org-element-property :type link
) (car rule
))
3360 (string-match (cdr rule
)
3361 (org-element-property :path link
))
3364 ;; Return nil if no rule matched.
3367 (defun org-export-resolve-coderef (ref info
)
3368 "Resolve a code reference REF.
3370 INFO is a plist used as a communication channel.
3372 Return associated line number in source code, or REF itself,
3373 depending on src-block or example element's switches."
3375 (plist-get info
:parse-tree
) '(example-block src-block
)
3378 (insert (org-trim (org-element-property :value el
)))
3379 (let* ((label-fmt (regexp-quote
3380 (or (org-element-property :label-fmt el
)
3381 org-coderef-label-format
)))
3383 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3384 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3385 ;; Element containing REF is found. Resolve it to either
3386 ;; a label or a line number, as needed.
3387 (when (re-search-backward ref-re nil t
)
3389 ((org-element-property :use-labels el
) ref
)
3390 ((eq (org-element-property :number-lines el
) 'continued
)
3391 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3392 (t (line-number-at-pos)))))))
3395 (defun org-export-resolve-fuzzy-link (link info
)
3396 "Return LINK destination.
3398 INFO is a plist holding contextual information.
3400 Return value can be an object, an element, or nil:
3402 - If LINK path matches a target object (i.e. <<path>>) or
3403 element (i.e. \"#+TARGET: path\"), return it.
3405 - If LINK path exactly matches the name affiliated keyword
3406 \(i.e. #+NAME: path) of an element, return that element.
3408 - If LINK path exactly matches any headline name, return that
3409 element. If more than one headline share that name, priority
3410 will be given to the one with the closest common ancestor, if
3411 any, or the first one in the parse tree otherwise.
3413 - Otherwise, return nil.
3415 Assume LINK type is \"fuzzy\"."
3416 (let* ((path (org-element-property :path link
))
3417 (match-title-p (eq (aref path
0) ?
*)))
3419 ;; First try to find a matching "<<path>>" unless user specified
3420 ;; he was looking for an headline (path starts with a *
3422 ((and (not match-title-p
)
3423 (loop for target in
(plist-get info
:target-list
)
3424 when
(string= (org-element-property :value target
) path
)
3426 ;; Then try to find an element with a matching "#+NAME: path"
3427 ;; affiliated keyword.
3428 ((and (not match-title-p
)
3430 (plist-get info
:parse-tree
) org-element-all-elements
3432 (when (string= (org-element-property :name el
) path
) el
))
3433 info
'first-match
)))
3434 ;; Last case: link either points to an headline or to
3435 ;; nothingness. Try to find the source, with priority given to
3436 ;; headlines with the closest common ancestor. If such candidate
3437 ;; is found, return it, otherwise return nil.
3439 (let ((find-headline
3441 ;; Return first headline whose `:raw-value' property
3442 ;; is NAME in parse tree DATA, or nil.
3448 (org-element-property :raw-value headline
)
3451 info
'first-match
)))))
3452 ;; Search among headlines sharing an ancestor with link,
3453 ;; from closest to farthest.
3457 (when (eq (org-element-type parent
) 'headline
)
3458 (let ((foundp (funcall find-headline path parent
)))
3459 (when foundp
(throw 'exit foundp
)))))
3460 (org-export-get-genealogy link
)) nil
)
3461 ;; No match with a common ancestor: try the full parse-tree.
3462 (funcall find-headline
3463 (if match-title-p
(substring path
1) path
)
3464 (plist-get info
:parse-tree
))))))))
3466 (defun org-export-resolve-id-link (link info
)
3467 "Return headline referenced as LINK destination.
3469 INFO is a plist used as a communication channel.
3471 Return value can be the headline element matched in current parse
3472 tree, a file name or nil. Assume LINK type is either \"id\" or
3474 (let ((id (org-element-property :path link
)))
3475 ;; First check if id is within the current parse tree.
3476 (or (org-element-map
3477 (plist-get info
:parse-tree
) 'headline
3479 (when (or (string= (org-element-property :id headline
) id
)
3480 (string= (org-element-property :custom-id headline
) id
))
3483 ;; Otherwise, look for external files.
3484 (cdr (assoc id
(plist-get info
:id-alist
))))))
3486 (defun org-export-resolve-radio-link (link info
)
3487 "Return radio-target object referenced as LINK destination.
3489 INFO is a plist used as a communication channel.
3491 Return value can be a radio-target object or nil. Assume LINK
3492 has type \"radio\"."
3493 (let ((path (org-element-property :path link
)))
3495 (plist-get info
:parse-tree
) 'radio-target
3497 (when (equal (org-element-property :value radio
) path
) radio
))
3498 info
'first-match
)))
3503 ;; `org-export-get-ordinal' associates a sequence number to any object
3506 (defun org-export-get-ordinal (element info
&optional types predicate
)
3507 "Return ordinal number of an element or object.
3509 ELEMENT is the element or object considered. INFO is the plist
3510 used as a communication channel.
3512 Optional argument TYPES, when non-nil, is a list of element or
3513 object types, as symbols, that should also be counted in.
3514 Otherwise, only provided element's type is considered.
3516 Optional argument PREDICATE is a function returning a non-nil
3517 value if the current element or object should be counted in. It
3518 accepts two arguments: the element or object being considered and
3519 the plist used as a communication channel. This allows to count
3520 only a certain type of objects (i.e. inline images).
3522 Return value is a list of numbers if ELEMENT is an headline or an
3523 item. It is nil for keywords. It represents the footnote number
3524 for footnote definitions and footnote references. If ELEMENT is
3525 a target, return the same value as if ELEMENT was the closest
3526 table, item or headline containing the target. In any other
3527 case, return the sequence number of ELEMENT among elements or
3528 objects of the same type."
3529 ;; A target keyword, representing an invisible target, never has
3530 ;; a sequence number.
3531 (unless (eq (org-element-type element
) 'keyword
)
3532 ;; Ordinal of a target object refer to the ordinal of the closest
3533 ;; table, item, or headline containing the object.
3534 (when (eq (org-element-type element
) 'target
)
3536 (loop for parent in
(org-export-get-genealogy element
)
3539 (org-element-type parent
)
3540 '(footnote-definition footnote-reference headline item
3543 (case (org-element-type element
)
3544 ;; Special case 1: An headline returns its number as a list.
3545 (headline (org-export-get-headline-number element info
))
3546 ;; Special case 2: An item returns its number as a list.
3547 (item (let ((struct (org-element-property :structure element
)))
3548 (org-list-get-item-number
3549 (org-element-property :begin element
)
3551 (org-list-prevs-alist struct
)
3552 (org-list-parents-alist struct
))))
3553 ((footnote-definition footnote-reference
)
3554 (org-export-get-footnote-number element info
))
3557 ;; Increment counter until ELEMENT is found again.
3559 (plist-get info
:parse-tree
) (or types
(org-element-type element
))
3562 ((eq element el
) (1+ counter
))
3563 ((not predicate
) (incf counter
) nil
)
3564 ((funcall predicate el info
) (incf counter
) nil
)))
3565 info
'first-match
))))))
3570 ;; `org-export-get-loc' counts number of code lines accumulated in
3571 ;; src-block or example-block elements with a "+n" switch until
3572 ;; a given element, excluded. Note: "-n" switches reset that count.
3574 ;; `org-export-unravel-code' extracts source code (along with a code
3575 ;; references alist) from an `element-block' or `src-block' type
3578 ;; `org-export-format-code' applies a formatting function to each line
3579 ;; of code, providing relative line number and code reference when
3580 ;; appropriate. Since it doesn't access the original element from
3581 ;; which the source code is coming, it expects from the code calling
3582 ;; it to know if lines should be numbered and if code references
3585 ;; Eventually, `org-export-format-code-default' is a higher-level
3586 ;; function (it makes use of the two previous functions) which handles
3587 ;; line numbering and code references inclusion, and returns source
3588 ;; code in a format suitable for plain text or verbatim output.
3590 (defun org-export-get-loc (element info
)
3591 "Return accumulated lines of code up to ELEMENT.
3593 INFO is the plist used as a communication channel.
3595 ELEMENT is excluded from count."
3598 (plist-get info
:parse-tree
)
3599 `(src-block example-block
,(org-element-type element
))
3602 ;; ELEMENT is reached: Quit the loop.
3604 ;; Only count lines from src-block and example-block elements
3605 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3606 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3607 ((let ((linums (org-element-property :number-lines el
)))
3609 ;; Accumulate locs or reset them.
3610 (let ((lines (org-count-lines
3611 (org-trim (org-element-property :value el
)))))
3612 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3613 ;; Return nil to stay in the loop.
3619 (defun org-export-unravel-code (element)
3620 "Clean source code and extract references out of it.
3622 ELEMENT has either a `src-block' an `example-block' type.
3624 Return a cons cell whose CAR is the source code, cleaned from any
3625 reference and protective comma and CDR is an alist between
3626 relative line number (integer) and name of code reference on that
3628 (let* ((line 0) refs
3629 ;; Get code and clean it. Remove blank lines at its
3630 ;; beginning and end.
3631 (code (let ((c (replace-regexp-in-string
3632 "\\`\\([ \t]*\n\\)+" ""
3633 (replace-regexp-in-string
3634 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3635 (org-element-property :value element
)))))
3636 ;; If appropriate, remove global indentation.
3637 (if (or org-src-preserve-indentation
3638 (org-element-property :preserve-indent element
))
3640 (org-remove-indentation c
))))
3641 ;; Get format used for references.
3642 (label-fmt (regexp-quote
3643 (or (org-element-property :label-fmt element
)
3644 org-coderef-label-format
)))
3645 ;; Build a regexp matching a loc with a reference.
3647 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3648 (replace-regexp-in-string
3649 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3652 ;; Code with references removed.
3653 (org-element-normalize-string
3657 (if (not (string-match with-ref-re loc
)) loc
3658 ;; Ref line: remove ref, and signal its position in REFS.
3659 (push (cons line
(match-string 3 loc
)) refs
)
3660 (replace-match "" nil nil loc
1)))
3661 (org-split-string code
"\n") "\n"))
3665 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3666 "Format CODE by applying FUN line-wise and return it.
3668 CODE is a string representing the code to format. FUN is
3669 a function. It must accept three arguments: a line of
3670 code (string), the current line number (integer) or nil and the
3671 reference associated to the current line (string) or nil.
3673 Optional argument NUM-LINES can be an integer representing the
3674 number of code lines accumulated until the current code. Line
3675 numbers passed to FUN will take it into account. If it is nil,
3676 FUN's second argument will always be nil. This number can be
3677 obtained with `org-export-get-loc' function.
3679 Optional argument REF-ALIST can be an alist between relative line
3680 number (i.e. ignoring NUM-LINES) and the name of the code
3681 reference on it. If it is nil, FUN's third argument will always
3682 be nil. It can be obtained through the use of
3683 `org-export-unravel-code' function."
3684 (let ((--locs (org-split-string code
"\n"))
3686 (org-element-normalize-string
3690 (let ((--ref (cdr (assq --line ref-alist
))))
3691 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3694 (defun org-export-format-code-default (element info
)
3695 "Return source code from ELEMENT, formatted in a standard way.
3697 ELEMENT is either a `src-block' or `example-block' element. INFO
3698 is a plist used as a communication channel.
3700 This function takes care of line numbering and code references
3701 inclusion. Line numbers, when applicable, appear at the
3702 beginning of the line, separated from the code by two white
3703 spaces. Code references, on the other hand, appear flushed to
3704 the right, separated by six white spaces from the widest line of
3706 ;; Extract code and references.
3707 (let* ((code-info (org-export-unravel-code element
))
3708 (code (car code-info
))
3709 (code-lines (org-split-string code
"\n"))
3710 (refs (and (org-element-property :retain-labels element
)
3712 ;; Handle line numbering.
3713 (num-start (case (org-element-property :number-lines element
)
3714 (continued (org-export-get-loc element info
))
3719 (length (number-to-string
3720 (+ (length code-lines
) num-start
))))))
3721 ;; Prepare references display, if required. Any reference
3722 ;; should start six columns after the widest line of code,
3723 ;; wrapped with parenthesis.
3725 (+ (apply 'max
(mapcar 'length code-lines
))
3726 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3727 (org-export-format-code
3729 (lambda (loc line-num ref
)
3730 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3735 (concat (make-string
3737 (+ (length loc
) (length number-str
))) ?
)
3738 (format "(%s)" ref
))))))
3744 ;; `org-export-table-has-special-column-p' and and
3745 ;; `org-export-table-row-is-special-p' are predicates used to look for
3746 ;; meta-information about the table structure.
3748 ;; `org-table-has-header-p' tells when the rows before the first rule
3749 ;; should be considered as table's header.
3751 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3752 ;; and `org-export-table-cell-borders' extract information from
3753 ;; a table-cell element.
3755 ;; `org-export-table-dimensions' gives the number on rows and columns
3756 ;; in the table, ignoring horizontal rules and special columns.
3757 ;; `org-export-table-cell-address', given a table-cell object, returns
3758 ;; the absolute address of a cell. On the other hand,
3759 ;; `org-export-get-table-cell-at' does the contrary.
3761 ;; `org-export-table-cell-starts-colgroup-p',
3762 ;; `org-export-table-cell-ends-colgroup-p',
3763 ;; `org-export-table-row-starts-rowgroup-p',
3764 ;; `org-export-table-row-ends-rowgroup-p',
3765 ;; `org-export-table-row-starts-header-p' and
3766 ;; `org-export-table-row-ends-header-p' indicate position of current
3767 ;; row or cell within the table.
3769 (defun org-export-table-has-special-column-p (table)
3770 "Non-nil when TABLE has a special column.
3771 All special columns will be ignored during export."
3772 ;; The table has a special column when every first cell of every row
3773 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3774 ;; "*" "_" and "^". Though, do not consider a first row containing
3775 ;; only empty cells as special.
3776 (let ((special-column-p 'empty
))
3780 (when (eq (org-element-property :type row
) 'standard
)
3781 (let ((value (org-element-contents
3782 (car (org-element-contents row
)))))
3783 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3784 (setq special-column-p
'special
))
3786 (t (throw 'exit nil
))))))
3787 (org-element-contents table
))
3788 (eq special-column-p
'special
))))
3790 (defun org-export-table-has-header-p (table info
)
3791 "Non-nil when TABLE has an header.
3793 INFO is a plist used as a communication channel.
3795 A table has an header when it contains at least two row groups."
3796 (let ((rowgroup 1) row-flag
)
3802 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
3803 (incf rowgroup
) (setq row-flag nil
))
3804 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
3805 (setq row-flag t
) nil
)))
3808 (defun org-export-table-row-is-special-p (table-row info
)
3809 "Non-nil if TABLE-ROW is considered special.
3811 INFO is a plist used as the communication channel.
3813 All special rows will be ignored during export."
3814 (when (eq (org-element-property :type table-row
) 'standard
)
3815 (let ((first-cell (org-element-contents
3816 (car (org-element-contents table-row
)))))
3817 ;; A row is special either when...
3819 ;; ... it starts with a field only containing "/",
3820 (equal first-cell
'("/"))
3821 ;; ... the table contains a special column and the row start
3822 ;; with a marking character among, "^", "_", "$" or "!",
3823 (and (org-export-table-has-special-column-p
3824 (org-export-get-parent table-row
))
3825 (member first-cell
'(("^") ("_") ("$") ("!"))))
3826 ;; ... it contains only alignment cookies and empty cells.
3827 (let ((special-row-p 'empty
))
3831 (let ((value (org-element-contents cell
)))
3832 ;; Since VALUE is a secondary string, the following
3833 ;; checks avoid expanding it with `org-export-data'.
3835 ((and (not (cdr value
))
3836 (stringp (car value
))
3837 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3839 (setq special-row-p
'cookie
))
3840 (t (throw 'exit nil
)))))
3841 (org-element-contents table-row
))
3842 (eq special-row-p
'cookie
)))))))
3844 (defun org-export-table-row-group (table-row info
)
3845 "Return TABLE-ROW's group.
3847 INFO is a plist used as the communication channel.
3849 Return value is the group number, as an integer, or nil special
3850 rows and table rules. Group 1 is also table's header."
3851 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3852 (org-export-table-row-is-special-p table-row info
))
3853 (let ((group 0) row-flag
)
3858 ((and (eq (org-element-property :type row
) 'standard
)
3859 (not (org-export-table-row-is-special-p row info
)))
3860 (unless row-flag
(incf group
) (setq row-flag t
)))
3861 ((eq (org-element-property :type row
) 'rule
)
3862 (setq row-flag nil
)))
3863 (when (eq table-row row
) (throw 'found group
)))
3864 (org-element-contents (org-export-get-parent table-row
)))))))
3866 (defun org-export-table-cell-width (table-cell info
)
3867 "Return TABLE-CELL contents width.
3869 INFO is a plist used as the communication channel.
3871 Return value is the width given by the last width cookie in the
3872 same column as TABLE-CELL, or nil."
3873 (let* ((row (org-export-get-parent table-cell
))
3874 (column (let ((cells (org-element-contents row
)))
3875 (- (length cells
) (length (memq table-cell cells
)))))
3876 (table (org-export-get-parent-table table-cell
))
3881 ;; In a special row, try to find a width cookie at COLUMN.
3882 ((org-export-table-row-is-special-p row info
)
3883 (let ((value (org-element-contents
3884 (elt (org-element-contents row
) column
))))
3885 ;; The following checks avoid expanding unnecessarily the
3886 ;; cell with `org-export-data'
3889 (stringp (car value
))
3890 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
3891 (match-string 1 (car value
)))
3893 (string-to-number (match-string 1 (car value
)))))))
3894 ;; Ignore table rules.
3895 ((eq (org-element-property :type row
) 'rule
))))
3896 (org-element-contents table
))
3900 (defun org-export-table-cell-alignment (table-cell info
)
3901 "Return TABLE-CELL contents alignment.
3903 INFO is a plist used as the communication channel.
3905 Return alignment as specified by the last alignment cookie in the
3906 same column as TABLE-CELL. If no such cookie is found, a default
3907 alignment value will be deduced from fraction of numbers in the
3908 column (see `org-table-number-fraction' for more information).
3909 Possible values are `left', `right' and `center'."
3910 (let* ((row (org-export-get-parent table-cell
))
3911 (column (let ((cells (org-element-contents row
)))
3912 (- (length cells
) (length (memq table-cell cells
)))))
3913 (table (org-export-get-parent-table table-cell
))
3920 ;; In a special row, try to find an alignment cookie at
3922 ((org-export-table-row-is-special-p row info
)
3923 (let ((value (org-element-contents
3924 (elt (org-element-contents row
) column
))))
3925 ;; Since VALUE is a secondary string, the following checks
3926 ;; avoid useless expansion through `org-export-data'.
3929 (stringp (car value
))
3930 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3932 (match-string 1 (car value
)))
3933 (setq cookie-align
(match-string 1 (car value
))))))
3934 ;; Ignore table rules.
3935 ((eq (org-element-property :type row
) 'rule
))
3936 ;; In a standard row, check if cell's contents are expressing
3937 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3938 ;; Though, don't bother if an alignment cookie has already
3939 ;; defined cell's alignment.
3941 (let ((value (org-export-data
3942 (org-element-contents
3943 (elt (org-element-contents row
) column
))
3946 (when (string-match org-table-number-regexp value
)
3947 (incf number-cells
))))))
3948 (org-element-contents table
))
3949 ;; Return value. Alignment specified by cookies has precedence
3950 ;; over alignment deduced from cells contents.
3951 (cond ((equal cookie-align
"l") 'left
)
3952 ((equal cookie-align
"r") 'right
)
3953 ((equal cookie-align
"c") 'center
)
3954 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
3958 (defun org-export-table-cell-borders (table-cell info
)
3959 "Return TABLE-CELL borders.
3961 INFO is a plist used as a communication channel.
3963 Return value is a list of symbols, or nil. Possible values are:
3964 `top', `bottom', `above', `below', `left' and `right'. Note:
3965 `top' (resp. `bottom') only happen for a cell in the first
3966 row (resp. last row) of the table, ignoring table rules, if any.
3968 Returned borders ignore special rows."
3969 (let* ((row (org-export-get-parent table-cell
))
3970 (table (org-export-get-parent-table table-cell
))
3972 ;; Top/above border? TABLE-CELL has a border above when a rule
3973 ;; used to demarcate row groups can be found above. Hence,
3974 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3975 ;; another regular row has to be found above that rule.
3979 (cond ((eq (org-element-property :type row
) 'rule
)
3981 ((not (org-export-table-row-is-special-p row info
))
3982 (if rule-flag
(throw 'exit
(push 'above borders
))
3983 (throw 'exit nil
)))))
3984 ;; Look at every row before the current one.
3985 (cdr (memq row
(reverse (org-element-contents table
)))))
3986 ;; No rule above, or rule found starts the table (ignoring any
3987 ;; special row): TABLE-CELL is at the top of the table.
3988 (when rule-flag
(push 'above borders
))
3989 (push 'top borders
)))
3990 ;; Bottom/below border? TABLE-CELL has a border below when next
3991 ;; non-regular row below is a rule.
3995 (cond ((eq (org-element-property :type row
) 'rule
)
3997 ((not (org-export-table-row-is-special-p row info
))
3998 (if rule-flag
(throw 'exit
(push 'below borders
))
3999 (throw 'exit nil
)))))
4000 ;; Look at every row after the current one.
4001 (cdr (memq row
(org-element-contents table
))))
4002 ;; No rule below, or rule found ends the table (modulo some
4003 ;; special row): TABLE-CELL is at the bottom of the table.
4004 (when rule-flag
(push 'below borders
))
4005 (push 'bottom borders
)))
4006 ;; Right/left borders? They can only be specified by column
4007 ;; groups. Column groups are defined in a row starting with "/".
4008 ;; Also a column groups row only contains "<", "<>", ">" or blank
4011 (let ((column (let ((cells (org-element-contents row
)))
4012 (- (length cells
) (length (memq table-cell cells
))))))
4015 (unless (eq (org-element-property :type row
) 'rule
)
4016 (when (equal (org-element-contents
4017 (car (org-element-contents row
)))
4019 (let ((column-groups
4022 (let ((value (org-element-contents cell
)))
4023 (when (member value
'(("<") ("<>") (">") nil
))
4025 (org-element-contents row
))))
4026 ;; There's a left border when previous cell, if
4027 ;; any, ends a group, or current one starts one.
4028 (when (or (and (not (zerop column
))
4029 (member (elt column-groups
(1- column
))
4031 (member (elt column-groups column
) '("<" "<>")))
4032 (push 'left borders
))
4033 ;; There's a right border when next cell, if any,
4034 ;; starts a group, or current one ends one.
4035 (when (or (and (/= (1+ column
) (length column-groups
))
4036 (member (elt column-groups
(1+ column
))
4038 (member (elt column-groups column
) '(">" "<>")))
4039 (push 'right borders
))
4040 (throw 'exit nil
)))))
4041 ;; Table rows are read in reverse order so last column groups
4042 ;; row has precedence over any previous one.
4043 (reverse (org-element-contents table
)))))
4047 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4048 "Non-nil when TABLE-CELL is at the beginning of a row group.
4049 INFO is a plist used as a communication channel."
4050 ;; A cell starts a column group either when it is at the beginning
4051 ;; of a row (or after the special column, if any) or when it has
4053 (or (eq (org-element-map
4054 (org-export-get-parent table-cell
)
4055 'table-cell
'identity info
'first-match
)
4057 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4059 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4060 "Non-nil when TABLE-CELL is at the end of a row group.
4061 INFO is a plist used as a communication channel."
4062 ;; A cell ends a column group either when it is at the end of a row
4063 ;; or when it has a right border.
4064 (or (eq (car (last (org-element-contents
4065 (org-export-get-parent table-cell
))))
4067 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4069 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4070 "Non-nil when TABLE-ROW is at the beginning of a column group.
4071 INFO is a plist used as a communication channel."
4072 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4073 (org-export-table-row-is-special-p table-row info
))
4074 (let ((borders (org-export-table-cell-borders
4075 (car (org-element-contents table-row
)) info
)))
4076 (or (memq 'top borders
) (memq 'above borders
)))))
4078 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4079 "Non-nil when TABLE-ROW is at the end of a column group.
4080 INFO is a plist used as a communication channel."
4081 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4082 (org-export-table-row-is-special-p table-row info
))
4083 (let ((borders (org-export-table-cell-borders
4084 (car (org-element-contents table-row
)) info
)))
4085 (or (memq 'bottom borders
) (memq 'below borders
)))))
4087 (defun org-export-table-row-starts-header-p (table-row info
)
4088 "Non-nil when TABLE-ROW is the first table header's row.
4089 INFO is a plist used as a communication channel."
4090 (and (org-export-table-has-header-p
4091 (org-export-get-parent-table table-row
) info
)
4092 (org-export-table-row-starts-rowgroup-p table-row info
)
4093 (= (org-export-table-row-group table-row info
) 1)))
4095 (defun org-export-table-row-ends-header-p (table-row info
)
4096 "Non-nil when TABLE-ROW is the last table header's row.
4097 INFO is a plist used as a communication channel."
4098 (and (org-export-table-has-header-p
4099 (org-export-get-parent-table table-row
) info
)
4100 (org-export-table-row-ends-rowgroup-p table-row info
)
4101 (= (org-export-table-row-group table-row info
) 1)))
4103 (defun org-export-table-dimensions (table info
)
4104 "Return TABLE dimensions.
4106 INFO is a plist used as a communication channel.
4108 Return value is a CONS like (ROWS . COLUMNS) where
4109 ROWS (resp. COLUMNS) is the number of exportable
4110 rows (resp. columns)."
4111 (let (first-row (columns 0) (rows 0))
4112 ;; Set number of rows, and extract first one.
4116 (when (eq (org-element-property :type row
) 'standard
)
4118 (unless first-row
(setq first-row row
)))) info
)
4119 ;; Set number of columns.
4120 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4122 (cons rows columns
)))
4124 (defun org-export-table-cell-address (table-cell info
)
4125 "Return address of a regular TABLE-CELL object.
4127 TABLE-CELL is the cell considered. INFO is a plist used as
4128 a communication channel.
4130 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4131 zero-based index. Only exportable cells are considered. The
4132 function returns nil for other cells."
4133 (let* ((table-row (org-export-get-parent table-cell
))
4134 (table (org-export-get-parent-table table-cell
)))
4135 ;; Ignore cells in special rows or in special column.
4136 (unless (or (org-export-table-row-is-special-p table-row info
)
4137 (and (org-export-table-has-special-column-p table
)
4138 (eq (car (org-element-contents table-row
)) table-cell
)))
4141 (let ((row-count 0))
4145 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4146 ((eq row table-row
) row-count
)
4147 (t (incf row-count
) nil
)))
4150 (let ((col-count 0))
4152 table-row
'table-cell
4154 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4155 info
'first-match
))))))
4157 (defun org-export-get-table-cell-at (address table info
)
4158 "Return regular table-cell object at ADDRESS in TABLE.
4160 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4161 zero-based index. TABLE is a table type element. INFO is
4162 a plist used as a communication channel.
4164 If no table-cell, among exportable cells, is found at ADDRESS,
4166 (let ((column-pos (cdr address
)) (column-count 0))
4168 ;; Row at (car address) or nil.
4169 (let ((row-pos (car address
)) (row-count 0))
4173 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4174 ((= row-count row-pos
) row
)
4175 (t (incf row-count
) nil
)))
4179 (if (= column-count column-pos
) cell
4180 (incf column-count
) nil
))
4181 info
'first-match
)))
4184 ;;;; For Tables Of Contents
4186 ;; `org-export-collect-headlines' builds a list of all exportable
4187 ;; headline elements, maybe limited to a certain depth. One can then
4188 ;; easily parse it and transcode it.
4190 ;; Building lists of tables, figures or listings is quite similar.
4191 ;; Once the generic function `org-export-collect-elements' is defined,
4192 ;; `org-export-collect-tables', `org-export-collect-figures' and
4193 ;; `org-export-collect-listings' can be derived from it.
4195 (defun org-export-collect-headlines (info &optional n
)
4196 "Collect headlines in order to build a table of contents.
4198 INFO is a plist used as a communication channel.
4200 When optional argument N is an integer, it specifies the depth of
4201 the table of contents. Otherwise, it is set to the value of the
4202 last headline level. See `org-export-headline-levels' for more
4205 Return a list of all exportable headlines as parsed elements."
4206 (unless (wholenump n
) (setq n
(plist-get info
:headline-levels
)))
4208 (plist-get info
:parse-tree
)
4211 ;; Strip contents from HEADLINE.
4212 (let ((relative-level (org-export-get-relative-level headline info
)))
4213 (unless (> relative-level n
) headline
)))
4216 (defun org-export-collect-elements (type info
&optional predicate
)
4217 "Collect referenceable elements of a determined type.
4219 TYPE can be a symbol or a list of symbols specifying element
4220 types to search. Only elements with a caption are collected.
4222 INFO is a plist used as a communication channel.
4224 When non-nil, optional argument PREDICATE is a function accepting
4225 one argument, an element of type TYPE. It returns a non-nil
4226 value when that element should be collected.
4228 Return a list of all elements found, in order of appearance."
4230 (plist-get info
:parse-tree
) type
4232 (and (org-element-property :caption element
)
4233 (or (not predicate
) (funcall predicate element
))
4237 (defun org-export-collect-tables (info)
4238 "Build a list of tables.
4239 INFO is a plist used as a communication channel.
4241 Return a list of table elements with a caption."
4242 (org-export-collect-elements 'table info
))
4244 (defun org-export-collect-figures (info predicate
)
4245 "Build a list of figures.
4247 INFO is a plist used as a communication channel. PREDICATE is
4248 a function which accepts one argument: a paragraph element and
4249 whose return value is non-nil when that element should be
4252 A figure is a paragraph type element, with a caption, verifying
4253 PREDICATE. The latter has to be provided since a \"figure\" is
4254 a vague concept that may depend on back-end.
4256 Return a list of elements recognized as figures."
4257 (org-export-collect-elements 'paragraph info predicate
))
4259 (defun org-export-collect-listings (info)
4260 "Build a list of src blocks.
4262 INFO is a plist used as a communication channel.
4264 Return a list of src-block elements with a caption."
4265 (org-export-collect-elements 'src-block info
))
4270 (defconst org-export-smart-quotes-alist
4272 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4273 :texinfo
"@quotedblbase{}")
4274 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4275 :texinfo
"@quotedblleft{}")
4276 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4277 :texinfo
"@quotesinglbase{}")
4278 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4279 :texinfo
"@quoteleft{}")
4280 (apostrophe :utf-8
"’" :html
"’"))
4282 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4283 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4284 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4285 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4286 (apostrophe :utf-8
"’" :html
"’"))
4288 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4289 :texinfo
"@guillemetleft{}")
4290 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4291 :texinfo
"@guillemetright{}")
4292 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4293 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4294 (apostrophe :utf-8
"’" :html
"’"))
4296 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
4297 :texinfo
"@guillemetleft{}@tie{}")
4298 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4299 :texinfo
"@tie{}@guillemetright{}")
4300 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
4301 :texinfo
"@guillemetleft{}@tie{}")
4302 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4303 :texinfo
"@tie{}@guillemetright{}")
4304 (apostrophe :utf-8
"’" :html
"’")))
4305 "Smart quotes translations.
4307 Alist whose CAR is a language string and CDR is an alist with
4308 quote type as key and a plist associating various encodings to
4309 their translation as value.
4311 A quote type can be any symbol among `opening-double-quote',
4312 `closing-double-quote', `opening-single-quote',
4313 `closing-single-quote' and `apostrophe'.
4315 Valid encodings include `:utf-8', `:html', `:latex' and
4318 If no translation is found, the quote character is left as-is.")
4320 (defconst org-export-smart-quotes-regexps
4322 ;; Possible opening quote at beginning of string.
4323 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4324 ;; Possible closing quote at beginning of string.
4325 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4326 ;; Possible apostrophe at beginning of string.
4328 ;; Opening single and double quotes.
4329 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4330 ;; Closing single and double quotes.
4331 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4334 ;; Possible opening quote at end of string.
4335 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4336 ;; Possible closing quote at end of string.
4337 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4338 ;; Possible apostrophe at end of string.
4340 "List of regexps matching a quote or an apostrophe.
4341 In every regexp, quote or apostrophe matched is put in group 1.")
4343 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
4344 "Replace regular quotes with \"smart\" quotes in string S.
4346 ENCODING is a symbol among `:html', `:latex' and `:utf-8'. INFO
4347 is a plist used as a communication channel.
4349 The function has to retrieve information about string
4350 surroundings in parse tree. It can only happen with an
4351 unmodified string. Thus, if S has already been through another
4352 process, a non-nil ORIGINAL optional argument will provide that
4355 Return the new string."
4357 (let* ((prev (org-export-get-previous-element (or original s
) info
))
4358 (pre-blank (and prev
(org-element-property :post-blank prev
)))
4359 (next (org-export-get-next-element (or original s
) info
))
4362 ;; Return smart quote associated to a give quote Q, as
4363 ;; a string. TYPE is a symbol among `open', `close' and
4365 (let ((key (case type
4366 (apostrophe 'apostrophe
)
4367 (open (if (equal "'" q
) 'opening-single-quote
4368 'opening-double-quote
))
4369 (otherwise (if (equal "'" q
) 'closing-single-quote
4370 'closing-double-quote
)))))
4373 (cdr (assoc (plist-get info
:language
)
4374 org-export-smart-quotes-alist
))))
4377 (if (or (equal "\"" s
) (equal "'" s
))
4378 ;; Only a quote: no regexp can match. We have to check both
4379 ;; sides and decide what to do.
4380 (cond ((and (not prev
) (not next
)) s
)
4381 ((not prev
) (funcall get-smart-quote s
'open
))
4382 ((and (not next
) (zerop pre-blank
))
4383 (funcall get-smart-quote s
'close
))
4385 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
4386 (t (funcall get-smart-quote
'open
)))
4387 ;; 1. Replace quote character at the beginning of S.
4390 ((and prev
(zerop pre-blank
)
4391 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
4392 (setq s
(replace-match
4393 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
4396 ((and prev
(zerop pre-blank
)
4397 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
4398 (setq s
(replace-match
4399 (funcall get-smart-quote
(match-string 1 s
) 'close
)
4402 ((and (or (not prev
) (> pre-blank
0))
4403 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
4404 (setq s
(replace-match
4405 (funcall get-smart-quote
(match-string 1 s
) 'open
)
4407 ;; 2. Replace quotes in the middle of the string.
4408 (setq s
(replace-regexp-in-string
4410 (nth 3 org-export-smart-quotes-regexps
)
4412 (funcall get-smart-quote
(match-string 1 text
) 'open
))
4414 (setq s
(replace-regexp-in-string
4416 (nth 4 org-export-smart-quotes-regexps
)
4418 (funcall get-smart-quote
(match-string 1 text
) 'close
))
4420 (setq s
(replace-regexp-in-string
4422 (nth 5 org-export-smart-quotes-regexps
)
4424 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
4426 ;; 3. Replace quote character at the end of S.
4429 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
4430 (setq s
(replace-match
4431 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
4435 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
4436 (setq s
(replace-match
4437 (funcall get-smart-quote
(match-string 1 s
) 'close
)
4440 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
4441 (setq s
(replace-match
4442 (funcall get-smart-quote
(match-string 1 s
) 'open
)
4444 ;; Return string with smart quotes.
4449 ;; Here are various functions to retrieve information about the
4450 ;; neighbourhood of a given element or object. Neighbours of interest
4451 ;; are direct parent (`org-export-get-parent'), parent headline
4452 ;; (`org-export-get-parent-headline'), first element containing an
4453 ;; object, (`org-export-get-parent-element'), parent table
4454 ;; (`org-export-get-parent-table'), previous element or object
4455 ;; (`org-export-get-previous-element') and next element or object
4456 ;; (`org-export-get-next-element').
4458 ;; `org-export-get-genealogy' returns the full genealogy of a given
4459 ;; element or object, from closest parent to full parse tree.
4461 (defun org-export-get-parent (blob)
4462 "Return BLOB parent or nil.
4463 BLOB is the element or object considered."
4464 (org-element-property :parent blob
))
4466 (defun org-export-get-genealogy (blob)
4467 "Return full genealogy relative to a given element or object.
4469 BLOB is the element or object being considered.
4471 Ancestors are returned from closest to farthest, the last one
4472 being the full parse tree."
4473 (let (genealogy (parent blob
))
4474 (while (setq parent
(org-element-property :parent parent
))
4475 (push parent genealogy
))
4476 (nreverse genealogy
)))
4478 (defun org-export-get-parent-headline (blob)
4479 "Return BLOB parent headline or nil.
4480 BLOB is the element or object being considered."
4481 (let ((parent blob
))
4482 (while (and (setq parent
(org-element-property :parent parent
))
4483 (not (eq (org-element-type parent
) 'headline
))))
4486 (defun org-export-get-parent-element (object)
4487 "Return first element containing OBJECT or nil.
4488 OBJECT is the object to consider."
4489 (let ((parent object
))
4490 (while (and (setq parent
(org-element-property :parent parent
))
4491 (memq (org-element-type parent
) org-element-all-objects
)))
4494 (defun org-export-get-parent-table (object)
4495 "Return OBJECT parent table or nil.
4496 OBJECT is either a `table-cell' or `table-element' type object."
4497 (let ((parent object
))
4498 (while (and (setq parent
(org-element-property :parent parent
))
4499 (not (eq (org-element-type parent
) 'table
))))
4502 (defun org-export-get-previous-element (blob info
)
4503 "Return previous element or object.
4504 BLOB is an element or object. INFO is a plist used as
4505 a communication channel. Return previous exportable element or
4506 object, a string, or nil."
4510 (cond ((eq obj blob
) (throw 'exit prev
))
4511 ((memq obj
(plist-get info
:ignore-list
)))
4512 (t (setq prev obj
))))
4513 ;; An object can belong to the contents of its parent or
4514 ;; to a secondary string. We check the latter option
4516 (let ((parent (org-export-get-parent blob
)))
4517 (or (and (not (memq (org-element-type blob
)
4518 org-element-all-elements
))
4520 (org-element-property
4521 (cdr (assq (org-element-type parent
)
4522 org-element-secondary-value-alist
))
4524 (and (memq blob sec-value
) sec-value
)))
4525 (org-element-contents parent
)))))))
4527 (defun org-export-get-next-element (blob info
)
4528 "Return next element or object.
4529 BLOB is an element or object. INFO is a plist used as
4530 a communication channel. Return next exportable element or
4531 object, a string, or nil."
4534 (unless (memq obj
(plist-get info
:ignore-list
))
4535 (throw 'found obj
)))
4536 ;; An object can belong to the contents of its parent or to
4537 ;; a secondary string. We check the latter option first.
4538 (let ((parent (org-export-get-parent blob
)))
4539 (or (and (not (memq (org-element-type blob
)
4540 org-element-all-objects
))
4542 (org-element-property
4543 (cdr (assq (org-element-type parent
)
4544 org-element-secondary-value-alist
))
4546 (cdr (memq blob sec-value
))))
4547 (cdr (memq blob
(org-element-contents parent
))))))
4553 ;; `org-export-translate' translates a string according to language
4554 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4555 ;; variable and a specified charset. `org-export-dictionary' contains
4556 ;; the dictionary used for the translation.
4558 (defconst org-export-dictionary
4560 ("ca" :default
"Autor")
4561 ("cs" :default
"Autor")
4562 ("da" :default
"Ophavsmand")
4563 ("de" :default
"Autor")
4564 ("eo" :html
"Aŭtoro")
4565 ("es" :default
"Autor")
4566 ("fi" :html
"Tekijä")
4567 ("fr" :default
"Auteur")
4568 ("hu" :default
"Szerzõ")
4569 ("is" :html
"Höfundur")
4570 ("it" :default
"Autore")
4571 ("ja" :html
"著者" :utf-8
"著者")
4572 ("nl" :default
"Auteur")
4573 ("no" :default
"Forfatter")
4574 ("nb" :default
"Forfatter")
4575 ("nn" :default
"Forfattar")
4576 ("pl" :default
"Autor")
4577 ("ru" :html
"Автор" :utf-8
"Автор")
4578 ("sv" :html
"Författare")
4579 ("uk" :html
"Автор" :utf-8
"Автор")
4580 ("zh-CN" :html
"作者" :utf-8
"作者")
4581 ("zh-TW" :html
"作者" :utf-8
"作者"))
4583 ("ca" :default
"Data")
4584 ("cs" :default
"Datum")
4585 ("da" :default
"Dato")
4586 ("de" :default
"Datum")
4587 ("eo" :default
"Dato")
4588 ("es" :default
"Fecha")
4589 ("fi" :html
"Päivämäärä")
4590 ("hu" :html
"Dátum")
4591 ("is" :default
"Dagsetning")
4592 ("it" :default
"Data")
4593 ("ja" :html
"日付" :utf-8
"日付")
4594 ("nl" :default
"Datum")
4595 ("no" :default
"Dato")
4596 ("nb" :default
"Dato")
4597 ("nn" :default
"Dato")
4598 ("pl" :default
"Data")
4599 ("ru" :html
"Дата" :utf-8
"Дата")
4600 ("sv" :default
"Datum")
4601 ("uk" :html
"Дата" :utf-8
"Дата")
4602 ("zh-CN" :html
"日期" :utf-8
"日期")
4603 ("zh-TW" :html
"日期" :utf-8
"日期"))
4605 ("fr" :ascii
"Equation" :default
"Équation"))
4608 ("ca" :html
"Peus de pàgina")
4609 ("cs" :default
"Pozn\xe1mky pod carou")
4610 ("da" :default
"Fodnoter")
4611 ("de" :html
"Fußnoten")
4612 ("eo" :default
"Piednotoj")
4613 ("es" :html
"Pies de página")
4614 ("fi" :default
"Alaviitteet")
4615 ("fr" :default
"Notes de bas de page")
4616 ("hu" :html
"Lábjegyzet")
4617 ("is" :html
"Aftanmálsgreinar")
4618 ("it" :html
"Note a piè di pagina")
4619 ("ja" :html
"脚注" :utf-8
"脚注")
4620 ("nl" :default
"Voetnoten")
4621 ("no" :default
"Fotnoter")
4622 ("nb" :default
"Fotnoter")
4623 ("nn" :default
"Fotnotar")
4624 ("pl" :default
"Przypis")
4625 ("ru" :html
"Сноски" :utf-8
"Сноски")
4626 ("sv" :default
"Fotnoter")
4627 ("uk" :html
"Примітки"
4629 ("zh-CN" :html
"脚注" :utf-8
"脚注")
4630 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
4632 ("fr" :default
"Liste des programmes"))
4634 ("fr" :default
"Liste des tableaux"))
4637 :ascii
"Programme %d :" :default
"Programme nº %d :"
4638 :latin1
"Programme %d :"))
4641 :ascii
"Programme %d : %s" :default
"Programme nº %d : %s"
4642 :latin1
"Programme %d : %s"))
4644 ("fr" :default
"cf. section %s"))
4647 :ascii
"Tableau %d :" :default
"Tableau nº %d :" :latin1
"Tableau %d :"))
4650 :ascii
"Tableau %d : %s" :default
"Tableau nº %d : %s"
4651 :latin1
"Tableau %d : %s"))
4652 ("Table of Contents"
4653 ("ca" :html
"Índex")
4654 ("cs" :default
"Obsah")
4655 ("da" :default
"Indhold")
4656 ("de" :default
"Inhaltsverzeichnis")
4657 ("eo" :default
"Enhavo")
4658 ("es" :html
"Índice")
4659 ("fi" :html
"Sisällysluettelo")
4660 ("fr" :ascii
"Sommaire" :default
"Table des matières")
4661 ("hu" :html
"Tartalomjegyzék")
4662 ("is" :default
"Efnisyfirlit")
4663 ("it" :default
"Indice")
4664 ("ja" :html
"目次" :utf-8
"目次")
4665 ("nl" :default
"Inhoudsopgave")
4666 ("no" :default
"Innhold")
4667 ("nb" :default
"Innhold")
4668 ("nn" :default
"Innhald")
4669 ("pl" :html
"Spis treści")
4670 ("ru" :html
"Содержание"
4671 :utf-8
"Содержание")
4672 ("sv" :html
"Innehåll")
4673 ("uk" :html
"Зміст" :utf-8
"Зміст")
4674 ("zh-CN" :html
"目录" :utf-8
"目录")
4675 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
4676 ("Unknown reference"
4677 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")))
4678 "Dictionary for export engine.
4680 Alist whose CAR is the string to translate and CDR is an alist
4681 whose CAR is the language string and CDR is a plist whose
4682 properties are possible charsets and values translated terms.
4684 It is used as a database for `org-export-translate'. Since this
4685 function returns the string as-is if no translation was found,
4686 the variable only needs to record values different from the
4689 (defun org-export-translate (s encoding info
)
4690 "Translate string S according to language specification.
4692 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4693 and `:utf-8'. INFO is a plist used as a communication channel.
4695 Translation depends on `:language' property. Return the
4696 translated string. If no translation is found, try to fall back
4697 to `:default' encoding. If it fails, return S."
4698 (let* ((lang (plist-get info
:language
))
4699 (translations (cdr (assoc lang
4700 (cdr (assoc s org-export-dictionary
))))))
4701 (or (plist-get translations encoding
)
4702 (plist-get translations
:default
)
4709 ;; `org-export-dispatch' is the standard interactive way to start an
4710 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4711 ;; for its interface, which, in turn, delegates response to key
4712 ;; pressed to `org-export-dispatch-action'.
4714 (defvar org-export-dispatch-menu-entries nil
4715 "List of menu entries available for `org-export-dispatch'.
4716 This variable shouldn't be set directly. Set-up :menu-entry
4717 keyword in either `org-export-define-backend' or
4718 `org-export-define-derived-backend' instead.")
4721 (defun org-export-dispatch ()
4722 "Export dispatcher for Org mode.
4724 It provides an access to common export related tasks in a buffer.
4725 Its interface comes in two flavours: standard and expert. While
4726 both share the same set of bindings, only the former displays the
4727 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4728 to switch to one or the other."
4730 (let* ((input (save-window-excursion
4732 (org-export-dispatch-ui (list org-export-initial-scope
)
4734 org-export-dispatch-use-expert-ui
)
4735 (and (get-buffer "*Org Export Dispatcher*")
4736 (kill-buffer "*Org Export Dispatcher*")))))
4737 (action (car input
))
4738 (optns (cdr input
)))
4740 ;; First handle special hard-coded actions.
4741 (publish-current-file (org-e-publish-current-file (memq 'force optns
)))
4742 (publish-current-project
4743 (org-e-publish-current-project (memq 'force optns
)))
4744 (publish-choose-project
4745 (org-e-publish (assoc (org-icompleting-read
4747 org-e-publish-project-alist nil t
)
4748 org-e-publish-project-alist
)
4749 (memq 'force optns
)))
4750 (publish-all (org-e-publish-all (memq 'force optns
)))
4753 (memq 'subtree optns
)
4754 (memq 'visible optns
)
4755 (memq 'body optns
))))))
4757 (defun org-export-dispatch-ui (options first-key expertp
)
4758 "Handle interface for `org-export-dispatch'.
4760 OPTIONS is a list containing current interactive options set for
4761 export. It can contain any of the following symbols:
4762 `body' toggles a body-only export
4763 `subtree' restricts export to current subtree
4764 `visible' restricts export to visible part of buffer.
4765 `force' force publishing files.
4767 FIRST-KEY is the key pressed to select the first level menu. It
4768 is nil when this menu hasn't been selected yet.
4770 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4771 buffer is provided, but indications about currently active
4772 options are given in the prompt. Moreover, \[?] allows to switch
4773 back to standard interface."
4775 (lambda (key &optional access-key
)
4776 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4777 ;; non-nil is the required first-level key to activate
4778 ;; KEY. When its value is t, activate KEY independently
4779 ;; on the first key, if any. A nil value means KEY will
4780 ;; only be activated at first level.
4781 (if (or (eq access-key t
) (eq access-key first-key
))
4782 (org-add-props key nil
'face
'org-warning
)
4783 (org-no-properties key
))))
4784 ;; Make sure order of menu doesn't depend on the order in
4785 ;; which back-ends are loaded.
4786 (backends (sort (copy-sequence org-export-dispatch-menu-entries
)
4787 (lambda (a b
) (< (car a
) (car b
)))))
4788 ;; Compute a list of allowed keys based on the first key
4789 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4790 ;; always available.
4792 (nconc (list ?
1 ?
2 ?
3 ?
4)
4794 (if (not first-key
) backends
4795 (nth 2 (assq first-key backends
))))
4796 (cond ((eq first-key ?P
) (list ?f ?p ?x ?a
))
4797 ((not first-key
) (list ?P
)))
4798 (when expertp
(list ??
))
4800 ;; Build the help menu for standard UI.
4804 ;; Options are hard-coded.
4806 [%s] Body only: %s [%s] Visible only: %s
4807 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4808 (funcall fontify-key
"1" t
)
4809 (if (memq 'body options
) "On " "Off")
4810 (funcall fontify-key
"2" t
)
4811 (if (memq 'visible options
) "On " "Off")
4812 (funcall fontify-key
"3" t
)
4813 (if (memq 'subtree options
) "Subtree" "Buffer ")
4814 (funcall fontify-key
"4" t
)
4815 (if (memq 'force options
) "On " "Off"))
4816 ;; Display registered back-end entries.
4819 (let ((top-key (car entry
)))
4822 (funcall fontify-key
(char-to-string top-key
))
4824 (let ((sub-menu (nth 2 entry
)))
4825 (unless (functionp sub-menu
)
4826 ;; Split sub-menu into two columns.
4832 (format (if (zerop (mod index
2)) " [%s] %-24s"
4834 (funcall fontify-key
4835 (char-to-string (car sub-entry
))
4839 (when (zerop (mod index
2)) "\n"))))))))
4841 ;; Publishing menu is hard-coded.
4842 (format "\n[%s] Publish
4843 [%s] Current file [%s] Current project
4844 [%s] Choose project [%s] All projects\n\n"
4845 (funcall fontify-key
"P")
4846 (funcall fontify-key
"f" ?P
)
4847 (funcall fontify-key
"p" ?P
)
4848 (funcall fontify-key
"x" ?P
)
4849 (funcall fontify-key
"a" ?P
))
4851 (funcall fontify-key
"q" t
)
4852 (if first-key
"Main menu" "Exit")))))
4853 ;; Build prompts for both standard and expert UI.
4854 (standard-prompt (unless expertp
"Export command: "))
4858 "Export command (Options: %s%s%s%s) [%s]: "
4859 (if (memq 'body options
) (funcall fontify-key
"b" t
) "-")
4860 (if (memq 'visible options
) (funcall fontify-key
"v" t
) "-")
4861 (if (memq 'subtree options
) (funcall fontify-key
"s" t
) "-")
4862 (if (memq 'force options
) (funcall fontify-key
"f" t
) "-")
4863 (concat allowed-keys
)))))
4864 ;; With expert UI, just read key with a fancy prompt. In standard
4865 ;; UI, display an intrusive help buffer.
4867 (org-export-dispatch-action
4868 expert-prompt allowed-keys backends options first-key expertp
)
4869 ;; At first call, create frame layout in order to display menu.
4870 (unless (get-buffer "*Org Export Dispatcher*")
4871 (delete-other-windows)
4872 (org-switch-to-buffer-other-window
4873 (get-buffer-create "*Org Export Dispatcher*"))
4874 (setq cursor-type nil
))
4875 ;; At this point, the buffer containing the menu exists and is
4876 ;; visible in the current window. So, refresh it.
4877 (with-current-buffer "*Org Export Dispatcher*"
4880 (org-fit-window-to-buffer)
4881 (org-export-dispatch-action
4882 standard-prompt allowed-keys backends options first-key expertp
))))
4884 (defun org-export-dispatch-action
4885 (prompt allowed-keys backends options first-key expertp
)
4886 "Read a character from command input and act accordingly.
4888 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4889 a list of characters available at a given step in the process.
4890 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4891 EXPERTP are the same as defined in `org-export-dispatch-ui',
4894 Toggle export options when required. Otherwise, return value is
4895 a list with action as CAR and a list of interactive export
4897 (let ((key (let ((k (read-char-exclusive prompt
)))
4898 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4899 ;; depending on user's key pressed.
4900 (if (< k
27) (+ k
96) k
))))
4902 ;; Ignore non-standard characters (i.e. "M-a") and
4903 ;; undefined associations.
4904 ((not (memq key allowed-keys
))
4906 (unless expertp
(message "Invalid key") (sit-for 1))
4907 (org-export-dispatch-ui options first-key expertp
))
4908 ;; q key at first level aborts export. At second
4909 ;; level, cancel first key instead.
4910 ((eq key ?q
) (if (not first-key
) (error "Export aborted")
4911 (org-export-dispatch-ui options nil expertp
)))
4912 ;; Help key: Switch back to standard interface if
4913 ;; expert UI was active.
4914 ((eq key ??
) (org-export-dispatch-ui options first-key nil
))
4915 ;; Toggle export options.
4916 ((memq key
'(?
1 ?
2 ?
3 ?
4))
4917 (org-export-dispatch-ui
4918 (let ((option (case key
(?
1 'body
) (?
2 'visible
) (?
3 'subtree
)
4920 (if (memq option options
) (remq option options
)
4921 (cons option options
)))
4923 ;; Action selected: Send key and options back to
4924 ;; `org-export-dispatch'.
4926 (and (eq first-key ?P
) (memq key
'(?f ?p ?x ?a
)))
4927 (functionp (nth 2 (assq key backends
))))
4929 ((not first-key
) (nth 2 (assq key backends
)))
4930 ;; Publishing actions are hard-coded. Send a special
4931 ;; signal to `org-export-dispatch'.
4934 (?f
'publish-current-file
)
4935 (?p
'publish-current-project
)
4936 (?x
'publish-choose-project
)
4938 (t (nth 2 (assq key
(nth 2 (assq first-key backends
))))))
4940 ;; Otherwise, enter sub-menu.
4941 (t (org-export-dispatch-ui options key expertp
)))))
4944 (provide 'org-export
)
4945 ;;; org-export.el ends here